@ericminassian/platform-engine 0.3.0 → 0.4.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/dist/clients.d.ts +8 -0
- package/dist/clients.js +22 -1
- package/dist/clients.js.map +1 -1
- package/dist/compat.d.ts +21 -9
- package/dist/compat.js +27 -7
- package/dist/compat.js.map +1 -1
- package/dist/drivers/appsync.d.ts +49 -0
- package/dist/drivers/appsync.js +155 -0
- package/dist/drivers/appsync.js.map +1 -0
- package/dist/drivers/cognito.d.ts +9 -3
- package/dist/drivers/cognito.js +18 -7
- package/dist/drivers/cognito.js.map +1 -1
- package/dist/drivers/ecr.d.ts +109 -0
- package/dist/drivers/ecr.js +303 -0
- package/dist/drivers/ecr.js.map +1 -0
- package/dist/drivers/ecs.d.ts +163 -0
- package/dist/drivers/ecs.js +447 -0
- package/dist/drivers/ecs.js.map +1 -0
- package/dist/drivers/events.d.ts +10 -0
- package/dist/drivers/events.js +13 -1
- package/dist/drivers/events.js.map +1 -1
- package/dist/drivers/index.d.ts +5 -0
- package/dist/drivers/index.js +5 -0
- package/dist/drivers/index.js.map +1 -1
- package/dist/drivers/kvs.d.ts +3 -0
- package/dist/drivers/kvs.js +14 -7
- package/dist/drivers/kvs.js.map +1 -1
- package/dist/drivers/lambda.d.ts +22 -0
- package/dist/drivers/lambda.js +74 -3
- package/dist/drivers/lambda.js.map +1 -1
- package/dist/drivers/request-function.d.ts +60 -0
- package/dist/drivers/request-function.js +167 -0
- package/dist/drivers/request-function.js.map +1 -0
- package/dist/drivers/s3.d.ts +5 -0
- package/dist/drivers/s3.js +26 -0
- package/dist/drivers/s3.js.map +1 -1
- package/dist/drivers/scheduler.d.ts +24 -0
- package/dist/drivers/scheduler.js +60 -5
- package/dist/drivers/scheduler.js.map +1 -1
- package/dist/drivers/ses.d.ts +77 -0
- package/dist/drivers/ses.js +181 -0
- package/dist/drivers/ses.js.map +1 -0
- package/dist/drivers/sqs.d.ts +11 -0
- package/dist/drivers/sqs.js +67 -1
- package/dist/drivers/sqs.js.map +1 -1
- package/dist/drivers/web-function.d.ts +11 -3
- package/dist/drivers/web-function.js +22 -5
- package/dist/drivers/web-function.js.map +1 -1
- package/dist/emulator.js.map +1 -1
- package/dist/images.d.ts +104 -0
- package/dist/images.js +182 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +130 -6
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/local.js +222 -7
- package/dist/local.js.map +1 -1
- package/dist/policy.d.ts +47 -4
- package/dist/policy.js +365 -13
- package/dist/policy.js.map +1 -1
- package/dist/services.d.ts +155 -0
- package/dist/services.js +464 -0
- package/dist/services.js.map +1 -0
- package/dist/stage.d.ts +33 -8
- package/dist/stage.js +598 -125
- package/dist/stage.js.map +1 -1
- package/dist/static.d.ts +1 -1
- package/dist/static.js +2 -36
- package/dist/static.js.map +1 -1
- package/dist/util.d.ts +22 -0
- package/dist/util.js +18 -2
- package/dist/util.js.map +1 -1
- package/package.json +20 -14
- package/src/clients.ts +25 -1
- package/src/compat.ts +34 -11
- package/src/drivers/appsync.ts +220 -0
- package/src/drivers/cognito.ts +19 -9
- package/src/drivers/ecr.ts +457 -0
- package/src/drivers/ecs.ts +661 -0
- package/src/drivers/events.ts +29 -2
- package/src/drivers/index.ts +5 -0
- package/src/drivers/kvs.ts +21 -12
- package/src/drivers/lambda.ts +103 -2
- package/src/drivers/request-function.ts +295 -0
- package/src/drivers/s3.ts +38 -0
- package/src/drivers/scheduler.ts +88 -5
- package/src/drivers/ses.ts +267 -0
- package/src/drivers/sqs.ts +95 -0
- package/src/drivers/web-function.ts +29 -9
- package/src/emulator.ts +3 -3
- package/src/images.ts +254 -0
- package/src/index.ts +129 -7
- package/src/local.ts +251 -7
- package/src/policy.ts +446 -24
- package/src/services.ts +663 -0
- package/src/stage.ts +753 -140
- package/src/static.ts +2 -37
- package/src/util.ts +38 -4
package/src/local.ts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CreateApiCommand } from '@aws-sdk/client-appsync';
|
|
2
|
+
import { CreateFunctionCommand, GetFunctionConfigurationCommand } from '@aws-sdk/client-lambda';
|
|
3
|
+
import {
|
|
4
|
+
emailDomain,
|
|
5
|
+
type ContainersRuntimeConfig,
|
|
6
|
+
NODE_RUNTIME,
|
|
7
|
+
type PlatformRuntimeConfig,
|
|
8
|
+
platformNames,
|
|
9
|
+
type RealtimeRuntimeConfig,
|
|
10
|
+
} from '@ericminassian/platform-core';
|
|
11
|
+
import { Hash } from '@smithy/hash-node';
|
|
12
|
+
import { SignatureV4 } from '@smithy/signature-v4';
|
|
13
|
+
import { strToU8, zipSync } from 'fflate';
|
|
14
|
+
import { findEventApi } from './drivers/appsync.ts';
|
|
2
15
|
import { ensureUserPool } from './drivers/cognito.ts';
|
|
3
16
|
import { ensureTable } from './drivers/dynamodb.ts';
|
|
17
|
+
import { ensureRepository } from './drivers/ecr.ts';
|
|
18
|
+
import { ensureCluster } from './drivers/ecs.ts';
|
|
19
|
+
import { ensureRole } from './drivers/iam.ts';
|
|
4
20
|
import { ensureBucket } from './drivers/s3.ts';
|
|
5
|
-
import {
|
|
21
|
+
import { ensureEmailIdentity } from './drivers/ses.ts';
|
|
22
|
+
import { containerExecutionPolicy, containerSchedulerPolicy, serviceTrustPolicy } from './policy.ts';
|
|
23
|
+
import { type EngineContext, ignoreNotFound, platformTags, sendRaw, waitFor } from './util.ts';
|
|
6
24
|
|
|
7
25
|
/**
|
|
8
26
|
* Inside a fresh AWS Emulator, create the shared resources a stage deploy
|
|
@@ -20,6 +38,7 @@ export async function ensureLocal(
|
|
|
20
38
|
const artifactsBucket = platformNames.artifactsBucket(accountId, region);
|
|
21
39
|
const sitesBucket = platformNames.sitesBucket(accountId, region);
|
|
22
40
|
const storageBucket = platformNames.storageBucket(accountId, region);
|
|
41
|
+
const domain = opts.domain ?? 'localhost';
|
|
23
42
|
|
|
24
43
|
await ensureBucket(ctx, { name: artifactsBucket, versioned: true, tags });
|
|
25
44
|
await ensureBucket(ctx, { name: sitesBucket, tags });
|
|
@@ -28,10 +47,7 @@ export async function ensureLocal(
|
|
|
28
47
|
tags,
|
|
29
48
|
cors: [
|
|
30
49
|
{
|
|
31
|
-
AllowedOrigins: [
|
|
32
|
-
`http://*.${opts.domain ?? 'localhost'}:${opts.port}`,
|
|
33
|
-
`http://localhost:${opts.port}`,
|
|
34
|
-
],
|
|
50
|
+
AllowedOrigins: [`http://*.${domain}:${opts.port}`, `http://localhost:${opts.port}`],
|
|
35
51
|
AllowedMethods: ['GET', 'PUT', 'POST', 'HEAD'],
|
|
36
52
|
AllowedHeaders: ['*'],
|
|
37
53
|
ExposeHeaders: ['ETag'],
|
|
@@ -54,13 +70,19 @@ export async function ensureLocal(
|
|
|
54
70
|
tags,
|
|
55
71
|
});
|
|
56
72
|
const userPoolId = await ensureUserPool(ctx, { name: platformNames.userPool, tags });
|
|
73
|
+
const realtime = await ensureLocalRealtime(ctx, tags);
|
|
74
|
+
// Stages get real tenants and configuration sets in the emulator, so the domain they're
|
|
75
|
+
// associated with must exist. It never verifies and nothing is sent: local stages hand
|
|
76
|
+
// their mail to the CLI (`email.mode: 'local'`).
|
|
77
|
+
const mail = await ensureEmailIdentity(ctx, { domain: emailDomain(domain), tags });
|
|
78
|
+
const containers = await ensureLocalContainers(ctx, tags);
|
|
57
79
|
|
|
58
80
|
return {
|
|
59
81
|
v: 1,
|
|
60
82
|
target: 'emulator',
|
|
61
83
|
accountId,
|
|
62
84
|
region,
|
|
63
|
-
domain
|
|
85
|
+
domain,
|
|
64
86
|
publicScheme: 'http',
|
|
65
87
|
publicPort: opts.port,
|
|
66
88
|
artifactsBucket,
|
|
@@ -71,5 +93,227 @@ export async function ensureLocal(
|
|
|
71
93
|
userPoolId,
|
|
72
94
|
loginUrl: `http://localhost:${opts.port}`,
|
|
73
95
|
jwksBaseUrl: opts.jwksBaseUrl,
|
|
96
|
+
realtime,
|
|
97
|
+
email: { domain: mail.domain, identityArn: mail.arn },
|
|
98
|
+
containers,
|
|
74
99
|
};
|
|
75
100
|
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Container compute inside the emulator, like the foundation's: a Fargate
|
|
104
|
+
* cluster, the image repository, public subnets with a security group, and
|
|
105
|
+
* the execution and scheduler roles. The emulator's ECS runs tasks
|
|
106
|
+
* (simulated: it pulls nothing and runs no code) only when their subnet
|
|
107
|
+
* routes to an internet gateway, which its default VPC lacks; this adds one.
|
|
108
|
+
*/
|
|
109
|
+
async function ensureLocalContainers(
|
|
110
|
+
ctx: EngineContext,
|
|
111
|
+
tags: Record<string, string>,
|
|
112
|
+
): Promise<ContainersRuntimeConfig> {
|
|
113
|
+
const accountId = await ctx.accountId();
|
|
114
|
+
const cluster = platformNames.containerCluster;
|
|
115
|
+
await ensureCluster(ctx, { name: cluster, tags });
|
|
116
|
+
const repository = await ensureRepository(ctx, { name: platformNames.containerRepository, tags });
|
|
117
|
+
const execution = await ensureRole(ctx, {
|
|
118
|
+
name: platformNames.containerExecutionRole,
|
|
119
|
+
trust: serviceTrustPolicy('ecs-tasks.amazonaws.com'),
|
|
120
|
+
description: 'plat dev: pulls platform container images and writes their logs',
|
|
121
|
+
inlinePolicies: {
|
|
122
|
+
execution: containerExecutionPolicy({ region: ctx.region, accountId, repository: repository.name }),
|
|
123
|
+
},
|
|
124
|
+
tags,
|
|
125
|
+
});
|
|
126
|
+
const scheduler = await ensureRole(ctx, {
|
|
127
|
+
name: platformNames.containerSchedulerRole,
|
|
128
|
+
trust: serviceTrustPolicy('scheduler.amazonaws.com'),
|
|
129
|
+
description: "plat dev: scales stages' containers (preview stop schedules)",
|
|
130
|
+
inlinePolicies: { scale: containerSchedulerPolicy({ region: ctx.region, accountId, cluster }) },
|
|
131
|
+
tags,
|
|
132
|
+
});
|
|
133
|
+
const network = await ensureLocalNetwork(ctx);
|
|
134
|
+
return {
|
|
135
|
+
cluster,
|
|
136
|
+
subnets: network.subnets,
|
|
137
|
+
securityGroups: [network.securityGroup],
|
|
138
|
+
repository: repository.name,
|
|
139
|
+
// The emulator's CreateRepository answers with its own host and port, but its ECS only
|
|
140
|
+
// resolves images named the way AWS names them.
|
|
141
|
+
repositoryUri: `${repository.registryId}.dkr.ecr.${ctx.region}.amazonaws.com/${repository.name}`,
|
|
142
|
+
executionRoleArn: execution.arn,
|
|
143
|
+
schedulerRoleArn: scheduler.arn,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The default VPC's subnets, made public (an internet gateway and a default
|
|
149
|
+
* route, which AWS's default VPCs come with), and the containers' security
|
|
150
|
+
* group in it.
|
|
151
|
+
*/
|
|
152
|
+
async function ensureLocalNetwork(ctx: EngineContext): Promise<{ subnets: string[]; securityGroup: string }> {
|
|
153
|
+
const vpcs = await ec2(ctx, 'DescribeVpcs', { 'Filter.1.Name': 'is-default', 'Filter.1.Value.1': 'true' });
|
|
154
|
+
const vpcId = xmlValues(vpcs, 'vpcId')[0];
|
|
155
|
+
if (!vpcId) throw new Error('the AWS Emulator has no default VPC');
|
|
156
|
+
const inVpc = { 'Filter.1.Name': 'vpc-id', 'Filter.1.Value.1': vpcId };
|
|
157
|
+
const subnets = [...new Set(xmlValues(await ec2(ctx, 'DescribeSubnets', inVpc), 'subnetId'))].toSorted();
|
|
158
|
+
if (!subnets.length) throw new Error(`the AWS Emulator's default VPC ${vpcId} has no subnets`);
|
|
159
|
+
|
|
160
|
+
const gateways = await ec2(ctx, 'DescribeInternetGateways', {
|
|
161
|
+
'Filter.1.Name': 'attachment.vpc-id',
|
|
162
|
+
'Filter.1.Value.1': vpcId,
|
|
163
|
+
});
|
|
164
|
+
let gateway = xmlValues(gateways, 'internetGatewayId')[0];
|
|
165
|
+
if (!gateway) {
|
|
166
|
+
gateway = xmlValues(await ec2(ctx, 'CreateInternetGateway', {}), 'internetGatewayId')[0]!;
|
|
167
|
+
await ec2(ctx, 'AttachInternetGateway', { InternetGatewayId: gateway, VpcId: vpcId });
|
|
168
|
+
}
|
|
169
|
+
const tables = await ec2(ctx, 'DescribeRouteTables', {
|
|
170
|
+
...inVpc,
|
|
171
|
+
'Filter.2.Name': 'association.main',
|
|
172
|
+
'Filter.2.Value.1': 'true',
|
|
173
|
+
});
|
|
174
|
+
const table = xmlValues(tables, 'routeTableId')[0];
|
|
175
|
+
if (table && !xmlValues(tables, 'destinationCidrBlock').includes('0.0.0.0/0')) {
|
|
176
|
+
await ec2(ctx, 'CreateRoute', {
|
|
177
|
+
RouteTableId: table,
|
|
178
|
+
DestinationCidrBlock: '0.0.0.0/0',
|
|
179
|
+
GatewayId: gateway,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const groups = await ec2(ctx, 'DescribeSecurityGroups', {
|
|
184
|
+
...inVpc,
|
|
185
|
+
'Filter.2.Name': 'group-name',
|
|
186
|
+
'Filter.2.Value.1': platformNames.containerNetwork,
|
|
187
|
+
});
|
|
188
|
+
const securityGroup =
|
|
189
|
+
xmlValues(groups, 'groupId')[0] ??
|
|
190
|
+
xmlValues(
|
|
191
|
+
await ec2(ctx, 'CreateSecurityGroup', {
|
|
192
|
+
GroupName: platformNames.containerNetwork,
|
|
193
|
+
GroupDescription: 'Platform containers: no inbound traffic, all outbound',
|
|
194
|
+
VpcId: vpcId,
|
|
195
|
+
}),
|
|
196
|
+
'groupId',
|
|
197
|
+
)[0]!;
|
|
198
|
+
return { subnets, securityGroup };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* One EC2 Query API call against the emulator, signed with the SDK's SigV4
|
|
203
|
+
* signer and sent through an SDK client's endpoint, credentials and HTTP
|
|
204
|
+
* handler. These few VPC calls are the engine's only EC2 calls, which
|
|
205
|
+
* doesn't warrant the SDK's EC2 client, one of its largest.
|
|
206
|
+
*/
|
|
207
|
+
async function ec2(ctx: EngineContext, action: string, params: Record<string, string>): Promise<string> {
|
|
208
|
+
const client = ctx.clients.sts;
|
|
209
|
+
const endpoint = await client.config.endpoint!();
|
|
210
|
+
const body = new URLSearchParams({ Action: action, Version: '2016-11-15', ...params }).toString();
|
|
211
|
+
const signer = new SignatureV4({
|
|
212
|
+
service: 'ec2',
|
|
213
|
+
region: ctx.region,
|
|
214
|
+
credentials: client.config.credentials,
|
|
215
|
+
sha256: Hash.bind(null, 'sha256'),
|
|
216
|
+
});
|
|
217
|
+
const request = await signer.sign({
|
|
218
|
+
method: 'POST',
|
|
219
|
+
protocol: endpoint.protocol,
|
|
220
|
+
hostname: endpoint.hostname,
|
|
221
|
+
...(endpoint.port ? { port: endpoint.port } : {}),
|
|
222
|
+
path: '/',
|
|
223
|
+
query: {},
|
|
224
|
+
headers: {
|
|
225
|
+
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
|
|
226
|
+
host: endpoint.port ? `${endpoint.hostname}:${endpoint.port}` : endpoint.hostname,
|
|
227
|
+
'content-length': String(Buffer.byteLength(body)),
|
|
228
|
+
},
|
|
229
|
+
body,
|
|
230
|
+
});
|
|
231
|
+
const res = await sendRaw(client, request as Parameters<typeof sendRaw>[1]);
|
|
232
|
+
const xml = new TextDecoder().decode(res.body);
|
|
233
|
+
if (res.statusCode !== 200) {
|
|
234
|
+
const [code] = xmlValues(xml, 'Code');
|
|
235
|
+
const [message] = xmlValues(xml, 'Message');
|
|
236
|
+
throw new Error(`EC2 ${action} failed in the AWS Emulator: ${code ?? res.statusCode} ${message ?? xml}`);
|
|
237
|
+
}
|
|
238
|
+
return xml;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function xmlValues(xml: string, element: string): string[] {
|
|
242
|
+
return [...xml.matchAll(new RegExp(`<${element}>([^<]*)</${element}>`, 'g'))].map((m) => m[1]!);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The emulator never invokes an Event API's Lambda authorizer (it accepts any
|
|
247
|
+
* token), but the API still needs one to accept the AWS_LAMBDA mode signed-in
|
|
248
|
+
* namespaces use. So this one only satisfies the API's configuration.
|
|
249
|
+
*/
|
|
250
|
+
const STUB_AUTHORIZER = `// plat dev: the AWS Emulator doesn't invoke Event API authorizers, so subscribe tokens
|
|
251
|
+
// aren't checked locally. The platform's real authorizer runs in AWS.
|
|
252
|
+
export const handler = async () => ({ isAuthorized: true });
|
|
253
|
+
`;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The platform's Event API inside the emulator, configured like the
|
|
257
|
+
* foundation's (same auth providers and defaults). The emulator serves its
|
|
258
|
+
* HTTP and realtime endpoints on local ports (`dns` is `127.0.0.1:<port>`).
|
|
259
|
+
*/
|
|
260
|
+
async function ensureLocalRealtime(
|
|
261
|
+
ctx: EngineContext,
|
|
262
|
+
tags: Record<string, string>,
|
|
263
|
+
): Promise<RealtimeRuntimeConfig> {
|
|
264
|
+
let api = await findEventApi(ctx, platformNames.realtimeApi);
|
|
265
|
+
if (!api) {
|
|
266
|
+
const authorizerArn = await ensureStubAuthorizer(ctx, tags);
|
|
267
|
+
const res = await ctx.clients.appsync.send(
|
|
268
|
+
new CreateApiCommand({
|
|
269
|
+
name: platformNames.realtimeApi,
|
|
270
|
+
tags,
|
|
271
|
+
eventConfig: {
|
|
272
|
+
authProviders: [
|
|
273
|
+
{ authType: 'API_KEY' },
|
|
274
|
+
{ authType: 'AWS_IAM' },
|
|
275
|
+
{ authType: 'AWS_LAMBDA', lambdaAuthorizerConfig: { authorizerUri: authorizerArn } },
|
|
276
|
+
],
|
|
277
|
+
connectionAuthModes: [{ authType: 'API_KEY' }, { authType: 'AWS_LAMBDA' }],
|
|
278
|
+
defaultPublishAuthModes: [{ authType: 'AWS_IAM' }],
|
|
279
|
+
defaultSubscribeAuthModes: [{ authType: 'AWS_LAMBDA' }],
|
|
280
|
+
},
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
api = res.api;
|
|
284
|
+
}
|
|
285
|
+
const httpDomain = api?.dns?.HTTP;
|
|
286
|
+
const realtimeDomain = api?.dns?.REALTIME;
|
|
287
|
+
if (!api?.apiId || !httpDomain || !realtimeDomain)
|
|
288
|
+
throw new Error('the AWS Emulator returned an Event API without endpoints');
|
|
289
|
+
return { apiId: api.apiId, httpDomain, realtimeDomain };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function ensureStubAuthorizer(ctx: EngineContext, tags: Record<string, string>): Promise<string> {
|
|
293
|
+
const name = platformNames.realtimeAuthorizer;
|
|
294
|
+
const existing = await ignoreNotFound(
|
|
295
|
+
ctx.clients.lambda.send(new GetFunctionConfigurationCommand({ FunctionName: name })),
|
|
296
|
+
);
|
|
297
|
+
if (existing?.FunctionArn) return existing.FunctionArn;
|
|
298
|
+
const role = await ensureRole(ctx, {
|
|
299
|
+
name,
|
|
300
|
+
trust: serviceTrustPolicy('lambda.amazonaws.com'),
|
|
301
|
+
description: 'plat dev stub realtime authorizer',
|
|
302
|
+
tags,
|
|
303
|
+
});
|
|
304
|
+
const res = await ctx.clients.lambda.send(
|
|
305
|
+
new CreateFunctionCommand({
|
|
306
|
+
FunctionName: name,
|
|
307
|
+
Role: role.arn,
|
|
308
|
+
Runtime: NODE_RUNTIME,
|
|
309
|
+
Handler: 'index.handler',
|
|
310
|
+
Code: { ZipFile: zipSync({ 'index.mjs': strToU8(STUB_AUTHORIZER) }) },
|
|
311
|
+
Tags: tags,
|
|
312
|
+
}),
|
|
313
|
+
);
|
|
314
|
+
await waitFor(`${name} to become active`, async () => {
|
|
315
|
+
const c = await ctx.clients.lambda.send(new GetFunctionConfigurationCommand({ FunctionName: name }));
|
|
316
|
+
return c.State === 'Active';
|
|
317
|
+
});
|
|
318
|
+
return res.FunctionArn!;
|
|
319
|
+
}
|