@ericminassian/platform-engine 0.0.0-stage → 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.
- package/dist/clients.d.ts +61 -0
- package/dist/clients.js +122 -0
- package/dist/clients.js.map +1 -0
- package/dist/compat.d.ts +19 -0
- package/dist/compat.js +63 -0
- package/dist/compat.js.map +1 -0
- package/dist/drivers/acm.d.ts +20 -0
- package/dist/drivers/acm.js +62 -0
- package/dist/drivers/acm.js.map +1 -0
- package/dist/drivers/cloudfront.d.ts +52 -0
- package/dist/drivers/cloudfront.js +223 -0
- package/dist/drivers/cloudfront.js.map +1 -0
- package/dist/drivers/cognito.d.ts +27 -0
- package/dist/drivers/cognito.js +151 -0
- package/dist/drivers/cognito.js.map +1 -0
- package/dist/drivers/dynamodb.d.ts +26 -0
- package/dist/drivers/dynamodb.js +151 -0
- package/dist/drivers/dynamodb.js.map +1 -0
- package/dist/drivers/events.d.ts +21 -0
- package/dist/drivers/events.js +62 -0
- package/dist/drivers/events.js.map +1 -0
- package/dist/drivers/iam.d.ts +29 -0
- package/dist/drivers/iam.js +137 -0
- package/dist/drivers/iam.js.map +1 -0
- package/dist/drivers/index.d.ts +16 -0
- package/dist/drivers/index.js +17 -0
- package/dist/drivers/index.js.map +1 -0
- package/dist/drivers/kvs.d.ts +5 -0
- package/dist/drivers/kvs.js +28 -0
- package/dist/drivers/kvs.js.map +1 -0
- package/dist/drivers/lambda.d.ts +48 -0
- package/dist/drivers/lambda.js +158 -0
- package/dist/drivers/lambda.js.map +1 -0
- package/dist/drivers/logs.d.ts +20 -0
- package/dist/drivers/logs.js +62 -0
- package/dist/drivers/logs.js.map +1 -0
- package/dist/drivers/route53.d.ts +30 -0
- package/dist/drivers/route53.js +96 -0
- package/dist/drivers/route53.js.map +1 -0
- package/dist/drivers/s3.d.ts +30 -0
- package/dist/drivers/s3.js +126 -0
- package/dist/drivers/s3.js.map +1 -0
- package/dist/drivers/scheduler.d.ts +21 -0
- package/dist/drivers/scheduler.js +59 -0
- package/dist/drivers/scheduler.js.map +1 -0
- package/dist/drivers/sns.d.ts +7 -0
- package/dist/drivers/sns.js +20 -0
- package/dist/drivers/sns.js.map +1 -0
- package/dist/drivers/sqs.d.ts +18 -0
- package/dist/drivers/sqs.js +46 -0
- package/dist/drivers/sqs.js.map +1 -0
- package/dist/drivers/ssm.d.ts +16 -0
- package/dist/drivers/ssm.js +45 -0
- package/dist/drivers/ssm.js.map +1 -0
- package/dist/drivers/web-function.d.ts +71 -0
- package/dist/drivers/web-function.js +227 -0
- package/dist/drivers/web-function.js.map +1 -0
- package/dist/emulator.d.ts +51 -0
- package/dist/emulator.js +142 -0
- package/dist/emulator.js.map +1 -0
- package/dist/index.d.ts +139 -0
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -0
- package/dist/local.d.ts +12 -0
- package/dist/local.js +71 -0
- package/dist/local.js.map +1 -0
- package/dist/policy.d.ts +47 -0
- package/dist/policy.js +332 -0
- package/dist/policy.js.map +1 -0
- package/dist/stage.d.ts +19 -0
- package/dist/stage.js +479 -0
- package/dist/stage.js.map +1 -0
- package/dist/static.d.ts +9 -0
- package/dist/static.js +55 -0
- package/dist/static.js.map +1 -0
- package/dist/util.d.ts +56 -0
- package/dist/util.js +127 -0
- package/dist/util.js.map +1 -0
- package/package.json +52 -3
- package/src/clients.ts +166 -0
- package/src/compat.ts +63 -0
- package/src/drivers/acm.ts +88 -0
- package/src/drivers/cloudfront.ts +291 -0
- package/src/drivers/cognito.ts +199 -0
- package/src/drivers/dynamodb.ts +206 -0
- package/src/drivers/events.ts +92 -0
- package/src/drivers/iam.ts +195 -0
- package/src/drivers/index.ts +16 -0
- package/src/drivers/kvs.ts +55 -0
- package/src/drivers/lambda.ts +283 -0
- package/src/drivers/logs.ts +81 -0
- package/src/drivers/route53.ts +129 -0
- package/src/drivers/s3.ts +196 -0
- package/src/drivers/scheduler.ts +90 -0
- package/src/drivers/sns.ts +33 -0
- package/src/drivers/sqs.ts +70 -0
- package/src/drivers/ssm.ts +65 -0
- package/src/drivers/web-function.ts +352 -0
- package/src/emulator.ts +203 -0
- package/src/index.ts +182 -0
- package/src/local.ts +75 -0
- package/src/policy.ts +398 -0
- package/src/stage.ts +568 -0
- package/src/static.ts +56 -0
- package/src/util.ts +175 -0
- package/README.md +0 -3
package/src/index.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ericminassian/platform-engine — provisions and deploys platform stages
|
|
3
|
+
* with direct AWS SDK calls. No CloudFormation, no state file: resources have
|
|
4
|
+
* deterministic names (`names` in core) and `platform:*` tags, so the engine
|
|
5
|
+
* can always rediscover what it owns. Every driver is idempotent ("ensure").
|
|
6
|
+
*
|
|
7
|
+
* The same engine runs in three places:
|
|
8
|
+
* - the control plane's deployer (real AWS),
|
|
9
|
+
* - `plat dev` / `plat test` (AWS Emulator, `webMode: 'external'`),
|
|
10
|
+
* - `foundation/` (the platform's own shared resources).
|
|
11
|
+
*
|
|
12
|
+
* CONTRACT: the exported names and types below are used by the CLI, the
|
|
13
|
+
* control plane and the foundation. Keep them stable.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { GetCallerIdentityCommand } from '@aws-sdk/client-sts';
|
|
17
|
+
import type {
|
|
18
|
+
Bindings,
|
|
19
|
+
PlatformRuntimeConfig,
|
|
20
|
+
ResolvedManifest,
|
|
21
|
+
RouteRecord,
|
|
22
|
+
} from '@ericminassian/platform-core';
|
|
23
|
+
import { createClients } from './clients.ts';
|
|
24
|
+
import { putObject } from './drivers/s3.ts';
|
|
25
|
+
import { ensureLocal } from './local.ts';
|
|
26
|
+
import { deployStage, destroyStage, listStageResources, rollbackStage } from './stage.ts';
|
|
27
|
+
import type { EngineContext } from './util.ts';
|
|
28
|
+
|
|
29
|
+
export type { AwsClients } from './clients.ts';
|
|
30
|
+
export * from './drivers/index.ts';
|
|
31
|
+
export * from './emulator.ts';
|
|
32
|
+
export * as policies from './policy.ts';
|
|
33
|
+
export { hostFor } from './stage.ts';
|
|
34
|
+
export { cacheControlFor, contentTypeFor } from './static.ts';
|
|
35
|
+
export type { EngineContext, PolicyDocument, PolicyStatement } from './util.ts';
|
|
36
|
+
export { errorName, isNotFound, isNotImplemented, platformTags, retry, stageTags, waitFor } from './util.ts';
|
|
37
|
+
|
|
38
|
+
export interface EngineLogEntry {
|
|
39
|
+
level: 'info' | 'warn' | 'error';
|
|
40
|
+
msg: string;
|
|
41
|
+
/** e.g. `table foo--prod`. */
|
|
42
|
+
resource?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface EngineOptions {
|
|
46
|
+
region: string;
|
|
47
|
+
/** Target the AWS Emulator instead of AWS (endpoint, CA, static credentials + compat shims). */
|
|
48
|
+
emulator?: {
|
|
49
|
+
endpoint: string;
|
|
50
|
+
caFile: string;
|
|
51
|
+
credentials: { accessKeyId: string; secretAccessKey: string };
|
|
52
|
+
};
|
|
53
|
+
/** Explicit credentials; default is the AWS SDK default chain. */
|
|
54
|
+
credentials?: { accessKeyId: string; secretAccessKey: string; sessionToken?: string };
|
|
55
|
+
log?: (e: EngineLogEntry) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface S3Location {
|
|
59
|
+
bucket: string;
|
|
60
|
+
key: string;
|
|
61
|
+
versionId?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface DeployStageInput {
|
|
65
|
+
manifest: ResolvedManifest;
|
|
66
|
+
stage: string;
|
|
67
|
+
deploymentId: string;
|
|
68
|
+
platform: PlatformRuntimeConfig;
|
|
69
|
+
/** Uploaded build artifacts (zips) in the artifacts bucket. */
|
|
70
|
+
artifacts: { server?: S3Location; worker?: S3Location; static?: S3Location };
|
|
71
|
+
/**
|
|
72
|
+
* `function`: create/update the Lambda Web Function + route record (AWS).
|
|
73
|
+
* `external`: skip both; `plat dev` runs the server process itself because
|
|
74
|
+
* the emulator does not serve Web Function endpoints yet.
|
|
75
|
+
*/
|
|
76
|
+
webMode?: 'function' | 'external';
|
|
77
|
+
/** Use this role instead of creating the per-stage role (the control plane runs privileged). */
|
|
78
|
+
roleArn?: string;
|
|
79
|
+
/** Extra environment variables for web + worker (merged over manifest `env`). */
|
|
80
|
+
extraEnv?: Record<string, string>;
|
|
81
|
+
logRetentionDays?: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ResourceRef {
|
|
85
|
+
/** `role`, `table`, `queue`, `bus`, `rule`, `schedule-group`, `schedule`, `log-group`, `web-function`, `function`, `esm`, `user-pool-client`, `route`, `static` */
|
|
86
|
+
type: string;
|
|
87
|
+
name: string;
|
|
88
|
+
arn?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DeployStageResult {
|
|
92
|
+
bindings: Bindings;
|
|
93
|
+
host: string;
|
|
94
|
+
url: string;
|
|
95
|
+
route?: RouteRecord;
|
|
96
|
+
webFunction?: { name: string; endpointDomain: string; revisionId: string };
|
|
97
|
+
workerFunction?: { name: string; arn: string };
|
|
98
|
+
resources: ResourceRef[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface RollbackStageInput {
|
|
102
|
+
app: string;
|
|
103
|
+
stage: string;
|
|
104
|
+
platform: PlatformRuntimeConfig;
|
|
105
|
+
/** Route record of the deployment to go back to (its static prefix + endpoint). */
|
|
106
|
+
route: RouteRecord;
|
|
107
|
+
/** Web Function revision to route 100% of traffic to. */
|
|
108
|
+
revisionId?: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface DestroyStageInput {
|
|
112
|
+
app: string;
|
|
113
|
+
stage: string;
|
|
114
|
+
platform: PlatformRuntimeConfig;
|
|
115
|
+
/** Keep the table and storage objects (default false; prod should pass true). */
|
|
116
|
+
keepData?: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface Engine {
|
|
120
|
+
readonly region: string;
|
|
121
|
+
readonly target: 'aws' | 'emulator';
|
|
122
|
+
accountId(): Promise<string>;
|
|
123
|
+
deployStage(input: DeployStageInput): Promise<DeployStageResult>;
|
|
124
|
+
rollbackStage(input: RollbackStageInput): Promise<void>;
|
|
125
|
+
destroyStage(input: DestroyStageInput): Promise<void>;
|
|
126
|
+
/** Everything the engine owns for a stage (found by name + tags). */
|
|
127
|
+
listStageResources(app: string, stage: string): Promise<ResourceRef[]>;
|
|
128
|
+
/** Upload a local file/buffer into the artifacts bucket (used by `plat dev`). */
|
|
129
|
+
putArtifact(platform: PlatformRuntimeConfig, key: string, body: Uint8Array): Promise<S3Location>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function createEngine(opts: EngineOptions): Engine {
|
|
133
|
+
const ctx = createContext(opts);
|
|
134
|
+
const engine: Engine = {
|
|
135
|
+
region: opts.region,
|
|
136
|
+
target: ctx.target,
|
|
137
|
+
accountId: () => ctx.accountId(),
|
|
138
|
+
deployStage: (input) => deployStage(ctx, input),
|
|
139
|
+
rollbackStage: (input) => rollbackStage(ctx, input),
|
|
140
|
+
destroyStage: (input) => destroyStage(ctx, input),
|
|
141
|
+
listStageResources: (app, stage) => listStageResources(ctx, app, stage),
|
|
142
|
+
putArtifact: (platform, key, body) => putObject(ctx, { bucket: platform.artifactsBucket, key, body }),
|
|
143
|
+
};
|
|
144
|
+
contexts.set(engine, ctx);
|
|
145
|
+
return engine;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const contexts = new WeakMap<Engine, EngineContext>();
|
|
149
|
+
|
|
150
|
+
/** Driver-level access (clients, logger) for advanced callers such as `foundation/`. */
|
|
151
|
+
export function engineContext(engine: Engine): EngineContext {
|
|
152
|
+
const ctx = contexts.get(engine);
|
|
153
|
+
if (!ctx) throw new Error('not an engine created by createEngine');
|
|
154
|
+
return ctx;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function createContext(opts: EngineOptions): EngineContext {
|
|
158
|
+
const clients = createClients(opts);
|
|
159
|
+
const target = opts.emulator ? 'emulator' : 'aws';
|
|
160
|
+
let account: Promise<string> | undefined;
|
|
161
|
+
return {
|
|
162
|
+
clients,
|
|
163
|
+
region: opts.region,
|
|
164
|
+
target,
|
|
165
|
+
log: (level, msg, resource) => opts.log?.({ level, msg, ...(resource ? { resource } : {}) }),
|
|
166
|
+
accountId: () => {
|
|
167
|
+
account ??= clients.sts.send(new GetCallerIdentityCommand({})).then((r) => r.Account!);
|
|
168
|
+
return account;
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Shared resources `plat dev` needs inside a fresh emulator (buckets, tables,
|
|
175
|
+
* user pool) and the matching runtime config (`target: 'emulator'`).
|
|
176
|
+
*/
|
|
177
|
+
export async function ensureLocalPlatform(
|
|
178
|
+
engine: Engine,
|
|
179
|
+
opts: { domain?: string; port: number; jwksBaseUrl: string },
|
|
180
|
+
): Promise<PlatformRuntimeConfig> {
|
|
181
|
+
return ensureLocal(engineContext(engine), opts);
|
|
182
|
+
}
|
package/src/local.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type PlatformRuntimeConfig, platformNames } from '@ericminassian/platform-core';
|
|
2
|
+
import { ensureUserPool } from './drivers/cognito.ts';
|
|
3
|
+
import { ensureTable } from './drivers/dynamodb.ts';
|
|
4
|
+
import { ensureBucket } from './drivers/s3.ts';
|
|
5
|
+
import { type EngineContext, platformTags } from './util.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Inside a fresh AWS Emulator, create the shared resources a stage deploy
|
|
9
|
+
* expects (the same ones `foundation/` creates in AWS, minus CloudFront,
|
|
10
|
+
* DNS and certificates) and describe them as a runtime config.
|
|
11
|
+
*/
|
|
12
|
+
export async function ensureLocal(
|
|
13
|
+
ctx: EngineContext,
|
|
14
|
+
opts: { domain?: string; port: number; jwksBaseUrl: string },
|
|
15
|
+
): Promise<PlatformRuntimeConfig> {
|
|
16
|
+
if (ctx.target !== 'emulator') throw new Error('ensureLocalPlatform only runs against the AWS Emulator');
|
|
17
|
+
const accountId = await ctx.accountId();
|
|
18
|
+
const region = ctx.region;
|
|
19
|
+
const tags = platformTags('local');
|
|
20
|
+
const artifactsBucket = platformNames.artifactsBucket(accountId, region);
|
|
21
|
+
const sitesBucket = platformNames.sitesBucket(accountId, region);
|
|
22
|
+
const storageBucket = platformNames.storageBucket(accountId, region);
|
|
23
|
+
|
|
24
|
+
await ensureBucket(ctx, { name: artifactsBucket, versioned: true, tags });
|
|
25
|
+
await ensureBucket(ctx, { name: sitesBucket, tags });
|
|
26
|
+
await ensureBucket(ctx, {
|
|
27
|
+
name: storageBucket,
|
|
28
|
+
tags,
|
|
29
|
+
cors: [
|
|
30
|
+
{
|
|
31
|
+
AllowedOrigins: [
|
|
32
|
+
`http://*.${opts.domain ?? 'localhost'}:${opts.port}`,
|
|
33
|
+
`http://localhost:${opts.port}`,
|
|
34
|
+
],
|
|
35
|
+
AllowedMethods: ['GET', 'PUT', 'POST', 'HEAD'],
|
|
36
|
+
AllowedHeaders: ['*'],
|
|
37
|
+
ExposeHeaders: ['ETag'],
|
|
38
|
+
MaxAgeSeconds: 3600,
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
await ensureTable(ctx, {
|
|
43
|
+
name: platformNames.stateTable,
|
|
44
|
+
pk: 'pk',
|
|
45
|
+
sk: 'sk',
|
|
46
|
+
gsis: [['gsi1', 'gsi1pk', 'gsi1sk']],
|
|
47
|
+
tags,
|
|
48
|
+
});
|
|
49
|
+
await ensureTable(ctx, {
|
|
50
|
+
name: platformNames.telemetryTable,
|
|
51
|
+
pk: 'pk',
|
|
52
|
+
sk: 'sk',
|
|
53
|
+
ttlAttribute: 'ttl',
|
|
54
|
+
tags,
|
|
55
|
+
});
|
|
56
|
+
const userPoolId = await ensureUserPool(ctx, { name: platformNames.userPool, tags });
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
v: 1,
|
|
60
|
+
target: 'emulator',
|
|
61
|
+
accountId,
|
|
62
|
+
region,
|
|
63
|
+
domain: opts.domain ?? 'localhost',
|
|
64
|
+
publicScheme: 'http',
|
|
65
|
+
publicPort: opts.port,
|
|
66
|
+
artifactsBucket,
|
|
67
|
+
sitesBucket,
|
|
68
|
+
storageBucket,
|
|
69
|
+
stateTable: platformNames.stateTable,
|
|
70
|
+
telemetryTable: platformNames.telemetryTable,
|
|
71
|
+
userPoolId,
|
|
72
|
+
loginUrl: `http://localhost:${opts.port}`,
|
|
73
|
+
jwksBaseUrl: opts.jwksBaseUrl,
|
|
74
|
+
};
|
|
75
|
+
}
|
package/src/policy.ts
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IAM_APP_PATH,
|
|
3
|
+
names,
|
|
4
|
+
type PlatformRuntimeConfig,
|
|
5
|
+
platformNames,
|
|
6
|
+
type ResolvedManifest,
|
|
7
|
+
} from '@ericminassian/platform-core';
|
|
8
|
+
import { type PolicyDocument, type PolicyStatement, policy } from './util.ts';
|
|
9
|
+
|
|
10
|
+
export interface StagePolicyInput {
|
|
11
|
+
app: string;
|
|
12
|
+
stage: string;
|
|
13
|
+
region: string;
|
|
14
|
+
accountId: string;
|
|
15
|
+
manifest: ResolvedManifest;
|
|
16
|
+
platform: Pick<PlatformRuntimeConfig, 'storageBucket'>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Trust for per-stage roles: the worker and Web Function (Lambda) and schedules (Scheduler). */
|
|
20
|
+
export function stageTrustPolicy(): PolicyDocument {
|
|
21
|
+
return policy({
|
|
22
|
+
Effect: 'Allow',
|
|
23
|
+
Principal: { Service: ['lambda.amazonaws.com', 'scheduler.amazonaws.com'] },
|
|
24
|
+
Action: 'sts:AssumeRole',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Exactly what the runtime SDK needs for one stage, nothing more. Every
|
|
30
|
+
* resource is addressed by its deterministic name, and app/stage names can
|
|
31
|
+
* never contain `--`, so `<app>--<stage>--*` cannot match another stage.
|
|
32
|
+
*/
|
|
33
|
+
export function stageRolePolicy(input: StagePolicyInput): PolicyDocument {
|
|
34
|
+
const { app, stage, region, accountId: acct, manifest } = input;
|
|
35
|
+
const base = names.base(app, stage);
|
|
36
|
+
const statements: PolicyStatement[] = [];
|
|
37
|
+
|
|
38
|
+
statements.push({
|
|
39
|
+
Sid: 'Logs',
|
|
40
|
+
Effect: 'Allow',
|
|
41
|
+
Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'],
|
|
42
|
+
Resource: [
|
|
43
|
+
`arn:aws:logs:${region}:${acct}:log-group:${names.logGroupPrefix(app, stage)}*`,
|
|
44
|
+
`arn:aws:logs:${region}:${acct}:log-group:${names.logGroupPrefix(app, stage)}*:log-stream:*`,
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (manifest.db) {
|
|
49
|
+
const table = `arn:aws:dynamodb:${region}:${acct}:table/${names.table(app, stage)}`;
|
|
50
|
+
statements.push({
|
|
51
|
+
Sid: 'Db',
|
|
52
|
+
Effect: 'Allow',
|
|
53
|
+
Action: [
|
|
54
|
+
'dynamodb:GetItem',
|
|
55
|
+
'dynamodb:BatchGetItem',
|
|
56
|
+
'dynamodb:PutItem',
|
|
57
|
+
'dynamodb:UpdateItem',
|
|
58
|
+
'dynamodb:DeleteItem',
|
|
59
|
+
'dynamodb:BatchWriteItem',
|
|
60
|
+
'dynamodb:Query',
|
|
61
|
+
'dynamodb:Scan',
|
|
62
|
+
'dynamodb:ConditionCheckItem',
|
|
63
|
+
'dynamodb:DescribeTable',
|
|
64
|
+
],
|
|
65
|
+
Resource: [table, `${table}/index/*`],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (Object.keys(manifest.queues).length > 0) {
|
|
70
|
+
statements.push({
|
|
71
|
+
Sid: 'Queues',
|
|
72
|
+
Effect: 'Allow',
|
|
73
|
+
Action: [
|
|
74
|
+
'sqs:SendMessage',
|
|
75
|
+
'sqs:ReceiveMessage',
|
|
76
|
+
'sqs:DeleteMessage',
|
|
77
|
+
'sqs:GetQueueAttributes',
|
|
78
|
+
'sqs:GetQueueUrl',
|
|
79
|
+
'sqs:ChangeMessageVisibility',
|
|
80
|
+
],
|
|
81
|
+
Resource: `arn:aws:sqs:${region}:${acct}:${base}--*`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
statements.push({
|
|
86
|
+
Sid: 'Events',
|
|
87
|
+
Effect: 'Allow',
|
|
88
|
+
Action: 'events:PutEvents',
|
|
89
|
+
Resource: `arn:aws:events:${region}:${acct}:event-bus/${names.eventBus(app, stage)}`,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (manifest.storage.length > 0) {
|
|
93
|
+
const prefix = names.storagePrefix(app, stage);
|
|
94
|
+
statements.push(
|
|
95
|
+
{
|
|
96
|
+
Sid: 'StorageObjects',
|
|
97
|
+
Effect: 'Allow',
|
|
98
|
+
Action: ['s3:GetObject', 's3:PutObject', 's3:DeleteObject'],
|
|
99
|
+
Resource: `arn:aws:s3:::${input.platform.storageBucket}/${prefix}*`,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
Sid: 'StorageList',
|
|
103
|
+
Effect: 'Allow',
|
|
104
|
+
Action: 's3:ListBucket',
|
|
105
|
+
Resource: `arn:aws:s3:::${input.platform.storageBucket}`,
|
|
106
|
+
Condition: { StringLike: { 's3:prefix': [`${prefix}*`] } },
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const secrets = names.secretsPath(app, stage).replace(/\/$/, '');
|
|
112
|
+
const shared = names.sharedSecretsPath(app).replace(/\/$/, '');
|
|
113
|
+
statements.push(
|
|
114
|
+
{
|
|
115
|
+
Sid: 'Secrets',
|
|
116
|
+
Effect: 'Allow',
|
|
117
|
+
Action: ['ssm:GetParameter', 'ssm:GetParameters', 'ssm:GetParametersByPath'],
|
|
118
|
+
Resource: [secrets, `${secrets}/*`, shared, `${shared}/*`].map(
|
|
119
|
+
(p) => `arn:aws:ssm:${region}:${acct}:parameter${p}`,
|
|
120
|
+
),
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
Sid: 'SecretsDecrypt',
|
|
124
|
+
Effect: 'Allow',
|
|
125
|
+
Action: 'kms:Decrypt',
|
|
126
|
+
Resource: `arn:aws:kms:${region}:${acct}:key/*`,
|
|
127
|
+
Condition: { StringEquals: { 'kms:ViaService': `ssm.${region}.amazonaws.com` } },
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
if (manifest.worker) {
|
|
132
|
+
statements.push({
|
|
133
|
+
Sid: 'InvokeWorker',
|
|
134
|
+
Effect: 'Allow',
|
|
135
|
+
Action: 'lambda:InvokeFunction',
|
|
136
|
+
Resource: `arn:aws:lambda:${region}:${acct}:function:${names.workerFunction(app, stage)}`,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return policy(...statements);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Permission boundary on every per-stage role: the union of what any stage
|
|
145
|
+
* role may ever do, scoped to platform naming patterns. Even if a stage
|
|
146
|
+
* policy were wrong, a stage role can't touch platform state, IAM or other
|
|
147
|
+
* accounts' resources.
|
|
148
|
+
*/
|
|
149
|
+
export function boundaryPolicy(input: {
|
|
150
|
+
region: string;
|
|
151
|
+
accountId: string;
|
|
152
|
+
storageBucket: string;
|
|
153
|
+
}): PolicyDocument {
|
|
154
|
+
const { region, accountId: acct } = input;
|
|
155
|
+
return policy(
|
|
156
|
+
{
|
|
157
|
+
Sid: 'StageLogs',
|
|
158
|
+
Effect: 'Allow',
|
|
159
|
+
Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'],
|
|
160
|
+
Resource: `arn:aws:logs:${region}:${acct}:log-group:/platform/apps/*`,
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
Sid: 'StageData',
|
|
164
|
+
Effect: 'Allow',
|
|
165
|
+
Action: ['dynamodb:*Item', 'dynamodb:Query', 'dynamodb:Scan', 'dynamodb:DescribeTable'],
|
|
166
|
+
Resource: [
|
|
167
|
+
`arn:aws:dynamodb:${region}:${acct}:table/*--*`,
|
|
168
|
+
`arn:aws:dynamodb:${region}:${acct}:table/*--*/index/*`,
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
Sid: 'StageQueues',
|
|
173
|
+
Effect: 'Allow',
|
|
174
|
+
Action: [
|
|
175
|
+
'sqs:SendMessage',
|
|
176
|
+
'sqs:ReceiveMessage',
|
|
177
|
+
'sqs:DeleteMessage',
|
|
178
|
+
'sqs:GetQueueAttributes',
|
|
179
|
+
'sqs:GetQueueUrl',
|
|
180
|
+
'sqs:ChangeMessageVisibility',
|
|
181
|
+
],
|
|
182
|
+
Resource: `arn:aws:sqs:${region}:${acct}:*--*`,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
Sid: 'StageEvents',
|
|
186
|
+
Effect: 'Allow',
|
|
187
|
+
Action: 'events:PutEvents',
|
|
188
|
+
Resource: `arn:aws:events:${region}:${acct}:event-bus/*--*`,
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
Sid: 'StageStorage',
|
|
192
|
+
Effect: 'Allow',
|
|
193
|
+
Action: ['s3:GetObject', 's3:PutObject', 's3:DeleteObject', 's3:ListBucket'],
|
|
194
|
+
Resource: [`arn:aws:s3:::${input.storageBucket}`, `arn:aws:s3:::${input.storageBucket}/*`],
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
Sid: 'StageSecrets',
|
|
198
|
+
Effect: 'Allow',
|
|
199
|
+
Action: ['ssm:GetParameter', 'ssm:GetParameters', 'ssm:GetParametersByPath', 'kms:Decrypt'],
|
|
200
|
+
Resource: [
|
|
201
|
+
`arn:aws:ssm:${region}:${acct}:parameter/platform/apps/*`,
|
|
202
|
+
`arn:aws:kms:${region}:${acct}:key/*`,
|
|
203
|
+
],
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
Sid: 'StageWorkers',
|
|
207
|
+
Effect: 'Allow',
|
|
208
|
+
Action: 'lambda:InvokeFunction',
|
|
209
|
+
Resource: `arn:aws:lambda:${region}:${acct}:function:*--*--worker`,
|
|
210
|
+
},
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The control plane (API + deployer + telemetry) manages every stage's
|
|
216
|
+
* resources, reads telemetry and costs, and writes the route store. It may
|
|
217
|
+
* only create roles under the app path, and only with the boundary attached.
|
|
218
|
+
*/
|
|
219
|
+
export function controlPlanePolicy(input: {
|
|
220
|
+
region: string;
|
|
221
|
+
accountId: string;
|
|
222
|
+
runtime: Pick<
|
|
223
|
+
PlatformRuntimeConfig,
|
|
224
|
+
| 'artifactsBucket'
|
|
225
|
+
| 'sitesBucket'
|
|
226
|
+
| 'storageBucket'
|
|
227
|
+
| 'kvsArn'
|
|
228
|
+
| 'userPoolId'
|
|
229
|
+
| 'boundaryPolicyArn'
|
|
230
|
+
| 'alertsTopicArn'
|
|
231
|
+
>;
|
|
232
|
+
}): PolicyDocument {
|
|
233
|
+
const { region, accountId: acct, runtime } = input;
|
|
234
|
+
const appRoles = `arn:aws:iam::${acct}:role${IAM_APP_PATH}*`;
|
|
235
|
+
const statements: PolicyStatement[] = [
|
|
236
|
+
{
|
|
237
|
+
Sid: 'StageRoles',
|
|
238
|
+
Effect: 'Allow',
|
|
239
|
+
Action: ['iam:CreateRole', 'iam:PutRolePermissionsBoundary'],
|
|
240
|
+
Resource: appRoles,
|
|
241
|
+
...(runtime.boundaryPolicyArn
|
|
242
|
+
? { Condition: { StringEquals: { 'iam:PermissionsBoundary': runtime.boundaryPolicyArn } } }
|
|
243
|
+
: {}),
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
// IAM authorizes GetRole on a role that doesn't exist yet against the
|
|
247
|
+
// path-less ARN (`role/<name>`), so a path-scoped grant can't cover the
|
|
248
|
+
// "does it exist?" lookup. Reading role metadata is harmless; everything
|
|
249
|
+
// that changes a role stays scoped to the app path below.
|
|
250
|
+
Sid: 'StageRolesLookup',
|
|
251
|
+
Effect: 'Allow',
|
|
252
|
+
Action: ['iam:GetRole'],
|
|
253
|
+
Resource: `arn:aws:iam::${acct}:role/*`,
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
Sid: 'StageRolesManage',
|
|
257
|
+
Effect: 'Allow',
|
|
258
|
+
Action: [
|
|
259
|
+
'iam:DeleteRole',
|
|
260
|
+
'iam:UpdateAssumeRolePolicy',
|
|
261
|
+
'iam:PutRolePolicy',
|
|
262
|
+
'iam:GetRolePolicy',
|
|
263
|
+
'iam:DeleteRolePolicy',
|
|
264
|
+
'iam:ListRolePolicies',
|
|
265
|
+
'iam:ListAttachedRolePolicies',
|
|
266
|
+
'iam:TagRole',
|
|
267
|
+
'iam:UntagRole',
|
|
268
|
+
'iam:PassRole',
|
|
269
|
+
],
|
|
270
|
+
Resource: appRoles,
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
Sid: 'StageCompute',
|
|
274
|
+
Effect: 'Allow',
|
|
275
|
+
Action: ['lambda:*'],
|
|
276
|
+
Resource: [
|
|
277
|
+
`arn:aws:lambda:${region}:${acct}:function:*--*`,
|
|
278
|
+
`arn:aws:lambda:${region}:${acct}:web-function/*--*`,
|
|
279
|
+
`arn:aws:lambda:${region}:${acct}:web-function/*--*/*`,
|
|
280
|
+
`arn:aws:lambda:${region}:${acct}:event-source-mapping:*`,
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
Sid: 'StageComputeList',
|
|
285
|
+
Effect: 'Allow',
|
|
286
|
+
Action: [
|
|
287
|
+
'lambda:ListEventSourceMappings',
|
|
288
|
+
'lambda:CreateEventSourceMapping',
|
|
289
|
+
'lambda:ListFunctions',
|
|
290
|
+
'lambda:ListWebFunctions',
|
|
291
|
+
],
|
|
292
|
+
Resource: '*',
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
Sid: 'StageData',
|
|
296
|
+
Effect: 'Allow',
|
|
297
|
+
Action: ['dynamodb:*'],
|
|
298
|
+
Resource: [`arn:aws:dynamodb:${region}:${acct}:table/*`],
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
Sid: 'StageMessaging',
|
|
302
|
+
Effect: 'Allow',
|
|
303
|
+
Action: ['sqs:*', 'events:*', 'scheduler:*'],
|
|
304
|
+
Resource: '*',
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
Sid: 'Logs',
|
|
308
|
+
Effect: 'Allow',
|
|
309
|
+
Action: ['logs:*'],
|
|
310
|
+
Resource: '*',
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
Sid: 'PlatformBuckets',
|
|
314
|
+
Effect: 'Allow',
|
|
315
|
+
Action: ['s3:*'],
|
|
316
|
+
Resource: [runtime.artifactsBucket, runtime.sitesBucket, runtime.storageBucket].flatMap((b) => [
|
|
317
|
+
`arn:aws:s3:::${b}`,
|
|
318
|
+
`arn:aws:s3:::${b}/*`,
|
|
319
|
+
]),
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
Sid: 'Parameters',
|
|
323
|
+
Effect: 'Allow',
|
|
324
|
+
Action: ['ssm:*Parameter*', 'kms:Decrypt', 'kms:Encrypt', 'kms:GenerateDataKey'],
|
|
325
|
+
Resource: [`arn:aws:ssm:${region}:${acct}:parameter/platform/*`, `arn:aws:kms:${region}:${acct}:key/*`],
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
Sid: 'ReadOnlyTelemetry',
|
|
329
|
+
Effect: 'Allow',
|
|
330
|
+
Action: [
|
|
331
|
+
'cloudwatch:GetMetricData',
|
|
332
|
+
'cloudwatch:ListMetrics',
|
|
333
|
+
'ce:GetCostAndUsage',
|
|
334
|
+
'ce:GetCostForecast',
|
|
335
|
+
'tag:GetResources',
|
|
336
|
+
'sts:GetCallerIdentity',
|
|
337
|
+
],
|
|
338
|
+
Resource: '*',
|
|
339
|
+
},
|
|
340
|
+
];
|
|
341
|
+
if (runtime.kvsArn) {
|
|
342
|
+
statements.push({
|
|
343
|
+
Sid: 'Routes',
|
|
344
|
+
Effect: 'Allow',
|
|
345
|
+
Action: ['cloudfront-keyvaluestore:*', 'cloudfront:DescribeKeyValueStore'],
|
|
346
|
+
Resource: runtime.kvsArn,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
if (runtime.userPoolId) {
|
|
350
|
+
statements.push({
|
|
351
|
+
Sid: 'AppClients',
|
|
352
|
+
Effect: 'Allow',
|
|
353
|
+
Action: [
|
|
354
|
+
'cognito-idp:*UserPoolClient*',
|
|
355
|
+
'cognito-idp:*ManagedLoginBranding*',
|
|
356
|
+
'cognito-idp:ListUsers',
|
|
357
|
+
'cognito-idp:AdminGetUser',
|
|
358
|
+
],
|
|
359
|
+
Resource: `arn:aws:cognito-idp:${region}:${acct}:userpool/${runtime.userPoolId}`,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
if (runtime.alertsTopicArn) {
|
|
363
|
+
statements.push({
|
|
364
|
+
Sid: 'Alerts',
|
|
365
|
+
Effect: 'Allow',
|
|
366
|
+
Action: 'sns:Publish',
|
|
367
|
+
Resource: runtime.alertsTopicArn,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return policy(...statements);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Trust for the platform repo's CI role: GitHub Actions OIDC, main branch only. */
|
|
374
|
+
export function githubCiTrustPolicy(input: {
|
|
375
|
+
accountId: string;
|
|
376
|
+
owner: string;
|
|
377
|
+
repo: string;
|
|
378
|
+
}): PolicyDocument {
|
|
379
|
+
return policy({
|
|
380
|
+
Effect: 'Allow',
|
|
381
|
+
Principal: {
|
|
382
|
+
Federated: `arn:aws:iam::${input.accountId}:oidc-provider/token.actions.githubusercontent.com`,
|
|
383
|
+
},
|
|
384
|
+
Action: 'sts:AssumeRoleWithWebIdentity',
|
|
385
|
+
Condition: {
|
|
386
|
+
StringEquals: {
|
|
387
|
+
'token.actions.githubusercontent.com:aud': 'sts.amazonaws.com',
|
|
388
|
+
'token.actions.githubusercontent.com:sub': `repo:${input.owner}/${input.repo}:ref:refs/heads/main`,
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function serviceTrustPolicy(...services: string[]): PolicyDocument {
|
|
395
|
+
return policy({ Effect: 'Allow', Principal: { Service: services }, Action: 'sts:AssumeRole' });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export const BOUNDARY_POLICY_NAME = platformNames.boundaryPolicy;
|