Repository navigation
Expand file tree
/
Copy pathtemplate.yaml
More file actions
1858 lines (1799 loc) · 61.5 KB
/
Copy pathtemplate.yaml
File metadata and controls
1858 lines (1799 loc) · 61.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Wallet Attached Storage API (WAS v0.5)
Parameters:
# Base URL that provisioned space URLs are built from; a space's registry
# row is keyed {SpaceBaseUrl}/{bucket name}.
SpaceBaseUrl:
Type: String
Default: https://was.lcw-sandbox.org/space
# The identity domain did:webvh DIDs of this deployment's accounts name
# (did:webvh:{scid}:{IdDomain}:{spaceId}). A log at that location is the
# `id/did.jsonl` resource of the named Space, read from its bucket; any
# other did:webvh is fetched over HTTPS.
IdDomain:
Type: String
Default: id.lcw-sandbox.org
# The identity domain is served by a CloudFront distribution in front of
# the API's own domain: /{spaceId}/did.jsonl and /{spaceId}/did.json are
# rewritten onto the Space's `id` collection. The certificate must cover
# IdDomain and live in us-east-1; the zone takes the alias records.
ApiDomain:
Type: String
Description: The API's public host name, the distribution's origin.
Default: was.lcw-sandbox.org
IdCertificateArn:
Type: String
Description: ACM certificate (us-east-1) covering IdDomain.
Default: arn:aws:acm:us-east-1:747170788015:certificate/c63f6ba4-2630-4c4f-973f-4fd6436962f0
HostedZoneId:
Type: String
Description: Route 53 hosted zone the IdDomain alias records go in.
Default: Z04250862QG6MQUAE24XV
OpenProvisioning:
Type: String
Description: >-
"true" lets any did:key create a Space by id with PUT /space/{id}/meta
and If-None-Match: * (the reference client's create-by-id, which a
FreeWallet account needs), within MaxSpacesPerController; "false" keeps
Space creation to POST /spaces/ with a coupon, the Spaces created
under such a Space, and the unlock Spaces.
Default: "false"
AllowedValues: ["true", "false"]
MaxSpacesPerController:
Type: String
Description: How many Spaces one did:key may create by id under open provisioning.
Default: "20"
# The limit on the Spaces of one account: how many Spaces created under
# an account Space (POST /space/{s}/spaces) may exist at once, counted on
# the account Space's registry row. "0" closes that route. See README,
# "The limit on the Spaces of one account".
MaxSpacesPerAccount:
Type: String
Description: How many Spaces created under one account Space may exist at once; "0" closes the route.
Default: "100"
# The cap on Spaces (and so S3 buckets) created without a coupon: unlock
# Spaces, whose derived id any key holder can claim, and Spaces created
# by id under open provisioning. Spaces created with a coupon, and the
# Spaces created under one of those, never count. "0" closes both paths.
# See README, "The cap on Spaces created without a coupon".
MaxCouponlessSpaces:
Type: String
Description: How many Spaces may exist that were created without a coupon; "0" closes those paths.
Default: "200"
CouponlessAlarmThreshold:
Type: String
Description: The CouponlessSpaces count at or above which the stack's alarm fires.
Default: "160"
SpaceBucketsAlarmThreshold:
Type: String
Description: The number of dcc-pws-* buckets at or above which the stack's alarm fires.
Default: "300"
AlertEmail:
Type: String
Description: An address subscribed to the quota alarms' topic (empty for none; the subscription must be confirmed by email).
Default: ""
Conditions:
HasAlertEmail: !Not [!Equals [!Ref AlertEmail, ""]]
Globals:
Function:
Runtime: nodejs24.x
Timeout: 10
Layers:
- !Ref WasLibLayer
Environment:
Variables:
# The spaces registry (PwsSpacesTable below).
SPACES_TABLE_NAME: !Ref PwsSpacesTable
COUPONS_TABLE_NAME: pws-coupons
SPACE_URL_BASE: !Ref SpaceBaseUrl
ID_DOMAIN: !Ref IdDomain
# The largest did:webvh log the resolver fetches (was-lib/webvh.mjs;
# 8 MiB). Every entry of an account's log carries the whole account
# document, so a long-lived account's log runs to megabytes.
WEBVH_MAX_LOG_BYTES: "8388608"
INSTANCE_VERSION: '0.5.0'
FEED_TABLE_NAME: !Ref PwsFeedTable
FEED_QUEUE_URL: !Ref FeedQueue
API_HOST: !Ref ApiDomain
OPEN_PROVISIONING: !Ref OpenProvisioning
MAX_SPACES_PER_CONTROLLER: !Ref MaxSpacesPerController
MAX_COUPONLESS_SPACES: !Ref MaxCouponlessSpaces
MAX_SPACES_PER_ACCOUNT: !Ref MaxSpacesPerAccount
Resources:
# Space-creation coupons, owned by this stack: one row per coupon string.
# Optional attributes: usesRemaining (N; absent = unlimited) and expiresAt
# (ISO timestamp; absent = never). POST /spaces/ redeems a coupon from here,
# once per account: the Space it creates is the account Space, and the
# account's further Spaces are created under it with no coupon;
# whoever administers the deployment mints rows (the wallet back end seeds
# its registration code as an unlimited coupon).
PwsCouponsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: pws-coupons
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: coupon
AttributeType: S
KeySchema:
- AttributeName: coupon
KeyType: HASH
# The spaces registry: one row per Space, keyed by its URL (no trailing
# slash), carrying the controller DID (`did`), the type array, the name and
# the creation time, with a by-did index for the listing. The authorizer
# reads it on every request and the Space functions write it. Nothing else
# should: lcw-back-end's registration and login still use it by name until
# stage 3 of the did:webvh work removes that.
# The `changes` feed (was-lib/feed.mjs): one row per record of a
# Collection at the position of its latest write, plus a counter row per
# Collection. Fed by the Space buckets' object events through EventBridge
# and a FIFO queue (one message group, so positions are assigned in
# order), read by the query function through the by-position index.
PwsFeedTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: pws-feed
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: bucket
AttributeType: S
- AttributeName: record
AttributeType: S
- AttributeName: feed
AttributeType: S
- AttributeName: position
AttributeType: N
KeySchema:
- AttributeName: bucket
KeyType: HASH
- AttributeName: record
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: by-position
KeySchema:
- AttributeName: feed
KeyType: HASH
- AttributeName: position
KeyType: RANGE
Projection:
ProjectionType: ALL
FeedDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: pws-feed-dead-letters.fifo
FifoQueue: true
ContentBasedDeduplication: true
MessageRetentionPeriod: 1209600
FeedQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: pws-feed.fifo
FifoQueue: true
ContentBasedDeduplication: true
VisibilityTimeout: 120
RedrivePolicy:
deadLetterTargetArn: !GetAtt FeedDeadLetterQueue.Arn
maxReceiveCount: 5
FeedQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref FeedQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sqs:SendMessage
Resource: !GetAtt FeedQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !GetAtt FeedRule.Arn
# Every object created or deleted in a Space bucket (the buckets publish
# to EventBridge; src/feed/sweep enables it on buckets that predate this).
FeedRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.s3
detail-type:
- Object Created
- Object Deleted
detail:
bucket:
name:
- prefix: dcc-pws-
Targets:
- Id: feed-queue
Arn: !GetAtt FeedQueue.Arn
SqsParameters:
MessageGroupId: feed
FeedConsumerFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/feed/consumer
Handler: app.lambdaHandler
Timeout: 60
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref PwsFeedTable
- Statement:
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::dcc-pws-*/*
Events:
Feed:
Type: SQS
Properties:
Queue: !GetAtt FeedQueue.Arn
BatchSize: 10
FunctionResponseTypes:
- ReportBatchItemFailures
# Repairs feeds from the buckets' contents and enables event publishing on
# older buckets. Invoked by hand (`aws lambda invoke`) or on a schedule.
FeedSweepFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/feed/sweep
Handler: app.lambdaHandler
Timeout: 600
MemorySize: 512
Policies:
- DynamoDBReadPolicy:
TableName: !Ref PwsFeedTable
- DynamoDBReadPolicy:
TableName: !Ref PwsSpacesTable
- Statement:
# A whole-registry sweep reconciles the coupon-less counter and
# each account's count of the Spaces under it, and backfills
# `provisioning` on older rows
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource: !GetAtt PwsSpacesTable.Arn
- Effect: Allow
Action:
- s3:ListBucket
- s3:PutBucketNotification
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::dcc-pws-*/*
- Effect: Allow
Action: sqs:SendMessage
Resource: !GetAtt FeedQueue.Arn
# ---------------------------------------------------------------------------
# The cap on Spaces created without a coupon (README, "The cap on Spaces
# created without a coupon"): the counter lives in the registry table;
# this function publishes it and the bucket count as metrics every hour,
# and the two alarms below notify the topic.
# ---------------------------------------------------------------------------
QuotaWatchFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/quota/watch
Handler: app.lambdaHandler
Timeout: 60
Policies:
- DynamoDBReadPolicy:
TableName: !Ref PwsSpacesTable
- Statement:
- Effect: Allow
Action: s3:ListAllMyBuckets
Resource: "*"
- Effect: Allow
Action: cloudwatch:PutMetricData
Resource: "*"
Condition:
StringEquals:
cloudwatch:namespace: PwsServer
Events:
Hourly:
Type: Schedule
Properties:
Schedule: rate(1 hour)
QuotaTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: pws-server-quota-alerts
QuotaEmailSubscription:
Type: AWS::SNS::Subscription
Condition: HasAlertEmail
Properties:
TopicArn: !Ref QuotaTopic
Protocol: email
Endpoint: !Ref AlertEmail
CouponlessSpacesAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: pws-server-couponless-spaces
AlarmDescription: Spaces created without a coupon are approaching the cap (MaxCouponlessSpaces).
Namespace: PwsServer
MetricName: CouponlessSpaces
Statistic: Maximum
Period: 3600
EvaluationPeriods: 1
Threshold: !Ref CouponlessAlarmThreshold
ComparisonOperator: GreaterThanOrEqualToThreshold
TreatMissingData: notBreaching
AlarmActions:
- !Ref QuotaTopic
SpaceBucketsAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: pws-server-space-buckets
AlarmDescription: The number of dcc-pws-* buckets is high.
Namespace: PwsServer
MetricName: SpaceBuckets
Statistic: Maximum
Period: 3600
EvaluationPeriods: 1
Threshold: !Ref SpaceBucketsAlarmThreshold
ComparisonOperator: GreaterThanOrEqualToThreshold
TreatMissingData: notBreaching
AlarmActions:
- !Ref QuotaTopic
PwsSpacesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: pws-spaces
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: spaceURL
AttributeType: S
- AttributeName: did
AttributeType: S
KeySchema:
- AttributeName: spaceURL
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: by-did
KeySchema:
- AttributeName: did
KeyType: HASH
Projection:
ProjectionType: ALL
# The code every endpoint function shares (layers/was-lib, resolved as
# `was-lib/...` from /opt/nodejs/node_modules). Plain ES modules with no
# dependency beyond the AWS SDK the runtime provides, so the layer is
# packaged as-is. The layer grants nothing: each function's role bounds
# what the shared code may do when running in that function.
WasLibLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: was-lib
ContentUri: layers/was-lib
CompatibleRuntimes:
- nodejs24.x
RetentionPolicy: Delete
# ---------------------------------------------------------------------------
# The identity domain: did:webvh:{scid}:{IdDomain}:{spaceId} resolves by
# fetching https://{IdDomain}/{spaceId}/did.jsonl, which this distribution
# rewrites onto the Space's world-readable `id` collection on the API. A
# DID therefore names the identity domain, not the storage host, and if
# the Space moves the distribution's origin (or a redirect here) follows
# it. Nothing is cached: a resolver must see the log as it is now.
IdRewriteFunction:
Type: AWS::CloudFront::Function
Properties:
Name: !Sub ${AWS::StackName}-id-rewrite
AutoPublish: true
FunctionConfig:
Comment: Rewrites /{spaceId}/did.jsonl onto /space/{spaceId}/id/did.jsonl
Runtime: cloudfront-js-2.0
FunctionCode: |
function handler(event) {
var request = event.request;
var match = request.uri.match(/^\/([A-Za-z0-9_-]+)\/(did\.jsonl|did\.json)$/);
if (!match) {
return {
statusCode: 404,
statusDescription: 'Not Found',
headers: { 'content-type': { value: 'text/plain' } },
body: 'Not found'
};
}
request.uri = '/space/' + match[1] + '/id/' + match[2];
return request;
}
IdDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Comment: !Sub did:webvh identity domain for ${AWS::StackName}
Aliases:
- !Ref IdDomain
HttpVersion: http2
PriceClass: PriceClass_100
Origins:
- Id: api
DomainName: !Ref ApiDomain
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
DefaultCacheBehavior:
TargetOriginId: api
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
# CachingDisabled: every resolution reads the log as it stands.
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
# AllViewerExceptHostHeader: the Origin header reaches the API so
# its CORS answers browsers resolving a DID.
OriginRequestPolicyId: b689b0a8-53d0-40ab-baf2-68738e2966ac
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt IdRewriteFunction.FunctionMetadata.FunctionARN
ViewerCertificate:
AcmCertificateArn: !Ref IdCertificateArn
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
IdDnsA:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Name: !Ref IdDomain
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt IdDistribution.DomainName
IdDnsAAAA:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Name: !Ref IdDomain
Type: AAAA
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt IdDistribution.DomainName
# An HTTP API rather than a REST API: its CORS is a gateway feature that
# injects the headers into every response, and its $default stage serves
# at the root, so URLs carry no stage prefix.
WASHttpApi:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: WASZcapAuthorizer
Authorizers:
WASZcapAuthorizer:
AuthorizerPayloadFormatVersion: '2.0'
EnableSimpleResponses: true
EnableFunctionDefaultPermissions: true
FunctionArn: !GetAtt WASZcapAuthorizerFn.Arn
# No Identity.Headers: an identity source would make API Gateway
# answer unsigned requests itself with a 401, and unsigned GETs
# must reach the functions for the public-read policy check.
Identity:
# Every zcap invocation is signed over its own request, so a
# cached authorization result would be meaningless. 0 disables
# the authorizer result cache.
ReauthorizeEvery: 0
CorsConfiguration:
AllowMethods:
- GET
- HEAD
- POST
- PUT
- DELETE
- OPTIONS
# '*' covers the signed headers (authorization, capability-invocation,
# digest, date, if-match, key-epoch, ...) and anything a client adds.
AllowHeaders:
- '*'
AllowOrigins:
- '*'
# A browser client needs the validators and the discovery link.
ExposeHeaders:
- ETag
- Location
- Link
- Last-Modified
- Content-Type
MaxAge: 600
# Verifies the capability invocation of every signed request and reports
# the result in the request context (signed, verified, controller,
# delegated, denial, spaceController, spaceType, or space=missing). It
# never denies: a gateway denial is always a 403, and the spec's answers
# to an unauthorized request are shaped by the endpoint functions. A
# delegated chain also passes the chain inspectors: the Space's
# revocation records and the client-annex clause.
WASZcapAuthorizerFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/authorizer
Handler: app.lambdaHandler
# Resolving a did:webvh controller means reading and verifying its
# log, which a signed request can make the authorizer do once per
# cache lifetime.
Timeout: 10
Policies:
- DynamoDBReadPolicy:
TableName: !Ref PwsSpacesTable
- Statement:
# The DID logs of this deployment's accounts, read from their
# Spaces' buckets (ListBucket on the one key so a missing log is
# a 404 rather than a 403); the revocation records a delegated
# chain is checked against; the Space Metadata the client-annex
# clause reads a Space's type and controller from.
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
Condition:
StringLike:
s3:prefix:
- collections/*/did.jsonl
- revocations/*
- meta/space.json
- Effect: Allow
Action: s3:GetObject
Resource:
- arn:aws:s3:::dcc-pws-*/collections/*/did.jsonl
- arn:aws:s3:::dcc-pws-*/revocations/*
- arn:aws:s3:::dcc-pws-*/meta/space.json
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2022"
Format: "esm"
Platform: "node"
# The verification stack is bundled; the shared layer and the SDK
# are provided at run time.
External:
- "@aws-sdk/*"
- "was-lib/*"
OutExtension:
- .js=.mjs
EntryPoints:
- app.mjs
# createRequire lets the bundled CJS dependencies work inside an ESM
# output.
Banner:
- js=import { createRequire } from 'module'; const require = createRequire(import.meta.url);
# POST /space/{s}/zcaps/revocations/{id} -- revoke a delegated capability
# rooted in the Space. It verifies the capability chain in the request
# body, which needs the authorizer's verification stack, so it is built
# from the same directory with its own entry point; the gateway
# authorizer does not run for the route (the function verifies the
# invocation itself, under the dual-root rule).
SpaceRevocationPostFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/authorizer
Handler: revoke.lambdaHandler
Timeout: 10
Policies:
- DynamoDBReadPolicy:
TableName: !Ref PwsSpacesTable
- Statement:
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
Condition:
StringLike:
s3:prefix:
- collections/*/did.jsonl
- revocations/*
- meta/space.json
- Effect: Allow
Action: s3:GetObject
Resource:
- arn:aws:s3:::dcc-pws-*/collections/*/did.jsonl
- arn:aws:s3:::dcc-pws-*/revocations/*
- arn:aws:s3:::dcc-pws-*/meta/space.json
- Effect: Allow
Action: s3:PutObject
Resource: arn:aws:s3:::dcc-pws-*/revocations/*
Events:
Revoke:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/zcaps/revocations/{revocation_id}
Method: post
Auth:
Authorizer: NONE
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2022"
Format: "esm"
Platform: "node"
External:
- "@aws-sdk/*"
- "was-lib/*"
OutExtension:
- .js=.mjs
EntryPoints:
- revoke.mjs
Banner:
- js=import { createRequire } from 'module'; const require = createRequire(import.meta.url);
# ---------------------------------------------------------------------------
# One function per endpoint. Each carries only the actions and key prefixes
# its operation touches: a read function cannot write or delete, a
# function on one prefix (meta/, policies/, collections/) cannot reach
# another, and only the two Space-level functions can create or delete a
# bucket or a registry row. Functions that serve unsigned public reads also
# read policies/* for the PublicCanRead cascade.
# ---------------------------------------------------------------------------
# GET /service and GET / -- the service description. No authorization, no
# permissions. /service is where a client built on a base server URL reads
# it (pws-client's server layout); / keeps serving it for clients that
# follow the Link header from the root.
ServiceGetFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/service/get
Handler: app.lambdaHandler
Events:
Get:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /
Method: get
Auth:
Authorizer: NONE
Head:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /
Method: head
Auth:
Authorizer: NONE
GetService:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /service
Method: get
Auth:
Authorizer: NONE
HeadService:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /service
Method: head
Auth:
Authorizer: NONE
# POST /spaces/ -- provision an account Space: a bucket with its metadata
# object, a registry row, and a redeemed coupon. The further Spaces of the
# account are created under it (SpaceSpacesPostFn), with no coupon.
SpacesPostFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/spaces/post
Handler: app.lambdaHandler
Policies:
- DynamoDBWritePolicy:
TableName: !Ref PwsSpacesTable
- DynamoDBCrudPolicy:
TableName: !Ref PwsCouponsTable
- Statement:
# A client-chosen id is checked against the registry before the
# coupon is spent
- Effect: Allow
Action: dynamodb:GetItem
Resource: !GetAtt PwsSpacesTable.Arn
- Effect: Allow
Action:
- s3:CreateBucket
- s3:PutBucketNotification
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:PutObject
Resource: arn:aws:s3:::dcc-pws-*/meta/space.json
Events:
Post:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces
Method: post
PostSlash:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces/{p}
Method: post
# GET /spaces/ -- the signer's Spaces, from the registry's by-did index.
SpacesGetFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/spaces/get
Handler: app.lambdaHandler
Policies:
- DynamoDBReadPolicy:
TableName: !Ref PwsSpacesTable
Events:
Get:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces
Method: get
GetSlash:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces/{p}
Method: get
# was-client 0.96+ is built with the Spaces Repository URL and probes
# it with a HEAD for the service link before its first request.
Head:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces
Method: head
HeadSlash:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /spaces/{p}
Method: head
# POST /space/{s}/spaces -- create a further Space under an account Space
# (one a coupon was spent on), authorized by a capability on that Space
# and no coupon, within MaxSpacesPerAccount (the write policy's UpdateItem
# keeps the count on the account Space's row). The literal route wins over
# the {collection_id} one.
SpaceSpacesPostFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/spaces-post
Handler: app.lambdaHandler
Policies:
- DynamoDBWritePolicy:
TableName: !Ref PwsSpacesTable
- Statement:
# The account Space's row, and a client-chosen id checked
# against the registry
- Effect: Allow
Action: dynamodb:GetItem
Resource: !GetAtt PwsSpacesTable.Arn
- Effect: Allow
Action:
- s3:CreateBucket
- s3:PutBucketNotification
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:PutObject
Resource: arn:aws:s3:::dcc-pws-*/meta/space.json
Events:
Post:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/spaces
Method: post
# GET /space/{s}/ -- the Space's Collections. The gateway delivers the
# trailing-slash container URL to the {collection_id} route with an empty
# id; a non-empty id (the bare Collection URL) is answered with a 308.
SpaceCollectionsGetFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/collections-get
Handler: app.lambdaHandler
Policies:
- Statement:
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:GetObject
Resource:
- arn:aws:s3:::dcc-pws-*/meta/*
- arn:aws:s3:::dcc-pws-*/policies/*
Events:
Get:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/{collection_id}
Method: get
Head:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/{collection_id}
Method: head
# POST /space/{s}/ -- create a Collection (its metadata object).
CollectionCreateFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/collection-create
Handler: app.lambdaHandler
Policies:
- Statement:
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource: arn:aws:s3:::dcc-pws-*/meta/*
Events:
Post:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/{collection_id}
Method: post
# DELETE /space/{s}/ -- the whole Space: empty and remove the bucket, then
# the registry row. The only function that can delete a bucket.
SpaceDeleteFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/delete
Handler: app.lambdaHandler
# Emptying a bucket can outlast the default.
Timeout: 60
Policies:
- Statement:
# GetItem reads how the Space was provisioned; UpdateItem gives a
# coupon-less Space's slot back to the cap's counter, and the
# place of a Space created under an account back to that account
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:DeleteItem
- dynamodb:UpdateItem
Resource: !GetAtt PwsSpacesTable.Arn
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:DeleteItem
Resource: !GetAtt PwsFeedTable.Arn
- Effect: Allow
Action:
- s3:ListBucket
- s3:DeleteBucket
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action: s3:DeleteObject
Resource: arn:aws:s3:::dcc-pws-*/*
Events:
Delete:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/{collection_id}
Method: delete
# GET /space/{s}/meta
SpaceMetaGetFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/meta-get
Handler: app.lambdaHandler
Policies:
- Statement:
# Without ListBucket, S3 answers a HEAD/GET of a missing key with
# 403 rather than 404; the prefix condition keeps it to this
# function's own keys.
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
Condition:
StringLike:
s3:prefix:
- meta/space.json
- policies/space.json
- Statement:
- Effect: Allow
Action: s3:GetObject
Resource:
- arn:aws:s3:::dcc-pws-*/meta/space.json
- arn:aws:s3:::dcc-pws-*/policies/space.json
Events:
Get:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/meta
Method: get
Head:
Type: HttpApi
Properties:
ApiId: !Ref WASHttpApi
Path: /space/{space_id}/meta
Method: head
# PUT /space/{s}/meta -- also mirrors the name to the registry row.
SpaceMetaPutFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/space/meta-put
Handler: app.lambdaHandler
# Promoting a controller resolves the did:webvh's log.
Timeout: 10
Policies:
- Statement:
# Without ListBucket, S3 answers a HEAD/GET of a missing key with
# 403 rather than 404; the prefix condition keeps it to this
# function's own keys (the metadata object, and the DID log a
# promotion resolves).
- Effect: Allow
Action: s3:ListBucket
Resource: arn:aws:s3:::dcc-pws-*
Condition:
StringLike:
s3:prefix:
- meta/space.json
- collections/*/did.jsonl
- Statement:
# UpdateItem mirrors the name and records a promoted controller;
# PutItem and CreateBucket provision an unlock Space.
- Effect: Allow
Action:
- dynamodb:UpdateItem
- dynamodb:PutItem
Resource: !GetAtt PwsSpacesTable.Arn
# The per-controller cap of open provisioning counts by DID
- Effect: Allow
Action: dynamodb:Query
Resource: !Sub "${PwsSpacesTable.Arn}/index/by-did"
- Effect: Allow
Action:
- s3:CreateBucket
- s3:PutBucketNotification
Resource: arn:aws:s3:::dcc-pws-*
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource: arn:aws:s3:::dcc-pws-*/meta/space.json
# A promotion resolves the did:webvh's log, in `id` or, for the
# self-hosted form, in the Collection the DID names
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::dcc-pws-*/collections/*/did.jsonl
Events:
Put: