@anthusai/papyrus 5.110.0 → 5.112.0-next.1
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.
- package/amplify/auth/resource.js +3 -0
- package/amplify/auth/resource.ts +3 -0
- package/amplify/auth/tenant-session-claims.js +27 -0
- package/amplify/auth/tenant-session-claims.ts +31 -0
- package/amplify/cost-attribution.js +12 -0
- package/amplify/cost-attribution.ts +12 -0
- package/amplify/data/resource.js +79 -0
- package/amplify/data/resource.ts +82 -0
- package/amplify/data/schema.js +82 -1
- package/amplify/data/schema.ts +85 -1
- package/amplify/functions/assignment-action/handler.ts +14 -9
- package/amplify/functions/automation-view/wire.js +2 -1
- package/amplify/functions/automation-view/wire.ts +2 -1
- package/amplify/functions/category-action/handler.ts +24 -18
- package/amplify/functions/category-action/message-hashtags.js +2 -1
- package/amplify/functions/category-action/message-hashtags.ts +2 -1
- package/amplify/functions/category-action/reference-links.js +3 -2
- package/amplify/functions/category-action/reference-links.ts +3 -2
- package/amplify/functions/console-chat-responder/resource.js +2 -1
- package/amplify/functions/console-chat-responder/resource.ts +2 -1
- package/amplify/functions/content-actions/handler.py +54 -22
- package/amplify/functions/content-actions/resource.js +4 -0
- package/amplify/functions/content-actions/resource.ts +5 -0
- package/amplify/functions/editorial-checks/handler.py +5 -1
- package/amplify/functions/editorial-checks/schema.js +10 -2
- package/amplify/functions/editorial-checks/schema.ts +10 -2
- package/amplify/functions/editorial-checks/wire.js +2 -1
- package/amplify/functions/editorial-checks/wire.ts +2 -1
- package/amplify/functions/image-service/wire.js +2 -1
- package/amplify/functions/image-service/wire.ts +2 -1
- package/amplify/functions/monitoring-view/wire.js +2 -1
- package/amplify/functions/monitoring-view/wire.ts +2 -1
- package/amplify/functions/newsroom-summary/handler.ts +16 -12
- package/amplify/functions/shared/tenant-context.js +98 -0
- package/amplify/functions/shared/tenant-context.ts +33 -0
- package/amplify/functions/shared/tenant-scoped-client.ts +80 -0
- package/amplify/functions/tenant-claims/claims.ts +90 -0
- package/amplify/functions/tenant-claims/environment.js +7 -0
- package/amplify/functions/tenant-claims/environment.ts +7 -0
- package/amplify/functions/tenant-claims/handler.ts +56 -0
- package/amplify/functions/tenant-claims/resource.js +12 -0
- package/amplify/functions/tenant-claims/resource.ts +13 -0
- package/amplify/plugins/newsletter/sending.js +2 -1
- package/amplify/plugins/newsletter/sending.ts +2 -1
- package/amplify/plugins/notifications/papyrus.plugin.js +2 -1
- package/amplify/plugins/notifications/papyrus.plugin.ts +2 -1
- package/amplify/plugins/reader-steering/schema.js +15 -3
- package/amplify/plugins/reader-steering/schema.ts +15 -3
- package/amplify/plugins/signup/papyrus.plugin.js +2 -1
- package/amplify/plugins/signup/papyrus.plugin.ts +2 -1
- package/amplify/plugins/signup/schema.js +1 -0
- package/amplify/plugins/signup/schema.ts +1 -0
- package/amplify/shared-stack/alarms.js +53 -0
- package/amplify/shared-stack/alarms.ts +109 -0
- package/amplify/shared-stack/define.js +15 -0
- package/amplify/shared-stack/define.ts +17 -0
- package/amplify/shared-stack/deny.js +27 -0
- package/amplify/shared-stack/deny.ts +28 -0
- package/amplify/shared-stack/inline-policy.js +53 -0
- package/amplify/shared-stack/inline-policy.ts +65 -0
- package/amplify/shared-stack/isolation.js +161 -0
- package/amplify/shared-stack/isolation.ts +204 -0
- package/amplify/shared-stack/names.js +95 -0
- package/amplify/shared-stack/names.ts +133 -0
- package/amplify/shared-stack/settings.js +70 -0
- package/amplify/shared-stack/settings.ts +95 -0
- package/amplify/site-api-access.js +13 -0
- package/amplify/site-api-access.ts +14 -0
- package/amplify/site-backend-config.js +7 -1
- package/amplify/site-backend-config.ts +8 -2
- package/amplify/site-backend.js +65 -13
- package/amplify/site-backend.ts +75 -19
- package/components/news-desk-taxonomy-client.ts +5 -0
- package/components/newsroom-article-editor.tsx +7 -3
- package/components/newsroom-board-view.tsx +4 -2
- package/components/newsroom-organizer.css +2 -2
- package/components/use-newsroom-desk-sections.ts +31 -0
- package/infra/amplify-app-shell.js +4 -1
- package/infra/amplify-app-shell.ts +4 -1
- package/infra/build-specs.js +4 -1
- package/infra/build-specs.ts +5 -1
- package/infra/cost-tags.test.js +1 -1
- package/infra/cost-tags.test.ts +1 -1
- package/infra/shared-stack-deny.js +80 -0
- package/infra/shared-stack-deny.test.js +56 -0
- package/infra/shared-stack-deny.test.ts +66 -0
- package/infra/shared-stack-deny.ts +86 -0
- package/lib/newsroom-sections.ts +14 -0
- package/lib/tenant-keys.js +16 -0
- package/lib/tenant-keys.ts +19 -0
- package/package.json +2 -1
- package/routes.manifest.json +1 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { Aws, CfnParameter, Fn, RemovalPolicy, Stack, Token } from "aws-cdk-lib";
|
|
2
|
+
import { CfnRole } from "aws-cdk-lib/aws-iam";
|
|
3
|
+
import { CfnLogGroup } from "aws-cdk-lib/aws-logs";
|
|
4
|
+
import { CfnBucket } from "aws-cdk-lib/aws-s3";
|
|
5
|
+
import { CfnIndex, CfnVectorBucket } from "aws-cdk-lib/aws-s3vectors";
|
|
6
|
+
import { CfnQueue } from "aws-cdk-lib/aws-sqs";
|
|
7
|
+
import { CfnTable } from "aws-cdk-lib/aws-dynamodb";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { appendInlinePolicy, appendInlinePolicyToRawRole, findAllOfType, findResourcesOfCfnType, functionOwnerId, isInfrastructureFunction } from "./inline-policy.js";
|
|
10
|
+
/** What the key could not be attached to, by resource type, because Amplify or the service gives no way to. */
|
|
11
|
+
export const RESOURCES_THAT_CANNOT_TAKE_THE_KEY = [
|
|
12
|
+
{
|
|
13
|
+
resource: "Amplify secrets (secret(\"GOOGLE_CLIENT_ID\") and the other secret() values)",
|
|
14
|
+
reason: "Amplify Gen 2 stores them in SSM under /amplify/<app id>/ with the AWS-managed key and has no hook to choose another key.",
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
resource: "SecureString parameters made by custom resources (plugins that write one, such as newsletter)",
|
|
18
|
+
reason: "the shared stack runs no plugin that does; a plugin that is turned on later must be given the key by its custom resource handler.",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
resource: "CloudFront KeyValueStore of the projection publication",
|
|
22
|
+
reason: "CloudFront key value stores have no KMS option; the store holds redirects only, no tenant content.",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
resource: "Cognito user pool",
|
|
26
|
+
reason: "Cognito encrypts with AWS-owned keys and offers no customer-managed key.",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
resource: "AppSync API",
|
|
30
|
+
reason: "AppSync encrypts at rest with AWS-owned keys and offers no customer-managed key; the data lives in the tables and bucket, which take the key.",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
resource: "Lambda environment variables and code",
|
|
34
|
+
reason: "the Amplify function constructs expose no KMS key for them; the variables hold names and settings, no secrets.",
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
export function shortHash(value, length = 8) {
|
|
38
|
+
return createHash("sha256").update(value).digest("hex").slice(0, length);
|
|
39
|
+
}
|
|
40
|
+
function slug(value, max) {
|
|
41
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, max).replace(/-+$/g, "");
|
|
42
|
+
}
|
|
43
|
+
export function boundaryPolicyArn(plan) {
|
|
44
|
+
return `arn:${Aws.PARTITION}:iam::${Aws.ACCOUNT_ID}:policy/${plan.boundaryPolicyName}`;
|
|
45
|
+
}
|
|
46
|
+
/** The statement a shared role needs to use the stack's key; the boundary also allows it, the key policy admits the role path. */
|
|
47
|
+
export function keyUseStatement(keyArn) {
|
|
48
|
+
return {
|
|
49
|
+
Sid: "UseTheSharedStackKey",
|
|
50
|
+
Effect: "Allow",
|
|
51
|
+
Action: ["kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:DescribeKey", "kms:CreateGrant"],
|
|
52
|
+
Resource: [keyArn],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function applySharedStackIsolation(backend, plan, settings) {
|
|
56
|
+
const root = backend.stack;
|
|
57
|
+
const keyParameter = new CfnParameter(root, "PapyrusSharedKmsKeyArn", {
|
|
58
|
+
type: "AWS::SSM::Parameter::Value<String>",
|
|
59
|
+
default: plan.keyArnParameter,
|
|
60
|
+
description: `The ${plan.tier} shared stack's KMS key ARN, written by the ${plan.foundationStackName} stack.`,
|
|
61
|
+
});
|
|
62
|
+
const keyArn = keyParameter.valueAsString;
|
|
63
|
+
const encryption = { sseEnabled: true, sseType: "KMS", kmsMasterKeyId: keyArn };
|
|
64
|
+
// Amplify's model tables are made by a custom resource, so the key goes through the table wrapper.
|
|
65
|
+
const amplifyTables = Object.values(backend.data.resources.cfnResources.amplifyDynamoDbTables);
|
|
66
|
+
for (const table of amplifyTables)
|
|
67
|
+
table.sseSpecification = encryption;
|
|
68
|
+
const roles = findAllOfType(root, CfnRole);
|
|
69
|
+
const boundary = boundaryPolicyArn(plan);
|
|
70
|
+
const keyUsePolicy = { Version: "2012-10-17", Statement: [keyUseStatement(keyArn)] };
|
|
71
|
+
for (const role of roles) {
|
|
72
|
+
role.path = plan.rolePath;
|
|
73
|
+
role.permissionsBoundary = boundary;
|
|
74
|
+
appendInlinePolicy(role, `${plan.stackPrefix}-key-use`, keyUsePolicy);
|
|
75
|
+
}
|
|
76
|
+
// CDK writes the role of a custom resource provider as a raw resource, not a CfnRole.
|
|
77
|
+
const rawRoles = findResourcesOfCfnType(root, "AWS::IAM::Role").filter((role) => !(role instanceof CfnRole));
|
|
78
|
+
for (const role of rawRoles) {
|
|
79
|
+
role.addPropertyOverride("Path", plan.rolePath);
|
|
80
|
+
role.addPropertyOverride("PermissionsBoundary", boundary);
|
|
81
|
+
appendInlinePolicyToRawRole(role, `${plan.stackPrefix}-key-use`, keyUsePolicy);
|
|
82
|
+
}
|
|
83
|
+
let tables = amplifyTables.length;
|
|
84
|
+
for (const table of findAllOfType(root, CfnTable)) {
|
|
85
|
+
table.sseSpecification = { sseEnabled: true, sseType: "KMS", kmsMasterKeyId: keyArn };
|
|
86
|
+
tables += 1;
|
|
87
|
+
}
|
|
88
|
+
const buckets = findAllOfType(root, CfnBucket);
|
|
89
|
+
for (const bucket of buckets) {
|
|
90
|
+
// A bucket name that carries the stack's prefix lets the boundary and the dedicated deny match objects, which have no tags of their own.
|
|
91
|
+
const named = bucket.bucketName;
|
|
92
|
+
if (typeof named === "string" && !Token.isUnresolved(named)) {
|
|
93
|
+
if (!named.startsWith(`${plan.stackPrefix}-`))
|
|
94
|
+
throw new Error(`Bucket ${bucket.node.path} is named ${named}, which does not start with ${plan.stackPrefix}-.`);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
bucket.bucketName = Fn.join("-", [plan.stackPrefix, shortHash(bucket.node.path), Aws.ACCOUNT_ID, Aws.REGION]);
|
|
98
|
+
}
|
|
99
|
+
bucket.bucketEncryption = {
|
|
100
|
+
serverSideEncryptionConfiguration: [{ bucketKeyEnabled: true, serverSideEncryptionByDefault: { sseAlgorithm: "aws:kms", kmsMasterKeyId: keyArn } }],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const queues = findAllOfType(root, CfnQueue);
|
|
104
|
+
for (const queue of queues) {
|
|
105
|
+
// SQS-managed and KMS encryption exclude each other.
|
|
106
|
+
queue.sqsManagedSseEnabled = undefined;
|
|
107
|
+
queue.kmsMasterKeyId = keyArn;
|
|
108
|
+
}
|
|
109
|
+
let vectorResources = 0;
|
|
110
|
+
for (const vectorBucket of findAllOfType(root, CfnVectorBucket)) {
|
|
111
|
+
vectorBucket.encryptionConfiguration = { sseType: "aws:kms", kmsKeyArn: keyArn };
|
|
112
|
+
vectorResources += 1;
|
|
113
|
+
}
|
|
114
|
+
for (const index of findAllOfType(root, CfnIndex)) {
|
|
115
|
+
index.encryptionConfiguration = { sseType: "aws:kms", kmsKeyArn: keyArn };
|
|
116
|
+
vectorResources += 1;
|
|
117
|
+
}
|
|
118
|
+
const functions = findResourcesOfCfnType(root, "AWS::Lambda::Function");
|
|
119
|
+
const reservedFunctions = [];
|
|
120
|
+
const functionTargets = [];
|
|
121
|
+
let reservedTotal = 0;
|
|
122
|
+
let logGroups = 0;
|
|
123
|
+
for (const fn of functions) {
|
|
124
|
+
const owner = functionOwnerId(fn);
|
|
125
|
+
const name = `${plan.stackPrefix}-${slug(owner, 24)}-${shortHash(fn.node.path, 10)}`;
|
|
126
|
+
fn.addPropertyOverride("FunctionName", name);
|
|
127
|
+
const group = new CfnLogGroup(Stack.of(fn), `${owner}SharedLogGroup${shortHash(fn.node.path, 6)}`, {
|
|
128
|
+
logGroupName: `/aws/lambda/${name}`,
|
|
129
|
+
kmsKeyId: keyArn,
|
|
130
|
+
retentionInDays: settings.logRetentionDays,
|
|
131
|
+
});
|
|
132
|
+
group.applyRemovalPolicy(RemovalPolicy.RETAIN);
|
|
133
|
+
fn.addDependency(group);
|
|
134
|
+
logGroups += 1;
|
|
135
|
+
if (isInfrastructureFunction(fn)) {
|
|
136
|
+
functionTargets.push({ functionName: name });
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const reserved = settings.reservedConcurrency.overrides[owner] ?? settings.reservedConcurrency.perFunction;
|
|
140
|
+
fn.addPropertyOverride("ReservedConcurrentExecutions", reserved);
|
|
141
|
+
reservedFunctions.push(owner);
|
|
142
|
+
functionTargets.push({ functionName: name, reserved });
|
|
143
|
+
reservedTotal += reserved;
|
|
144
|
+
}
|
|
145
|
+
if (reservedTotal > settings.reservedConcurrency.tierLimit) {
|
|
146
|
+
throw new Error(`The ${plan.tier} shared stack reserves ${reservedTotal} concurrent executions across ${reservedFunctions.length} functions, over its tierLimit of ${settings.reservedConcurrency.tierLimit} in amplify/shared-stack/settings.ts.`);
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
keyArnParameter: plan.keyArnParameter,
|
|
150
|
+
roles: roles.length + rawRoles.length,
|
|
151
|
+
functions: functions.length,
|
|
152
|
+
reservedFunctions,
|
|
153
|
+
functionTargets,
|
|
154
|
+
reservedTotal,
|
|
155
|
+
buckets: buckets.length,
|
|
156
|
+
queues: queues.length,
|
|
157
|
+
tables,
|
|
158
|
+
logGroups,
|
|
159
|
+
vectorResources,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Aws, CfnParameter, Fn, RemovalPolicy, Stack, Token } from "aws-cdk-lib";
|
|
2
|
+
import { CfnRole } from "aws-cdk-lib/aws-iam";
|
|
3
|
+
import { CfnLogGroup } from "aws-cdk-lib/aws-logs";
|
|
4
|
+
import { CfnBucket } from "aws-cdk-lib/aws-s3";
|
|
5
|
+
import { CfnIndex, CfnVectorBucket } from "aws-cdk-lib/aws-s3vectors";
|
|
6
|
+
import { CfnQueue } from "aws-cdk-lib/aws-sqs";
|
|
7
|
+
import { CfnTable } from "aws-cdk-lib/aws-dynamodb";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { appendInlinePolicy, appendInlinePolicyToRawRole, findAllOfType, findResourcesOfCfnType, functionOwnerId, isInfrastructureFunction } from "./inline-policy";
|
|
10
|
+
import type { SharedTierPlan } from "./names";
|
|
11
|
+
import type { SharedStackSettings } from "./settings";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Layers 2 and 3 of the shared tier's isolation, applied to the backend `defineSiteBackend` built (docs/shared-stack.md):
|
|
15
|
+
* the stack's own KMS key on everything that can take one, the permissions boundary and the role path on every role, names
|
|
16
|
+
* that carry the stack's prefix, and reserved concurrency taken from `settings.ts`.
|
|
17
|
+
*
|
|
18
|
+
* The key and the boundary policy are made by the foundation stack (infra/shared-stack/papyrus-shared-foundation.yaml). The
|
|
19
|
+
* backend learns the key's ARN from an SSM parameter at deploy time and the boundary's ARN from its name.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export type SharedBackendHandle = {
|
|
23
|
+
stack: Stack;
|
|
24
|
+
data: { resources: { cfnResources: { amplifyDynamoDbTables: Record<string, { sseSpecification: unknown }> } } };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** What the key could not be attached to, by resource type, because Amplify or the service gives no way to. */
|
|
28
|
+
export const RESOURCES_THAT_CANNOT_TAKE_THE_KEY = [
|
|
29
|
+
{
|
|
30
|
+
resource: "Amplify secrets (secret(\"GOOGLE_CLIENT_ID\") and the other secret() values)",
|
|
31
|
+
reason: "Amplify Gen 2 stores them in SSM under /amplify/<app id>/ with the AWS-managed key and has no hook to choose another key.",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
resource: "SecureString parameters made by custom resources (plugins that write one, such as newsletter)",
|
|
35
|
+
reason: "the shared stack runs no plugin that does; a plugin that is turned on later must be given the key by its custom resource handler.",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
resource: "CloudFront KeyValueStore of the projection publication",
|
|
39
|
+
reason: "CloudFront key value stores have no KMS option; the store holds redirects only, no tenant content.",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
resource: "Cognito user pool",
|
|
43
|
+
reason: "Cognito encrypts with AWS-owned keys and offers no customer-managed key.",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
resource: "AppSync API",
|
|
47
|
+
reason: "AppSync encrypts at rest with AWS-owned keys and offers no customer-managed key; the data lives in the tables and bucket, which take the key.",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
resource: "Lambda environment variables and code",
|
|
51
|
+
reason: "the Amplify function constructs expose no KMS key for them; the variables hold names and settings, no secrets.",
|
|
52
|
+
},
|
|
53
|
+
] as const;
|
|
54
|
+
|
|
55
|
+
export function shortHash(value: string, length = 8): string {
|
|
56
|
+
return createHash("sha256").update(value).digest("hex").slice(0, length);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function slug(value: string, max: number): string {
|
|
60
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, max).replace(/-+$/g, "");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function boundaryPolicyArn(plan: SharedTierPlan): string {
|
|
64
|
+
return `arn:${Aws.PARTITION}:iam::${Aws.ACCOUNT_ID}:policy/${plan.boundaryPolicyName}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The statement a shared role needs to use the stack's key; the boundary also allows it, the key policy admits the role path. */
|
|
68
|
+
export function keyUseStatement(keyArn: string): object {
|
|
69
|
+
return {
|
|
70
|
+
Sid: "UseTheSharedStackKey",
|
|
71
|
+
Effect: "Allow",
|
|
72
|
+
Action: ["kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:DescribeKey", "kms:CreateGrant"],
|
|
73
|
+
Resource: [keyArn],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type SharedIsolationReport = {
|
|
78
|
+
keyArnParameter: string;
|
|
79
|
+
roles: number;
|
|
80
|
+
functions: number;
|
|
81
|
+
reservedFunctions: string[];
|
|
82
|
+
functionTargets: Array<{ functionName: string; reserved?: number }>;
|
|
83
|
+
reservedTotal: number;
|
|
84
|
+
buckets: number;
|
|
85
|
+
queues: number;
|
|
86
|
+
tables: number;
|
|
87
|
+
logGroups: number;
|
|
88
|
+
vectorResources: number;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export function applySharedStackIsolation(backend: SharedBackendHandle, plan: SharedTierPlan, settings: SharedStackSettings): SharedIsolationReport {
|
|
92
|
+
const root = backend.stack;
|
|
93
|
+
const keyParameter = new CfnParameter(root, "PapyrusSharedKmsKeyArn", {
|
|
94
|
+
type: "AWS::SSM::Parameter::Value<String>",
|
|
95
|
+
default: plan.keyArnParameter,
|
|
96
|
+
description: `The ${plan.tier} shared stack's KMS key ARN, written by the ${plan.foundationStackName} stack.`,
|
|
97
|
+
});
|
|
98
|
+
const keyArn = keyParameter.valueAsString;
|
|
99
|
+
const encryption = { sseEnabled: true, sseType: "KMS", kmsMasterKeyId: keyArn };
|
|
100
|
+
|
|
101
|
+
// Amplify's model tables are made by a custom resource, so the key goes through the table wrapper.
|
|
102
|
+
const amplifyTables = Object.values(backend.data.resources.cfnResources.amplifyDynamoDbTables);
|
|
103
|
+
for (const table of amplifyTables) table.sseSpecification = encryption;
|
|
104
|
+
|
|
105
|
+
const roles = findAllOfType(root, CfnRole);
|
|
106
|
+
const boundary = boundaryPolicyArn(plan);
|
|
107
|
+
const keyUsePolicy = { Version: "2012-10-17", Statement: [keyUseStatement(keyArn)] };
|
|
108
|
+
for (const role of roles) {
|
|
109
|
+
role.path = plan.rolePath;
|
|
110
|
+
role.permissionsBoundary = boundary;
|
|
111
|
+
appendInlinePolicy(role, `${plan.stackPrefix}-key-use`, keyUsePolicy);
|
|
112
|
+
}
|
|
113
|
+
// CDK writes the role of a custom resource provider as a raw resource, not a CfnRole.
|
|
114
|
+
const rawRoles = findResourcesOfCfnType(root, "AWS::IAM::Role").filter((role) => !(role instanceof CfnRole));
|
|
115
|
+
for (const role of rawRoles) {
|
|
116
|
+
role.addPropertyOverride("Path", plan.rolePath);
|
|
117
|
+
role.addPropertyOverride("PermissionsBoundary", boundary);
|
|
118
|
+
appendInlinePolicyToRawRole(role, `${plan.stackPrefix}-key-use`, keyUsePolicy);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let tables = amplifyTables.length;
|
|
122
|
+
for (const table of findAllOfType(root, CfnTable)) {
|
|
123
|
+
table.sseSpecification = { sseEnabled: true, sseType: "KMS", kmsMasterKeyId: keyArn };
|
|
124
|
+
tables += 1;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const buckets = findAllOfType(root, CfnBucket);
|
|
128
|
+
for (const bucket of buckets) {
|
|
129
|
+
// A bucket name that carries the stack's prefix lets the boundary and the dedicated deny match objects, which have no tags of their own.
|
|
130
|
+
const named = bucket.bucketName;
|
|
131
|
+
if (typeof named === "string" && !Token.isUnresolved(named)) {
|
|
132
|
+
if (!named.startsWith(`${plan.stackPrefix}-`)) throw new Error(`Bucket ${bucket.node.path} is named ${named}, which does not start with ${plan.stackPrefix}-.`);
|
|
133
|
+
} else {
|
|
134
|
+
bucket.bucketName = Fn.join("-", [plan.stackPrefix, shortHash(bucket.node.path), Aws.ACCOUNT_ID, Aws.REGION]);
|
|
135
|
+
}
|
|
136
|
+
bucket.bucketEncryption = {
|
|
137
|
+
serverSideEncryptionConfiguration: [{ bucketKeyEnabled: true, serverSideEncryptionByDefault: { sseAlgorithm: "aws:kms", kmsMasterKeyId: keyArn } }],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const queues = findAllOfType(root, CfnQueue);
|
|
142
|
+
for (const queue of queues) {
|
|
143
|
+
// SQS-managed and KMS encryption exclude each other.
|
|
144
|
+
queue.sqsManagedSseEnabled = undefined;
|
|
145
|
+
queue.kmsMasterKeyId = keyArn;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let vectorResources = 0;
|
|
149
|
+
for (const vectorBucket of findAllOfType(root, CfnVectorBucket)) {
|
|
150
|
+
vectorBucket.encryptionConfiguration = { sseType: "aws:kms", kmsKeyArn: keyArn };
|
|
151
|
+
vectorResources += 1;
|
|
152
|
+
}
|
|
153
|
+
for (const index of findAllOfType(root, CfnIndex)) {
|
|
154
|
+
index.encryptionConfiguration = { sseType: "aws:kms", kmsKeyArn: keyArn };
|
|
155
|
+
vectorResources += 1;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const functions = findResourcesOfCfnType(root, "AWS::Lambda::Function");
|
|
159
|
+
const reservedFunctions: string[] = [];
|
|
160
|
+
const functionTargets: Array<{ functionName: string; reserved?: number }> = [];
|
|
161
|
+
let reservedTotal = 0;
|
|
162
|
+
let logGroups = 0;
|
|
163
|
+
for (const fn of functions) {
|
|
164
|
+
const owner = functionOwnerId(fn);
|
|
165
|
+
const name = `${plan.stackPrefix}-${slug(owner, 24)}-${shortHash(fn.node.path, 10)}`;
|
|
166
|
+
fn.addPropertyOverride("FunctionName", name);
|
|
167
|
+
const group = new CfnLogGroup(Stack.of(fn), `${owner}SharedLogGroup${shortHash(fn.node.path, 6)}`, {
|
|
168
|
+
logGroupName: `/aws/lambda/${name}`,
|
|
169
|
+
kmsKeyId: keyArn,
|
|
170
|
+
retentionInDays: settings.logRetentionDays,
|
|
171
|
+
});
|
|
172
|
+
group.applyRemovalPolicy(RemovalPolicy.RETAIN);
|
|
173
|
+
fn.addDependency(group);
|
|
174
|
+
logGroups += 1;
|
|
175
|
+
if (isInfrastructureFunction(fn)) {
|
|
176
|
+
functionTargets.push({ functionName: name });
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const reserved = settings.reservedConcurrency.overrides[owner] ?? settings.reservedConcurrency.perFunction;
|
|
180
|
+
fn.addPropertyOverride("ReservedConcurrentExecutions", reserved);
|
|
181
|
+
reservedFunctions.push(owner);
|
|
182
|
+
functionTargets.push({ functionName: name, reserved });
|
|
183
|
+
reservedTotal += reserved;
|
|
184
|
+
}
|
|
185
|
+
if (reservedTotal > settings.reservedConcurrency.tierLimit) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`The ${plan.tier} shared stack reserves ${reservedTotal} concurrent executions across ${reservedFunctions.length} functions, over its tierLimit of ${settings.reservedConcurrency.tierLimit} in amplify/shared-stack/settings.ts.`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
keyArnParameter: plan.keyArnParameter,
|
|
193
|
+
roles: roles.length + rawRoles.length,
|
|
194
|
+
functions: functions.length,
|
|
195
|
+
reservedFunctions,
|
|
196
|
+
functionTargets,
|
|
197
|
+
reservedTotal,
|
|
198
|
+
buckets: buckets.length,
|
|
199
|
+
queues: queues.length,
|
|
200
|
+
tables,
|
|
201
|
+
logGroups,
|
|
202
|
+
vectorResources,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names and statements of the shared tier's isolation layers (PPY-daf673, docs/shared-stack.md,
|
|
3
|
+
* docs/plans/multi-tenant-architecture.md "Isolation inside an existing account").
|
|
4
|
+
*
|
|
5
|
+
* Pure: no CDK imports, so the app shell and the tests can read the same facts. The app shell's package keeps a copy of the
|
|
6
|
+
* deny (infra/amplify-app-shell/lib/shared-stack-deny.ts) because it ships separately; scripts/test-shared-stack.ts fails
|
|
7
|
+
* when the two differ.
|
|
8
|
+
*/
|
|
9
|
+
export const SHARED_TIERS = ["production", "staging"];
|
|
10
|
+
/** Every name the shared tier owns starts with this. No dedicated site's stack or resource does. */
|
|
11
|
+
export const SHARED_FAMILY_PREFIX = "papyrus-shared-";
|
|
12
|
+
/** The cost tag that names a deployment (amplify/vendor/anthus-cost-accounting/tags.ts: TAG_DEPLOYMENT). */
|
|
13
|
+
export const SHARED_DEPLOYMENT_TAG_KEY = "anthus:deployment";
|
|
14
|
+
const PLANS = {
|
|
15
|
+
production: plan("production", "papyrus-shared-prod", "shared-prod", "prod"),
|
|
16
|
+
staging: plan("staging", "papyrus-shared-staging", "shared-staging", "staging"),
|
|
17
|
+
};
|
|
18
|
+
function plan(tier, stackPrefix, deployment, ssmName) {
|
|
19
|
+
return {
|
|
20
|
+
tier,
|
|
21
|
+
stackPrefix,
|
|
22
|
+
deployment,
|
|
23
|
+
foundationStackName: `${stackPrefix}-foundation`,
|
|
24
|
+
rolePath: `/${stackPrefix}/`,
|
|
25
|
+
keyAlias: `alias/${stackPrefix}`,
|
|
26
|
+
boundaryPolicyName: `${stackPrefix}-boundary`,
|
|
27
|
+
keyArnParameter: `/papyrus/shared/${ssmName}/kms-key-arn`,
|
|
28
|
+
ssmPrefix: `/papyrus/shared/${ssmName}`,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function sharedTierPlan(tier) {
|
|
32
|
+
return PLANS[tier];
|
|
33
|
+
}
|
|
34
|
+
export function isSharedTier(value) {
|
|
35
|
+
return value === "production" || value === "staging";
|
|
36
|
+
}
|
|
37
|
+
/** Both tiers' `anthus:deployment` values; the deny on dedicated roles names exactly these. */
|
|
38
|
+
export const SHARED_DEPLOYMENT_VALUES = SHARED_TIERS.map((tier) => PLANS[tier].deployment);
|
|
39
|
+
/**
|
|
40
|
+
* A dedicated site's id is its `anthus:deployment` value, so it must never equal a shared value (a site named
|
|
41
|
+
* `shared-prod` would be denied its own resources and would be billed as the shared tier). `shared` alone is reserved too:
|
|
42
|
+
* the design note and the cost key `shared/papyrus/` use it for the account value.
|
|
43
|
+
*/
|
|
44
|
+
export function isReservedDeployment(siteId) {
|
|
45
|
+
return siteId === "shared" || SHARED_DEPLOYMENT_VALUES.includes(siteId);
|
|
46
|
+
}
|
|
47
|
+
/** The name of the inline policy that carries the deny on a dedicated role. */
|
|
48
|
+
export const SHARED_DENY_POLICY_NAME = "papyrus-deny-shared-stack";
|
|
49
|
+
/**
|
|
50
|
+
* What a dedicated site's role may never touch: any resource that carries a shared `anthus:deployment` tag, and any
|
|
51
|
+
* resource named with the shared prefix. Both are needed: a tag condition is evaluated only for services and actions that
|
|
52
|
+
* support it, and a name pattern needs the resource to be named. Roles that the shared tier's own pipeline and operator use
|
|
53
|
+
* are not dedicated roles and do not carry this.
|
|
54
|
+
*/
|
|
55
|
+
export function sharedStackDenyStatements() {
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
Sid: "DenyTaggedSharedStackResources",
|
|
59
|
+
Effect: "Deny",
|
|
60
|
+
Action: ["*"],
|
|
61
|
+
Resource: ["*"],
|
|
62
|
+
Condition: { StringEquals: { [`aws:ResourceTag/${SHARED_DEPLOYMENT_TAG_KEY}`]: [...SHARED_DEPLOYMENT_VALUES] } },
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
Sid: "DenyNamedSharedStackResources",
|
|
66
|
+
Effect: "Deny",
|
|
67
|
+
Action: ["*"],
|
|
68
|
+
Resource: [
|
|
69
|
+
`arn:aws:cloudformation:*:*:stack/${SHARED_FAMILY_PREFIX}*`,
|
|
70
|
+
`arn:aws:s3:::${SHARED_FAMILY_PREFIX}*`,
|
|
71
|
+
`arn:aws:s3:::${SHARED_FAMILY_PREFIX}*/*`,
|
|
72
|
+
`arn:aws:lambda:*:*:function:${SHARED_FAMILY_PREFIX}*`,
|
|
73
|
+
`arn:aws:logs:*:*:log-group:/aws/lambda/${SHARED_FAMILY_PREFIX}*`,
|
|
74
|
+
`arn:aws:logs:*:*:log-group:/aws/lambda/${SHARED_FAMILY_PREFIX}*:*`,
|
|
75
|
+
`arn:aws:iam::*:role/${SHARED_FAMILY_PREFIX}*`,
|
|
76
|
+
`arn:aws:iam::*:policy/${SHARED_FAMILY_PREFIX}*`,
|
|
77
|
+
`arn:aws:ssm:*:*:parameter/papyrus/shared/*`,
|
|
78
|
+
`arn:aws:sns:*:*:${SHARED_FAMILY_PREFIX}*`,
|
|
79
|
+
`arn:aws:sqs:*:*:${SHARED_FAMILY_PREFIX}*`,
|
|
80
|
+
`arn:aws:cloudwatch:*:*:alarm:${SHARED_FAMILY_PREFIX}*`,
|
|
81
|
+
`arn:aws:kms:*:*:alias/${SHARED_FAMILY_PREFIX}*`,
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
Sid: "DenySharedStackKeys",
|
|
86
|
+
Effect: "Deny",
|
|
87
|
+
Action: ["kms:*"],
|
|
88
|
+
Resource: ["*"],
|
|
89
|
+
Condition: { "ForAnyValue:StringLike": { "kms:ResourceAliases": `alias/${SHARED_FAMILY_PREFIX}*` } },
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
export function sharedStackDenyPolicyDocument() {
|
|
94
|
+
return { Version: "2012-10-17", Statement: sharedStackDenyStatements() };
|
|
95
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names and statements of the shared tier's isolation layers (PPY-daf673, docs/shared-stack.md,
|
|
3
|
+
* docs/plans/multi-tenant-architecture.md "Isolation inside an existing account").
|
|
4
|
+
*
|
|
5
|
+
* Pure: no CDK imports, so the app shell and the tests can read the same facts. The app shell's package keeps a copy of the
|
|
6
|
+
* deny (infra/amplify-app-shell/lib/shared-stack-deny.ts) because it ships separately; scripts/test-shared-stack.ts fails
|
|
7
|
+
* when the two differ.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type SharedTier = "production" | "staging";
|
|
11
|
+
export const SHARED_TIERS: readonly SharedTier[] = ["production", "staging"];
|
|
12
|
+
|
|
13
|
+
/** Every name the shared tier owns starts with this. No dedicated site's stack or resource does. */
|
|
14
|
+
export const SHARED_FAMILY_PREFIX = "papyrus-shared-";
|
|
15
|
+
|
|
16
|
+
/** The cost tag that names a deployment (amplify/vendor/anthus-cost-accounting/tags.ts: TAG_DEPLOYMENT). */
|
|
17
|
+
export const SHARED_DEPLOYMENT_TAG_KEY = "anthus:deployment";
|
|
18
|
+
|
|
19
|
+
export type SharedTierPlan = {
|
|
20
|
+
tier: SharedTier;
|
|
21
|
+
/** The prefix of the tier's stacks, roles, policy, key alias, functions and buckets. Neither tier's prefix starts with the other's. */
|
|
22
|
+
stackPrefix: string;
|
|
23
|
+
/** The `anthus:deployment` value every resource of the tier carries. */
|
|
24
|
+
deployment: string;
|
|
25
|
+
/** The pipeline-made stack that holds the key, the permissions boundary and the budget. */
|
|
26
|
+
foundationStackName: string;
|
|
27
|
+
/** Every role the tier's backend creates sits on this IAM path; the key policy and the boundary read it. */
|
|
28
|
+
rolePath: string;
|
|
29
|
+
keyAlias: string;
|
|
30
|
+
boundaryPolicyName: string;
|
|
31
|
+
/** SSM String parameters the foundation stack writes, read by the backend at deploy time. */
|
|
32
|
+
keyArnParameter: string;
|
|
33
|
+
ssmPrefix: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const PLANS: Record<SharedTier, SharedTierPlan> = {
|
|
37
|
+
production: plan("production", "papyrus-shared-prod", "shared-prod", "prod"),
|
|
38
|
+
staging: plan("staging", "papyrus-shared-staging", "shared-staging", "staging"),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function plan(tier: SharedTier, stackPrefix: string, deployment: string, ssmName: string): SharedTierPlan {
|
|
42
|
+
return {
|
|
43
|
+
tier,
|
|
44
|
+
stackPrefix,
|
|
45
|
+
deployment,
|
|
46
|
+
foundationStackName: `${stackPrefix}-foundation`,
|
|
47
|
+
rolePath: `/${stackPrefix}/`,
|
|
48
|
+
keyAlias: `alias/${stackPrefix}`,
|
|
49
|
+
boundaryPolicyName: `${stackPrefix}-boundary`,
|
|
50
|
+
keyArnParameter: `/papyrus/shared/${ssmName}/kms-key-arn`,
|
|
51
|
+
ssmPrefix: `/papyrus/shared/${ssmName}`,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function sharedTierPlan(tier: SharedTier): SharedTierPlan {
|
|
56
|
+
return PLANS[tier];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isSharedTier(value: unknown): value is SharedTier {
|
|
60
|
+
return value === "production" || value === "staging";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Both tiers' `anthus:deployment` values; the deny on dedicated roles names exactly these. */
|
|
64
|
+
export const SHARED_DEPLOYMENT_VALUES: readonly string[] = SHARED_TIERS.map((tier) => PLANS[tier].deployment);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A dedicated site's id is its `anthus:deployment` value, so it must never equal a shared value (a site named
|
|
68
|
+
* `shared-prod` would be denied its own resources and would be billed as the shared tier). `shared` alone is reserved too:
|
|
69
|
+
* the design note and the cost key `shared/papyrus/` use it for the account value.
|
|
70
|
+
*/
|
|
71
|
+
export function isReservedDeployment(siteId: string): boolean {
|
|
72
|
+
return siteId === "shared" || SHARED_DEPLOYMENT_VALUES.includes(siteId);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type IamStatement = {
|
|
76
|
+
Sid: string;
|
|
77
|
+
Effect: "Allow" | "Deny";
|
|
78
|
+
Action: string[];
|
|
79
|
+
Resource: string[];
|
|
80
|
+
Condition?: Record<string, Record<string, string | string[]>>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** The name of the inline policy that carries the deny on a dedicated role. */
|
|
84
|
+
export const SHARED_DENY_POLICY_NAME = "papyrus-deny-shared-stack";
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a dedicated site's role may never touch: any resource that carries a shared `anthus:deployment` tag, and any
|
|
88
|
+
* resource named with the shared prefix. Both are needed: a tag condition is evaluated only for services and actions that
|
|
89
|
+
* support it, and a name pattern needs the resource to be named. Roles that the shared tier's own pipeline and operator use
|
|
90
|
+
* are not dedicated roles and do not carry this.
|
|
91
|
+
*/
|
|
92
|
+
export function sharedStackDenyStatements(): IamStatement[] {
|
|
93
|
+
return [
|
|
94
|
+
{
|
|
95
|
+
Sid: "DenyTaggedSharedStackResources",
|
|
96
|
+
Effect: "Deny",
|
|
97
|
+
Action: ["*"],
|
|
98
|
+
Resource: ["*"],
|
|
99
|
+
Condition: { StringEquals: { [`aws:ResourceTag/${SHARED_DEPLOYMENT_TAG_KEY}`]: [...SHARED_DEPLOYMENT_VALUES] } },
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
Sid: "DenyNamedSharedStackResources",
|
|
103
|
+
Effect: "Deny",
|
|
104
|
+
Action: ["*"],
|
|
105
|
+
Resource: [
|
|
106
|
+
`arn:aws:cloudformation:*:*:stack/${SHARED_FAMILY_PREFIX}*`,
|
|
107
|
+
`arn:aws:s3:::${SHARED_FAMILY_PREFIX}*`,
|
|
108
|
+
`arn:aws:s3:::${SHARED_FAMILY_PREFIX}*/*`,
|
|
109
|
+
`arn:aws:lambda:*:*:function:${SHARED_FAMILY_PREFIX}*`,
|
|
110
|
+
`arn:aws:logs:*:*:log-group:/aws/lambda/${SHARED_FAMILY_PREFIX}*`,
|
|
111
|
+
`arn:aws:logs:*:*:log-group:/aws/lambda/${SHARED_FAMILY_PREFIX}*:*`,
|
|
112
|
+
`arn:aws:iam::*:role/${SHARED_FAMILY_PREFIX}*`,
|
|
113
|
+
`arn:aws:iam::*:policy/${SHARED_FAMILY_PREFIX}*`,
|
|
114
|
+
`arn:aws:ssm:*:*:parameter/papyrus/shared/*`,
|
|
115
|
+
`arn:aws:sns:*:*:${SHARED_FAMILY_PREFIX}*`,
|
|
116
|
+
`arn:aws:sqs:*:*:${SHARED_FAMILY_PREFIX}*`,
|
|
117
|
+
`arn:aws:cloudwatch:*:*:alarm:${SHARED_FAMILY_PREFIX}*`,
|
|
118
|
+
`arn:aws:kms:*:*:alias/${SHARED_FAMILY_PREFIX}*`,
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
Sid: "DenySharedStackKeys",
|
|
123
|
+
Effect: "Deny",
|
|
124
|
+
Action: ["kms:*"],
|
|
125
|
+
Resource: ["*"],
|
|
126
|
+
Condition: { "ForAnyValue:StringLike": { "kms:ResourceAliases": `alias/${SHARED_FAMILY_PREFIX}*` } },
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function sharedStackDenyPolicyDocument(): { Version: string; Statement: IamStatement[] } {
|
|
132
|
+
return { Version: "2012-10-17", Statement: sharedStackDenyStatements() };
|
|
133
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { SHARED_TIERS } from "./names.js";
|
|
2
|
+
/**
|
|
3
|
+
* The settings of each shared stack (PPY-daf673, PPY-f40a02).
|
|
4
|
+
*
|
|
5
|
+
* - The staging values are a documented starting point so the first create can happen; they MUST be replaced from staging traffic
|
|
6
|
+
* (measured peak concurrent executions per function, alarm history) before shared production is created.
|
|
7
|
+
* - Production has no values on purpose: its synthesis refuses until the reserved concurrency, the tier limit and the alarm
|
|
8
|
+
* thresholds are set from staging traffic.
|
|
9
|
+
* - `perFunction` is the reserved concurrency of every function the stack owns; `overrides` sets one function (the key is the
|
|
10
|
+
* function's construct id). `tierLimit` is the most the stack may reserve in total, so unreserved headroom stays with the
|
|
11
|
+
* dedicated sites (AWS also requires 100 unreserved).
|
|
12
|
+
*/
|
|
13
|
+
export const SHARED_STACK_SETTINGS = {
|
|
14
|
+
tiers: {
|
|
15
|
+
staging: {
|
|
16
|
+
reservedConcurrency: { perFunction: 2, overrides: {}, tierLimit: 120 },
|
|
17
|
+
alarms: {
|
|
18
|
+
periodMinutes: 5,
|
|
19
|
+
functionErrors: 1,
|
|
20
|
+
functionThrottles: 1,
|
|
21
|
+
concurrencyShareOfReserved: 0.8,
|
|
22
|
+
appSyncServerErrors: 1,
|
|
23
|
+
appSyncClientErrors: 50,
|
|
24
|
+
dynamoDbThrottledRequests: 1,
|
|
25
|
+
projectionDeadLetterMessages: 1,
|
|
26
|
+
purgedPathsPerWeek: 25,
|
|
27
|
+
},
|
|
28
|
+
logRetentionDays: 30,
|
|
29
|
+
},
|
|
30
|
+
production: null,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
const POSITIVE_INTEGER_FIELDS = [
|
|
34
|
+
["reservedConcurrency", "perFunction"],
|
|
35
|
+
["reservedConcurrency", "tierLimit"],
|
|
36
|
+
["alarms", "periodMinutes"],
|
|
37
|
+
["alarms", "functionErrors"],
|
|
38
|
+
["alarms", "functionThrottles"],
|
|
39
|
+
["alarms", "appSyncServerErrors"],
|
|
40
|
+
["alarms", "appSyncClientErrors"],
|
|
41
|
+
["alarms", "dynamoDbThrottledRequests"],
|
|
42
|
+
["alarms", "projectionDeadLetterMessages"],
|
|
43
|
+
["alarms", "purgedPathsPerWeek"],
|
|
44
|
+
];
|
|
45
|
+
/** Refuses a missing or malformed value naming the setting, so a bad number never reaches a template. */
|
|
46
|
+
export function validateSharedStackSettings(tier, value) {
|
|
47
|
+
if (value === null || value === undefined) {
|
|
48
|
+
throw new Error(`The ${tier} shared stack has no settings in amplify/shared-stack/settings.ts: its reserved concurrency (PPY-f40a02), tier limit and alarm thresholds must be set from staging traffic before it can be synthesised.`);
|
|
49
|
+
}
|
|
50
|
+
for (const [group, field] of POSITIVE_INTEGER_FIELDS) {
|
|
51
|
+
const number = value[group]?.[field];
|
|
52
|
+
if (!Number.isInteger(number) || number < 1)
|
|
53
|
+
throw new Error(`Shared ${tier} settings: ${group}.${field} must be a positive integer.`);
|
|
54
|
+
}
|
|
55
|
+
const share = value.alarms.concurrencyShareOfReserved;
|
|
56
|
+
if (typeof share !== "number" || !(share > 0 && share <= 1))
|
|
57
|
+
throw new Error(`Shared ${tier} settings: alarms.concurrencyShareOfReserved must be above 0 and at most 1.`);
|
|
58
|
+
if (!Number.isInteger(value.logRetentionDays) || value.logRetentionDays < 1)
|
|
59
|
+
throw new Error(`Shared ${tier} settings: logRetentionDays must be a positive integer.`);
|
|
60
|
+
for (const [name, number] of Object.entries(value.reservedConcurrency.overrides ?? {})) {
|
|
61
|
+
if (!Number.isInteger(number) || number < 1)
|
|
62
|
+
throw new Error(`Shared ${tier} settings: reservedConcurrency.overrides.${name} must be a positive integer.`);
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
export function sharedStackSettings(tier, file = SHARED_STACK_SETTINGS) {
|
|
67
|
+
if (!SHARED_TIERS.includes(tier))
|
|
68
|
+
throw new Error(`Unknown shared tier ${JSON.stringify(tier)}.`);
|
|
69
|
+
return validateSharedStackSettings(tier, file.tiers[tier]);
|
|
70
|
+
}
|