@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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateLogGroupCommand,
|
|
3
|
+
DeleteLogGroupCommand,
|
|
4
|
+
DescribeLogGroupsCommand,
|
|
5
|
+
PutAccountPolicyCommand,
|
|
6
|
+
PutRetentionPolicyCommand,
|
|
7
|
+
} from '@aws-sdk/client-cloudwatch-logs';
|
|
8
|
+
import { type EngineContext, errorName, ignoreNotFound } from '../util.ts';
|
|
9
|
+
|
|
10
|
+
/** Retention values CloudWatch Logs accepts. */
|
|
11
|
+
const RETENTION_DAYS = [
|
|
12
|
+
1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557, 2922, 3288, 3653,
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export function snapRetention(days: number): number {
|
|
16
|
+
return RETENTION_DAYS.find((d) => d >= days) ?? 3653;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function ensureLogGroup(
|
|
20
|
+
ctx: EngineContext,
|
|
21
|
+
spec: { name: string; retentionDays: number; tags?: Record<string, string> },
|
|
22
|
+
): Promise<string> {
|
|
23
|
+
const logs = ctx.clients.logs;
|
|
24
|
+
try {
|
|
25
|
+
await logs.send(new CreateLogGroupCommand({ logGroupName: spec.name, tags: spec.tags }));
|
|
26
|
+
ctx.log('info', 'created log group', `log-group ${spec.name}`);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if (errorName(e) !== 'ResourceAlreadyExistsException') throw e;
|
|
29
|
+
}
|
|
30
|
+
await logs.send(
|
|
31
|
+
new PutRetentionPolicyCommand({
|
|
32
|
+
logGroupName: spec.name,
|
|
33
|
+
retentionInDays: snapRetention(spec.retentionDays),
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
const acct = await ctx.accountId();
|
|
37
|
+
return `arn:aws:logs:${ctx.region}:${acct}:log-group:${spec.name}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function deleteLogGroup(ctx: EngineContext, name: string): Promise<void> {
|
|
41
|
+
await ignoreNotFound(ctx.clients.logs.send(new DeleteLogGroupCommand({ logGroupName: name })));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function listLogGroups(ctx: EngineContext, prefix: string): Promise<string[]> {
|
|
45
|
+
const out: string[] = [];
|
|
46
|
+
let nextToken: string | undefined;
|
|
47
|
+
do {
|
|
48
|
+
const res = await ctx.clients.logs.send(
|
|
49
|
+
new DescribeLogGroupsCommand({ logGroupNamePrefix: prefix, nextToken }),
|
|
50
|
+
);
|
|
51
|
+
for (const g of res.logGroups ?? []) if (g.logGroupName) out.push(g.logGroupName);
|
|
52
|
+
nextToken = res.nextToken;
|
|
53
|
+
} while (nextToken);
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The one account-level subscription filter: every log group in the region
|
|
59
|
+
* (except `excludeLogGroups`, to avoid the processor feeding itself) streams
|
|
60
|
+
* lines matching `filterPattern` to `destinationArn`.
|
|
61
|
+
*/
|
|
62
|
+
export async function ensureAccountSubscriptionFilter(
|
|
63
|
+
ctx: EngineContext,
|
|
64
|
+
spec: { name: string; destinationArn: string; filterPattern: string; excludeLogGroups: string[] },
|
|
65
|
+
): Promise<void> {
|
|
66
|
+
await ctx.clients.logs.send(
|
|
67
|
+
new PutAccountPolicyCommand({
|
|
68
|
+
policyName: spec.name,
|
|
69
|
+
policyType: 'SUBSCRIPTION_FILTER_POLICY',
|
|
70
|
+
scope: 'ALL',
|
|
71
|
+
policyDocument: JSON.stringify({
|
|
72
|
+
DestinationArn: spec.destinationArn,
|
|
73
|
+
FilterPattern: spec.filterPattern,
|
|
74
|
+
}),
|
|
75
|
+
selectionCriteria: spec.excludeLogGroups.length
|
|
76
|
+
? `LogGroupName NOT IN [${spec.excludeLogGroups.map((g) => JSON.stringify(g)).join(', ')}]`
|
|
77
|
+
: undefined,
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
ctx.log('info', 'put account subscription filter', `account-policy ${spec.name}`);
|
|
81
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Change,
|
|
3
|
+
ChangeResourceRecordSetsCommand,
|
|
4
|
+
CreateHostedZoneCommand,
|
|
5
|
+
GetHostedZoneCommand,
|
|
6
|
+
ListHostedZonesByNameCommand,
|
|
7
|
+
} from '@aws-sdk/client-route-53';
|
|
8
|
+
import { AssumeRoleCommand } from '@aws-sdk/client-sts';
|
|
9
|
+
import { type EngineContext, platformTags } from '../util.ts';
|
|
10
|
+
import { CLOUDFRONT_HOSTED_ZONE_ID } from './cloudfront.ts';
|
|
11
|
+
|
|
12
|
+
const fqdn = (name: string) => (name.endsWith('.') ? name : `${name}.`);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Public zone id by name using only `ListHostedZonesByName`: that is all the
|
|
16
|
+
* cross-account delegation role in github.com/eric-minassian/aws may call.
|
|
17
|
+
*/
|
|
18
|
+
export async function findHostedZoneId(
|
|
19
|
+
ctx: Pick<EngineContext, 'clients'>,
|
|
20
|
+
name: string,
|
|
21
|
+
): Promise<string | undefined> {
|
|
22
|
+
const res = await ctx.clients.route53.send(
|
|
23
|
+
new ListHostedZonesByNameCommand({ DNSName: fqdn(name), MaxItems: 1 }),
|
|
24
|
+
);
|
|
25
|
+
const zone = (res.HostedZones ?? []).find((z) => z.Name === fqdn(name) && !z.Config?.PrivateZone);
|
|
26
|
+
return zone?.Id?.replace('/hostedzone/', '');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function findHostedZone(
|
|
30
|
+
ctx: Pick<EngineContext, 'clients'>,
|
|
31
|
+
name: string,
|
|
32
|
+
): Promise<{ id: string; nameServers: string[] } | undefined> {
|
|
33
|
+
const id = await findHostedZoneId(ctx, name);
|
|
34
|
+
if (!id) return undefined;
|
|
35
|
+
const details = await ctx.clients.route53.send(new GetHostedZoneCommand({ Id: id }));
|
|
36
|
+
return { id, nameServers: details.DelegationSet?.NameServers ?? [] };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function ensureHostedZone(
|
|
40
|
+
ctx: EngineContext,
|
|
41
|
+
name: string,
|
|
42
|
+
): Promise<{ id: string; nameServers: string[] }> {
|
|
43
|
+
const existing = await findHostedZone(ctx, name);
|
|
44
|
+
if (existing) return existing;
|
|
45
|
+
const res = await ctx.clients.route53.send(
|
|
46
|
+
new CreateHostedZoneCommand({
|
|
47
|
+
Name: name,
|
|
48
|
+
CallerReference: `${name}-${Date.now()}`,
|
|
49
|
+
HostedZoneConfig: { Comment: `platform (${JSON.stringify(platformTags('dns'))})` },
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
ctx.log('info', 'created hosted zone', `zone ${name}`);
|
|
53
|
+
return {
|
|
54
|
+
id: res.HostedZone!.Id!.replace('/hostedzone/', ''),
|
|
55
|
+
nameServers: res.DelegationSet?.NameServers ?? [],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function upsertRecords(
|
|
60
|
+
ctx: Pick<EngineContext, 'clients'>,
|
|
61
|
+
zoneId: string,
|
|
62
|
+
changes: Change[],
|
|
63
|
+
): Promise<void> {
|
|
64
|
+
if (!changes.length) return;
|
|
65
|
+
await ctx.clients.route53.send(
|
|
66
|
+
new ChangeResourceRecordSetsCommand({ HostedZoneId: zoneId, ChangeBatch: { Changes: changes } }),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A + AAAA alias records pointing `name` at a CloudFront distribution domain. */
|
|
71
|
+
export function cloudFrontAlias(name: string, distributionDomain: string): Change[] {
|
|
72
|
+
return (['A', 'AAAA'] as const).map((Type) => ({
|
|
73
|
+
Action: 'UPSERT' as const,
|
|
74
|
+
ResourceRecordSet: {
|
|
75
|
+
Name: fqdn(name),
|
|
76
|
+
Type,
|
|
77
|
+
AliasTarget: {
|
|
78
|
+
DNSName: distributionDomain,
|
|
79
|
+
HostedZoneId: CLOUDFRONT_HOSTED_ZONE_ID,
|
|
80
|
+
EvaluateTargetHealth: false,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function cnameRecord(name: string, value: string, ttl = 300): Change {
|
|
87
|
+
return {
|
|
88
|
+
Action: 'UPSERT',
|
|
89
|
+
ResourceRecordSet: { Name: fqdn(name), Type: 'CNAME', TTL: ttl, ResourceRecords: [{ Value: value }] },
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Register `zoneName`'s NS records in its parent zone, which lives in another
|
|
95
|
+
* account (github.com/eric-minassian/aws). That account grants a role that may
|
|
96
|
+
* UPSERT exactly this NS record and nothing else.
|
|
97
|
+
*/
|
|
98
|
+
export async function delegateZone(
|
|
99
|
+
ctx: EngineContext,
|
|
100
|
+
spec: { roleArn: string; parentZoneName: string; zoneName: string; nameServers: string[] },
|
|
101
|
+
): Promise<void> {
|
|
102
|
+
const assumed = await ctx.clients.sts.send(
|
|
103
|
+
new AssumeRoleCommand({
|
|
104
|
+
RoleArn: spec.roleArn,
|
|
105
|
+
RoleSessionName: 'platform-dns-delegation',
|
|
106
|
+
DurationSeconds: 900,
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
const c = assumed.Credentials!;
|
|
110
|
+
const parent = ctx.clients.withCredentials({
|
|
111
|
+
accessKeyId: c.AccessKeyId!,
|
|
112
|
+
secretAccessKey: c.SecretAccessKey!,
|
|
113
|
+
sessionToken: c.SessionToken!,
|
|
114
|
+
});
|
|
115
|
+
const parentId = await findHostedZoneId({ clients: parent }, spec.parentZoneName);
|
|
116
|
+
if (!parentId) throw new Error(`parent zone ${spec.parentZoneName} not visible to ${spec.roleArn}`);
|
|
117
|
+
await upsertRecords({ clients: parent }, parentId, [
|
|
118
|
+
{
|
|
119
|
+
Action: 'UPSERT',
|
|
120
|
+
ResourceRecordSet: {
|
|
121
|
+
Name: fqdn(spec.zoneName),
|
|
122
|
+
Type: 'NS',
|
|
123
|
+
TTL: 172800,
|
|
124
|
+
ResourceRecords: spec.nameServers.map((Value) => ({ Value })),
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
]);
|
|
128
|
+
ctx.log('info', `delegated ${spec.zoneName} from ${spec.parentZoneName}`, `zone ${spec.zoneName}`);
|
|
129
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CORSRule,
|
|
3
|
+
CreateBucketCommand,
|
|
4
|
+
DeleteObjectsCommand,
|
|
5
|
+
GetObjectCommand,
|
|
6
|
+
HeadBucketCommand,
|
|
7
|
+
type LifecycleRule,
|
|
8
|
+
ListObjectsV2Command,
|
|
9
|
+
ListObjectVersionsCommand,
|
|
10
|
+
PutBucketCorsCommand,
|
|
11
|
+
PutBucketLifecycleConfigurationCommand,
|
|
12
|
+
PutBucketPolicyCommand,
|
|
13
|
+
PutBucketTaggingCommand,
|
|
14
|
+
PutBucketVersioningCommand,
|
|
15
|
+
PutObjectCommand,
|
|
16
|
+
PutPublicAccessBlockCommand,
|
|
17
|
+
} from '@aws-sdk/client-s3';
|
|
18
|
+
import { unzipSync } from 'fflate';
|
|
19
|
+
import type { S3Location } from '../index.ts';
|
|
20
|
+
import { cacheControlFor, contentTypeFor } from '../static.ts';
|
|
21
|
+
import {
|
|
22
|
+
type EngineContext,
|
|
23
|
+
errorName,
|
|
24
|
+
isNotFound,
|
|
25
|
+
mapLimit,
|
|
26
|
+
type PolicyDocument,
|
|
27
|
+
tagList,
|
|
28
|
+
} from '../util.ts';
|
|
29
|
+
|
|
30
|
+
export interface BucketSpec {
|
|
31
|
+
name: string;
|
|
32
|
+
versioned?: boolean;
|
|
33
|
+
cors?: CORSRule[];
|
|
34
|
+
lifecycle?: LifecycleRule[];
|
|
35
|
+
policy?: PolicyDocument;
|
|
36
|
+
tags?: Record<string, string>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function ensureBucket(ctx: EngineContext, spec: BucketSpec): Promise<string> {
|
|
40
|
+
const s3 = ctx.clients.s3;
|
|
41
|
+
let exists = true;
|
|
42
|
+
try {
|
|
43
|
+
await s3.send(new HeadBucketCommand({ Bucket: spec.name }));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (!isNotFound(e) && errorName(e) !== 'NotFound') throw e;
|
|
46
|
+
exists = false;
|
|
47
|
+
}
|
|
48
|
+
if (!exists) {
|
|
49
|
+
await s3.send(
|
|
50
|
+
new CreateBucketCommand({
|
|
51
|
+
Bucket: spec.name,
|
|
52
|
+
...(ctx.region === 'us-east-1'
|
|
53
|
+
? {}
|
|
54
|
+
: { CreateBucketConfiguration: { LocationConstraint: ctx.region as never } }),
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
ctx.log('info', 'created bucket', `bucket ${spec.name}`);
|
|
58
|
+
}
|
|
59
|
+
await s3.send(
|
|
60
|
+
new PutPublicAccessBlockCommand({
|
|
61
|
+
Bucket: spec.name,
|
|
62
|
+
PublicAccessBlockConfiguration: {
|
|
63
|
+
BlockPublicAcls: true,
|
|
64
|
+
IgnorePublicAcls: true,
|
|
65
|
+
BlockPublicPolicy: true,
|
|
66
|
+
RestrictPublicBuckets: true,
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
if (spec.versioned) {
|
|
71
|
+
await s3.send(
|
|
72
|
+
new PutBucketVersioningCommand({ Bucket: spec.name, VersioningConfiguration: { Status: 'Enabled' } }),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (spec.cors)
|
|
76
|
+
await s3.send(
|
|
77
|
+
new PutBucketCorsCommand({ Bucket: spec.name, CORSConfiguration: { CORSRules: spec.cors } }),
|
|
78
|
+
);
|
|
79
|
+
if (spec.lifecycle) {
|
|
80
|
+
await s3.send(
|
|
81
|
+
new PutBucketLifecycleConfigurationCommand({
|
|
82
|
+
Bucket: spec.name,
|
|
83
|
+
LifecycleConfiguration: { Rules: spec.lifecycle },
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (spec.policy)
|
|
88
|
+
await s3.send(new PutBucketPolicyCommand({ Bucket: spec.name, Policy: JSON.stringify(spec.policy) }));
|
|
89
|
+
if (spec.tags)
|
|
90
|
+
await s3.send(
|
|
91
|
+
new PutBucketTaggingCommand({ Bucket: spec.name, Tagging: { TagSet: tagList(spec.tags) } }),
|
|
92
|
+
);
|
|
93
|
+
return `arn:aws:s3:::${spec.name}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function putObject(
|
|
97
|
+
ctx: EngineContext,
|
|
98
|
+
spec: {
|
|
99
|
+
bucket: string;
|
|
100
|
+
key: string;
|
|
101
|
+
body: Uint8Array | string;
|
|
102
|
+
contentType?: string;
|
|
103
|
+
cacheControl?: string;
|
|
104
|
+
},
|
|
105
|
+
): Promise<S3Location> {
|
|
106
|
+
const res = await ctx.clients.s3.send(
|
|
107
|
+
new PutObjectCommand({
|
|
108
|
+
Bucket: spec.bucket,
|
|
109
|
+
Key: spec.key,
|
|
110
|
+
Body: spec.body,
|
|
111
|
+
ContentType: spec.contentType,
|
|
112
|
+
CacheControl: spec.cacheControl,
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
return { bucket: spec.bucket, key: spec.key, ...(res.VersionId ? { versionId: res.VersionId } : {}) };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function getObjectBytes(ctx: EngineContext, loc: S3Location): Promise<Uint8Array> {
|
|
119
|
+
const res = await ctx.clients.s3.send(
|
|
120
|
+
new GetObjectCommand({ Bucket: loc.bucket, Key: loc.key, VersionId: loc.versionId }),
|
|
121
|
+
);
|
|
122
|
+
return res.Body!.transformToByteArray();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Unzip a static bundle into `bucket/prefix/` with content types and cache headers. Returns file count. */
|
|
126
|
+
export async function publishStaticZip(
|
|
127
|
+
ctx: EngineContext,
|
|
128
|
+
spec: { source: S3Location; bucket: string; prefix: string },
|
|
129
|
+
): Promise<number> {
|
|
130
|
+
const zip = await getObjectBytes(ctx, spec.source);
|
|
131
|
+
const files = Object.entries(unzipSync(zip)).filter(([path]) => !path.endsWith('/'));
|
|
132
|
+
await mapLimit(files, 16, async ([path, body]) => {
|
|
133
|
+
const rel = path.replace(/^\/+/, '');
|
|
134
|
+
await putObject(ctx, {
|
|
135
|
+
bucket: spec.bucket,
|
|
136
|
+
key: `${spec.prefix}/${rel}`,
|
|
137
|
+
body,
|
|
138
|
+
contentType: contentTypeFor(rel),
|
|
139
|
+
cacheControl: cacheControlFor(rel),
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
ctx.log('info', `published ${files.length} static files`, `static ${spec.prefix}`);
|
|
143
|
+
return files.length;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Immediate child "directories" under `prefix` (ends with `/`). */
|
|
147
|
+
export async function listChildPrefixes(
|
|
148
|
+
ctx: EngineContext,
|
|
149
|
+
bucket: string,
|
|
150
|
+
prefix: string,
|
|
151
|
+
): Promise<string[]> {
|
|
152
|
+
const out: string[] = [];
|
|
153
|
+
let ContinuationToken: string | undefined;
|
|
154
|
+
do {
|
|
155
|
+
const res = await ctx.clients.s3.send(
|
|
156
|
+
new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix, Delimiter: '/', ContinuationToken }),
|
|
157
|
+
);
|
|
158
|
+
for (const p of res.CommonPrefixes ?? []) if (p.Prefix) out.push(p.Prefix);
|
|
159
|
+
ContinuationToken = res.NextContinuationToken;
|
|
160
|
+
} while (ContinuationToken);
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Delete every object (and, in versioned buckets, every version) under `prefix`. */
|
|
165
|
+
export async function deletePrefix(ctx: EngineContext, bucket: string, prefix: string): Promise<number> {
|
|
166
|
+
const s3 = ctx.clients.s3;
|
|
167
|
+
let deleted = 0;
|
|
168
|
+
try {
|
|
169
|
+
let KeyMarker: string | undefined;
|
|
170
|
+
let VersionIdMarker: string | undefined;
|
|
171
|
+
do {
|
|
172
|
+
const res = await s3.send(
|
|
173
|
+
new ListObjectVersionsCommand({ Bucket: bucket, Prefix: prefix, KeyMarker, VersionIdMarker }),
|
|
174
|
+
);
|
|
175
|
+
const objects = [...(res.Versions ?? []), ...(res.DeleteMarkers ?? [])].map((v) => ({
|
|
176
|
+
Key: v.Key!,
|
|
177
|
+
VersionId: v.VersionId ?? undefined,
|
|
178
|
+
}));
|
|
179
|
+
for (let i = 0; i < objects.length; i += 1000) {
|
|
180
|
+
const batch = objects.slice(i, i + 1000);
|
|
181
|
+
if (batch.length) {
|
|
182
|
+
await s3.send(
|
|
183
|
+
new DeleteObjectsCommand({ Bucket: bucket, Delete: { Objects: batch, Quiet: true } }),
|
|
184
|
+
);
|
|
185
|
+
deleted += batch.length;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
KeyMarker = res.IsTruncated ? res.NextKeyMarker : undefined;
|
|
189
|
+
VersionIdMarker = res.IsTruncated ? res.NextVersionIdMarker : undefined;
|
|
190
|
+
} while (KeyMarker);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
if (isNotFound(e)) return deleted;
|
|
193
|
+
throw e;
|
|
194
|
+
}
|
|
195
|
+
return deleted;
|
|
196
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateScheduleCommand,
|
|
3
|
+
CreateScheduleGroupCommand,
|
|
4
|
+
DeleteScheduleCommand,
|
|
5
|
+
DeleteScheduleGroupCommand,
|
|
6
|
+
GetScheduleCommand,
|
|
7
|
+
GetScheduleGroupCommand,
|
|
8
|
+
ListSchedulesCommand,
|
|
9
|
+
UpdateScheduleCommand,
|
|
10
|
+
} from '@aws-sdk/client-scheduler';
|
|
11
|
+
import { type EngineContext, ignoreNotFound, retry, tagList } from '../util.ts';
|
|
12
|
+
|
|
13
|
+
export async function ensureScheduleGroup(
|
|
14
|
+
ctx: EngineContext,
|
|
15
|
+
spec: { name: string; tags?: Record<string, string> },
|
|
16
|
+
): Promise<string> {
|
|
17
|
+
const sch = ctx.clients.scheduler;
|
|
18
|
+
const existing = await ignoreNotFound(sch.send(new GetScheduleGroupCommand({ Name: spec.name })));
|
|
19
|
+
if (existing?.Arn) return existing.Arn;
|
|
20
|
+
const res = await sch.send(
|
|
21
|
+
new CreateScheduleGroupCommand({ Name: spec.name, Tags: spec.tags ? tagList(spec.tags) : undefined }),
|
|
22
|
+
);
|
|
23
|
+
ctx.log('info', 'created schedule group', `schedule-group ${spec.name}`);
|
|
24
|
+
return res.ScheduleGroupArn!;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ScheduleSpec {
|
|
28
|
+
group: string;
|
|
29
|
+
name: string;
|
|
30
|
+
/** EventBridge Scheduler expression (`cron(...)` / `rate(...)`). */
|
|
31
|
+
expression: string;
|
|
32
|
+
timezone?: string;
|
|
33
|
+
target: { arn: string; roleArn: string; input: string };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function ensureSchedule(ctx: EngineContext, spec: ScheduleSpec): Promise<void> {
|
|
37
|
+
const sch = ctx.clients.scheduler;
|
|
38
|
+
const definition = {
|
|
39
|
+
Name: spec.name,
|
|
40
|
+
GroupName: spec.group,
|
|
41
|
+
ScheduleExpression: spec.expression,
|
|
42
|
+
ScheduleExpressionTimezone: spec.timezone,
|
|
43
|
+
FlexibleTimeWindow: { Mode: 'OFF' as const },
|
|
44
|
+
State: 'ENABLED' as const,
|
|
45
|
+
Target: {
|
|
46
|
+
Arn: spec.target.arn,
|
|
47
|
+
RoleArn: spec.target.roleArn,
|
|
48
|
+
Input: spec.target.input,
|
|
49
|
+
RetryPolicy: { MaximumRetryAttempts: 2, MaximumEventAgeInSeconds: 3600 },
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
const existing = await ignoreNotFound(
|
|
53
|
+
sch.send(new GetScheduleCommand({ Name: spec.name, GroupName: spec.group })),
|
|
54
|
+
);
|
|
55
|
+
// A freshly created role may not be assumable by Scheduler yet.
|
|
56
|
+
await retry(
|
|
57
|
+
() =>
|
|
58
|
+
existing
|
|
59
|
+
? sch.send(new UpdateScheduleCommand(definition))
|
|
60
|
+
: sch.send(new CreateScheduleCommand(definition)),
|
|
61
|
+
{ when: (e) => /assume the role|execution role/i.test(String((e as Error).message)), attempts: 12 },
|
|
62
|
+
);
|
|
63
|
+
if (!existing) ctx.log('info', 'created schedule', `schedule ${spec.group}/${spec.name}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function listSchedules(ctx: EngineContext, group: string): Promise<string[]> {
|
|
67
|
+
const out: string[] = [];
|
|
68
|
+
let NextToken: string | undefined;
|
|
69
|
+
do {
|
|
70
|
+
const res = await ignoreNotFound(
|
|
71
|
+
ctx.clients.scheduler.send(new ListSchedulesCommand({ GroupName: group, NextToken })),
|
|
72
|
+
);
|
|
73
|
+
if (!res) return out;
|
|
74
|
+
for (const s of res.Schedules ?? []) if (s.Name) out.push(s.Name);
|
|
75
|
+
NextToken = res.NextToken;
|
|
76
|
+
} while (NextToken);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function deleteSchedule(ctx: EngineContext, group: string, name: string): Promise<void> {
|
|
81
|
+
await ignoreNotFound(
|
|
82
|
+
ctx.clients.scheduler.send(new DeleteScheduleCommand({ Name: name, GroupName: group })),
|
|
83
|
+
);
|
|
84
|
+
ctx.log('info', 'deleted schedule', `schedule ${group}/${name}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function deleteScheduleGroup(ctx: EngineContext, group: string): Promise<void> {
|
|
88
|
+
for (const s of await listSchedules(ctx, group)) await deleteSchedule(ctx, group, s);
|
|
89
|
+
await ignoreNotFound(ctx.clients.scheduler.send(new DeleteScheduleGroupCommand({ Name: group })));
|
|
90
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CreateTopicCommand, ListSubscriptionsByTopicCommand, SubscribeCommand } from '@aws-sdk/client-sns';
|
|
2
|
+
import { type EngineContext, tagList } from '../util.ts';
|
|
3
|
+
|
|
4
|
+
export async function ensureTopic(
|
|
5
|
+
ctx: EngineContext,
|
|
6
|
+
spec: { name: string; tags?: Record<string, string> },
|
|
7
|
+
): Promise<string> {
|
|
8
|
+
// CreateTopic is idempotent for an existing name.
|
|
9
|
+
const res = await ctx.clients.sns.send(
|
|
10
|
+
new CreateTopicCommand({ Name: spec.name, Tags: spec.tags ? tagList(spec.tags) : undefined }),
|
|
11
|
+
);
|
|
12
|
+
return res.TopicArn!;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Email subscription; AWS sends a confirmation email the first time. */
|
|
16
|
+
export async function ensureEmailSubscription(
|
|
17
|
+
ctx: EngineContext,
|
|
18
|
+
topicArn: string,
|
|
19
|
+
email: string,
|
|
20
|
+
): Promise<void> {
|
|
21
|
+
let NextToken: string | undefined;
|
|
22
|
+
do {
|
|
23
|
+
const res = await ctx.clients.sns.send(
|
|
24
|
+
new ListSubscriptionsByTopicCommand({ TopicArn: topicArn, NextToken }),
|
|
25
|
+
);
|
|
26
|
+
if ((res.Subscriptions ?? []).some((s) => s.Protocol === 'email' && s.Endpoint === email)) return;
|
|
27
|
+
NextToken = res.NextToken;
|
|
28
|
+
} while (NextToken);
|
|
29
|
+
await ctx.clients.sns.send(
|
|
30
|
+
new SubscribeCommand({ TopicArn: topicArn, Protocol: 'email', Endpoint: email }),
|
|
31
|
+
);
|
|
32
|
+
ctx.log('info', 'subscribed alert email (confirm the email AWS sends)', `topic ${topicArn}`);
|
|
33
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateQueueCommand,
|
|
3
|
+
DeleteQueueCommand,
|
|
4
|
+
GetQueueAttributesCommand,
|
|
5
|
+
GetQueueUrlCommand,
|
|
6
|
+
ListQueuesCommand,
|
|
7
|
+
SetQueueAttributesCommand,
|
|
8
|
+
TagQueueCommand,
|
|
9
|
+
} from '@aws-sdk/client-sqs';
|
|
10
|
+
import { type EngineContext, ignoreNotFound } from '../util.ts';
|
|
11
|
+
|
|
12
|
+
export interface QueueState {
|
|
13
|
+
name: string;
|
|
14
|
+
url: string;
|
|
15
|
+
arn: string;
|
|
16
|
+
created: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function getQueueUrl(ctx: EngineContext, name: string): Promise<string | undefined> {
|
|
20
|
+
const res = await ignoreNotFound(ctx.clients.sqs.send(new GetQueueUrlCommand({ QueueName: name })));
|
|
21
|
+
return res?.QueueUrl;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function ensureQueue(
|
|
25
|
+
ctx: EngineContext,
|
|
26
|
+
spec: { name: string; attributes?: Record<string, string>; tags?: Record<string, string> },
|
|
27
|
+
): Promise<QueueState> {
|
|
28
|
+
const sqs = ctx.clients.sqs;
|
|
29
|
+
let url = await getQueueUrl(ctx, spec.name);
|
|
30
|
+
let created = false;
|
|
31
|
+
if (!url) {
|
|
32
|
+
const res = await sqs.send(
|
|
33
|
+
new CreateQueueCommand({ QueueName: spec.name, Attributes: spec.attributes, tags: spec.tags }),
|
|
34
|
+
);
|
|
35
|
+
url = res.QueueUrl!;
|
|
36
|
+
created = true;
|
|
37
|
+
ctx.log('info', 'created queue', `queue ${spec.name}`);
|
|
38
|
+
} else {
|
|
39
|
+
if (spec.attributes && Object.keys(spec.attributes).length) {
|
|
40
|
+
await sqs.send(new SetQueueAttributesCommand({ QueueUrl: url, Attributes: spec.attributes }));
|
|
41
|
+
}
|
|
42
|
+
if (spec.tags) await sqs.send(new TagQueueCommand({ QueueUrl: url, Tags: spec.tags }));
|
|
43
|
+
}
|
|
44
|
+
const attrs = await sqs.send(
|
|
45
|
+
new GetQueueAttributesCommand({ QueueUrl: url, AttributeNames: ['QueueArn'] }),
|
|
46
|
+
);
|
|
47
|
+
return { name: spec.name, url, arn: attrs.Attributes!.QueueArn!, created };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function deleteQueue(ctx: EngineContext, name: string): Promise<boolean> {
|
|
51
|
+
const url = await getQueueUrl(ctx, name);
|
|
52
|
+
if (!url) return false;
|
|
53
|
+
await ignoreNotFound(ctx.clients.sqs.send(new DeleteQueueCommand({ QueueUrl: url })));
|
|
54
|
+
ctx.log('info', 'deleted queue', `queue ${name}`);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function listQueues(
|
|
59
|
+
ctx: EngineContext,
|
|
60
|
+
prefix: string,
|
|
61
|
+
): Promise<Array<{ name: string; url: string }>> {
|
|
62
|
+
const out: Array<{ name: string; url: string }> = [];
|
|
63
|
+
let NextToken: string | undefined;
|
|
64
|
+
do {
|
|
65
|
+
const res = await ctx.clients.sqs.send(new ListQueuesCommand({ QueueNamePrefix: prefix, NextToken }));
|
|
66
|
+
for (const url of res.QueueUrls ?? []) out.push({ url, name: url.slice(url.lastIndexOf('/') + 1) });
|
|
67
|
+
NextToken = res.NextToken;
|
|
68
|
+
} while (NextToken);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteParametersCommand,
|
|
3
|
+
GetParameterCommand,
|
|
4
|
+
GetParametersByPathCommand,
|
|
5
|
+
PutParameterCommand,
|
|
6
|
+
} from '@aws-sdk/client-ssm';
|
|
7
|
+
import { type EngineContext, ignoreNotFound } from '../util.ts';
|
|
8
|
+
|
|
9
|
+
export async function getParameter(
|
|
10
|
+
ctx: EngineContext,
|
|
11
|
+
name: string,
|
|
12
|
+
decrypt = true,
|
|
13
|
+
): Promise<string | undefined> {
|
|
14
|
+
const res = await ignoreNotFound(
|
|
15
|
+
ctx.clients.ssm.send(new GetParameterCommand({ Name: name, WithDecryption: decrypt })),
|
|
16
|
+
);
|
|
17
|
+
return res?.Parameter?.Value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function putParameter(
|
|
21
|
+
ctx: EngineContext,
|
|
22
|
+
spec: { name: string; value: string; secure?: boolean; description?: string },
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
await ctx.clients.ssm.send(
|
|
25
|
+
new PutParameterCommand({
|
|
26
|
+
Name: spec.name,
|
|
27
|
+
Value: spec.value,
|
|
28
|
+
Type: spec.secure ? 'SecureString' : 'String',
|
|
29
|
+
Description: spec.description,
|
|
30
|
+
Overwrite: true,
|
|
31
|
+
Tier: Buffer.byteLength(spec.value) > 4096 ? 'Advanced' : 'Standard',
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Create the parameter only if it doesn't exist (e.g. a generated key). Returns the current value. */
|
|
37
|
+
export async function ensureParameterOnce(
|
|
38
|
+
ctx: EngineContext,
|
|
39
|
+
spec: { name: string; secure?: boolean; description?: string; generate: () => string },
|
|
40
|
+
): Promise<string> {
|
|
41
|
+
const current = await getParameter(ctx, spec.name);
|
|
42
|
+
if (current !== undefined) return current;
|
|
43
|
+
const value = spec.generate();
|
|
44
|
+
await putParameter(ctx, { name: spec.name, value, secure: spec.secure, description: spec.description });
|
|
45
|
+
ctx.log('info', 'created parameter', `parameter ${spec.name}`);
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function deleteParametersByPath(ctx: EngineContext, path: string): Promise<number> {
|
|
50
|
+
const ssm = ctx.clients.ssm;
|
|
51
|
+
const names: string[] = [];
|
|
52
|
+
let NextToken: string | undefined;
|
|
53
|
+
do {
|
|
54
|
+
const res = await ignoreNotFound(
|
|
55
|
+
ssm.send(new GetParametersByPathCommand({ Path: path, Recursive: true, NextToken })),
|
|
56
|
+
);
|
|
57
|
+
if (!res) break;
|
|
58
|
+
for (const p of res.Parameters ?? []) if (p.Name) names.push(p.Name);
|
|
59
|
+
NextToken = res.NextToken;
|
|
60
|
+
} while (NextToken);
|
|
61
|
+
for (let i = 0; i < names.length; i += 10) {
|
|
62
|
+
await ssm.send(new DeleteParametersCommand({ Names: names.slice(i, i + 10) }));
|
|
63
|
+
}
|
|
64
|
+
return names.length;
|
|
65
|
+
}
|