@ankhorage/infra 7.0.0 → 7.1.1
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/CHANGELOG.md +12 -0
- package/dist/cli/commands/destroy.d.ts.map +1 -1
- package/dist/cli/commands/destroy.js +1 -0
- package/dist/cli/commands/destroy.js.map +1 -1
- package/dist/cli/utils/prepareInfraCommandOperationAsync.d.ts.map +1 -1
- package/dist/cli/utils/prepareInfraCommandOperationAsync.js +4 -1
- package/dist/cli/utils/prepareInfraCommandOperationAsync.js.map +1 -1
- package/dist/features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.d.ts +11 -0
- package/dist/features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.d.ts.map +1 -0
- package/dist/features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.js +125 -0
- package/dist/features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.js.map +1 -0
- package/dist/features/environment-lifecycle/adapters/outbound/removeStoredInfraCredentialsAsync.d.ts +4 -0
- package/dist/features/environment-lifecycle/adapters/outbound/removeStoredInfraCredentialsAsync.d.ts.map +1 -0
- package/dist/features/environment-lifecycle/adapters/outbound/removeStoredInfraCredentialsAsync.js +9 -0
- package/dist/features/environment-lifecycle/adapters/outbound/removeStoredInfraCredentialsAsync.js.map +1 -0
- package/dist/features/environment-lifecycle/composition/createInfraCommandServices.d.ts.map +1 -1
- package/dist/features/environment-lifecycle/composition/createInfraCommandServices.js +9 -3
- package/dist/features/environment-lifecycle/composition/createInfraCommandServices.js.map +1 -1
- package/dist/features/environment-lifecycle/composition/createProjectInfraLifecycle.d.ts.map +1 -1
- package/dist/features/environment-lifecycle/composition/createProjectInfraLifecycle.js +28 -2
- package/dist/features/environment-lifecycle/composition/createProjectInfraLifecycle.js.map +1 -1
- package/dist/features/environment-lifecycle/utils/resolveInfraEnvironmentStateDirectory.d.ts +4 -0
- package/dist/features/environment-lifecycle/utils/resolveInfraEnvironmentStateDirectory.d.ts.map +1 -0
- package/dist/features/environment-lifecycle/utils/resolveInfraEnvironmentStateDirectory.js +11 -0
- package/dist/features/environment-lifecycle/utils/resolveInfraEnvironmentStateDirectory.js.map +1 -0
- package/dist/features/environment-lifecycle/utils/resolveInfraStatePath.d.ts +1 -1
- package/dist/features/environment-lifecycle/utils/resolveInfraStatePath.d.ts.map +1 -1
- package/dist/features/environment-lifecycle/utils/resolveInfraStatePath.js +2 -6
- package/dist/features/environment-lifecycle/utils/resolveInfraStatePath.js.map +1 -1
- package/dist/project/index.d.ts +1 -0
- package/dist/project/index.d.ts.map +1 -1
- package/dist/project/index.js +1 -0
- package/dist/project/index.js.map +1 -1
- package/dist/types/infraCli.d.ts +7 -1
- package/dist/types/infraCli.d.ts.map +1 -1
- package/dist/types/infraCli.js.map +1 -1
- package/package.json +4 -3
- package/src/cli/commands/destroy.ts +1 -0
- package/src/cli/utils/prepareInfraCommandOperationAsync.ts +4 -1
- package/src/features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.ts +182 -0
- package/src/features/environment-lifecycle/adapters/outbound/removeStoredInfraCredentialsAsync.ts +18 -0
- package/src/features/environment-lifecycle/composition/createInfraCommandServices.ts +9 -3
- package/src/features/environment-lifecycle/composition/createProjectInfraLifecycle.ts +33 -4
- package/src/features/environment-lifecycle/utils/resolveInfraEnvironmentStateDirectory.ts +16 -0
- package/src/features/environment-lifecycle/utils/resolveInfraStatePath.ts +3 -6
- package/src/hetznerK3sProduction.test.ts +29 -3
- package/src/hetznerK3sSupabaseR2.test.ts +33 -4
- package/src/localDockerComposeProviders.integration.test.ts +90 -51
- package/src/localK3sSupabase.test.ts +85 -27
- package/src/localMinikubeSupabase.integration.test.ts +175 -122
- package/src/localMinikubeSupabaseRecovery.integration.test.ts +37 -10
- package/src/project/index.test.ts +3 -1
- package/src/project/index.ts +1 -0
- package/src/projectCredentialLifecycle.test.ts +161 -0
- package/src/projectCredentialStore.test.ts +151 -0
- package/src/types/infraCli.ts +11 -1
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { promises as fs } from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
2
5
|
|
|
3
6
|
import type {
|
|
4
7
|
InfraLedger,
|
|
@@ -8,13 +11,8 @@ import type {
|
|
|
8
11
|
} from '@ankhorage/contracts/infra';
|
|
9
12
|
import { expect, test } from 'bun:test';
|
|
10
13
|
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import { downInfraEnvironmentAsync } from './features/environment-lifecycle/application/use-cases/downInfraEnvironmentAsync.js';
|
|
14
|
-
import { planInfraEnvironmentAsync } from './features/environment-lifecycle/application/use-cases/planInfraEnvironmentAsync.js';
|
|
15
|
-
import { statusInfraEnvironmentAsync } from './features/environment-lifecycle/application/use-cases/statusInfraEnvironmentAsync.js';
|
|
16
|
-
import { upInfraEnvironmentAsync } from './features/environment-lifecycle/application/use-cases/upInfraEnvironmentAsync.js';
|
|
17
|
-
import type { InfraOrchestrationDependencies } from './types/infraOrchestration.js';
|
|
14
|
+
import { createProjectInfraCredentialPort } from './features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.js';
|
|
15
|
+
import { createProjectInfraLifecycle } from './project/index.js';
|
|
18
16
|
|
|
19
17
|
const projectId = 'infra145-minikube-supabase';
|
|
20
18
|
const acceptanceNamespace = `${projectId}-local`;
|
|
@@ -23,15 +21,9 @@ const bucket = 'phase8-acceptance';
|
|
|
23
21
|
const staleOwnedConfigMap = 'phase8-stale-owned';
|
|
24
22
|
const unrelatedConfigMap = 'phase8-unrelated';
|
|
25
23
|
const staleOwnedResourceId = 'stale:phase8-owned';
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
jwtSecret,
|
|
30
|
-
anonKey: createJwt('anon'),
|
|
31
|
-
serviceRoleKey: createJwt('service_role'),
|
|
32
|
-
realtimeSecretKeyBase: 'r'.repeat(64),
|
|
33
|
-
realtimeDatabaseEncryptionKey: '0123456789abcdef',
|
|
34
|
-
pgMetaCryptoKey: 'phase8-meta-crypto-key-that-is-at-least-32-characters',
|
|
24
|
+
const bootstrapCredential = {
|
|
25
|
+
source: 'control-plane',
|
|
26
|
+
name: 'SUPABASE_BOOTSTRAP',
|
|
35
27
|
} as const;
|
|
36
28
|
const manifest = {
|
|
37
29
|
environments: {
|
|
@@ -56,52 +48,60 @@ const manifest = {
|
|
|
56
48
|
} as const satisfies InfraManifest;
|
|
57
49
|
|
|
58
50
|
test.skipIf(process.env.ANKH_INFRA_MINIKUBE_SUPABASE_E2E !== '1')(
|
|
59
|
-
'
|
|
51
|
+
'bootstraps fresh local Minikube and Supabase through the project lifecycle',
|
|
60
52
|
async () => {
|
|
61
|
-
const
|
|
53
|
+
const projectPath = await fs.mkdtemp(path.join(os.tmpdir(), 'ankh-infra170-minikube-'));
|
|
54
|
+
const isolatedProjectPath = await fs.mkdtemp(path.join(os.tmpdir(), 'ankh-infra170-isolated-'));
|
|
55
|
+
const request = createOperationRequest(projectPath);
|
|
62
56
|
let ledger: InfraLedger | undefined;
|
|
57
|
+
let destroyed = false;
|
|
63
58
|
|
|
64
59
|
try {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
);
|
|
60
|
+
const firstLifecycle = createProjectInfraLifecycle();
|
|
61
|
+
const initialPlan = requireSuccess(await firstLifecycle.planAsync(request));
|
|
68
62
|
expect(initialPlan.actions.some(({ operation }) => operation === 'create')).toBe(true);
|
|
69
63
|
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
assertSafePublicOutputs(
|
|
64
|
+
const firstUp = requireSuccess(await firstLifecycle.upAsync(request));
|
|
65
|
+
({ ledger } = firstUp);
|
|
66
|
+
const firstCredentials = await resolveBootstrapCredentialsAsync(projectPath);
|
|
67
|
+
const firstFingerprint = credentialFingerprint(firstCredentials);
|
|
68
|
+
assertSafePublicOutputs(firstUp.outputs, firstCredentials);
|
|
69
|
+
assertSecretFree(firstUp, firstCredentials);
|
|
70
|
+
await assertStoredStateSecretFreeAsync(projectPath, firstCredentials);
|
|
71
|
+
await assertCredentialFilePermissionsAsync(projectPath);
|
|
75
72
|
expect(
|
|
76
|
-
|
|
73
|
+
firstUp.outputs.some(
|
|
77
74
|
({ owner, name, value }) =>
|
|
78
75
|
owner.adapter === 'minikube' && name === 'localUrl' && value === baseUrl,
|
|
79
76
|
),
|
|
80
77
|
).toBe(true);
|
|
81
78
|
expect(
|
|
82
|
-
|
|
79
|
+
firstUp.outputs.some(
|
|
83
80
|
({ environmentVariable, value }) =>
|
|
84
81
|
environmentVariable === 'EXPO_PUBLIC_SUPABASE_URL' && value === baseUrl,
|
|
85
82
|
),
|
|
86
83
|
).toBe(true);
|
|
87
|
-
expect(
|
|
84
|
+
expect(firstUp.outputs.some(({ name, value }) => name === 'bucket' && value === bucket)).toBe(
|
|
88
85
|
true,
|
|
89
86
|
);
|
|
90
87
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
);
|
|
88
|
+
const secondLifecycle = createProjectInfraLifecycle();
|
|
89
|
+
const status = requireSuccess(await secondLifecycle.statusAsync(request));
|
|
94
90
|
expect(status.state).toBe('ready');
|
|
95
91
|
|
|
96
|
-
const convergedPlan = requireSuccess(
|
|
97
|
-
await planInfraEnvironmentAsync({ projectId, manifest, previous: ledger }, dependencies),
|
|
98
|
-
);
|
|
92
|
+
const convergedPlan = requireSuccess(await secondLifecycle.planAsync(request));
|
|
99
93
|
expect(convergedPlan.actions.every(({ operation }) => operation === 'noop')).toBe(true);
|
|
94
|
+
assertSecretFree(convergedPlan, firstCredentials);
|
|
100
95
|
|
|
101
|
-
await
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
const secondUp = requireSuccess(await secondLifecycle.upAsync(request));
|
|
97
|
+
({ ledger } = secondUp);
|
|
98
|
+
expect(credentialFingerprint(await resolveBootstrapCredentialsAsync(projectPath))).toBe(
|
|
99
|
+
firstFingerprint,
|
|
104
100
|
);
|
|
101
|
+
expect(resourceIdentities(secondUp.resources)).toEqual(resourceIdentities(firstUp.resources));
|
|
102
|
+
|
|
103
|
+
await createStaleOwnershipFixturesAsync();
|
|
104
|
+
const stalePlan = requireSuccess(await secondLifecycle.planAsync(request));
|
|
105
105
|
expect(
|
|
106
106
|
stalePlan.actions.some(
|
|
107
107
|
({ operation, owner }) =>
|
|
@@ -109,10 +109,8 @@ test.skipIf(process.env.ANKH_INFRA_MINIKUBE_SUPABASE_E2E !== '1')(
|
|
|
109
109
|
),
|
|
110
110
|
).toBe(true);
|
|
111
111
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
);
|
|
115
|
-
ledger = prunedLedger;
|
|
112
|
+
const pruned = requireSuccess(await secondLifecycle.upAsync(request));
|
|
113
|
+
({ ledger } = pruned);
|
|
116
114
|
expect(await configMapExistsAsync(staleOwnedConfigMap)).toBe(false);
|
|
117
115
|
expect(await configMapExistsAsync(unrelatedConfigMap)).toBe(true);
|
|
118
116
|
await runKubectlAsync([
|
|
@@ -124,35 +122,53 @@ test.skipIf(process.env.ANKH_INFRA_MINIKUBE_SUPABASE_E2E !== '1')(
|
|
|
124
122
|
'--ignore-not-found=true',
|
|
125
123
|
]);
|
|
126
124
|
|
|
127
|
-
const
|
|
128
|
-
|
|
125
|
+
const down = requireSuccess(await secondLifecycle.downAsync(request));
|
|
126
|
+
({ ledger } = down);
|
|
127
|
+
expect(credentialFingerprint(await resolveBootstrapCredentialsAsync(projectPath))).toBe(
|
|
128
|
+
firstFingerprint,
|
|
129
129
|
);
|
|
130
|
-
ledger = downLedger;
|
|
131
130
|
|
|
132
|
-
const
|
|
133
|
-
|
|
131
|
+
const resumedLifecycle = createProjectInfraLifecycle();
|
|
132
|
+
const resumed = requireSuccess(await resumedLifecycle.upAsync(request));
|
|
133
|
+
({ ledger } = resumed);
|
|
134
|
+
expect(credentialFingerprint(await resolveBootstrapCredentialsAsync(projectPath))).toBe(
|
|
135
|
+
firstFingerprint,
|
|
134
136
|
);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
assertSafePublicOutputs(resumed.outputs, firstCredentials);
|
|
138
|
+
assertSecretFree(resumed, firstCredentials);
|
|
139
|
+
await assertStoredStateSecretFreeAsync(projectPath, firstCredentials);
|
|
137
140
|
|
|
138
141
|
const health = await fetch(`${baseUrl}/auth/v1/health`);
|
|
139
142
|
expect(health.ok).toBe(true);
|
|
140
143
|
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
const isolatedPort = createProjectInfraCredentialPort({
|
|
145
|
+
projectPath: isolatedProjectPath,
|
|
146
|
+
environment: 'local',
|
|
147
|
+
processEnvironment: {},
|
|
148
|
+
});
|
|
149
|
+
expect(await isolatedPort.persistAsync(bootstrapCredential, firstCredentials)).toMatchObject({
|
|
150
|
+
ok: true,
|
|
151
|
+
});
|
|
152
|
+
const isolatedFingerprint = credentialFingerprint(
|
|
153
|
+
requireSuccess(await isolatedPort.resolveAsync(bootstrapCredential)),
|
|
146
154
|
);
|
|
147
|
-
|
|
148
|
-
|
|
155
|
+
|
|
156
|
+
const destroyResult = requireSuccess(
|
|
157
|
+
await resumedLifecycle.destroyAsync(createDestroyRequest(projectPath, ledger)),
|
|
158
|
+
);
|
|
159
|
+
ledger = undefined;
|
|
160
|
+
destroyed = true;
|
|
161
|
+
expect(destroyResult.ledger).toBeNull();
|
|
162
|
+
expect(await findBootstrapCredentialsAsync(projectPath)).toBeNull();
|
|
163
|
+
expect(
|
|
164
|
+
credentialFingerprint(requireSuccess(await isolatedPort.resolveAsync(bootstrapCredential))),
|
|
165
|
+
).toBe(isolatedFingerprint);
|
|
149
166
|
} finally {
|
|
150
|
-
if (ledger !== undefined) {
|
|
151
|
-
await
|
|
152
|
-
createDestroyRequest(ledger, persistentIdentities(ledger)),
|
|
153
|
-
dependencies,
|
|
154
|
-
);
|
|
167
|
+
if (!destroyed && ledger !== undefined) {
|
|
168
|
+
await createProjectInfraLifecycle().destroyAsync(createDestroyRequest(projectPath, ledger));
|
|
155
169
|
}
|
|
170
|
+
await fs.rm(projectPath, { recursive: true, force: true });
|
|
171
|
+
await fs.rm(isolatedProjectPath, { recursive: true, force: true });
|
|
156
172
|
}
|
|
157
173
|
},
|
|
158
174
|
900_000,
|
|
@@ -227,43 +243,24 @@ async function runKubectlAsync(args: readonly string[]): Promise<string> {
|
|
|
227
243
|
return stdout.trim();
|
|
228
244
|
}
|
|
229
245
|
|
|
230
|
-
function
|
|
246
|
+
function createOperationRequest(projectPath: string) {
|
|
231
247
|
return {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
? success(credentials)
|
|
238
|
-
: failure(
|
|
239
|
-
'unexpected-control-plane-credential',
|
|
240
|
-
`Unexpected control-plane credential ${reference.name}.`,
|
|
241
|
-
),
|
|
242
|
-
),
|
|
243
|
-
},
|
|
244
|
-
secrets: {
|
|
245
|
-
resolveAsync: (reference) =>
|
|
246
|
-
Promise.resolve(
|
|
247
|
-
failure(
|
|
248
|
-
'unexpected-managed-secret',
|
|
249
|
-
`Unexpected managed secret ${reference.ref}/${reference.key}.`,
|
|
250
|
-
),
|
|
251
|
-
),
|
|
252
|
-
},
|
|
248
|
+
projectId,
|
|
249
|
+
projectPath,
|
|
250
|
+
manifest,
|
|
251
|
+
environment: 'local' as const,
|
|
252
|
+
executionEnvironment: {},
|
|
253
253
|
};
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
function createDestroyRequest(
|
|
257
|
-
previous: InfraLedger,
|
|
258
|
-
confirmedResources: readonly InfraResourceIdentity[],
|
|
259
|
-
) {
|
|
256
|
+
function createDestroyRequest(projectPath: string, previous: InfraLedger) {
|
|
260
257
|
return {
|
|
261
|
-
|
|
262
|
-
manifest,
|
|
263
|
-
environment: 'local' as const,
|
|
264
|
-
previous,
|
|
258
|
+
...createOperationRequest(projectPath),
|
|
265
259
|
confirmation: { projectId, environment: 'local' as const },
|
|
266
|
-
persistence: {
|
|
260
|
+
persistence: {
|
|
261
|
+
policy: 'delete' as const,
|
|
262
|
+
confirmedResources: persistentIdentities(previous),
|
|
263
|
+
},
|
|
267
264
|
};
|
|
268
265
|
}
|
|
269
266
|
|
|
@@ -271,48 +268,104 @@ function persistentIdentities(ledger: InfraLedger): readonly InfraResourceIdenti
|
|
|
271
268
|
return ledger.resources.filter(({ persistent }) => persistent).map(({ identity }) => identity);
|
|
272
269
|
}
|
|
273
270
|
|
|
274
|
-
function
|
|
271
|
+
function resourceIdentities(
|
|
272
|
+
resources: readonly { readonly identity: InfraResourceIdentity }[],
|
|
273
|
+
): readonly string[] {
|
|
274
|
+
return resources
|
|
275
|
+
.map(({ identity }) =>
|
|
276
|
+
[identity.projectId, identity.environment, identity.adapter, identity.resourceId].join(':'),
|
|
277
|
+
)
|
|
278
|
+
.sort();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function resolveBootstrapCredentialsAsync(
|
|
282
|
+
projectPath: string,
|
|
283
|
+
): Promise<Readonly<Record<string, string>>> {
|
|
284
|
+
const port = createProjectInfraCredentialPort({
|
|
285
|
+
projectPath,
|
|
286
|
+
environment: 'local',
|
|
287
|
+
processEnvironment: {},
|
|
288
|
+
});
|
|
289
|
+
return requireSuccess(await port.resolveAsync(bootstrapCredential));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function findBootstrapCredentialsAsync(
|
|
293
|
+
projectPath: string,
|
|
294
|
+
): Promise<Readonly<Record<string, string>> | null> {
|
|
295
|
+
const port = createProjectInfraCredentialPort({
|
|
296
|
+
projectPath,
|
|
297
|
+
environment: 'local',
|
|
298
|
+
processEnvironment: {},
|
|
299
|
+
});
|
|
300
|
+
return requireSuccess(await port.findAsync(bootstrapCredential));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function credentialFingerprint(values: Readonly<Record<string, string>>): string {
|
|
304
|
+
const canonical = Object.entries(values)
|
|
305
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
306
|
+
.map(([key, value]) => `${key}\u0000${value}`)
|
|
307
|
+
.join('\u0001');
|
|
308
|
+
return createHash('sha256').update(canonical).digest('hex');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function privilegedCredentialValues(values: Readonly<Record<string, string>>): readonly string[] {
|
|
312
|
+
const { anonKey: _anonKey, ...privileged } = values;
|
|
313
|
+
return Object.values(privileged);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function assertSafePublicOutputs(
|
|
317
|
+
outputs: InfraLedger['outputs'],
|
|
318
|
+
credentials: Readonly<Record<string, string>>,
|
|
319
|
+
): void {
|
|
275
320
|
const serialized = JSON.stringify(outputs);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
expect(serialized).not.toContain(credentials.pgMetaCryptoKey);
|
|
321
|
+
for (const value of privilegedCredentialValues(credentials)) {
|
|
322
|
+
expect(serialized).not.toContain(value);
|
|
323
|
+
}
|
|
324
|
+
const { anonKey } = credentials;
|
|
325
|
+
if (anonKey === undefined) throw new Error('Expected generated Supabase anonKey.');
|
|
282
326
|
expect(
|
|
283
327
|
outputs.some(
|
|
284
328
|
({ environmentVariable, value }) =>
|
|
285
|
-
environmentVariable === 'EXPO_PUBLIC_SUPABASE_ANON_KEY' && value ===
|
|
329
|
+
environmentVariable === 'EXPO_PUBLIC_SUPABASE_ANON_KEY' && value === anonKey,
|
|
286
330
|
),
|
|
287
331
|
).toBe(true);
|
|
288
332
|
}
|
|
289
333
|
|
|
290
|
-
function
|
|
291
|
-
const
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
iat: 1_700_000_000,
|
|
296
|
-
exp: 4_102_444_800,
|
|
297
|
-
});
|
|
298
|
-
const unsigned = `${encodedHeader}.${encodedPayload}`;
|
|
299
|
-
const signature = createHmac('sha256', jwtSecret).update(unsigned).digest('base64url');
|
|
300
|
-
return `${unsigned}.${signature}`;
|
|
334
|
+
function assertSecretFree(value: unknown, credentials: Readonly<Record<string, string>>): void {
|
|
335
|
+
const serialized = JSON.stringify(value);
|
|
336
|
+
for (const secret of privilegedCredentialValues(credentials)) {
|
|
337
|
+
expect(serialized).not.toContain(secret);
|
|
338
|
+
}
|
|
301
339
|
}
|
|
302
340
|
|
|
303
|
-
function
|
|
304
|
-
|
|
341
|
+
async function assertStoredStateSecretFreeAsync(
|
|
342
|
+
projectPath: string,
|
|
343
|
+
credentials: Readonly<Record<string, string>>,
|
|
344
|
+
): Promise<void> {
|
|
345
|
+
const state = await fs.readFile(
|
|
346
|
+
path.join(projectPath, '.ankh', 'infra', 'local', 'state.json'),
|
|
347
|
+
'utf8',
|
|
348
|
+
);
|
|
349
|
+
for (const secret of privilegedCredentialValues(credentials)) {
|
|
350
|
+
expect(state).not.toContain(secret);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async function assertCredentialFilePermissionsAsync(projectPath: string): Promise<void> {
|
|
355
|
+
if (process.platform === 'win32') return;
|
|
356
|
+
const digest = createHash('sha256').update(bootstrapCredential.name).digest('hex');
|
|
357
|
+
const credentialPath = path.join(
|
|
358
|
+
projectPath,
|
|
359
|
+
'.ankh',
|
|
360
|
+
'infra',
|
|
361
|
+
'local',
|
|
362
|
+
'credentials',
|
|
363
|
+
`${digest}.json`,
|
|
364
|
+
);
|
|
365
|
+
expect((await fs.stat(credentialPath)).mode & 0o777).toBe(0o600);
|
|
305
366
|
}
|
|
306
367
|
|
|
307
368
|
function requireSuccess<T>(result: InfraResult<T>): T {
|
|
308
369
|
if (!result.ok) throw new Error(result.diagnostics.map(({ message }) => message).join(' '));
|
|
309
370
|
return result.value;
|
|
310
371
|
}
|
|
311
|
-
|
|
312
|
-
function success<T>(value: T): InfraResult<T> {
|
|
313
|
-
return { ok: true, value, diagnostics: [] };
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function failure(code: string, message: string): InfraResult<never> {
|
|
317
|
-
return { ok: false, diagnostics: [{ severity: 'error', code, message }] };
|
|
318
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createHmac } from 'node:crypto';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
+
InfraControlPlaneCredentialRef,
|
|
5
|
+
InfraCredentialPort,
|
|
4
6
|
InfraLedger,
|
|
5
7
|
InfraManifest,
|
|
6
8
|
InfraResourceIdentity,
|
|
@@ -430,16 +432,7 @@ function redactDiagnosticText(value: string): string {
|
|
|
430
432
|
function createDependencies(): InfraOrchestrationDependencies {
|
|
431
433
|
return {
|
|
432
434
|
adapterResolver: createNodeInfraAdapterPackageResolver(),
|
|
433
|
-
credentials:
|
|
434
|
-
resolveAsync: ({ name }) =>
|
|
435
|
-
Promise.resolve(
|
|
436
|
-
name === 'SUPABASE_BOOTSTRAP'
|
|
437
|
-
? success(supabaseCredentials())
|
|
438
|
-
: name === 'S3_PERSISTENCE'
|
|
439
|
-
? success({ accessKeyId: s3AccessKey, secretAccessKey: s3SecretKey })
|
|
440
|
-
: failure('unexpected-credential', `Unexpected credential reference: ${name}`),
|
|
441
|
-
),
|
|
442
|
-
},
|
|
435
|
+
credentials: createCredentialPort(),
|
|
443
436
|
secrets: {
|
|
444
437
|
resolveAsync: ({ ref, key }) =>
|
|
445
438
|
Promise.resolve(failure('unexpected-secret', `Unexpected managed secret ${ref}/${key}.`)),
|
|
@@ -447,6 +440,40 @@ function createDependencies(): InfraOrchestrationDependencies {
|
|
|
447
440
|
};
|
|
448
441
|
}
|
|
449
442
|
|
|
443
|
+
function createCredentialPort(): InfraCredentialPort {
|
|
444
|
+
return {
|
|
445
|
+
findAsync: (reference) => Promise.resolve(findCredential(reference)),
|
|
446
|
+
resolveAsync: (reference) => Promise.resolve(resolveCredential(reference)),
|
|
447
|
+
persistAsync: (reference) =>
|
|
448
|
+
Promise.resolve(
|
|
449
|
+
failure(
|
|
450
|
+
'unexpected-credential-persist',
|
|
451
|
+
`Recovery acceptance must not persist credential ${reference.name}.`,
|
|
452
|
+
),
|
|
453
|
+
),
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function findCredential(
|
|
458
|
+
reference: InfraControlPlaneCredentialRef,
|
|
459
|
+
): InfraResult<Readonly<Record<string, string>> | null> {
|
|
460
|
+
if (reference.name === 'SUPABASE_BOOTSTRAP') return success(supabaseCredentials());
|
|
461
|
+
if (reference.name === 'S3_PERSISTENCE') {
|
|
462
|
+
return success({ accessKeyId: s3AccessKey, secretAccessKey: s3SecretKey });
|
|
463
|
+
}
|
|
464
|
+
return success(null);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function resolveCredential(
|
|
468
|
+
reference: InfraControlPlaneCredentialRef,
|
|
469
|
+
): InfraResult<Readonly<Record<string, string>>> {
|
|
470
|
+
if (reference.name === 'SUPABASE_BOOTSTRAP') return success(supabaseCredentials());
|
|
471
|
+
if (reference.name === 'S3_PERSISTENCE') {
|
|
472
|
+
return success({ accessKeyId: s3AccessKey, secretAccessKey: s3SecretKey });
|
|
473
|
+
}
|
|
474
|
+
return failure('unexpected-credential', `Unexpected credential reference: ${reference.name}`);
|
|
475
|
+
}
|
|
476
|
+
|
|
450
477
|
function createDestroyRequest(
|
|
451
478
|
previous: InfraLedger,
|
|
452
479
|
confirmedResources: readonly InfraResourceIdentity[],
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
createProjectInfraCredentialPort,
|
|
4
5
|
createProjectInfraLifecycle,
|
|
5
6
|
readStoredInfraStateAsync,
|
|
6
7
|
resolveInfraProjectAsync,
|
|
@@ -8,7 +9,8 @@ import {
|
|
|
8
9
|
} from './index.js';
|
|
9
10
|
|
|
10
11
|
describe('@ankhorage/infra/project', () => {
|
|
11
|
-
test('exports project resolution, safe state and artifact owner APIs', () => {
|
|
12
|
+
test('exports project resolution, credentials, safe state and artifact owner APIs', () => {
|
|
13
|
+
expect(createProjectInfraCredentialPort).toBeFunction();
|
|
12
14
|
expect(createProjectInfraLifecycle).toBeFunction();
|
|
13
15
|
expect(resolveInfraProjectAsync).toBeFunction();
|
|
14
16
|
expect(readStoredInfraStateAsync).toBeFunction();
|
package/src/project/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { createProjectInfraCredentialPort } from '../features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.js';
|
|
1
2
|
export { readStoredInfraStateAsync } from '../features/environment-lifecycle/adapters/outbound/readStoredInfraStateAsync.js';
|
|
2
3
|
export { removeStoredInfraStateAsync } from '../features/environment-lifecycle/adapters/outbound/removeStoredInfraStateAsync.js';
|
|
3
4
|
export { resolveInfraProjectAsync } from '../features/environment-lifecycle/adapters/outbound/resolveInfraProjectAsync.js';
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
InfraControlPlaneCredentialRef,
|
|
7
|
+
InfraEnvironmentSpec,
|
|
8
|
+
InfraLedger,
|
|
9
|
+
InfraResult,
|
|
10
|
+
} from '@ankhorage/contracts/infra';
|
|
11
|
+
import { expect, it, mock } from 'bun:test';
|
|
12
|
+
|
|
13
|
+
import { createProjectInfraCredentialPort } from './features/environment-lifecycle/adapters/outbound/createProjectInfraCredentialPort.js';
|
|
14
|
+
import { createProjectInfraLifecycle } from './project/index.js';
|
|
15
|
+
import type { InfraLifecycleOperations } from './types/infraCli.js';
|
|
16
|
+
|
|
17
|
+
const bootstrap = {
|
|
18
|
+
source: 'control-plane',
|
|
19
|
+
name: 'FIXTURE_BOOTSTRAP',
|
|
20
|
+
} as const satisfies InfraControlPlaneCredentialRef;
|
|
21
|
+
const generated = { username: 'fixture', password: 'generated-secret' } as const;
|
|
22
|
+
const previewGenerated = { username: 'preview', password: 'preview-secret' } as const;
|
|
23
|
+
|
|
24
|
+
const desired = {
|
|
25
|
+
deployment: {
|
|
26
|
+
compute: { provider: 'local' },
|
|
27
|
+
runtime: { provider: 'docker-compose' },
|
|
28
|
+
},
|
|
29
|
+
} as const satisfies InfraEnvironmentSpec;
|
|
30
|
+
|
|
31
|
+
const ledger = {
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
projectId: 'sample',
|
|
34
|
+
environment: 'local',
|
|
35
|
+
targets: [],
|
|
36
|
+
resources: [],
|
|
37
|
+
outputs: [],
|
|
38
|
+
artifacts: [],
|
|
39
|
+
} as const satisfies InfraLedger;
|
|
40
|
+
|
|
41
|
+
it('persists provider-created credentials across up/down and removes only the destroyed environment', async () => {
|
|
42
|
+
await withTempProjectAsync(async (projectPath) => {
|
|
43
|
+
const generateCredential = mock(() => generated);
|
|
44
|
+
const operations = createCredentialOperations(generateCredential);
|
|
45
|
+
const request = {
|
|
46
|
+
projectId: 'sample',
|
|
47
|
+
projectPath,
|
|
48
|
+
manifest: { environments: { local: desired, preview: desired }, modules: [] },
|
|
49
|
+
environment: 'local' as const,
|
|
50
|
+
executionEnvironment: {},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const firstLifecycle = createProjectInfraLifecycle({ services: { operations } });
|
|
54
|
+
const firstUp = await firstLifecycle.upAsync(request);
|
|
55
|
+
expect(firstUp.ok).toBe(true);
|
|
56
|
+
expect(generateCredential).toHaveBeenCalledTimes(1);
|
|
57
|
+
expect(JSON.stringify(firstUp)).not.toContain('generated-secret');
|
|
58
|
+
expect(await fs.readFile(statePath(projectPath, 'local'), 'utf8')).not.toContain(
|
|
59
|
+
'generated-secret',
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const down = await firstLifecycle.downAsync(request);
|
|
63
|
+
expect(down.ok).toBe(true);
|
|
64
|
+
expect(await createPort(projectPath, 'local').resolveAsync(bootstrap)).toMatchObject({
|
|
65
|
+
ok: true,
|
|
66
|
+
value: generated,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const secondLifecycle = createProjectInfraLifecycle({ services: { operations } });
|
|
70
|
+
const secondUp = await secondLifecycle.upAsync(request);
|
|
71
|
+
expect(secondUp.ok).toBe(true);
|
|
72
|
+
expect(generateCredential).toHaveBeenCalledTimes(1);
|
|
73
|
+
expect(JSON.stringify(secondUp)).not.toContain('generated-secret');
|
|
74
|
+
|
|
75
|
+
const previewPort = createPort(projectPath, 'preview');
|
|
76
|
+
expect(await previewPort.persistAsync(bootstrap, previewGenerated)).toMatchObject({ ok: true });
|
|
77
|
+
|
|
78
|
+
const destroyed = await secondLifecycle.destroyAsync({
|
|
79
|
+
...request,
|
|
80
|
+
confirmation: { projectId: 'sample', environment: 'local' },
|
|
81
|
+
persistence: { policy: 'retain' },
|
|
82
|
+
});
|
|
83
|
+
expect(destroyed.ok).toBe(true);
|
|
84
|
+
expect(await createPort(projectPath, 'local').findAsync(bootstrap)).toEqual({
|
|
85
|
+
ok: true,
|
|
86
|
+
value: null,
|
|
87
|
+
diagnostics: [],
|
|
88
|
+
});
|
|
89
|
+
expect(await previewPort.resolveAsync(bootstrap)).toMatchObject({
|
|
90
|
+
ok: true,
|
|
91
|
+
value: previewGenerated,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
/*** Create provider-neutral operation fixtures that generate credentials only when lookup is missing. */
|
|
97
|
+
function createCredentialOperations(
|
|
98
|
+
generateCredential: () => Readonly<Record<string, string>>,
|
|
99
|
+
): InfraLifecycleOperations {
|
|
100
|
+
return {
|
|
101
|
+
validate: () => Promise.resolve(success({ environment: 'local' })),
|
|
102
|
+
plan: (request) =>
|
|
103
|
+
Promise.resolve(success({ projectId: request.projectId, environment: 'local', actions: [] })),
|
|
104
|
+
generate: () => Promise.resolve(success({ environment: 'local', artifacts: [], ledger })),
|
|
105
|
+
up: async (_request, dependencies) => {
|
|
106
|
+
const found = await dependencies.credentials.findAsync(bootstrap);
|
|
107
|
+
if (!found.ok) return { ok: false, diagnostics: found.diagnostics };
|
|
108
|
+
if (found.value === null) {
|
|
109
|
+
const persisted = await dependencies.credentials.persistAsync(
|
|
110
|
+
bootstrap,
|
|
111
|
+
generateCredential(),
|
|
112
|
+
);
|
|
113
|
+
if (!persisted.ok) return { ok: false, diagnostics: persisted.diagnostics };
|
|
114
|
+
}
|
|
115
|
+
return success({
|
|
116
|
+
environment: 'local',
|
|
117
|
+
targets: [],
|
|
118
|
+
resources: [],
|
|
119
|
+
outputs: [],
|
|
120
|
+
ledger,
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
status: (request) =>
|
|
124
|
+
Promise.resolve(
|
|
125
|
+
success({
|
|
126
|
+
projectId: request.projectId,
|
|
127
|
+
environment: 'local',
|
|
128
|
+
state: 'ready',
|
|
129
|
+
resources: [],
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
outputs: () => success({ environment: 'local', outputs: [] }),
|
|
133
|
+
down: () => Promise.resolve(success({ environment: 'local', ledger })),
|
|
134
|
+
destroy: () => Promise.resolve(success({ environment: 'local', ledger: null })),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/*** Create the real project-local credential adapter for one isolated test environment. */
|
|
139
|
+
function createPort(projectPath: string, environment: 'local' | 'preview') {
|
|
140
|
+
return createProjectInfraCredentialPort({ projectPath, environment, processEnvironment: {} });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/*** Resolve the canonical Infra ownership-state path without touching credential storage internals. */
|
|
144
|
+
function statePath(projectPath: string, environment: 'local' | 'preview'): string {
|
|
145
|
+
return path.join(projectPath, '.ankh', 'infra', environment, 'state.json');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/*** Create one successful Infra result for operation fixtures. */
|
|
149
|
+
function success<T>(value: T): InfraResult<T> {
|
|
150
|
+
return { ok: true, value, diagnostics: [] };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/*** Run one filesystem test against a disposable project root. */
|
|
154
|
+
async function withTempProjectAsync(run: (projectPath: string) => Promise<void>): Promise<void> {
|
|
155
|
+
const projectPath = await fs.mkdtemp(path.join(os.tmpdir(), 'ankh-infra-credential-lifecycle-'));
|
|
156
|
+
try {
|
|
157
|
+
await run(projectPath);
|
|
158
|
+
} finally {
|
|
159
|
+
await fs.rm(projectPath, { recursive: true, force: true });
|
|
160
|
+
}
|
|
161
|
+
}
|