@ericminassian/platform-engine 0.1.0 → 0.3.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 +0 -11
- package/dist/clients.js +0 -19
- package/dist/clients.js.map +1 -1
- package/dist/drivers/cognito.d.ts +0 -9
- package/dist/drivers/cognito.js +2 -40
- package/dist/drivers/cognito.js.map +1 -1
- package/dist/drivers/iam.d.ts +0 -9
- package/dist/drivers/iam.js +1 -52
- package/dist/drivers/iam.js.map +1 -1
- package/dist/drivers/index.d.ts +0 -4
- package/dist/drivers/index.js +0 -4
- package/dist/drivers/index.js.map +1 -1
- package/dist/drivers/s3.d.ts +2 -0
- package/dist/drivers/s3.js +5 -0
- package/dist/drivers/s3.js.map +1 -1
- package/dist/drivers/ssm.d.ts +1 -0
- package/dist/drivers/ssm.js +23 -3
- package/dist/drivers/ssm.js.map +1 -1
- package/dist/drivers/web-function.d.ts +30 -8
- package/dist/drivers/web-function.js +42 -5
- package/dist/drivers/web-function.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/local.d.ts +2 -2
- package/dist/local.js +2 -2
- package/dist/policy.d.ts +0 -6
- package/dist/policy.js +4 -18
- package/dist/policy.js.map +1 -1
- package/dist/stage.d.ts +17 -2
- package/dist/stage.js +110 -41
- package/dist/stage.js.map +1 -1
- package/package.json +2 -6
- package/src/clients.ts +0 -26
- package/src/drivers/cognito.ts +1 -53
- package/src/drivers/iam.ts +0 -70
- package/src/drivers/index.ts +0 -4
- package/src/drivers/s3.ts +8 -0
- package/src/drivers/ssm.ts +23 -2
- package/src/drivers/web-function.ts +81 -10
- package/src/index.ts +6 -4
- package/src/local.ts +2 -2
- package/src/policy.ts +4 -23
- package/src/stage.ts +116 -44
- package/dist/drivers/acm.d.ts +0 -20
- package/dist/drivers/acm.js +0 -62
- package/dist/drivers/acm.js.map +0 -1
- package/dist/drivers/cloudfront.d.ts +0 -52
- package/dist/drivers/cloudfront.js +0 -223
- package/dist/drivers/cloudfront.js.map +0 -1
- package/dist/drivers/route53.d.ts +0 -30
- package/dist/drivers/route53.js +0 -96
- package/dist/drivers/route53.js.map +0 -1
- package/dist/drivers/sns.d.ts +0 -7
- package/dist/drivers/sns.js +0 -20
- package/dist/drivers/sns.js.map +0 -1
- package/src/drivers/acm.ts +0 -88
- package/src/drivers/cloudfront.ts +0 -291
- package/src/drivers/route53.ts +0 -129
- package/src/drivers/sns.ts +0 -33
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type CachePolicyConfig,
|
|
3
|
-
CreateCachePolicyCommand,
|
|
4
|
-
CreateDistributionWithTagsCommand,
|
|
5
|
-
CreateFunctionCommand,
|
|
6
|
-
CreateKeyValueStoreCommand,
|
|
7
|
-
CreateOriginAccessControlCommand,
|
|
8
|
-
DescribeFunctionCommand,
|
|
9
|
-
DescribeKeyValueStoreCommand,
|
|
10
|
-
type DistributionConfig,
|
|
11
|
-
GetCachePolicyConfigCommand,
|
|
12
|
-
GetDistributionConfigCommand,
|
|
13
|
-
GetFunctionCommand,
|
|
14
|
-
ListCachePoliciesCommand,
|
|
15
|
-
ListDistributionsCommand,
|
|
16
|
-
ListKeyValueStoresCommand,
|
|
17
|
-
ListOriginAccessControlsCommand,
|
|
18
|
-
PublishFunctionCommand,
|
|
19
|
-
UpdateCachePolicyCommand,
|
|
20
|
-
UpdateDistributionCommand,
|
|
21
|
-
UpdateFunctionCommand,
|
|
22
|
-
} from '@aws-sdk/client-cloudfront';
|
|
23
|
-
import { type EngineContext, ignoreNotFound, tagList, waitFor } from '../util.ts';
|
|
24
|
-
|
|
25
|
-
/** AWS managed origin request policy `Managed-AllViewerExceptHostHeader` (Web Function origins need it). */
|
|
26
|
-
export const ALL_VIEWER_EXCEPT_HOST_HEADER = 'b689b0a8-53d0-40ab-baf2-68738e2966ac';
|
|
27
|
-
/** Route 53 hosted zone id for every CloudFront alias target. */
|
|
28
|
-
export const CLOUDFRONT_HOSTED_ZONE_ID = 'Z2FDTNDATAQYW2';
|
|
29
|
-
|
|
30
|
-
export async function ensureKeyValueStore(
|
|
31
|
-
ctx: EngineContext,
|
|
32
|
-
spec: { name: string; comment?: string },
|
|
33
|
-
): Promise<{ arn: string; id: string }> {
|
|
34
|
-
const cf = ctx.clients.cloudfront;
|
|
35
|
-
let Marker: string | undefined;
|
|
36
|
-
do {
|
|
37
|
-
const res = await cf.send(new ListKeyValueStoresCommand({ Marker }));
|
|
38
|
-
const found = (res.KeyValueStoreList?.Items ?? []).find((k) => k.Name === spec.name);
|
|
39
|
-
if (found?.ARN && found.Id) return { arn: found.ARN, id: found.Id };
|
|
40
|
-
Marker = res.KeyValueStoreList?.NextMarker;
|
|
41
|
-
} while (Marker);
|
|
42
|
-
const res = await cf.send(new CreateKeyValueStoreCommand({ Name: spec.name, Comment: spec.comment }));
|
|
43
|
-
const kvs = res.KeyValueStore!;
|
|
44
|
-
ctx.log('info', 'created key value store', `kvs ${spec.name}`);
|
|
45
|
-
await waitFor(`key value store ${spec.name}`, async () => {
|
|
46
|
-
const d = await cf.send(new DescribeKeyValueStoreCommand({ Name: spec.name }));
|
|
47
|
-
return d.KeyValueStore?.Status === 'READY' || d.KeyValueStore?.Status === undefined;
|
|
48
|
-
});
|
|
49
|
-
return { arn: kvs.ARN!, id: kvs.Id! };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Create/update a CloudFront Function (runtime 2.0) and publish it to LIVE. Returns the function ARN. */
|
|
53
|
-
export async function ensureRouterFunction(
|
|
54
|
-
ctx: EngineContext,
|
|
55
|
-
spec: { name: string; code: string; kvsArn?: string; comment?: string },
|
|
56
|
-
): Promise<string> {
|
|
57
|
-
const cf = ctx.clients.cloudfront;
|
|
58
|
-
const code = new TextEncoder().encode(spec.code);
|
|
59
|
-
const FunctionConfig = {
|
|
60
|
-
Comment: spec.comment ?? 'platform router',
|
|
61
|
-
Runtime: 'cloudfront-js-2.0' as const,
|
|
62
|
-
...(spec.kvsArn
|
|
63
|
-
? { KeyValueStoreAssociations: { Quantity: 1, Items: [{ KeyValueStoreARN: spec.kvsArn }] } }
|
|
64
|
-
: {}),
|
|
65
|
-
};
|
|
66
|
-
const existing = await ignoreNotFound(
|
|
67
|
-
cf.send(new DescribeFunctionCommand({ Name: spec.name, Stage: 'DEVELOPMENT' })),
|
|
68
|
-
);
|
|
69
|
-
let etag: string | undefined;
|
|
70
|
-
if (!existing?.ETag) {
|
|
71
|
-
const res = await cf.send(
|
|
72
|
-
new CreateFunctionCommand({ Name: spec.name, FunctionConfig, FunctionCode: code }),
|
|
73
|
-
);
|
|
74
|
-
etag = res.ETag;
|
|
75
|
-
ctx.log('info', 'created router function', `cloudfront-function ${spec.name}`);
|
|
76
|
-
} else {
|
|
77
|
-
const live = await ignoreNotFound(cf.send(new GetFunctionCommand({ Name: spec.name, Stage: 'LIVE' })));
|
|
78
|
-
const liveCode = live?.FunctionCode ? new TextDecoder().decode(live.FunctionCode) : undefined;
|
|
79
|
-
if (liveCode === spec.code) return existing.FunctionSummary!.FunctionMetadata!.FunctionARN!;
|
|
80
|
-
const res = await cf.send(
|
|
81
|
-
new UpdateFunctionCommand({
|
|
82
|
-
Name: spec.name,
|
|
83
|
-
IfMatch: existing.ETag,
|
|
84
|
-
FunctionConfig,
|
|
85
|
-
FunctionCode: code,
|
|
86
|
-
}),
|
|
87
|
-
);
|
|
88
|
-
etag = res.ETag;
|
|
89
|
-
ctx.log('info', 'updated router function', `cloudfront-function ${spec.name}`);
|
|
90
|
-
}
|
|
91
|
-
const published = await cf.send(new PublishFunctionCommand({ Name: spec.name, IfMatch: etag }));
|
|
92
|
-
return published.FunctionSummary!.FunctionMetadata!.FunctionARN!;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export async function ensureOriginAccessControl(
|
|
96
|
-
ctx: EngineContext,
|
|
97
|
-
spec: { name: string; originType: 's3' | 'lambda'; signingBehavior?: 'always' | 'always-amz-auth' },
|
|
98
|
-
): Promise<string> {
|
|
99
|
-
const cf = ctx.clients.cloudfront;
|
|
100
|
-
let Marker: string | undefined;
|
|
101
|
-
do {
|
|
102
|
-
const res = await cf.send(new ListOriginAccessControlsCommand({ Marker }));
|
|
103
|
-
const found = (res.OriginAccessControlList?.Items ?? []).find((o) => o.Name === spec.name);
|
|
104
|
-
if (found?.Id) return found.Id;
|
|
105
|
-
Marker = res.OriginAccessControlList?.NextMarker;
|
|
106
|
-
} while (Marker);
|
|
107
|
-
const res = await cf.send(
|
|
108
|
-
new CreateOriginAccessControlCommand({
|
|
109
|
-
OriginAccessControlConfig: {
|
|
110
|
-
Name: spec.name,
|
|
111
|
-
SigningProtocol: 'sigv4',
|
|
112
|
-
SigningBehavior: (spec.signingBehavior ?? 'always') as never,
|
|
113
|
-
OriginAccessControlOriginType: spec.originType,
|
|
114
|
-
},
|
|
115
|
-
}),
|
|
116
|
-
);
|
|
117
|
-
ctx.log('info', 'created origin access control', `oac ${spec.name}`);
|
|
118
|
-
return res.OriginAccessControl!.Id!;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export async function ensureCachePolicy(ctx: EngineContext, config: CachePolicyConfig): Promise<string> {
|
|
122
|
-
const cf = ctx.clients.cloudfront;
|
|
123
|
-
const list = await cf.send(new ListCachePoliciesCommand({ Type: 'custom' }));
|
|
124
|
-
const found = (list.CachePolicyList?.Items ?? []).find(
|
|
125
|
-
(p) => p.CachePolicy?.CachePolicyConfig?.Name === config.Name,
|
|
126
|
-
);
|
|
127
|
-
if (found?.CachePolicy?.Id) {
|
|
128
|
-
const id = found.CachePolicy.Id;
|
|
129
|
-
const current = await cf.send(new GetCachePolicyConfigCommand({ Id: id }));
|
|
130
|
-
if (diffPaths(normalize(current.CachePolicyConfig), normalize(config)).length) {
|
|
131
|
-
await cf.send(
|
|
132
|
-
new UpdateCachePolicyCommand({ Id: id, IfMatch: current.ETag, CachePolicyConfig: config }),
|
|
133
|
-
);
|
|
134
|
-
ctx.log('info', 'updated cache policy', `cache-policy ${config.Name}`);
|
|
135
|
-
}
|
|
136
|
-
return id;
|
|
137
|
-
}
|
|
138
|
-
const res = await cf.send(new CreateCachePolicyCommand({ CachePolicyConfig: config }));
|
|
139
|
-
ctx.log('info', 'created cache policy', `cache-policy ${config.Name}`);
|
|
140
|
-
return res.CachePolicy!.Id!;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** The platform router's cache policy: origins decide caching; the host is part of the key. */
|
|
144
|
-
export function routerCachePolicy(name: string): CachePolicyConfig {
|
|
145
|
-
return {
|
|
146
|
-
Name: name,
|
|
147
|
-
Comment: 'platform router: origin-controlled caching, per-host keys',
|
|
148
|
-
MinTTL: 0,
|
|
149
|
-
DefaultTTL: 0,
|
|
150
|
-
MaxTTL: 31_536_000,
|
|
151
|
-
ParametersInCacheKeyAndForwardedToOrigin: {
|
|
152
|
-
EnableAcceptEncodingGzip: true,
|
|
153
|
-
EnableAcceptEncodingBrotli: true,
|
|
154
|
-
HeadersConfig: {
|
|
155
|
-
HeaderBehavior: 'whitelist',
|
|
156
|
-
Headers: { Quantity: 2, Items: ['x-platform-host', 'authorization'] },
|
|
157
|
-
},
|
|
158
|
-
CookiesConfig: { CookieBehavior: 'none' },
|
|
159
|
-
QueryStringsConfig: { QueryStringBehavior: 'all' },
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export interface DistributionState {
|
|
165
|
-
id: string;
|
|
166
|
-
arn: string;
|
|
167
|
-
domainName: string;
|
|
168
|
-
status: string;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* A distribution identified by its `Comment` (CloudFront has no names).
|
|
173
|
-
* `config` is the full desired DistributionConfig minus CallerReference.
|
|
174
|
-
*/
|
|
175
|
-
export async function ensureDistribution(
|
|
176
|
-
ctx: EngineContext,
|
|
177
|
-
spec: {
|
|
178
|
-
comment: string;
|
|
179
|
-
config: Omit<DistributionConfig, 'CallerReference' | 'Comment'>;
|
|
180
|
-
tags?: Record<string, string>;
|
|
181
|
-
},
|
|
182
|
-
): Promise<DistributionState> {
|
|
183
|
-
const cf = ctx.clients.cloudfront;
|
|
184
|
-
const existing = await findDistribution(ctx, spec.comment);
|
|
185
|
-
if (!existing) {
|
|
186
|
-
const res = await cf.send(
|
|
187
|
-
new CreateDistributionWithTagsCommand({
|
|
188
|
-
DistributionConfigWithTags: {
|
|
189
|
-
DistributionConfig: {
|
|
190
|
-
...spec.config,
|
|
191
|
-
Comment: spec.comment,
|
|
192
|
-
CallerReference: `${spec.comment}-${Date.now()}`,
|
|
193
|
-
},
|
|
194
|
-
Tags: { Items: tagList(spec.tags ?? {}) },
|
|
195
|
-
},
|
|
196
|
-
}),
|
|
197
|
-
);
|
|
198
|
-
const d = res.Distribution!;
|
|
199
|
-
ctx.log('info', 'created distribution (propagation takes a few minutes)', `distribution ${spec.comment}`);
|
|
200
|
-
return { id: d.Id!, arn: d.ARN!, domainName: d.DomainName!, status: d.Status! };
|
|
201
|
-
}
|
|
202
|
-
const current = await cf.send(new GetDistributionConfigCommand({ Id: existing.id }));
|
|
203
|
-
// UpdateDistribution replaces the whole config and rejects one that omits
|
|
204
|
-
// required fields (SmoothStreaming, FieldLevelEncryptionId, ...), so the
|
|
205
|
-
// desired settings are deep-merged over what CloudFront returned.
|
|
206
|
-
const merged = deepMerge(current.DistributionConfig!, {
|
|
207
|
-
...spec.config,
|
|
208
|
-
Comment: spec.comment,
|
|
209
|
-
CallerReference: current.DistributionConfig!.CallerReference,
|
|
210
|
-
}) as DistributionConfig;
|
|
211
|
-
const changed = diffPaths(normalize(current.DistributionConfig!), normalize(merged));
|
|
212
|
-
if (changed.length) {
|
|
213
|
-
await cf.send(
|
|
214
|
-
new UpdateDistributionCommand({ Id: existing.id, IfMatch: current.ETag, DistributionConfig: merged }),
|
|
215
|
-
);
|
|
216
|
-
ctx.log(
|
|
217
|
-
'info',
|
|
218
|
-
`updated distribution (${changed.slice(0, 5).join(', ')})`,
|
|
219
|
-
`distribution ${spec.comment}`,
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
return existing;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export async function findDistribution(
|
|
226
|
-
ctx: EngineContext,
|
|
227
|
-
comment: string,
|
|
228
|
-
): Promise<DistributionState | undefined> {
|
|
229
|
-
const cf = ctx.clients.cloudfront;
|
|
230
|
-
let Marker: string | undefined;
|
|
231
|
-
do {
|
|
232
|
-
const res = await cf.send(new ListDistributionsCommand({ Marker }));
|
|
233
|
-
const found = (res.DistributionList?.Items ?? []).find((d) => d.Comment === comment);
|
|
234
|
-
if (found)
|
|
235
|
-
return { id: found.Id!, arn: found.ARN!, domainName: found.DomainName!, status: found.Status! };
|
|
236
|
-
Marker = res.DistributionList?.IsTruncated ? res.DistributionList.NextMarker : undefined;
|
|
237
|
-
} while (Marker);
|
|
238
|
-
return undefined;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Overlay `patch` on `base`: objects merge key by key, equal-length arrays
|
|
243
|
-
* merge element by element (CloudFront lists like Origins keep their order),
|
|
244
|
-
* anything else is replaced. Undefined patch values keep the base value.
|
|
245
|
-
*/
|
|
246
|
-
export function deepMerge(base: unknown, patch: unknown): unknown {
|
|
247
|
-
if (patch === undefined) return base;
|
|
248
|
-
if (Array.isArray(base) && Array.isArray(patch)) {
|
|
249
|
-
return base.length === patch.length ? patch.map((p, i) => deepMerge(base[i], p)) : patch;
|
|
250
|
-
}
|
|
251
|
-
if (isPlainObject(base) && isPlainObject(patch)) {
|
|
252
|
-
const out: Record<string, unknown> = { ...base };
|
|
253
|
-
for (const [k, v] of Object.entries(patch)) out[k] = deepMerge(base[k], v);
|
|
254
|
-
return out;
|
|
255
|
-
}
|
|
256
|
-
return patch;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
260
|
-
return typeof v === 'object' && v !== null && !Array.isArray(v) && !(v instanceof Date);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/** Paths where two normalized values differ, e.g. `DefaultCacheBehavior.Compress`. */
|
|
264
|
-
export function diffPaths(a: unknown, b: unknown, path = ''): string[] {
|
|
265
|
-
if (JSON.stringify(a) === JSON.stringify(b)) return [];
|
|
266
|
-
if (isPlainObject(a) && isPlainObject(b)) {
|
|
267
|
-
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
268
|
-
return [...keys].flatMap((k) => diffPaths(a[k], b[k], path ? `${path}.${k}` : k));
|
|
269
|
-
}
|
|
270
|
-
if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
|
|
271
|
-
return a.flatMap((v, i) => diffPaths(v, b[i], `${path}[${i}]`));
|
|
272
|
-
}
|
|
273
|
-
return [path || '(root)'];
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/** Drop undefined fields so a fetched config and a desired config compare equal. */
|
|
277
|
-
function normalize(v: unknown): unknown {
|
|
278
|
-
// String lists in a distribution config (methods, aliases, header names) are
|
|
279
|
-
// sets; CloudFront returns them in its own order.
|
|
280
|
-
if (Array.isArray(v) && v.every((x) => typeof x === 'string')) return [...v].sort();
|
|
281
|
-
if (Array.isArray(v)) return v.map(normalize);
|
|
282
|
-
if (v && typeof v === 'object') {
|
|
283
|
-
const out: Record<string, unknown> = {};
|
|
284
|
-
for (const k of Object.keys(v as object).sort()) {
|
|
285
|
-
const val = (v as Record<string, unknown>)[k];
|
|
286
|
-
if (val !== undefined) out[k] = normalize(val);
|
|
287
|
-
}
|
|
288
|
-
return out;
|
|
289
|
-
}
|
|
290
|
-
return v;
|
|
291
|
-
}
|
package/src/drivers/route53.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
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
|
-
}
|
package/src/drivers/sns.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
}
|