saaslayer 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +195 -8
  2. package/bin/saaslayer.js +454 -38
  3. package/package.json +19 -4
  4. package/src/api-client.js +91 -0
  5. package/src/aws-sigv4.js +47 -0
  6. package/src/commands/access-admins.js +73 -0
  7. package/src/commands/accounts.js +54 -0
  8. package/src/commands/activity.js +33 -0
  9. package/src/commands/aws.js +132 -0
  10. package/src/commands/catalog.js +246 -0
  11. package/src/commands/context.js +64 -0
  12. package/src/commands/deploy.js +118 -0
  13. package/src/commands/first-admin.js +42 -0
  14. package/src/commands/foundation.js +405 -0
  15. package/src/commands/lifecycle.js +124 -0
  16. package/src/commands/login.js +261 -0
  17. package/src/commands/machine-trust.js +96 -0
  18. package/src/commands/migrate.js +290 -0
  19. package/src/commands/operations.js +140 -0
  20. package/src/commands/orgs.js +81 -0
  21. package/src/commands/permissions-inspect.js +228 -0
  22. package/src/commands/release-manifest.js +138 -0
  23. package/src/commands/service-principals.js +81 -0
  24. package/src/commands/settings.js +140 -0
  25. package/src/commands/stacks.js +22 -0
  26. package/src/commands/users.js +269 -0
  27. package/src/config.js +49 -0
  28. package/src/context-store.js +37 -0
  29. package/src/credentials.js +285 -0
  30. package/src/deployment-api-auth.js +61 -0
  31. package/src/deployment-auth.js +38 -0
  32. package/src/deployments.js +26 -0
  33. package/src/discovery.js +66 -0
  34. package/src/env-compare.js +143 -0
  35. package/src/foundation-template.js +57 -0
  36. package/src/iam-session.js +67 -0
  37. package/src/migrations-lib.js +15 -0
  38. package/src/module-catalog.js +66 -0
  39. package/src/permissions-request.js +43 -0
  40. package/src/prompt.js +19 -0
  41. package/src/release-manifest.js +334 -0
  42. package/src/scope.js +114 -0
  43. package/src/settings.js +272 -0
  44. package/src/users-api.js +165 -0
  45. package/src/users-import/ids.js +24 -0
  46. package/src/users-import/import.js +292 -0
  47. package/src/users-import/manifest.js +90 -0
  48. package/src/users-import/providers/cognito.js +61 -0
  49. package/src/users-import/providers/index.js +24 -0
  50. package/templates/saaslayer-foundation-region.yml +109 -0
  51. package/templates/{host-account.yml → saaslayer-foundation.yml} +381 -87
@@ -1,6 +1,13 @@
1
1
  # SaaSLayer host-account bootstrap. Installed once per host (customer) account; grants the
2
2
  # SaaSLayer control plane the minimum access needed to deploy and support SaaSLayer modules.
3
3
  #
4
+ # SOURCE OF TRUTH — edit THIS file only. packages/cli/templates/saaslayer-foundation.yml is an
5
+ # auto-synced copy (run `npm run sync-template` from packages/cli). `npm test` fails on drift.
6
+ #
7
+ # NAMING (tbrme.13.19): the CloudFormation LOGICAL ids below stay SaaSLayer* (internal, unchanged), while the
8
+ # physical RoleName/ManagedPolicyName values are now sl-foundation-{deploy-role,cfn-exec-role,support-ro-role,
9
+ # managed-boundary} (+ saaslayer-org-vending-role in account-vending-access). Prose below names the roles by logical id.
10
+ #
4
11
  # SECURITY REVIEWER SUMMARY
5
12
  #
6
13
  # This template creates one S3 bucket, one permissions-boundary managed policy, and three IAM roles:
@@ -10,7 +17,9 @@
10
17
  # ExternalId. It can drive the CloudFormation lifecycle of stacks whose names start with
11
18
  # StackNamePrefix (default "sl-"), read/write the deployment artifact buckets, and pass exactly
12
19
  # one role (SaaSLayerCloudFormationExecutionRole) to CloudFormation. It cannot create IAM
13
- # resources or touch anything outside the managed stack namespace directly.
20
+ # resources or touch anything outside the managed stack namespace directly. Its only
21
+ # sts:AssumeRole grant is into a module stack's own data-migration role
22
+ # (role/${StackNamePrefix}*-migrate), whose policy limits it to that service's tables.
14
23
  #
15
24
  # 2. SaaSLayerCloudFormationExecutionRole — assumable ONLY by the CloudFormation service, never
16
25
  # cross-account. CloudFormation uses it to create/update/delete the resources declared by
@@ -38,11 +47,9 @@
38
47
  # support role, external-id enforcement, wildcard justification audit).
39
48
  AWSTemplateFormatVersion: '2010-09-09'
40
49
  Description: >-
41
- SaaSLayer host-account bootstrap: deployment artifact bucket, CloudFormation execution role for
42
- approved SaaSLayer module templates, permissions boundary for module-created IAM roles, a
43
- cross-account deployment role restricted to SaaSLayer-prefixed stacks, and a cross-account
44
- read-only support role. Both cross-account roles require an external ID and trust only the
45
- SaaSLayer control-plane account supplied as a parameter.
50
+ SaaSLayer foundation stack: grants the SaaSLayer control plane scoped, external-ID-gated access
51
+ to deploy and support SaaSLayer modules in this account. Installed once per account. Details:
52
+ https://saaslayer.com/docs/account-access
46
53
 
