@ericminassian/platform-engine 0.0.0-stage → 0.2.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 +50 -0
- package/dist/clients.js +103 -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/cognito.d.ts +18 -0
- package/dist/drivers/cognito.js +113 -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 +20 -0
- package/dist/drivers/iam.js +86 -0
- package/dist/drivers/iam.js.map +1 -0
- package/dist/drivers/index.d.ts +12 -0
- package/dist/drivers/index.js +13 -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/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/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 +140 -0
- package/dist/index.js +72 -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 +41 -0
- package/dist/policy.js +316 -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 +48 -3
- package/src/clients.ts +140 -0
- package/src/compat.ts +63 -0
- package/src/drivers/cognito.ts +147 -0
- package/src/drivers/dynamodb.ts +206 -0
- package/src/drivers/events.ts +92 -0
- package/src/drivers/iam.ts +125 -0
- package/src/drivers/index.ts +12 -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/s3.ts +196 -0
- package/src/drivers/scheduler.ts +90 -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 +183 -0
- package/src/local.ts +75 -0
- package/src/policy.ts +377 -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,206 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AttributeDefinition,
|
|
3
|
+
CreateTableCommand,
|
|
4
|
+
DeleteTableCommand,
|
|
5
|
+
DescribeContinuousBackupsCommand,
|
|
6
|
+
DescribeTableCommand,
|
|
7
|
+
DescribeTimeToLiveCommand,
|
|
8
|
+
type GlobalSecondaryIndex,
|
|
9
|
+
TagResourceCommand,
|
|
10
|
+
UpdateContinuousBackupsCommand,
|
|
11
|
+
UpdateTableCommand,
|
|
12
|
+
UpdateTimeToLiveCommand,
|
|
13
|
+
} from '@aws-sdk/client-dynamodb';
|
|
14
|
+
import { type EngineContext, ignoreNotFound, tagList, waitFor } from '../util.ts';
|
|
15
|
+
|
|
16
|
+
export interface TableSpec {
|
|
17
|
+
name: string;
|
|
18
|
+
/** Partition/sort key attribute names (all strings). */
|
|
19
|
+
pk: string;
|
|
20
|
+
sk?: string;
|
|
21
|
+
/** GSIs as `[indexName, pkAttr, skAttr?]` (string attributes, projection ALL). */
|
|
22
|
+
gsis?: Array<[string, string, string?]>;
|
|
23
|
+
ttlAttribute?: string;
|
|
24
|
+
pointInTimeRecovery?: boolean;
|
|
25
|
+
deletionProtection?: boolean;
|
|
26
|
+
tags?: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TableState {
|
|
30
|
+
name: string;
|
|
31
|
+
arn: string;
|
|
32
|
+
created: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The per-stage table: `pk`/`sk`, three generic GSIs and TTL on `_ttl`. */
|
|
36
|
+
export function stageTableSpec(
|
|
37
|
+
name: string,
|
|
38
|
+
opts: { prod: boolean; tags: Record<string, string> },
|
|
39
|
+
): TableSpec {
|
|
40
|
+
return {
|
|
41
|
+
name,
|
|
42
|
+
pk: 'pk',
|
|
43
|
+
sk: 'sk',
|
|
44
|
+
gsis: [
|
|
45
|
+
['gsi1', 'gsi1pk', 'gsi1sk'],
|
|
46
|
+
['gsi2', 'gsi2pk', 'gsi2sk'],
|
|
47
|
+
['gsi3', 'gsi3pk', 'gsi3sk'],
|
|
48
|
+
],
|
|
49
|
+
ttlAttribute: '_ttl',
|
|
50
|
+
pointInTimeRecovery: opts.prod,
|
|
51
|
+
deletionProtection: opts.prod,
|
|
52
|
+
tags: opts.tags,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function ensureTable(ctx: EngineContext, spec: TableSpec): Promise<TableState> {
|
|
57
|
+
const ddb = ctx.clients.dynamodb;
|
|
58
|
+
const existing = await ignoreNotFound(ddb.send(new DescribeTableCommand({ TableName: spec.name })));
|
|
59
|
+
let created = false;
|
|
60
|
+
let arn = existing?.Table?.TableArn;
|
|
61
|
+
|
|
62
|
+
if (!existing?.Table) {
|
|
63
|
+
const attrs = new Map<string, AttributeDefinition>();
|
|
64
|
+
const addAttr = (n: string) => attrs.set(n, { AttributeName: n, AttributeType: 'S' });
|
|
65
|
+
addAttr(spec.pk);
|
|
66
|
+
if (spec.sk) addAttr(spec.sk);
|
|
67
|
+
const gsis: GlobalSecondaryIndex[] = (spec.gsis ?? []).map(([IndexName, pk, sk]) => {
|
|
68
|
+
addAttr(pk);
|
|
69
|
+
if (sk) addAttr(sk);
|
|
70
|
+
return {
|
|
71
|
+
IndexName,
|
|
72
|
+
KeySchema: [
|
|
73
|
+
{ AttributeName: pk, KeyType: 'HASH' },
|
|
74
|
+
...(sk ? [{ AttributeName: sk, KeyType: 'RANGE' as const }] : []),
|
|
75
|
+
],
|
|
76
|
+
Projection: { ProjectionType: 'ALL' },
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const res = await ddb.send(
|
|
80
|
+
new CreateTableCommand({
|
|
81
|
+
TableName: spec.name,
|
|
82
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
83
|
+
AttributeDefinitions: [...attrs.values()],
|
|
84
|
+
KeySchema: [
|
|
85
|
+
{ AttributeName: spec.pk, KeyType: 'HASH' },
|
|
86
|
+
...(spec.sk ? [{ AttributeName: spec.sk, KeyType: 'RANGE' as const }] : []),
|
|
87
|
+
],
|
|
88
|
+
GlobalSecondaryIndexes: gsis.length ? gsis : undefined,
|
|
89
|
+
DeletionProtectionEnabled: spec.deletionProtection ?? false,
|
|
90
|
+
Tags: spec.tags ? tagList(spec.tags) : undefined,
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
arn = res.TableDescription?.TableArn;
|
|
94
|
+
created = true;
|
|
95
|
+
ctx.log('info', 'created table', `table ${spec.name}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await waitActive(ctx, spec.name);
|
|
99
|
+
|
|
100
|
+
if (!created && existing?.Table) {
|
|
101
|
+
// Add any missing GSIs (DynamoDB allows one index creation per UpdateTable call).
|
|
102
|
+
const have = new Set((existing.Table.GlobalSecondaryIndexes ?? []).map((g) => g.IndexName));
|
|
103
|
+
for (const [IndexName, pk, sk] of spec.gsis ?? []) {
|
|
104
|
+
if (have.has(IndexName)) continue;
|
|
105
|
+
await ddb.send(
|
|
106
|
+
new UpdateTableCommand({
|
|
107
|
+
TableName: spec.name,
|
|
108
|
+
AttributeDefinitions: [
|
|
109
|
+
{ AttributeName: pk, AttributeType: 'S' },
|
|
110
|
+
...(sk ? [{ AttributeName: sk, AttributeType: 'S' as const }] : []),
|
|
111
|
+
],
|
|
112
|
+
GlobalSecondaryIndexUpdates: [
|
|
113
|
+
{
|
|
114
|
+
Create: {
|
|
115
|
+
IndexName,
|
|
116
|
+
KeySchema: [
|
|
117
|
+
{ AttributeName: pk, KeyType: 'HASH' },
|
|
118
|
+
...(sk ? [{ AttributeName: sk, KeyType: 'RANGE' as const }] : []),
|
|
119
|
+
],
|
|
120
|
+
Projection: { ProjectionType: 'ALL' },
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
ctx.log('info', `adding index ${IndexName}`, `table ${spec.name}`);
|
|
127
|
+
await waitActive(ctx, spec.name);
|
|
128
|
+
}
|
|
129
|
+
if (
|
|
130
|
+
spec.deletionProtection !== undefined &&
|
|
131
|
+
existing.Table.DeletionProtectionEnabled !== spec.deletionProtection
|
|
132
|
+
) {
|
|
133
|
+
await ddb.send(
|
|
134
|
+
new UpdateTableCommand({ TableName: spec.name, DeletionProtectionEnabled: spec.deletionProtection }),
|
|
135
|
+
);
|
|
136
|
+
await waitActive(ctx, spec.name);
|
|
137
|
+
}
|
|
138
|
+
if (spec.tags && arn)
|
|
139
|
+
await ddb.send(new TagResourceCommand({ ResourceArn: arn, Tags: tagList(spec.tags) }));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (spec.ttlAttribute) {
|
|
143
|
+
const ttl = await ddb.send(new DescribeTimeToLiveCommand({ TableName: spec.name }));
|
|
144
|
+
const status = ttl.TimeToLiveDescription?.TimeToLiveStatus;
|
|
145
|
+
if (status !== 'ENABLED' && status !== 'ENABLING') {
|
|
146
|
+
await ddb.send(
|
|
147
|
+
new UpdateTimeToLiveCommand({
|
|
148
|
+
TableName: spec.name,
|
|
149
|
+
TimeToLiveSpecification: { Enabled: true, AttributeName: spec.ttlAttribute },
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (spec.pointInTimeRecovery !== undefined) {
|
|
156
|
+
const backups = await ddb.send(new DescribeContinuousBackupsCommand({ TableName: spec.name }));
|
|
157
|
+
const enabled =
|
|
158
|
+
backups.ContinuousBackupsDescription?.PointInTimeRecoveryDescription?.PointInTimeRecoveryStatus ===
|
|
159
|
+
'ENABLED';
|
|
160
|
+
if (enabled !== spec.pointInTimeRecovery) {
|
|
161
|
+
await ddb.send(
|
|
162
|
+
new UpdateContinuousBackupsCommand({
|
|
163
|
+
TableName: spec.name,
|
|
164
|
+
PointInTimeRecoverySpecification: { PointInTimeRecoveryEnabled: spec.pointInTimeRecovery },
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!arn) {
|
|
171
|
+
const d = await ddb.send(new DescribeTableCommand({ TableName: spec.name }));
|
|
172
|
+
arn = d.Table!.TableArn!;
|
|
173
|
+
}
|
|
174
|
+
return { name: spec.name, arn, created };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function waitActive(ctx: EngineContext, name: string): Promise<void> {
|
|
178
|
+
await waitFor(`table ${name} ACTIVE`, async () => {
|
|
179
|
+
const d = await ctx.clients.dynamodb.send(new DescribeTableCommand({ TableName: name }));
|
|
180
|
+
const t = d.Table;
|
|
181
|
+
return (
|
|
182
|
+
t?.TableStatus === 'ACTIVE' && (t.GlobalSecondaryIndexes ?? []).every((g) => g.IndexStatus === 'ACTIVE')
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function describeTable(ctx: EngineContext, name: string) {
|
|
188
|
+
return (await ignoreNotFound(ctx.clients.dynamodb.send(new DescribeTableCommand({ TableName: name }))))
|
|
189
|
+
?.Table;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function deleteTable(ctx: EngineContext, name: string): Promise<boolean> {
|
|
193
|
+
const ddb = ctx.clients.dynamodb;
|
|
194
|
+
const t = await describeTable(ctx, name);
|
|
195
|
+
if (!t) return false;
|
|
196
|
+
if (t.DeletionProtectionEnabled) {
|
|
197
|
+
await ddb.send(new UpdateTableCommand({ TableName: name, DeletionProtectionEnabled: false }));
|
|
198
|
+
await waitActive(ctx, name);
|
|
199
|
+
}
|
|
200
|
+
await ignoreNotFound(ddb.send(new DeleteTableCommand({ TableName: name })));
|
|
201
|
+
await waitFor(`table ${name} deleted`, async () => !(await describeTable(ctx, name)), {
|
|
202
|
+
timeoutMs: 120_000,
|
|
203
|
+
});
|
|
204
|
+
ctx.log('info', 'deleted table', `table ${name}`);
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateEventBusCommand,
|
|
3
|
+
DeleteEventBusCommand,
|
|
4
|
+
DeleteRuleCommand,
|
|
5
|
+
DescribeEventBusCommand,
|
|
6
|
+
ListRulesCommand,
|
|
7
|
+
ListTargetsByRuleCommand,
|
|
8
|
+
PutRuleCommand,
|
|
9
|
+
PutTargetsCommand,
|
|
10
|
+
RemoveTargetsCommand,
|
|
11
|
+
} from '@aws-sdk/client-eventbridge';
|
|
12
|
+
import { type EngineContext, ignoreNotFound, tagList } from '../util.ts';
|
|
13
|
+
|
|
14
|
+
export async function ensureEventBus(
|
|
15
|
+
ctx: EngineContext,
|
|
16
|
+
spec: { name: string; tags?: Record<string, string> },
|
|
17
|
+
): Promise<string> {
|
|
18
|
+
const ev = ctx.clients.events;
|
|
19
|
+
const existing = await ignoreNotFound(ev.send(new DescribeEventBusCommand({ Name: spec.name })));
|
|
20
|
+
if (existing?.Arn) return existing.Arn;
|
|
21
|
+
const res = await ev.send(
|
|
22
|
+
new CreateEventBusCommand({ Name: spec.name, Tags: spec.tags ? tagList(spec.tags) : undefined }),
|
|
23
|
+
);
|
|
24
|
+
ctx.log('info', 'created event bus', `bus ${spec.name}`);
|
|
25
|
+
return res.EventBusArn!;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RuleSpec {
|
|
29
|
+
bus: string;
|
|
30
|
+
name: string;
|
|
31
|
+
pattern: unknown;
|
|
32
|
+
targets: Array<{ id: string; arn: string; input?: string; roleArn?: string }>;
|
|
33
|
+
tags?: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function ensureRule(ctx: EngineContext, spec: RuleSpec): Promise<string> {
|
|
37
|
+
const ev = ctx.clients.events;
|
|
38
|
+
const rule = await ev.send(
|
|
39
|
+
new PutRuleCommand({
|
|
40
|
+
Name: spec.name,
|
|
41
|
+
EventBusName: spec.bus,
|
|
42
|
+
EventPattern: JSON.stringify(spec.pattern),
|
|
43
|
+
State: 'ENABLED',
|
|
44
|
+
Tags: spec.tags ? tagList(spec.tags) : undefined,
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
await ev.send(
|
|
48
|
+
new PutTargetsCommand({
|
|
49
|
+
Rule: spec.name,
|
|
50
|
+
EventBusName: spec.bus,
|
|
51
|
+
Targets: spec.targets.map((t) => ({ Id: t.id, Arn: t.arn, Input: t.input, RoleArn: t.roleArn })),
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
const existing = await ev.send(new ListTargetsByRuleCommand({ Rule: spec.name, EventBusName: spec.bus }));
|
|
55
|
+
const wanted = new Set(spec.targets.map((t) => t.id));
|
|
56
|
+
const stale = (existing.Targets ?? []).map((t) => t.Id!).filter((id) => !wanted.has(id));
|
|
57
|
+
if (stale.length)
|
|
58
|
+
await ev.send(new RemoveTargetsCommand({ Rule: spec.name, EventBusName: spec.bus, Ids: stale }));
|
|
59
|
+
return rule.RuleArn!;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function listRules(ctx: EngineContext, bus: string, prefix?: string): Promise<string[]> {
|
|
63
|
+
const out: string[] = [];
|
|
64
|
+
let NextToken: string | undefined;
|
|
65
|
+
do {
|
|
66
|
+
const res = await ignoreNotFound(
|
|
67
|
+
ctx.clients.events.send(new ListRulesCommand({ EventBusName: bus, NamePrefix: prefix, NextToken })),
|
|
68
|
+
);
|
|
69
|
+
if (!res) return out;
|
|
70
|
+
for (const r of res.Rules ?? []) if (r.Name) out.push(r.Name);
|
|
71
|
+
NextToken = res.NextToken;
|
|
72
|
+
} while (NextToken);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function deleteRule(ctx: EngineContext, bus: string, name: string): Promise<void> {
|
|
77
|
+
const ev = ctx.clients.events;
|
|
78
|
+
const targets = await ignoreNotFound(
|
|
79
|
+
ev.send(new ListTargetsByRuleCommand({ Rule: name, EventBusName: bus })),
|
|
80
|
+
);
|
|
81
|
+
const ids = (targets?.Targets ?? []).map((t) => t.Id!);
|
|
82
|
+
if (ids.length)
|
|
83
|
+
await ev.send(new RemoveTargetsCommand({ Rule: name, EventBusName: bus, Ids: ids, Force: true }));
|
|
84
|
+
await ignoreNotFound(ev.send(new DeleteRuleCommand({ Name: name, EventBusName: bus, Force: true })));
|
|
85
|
+
ctx.log('info', 'deleted rule', `rule ${name}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function deleteEventBus(ctx: EngineContext, name: string): Promise<void> {
|
|
89
|
+
for (const rule of await listRules(ctx, name)) await deleteRule(ctx, name, rule);
|
|
90
|
+
await ignoreNotFound(ctx.clients.events.send(new DeleteEventBusCommand({ Name: name })));
|
|
91
|
+
ctx.log('info', 'deleted event bus', `bus ${name}`);
|
|
92
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AttachRolePolicyCommand,
|
|
3
|
+
CreateRoleCommand,
|
|
4
|
+
DeleteRoleCommand,
|
|
5
|
+
DeleteRolePermissionsBoundaryCommand,
|
|
6
|
+
DeleteRolePolicyCommand,
|
|
7
|
+
DetachRolePolicyCommand,
|
|
8
|
+
GetRoleCommand,
|
|
9
|
+
ListAttachedRolePoliciesCommand,
|
|
10
|
+
ListRolePoliciesCommand,
|
|
11
|
+
PutRolePermissionsBoundaryCommand,
|
|
12
|
+
PutRolePolicyCommand,
|
|
13
|
+
TagRoleCommand,
|
|
14
|
+
UpdateAssumeRolePolicyCommand,
|
|
15
|
+
} from '@aws-sdk/client-iam';
|
|
16
|
+
import { type EngineContext, ignoreNotFound, type PolicyDocument, samePolicy, tagList } from '../util.ts';
|
|
17
|
+
|
|
18
|
+
export interface RoleSpec {
|
|
19
|
+
name: string;
|
|
20
|
+
path?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
trust: PolicyDocument;
|
|
23
|
+
/** Inline policies by name; inline policies not listed are removed. */
|
|
24
|
+
inlinePolicies?: Record<string, PolicyDocument>;
|
|
25
|
+
managedPolicyArns?: string[];
|
|
26
|
+
boundaryArn?: string;
|
|
27
|
+
tags?: Record<string, string>;
|
|
28
|
+
maxSessionDurationSeconds?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RoleState {
|
|
32
|
+
arn: string;
|
|
33
|
+
name: string;
|
|
34
|
+
created: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function ensureRole(ctx: EngineContext, spec: RoleSpec): Promise<RoleState> {
|
|
38
|
+
const iam = ctx.clients.iam;
|
|
39
|
+
const existing = await ignoreNotFound(iam.send(new GetRoleCommand({ RoleName: spec.name })));
|
|
40
|
+
let arn: string;
|
|
41
|
+
let created = false;
|
|
42
|
+
if (!existing?.Role) {
|
|
43
|
+
const res = await iam.send(
|
|
44
|
+
new CreateRoleCommand({
|
|
45
|
+
RoleName: spec.name,
|
|
46
|
+
Path: spec.path ?? '/',
|
|
47
|
+
Description: spec.description,
|
|
48
|
+
AssumeRolePolicyDocument: JSON.stringify(spec.trust),
|
|
49
|
+
PermissionsBoundary: spec.boundaryArn,
|
|
50
|
+
MaxSessionDuration: spec.maxSessionDurationSeconds,
|
|
51
|
+
Tags: spec.tags ? tagList(spec.tags) : undefined,
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
arn = res.Role!.Arn!;
|
|
55
|
+
created = true;
|
|
56
|
+
ctx.log('info', 'created role', `role ${spec.name}`);
|
|
57
|
+
} else {
|
|
58
|
+
const role = existing.Role;
|
|
59
|
+
arn = role.Arn!;
|
|
60
|
+
if (!samePolicy(role.AssumeRolePolicyDocument, spec.trust)) {
|
|
61
|
+
await iam.send(
|
|
62
|
+
new UpdateAssumeRolePolicyCommand({
|
|
63
|
+
RoleName: spec.name,
|
|
64
|
+
PolicyDocument: JSON.stringify(spec.trust),
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const currentBoundary = role.PermissionsBoundary?.PermissionsBoundaryArn;
|
|
69
|
+
if (spec.boundaryArn && currentBoundary !== spec.boundaryArn) {
|
|
70
|
+
await iam.send(
|
|
71
|
+
new PutRolePermissionsBoundaryCommand({ RoleName: spec.name, PermissionsBoundary: spec.boundaryArn }),
|
|
72
|
+
);
|
|
73
|
+
} else if (!spec.boundaryArn && currentBoundary) {
|
|
74
|
+
await iam.send(new DeleteRolePermissionsBoundaryCommand({ RoleName: spec.name }));
|
|
75
|
+
}
|
|
76
|
+
if (spec.tags) await iam.send(new TagRoleCommand({ RoleName: spec.name, Tags: tagList(spec.tags) }));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const wanted = spec.inlinePolicies ?? {};
|
|
80
|
+
for (const [policyName, doc] of Object.entries(wanted)) {
|
|
81
|
+
await iam.send(
|
|
82
|
+
new PutRolePolicyCommand({
|
|
83
|
+
RoleName: spec.name,
|
|
84
|
+
PolicyName: policyName,
|
|
85
|
+
PolicyDocument: JSON.stringify(doc),
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const listed = await iam.send(new ListRolePoliciesCommand({ RoleName: spec.name }));
|
|
90
|
+
for (const name of listed.PolicyNames ?? []) {
|
|
91
|
+
if (!(name in wanted))
|
|
92
|
+
await iam.send(new DeleteRolePolicyCommand({ RoleName: spec.name, PolicyName: name }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (spec.managedPolicyArns) {
|
|
96
|
+
const attached = await iam.send(new ListAttachedRolePoliciesCommand({ RoleName: spec.name }));
|
|
97
|
+
const have = new Set((attached.AttachedPolicies ?? []).map((p) => p.PolicyArn!));
|
|
98
|
+
for (const p of spec.managedPolicyArns) {
|
|
99
|
+
if (!have.has(p)) await iam.send(new AttachRolePolicyCommand({ RoleName: spec.name, PolicyArn: p }));
|
|
100
|
+
}
|
|
101
|
+
for (const p of have) {
|
|
102
|
+
if (!spec.managedPolicyArns.includes(p)) {
|
|
103
|
+
await iam.send(new DetachRolePolicyCommand({ RoleName: spec.name, PolicyArn: p }));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { arn, name: spec.name, created };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function deleteRole(ctx: EngineContext, name: string): Promise<boolean> {
|
|
111
|
+
const iam = ctx.clients.iam;
|
|
112
|
+
const existing = await ignoreNotFound(iam.send(new GetRoleCommand({ RoleName: name })));
|
|
113
|
+
if (!existing?.Role) return false;
|
|
114
|
+
const inline = await iam.send(new ListRolePoliciesCommand({ RoleName: name }));
|
|
115
|
+
for (const p of inline.PolicyNames ?? []) {
|
|
116
|
+
await ignoreNotFound(iam.send(new DeleteRolePolicyCommand({ RoleName: name, PolicyName: p })));
|
|
117
|
+
}
|
|
118
|
+
const attached = await iam.send(new ListAttachedRolePoliciesCommand({ RoleName: name }));
|
|
119
|
+
for (const p of attached.AttachedPolicies ?? []) {
|
|
120
|
+
await ignoreNotFound(iam.send(new DetachRolePolicyCommand({ RoleName: name, PolicyArn: p.PolicyArn })));
|
|
121
|
+
}
|
|
122
|
+
await ignoreNotFound(iam.send(new DeleteRoleCommand({ RoleName: name })));
|
|
123
|
+
ctx.log('info', 'deleted role', `role ${name}`);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './cognito.ts';
|
|
2
|
+
export * from './dynamodb.ts';
|
|
3
|
+
export * from './events.ts';
|
|
4
|
+
export * from './iam.ts';
|
|
5
|
+
export * from './kvs.ts';
|
|
6
|
+
export * from './lambda.ts';
|
|
7
|
+
export * from './logs.ts';
|
|
8
|
+
export * from './s3.ts';
|
|
9
|
+
export * from './scheduler.ts';
|
|
10
|
+
export * from './sqs.ts';
|
|
11
|
+
export * from './ssm.ts';
|
|
12
|
+
export * from './web-function.ts';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteKeyCommand,
|
|
3
|
+
DescribeKeyValueStoreCommand,
|
|
4
|
+
GetKeyCommand,
|
|
5
|
+
PutKeyCommand,
|
|
6
|
+
} from '@aws-sdk/client-cloudfront-keyvaluestore';
|
|
7
|
+
import { encodeRouteRecord, type RouteRecord } from '@ericminassian/platform-core';
|
|
8
|
+
import { type EngineContext, errorName, ignoreNotFound, retry } from '../util.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Host → route records in the CloudFront KeyValueStore read by the router
|
|
12
|
+
* function. Writes are optimistic (ETag); concurrent deploys retry.
|
|
13
|
+
*/
|
|
14
|
+
const isConflict = (e: unknown) => /Conflict|PreconditionFailed|ValidationException/.test(errorName(e));
|
|
15
|
+
|
|
16
|
+
export async function putRoute(
|
|
17
|
+
ctx: EngineContext,
|
|
18
|
+
kvsArn: string,
|
|
19
|
+
host: string,
|
|
20
|
+
record: RouteRecord,
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
const value = encodeRouteRecord(record);
|
|
23
|
+
await retry(
|
|
24
|
+
async () => {
|
|
25
|
+
const d = await ctx.clients.kvs.send(new DescribeKeyValueStoreCommand({ KvsARN: kvsArn }));
|
|
26
|
+
await ctx.clients.kvs.send(
|
|
27
|
+
new PutKeyCommand({ KvsARN: kvsArn, Key: host, Value: value, IfMatch: d.ETag }),
|
|
28
|
+
);
|
|
29
|
+
},
|
|
30
|
+
{ when: isConflict, attempts: 8, delayMs: 300 },
|
|
31
|
+
);
|
|
32
|
+
ctx.log('info', `route ${host} → ${record.d}`, `route ${host}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function getRoute(
|
|
36
|
+
ctx: EngineContext,
|
|
37
|
+
kvsArn: string,
|
|
38
|
+
host: string,
|
|
39
|
+
): Promise<RouteRecord | undefined> {
|
|
40
|
+
const res = await ignoreNotFound(ctx.clients.kvs.send(new GetKeyCommand({ KvsARN: kvsArn, Key: host })));
|
|
41
|
+
return res?.Value ? (JSON.parse(res.Value) as RouteRecord) : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function deleteRoute(ctx: EngineContext, kvsArn: string, host: string): Promise<void> {
|
|
45
|
+
await retry(
|
|
46
|
+
async () => {
|
|
47
|
+
const d = await ctx.clients.kvs.send(new DescribeKeyValueStoreCommand({ KvsARN: kvsArn }));
|
|
48
|
+
await ignoreNotFound(
|
|
49
|
+
ctx.clients.kvs.send(new DeleteKeyCommand({ KvsARN: kvsArn, Key: host, IfMatch: d.ETag })),
|
|
50
|
+
);
|
|
51
|
+
},
|
|
52
|
+
{ when: isConflict, attempts: 8, delayMs: 300 },
|
|
53
|
+
);
|
|
54
|
+
ctx.log('info', 'deleted route', `route ${host}`);
|
|
55
|
+
}
|