@geekmidas/cloud 10.0.0-alpha.15 → 10.0.0-alpha.150
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/README.md +14 -6
- package/package.json +9 -8
- package/src/sst/__tests__/api.spec.ts +0 -46
- package/src/sst/__tests__/bootstrap.spec.ts +15 -0
- package/src/sst/__tests__/encryption.spec.ts +90 -0
- package/src/sst/__tests__/fromManifest.inbox.spec.ts +160 -0
- package/src/sst/__tests__/fromManifest.integration.spec.ts +86 -2
- package/src/sst/__tests__/fromManifest.secret.spec.ts +79 -0
- package/src/sst/__tests__/fromManifest.spec.ts +379 -5
- package/src/sst/__tests__/function.spec.ts +0 -71
- package/src/sst/__tests__/surfaces.spec.ts +17 -0
- package/src/sst/__tests__/wildcardSite.spec.ts +195 -0
- package/src/sst/__type-tests__/overrides.type-test.ts +152 -0
- package/src/sst/aws/Api.ts +0 -43
- package/src/sst/aws/Credential.ts +42 -9
- package/src/sst/aws/Cron.ts +5 -45
- package/src/sst/aws/DatabaseBootstrap.ts +6 -0
- package/src/sst/aws/Encryption.ts +91 -0
- package/src/sst/aws/ExternalApi.ts +58 -0
- package/src/sst/aws/Function.ts +9 -38
- package/src/sst/aws/Inbox.ts +138 -0
- package/src/sst/aws/Queue.ts +34 -1
- package/src/sst/aws/RestApiSurface.ts +36 -14
- package/src/sst/aws/Secret.ts +5 -3
- package/src/sst/aws/StaticSite.ts +44 -1
- package/src/sst/aws/bootstrap/handler.ts +12 -1
- package/src/sst/errors.ts +15 -0
- package/src/sst/fromManifest.ts +472 -50
- package/src/sst/index.ts +13 -1
- package/src/sst/overrides.ts +147 -0
- package/src/sst/__type-tests__/manifest.type-test.ts +0 -77
package/src/sst/fromManifest.ts
CHANGED
|
@@ -19,6 +19,8 @@ import type {
|
|
|
19
19
|
Declaration,
|
|
20
20
|
DeclarationKind,
|
|
21
21
|
Dependency,
|
|
22
|
+
SecretDeclaration,
|
|
23
|
+
SecretFormat,
|
|
22
24
|
SiteDeclaration,
|
|
23
25
|
} from '@geekmidas/manifest';
|
|
24
26
|
import {
|
|
@@ -26,11 +28,17 @@ import {
|
|
|
26
28
|
cookieDomain,
|
|
27
29
|
DEFAULT_POSTGRES_VERSION,
|
|
28
30
|
dependentsOf,
|
|
31
|
+
isWildcardSite,
|
|
32
|
+
mobileOrigins,
|
|
29
33
|
PUBLIC,
|
|
30
34
|
PUBLIC_PREFIX,
|
|
31
35
|
providedKeyFor,
|
|
32
36
|
provisionOrder,
|
|
37
|
+
SECRET_LENGTH,
|
|
38
|
+
SECRET_SYMBOLS,
|
|
39
|
+
schemeBase,
|
|
33
40
|
} from '@geekmidas/manifest';
|
|
41
|
+
import { RandomPassword } from '@pulumi/random';
|
|
34
42
|
import {
|
|
35
43
|
Cache,
|
|
36
44
|
CacheIsAmbiguous,
|
|
@@ -39,15 +47,20 @@ import {
|
|
|
39
47
|
withCacheTable,
|
|
40
48
|
} from './aws/Cache';
|
|
41
49
|
import { Credential } from './aws/Credential';
|
|
42
|
-
import {
|
|
50
|
+
import { Cron, type CronSchedule } from './aws/Cron';
|
|
51
|
+
import { Database, DatabaseNeedsVpc, type DatabaseProps } from './aws/Database';
|
|
43
52
|
import { DatabaseBootstrap } from './aws/DatabaseBootstrap';
|
|
44
53
|
import { DatabaseReader, DatabaseSchema } from './aws/DerivedDatabase';
|
|
45
54
|
import { Email, EmailNeedsSender } from './aws/Email';
|
|
55
|
+
import { Encryption, type EncryptionProps } from './aws/Encryption';
|
|
56
|
+
import { ExternalApi, type ExternalApiProps } from './aws/ExternalApi';
|
|
46
57
|
import { FileServer } from './aws/FileServer';
|
|
58
|
+
import { Function } from './aws/Function';
|
|
59
|
+
import { Inbox, InboxTopic } from './aws/Inbox';
|
|
47
60
|
import { ObjectStorage } from './aws/ObjectStorage';
|
|
48
61
|
import { Queue } from './aws/Queue';
|
|
49
62
|
import { RestApiSurface } from './aws/RestApiSurface';
|
|
50
|
-
import { Secret } from './aws/Secret';
|
|
63
|
+
import { Secret, type SecretProps } from './aws/Secret';
|
|
51
64
|
import { StaticSite } from './aws/StaticSite';
|
|
52
65
|
import { Topic } from './aws/Topic';
|
|
53
66
|
import {
|
|
@@ -56,6 +69,7 @@ import {
|
|
|
56
69
|
UnresolvedDependency,
|
|
57
70
|
} from './errors';
|
|
58
71
|
import type { GkmLinkable } from './Linkable';
|
|
72
|
+
import type { ManifestBackends, ManifestOverrides } from './overrides';
|
|
59
73
|
import type { StackType } from './Stack';
|
|
60
74
|
|
|
61
75
|
/**
|
|
@@ -105,6 +119,8 @@ export interface ProvisionContext {
|
|
|
105
119
|
*/
|
|
106
120
|
cache?: 'upstash' | 'elasticache' | 'db';
|
|
107
121
|
email?: 'resend' | 'ses' | 'smtp';
|
|
122
|
+
/** The labels no tenant of a wildcard site may take. */
|
|
123
|
+
protectedSubdomains?: readonly string[];
|
|
108
124
|
/**
|
|
109
125
|
* One deferred caller list per surface, resolved once everything exists.
|
|
110
126
|
*
|
|
@@ -133,21 +149,6 @@ type Provisioner = (
|
|
|
133
149
|
context: ProvisionContext,
|
|
134
150
|
) => Provisioned;
|
|
135
151
|
|
|
136
|
-
/**
|
|
137
|
-
* Provider-specific props, per construct id.
|
|
138
|
-
*
|
|
139
|
-
* Neutral options — `versioned`, and later `cdn` — travel in the declaration,
|
|
140
|
-
* because the app legitimately has an opinion about them. Anything with S3 in
|
|
141
|
-
* its name does not belong in application code, so lifecycle rules, CORS, and
|
|
142
|
-
* canned ACLs are supplied here, in the deploy layer, keyed by the id they
|
|
143
|
-
* apply to and typed against the component that receives them.
|
|
144
|
-
*
|
|
145
|
-
* A third escape hatch needs no API at all: `fromManifest` returns the
|
|
146
|
-
* components, so `provisioned.Uploads.nodes.bucket` is reachable for anything
|
|
147
|
-
* neither route covers.
|
|
148
|
-
*/
|
|
149
|
-
export type ComponentOverrides = Record<string, Record<string, unknown>>;
|
|
150
|
-
|
|
151
152
|
/**
|
|
152
153
|
* Which component provisions which kind.
|
|
153
154
|
*
|
|
@@ -189,7 +190,15 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
189
190
|
return new StaticSite(stack, d.id, {
|
|
190
191
|
path: d.app.path,
|
|
191
192
|
variant: d.variant,
|
|
192
|
-
environment: siteEnvironment(d, context),
|
|
193
|
+
environment: siteEnvironment(d, context, stack),
|
|
194
|
+
// Every subdomain, on one distribution: see `StaticSite`.
|
|
195
|
+
...(isWildcardSite(d)
|
|
196
|
+
? {
|
|
197
|
+
tenants: {
|
|
198
|
+
protectedSubdomains: context.protectedSubdomains ?? [],
|
|
199
|
+
},
|
|
200
|
+
}
|
|
201
|
+
: {}),
|
|
193
202
|
...props,
|
|
194
203
|
});
|
|
195
204
|
},
|
|
@@ -206,6 +215,12 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
206
215
|
// producer's and subscribers' business, and SNS has no per-event
|
|
207
216
|
// configuration to map it onto.
|
|
208
217
|
topic: (stack, d, props) => new Topic(stack, d.id, props),
|
|
218
|
+
// What receives the mail is its provider's, made before SST runs; the
|
|
219
|
+
// inbox is the values that made, linked. See `aws/Inbox`.
|
|
220
|
+
inbox: (stack, d, props) => {
|
|
221
|
+
if (d.kind !== 'inbox') throw new UnknownDeclarationKind(d.kind, []);
|
|
222
|
+
return new Inbox(stack, d.id, { ...props, topicId: d.topic });
|
|
223
|
+
},
|
|
209
224
|
|
|
210
225
|
database: (stack, d, props, context) => {
|
|
211
226
|
if (d.kind !== 'database') throw new UnknownDeclarationKind(d.kind, []);
|
|
@@ -220,7 +235,11 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
220
235
|
// rather than whatever Aurora defaults to that month. Local ran 18
|
|
221
236
|
// while this ran 17.7, and neither was written down anywhere.
|
|
222
237
|
version: String(d.version ?? DEFAULT_POSTGRES_VERSION),
|
|
223
|
-
|
|
238
|
+
// `DatabaseProps`, not `sst.aws.PostgresArgs`: the database needs the
|
|
239
|
+
// `Vpc` component itself (its bootstrap function runs in the same VPC
|
|
240
|
+
// and needs the security groups too), where RDS would accept bare
|
|
241
|
+
// subnet ids. TS 7 checks the spread; 6 let the wider type through.
|
|
242
|
+
...(props as unknown as Omit<DatabaseProps, 'schema'>),
|
|
224
243
|
});
|
|
225
244
|
|
|
226
245
|
// A database needs its own roles, not just its tenants'. Registering only
|
|
@@ -237,6 +256,10 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
237
256
|
schema: d.schema,
|
|
238
257
|
runtime,
|
|
239
258
|
owner: ownerRole(runtime),
|
|
259
|
+
ownsDatabase: true,
|
|
260
|
+
// RDS ships PostGIS, so the image is not a question here — only the
|
|
261
|
+
// `CREATE EXTENSION`, which the master can run and the owner cannot.
|
|
262
|
+
...(d.extensions?.length ? { extensions: d.extensions } : {}),
|
|
240
263
|
...(hasReader(d.id, context) ? { reader: readerRole(runtime) } : {}),
|
|
241
264
|
});
|
|
242
265
|
|
|
@@ -370,17 +393,12 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
370
393
|
},
|
|
371
394
|
|
|
372
395
|
/**
|
|
373
|
-
* The surface, without its routes.
|
|
374
|
-
*
|
|
375
|
-
* An API Gateway with no routes 404s everything, and that is the honest
|
|
376
|
-
* state: the *surface* is what this kind declares, and mounting handlers on
|
|
377
|
-
* it is the endpoint merge that has not landed — routes still reach the
|
|
378
|
-
* deploy target through the separate `RouteInfo[]` pipeline.
|
|
396
|
+
* The surface, without its routes yet.
|
|
379
397
|
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
398
|
+
* Its address is what a site inlines as `VITE_API_URL`, what an auth server
|
|
399
|
+
* puts on its trusted-origin list, and what the cookie domain derives from —
|
|
400
|
+
* so it is provisioned in order, and its endpoints are mounted by
|
|
401
|
+
* {@link mountRoutes} once everything they link to exists.
|
|
384
402
|
*/
|
|
385
403
|
'rest-api': (stack, d, props, context) =>
|
|
386
404
|
new RestApiSurface(stack, d.id, {
|
|
@@ -388,10 +406,41 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
388
406
|
callers: context.callers?.get(d.id)?.promise,
|
|
389
407
|
}),
|
|
390
408
|
|
|
391
|
-
|
|
409
|
+
// Generated on the first deploy and kept in the stage's state, as a server
|
|
410
|
+
// stage keeps it in its secrets: every later deploy reads the same value
|
|
411
|
+
// back. One set with `sst secret set` wins over it.
|
|
412
|
+
secret: (stack, d, props) =>
|
|
413
|
+
new Secret(stack, d.id, {
|
|
414
|
+
placeholder: generatedSecret(d.id, d as SecretDeclaration),
|
|
415
|
+
...(props as SecretProps),
|
|
416
|
+
}),
|
|
392
417
|
|
|
393
418
|
// The same storage as a secret, under a different role — see the component.
|
|
394
419
|
credential: (stack, d, props) => new Credential(stack, d.id, props),
|
|
420
|
+
|
|
421
|
+
// In the stage's region, as mail is: a key in another region is a key
|
|
422
|
+
// every call crosses the world to reach.
|
|
423
|
+
encryption: (stack, d, props) =>
|
|
424
|
+
new Encryption(stack, d.id, {
|
|
425
|
+
...(props as Partial<EncryptionProps>),
|
|
426
|
+
region:
|
|
427
|
+
(props as Partial<EncryptionProps>).region ??
|
|
428
|
+
$app.providers?.aws?.region ??
|
|
429
|
+
'us-east-1',
|
|
430
|
+
}),
|
|
431
|
+
|
|
432
|
+
// Nothing to provision: the URL this stage calls, and the credentials kept
|
|
433
|
+
// as a secret.
|
|
434
|
+
'external-api': (stack, d, props) => {
|
|
435
|
+
if (d.kind !== 'external-api') {
|
|
436
|
+
throw new UnknownDeclarationKind(d.kind, []);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return new ExternalApi(stack, d.id, {
|
|
440
|
+
...(props as Partial<ExternalApiProps>),
|
|
441
|
+
url: d.url,
|
|
442
|
+
});
|
|
443
|
+
},
|
|
395
444
|
};
|
|
396
445
|
|
|
397
446
|
/**
|
|
@@ -410,10 +459,25 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
410
459
|
export function siteEnvironment(
|
|
411
460
|
declaration: SiteDeclaration,
|
|
412
461
|
context: ProvisionContext,
|
|
462
|
+
stack?: StackType,
|
|
413
463
|
): Record<string, $util.Input<string>> {
|
|
414
464
|
const prefix = PUBLIC_PREFIX[declaration.variant];
|
|
415
465
|
const environment: Record<string, $util.Input<string>> = {};
|
|
416
466
|
|
|
467
|
+
// A wildcard site takes its tenant off the host it is reached on, so it
|
|
468
|
+
// inlines the domain to take the label off against, and the labels no
|
|
469
|
+
// tenant may be — the same two keys `publicEnvFor` names locally.
|
|
470
|
+
if (isWildcardSite(declaration) && stack?.domain) {
|
|
471
|
+
const own = {
|
|
472
|
+
domain: stack.domain,
|
|
473
|
+
protectedSubdomains: (context.protectedSubdomains ?? []).join(','),
|
|
474
|
+
};
|
|
475
|
+
for (const [role, value] of Object.entries(own)) {
|
|
476
|
+
environment[`${prefix}${providedKeyFor(declaration.id, 'site', role)}`] =
|
|
477
|
+
value;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
417
481
|
for (const edge of declaration.dependencies) {
|
|
418
482
|
const target = context.manifest[edge.target];
|
|
419
483
|
if (!target) continue;
|
|
@@ -572,9 +636,7 @@ function rootCluster(component: Provisioned): Database {
|
|
|
572
636
|
* printed: one line per route, naming what it can reach.
|
|
573
637
|
*
|
|
574
638
|
* A surface with no routes says so rather than printing nothing, because "no
|
|
575
|
-
* routes
|
|
576
|
-
* application's own API that is currently the true state, pending the endpoint
|
|
577
|
-
* merge.
|
|
639
|
+
* routes" and "this printed nothing" look identical otherwise.
|
|
578
640
|
*
|
|
579
641
|
* Pure, so what gets printed can be asserted without a deploy.
|
|
580
642
|
*/
|
|
@@ -587,8 +649,8 @@ export function describeRoutes(manifest: ConstructManifest): string[] {
|
|
|
587
649
|
lines.push(`${id}:`);
|
|
588
650
|
|
|
589
651
|
if (declaration.endpoints.length === 0) {
|
|
590
|
-
// Empty is
|
|
591
|
-
//
|
|
652
|
+
// Empty is genuinely empty: discovery nests every route in the
|
|
653
|
+
// surface that serves it, so a surface with none has none.
|
|
592
654
|
lines.push(' (no routes)');
|
|
593
655
|
}
|
|
594
656
|
|
|
@@ -602,11 +664,11 @@ export function describeRoutes(manifest: ConstructManifest): string[] {
|
|
|
602
664
|
);
|
|
603
665
|
}
|
|
604
666
|
|
|
605
|
-
//
|
|
606
|
-
// nothing
|
|
607
|
-
if (declaration.
|
|
667
|
+
// What authenticates it, printed apart from the routes because it grants
|
|
668
|
+
// nothing: the auth server learns this surface's origin.
|
|
669
|
+
if (declaration.auth) {
|
|
608
670
|
lines.push(
|
|
609
|
-
`
|
|
671
|
+
` auth ${declaration.auth.target} (origin only; grants nothing)`,
|
|
610
672
|
);
|
|
611
673
|
}
|
|
612
674
|
}
|
|
@@ -643,6 +705,8 @@ async function callersOf(
|
|
|
643
705
|
id: string,
|
|
644
706
|
manifest: ConstructManifest,
|
|
645
707
|
provisioned: ProvisionedManifest,
|
|
708
|
+
/** The app's name — a mobile caller's scheme base when it gives none. */
|
|
709
|
+
project: string,
|
|
646
710
|
): Promise<{ trustedOrigins: string; cookieDomain?: string }> {
|
|
647
711
|
// Every address here is a Pulumi output, not a string — a CloudFront domain
|
|
648
712
|
// and an API Gateway endpoint are both known only after their resource
|
|
@@ -667,8 +731,17 @@ async function callersOf(
|
|
|
667
731
|
const own = await resolved(provisioned[id]?.provides().url);
|
|
668
732
|
const domain = cookieDomain([...(own ? [own] : []), ...origins]);
|
|
669
733
|
|
|
734
|
+
// A mobile caller is reached by its scheme — the bare one, deployed: what
|
|
735
|
+
// the store build registers.
|
|
736
|
+
const schemes = dependentsOf(manifest, id).flatMap((caller) => {
|
|
737
|
+
const app = manifest[caller];
|
|
738
|
+
return app?.kind === 'mobile-app'
|
|
739
|
+
? mobileOrigins(schemeBase(project, app.scheme))
|
|
740
|
+
: [];
|
|
741
|
+
});
|
|
742
|
+
|
|
670
743
|
return {
|
|
671
|
-
trustedOrigins: origins.join(','),
|
|
744
|
+
trustedOrigins: [...origins, ...schemes].join(','),
|
|
672
745
|
...(domain ? { cookieDomain: domain } : {}),
|
|
673
746
|
};
|
|
674
747
|
}
|
|
@@ -718,6 +791,299 @@ export function isServed(id: string, manifest: ConstructManifest): boolean {
|
|
|
718
791
|
);
|
|
719
792
|
}
|
|
720
793
|
|
|
794
|
+
/**
|
|
795
|
+
* Kinds the in-order pass skips.
|
|
796
|
+
*
|
|
797
|
+
* - `mobile-app` ships through EAS and the stores. It is in the manifest for
|
|
798
|
+
* what it makes the surfaces trust, which `callersOf` reads off the graph.
|
|
799
|
+
* - `worker` is a process, not a resource: what it runs is provisioned as
|
|
800
|
+
* what it is — a queue's consumer by {@link subscribeConsumers}.
|
|
801
|
+
* - `cron` and `function` are Lambdas linked to what they depend on, so they
|
|
802
|
+
* are provisioned once all of it exists, by {@link provisionCompute}.
|
|
803
|
+
* - `telemetry` provisions nothing: where it goes is the stage's
|
|
804
|
+
* `deploy.telemetry`, a provider somebody else runs.
|
|
805
|
+
*/
|
|
806
|
+
export const PROVISIONED_ELSEWHERE: ReadonlySet<DeclarationKind> =
|
|
807
|
+
new Set<DeclarationKind>([
|
|
808
|
+
'mobile-app',
|
|
809
|
+
'worker',
|
|
810
|
+
'cron',
|
|
811
|
+
'function',
|
|
812
|
+
'telemetry',
|
|
813
|
+
]);
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* Every function and cron: a Lambda running the handler the build wrote,
|
|
817
|
+
* linked to exactly what it depends on, in the database's network when it
|
|
818
|
+
* reaches one. A cron is that Lambda on its schedule; a function is reached
|
|
819
|
+
* at its URL, which only callers granted IAM access can invoke.
|
|
820
|
+
*
|
|
821
|
+
* After everything else is provisioned, because each links to things
|
|
822
|
+
* `provisionOrder` does not order before it.
|
|
823
|
+
*/
|
|
824
|
+
export function provisionCompute(
|
|
825
|
+
stack: StackType,
|
|
826
|
+
manifest: ConstructManifest,
|
|
827
|
+
provisioned: ProvisionedManifest,
|
|
828
|
+
): Cron[] {
|
|
829
|
+
const crons: Cron[] = [];
|
|
830
|
+
|
|
831
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
832
|
+
if (declaration?.kind !== 'function' && declaration?.kind !== 'cron') {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
const { link } = resolveEdges(declaration.dependencies, provisioned);
|
|
837
|
+
const vpc = vpcFor(link);
|
|
838
|
+
const lambda = new Function(
|
|
839
|
+
stack,
|
|
840
|
+
declaration.kind === 'cron' ? `${id}Function` : id,
|
|
841
|
+
{
|
|
842
|
+
name: stack.logicalPrefixedName(id),
|
|
843
|
+
handler: declaration.handler,
|
|
844
|
+
link,
|
|
845
|
+
...(vpc ? { vpc } : {}),
|
|
846
|
+
...(declaration.kind === 'function'
|
|
847
|
+
? { url: { authorization: 'iam' } }
|
|
848
|
+
: {}),
|
|
849
|
+
},
|
|
850
|
+
);
|
|
851
|
+
|
|
852
|
+
if (declaration.kind === 'cron') {
|
|
853
|
+
crons.push(
|
|
854
|
+
new Cron(stack, id, {
|
|
855
|
+
processor: lambda,
|
|
856
|
+
schedule: declaration.schedule as CronSchedule,
|
|
857
|
+
}),
|
|
858
|
+
);
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
assertProvides(
|
|
863
|
+
id,
|
|
864
|
+
declaration.provides,
|
|
865
|
+
Object.keys(lambda.provides()).map((role) =>
|
|
866
|
+
providedKeyFor(id, declaration.kind, role),
|
|
867
|
+
),
|
|
868
|
+
);
|
|
869
|
+
provisioned[id] = lambda;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
return crons;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Every file server's processors: each a Lambda behind a function URL with
|
|
877
|
+
* IAM auth, served through the file server's router on its pattern's prefix
|
|
878
|
+
* — so the CDN signs each request (origin access control) and the URL cannot
|
|
879
|
+
* be called around the cache. Linked to the bucket it reads and what it
|
|
880
|
+
* depends on; its native packages installed for the Lambda, not bundled.
|
|
881
|
+
*
|
|
882
|
+
* After everything else, for the reason {@link provisionCompute} is.
|
|
883
|
+
*/
|
|
884
|
+
export function provisionProcessors(
|
|
885
|
+
stack: StackType,
|
|
886
|
+
manifest: ConstructManifest,
|
|
887
|
+
provisioned: ProvisionedManifest,
|
|
888
|
+
): Function[] {
|
|
889
|
+
const lambdas: Function[] = [];
|
|
890
|
+
|
|
891
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
892
|
+
if (declaration?.kind !== 'file-server') continue;
|
|
893
|
+
const router = provisioned[id];
|
|
894
|
+
if (!router) continue;
|
|
895
|
+
|
|
896
|
+
for (const processor of declaration.processors ?? []) {
|
|
897
|
+
const { link } = resolveEdges(processor.dependencies, provisioned);
|
|
898
|
+
const vpc = vpcFor(link);
|
|
899
|
+
const native = processor.native ?? [];
|
|
900
|
+
lambdas.push(
|
|
901
|
+
new Function(stack, `${id}${processor.id}`, {
|
|
902
|
+
name: stack.logicalPrefixedName(`${id}${processor.id}`),
|
|
903
|
+
handler: processor.handler,
|
|
904
|
+
link,
|
|
905
|
+
...(vpc ? { vpc } : {}),
|
|
906
|
+
...(processor.timeout !== undefined
|
|
907
|
+
? {
|
|
908
|
+
timeout:
|
|
909
|
+
`${Math.ceil(processor.timeout / 1000)} seconds` as sst.aws.FunctionArgs['timeout'],
|
|
910
|
+
}
|
|
911
|
+
: {}),
|
|
912
|
+
...(processor.memorySize !== undefined
|
|
913
|
+
? {
|
|
914
|
+
memory:
|
|
915
|
+
`${processor.memorySize} MB` as sst.aws.FunctionArgs['memory'],
|
|
916
|
+
}
|
|
917
|
+
: {}),
|
|
918
|
+
...(native.length ? { nodejs: { install: [...native] } } : {}),
|
|
919
|
+
url: {
|
|
920
|
+
authorization: 'iam',
|
|
921
|
+
router: {
|
|
922
|
+
instance: router as unknown as sst.aws.Router,
|
|
923
|
+
path: processor.prefix.replace(/\/$/, ''),
|
|
924
|
+
},
|
|
925
|
+
},
|
|
926
|
+
}),
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
return lambdas;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/** A provisioned queue: what {@link subscribeConsumers} subscribes through. */
|
|
935
|
+
interface Consumable {
|
|
936
|
+
consume(consumer: {
|
|
937
|
+
handler: string;
|
|
938
|
+
link?: unknown[];
|
|
939
|
+
vpc?: sst.aws.Vpc;
|
|
940
|
+
}): unknown;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* The network a function has to run in to reach what it links to.
|
|
945
|
+
*
|
|
946
|
+
* A database is private to its VPC, so a handler linked to one — or to a
|
|
947
|
+
* schema tenant or reader of one — runs inside it, or its connection times out.
|
|
948
|
+
*/
|
|
949
|
+
export function vpcFor(link: readonly Provisioned[]): sst.aws.Vpc | undefined {
|
|
950
|
+
for (const component of link) {
|
|
951
|
+
const cluster = rootCluster(component);
|
|
952
|
+
if (cluster instanceof Database) return cluster.vpc;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
return undefined;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* Mount every surface's endpoints — the handlers the build wrote, each linked
|
|
960
|
+
* to exactly what that endpoint depends on.
|
|
961
|
+
*
|
|
962
|
+
* After every construct is provisioned, for the reason consumers are: an
|
|
963
|
+
* endpoint links to things `provisionOrder` does not order before its surface.
|
|
964
|
+
*/
|
|
965
|
+
export function mountRoutes(
|
|
966
|
+
manifest: ConstructManifest,
|
|
967
|
+
provisioned: ProvisionedManifest,
|
|
968
|
+
): void {
|
|
969
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
970
|
+
if (declaration?.kind !== 'rest-api') continue;
|
|
971
|
+
|
|
972
|
+
const surface = provisioned[id];
|
|
973
|
+
if (!(surface instanceof RestApiSurface)) continue;
|
|
974
|
+
|
|
975
|
+
for (const endpoint of declaration.endpoints) {
|
|
976
|
+
const { link } = resolveEdges(endpoint.dependencies, provisioned);
|
|
977
|
+
const vpc = vpcFor(link);
|
|
978
|
+
surface.mount({
|
|
979
|
+
method: endpoint.method,
|
|
980
|
+
path: endpoint.path,
|
|
981
|
+
handler: endpoint.handler,
|
|
982
|
+
link,
|
|
983
|
+
...(vpc ? { vpc } : {}),
|
|
984
|
+
...(endpoint.authorizer ? { authorizer: endpoint.authorizer } : {}),
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Subscribe every queue's one consumer — the handler the build wrote for it,
|
|
992
|
+
* linked to what that consumer depends on.
|
|
993
|
+
*
|
|
994
|
+
* After every construct is provisioned, because a consumer links to things
|
|
995
|
+
* that `provisionOrder` does not order before the queue.
|
|
996
|
+
*/
|
|
997
|
+
export function subscribeConsumers(
|
|
998
|
+
manifest: ConstructManifest,
|
|
999
|
+
provisioned: ProvisionedManifest,
|
|
1000
|
+
): void {
|
|
1001
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
1002
|
+
if (declaration?.kind !== 'queue') continue;
|
|
1003
|
+
|
|
1004
|
+
const queue = provisioned[id] as unknown as Consumable | undefined;
|
|
1005
|
+
if (!queue) continue;
|
|
1006
|
+
|
|
1007
|
+
const { link } = resolveEdges(declaration.worker.dependencies, provisioned);
|
|
1008
|
+
const vpc = vpcFor(link);
|
|
1009
|
+
queue.consume({
|
|
1010
|
+
handler: declaration.worker.handler,
|
|
1011
|
+
link,
|
|
1012
|
+
...(vpc ? { vpc } : {}),
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/** The topics inboxes declared: their provider's, never SST's to create. */
|
|
1018
|
+
export function inboxTopicIds(manifest: ConstructManifest): Set<string> {
|
|
1019
|
+
return new Set(
|
|
1020
|
+
Object.values(manifest).flatMap((declaration) =>
|
|
1021
|
+
declaration.kind === 'inbox' ? [declaration.topic] : [],
|
|
1022
|
+
),
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Each inbox's topic, as the provider made it — the ARN the inbox read from
|
|
1028
|
+
* the deploy — under the topic's own id, so its declaration's contract is
|
|
1029
|
+
* checked like any other.
|
|
1030
|
+
*/
|
|
1031
|
+
export function referenceInboxTopics(
|
|
1032
|
+
manifest: ConstructManifest,
|
|
1033
|
+
provisioned: ProvisionedManifest,
|
|
1034
|
+
): void {
|
|
1035
|
+
for (const declaration of Object.values(manifest)) {
|
|
1036
|
+
if (declaration.kind !== 'inbox') continue;
|
|
1037
|
+
const inbox = provisioned[declaration.id];
|
|
1038
|
+
if (!(inbox instanceof Inbox)) continue;
|
|
1039
|
+
|
|
1040
|
+
const topic = new InboxTopic(declaration.topic, inbox);
|
|
1041
|
+
const topicDeclaration = manifest[declaration.topic];
|
|
1042
|
+
assertProvides(
|
|
1043
|
+
declaration.topic,
|
|
1044
|
+
topicDeclaration?.provides,
|
|
1045
|
+
Object.keys(topic.provides()).map((role) =>
|
|
1046
|
+
providedKeyFor(declaration.topic, 'topic', role),
|
|
1047
|
+
),
|
|
1048
|
+
);
|
|
1049
|
+
provisioned[declaration.topic] = topic as unknown as Provisioned;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Subscribe each inbox subscriber's Lambda to its inbox's topic — the
|
|
1055
|
+
* provider's, by ARN — linked to what it depends on, its inbox included.
|
|
1056
|
+
*
|
|
1057
|
+
* Unfiltered: SES publishes its receipt notification with no `type` attribute
|
|
1058
|
+
* to filter on, and every message on an inbox's topic is for its subscribers.
|
|
1059
|
+
* The subscriber's adaptor reads the notification as `email.received`.
|
|
1060
|
+
*/
|
|
1061
|
+
export function subscribeInboxes(
|
|
1062
|
+
manifest: ConstructManifest,
|
|
1063
|
+
provisioned: ProvisionedManifest,
|
|
1064
|
+
): void {
|
|
1065
|
+
for (const declaration of Object.values(manifest)) {
|
|
1066
|
+
if (declaration.kind !== 'inbox') continue;
|
|
1067
|
+
const inbox = provisioned[declaration.id];
|
|
1068
|
+
const topic = manifest[declaration.topic];
|
|
1069
|
+
if (!(inbox instanceof Inbox) || topic?.kind !== 'topic') continue;
|
|
1070
|
+
|
|
1071
|
+
for (const subscriber of topic.subscribers) {
|
|
1072
|
+
const { link } = resolveEdges(subscriber.dependencies, provisioned);
|
|
1073
|
+
const vpc = vpcFor(link);
|
|
1074
|
+
sst.aws.SnsTopic.subscribe(
|
|
1075
|
+
`${subscriber.id}Subscription`,
|
|
1076
|
+
inbox.topicArn,
|
|
1077
|
+
{
|
|
1078
|
+
handler: subscriber.handler,
|
|
1079
|
+
link,
|
|
1080
|
+
...(vpc ? { vpc } : {}),
|
|
1081
|
+
},
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
721
1087
|
/** The provisioner for a kind. Pure — the lookup is testable without Pulumi. */
|
|
722
1088
|
export function provisionerFor(kind: DeclarationKind): Provisioner {
|
|
723
1089
|
const provisioner = PROVISIONERS[kind];
|
|
@@ -756,20 +1122,34 @@ export function assertProvides(
|
|
|
756
1122
|
* tenant, a read replica, and the surface over a bucket all resolve an address
|
|
757
1123
|
* off something else, and a pass in map order would find it half the time.
|
|
758
1124
|
*/
|
|
759
|
-
export function fromManifest
|
|
1125
|
+
export function fromManifest<
|
|
1126
|
+
const M extends ConstructManifest,
|
|
1127
|
+
const B extends ManifestBackends = {},
|
|
1128
|
+
>(
|
|
760
1129
|
stack: StackType,
|
|
761
|
-
manifest:
|
|
762
|
-
|
|
1130
|
+
manifest: M,
|
|
1131
|
+
/**
|
|
1132
|
+
* Provider-specific props, per construct id — typed from the manifest, so
|
|
1133
|
+
* only its ids are keys, each takes what its kind's component reads, and
|
|
1134
|
+
* what the synth cannot guess (a database's `vpc`, mail's `from`) is
|
|
1135
|
+
* required. See {@link ManifestOverrides}.
|
|
1136
|
+
*
|
|
1137
|
+
* Neutral options — `versioned`, `fifo` — travel in the declaration,
|
|
1138
|
+
* because the app legitimately has an opinion about them. Anything with S3
|
|
1139
|
+
* in its name does not belong in application code, so it is supplied here.
|
|
1140
|
+
* A third escape hatch needs no API: the components are returned, so
|
|
1141
|
+
* `provisioned.Uploads.nodes.bucket` is reachable for anything else.
|
|
1142
|
+
*/
|
|
1143
|
+
overrides: ManifestOverrides<M, B>,
|
|
763
1144
|
/**
|
|
764
1145
|
* The backend choices that are config rather than declaration.
|
|
765
1146
|
*
|
|
766
1147
|
* Defaulted the same way the local target defaults them, so a stage deployed
|
|
767
|
-
* without saying gets the same backend a developer ran against.
|
|
1148
|
+
* without saying gets the same backend a developer ran against. Typed
|
|
1149
|
+
* literally, because the backend decides what is required above: an
|
|
1150
|
+
* ElastiCache cache needs a `vpc`, Resend or SMTP mail a `url`.
|
|
768
1151
|
*/
|
|
769
|
-
backends: {
|
|
770
|
-
cache?: 'upstash' | 'elasticache' | 'db';
|
|
771
|
-
email?: 'resend' | 'ses' | 'smtp';
|
|
772
|
-
} = {},
|
|
1152
|
+
backends: B = {} as B,
|
|
773
1153
|
): ProvisionedManifest {
|
|
774
1154
|
const provisioned: ProvisionedManifest = {};
|
|
775
1155
|
|
|
@@ -801,16 +1181,24 @@ export function fromManifest(
|
|
|
801
1181
|
bootstraps: new Map<string, DatabaseBootstrap>(),
|
|
802
1182
|
...(backends.cache ? { cache: backends.cache } : {}),
|
|
803
1183
|
...(backends.email ? { email: backends.email } : {}),
|
|
1184
|
+
...(backends.protectedSubdomains
|
|
1185
|
+
? { protectedSubdomains: backends.protectedSubdomains }
|
|
1186
|
+
: {}),
|
|
804
1187
|
};
|
|
805
1188
|
|
|
1189
|
+
const inboxTopics = inboxTopicIds(manifest);
|
|
1190
|
+
|
|
806
1191
|
for (const id of [...rest, ...sites]) {
|
|
807
1192
|
const declaration = manifest[id];
|
|
808
1193
|
if (!declaration) continue;
|
|
1194
|
+
if (PROVISIONED_ELSEWHERE.has(declaration.kind)) continue;
|
|
1195
|
+
// Its provider made it: referenced once its inbox is, below.
|
|
1196
|
+
if (inboxTopics.has(id)) continue;
|
|
809
1197
|
|
|
810
1198
|
const component = provisionerFor(declaration.kind)(
|
|
811
1199
|
stack,
|
|
812
1200
|
declaration,
|
|
813
|
-
overrides[id] ?? {},
|
|
1201
|
+
(overrides as Record<string, Record<string, unknown>>)[id] ?? {},
|
|
814
1202
|
context,
|
|
815
1203
|
);
|
|
816
1204
|
|
|
@@ -829,6 +1217,16 @@ export function fromManifest(
|
|
|
829
1217
|
provisioned[id] = component;
|
|
830
1218
|
}
|
|
831
1219
|
|
|
1220
|
+
referenceInboxTopics(manifest, provisioned);
|
|
1221
|
+
|
|
1222
|
+
// The functions and crons, each queue's one consumer and each surface's
|
|
1223
|
+
// endpoints, once everything they link to exists.
|
|
1224
|
+
provisionCompute(stack, manifest, provisioned);
|
|
1225
|
+
subscribeInboxes(manifest, provisioned);
|
|
1226
|
+
provisionProcessors(stack, manifest, provisioned);
|
|
1227
|
+
subscribeConsumers(manifest, provisioned);
|
|
1228
|
+
mountRoutes(manifest, provisioned);
|
|
1229
|
+
|
|
832
1230
|
for (const line of describeRoutes(manifest)) console.log(line);
|
|
833
1231
|
|
|
834
1232
|
// Now everything exists, so every surface's callers can be resolved — the
|
|
@@ -836,7 +1234,7 @@ export function fromManifest(
|
|
|
836
1234
|
for (const [id, deferred] of callers) {
|
|
837
1235
|
// Resolved with the promise itself: the addresses it needs are Pulumi
|
|
838
1236
|
// outputs, so the answer arrives when they do.
|
|
839
|
-
deferred.resolve(callersOf(id, manifest, provisioned));
|
|
1237
|
+
deferred.resolve(callersOf(id, manifest, provisioned, $app.name));
|
|
840
1238
|
}
|
|
841
1239
|
|
|
842
1240
|
// Last, and only now: the roles exist as passwords and secrets from the
|
|
@@ -851,6 +1249,30 @@ export function fromManifest(
|
|
|
851
1249
|
return provisioned;
|
|
852
1250
|
}
|
|
853
1251
|
|
|
1252
|
+
/**
|
|
1253
|
+
* A secret in its declared format — the alphabet `drawSecret` draws from on
|
|
1254
|
+
* every other stage, with one symbol guaranteed as it guarantees one.
|
|
1255
|
+
*
|
|
1256
|
+
* The format is fixed at the first deploy, as a server stage's is: changing it
|
|
1257
|
+
* would replace the password, and every later deploy is meant to read back the
|
|
1258
|
+
* one it had.
|
|
1259
|
+
*/
|
|
1260
|
+
function generatedSecret(id: string, format: SecretFormat) {
|
|
1261
|
+
const symbols = [...new Set(format.symbols ?? SECRET_SYMBOLS)].join('');
|
|
1262
|
+
|
|
1263
|
+
return new RandomPassword(
|
|
1264
|
+
`${id}Generated`,
|
|
1265
|
+
{
|
|
1266
|
+
length: format.length ?? SECRET_LENGTH,
|
|
1267
|
+
special: symbols.length > 0,
|
|
1268
|
+
...(symbols ? { overrideSpecial: symbols, minSpecial: 1 } : {}),
|
|
1269
|
+
},
|
|
1270
|
+
{
|
|
1271
|
+
ignoreChanges: ['length', 'special', 'overrideSpecial', 'minSpecial'],
|
|
1272
|
+
},
|
|
1273
|
+
).result;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
854
1276
|
/** What one function receives from its edges. */
|
|
855
1277
|
export interface ResolvedEdges {
|
|
856
1278
|
/** The components it may reach. SST turns these into IAM *and* injects their
|