47
54
  Parameters:
48
55
  ControlPlaneAccountId:
@@ -63,6 +70,12 @@ Parameters:
63
70
  Description: >-
64
71
  Prefix for SaaSLayer-managed CloudFormation stacks. The deployment role can only operate on
65
72
  stacks whose names start with this prefix.
73
+ OrgId:
74
+ Type: String
75
+ Default: ''
76
+ Description: >-
77
+ SaaSLayer org this account is connected to. Informational only — never used in a policy or
78
+ trust decision. Surfaced in Outputs so support can identify the account from the stack alone.
66
79
  EnableEmailInbound:
67
80
  Type: String
68
81
  AllowedValues: ['true', 'false']
@@ -70,27 +83,43 @@ Parameters:
70
83
  Description: >-
71
84
  When 'true', grant the SES receipt-rule provisioning + inbound-mail runtime permissions that
72
85
  email-inbound-service needs. Default 'false' so host accounts that do NOT run email-inbound are
73
- not over-permissioned for SES. The deployer sets this per host based on the services it hosts.
86
+ not over-permissioned for SES. Set per host via `cplane accounts enable-capability <host> ses`.
87
+ EnableTenantEdge:
88
+ Type: String
89
+ AllowedValues: ['true', 'false']
90
+ Default: 'false'
91
+ Description: >-
92
+ When 'true', grant the CloudFront distribution + edge-Function permissions the NATIVE
93
+ single-tenant edge (epic f55t, Model B) provisions in this account. Default 'false' so host
94
+ accounts that route through the shared edge are not granted CloudFront write. Set per host via
95
+ `cplane accounts enable-capability <host> tenant-edge`.
96
+ WARNING (rollout order): a host ALREADY running the native edge must have this enabled BEFORE
97
+ its foundation is redeployed at >= 1.1.0, or the redeploy strips the perms its live edge needs.
74
98
 
75
99
  Conditions:
100
+ # Capability gates. Each maps 1:1 to an entry in @saaslayer/foundation-contract (packages/
101
+ # foundation-contract/src/index.ts), which is what the CLI and the deployer read; a param here with
102
+ # no registry entry (or vice versa) fails that package's contract test.
76
103
  # Gate email-inbound-service's SES permissions so they exist only on host accounts that run it.
77
104
  EmailInboundEnabled: !Equals [!Ref EnableEmailInbound, 'true']
105
+ # Gate the native tenant-edge CloudFront permissions to hosts that actually run a native edge.
106
+ TenantEdgeEnabled: !Equals [!Ref EnableTenantEdge, 'true']
107
+
108
+ Mappings:
109
+ # Foundation template version (semver). BUMP on every change so support can tell which bootstrap an
110
+ # account runs (surfaced via the FoundationVersion / SaaSLayerAccountRef Outputs). Bump rules:
111
+ # major = breaking trust/permission-contract change (role renamed, permission removed) — account must re-bootstrap
112
+ # minor = additive capability (new resource-type perms, new optional param) — safe, no re-bootstrap
113
+ # patch = comments/docs/no behavior change
114
+ Meta:
115
+ Foundation:
116
+ Version: '5.12.0'
78
117
 
79
118
  Resources:
80
- # Private, encrypted bucket for packaged module templates and lambda artifacts uploaded by the
81
- # deployer. Only the deployment role reads/writes it (see saaslayer-stack-lifecycle below).
82
- DeploymentArtifactBucket:
83
- Type: AWS::S3::Bucket
84
- Properties:
85
- PublicAccessBlockConfiguration:
86
- BlockPublicAcls: true
87
- BlockPublicPolicy: true
88
- IgnorePublicAcls: true
89
- RestrictPublicBuckets: true
90
- BucketEncryption:
91
- ServerSideEncryptionConfiguration:
92
- - ServerSideEncryptionByDefault:
93
- SSEAlgorithm: AES256
119
+ # The shared deployment-artifact bucket moved OUT of this account-global stack into the per-region
120
+ # saaslayer-foundation-<region> stack (bead .30): S3 is regional and this IAM stack cannot deploy per-region.
121
+ # The deploy role reaches that bucket (sl-artifacts-<token>-<region>) through the ${StackNamePrefix}* grant
122
+ # in saaslayer-stack-lifecycle below, same as every other managed bucket.
94
123
 
95
124
  # Service role CloudFormation assumes while creating/updating/deleting resources declared by
96
125
  # approved SaaSLayer module templates. Not assumable by any AWS account — only the CloudFormation
@@ -102,7 +131,7 @@ Resources:
102
131
  SaaSLayerCloudFormationExecutionRole:
103
132
  Type: AWS::IAM::Role
104
133
  Properties:
105
- RoleName: SaaSLayerCloudFormationExecutionRole
134
+ RoleName: sl-foundation-cfn-exec-role
106
135
  AssumeRolePolicyDocument:
107
136
  Version: '2012-10-17'
108
137
  Statement:
@@ -136,6 +165,9 @@ Resources:
136
165
  - cloudwatch:EnableAlarmActions
137
166
  - cloudwatch:GetDashboard
138
167
  - cloudwatch:ListDashboards
168
+ # DetectStackDrift reads alarm tags; without this, drift checks on the ops alarms
169
+ # 403 and block every UPDATE (fail-closed drift gate).
170
+ - cloudwatch:ListTagsForResource
139
171
  - cloudwatch:PutDashboard
