@geekmidas/cloud 10.0.0-alpha.15 → 10.0.0-alpha.151
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
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ConstructManifest,
|
|
3
|
+
PUBLIC_PREFIX,
|
|
4
|
+
providedKeyFor,
|
|
5
|
+
trustsOrigin,
|
|
6
|
+
} from '@geekmidas/manifest';
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
8
|
+
import { App } from '../App';
|
|
9
|
+
import { WildcardSiteNeedsDomain } from '../errors';
|
|
10
|
+
import { fromManifest } from '../fromManifest';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A site on every subdomain, on AWS: one distribution under `*.<domain>`, a
|
|
14
|
+
* wildcard certificate validated through the stack's zone, one alias record,
|
|
15
|
+
* and nothing per tenant.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const keys = (id: string, kind: string, roles: string[]) =>
|
|
19
|
+
roles.map((role) => providedKeyFor(id, kind as never, role));
|
|
20
|
+
|
|
21
|
+
function manifest(): ConstructManifest {
|
|
22
|
+
return {
|
|
23
|
+
Api: {
|
|
24
|
+
kind: 'rest-api',
|
|
25
|
+
id: 'Api',
|
|
26
|
+
path: 'apps/api',
|
|
27
|
+
endpoints: [
|
|
28
|
+
{
|
|
29
|
+
id: 'TenantHosts',
|
|
30
|
+
handler: 'hosts.handler',
|
|
31
|
+
method: 'GET',
|
|
32
|
+
path: '/tenants/hosts',
|
|
33
|
+
dependencies: [],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
provides: keys('Api', 'rest-api', [
|
|
37
|
+
'url',
|
|
38
|
+
'trustedOrigins',
|
|
39
|
+
'cookieDomain',
|
|
40
|
+
]),
|
|
41
|
+
},
|
|
42
|
+
Admin: {
|
|
43
|
+
kind: 'site',
|
|
44
|
+
id: 'Admin',
|
|
45
|
+
variant: 'static',
|
|
46
|
+
subdomain: 'admin',
|
|
47
|
+
app: { path: 'apps/admin' },
|
|
48
|
+
dependencies: [{ target: 'Api', kind: 'rest-api' }],
|
|
49
|
+
provides: keys('Admin', 'site', ['url']),
|
|
50
|
+
},
|
|
51
|
+
App: {
|
|
52
|
+
kind: 'site',
|
|
53
|
+
id: 'App',
|
|
54
|
+
variant: 'next',
|
|
55
|
+
subdomain: '*',
|
|
56
|
+
hosts: { surface: 'Api', path: '/tenants/hosts' },
|
|
57
|
+
app: { path: 'apps/web' },
|
|
58
|
+
dependencies: [{ target: 'Api', kind: 'rest-api' }],
|
|
59
|
+
provides: keys('App', 'site', ['url', 'domain', 'protectedSubdomains']),
|
|
60
|
+
},
|
|
61
|
+
} as unknown as ConstructManifest;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const stackOn = (domain: string, hostedZoneId: string = 'Z123') =>
|
|
65
|
+
new App({
|
|
66
|
+
name: 'shop',
|
|
67
|
+
stage: 'production',
|
|
68
|
+
domain,
|
|
69
|
+
hostedZoneId,
|
|
70
|
+
region: 'eu-west-1',
|
|
71
|
+
}).stack('shop');
|
|
72
|
+
|
|
73
|
+
const argsOf = (component: unknown) =>
|
|
74
|
+
(component as { args: Record<string, any> }).args;
|
|
75
|
+
|
|
76
|
+
const settle = async (value: unknown): Promise<unknown> =>
|
|
77
|
+
typeof value === 'object' && value && 'apply' in value
|
|
78
|
+
? (value as { apply: (fn: (v: unknown) => unknown) => unknown }).apply(
|
|
79
|
+
(v) => v,
|
|
80
|
+
)
|
|
81
|
+
: value;
|
|
82
|
+
|
|
83
|
+
const PROTECTED = ['admin', 'api', 'www'];
|
|
84
|
+
|
|
85
|
+
describe('a wildcard site on AWS', () => {
|
|
86
|
+
afterEach(() => vi.restoreAllMocks());
|
|
87
|
+
|
|
88
|
+
it('is one distribution under *.<domain>, its certificate and alias through the stack’s zone', () => {
|
|
89
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
90
|
+
|
|
91
|
+
const provisioned = fromManifest(
|
|
92
|
+
stackOn('example.com'),
|
|
93
|
+
manifest(),
|
|
94
|
+
{},
|
|
95
|
+
{
|
|
96
|
+
protectedSubdomains: PROTECTED,
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(argsOf(provisioned.App).domain).toEqual({
|
|
101
|
+
name: '*.example.com',
|
|
102
|
+
dns: { dns: { zone: 'Z123' } },
|
|
103
|
+
});
|
|
104
|
+
// A named site keeps its own: nothing about it changed.
|
|
105
|
+
expect(argsOf(provisioned.Admin).domain).toBeUndefined();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('looks its zone up by name when the stack has none resolved', () => {
|
|
109
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
110
|
+
|
|
111
|
+
const provisioned = fromManifest(
|
|
112
|
+
stackOn('example.com', ''),
|
|
113
|
+
manifest(),
|
|
114
|
+
{},
|
|
115
|
+
{ protectedSubdomains: PROTECTED },
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
expect(argsOf(provisioned.App).domain.dns).toEqual({ dns: {} });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('provides its address, domain and protected labels, and inlines the last two', () => {
|
|
122
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
123
|
+
|
|
124
|
+
const provisioned = fromManifest(
|
|
125
|
+
stackOn('example.com'),
|
|
126
|
+
manifest(),
|
|
127
|
+
{},
|
|
128
|
+
{
|
|
129
|
+
protectedSubdomains: PROTECTED,
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(provisioned.App!.provides()).toEqual({
|
|
134
|
+
url: 'https://*.example.com',
|
|
135
|
+
domain: 'example.com',
|
|
136
|
+
protectedSubdomains: 'admin,api,www',
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const env = argsOf(provisioned.App).environment as Record<string, unknown>;
|
|
140
|
+
const prefix = PUBLIC_PREFIX.next;
|
|
141
|
+
expect(env[`${prefix}${providedKeyFor('App', 'site', 'domain')}`]).toBe(
|
|
142
|
+
'example.com',
|
|
143
|
+
);
|
|
144
|
+
expect(
|
|
145
|
+
env[`${prefix}${providedKeyFor('App', 'site', 'protectedSubdomains')}`],
|
|
146
|
+
).toBe('admin,api,www');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('has the surfaces it calls trust every tenant’s origin, one label deep', async () => {
|
|
150
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
151
|
+
|
|
152
|
+
const provisioned = fromManifest(
|
|
153
|
+
stackOn('example.com'),
|
|
154
|
+
manifest(),
|
|
155
|
+
{},
|
|
156
|
+
{
|
|
157
|
+
protectedSubdomains: PROTECTED,
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
const trusted = String(
|
|
161
|
+
await settle(provisioned.Api!.provides().trustedOrigins),
|
|
162
|
+
).split(',');
|
|
163
|
+
|
|
164
|
+
expect(trusted).toContain('https://*.example.com');
|
|
165
|
+
expect(trustsOrigin('https://acme.example.com', trusted)).toBe(true);
|
|
166
|
+
expect(trustsOrigin('https://a.b.example.com', trusted)).toBe(false);
|
|
167
|
+
expect(trustsOrigin('https://example.com', trusted)).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('lets an override win, as every other prop does', () => {
|
|
171
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
172
|
+
|
|
173
|
+
const domain = { name: '*.tenants.example.com' };
|
|
174
|
+
const provisioned = fromManifest(
|
|
175
|
+
stackOn('example.com'),
|
|
176
|
+
manifest(),
|
|
177
|
+
{ App: { domain } } as never,
|
|
178
|
+
{ protectedSubdomains: PROTECTED },
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
expect(argsOf(provisioned.App).domain).toBe(domain);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('is refused on a stack with no domain, naming the site', () => {
|
|
185
|
+
vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
186
|
+
|
|
187
|
+
expect.assertions(2);
|
|
188
|
+
try {
|
|
189
|
+
fromManifest(stackOn(''), manifest(), {}, {});
|
|
190
|
+
} catch (error) {
|
|
191
|
+
expect(error).toBeInstanceOf(WildcardSiteNeedsDomain);
|
|
192
|
+
expect((error as WildcardSiteNeedsDomain).id).toBe('App');
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Type-level tests for `fromManifest`'s overrides, inferred from the manifest.
|
|
2
|
+
// Checked by `ts:check:sst`; each `@ts-expect-error` self-validates: if the
|
|
3
|
+
// inference regresses, the now-unused directive becomes a TS error the gate
|
|
4
|
+
// catches.
|
|
5
|
+
|
|
6
|
+
import type { ConstructManifest } from '@geekmidas/manifest';
|
|
7
|
+
import { App } from '../App';
|
|
8
|
+
import { fromManifest } from '../fromManifest';
|
|
9
|
+
|
|
10
|
+
const stack = new App({
|
|
11
|
+
name: 'a',
|
|
12
|
+
stage: 'dev',
|
|
13
|
+
domain: 'example.com',
|
|
14
|
+
hostedZoneId: 'Z',
|
|
15
|
+
region: 'us-east-1',
|
|
16
|
+
}).stack('shop');
|
|
17
|
+
|
|
18
|
+
declare const vpc: sst.aws.Vpc;
|
|
19
|
+
|
|
20
|
+
// The shape `gkm build` emits: `as const satisfies ConstructManifest`.
|
|
21
|
+
const constructs = {
|
|
22
|
+
Orders: {
|
|
23
|
+
kind: 'database',
|
|
24
|
+
id: 'Orders',
|
|
25
|
+
engine: 'postgres',
|
|
26
|
+
provides: ['ORDERS_URL'],
|
|
27
|
+
},
|
|
28
|
+
Billing: {
|
|
29
|
+
kind: 'database-schema',
|
|
30
|
+
id: 'Billing',
|
|
31
|
+
of: 'Orders',
|
|
32
|
+
schema: 'billing',
|
|
33
|
+
provides: ['BILLING_URL'],
|
|
34
|
+
},
|
|
35
|
+
Sessions: { kind: 'cache', id: 'Sessions', provides: ['SESSIONS_URL'] },
|
|
36
|
+
Rates: {
|
|
37
|
+
kind: 'cache',
|
|
38
|
+
id: 'Rates',
|
|
39
|
+
of: 'Orders',
|
|
40
|
+
provides: ['RATES_URL'],
|
|
41
|
+
},
|
|
42
|
+
Mail: { kind: 'email', id: 'Mail', provides: ['MAIL_URL'] },
|
|
43
|
+
Uploads: { kind: 'objects', id: 'Uploads', provides: ['UPLOADS_URL'] },
|
|
44
|
+
Api: {
|
|
45
|
+
kind: 'rest-api',
|
|
46
|
+
id: 'Api',
|
|
47
|
+
path: 'apps/api',
|
|
48
|
+
endpoints: [],
|
|
49
|
+
provides: ['API_URL'],
|
|
50
|
+
},
|
|
51
|
+
} as const satisfies ConstructManifest;
|
|
52
|
+
|
|
53
|
+
// Valid: the two the synth cannot guess, plus an optional bucket prop.
|
|
54
|
+
export const ok = fromManifest(stack, constructs, {
|
|
55
|
+
Orders: { vpc },
|
|
56
|
+
Mail: { from: 'no-reply@example.com' },
|
|
57
|
+
Uploads: { cors: false },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// A database without its VPC.
|
|
61
|
+
// @ts-expect-error — `Orders` is a database: `vpc` is required.
|
|
62
|
+
fromManifest(stack, constructs, { Mail: { from: 'a@example.com' } });
|
|
63
|
+
|
|
64
|
+
// Mail without a sender.
|
|
65
|
+
fromManifest(stack, constructs, {
|
|
66
|
+
Orders: { vpc },
|
|
67
|
+
// @ts-expect-error — every provider rejects an unverified sender.
|
|
68
|
+
Mail: {},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// A misspelt id.
|
|
72
|
+
fromManifest(stack, constructs, {
|
|
73
|
+
Orders: { vpc },
|
|
74
|
+
Mail: { from: 'a@example.com' },
|
|
75
|
+
// @ts-expect-error — not a construct in this manifest.
|
|
76
|
+
Ordrs: { vpc },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// A prop the kind's component never reads.
|
|
80
|
+
fromManifest(stack, constructs, {
|
|
81
|
+
Orders: { vpc },
|
|
82
|
+
Mail: { from: 'a@example.com' },
|
|
83
|
+
// @ts-expect-error — a surface takes API Gateway args, not bucket ones.
|
|
84
|
+
Api: { versioning: true },
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// What the declaration already decides is not overridable.
|
|
88
|
+
fromManifest(stack, constructs, {
|
|
89
|
+
// @ts-expect-error — the schema is the declaration's, not the deploy's.
|
|
90
|
+
Orders: { vpc, schema: 'other' },
|
|
91
|
+
Mail: { from: 'a@example.com' },
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// A kind with nothing to override takes no key.
|
|
95
|
+
fromManifest(stack, constructs, {
|
|
96
|
+
Orders: { vpc },
|
|
97
|
+
Mail: { from: 'a@example.com' },
|
|
98
|
+
// @ts-expect-error — a schema derives from its database.
|
|
99
|
+
Billing: {},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Nor does a cache in a database: same address, one more table.
|
|
103
|
+
fromManifest(stack, constructs, {
|
|
104
|
+
Orders: { vpc },
|
|
105
|
+
Mail: { from: 'a@example.com' },
|
|
106
|
+
// @ts-expect-error — `Rates` lives in `Orders`.
|
|
107
|
+
Rates: { region: 'eu-west-1' },
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// The backend decides too: ElastiCache needs a VPC to live in…
|
|
111
|
+
fromManifest(
|
|
112
|
+
stack,
|
|
113
|
+
constructs,
|
|
114
|
+
// @ts-expect-error — `Sessions` on ElastiCache needs `vpc`.
|
|
115
|
+
{ Orders: { vpc }, Mail: { from: 'a@example.com' } },
|
|
116
|
+
{ cache: 'elasticache' },
|
|
117
|
+
);
|
|
118
|
+
export const elasticache = fromManifest(
|
|
119
|
+
stack,
|
|
120
|
+
constructs,
|
|
121
|
+
{
|
|
122
|
+
Orders: { vpc },
|
|
123
|
+
Sessions: { vpc: { subnets: [], securityGroups: [] } },
|
|
124
|
+
Mail: { from: 'a@example.com' },
|
|
125
|
+
},
|
|
126
|
+
{ cache: 'elasticache' },
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// …and mail that is not SES is an account somebody created, with its URL.
|
|
130
|
+
fromManifest(
|
|
131
|
+
stack,
|
|
132
|
+
constructs,
|
|
133
|
+
{
|
|
134
|
+
Orders: { vpc },
|
|
135
|
+
// @ts-expect-error — Resend's URL is not something a deploy can mint.
|
|
136
|
+
Mail: { from: 'a@example.com' },
|
|
137
|
+
},
|
|
138
|
+
{ email: 'resend' },
|
|
139
|
+
);
|
|
140
|
+
export const resend = fromManifest(
|
|
141
|
+
stack,
|
|
142
|
+
constructs,
|
|
143
|
+
{
|
|
144
|
+
Orders: { vpc },
|
|
145
|
+
Mail: { from: 'a@example.com', url: 'smtp://resend' },
|
|
146
|
+
},
|
|
147
|
+
{ email: 'resend' },
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// A manifest whose ids are not known statically keeps the untyped record.
|
|
151
|
+
declare const untyped: ConstructManifest;
|
|
152
|
+
export const loose = fromManifest(stack, untyped, { Anything: { at: 'all' } });
|
package/src/sst/aws/Api.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { EnvValidationError } from '@geekmidas/envkit/sst';
|
|
3
|
-
import {
|
|
4
|
-
flattenManifestField,
|
|
5
|
-
type ManifestField,
|
|
6
|
-
type RouteInfo,
|
|
7
|
-
} from '@geekmidas/manifest';
|
|
8
3
|
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
9
4
|
import { LinkedEnvironment } from '../LinkedEnvironment';
|
|
10
5
|
import type { StackType } from '../Stack';
|
|
@@ -157,44 +152,6 @@ export class Api<
|
|
|
157
152
|
}
|
|
158
153
|
}
|
|
159
154
|
|
|
160
|
-
/**
|
|
161
|
-
* Build an `Api` from a `gkm build` manifest's `routes` field (flat or
|
|
162
|
-
* partitioned): each `RouteInfo` becomes a route (env vars, authorizer,
|
|
163
|
-
* timeout/memory mapped). Supply `authorizers` (JWT/Lambda settings),
|
|
164
|
-
* `links`, and any native args via `props`.
|
|
165
|
-
*
|
|
166
|
-
* ```ts
|
|
167
|
-
* import { manifest } from './.gkm/manifest/aws';
|
|
168
|
-
* Api.fromManifest(stack, 'Api', manifest.routes, { links: [db] });
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
static fromManifest<
|
|
172
|
-
TAuthorizers extends Record<string, unknown> = {},
|
|
173
|
-
TStage extends string = string,
|
|
174
|
-
TDomain extends string = string,
|
|
175
|
-
>(
|
|
176
|
-
stack: StackType<TStage, TDomain>,
|
|
177
|
-
id: string,
|
|
178
|
-
routes: ManifestField<RouteInfo>,
|
|
179
|
-
props: Omit<ApiProps<TAuthorizers>, 'routes'> = {},
|
|
180
|
-
): Api<TAuthorizers, TStage, TDomain> {
|
|
181
|
-
const routeTable = flattenManifestField(routes).map(
|
|
182
|
-
(route): Route<AuthorizerName<TAuthorizers>> => ({
|
|
183
|
-
method: route.method as Route['method'],
|
|
184
|
-
path: route.path,
|
|
185
|
-
handler: route.handler,
|
|
186
|
-
environment: route.environment,
|
|
187
|
-
authorizer: route.authorizer as AuthorizerName<TAuthorizers>,
|
|
188
|
-
timeout: route.timeout ? `${route.timeout} seconds` : undefined,
|
|
189
|
-
memory: route.memorySize ? `${route.memorySize} MB` : undefined,
|
|
190
|
-
}),
|
|
191
|
-
);
|
|
192
|
-
return new Api(stack, id, {
|
|
193
|
-
...props,
|
|
194
|
-
routes: routeTable,
|
|
195
|
-
} as ApiProps<TAuthorizers>);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
155
|
/** Resolves a route's `authorizer` name to the SST `auth` option. */
|
|
199
156
|
private static buildRouteAuth(
|
|
200
157
|
route: Route<string>,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
2
|
+
import type { StackType } from '../Stack';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* `Credential` — a third-party credential SST holds, and the infra half of the
|
|
@@ -8,21 +9,53 @@ import { Secret, type SecretProps } from './Secret';
|
|
|
8
9
|
* the lifecycle rather than the mechanism: a secret is generated and rotated by
|
|
9
10
|
* the platform, while a credential is issued by someone else and has a shape
|
|
10
11
|
* the construct validates on the way in. Both are values you set out of band
|
|
11
|
-
* with `sst secret set
|
|
12
|
+
* with `sst secret set`, under the construct's id.
|
|
12
13
|
*
|
|
13
|
-
* The role is `
|
|
14
|
+
* The role is `credentials` rather than `value`, and that is not cosmetic. The
|
|
14
15
|
* role *is* the contract — `providedKeyFor` turns it into the key the app
|
|
15
16
|
* declared — so a credential providing `value` would supply `STRIPE_VALUE`
|
|
16
|
-
* against a declared `
|
|
17
|
-
* stack at synth.
|
|
17
|
+
* against a declared `STRIPE_CREDENTIALS`, and `assertProvides` would reject
|
|
18
|
+
* the stack at synth.
|
|
19
|
+
*
|
|
20
|
+
* It holds an `sst.Secret` rather than being one, and reports its own type,
|
|
21
|
+
* for the same reason one level down: the type decides the keys a link
|
|
22
|
+
* resolves to. As an SST secret it resolved to `STRIPE`, so a function that
|
|
23
|
+
* declared `STRIPE_CREDENTIALS` was linked to nothing.
|
|
18
24
|
*/
|
|
19
25
|
export class Credential<
|
|
20
26
|
TStage extends string = string,
|
|
21
27
|
TDomain extends string = string,
|
|
22
|
-
>
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
> implements GkmLinkable
|
|
29
|
+
{
|
|
30
|
+
readonly _id: string;
|
|
31
|
+
|
|
32
|
+
private readonly secret: sst.Secret;
|
|
33
|
+
|
|
34
|
+
get _type() {
|
|
35
|
+
return ResourceType.Credential;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
_stack: StackType<TStage, TDomain>,
|
|
40
|
+
name: string,
|
|
41
|
+
props: CredentialProps = {},
|
|
42
|
+
) {
|
|
43
|
+
this._id = name;
|
|
44
|
+
// The id itself, as it always was: a value already set with
|
|
45
|
+
// `sst secret set Stripe …` is still the one read.
|
|
46
|
+
this.secret = new sst.Secret(name, props.placeholder);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
provides(): Record<string, $util.Input<string>> {
|
|
50
|
+
return { credentials: this.secret.value };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getSSTLink() {
|
|
54
|
+
return { properties: { ...this.provides() } };
|
|
25
55
|
}
|
|
26
56
|
}
|
|
27
57
|
|
|
28
|
-
export interface CredentialProps
|
|
58
|
+
export interface CredentialProps {
|
|
59
|
+
/** A value until one is set, for a preview stage. */
|
|
60
|
+
placeholder?: string;
|
|
61
|
+
}
|
package/src/sst/aws/Cron.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type CronInfo,
|
|
3
|
-
flattenManifestField,
|
|
4
|
-
type ManifestField,
|
|
5
|
-
} from '@geekmidas/manifest';
|
|
6
|
-
import type { GkmLinkable } from '../Linkable';
|
|
7
1
|
import type { StackType } from '../Stack';
|
|
8
|
-
import { Function } from './Function';
|
|
2
|
+
import type { Function } from './Function';
|
|
9
3
|
|
|
10
4
|
export type CronExpressionValue = number | '*' | '?' | `${number}/${number}`;
|
|
11
5
|
export type CronExpressionDay =
|
|
@@ -52,6 +46,9 @@ export class Cron<
|
|
|
52
46
|
TStage extends string = string,
|
|
53
47
|
TDomain extends string = string,
|
|
54
48
|
> extends sst.aws.CronV2 {
|
|
49
|
+
/** The function it invokes. */
|
|
50
|
+
readonly processor: CronProps['processor'];
|
|
51
|
+
|
|
55
52
|
constructor(
|
|
56
53
|
_stack: StackType<TStage, TDomain>,
|
|
57
54
|
name: string,
|
|
@@ -63,44 +60,7 @@ export class Cron<
|
|
|
63
60
|
schedule,
|
|
64
61
|
function: processor.arn,
|
|
65
62
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Build one `Cron` per entry in a `gkm build` manifest's `crons` field (flat
|
|
70
|
-
* or partitioned). Each cron's handler becomes a validated `Function` (the
|
|
71
|
-
* cron's target), so pass `links` for that function's env validation;
|
|
72
|
-
* remaining `props` are CronV2 args.
|
|
73
|
-
*
|
|
74
|
-
* ```ts
|
|
75
|
-
* Cron.fromManifest(stack, manifest.crons, { links: [db] });
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
static fromManifest<
|
|
79
|
-
TStage extends string = string,
|
|
80
|
-
TDomain extends string = string,
|
|
81
|
-
>(
|
|
82
|
-
stack: StackType<TStage, TDomain>,
|
|
83
|
-
crons: ManifestField<CronInfo>,
|
|
84
|
-
props: Omit<CronProps, 'processor' | 'schedule'> & {
|
|
85
|
-
links?: GkmLinkable[];
|
|
86
|
-
} = {},
|
|
87
|
-
): Cron<TStage, TDomain>[] {
|
|
88
|
-
const { links, ...cronArgs } = props;
|
|
89
|
-
return flattenManifestField(crons).map((cron) => {
|
|
90
|
-
const processor = new Function(stack, `${cron.name}Function`, {
|
|
91
|
-
name: stack.logicalPrefixedName(cron.name),
|
|
92
|
-
handler: cron.handler,
|
|
93
|
-
envVars: cron.environment,
|
|
94
|
-
links,
|
|
95
|
-
timeout: cron.timeout ? `${cron.timeout} seconds` : undefined,
|
|
96
|
-
memory: cron.memorySize ? `${cron.memorySize} MB` : undefined,
|
|
97
|
-
});
|
|
98
|
-
return new Cron(stack, cron.name, {
|
|
99
|
-
...cronArgs,
|
|
100
|
-
processor,
|
|
101
|
-
schedule: cron.schedule as CronSchedule,
|
|
102
|
-
});
|
|
103
|
-
});
|
|
63
|
+
this.processor = processor;
|
|
104
64
|
}
|
|
105
65
|
}
|
|
106
66
|
|
|
@@ -65,6 +65,10 @@ export class DatabaseBootstrap {
|
|
|
65
65
|
runtime: string;
|
|
66
66
|
owner: string;
|
|
67
67
|
reader?: string;
|
|
68
|
+
/** The database's own roles, whose owner may create in it. */
|
|
69
|
+
ownsDatabase?: boolean;
|
|
70
|
+
/** Extensions the database declared, created as the master. */
|
|
71
|
+
extensions?: readonly string[];
|
|
68
72
|
}): { runtime: $util.Output<string>; reader?: $util.Output<string> } {
|
|
69
73
|
const runtime = this.password(tenant.runtime);
|
|
70
74
|
const reader = tenant.reader ? this.password(tenant.reader) : undefined;
|
|
@@ -75,6 +79,8 @@ export class DatabaseBootstrap {
|
|
|
75
79
|
runtime: tenant.runtime,
|
|
76
80
|
owner: tenant.owner,
|
|
77
81
|
...(tenant.reader ? { reader: tenant.reader } : {}),
|
|
82
|
+
...(tenant.ownsDatabase ? { ownsDatabase: true } : {}),
|
|
83
|
+
...(tenant.extensions?.length ? { extensions: tenant.extensions } : {}),
|
|
78
84
|
// Filled in by `bootstrapEvent` once the outputs resolve. The function
|
|
79
85
|
// reads them straight out of its input and never fetches a secret, so
|
|
80
86
|
// it needs no IAM to read one.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { kmsUrl } from '@geekmidas/manifest';
|
|
2
|
+
import { kms } from '@pulumi/aws';
|
|
3
|
+
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
4
|
+
import type { StackType } from '../Stack';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `Encryption` — two KMS keys, and the infra half of the `encryption` kind.
|
|
8
|
+
*
|
|
9
|
+
* One key encrypts, by envelope: each value is sealed with a data key KMS
|
|
10
|
+
* mints and wraps, so the key itself never leaves KMS. It rotates yearly, and
|
|
11
|
+
* KMS keeps every version it rotated through, so nothing written is ever
|
|
12
|
+
* stranded. The other is an HMAC key for the blind index, which does not
|
|
13
|
+
* rotate — a rotated index key would make every stored index miss.
|
|
14
|
+
*
|
|
15
|
+
* Both are reached through one `kms://` URL, and a function linked to this is
|
|
16
|
+
* granted exactly the calls the cipher makes on exactly these two keys. Not
|
|
17
|
+
* `kms:*`, and not any other key in the account.
|
|
18
|
+
*
|
|
19
|
+
* It holds the keys rather than being one, and reports its own type, so a link
|
|
20
|
+
* resolves to `<ID>_URL` — the key the construct declared.
|
|
21
|
+
*/
|
|
22
|
+
export class Encryption<
|
|
23
|
+
TStage extends string = string,
|
|
24
|
+
TDomain extends string = string,
|
|
25
|
+
> implements GkmLinkable
|
|
26
|
+
{
|
|
27
|
+
readonly _id: string;
|
|
28
|
+
|
|
29
|
+
readonly key: kms.Key;
|
|
30
|
+
readonly indexKey: kms.Key;
|
|
31
|
+
private readonly region: string;
|
|
32
|
+
|
|
33
|
+
get _type() {
|
|
34
|
+
return ResourceType.Encryption;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
_stack: StackType<TStage, TDomain>,
|
|
39
|
+
name: string,
|
|
40
|
+
props: EncryptionProps,
|
|
41
|
+
) {
|
|
42
|
+
this._id = name;
|
|
43
|
+
this.region = props.region;
|
|
44
|
+
|
|
45
|
+
this.key = new kms.Key(`${name}Key`, {
|
|
46
|
+
description: `${name}: encrypts what the application stores`,
|
|
47
|
+
enableKeyRotation: true,
|
|
48
|
+
deletionWindowInDays: props.deletionWindowInDays ?? 30,
|
|
49
|
+
});
|
|
50
|
+
this.indexKey = new kms.Key(`${name}IndexKey`, {
|
|
51
|
+
description: `${name}: the blind index's HMAC key`,
|
|
52
|
+
keyUsage: 'GENERATE_VERIFY_MAC',
|
|
53
|
+
customerMasterKeySpec: 'HMAC_256',
|
|
54
|
+
deletionWindowInDays: props.deletionWindowInDays ?? 30,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
provides(): Record<string, $util.Input<string>> {
|
|
59
|
+
return {
|
|
60
|
+
url: $util
|
|
61
|
+
.all([this.key.arn, this.indexKey.arn])
|
|
62
|
+
.apply(([key, index]) => kmsUrl({ region: this.region, key, index })),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getSSTLink() {
|
|
67
|
+
return {
|
|
68
|
+
properties: { ...this.provides() },
|
|
69
|
+
include: [
|
|
70
|
+
sst.aws.permission({
|
|
71
|
+
actions: ['kms:GenerateDataKey', 'kms:Decrypt'],
|
|
72
|
+
resources: [this.key.arn],
|
|
73
|
+
}),
|
|
74
|
+
sst.aws.permission({
|
|
75
|
+
actions: ['kms:GenerateMac'],
|
|
76
|
+
resources: [this.indexKey.arn],
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface EncryptionProps {
|
|
84
|
+
/** Where the keys live — the stage's region. */
|
|
85
|
+
region: string;
|
|
86
|
+
/**
|
|
87
|
+
* How long a deleted key can still be recovered, 7–30 days. A key's
|
|
88
|
+
* deletion is the deletion of everything it encrypted, so the longest.
|
|
89
|
+
*/
|
|
90
|
+
deletionWindowInDays?: number;
|
|
91
|
+
}
|