140
172
  - cloudwatch:PutMetricAlarm
141
173
  - cloudwatch:TagResource
@@ -208,18 +240,10 @@ Resources:
208
240
  - events:RemoveTargets
209
241
  - events:TagResource
210
242
  - events:UntagResource
211
- # IAM role/policy lifecycle for the roles module templates declare (lambda
212
- # execution roles, scheduler roles). Runtime permissions of created roles are
213
- # capped by SaaSLayerManagedResourceBoundary.
214
- - iam:CreatePolicy
215
- - iam:CreatePolicyVersion
216
- - iam:CreateRole
217
- - iam:DeletePolicy
218
- - iam:DeletePolicyVersion
219
- - iam:DeleteRole
220
- - iam:DeleteRolePolicy
221
- - iam:DetachRolePolicy
222
- - iam:AttachRolePolicy
243
+ # IAM read-only introspection during changeset execution. Mutating IAM verbs
244
+ # (Create/Delete/Update/Attach/Pass/Tag) are prefix-scoped in dedicated
245
+ # statements below — never Resource '*' — and CreateRole additionally requires
246
+ # SaaSLayerManagedResourceBoundary so no created role can exceed the boundary.
223
247
  - iam:GetPolicy
224
248
  - iam:GetPolicyVersion
225
249
  - iam:GetRole
@@ -228,30 +252,38 @@ Resources:
228
252
  - iam:ListEntitiesForPolicy
229
253
  - iam:ListPolicyVersions
230
254
  - iam:ListRolePolicies
231
- - iam:PassRole
232
- - iam:PutRolePolicy
233
- - iam:SetDefaultPolicyVersion
234
- - iam:TagPolicy
235
- - iam:TagRole
236
- - iam:UntagPolicy
237
- - iam:UntagRole
238
- - iam:UpdateAssumeRolePolicy
239
- - iam:UpdateRole
240
- - iam:UpdateRoleDescription
241
255
  # KMS customer-managed key + alias lifecycle. rbac-service provisions an
242
256
  # EventDeliveryKey (AWS::KMS::Key, rotation on) + alias for webhook payload
243
257
  # encryption; CreateKey mandates Resource '*', and TagResource on the new key
244
258
  # is required or CloudFormation fails with UnauthorizedTaggingOperation.
245
259
  - kms:CreateAlias
246
260
  - kms:CreateKey
261
+ # EarlyValidation runs a KMS::Alias name-conflict check on change-set creation; without
262
+ # ListAliases it reports a (non-fatal) "not allowed to check if resource already exists"
263
+ # FAIL in the console Hooks view and can't detect a real alias collision. Enumeration API,
264
+ # no resource-level scoping (monorepo-pcren).
265
+ - kms:ListAliases
266
+ # Grant + data-plane actions: a resource encrypted with a customer CMK (email-inbound's
267
+ # InboundTable via DynamoDB SSE-KMS) makes the service create a grant AND
268
+ # decrypt/generate data keys against the CMK during provisioning — without these the
269
+ # table create fails AccessDenied (kms:CreateGrant, then kms:Decrypt/GenerateDataKey).
270
+ - kms:CreateGrant
271
+ - kms:Decrypt
247
272
  - kms:DeleteAlias
248
273
  - kms:DescribeKey
249
274
  - kms:DisableKeyRotation
250
275
  - kms:EnableKeyRotation
276
+ - kms:Encrypt
277
+ - kms:GenerateDataKey
278
+ - kms:GenerateDataKeyWithoutPlaintext
251
279
  - kms:GetKeyPolicy
252
280
  - kms:GetKeyRotationStatus
281
+ - kms:ListGrants
253
282
  - kms:ListResourceTags
254
283
  - kms:PutKeyPolicy
284
+ - kms:ReEncryptFrom
285
+ - kms:ReEncryptTo
286
+ - kms:RevokeGrant
255
287
  - kms:ScheduleKeyDeletion
256
288
  - kms:TagResource
257
289
  - kms:UntagResource
@@ -416,6 +448,80 @@ Resources:
416
448
  - sqs:TagQueue
417
449
  - sqs:UntagQueue
418
450
  Resource: '*'
451
+ # Mutating IAM verbs are scoped to the managed-resource prefix (StackNamePrefix) so
452
+ # the exec role can only create/modify SaaSLayer module roles + customer-managed
453
+ # policies — never a pre-existing account role, and never the control-plane primitives
454
+ # (named SaaSLayer*, outside the prefix). CreateRole additionally requires the
455
+ # boundary, so no created role can exceed it even if a template attaches an admin policy.
456
+ - Effect: Allow
457
+ Action:
458
+ - iam:CreateRole
459
+ Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
460
+ Condition:
461
+ StringEquals:
462
+ iam:PermissionsBoundary: !Ref SaaSLayerManagedResourceBoundary
463
+ - Effect: Allow
464
+ Action:
465
+ - iam:DeleteRole
466
+ - iam:UpdateRole
467
+ - iam:UpdateRoleDescription
468
+ - iam:UpdateAssumeRolePolicy
469
+ - iam:PutRolePolicy
470
+ - iam:DeleteRolePolicy
471
+ - iam:AttachRolePolicy
472
+ - iam:DetachRolePolicy
473
+ - iam:TagRole
474
+ - iam:UntagRole
475
+ - iam:PassRole
476
+ Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
477
+ - Effect: Allow
478
+ Action:
479
+ - iam:CreatePolicy
480
+ - iam:CreatePolicyVersion
481
+ - iam:DeletePolicy
482
+ - iam:DeletePolicyVersion
483
+ - iam:SetDefaultPolicyVersion
484
+ - iam:TagPolicy
485
+ - iam:UntagPolicy
486
+ Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${StackNamePrefix}*
487
+ # Self-protecting Deny (defense-in-depth): the exec role can never mutate the
488
+ # permissions boundary or the three bootstrap roles, even though prefix scoping
489
+ # already excludes their SaaSLayer* names. An explicit Deny overrides any Allow.
490
+ - Effect: Deny
491
+ Action:
492
+ - iam:CreatePolicyVersion
493
+ - iam:SetDefaultPolicyVersion
494
+ - iam:DeletePolicy
495
+ - iam:DeletePolicyVersion
496
+ - iam:DeleteRole
497
+ - iam:PutRolePolicy
498
+ - iam:DeleteRolePolicy
499
+ - iam:AttachRolePolicy
500
+ - iam:DetachRolePolicy
501
+ - iam:UpdateAssumeRolePolicy
502
+ - iam:UpdateRole
503
+ Resource:
504
+ - !Ref SaaSLayerManagedResourceBoundary
505
+ - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-cfn-exec-role
506
+ - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-deploy-role
507
+ - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-support-ro-role
508
+ # Tenant-edge B (native single-tenant edge, epic f55t): CFN-driven Model B stands up a
509
+ # CloudFront distro + prefix-strip Function IN this account for
510
+ # {slug}.saaslayer-cloud.com/{service}. Only on hosts that run a native edge
511
+ # (EnableTenantEdge=true); the shared-edge path needs none of it.
512
+ - Fn::If:
513
+ - TenantEdgeEnabled
514
+ - Effect: Allow
515
+ Action:
516
+ - cloudfront:CreateFunction
517
+ - cloudfront:PublishFunction
518
+ - cloudfront:UpdateFunction
519
+ - cloudfront:DescribeFunction
520
+ - cloudfront:GetFunction
521
+ - cloudfront:ListFunctions
522
+ - cloudfront:ListDistributions
523
+ Resource: '*'
524
+ - !Ref AWS::NoValue
419
525
  # SES receipt rule set/rule provisioning — only on hosts that run email-inbound
420
526
  # (EnableEmailInbound=true). Identity verification (VerifyDomainIdentity/DKIM) runs
421
527
  # CP-side in the domain-service, never from the tenant account.
@@ -428,6 +534,9 @@ Resources:
428
534
  - ses:CreateReceiptRule
429
535
  - ses:DeleteReceiptRule
430
536
  - ses:UpdateReceiptRule
537
+ # CFN's AWS::SES::ReceiptRule calls SetReceiptRulePosition to (re)order a rule via its
538
+ # `After` property on UPDATE — without it a rule reorder 403s + wedges the stack.
539
+ - ses:SetReceiptRulePosition
431
540
  - ses:DescribeReceiptRuleSet
432
541
  - ses:DescribeReceiptRule
433
542
  Resource: '*'
@@ -449,20 +558,39 @@ Resources:
449
558
  SaaSLayerManagedResourceBoundary:
450
559
  Type: AWS::IAM::ManagedPolicy
451
560
  Properties:
452
- ManagedPolicyName: SaaSLayerManagedResourceBoundary
561
+ ManagedPolicyName: sl-foundation-managed-boundary
453
562
  Description: Maximum runtime permissions for IAM roles created by SaaSLayer managed stacks.
454
563
  PolicyDocument:
455
564
  Version: '2012-10-17'
456
565
  Statement:
457
566
  - Effect: Allow
458
567
  Action:
568
+ # Invitation registration creates the invited account after mailbox proof (user-service).
569
+ - cognito-idp:AdminCreateUser
570
+ - cognito-idp:AdminDeleteUser
459
571
  - cognito-idp:AdminDeleteUserAttributes
572
+ - cognito-idp:AdminDisableProviderForUser
573
+ # User lifecycle operations by suid (user-service API: disable/enable after global sign-out, reset-password).
574
+ - cognito-idp:AdminDisableUser
575
+ - cognito-idp:AdminEnableUser
460
576
  - cognito-idp:AdminGetUser
577
+ - cognito-idp:AdminLinkProviderForUser
461
578
  - cognito-idp:AdminListGroupsForUser
579
+ - cognito-idp:AdminResetUserPassword
462
580
  - cognito-idp:AdminSetUserMFAPreference
581
+ - cognito-idp:AdminSetUserPassword
463
582
  - cognito-idp:AdminUpdateUserAttributes
583
+ - cognito-idp:AdminUserGlobalSignOut
584
+ # Social login + identity stitching (epic monorepo-24667): the idpResource custom-resource
585
+ # Lambda manages the federated IdPs; the PreSignUp/stitching + account Lambdas link/disable
586
+ # providers, set passwords, and delete users. The boundary is the ceiling, so it must allow
587
+ # every action the managed roles use (their inline policies scope the resources).
588
+ - cognito-idp:CreateIdentityProvider
589
+ - cognito-idp:DeleteIdentityProvider
590
+ - cognito-idp:DescribeIdentityProvider
464
591
  - cognito-idp:GetUserPoolMfaConfig
465
592
  - cognito-idp:ListUsers
593
+ - cognito-idp:UpdateIdentityProvider
466
594
  - dynamodb:BatchGetItem
467
595
  - dynamodb:BatchWriteItem
468
596
  - dynamodb:DeleteItem
@@ -486,6 +614,9 @@ Resources:
486
614
  - lambda:InvokeFunction
487
615
  - logs:CreateLogGroup
488
616
  - logs:CreateLogStream
617
+ # Cognito validates a user-import job's logs role for DescribeLogStreams before it starts the job
618
+ # (user-service UserImportLogsRole; `saaslayer users import`).
619
+ - logs:DescribeLogStreams
489
620
  - logs:PutLogEvents
490
621
  - mobiletargeting:PutEvents
491
622
  - s3:AbortMultipartUpload
@@ -505,7 +636,34 @@ Resources:
505
636
  - sqs:ReceiveMessage
506
637
  - sqs:SendMessage
507
638
  - sqs:SendMessageBatch
639
+ # Social login (epic monorepo-24667): the idpResource custom resource reads the BYO client
640
+ # secret from an SSM SecureString (/saaslayer/deployment/*); kms:Decrypt above covers the
641
+ # SecureString's aws/ssm key.
642
+ - ssm:GetParameter
508
643
  Resource: '*'
644
+ # PITR check + opt-in on-demand backup for per-service migrate roles (@saaslayer/migrations).
645
+ # Managed-prefix tables only. The boundary is account-global and the role policies do the
646
+ # regional scoping, hence dynamodb:*:.
647
+ - Effect: Allow
648
+ Action:
649
+ - dynamodb:DescribeContinuousBackups
650
+ - dynamodb:CreateBackup
651
+ - dynamodb:DescribeBackup
652
+ Resource:
653
+ - !Sub arn:${AWS::Partition}:dynamodb:*:${AWS::AccountId}:table/${StackNamePrefix}*
654
+ - !Sub arn:${AWS::Partition}:dynamodb:*:${AWS::AccountId}:table/${StackNamePrefix}*/backup/*
655
+ # rbac's machine-trust API renders its own IAM-principals document under the deployment's SSM namespace.
656
+ - Effect: Allow
657
+ Action:
658
+ - ssm:PutParameter
659
+ Resource:
660
+ - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
661
+ # Transactional email from this account's own SES identities (user-service registration codes).
662
+ - Effect: Allow
663
+ Action:
664
+ - ses:SendEmail
665
+ Resource:
666
+ - !Sub arn:${AWS::Partition}:ses:*:${AWS::AccountId}:identity/*
509
667
  # SES runtime (forward/notify sends + the active-rule-set custom resource) — only on hosts
510
668
  # that run email-inbound. Receiving into S3 is granted to the ses.amazonaws.com service
511
669
  # principal by the module's bucket policy, not here.
@@ -528,7 +686,7 @@ Resources:
528
686
  SaaSLayerDeploymentRole:
529
687
  Type: AWS::IAM::Role
530
688
  Properties:
531
- RoleName: SaaSLayerDeploymentRole
689
+ RoleName: sl-foundation-deploy-role
532
690
  AssumeRolePolicyDocument:
533
691
  Version: '2012-10-17'
534
692
  Statement:
@@ -553,6 +711,9 @@ Resources:
553
711
  - cloudformation:UpdateStack
554
712
  - cloudformation:ExecuteChangeSet
555
713
  - cloudformation:SetStackPolicy
714
+ # Protected environments (prod, staging): the deployer enables termination protection
715
+ # after a deploy and disables it before an authorized destroy.
716
+ - cloudformation:UpdateTerminationProtection
556
717
  - cloudformation:DeleteChangeSet
557
718
  - cloudformation:DeleteStack
558
719
  - cloudformation:Describe*
@@ -575,11 +736,11 @@ Resources:
575
736
  Action:
576
737
  - iam:PassRole
577
738
  Resource: !GetAtt SaaSLayerCloudFormationExecutionRole.Arn
578
- # Artifact staging: the bootstrap-created artifact bucket, the per-stack serverless
579
- # deployment buckets (sl- prefixed), and the module data buckets named from the
580
- # deployer slug (sldep*) — e.g. user-service's versioned cognito-user-backup bucket,
581
- # which teardown must empty (ListBucketVersions/DeleteObjectVersion) before the stack
582
- # can delete it, or destroy fails with AccessDenied on s3:ListBucketVersions.
739
+ # Artifact staging + data-bucket lifecycle: the shared deployment-artifact bucket
740
+ # (sl-artifacts-<token>-<region>, in the per-region foundation stack), the module data buckets
741
+ # named from the managed prefix (sl-) — e.g. user-service's versioned cognito-user-backup bucket,
742
+ # which teardown must empty (ListBucketVersions/DeleteObjectVersion) before the stack can delete
743
+ # it, or destroy fails with AccessDenied on s3:ListBucketVersions.
583
744
  - Effect: Allow
584
745
  Action:
585
746
  - s3:GetBucketLocation
@@ -591,23 +752,14 @@ Resources:
591
752
  - s3:ListBucket
592
753
  - s3:ListBucketVersions
593
754
  Resource:
594
- - !GetAtt DeploymentArtifactBucket.Arn
595
- - !Sub ${DeploymentArtifactBucket.Arn}/*
596
- - arn:aws:s3:::sl-*serverlessdeploymentbuck*
597
- - arn:aws:s3:::sl-*serverlessdeploymentbuck*/*
598
- - arn:aws:s3:::sldep*-cognito-user-backup-*
599
- - arn:aws:s3:::sldep*-cognito-user-backup-*/*
600
- # rbac-admin-ui static-site bucket: serverless-s3-sync uploads the SPA + config.json
601
- # under this role at deploy, and teardown empties it before the stack deletes it.
602
- - arn:aws:s3:::sl-*-adminui-*
603
- - arn:aws:s3:::sl-*-adminui-*/*
604
- # email-inbound-service raw-MIME + attachment buckets: SES writes inbound mail here
605
- # and teardown must empty them (ListBucketVersions/DeleteObjectVersion) before the
606
- # stack can delete them, or destroy fails with AccessDenied.
607
- - arn:aws:s3:::sl-*-email-inbound-raw-*
608
- - arn:aws:s3:::sl-*-email-inbound-raw-*/*
609
- - arn:aws:s3:::sl-*-email-inbound-attachments-*
610
- - arn:aws:s3:::sl-*-email-inbound-attachments-*/*
755
+ # ALL deployer-managed service app buckets are now OPAQUE ${StackNamePrefix}<token>-<service>
756
+ # (naming §5, T-FOUNDATION-WILDCARDS): the opaque token replaces the old per-service name
757
+ # suffixes (eml-raw/att, cognito-user-backup, adminui, manifest) AND the framework
758
+ # *serverlessdeploymentbucket*, so ONE prefix-scoped wildcard covers every one. The deploy
759
+ # role can create + empty (ListBucketVersions/DeleteObjectVersion) only buckets under the
760
+ # managed prefix — never a bare literal (D8: StackNamePrefix is customer-configurable).
761
+ - !Sub arn:aws:s3:::${StackNamePrefix}*
762
+ - !Sub arn:aws:s3:::${StackNamePrefix}*/*
611
763
  # Deploy tooling introspection after stack operations complete: stack enumeration
612
764
  # (ListStacks has no resource scoping), API Gateway lifecycle calls the Serverless
613
765
  # tooling makes outside CloudFormation, and lambda version/alias inspection.
@@ -628,28 +780,157 @@ Resources:
628
780
  - lambda:ListVersionsByFunction
629
781
  Resource: '*'
630
782
  # Post-deploy SEED data-plane: after a module deploys, the deployer runs its seed
631
- # script under this role to make the deployment usable — create the initial admin
632
- # user in the just-created Cognito pool (user-service seed) and bootstrap the tenant,
633
- # admin role, and trust policy in the rbac tables (rbac-core seed). Cognito admin
634
- # actions cannot be resource-scoped to a pool that does not exist at bootstrap time;
635
- # DynamoDB is scoped to the rbac-* managed tables.
783
+ # script under this role to make the deployment usable — bootstrap the tenant, the
784
+ # designated first admin's deployment-administrator binding (and an explicit recovery's
785
+ # rebinding) and the trust policy in the rbac tables (rbac-core seed), then create and
786
+ # deliver the first admin in the just-created Cognito pool with its core-table records
787
+ # (user-service seed). Cognito admin actions cannot be resource-scoped to a pool that does
788
+ # not exist at bootstrap time; DynamoDB is scoped to the managed-prefix tables.
636
789
  - Effect: Allow
637
790
  Action:
638
791
  - cognito-idp:AdminCreateUser
639
792
  - cognito-idp:AdminGetUser
640
793
  - cognito-idp:AdminSetUserPassword
794
+ - cognito-idp:AdminUpdateUserAttributes
641
795
  - cognito-idp:ListUsers
796
+ # Scoped to SaaSLayer-managed pools by tag — the seed can never touch the customer's
797
+ # own Cognito pools. Module stacks tag the pool managed-by:saaslayer-deployer (CFN
798
+ # propagates stack tags), so it is tagged before the post-deploy seed runs.
642
799
  Resource: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
800
+ Condition:
801
+ StringEquals:
802
+ aws:ResourceTag/managed-by: saaslayer-deployer
643
803
  - Effect: Allow
644
804
  Action:
645
805
  - dynamodb:GetItem
646
806
  - dynamodb:PutItem
647
807
  - dynamodb:UpdateItem
808
+ # rbac unbinds a role with DeleteItem (first-admin recovery, legacy admin retirement).
809
+ - dynamodb:DeleteItem
648
810
  - dynamodb:Query
649
811
  - dynamodb:BatchWriteItem
812
+ # Teardown cleanup: a table with DeletionPolicy:Retain survives `serverless remove`, so the
813
+ # deployer's module-runner deletes those retained sl-* tables directly after the stack is gone
814
+ # (deleteStackOwnedTables) — else a same-named recreate collides. Scoped to sl-* tables only.
815
+ - dynamodb:DeleteTable
816
+ # Protected-environment tables carry deletion protection; a delete-custody teardown turns it off
817
+ # (UpdateTable DeletionProtectionEnabled=false) before DeleteTable. Same sl-* scope.
818
+ - dynamodb:UpdateTable
819
+ Resource:
820
+ - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${StackNamePrefix}*
821
+ - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${StackNamePrefix}*/index/*
822
+ # Teardown cleanup: AWS auto-creates a /aws/lambda/<function> log group on first invocation that
823
+ # survives `serverless remove` (and even an explicit AWS::Logs::LogGroup can outlive the stack), so
824
+ # the module-runner sweeps the module's log groups after remove (like the retained-table sweep) —
825
+ # else a same-name recreate collides ("LogGroup already exists" at EarlyValidation). DescribeLogGroups
826
+ # is an enumeration (no resource scoping); DeleteLogGroup is scoped to the managed prefix (monorepo-04egp).
827
+ - Effect: Allow
828
+ Action:
829
+ - logs:DescribeLogGroups
830
+ Resource: '*'
831
+ - Effect: Allow
832
+ Action:
833
+ - logs:DeleteLogGroup
650
834
  Resource:
651
- - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/rbac-*
652
- - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/rbac-*/index/*
835
+ - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${StackNamePrefix}*
836
+ # Data migrations (@saaslayer/migrations): hop into a service stack's own migrate role; the
837
+ # role's own policy limits it to that service's tables. Its trust is account root + an
838
+ # aws:PrincipalArn condition, which delegates to this identity policy, so the grant is required.
839
+ - Effect: Allow
840
+ Action: sts:AssumeRole
841
+ Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*-migrate
842
+ # Domain-mapping (D1.0, monorepo-88mn.3): the domain-service assumes this role to issue an ACM
843
+ # cert for the tenant's {slug}.saaslayer-cloud.com names, then attach an API-GW regional custom
844
+ # domain — the apigateway create/read/update/delete verbs are ALREADY granted above (Resource
845
+ # '*'), so only ACM is added here. Scoped to certs tagged managed-by=saaslayer-deployer so this
846
+ # role can only touch certs it created; RequestCertificate has no resource-level ARN, so the
847
+ # request-tag condition is the only guard. Regional custom domains do NOT need a service-linked
848
+ # role; add iam:CreateServiceLinkedRole for apigateway ONLY if domain-iam-spike.sh (D1.0a) proves it.
849
+ # attachDomain (domain-service.ts) creates the API Gateway service-linked role on the FIRST
850
+ # custom domain in the account — the earlier "regional custom domains do NOT need a
851
+ # service-linked role" assumption was wrong (attachDomain AccessDenied'd on
852
+ # iam:CreateServiceLinkedRole). Scoped to the apigateway service-linked-role path only.
853
+ - Effect: Allow
854
+ Action:
855
+ - iam:CreateServiceLinkedRole
856
+ Resource: 'arn:aws:iam::*:role/aws-service-role/*apigateway.amazonaws.com/*'
857
+ Condition:
858
+ StringLike:
859
+ iam:AWSServiceName: '*apigateway.amazonaws.com'
860
+ - Effect: Allow
861
+ Action:
862
+ - acm:RequestCertificate
863
+ Resource: '*'
864
+ Condition:
865
+ StringEquals:
866
+ aws:RequestTag/managed-by: saaslayer-deployer
867
+ - Effect: Allow
868
+ Action:
869
+ - acm:DescribeCertificate
870
+ - acm:DeleteCertificate
871
+ - acm:AddTagsToCertificate
872
+ - acm:ListTagsForCertificate
873
+ Resource: '*'
874
+ Condition:
875
+ StringEquals:
876
+ aws:ResourceTag/managed-by: saaslayer-deployer
877
+ # ListCertificates has no resource-level or condition support.
878
+ - Effect: Allow
879
+ Action:
880
+ - acm:ListCertificates
881
+ Resource: '*'
882
+ # M2 vendable-key handoff (plan s7oc.9.3): the deploy role runs rbac's post-deploy SEED via
883
+ # DIRECT SDK calls (like the native-edge teardown below), and the seed writes the scoped
884
+ # onboarding endpoint (String) + service key (SecureString) into THIS deployment's SSM
885
+ # namespace for user-service to read (M4). GetParameter re-reads to honor the SSM-authoritative
886
+ # verify contract (reuse a still-valid key instead of re-minting on every deploy). Scoped to
887
+ # /saaslayer/deployment/* — the deploy role is the account's deploy agent across deployments.
888
+ - Effect: Allow
889
+ Action:
890
+ - ssm:GetParameter
891
+ - ssm:PutParameter
892
+ # DeleteParameter: the destroy/teardown phase clears the handoff params (they are written
893
+ # by PutParameter, NOT CloudFormation, so a stack delete leaves them behind) — M4b.
894
+ - ssm:DeleteParameter
895
+ Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/saaslayer/deployment/*
896
+ # Encrypt/decrypt the SecureString onboarding key through the SSM-managed default key
897
+ # (alias/aws/ssm). ViaService confines this grant to operations performed by SSM.
898
+ - Effect: Allow
899
+ Action:
900
+ - kms:Encrypt
901
+ - kms:Decrypt
902
+ - kms:GenerateDataKey
903
+ Resource: '*'
904
+ Condition:
905
+ StringEquals:
906
+ kms:ViaService: !Sub ssm.${AWS::Region}.amazonaws.com
907
+ # Tenant-edge B (native single-tenant edge, epic f55t): the deploy role provisions a CloudFront
908
+ # distro + prefix-strip Function IN this account via DIRECT SDK calls (not CloudFormation), so
909
+ # these live on the DeploymentRole (the ExecutionRole copies are for CFN-driven Model-B). No IAM
910
+ # creation here — CloudFront distros/functions don't create roles — so the role stays narrow.
911
+ # Gated on the tenant-edge capability: a host on the shared edge never needs CloudFront write.
912
+ - Fn::If:
913
+ - TenantEdgeEnabled
914
+ - Effect: Allow
915
+ Action:
916
+ - cloudfront:CreateDistribution
917
+ - cloudfront:GetDistribution
918
+ - cloudfront:GetDistributionConfig
919
+ - cloudfront:UpdateDistribution
920
+ - cloudfront:ListDistributions
921
+ - cloudfront:TagResource
922
+ - cloudfront:CreateFunction
923
+ - cloudfront:PublishFunction
924
+ - cloudfront:UpdateFunction
925
+ - cloudfront:DescribeFunction
926
+ - cloudfront:GetFunction
927
+ - cloudfront:ListFunctions
928
+ # Native/B teardown (teardownNativeEdge runs via THIS DeploymentRole, direct SDK): delete
929
+ # the per-tenant distro + strip Function on destroy. Without these the B teardown leaks.
930
+ - cloudfront:DeleteDistribution
931
+ - cloudfront:DeleteFunction
932
+ Resource: '*'
933
+ - !Ref AWS::NoValue
653
934
 
654
935
  # Cross-account READ-ONLY role for SaaSLayer support staff to diagnose managed deployments
655
936
  # without the deployment role's write power. Trust matches the deployment role (control-plane
@@ -660,7 +941,7 @@ Resources:
660
941
  SaaSLayerSupportReadOnlyRole:
661
942
  Type: AWS::IAM::Role
662
943
  Properties:
663
- RoleName: SaaSLayerSupportReadOnlyRole
944
+ RoleName: sl-foundation-support-ro-role
664
945
  AssumeRolePolicyDocument:
665
946
  Version: '2012-10-17'
666
947
  Statement:
@@ -684,7 +965,13 @@ Resources:
684
965
  - cloudformation:Describe*
685
966
  - cloudformation:Get*
686
967
  - cloudformation:ListStackResources
687
- Resource: !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${StackNamePrefix}*
968
+ Resource:
969
+ # SaaSLayer-managed module stacks (sl-*) AND the foundation stack itself
970
+ # (saaslayer-foundation / -host-bootstrap): the deployer's foundation discovery assumes
971
+ # THIS support-ro role and reads the foundation stack Outputs, so it must be in scope.
972
+ - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${StackNamePrefix}*
973
+ - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/saaslayer-foundation*
974
+ - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/saaslayer-host-bootstrap*
688
975
  # Stack enumeration; the ListStacks API does not support resource scoping.
689
976
  - Effect: Allow
690
977
  Action:
@@ -720,20 +1007,22 @@ Resources:
720
1007
  Condition:
721
1008
  StringEquals:
722
1009
  aws:ResourceTag/managed-by: saaslayer-deployer
723
- # Log reads for diagnosing module function failures. CloudWatch Logs does not
724
- # support aws:ResourceTag conditions on these read actions, and module log group
725
- # names follow function names rather than a common prefix, so these cannot be
726
- # tag- or ARN-scoped yet. They expose log content and log-group metadata only —
727
- # no service configuration or data-plane records.
1010
+ # Log-group enumeration + tag reads stay account-wide (enumeration APIs return only
1011
+ # names/metadata). Log CONTENT reads are scoped to SaaSLayer module log groups
1012
+ # (/aws/lambda/<StackNamePrefix>*), so support can never read the customer's own
1013
+ # application logs — which routinely carry PII, bearer tokens, and secrets.
728
1014
  - Effect: Allow
729
1015
  Action:
730
1016
  - logs:DescribeLogGroups
1017
+ - logs:ListTagsForResource
1018
+ Resource: '*'
1019
+ - Effect: Allow
1020
+ Action:
731
1021
  - logs:DescribeLogStreams
732
1022
  - logs:GetLogEvents
733
1023
  - logs:GetLogGroupFields
734
1024
  - logs:GetLogRecord
735
- - logs:ListTagsForResource
736
- Resource: '*'
1025
+ Resource: !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${StackNamePrefix}*:*
737
1026
  # Alarm/dashboard state for module observability. CloudWatch alarms support
738
1027
  # aws:ResourceTag; DescribeAlarms is an enumeration API evaluated per-alarm.
739
1028
  - Effect: Allow
@@ -753,9 +1042,14 @@ Outputs:
753
1042
  CloudFormationExecutionRoleArn:
754
1043
  Description: Service role CloudFormation uses to provision approved SaaSLayer module stacks; not assumable cross-account.
755
1044
  Value: !GetAtt SaaSLayerCloudFormationExecutionRole.Arn
756
- DeploymentArtifactBucketName:
757
- Description: Private, encrypted bucket holding packaged SaaSLayer deployment artifacts.
758
- Value: !Ref DeploymentArtifactBucket
759
1045
  ManagedResourceBoundaryArn:
760
1046
  Description: Permissions boundary capping the runtime permissions of every IAM role created by SaaSLayer module stacks.
761
1047
  Value: !Ref SaaSLayerManagedResourceBoundary
1048
+ FoundationVersion:
1049
+ Description: Version of this foundation template installed in the account.
1050
+ Value: !FindInMap [Meta, Foundation, Version]
1051
+ SaaSLayerAccountRef:
1052
+ Description: Share this with SaaSLayer support to identify your account and foundation version.
1053
+ Value: !Sub
1054
+ - 'org=${OrgId} account=${AWS::AccountId} region=${AWS::Region} prefix=${StackNamePrefix} v=${Ver}'
1055
+ - Ver: !FindInMap [Meta, Foundation, Version]