@fabrika/installation-init 0.9.0-dev.2 → 0.9.0-dev.20
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/package.json +7 -5
- package/src/admin.ts +17 -8
- package/src/auth-methods.ts +90 -0
- package/src/environment.ts +32 -4
- package/src/index.ts +49 -0
- package/src/log.ts +1 -1
- package/src/mcp.ts +102 -0
- package/src/pin.ts +26 -0
- package/src/prompt.ts +3 -3
- package/src/proxy-manifest.ts +221 -0
- package/src/secrets-file.ts +61 -0
- package/src/secrets.ts +78 -0
- package/src/service-environment.ts +231 -0
- package/types/src/admin.d.ts +6 -1
- package/types/src/auth-methods.d.ts +34 -0
- package/types/src/environment.d.ts +5 -0
- package/types/src/index.d.ts +8 -1
- package/types/src/mcp.d.ts +18 -0
- package/types/src/pin.d.ts +2 -0
- package/types/src/proxy-manifest.d.ts +109 -0
- package/types/src/secrets-file.d.ts +18 -0
- package/types/src/secrets.d.ts +32 -0
- package/types/src/service-environment.d.ts +93 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// The proxy manifest a platform installation is configured with, split into the half that is the same
|
|
2
|
+
// on every installation and the half that is one account's.
|
|
3
|
+
//
|
|
4
|
+
// ── Why a template rather than a `ProxyManifest` with the hosts left out ──────────────────────────
|
|
5
|
+
//
|
|
6
|
+
// `parseProxyManifest` refuses an app that declares no hosts, and rightly — a host-less app is fronted
|
|
7
|
+
// by nothing and protects nothing. So the committed half cannot BE a manifest with empty hosts; it is a
|
|
8
|
+
// type of its own, and `resolvePlatformProxyManifest` is the one way to turn it into the document the
|
|
9
|
+
// proxy parses.
|
|
10
|
+
//
|
|
11
|
+
// ── The split ────────────────────────────────────────────────────────────────────────────────────
|
|
12
|
+
//
|
|
13
|
+
// template which apps exist, their ids, the private service each is dialed at, the public listener
|
|
14
|
+
// each answers on, and their service table. True of every installation of one provider, so the
|
|
15
|
+
// provider ships it GENERATED (Zerops: `provider-zerops/src/installation/generated/`) and reviewed in
|
|
16
|
+
// a diff.
|
|
17
|
+
// placement the hostnames and the browser-facing scheme. A generated cloud hostname names one
|
|
18
|
+
// account's project, and a production installation binds custom domains instead, so
|
|
19
|
+
// whoever is deploying supplies these.
|
|
20
|
+
//
|
|
21
|
+
// Nothing in this file imports the packages that OWN the tables. That is the point of the split:
|
|
22
|
+
// they live in the private service packages and reach a deployed installation as DATA, through the
|
|
23
|
+
// generated artifact, so the published deploy path never depends on a package it cannot resolve.
|
|
24
|
+
|
|
25
|
+
import { type InstallationEndpointTable, installationTableDigest } from '@fabrika/auth-core'
|
|
26
|
+
import {
|
|
27
|
+
type InstallationProxyApp,
|
|
28
|
+
isApplicationEntry,
|
|
29
|
+
type OAuthProtectedResource,
|
|
30
|
+
parseProxyManifest,
|
|
31
|
+
serviceProxyTable,
|
|
32
|
+
} from '@fabrika/proxy-core/manifest'
|
|
33
|
+
|
|
34
|
+
/** The platform services an installation's proxy fronts. Each name is a service hostname in the topology. */
|
|
35
|
+
export type PlatformProxyService = 'iam' | 'control' | 'operations'
|
|
36
|
+
|
|
37
|
+
/** The public hostname of each platform service, without a scheme and without a port. */
|
|
38
|
+
export type PlatformHosts = Readonly<Record<PlatformProxyService, string>>
|
|
39
|
+
|
|
40
|
+
/** `https://<host>` — the origin a browser, a token's `iss` and a return-origin registry all use. */
|
|
41
|
+
export const platformOrigin = (scheme: 'http' | 'https', host: string): string => `${scheme}://${host}`
|
|
42
|
+
|
|
43
|
+
/** What one OAuth scope lets a token do: the owning app's actions, resolved by IAM at the token endpoint. */
|
|
44
|
+
export interface PlatformOAuthScopeGrant {
|
|
45
|
+
readonly scope: string
|
|
46
|
+
readonly actions: readonly string[]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A path an app serves to OAuth clients, protected only on an installation that enables it.
|
|
51
|
+
*
|
|
52
|
+
* The resource URI is the app's public origin plus `path`, so the template holds the path and a deploy
|
|
53
|
+
* binds the origin. Copied from the owning service module by the generator, like `table`.
|
|
54
|
+
*/
|
|
55
|
+
export interface PlatformOAuthResourceTemplate {
|
|
56
|
+
readonly path: string
|
|
57
|
+
readonly scopes: readonly string[]
|
|
58
|
+
readonly scopeGrants: readonly PlatformOAuthScopeGrant[]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* One fronted app, in the form that is the same on every installation: protected by its service contract (ADR-0022
|
|
63
|
+
* "Installation entries"), copied in by the generator and never written by hand.
|
|
64
|
+
*/
|
|
65
|
+
export interface PlatformProxyApp {
|
|
66
|
+
/** The IAM app id. It is the audience of every token the proxy mints for this app's host. */
|
|
67
|
+
readonly id: string
|
|
68
|
+
/** The topology service that answers for it. */
|
|
69
|
+
readonly service: PlatformProxyService
|
|
70
|
+
/**
|
|
71
|
+
* The proxy's public listener this app answers on.
|
|
72
|
+
*
|
|
73
|
+
* On a `zerops-subdomain` installation a port IS a hostname — Zerops mints one generated host per
|
|
74
|
+
* HTTP port — so this is the only installation-independent half of `hosts`. A custom-domain
|
|
75
|
+
* installation ignores it: every domain arrives on one listener and the `Host` header decides.
|
|
76
|
+
*/
|
|
77
|
+
readonly port: number
|
|
78
|
+
/** Where the proxy dials it over the project's private network. */
|
|
79
|
+
readonly upstream: string
|
|
80
|
+
/** The service's rows; with an `oauthResource`, the table served when the composition names an authorization server. */
|
|
81
|
+
readonly table: InstallationEndpointTable
|
|
82
|
+
/** Present only on an app that can serve OAuth clients; off unless the composition names an authorization server. */
|
|
83
|
+
readonly oauthResource?: PlatformOAuthResourceTemplate
|
|
84
|
+
/**
|
|
85
|
+
* Present exactly with `oauthResource`: the service's table when it serves no OAuth client. The service picks the
|
|
86
|
+
* same one, by the digest the deploy writes beside it (`platformServiceTableDigest`).
|
|
87
|
+
*/
|
|
88
|
+
readonly tableWithoutOAuth?: InstallationEndpointTable
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Everything about an installation's manifest that does not depend on WHICH installation it is. */
|
|
92
|
+
export interface PlatformProxyManifestTemplate {
|
|
93
|
+
readonly apps: readonly PlatformProxyApp[]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The template's entry for one platform service.
|
|
98
|
+
*
|
|
99
|
+
* The app IDS live in the template, so a caller that needs one — the deploy's schema reconcile needs
|
|
100
|
+
* the console's — reads it from there rather than restating a literal that could drift from the
|
|
101
|
+
* document the proxy is actually configured with.
|
|
102
|
+
*/
|
|
103
|
+
export const platformProxyAppFor = (
|
|
104
|
+
template: PlatformProxyManifestTemplate,
|
|
105
|
+
service: PlatformProxyService,
|
|
106
|
+
): PlatformProxyApp => {
|
|
107
|
+
const app = template.apps.find((candidate) => candidate.service === service)
|
|
108
|
+
if (app === undefined) {
|
|
109
|
+
throw new Error(`the platform proxy manifest template fronts no \`${service}\` service`)
|
|
110
|
+
}
|
|
111
|
+
return app
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Where one app answers in a particular composition. */
|
|
115
|
+
export interface PlatformProxyPlacement {
|
|
116
|
+
/** Public hostnames, without a port — `parseProxyManifest` refuses a `:port` suffix. */
|
|
117
|
+
readonly hosts: readonly string[]
|
|
118
|
+
/** Overrides the private dial address. The local composition runs IAM on a port of its own. */
|
|
119
|
+
readonly upstream?: string
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** One composition of the platform: a deployed installation, or the local Docker stack. */
|
|
123
|
+
export interface PlatformProxyComposition {
|
|
124
|
+
/** The scheme the BROWSER speaks to these apps — configuration, never a header (see `ProxyApp.scheme`). */
|
|
125
|
+
readonly scheme: 'http' | 'https'
|
|
126
|
+
readonly placement: Readonly<Record<PlatformProxyService, PlatformProxyPlacement>>
|
|
127
|
+
/**
|
|
128
|
+
* The public IAM origin that issues OAuth tokens. Present only when the installation enables MCP
|
|
129
|
+
* authoring; absent leaves every `oauthResource` unregistered and serves each service's table without OAuth.
|
|
130
|
+
*/
|
|
131
|
+
readonly authorizationServer?: string
|
|
132
|
+
/** The port browsers reach every app on, when it is not the scheme's default. The local composition's is 18080. */
|
|
133
|
+
readonly publicPort?: number
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The RFC 8707 resource URI and RFC 9728 metadata URL of an app's OAuth resource, bound to the app's
|
|
138
|
+
* FIRST host: the proxy refuses a resource on a host the app does not answer on.
|
|
139
|
+
*/
|
|
140
|
+
export const platformOAuthResourceUris = (
|
|
141
|
+
composition: Pick<PlatformProxyComposition, 'scheme' | 'publicPort'>,
|
|
142
|
+
hosts: readonly string[],
|
|
143
|
+
template: PlatformOAuthResourceTemplate,
|
|
144
|
+
): { readonly resource: string; readonly resourceMetadata: string } => {
|
|
145
|
+
const host = hosts[0]
|
|
146
|
+
if (host === undefined) {
|
|
147
|
+
throw new Error('an OAuth resource needs a host to stand on')
|
|
148
|
+
}
|
|
149
|
+
const origin = composition.publicPort === undefined
|
|
150
|
+
? `${composition.scheme}://${host}`
|
|
151
|
+
: `${composition.scheme}://${host}:${composition.publicPort}`
|
|
152
|
+
return {
|
|
153
|
+
resource: `${origin}${template.path}`,
|
|
154
|
+
resourceMetadata: `${origin}/.well-known/oauth-protected-resource${template.path}`,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const registeredResource = (
|
|
159
|
+
hosts: readonly string[],
|
|
160
|
+
template: PlatformOAuthResourceTemplate,
|
|
161
|
+
composition: PlatformProxyComposition,
|
|
162
|
+
authorizationServer: string,
|
|
163
|
+
): OAuthProtectedResource => ({
|
|
164
|
+
...platformOAuthResourceUris(composition, hosts, template),
|
|
165
|
+
authorizationServers: [authorizationServer],
|
|
166
|
+
scopesSupported: [...template.scopes],
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
/** Only an installation that names an authorization server registers the resource and serves its OAuth rows. */
|
|
170
|
+
const resolvedApp = (app: PlatformProxyApp, composition: PlatformProxyComposition): InstallationProxyApp => {
|
|
171
|
+
const placement = composition.placement[app.service]
|
|
172
|
+
const hosts = [...placement.hosts]
|
|
173
|
+
const registered = app.oauthResource === undefined || composition.authorizationServer === undefined
|
|
174
|
+
? undefined
|
|
175
|
+
: registeredResource(hosts, app.oauthResource, composition, composition.authorizationServer)
|
|
176
|
+
const table = registered === undefined ? (app.tableWithoutOAuth ?? app.table) : app.table
|
|
177
|
+
return {
|
|
178
|
+
id: app.id,
|
|
179
|
+
hosts,
|
|
180
|
+
upstream: placement.upstream ?? app.upstream,
|
|
181
|
+
scheme: composition.scheme,
|
|
182
|
+
...serviceProxyTable(table, { oauth: registered !== undefined }),
|
|
183
|
+
...(registered === undefined ? {} : { oauthResources: [registered] }),
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Bind a template to one composition and get the document the proxy actually parses.
|
|
189
|
+
*
|
|
190
|
+
* The result is returned THROUGH `parseProxyManifest`, so a duplicate id, a repeated host or a `:port`
|
|
191
|
+
* suffix is refused right here instead of at a deployed proxy's next build.
|
|
192
|
+
*/
|
|
193
|
+
export const resolvePlatformProxyManifest = (
|
|
194
|
+
template: PlatformProxyManifestTemplate,
|
|
195
|
+
composition: PlatformProxyComposition,
|
|
196
|
+
): PlatformProxyManifest => {
|
|
197
|
+
const manifest = parseProxyManifest({ apps: template.apps.map((app) => resolvedApp(app, composition)) })
|
|
198
|
+
// Every platform service is an installation entry, never an application.
|
|
199
|
+
const services = manifest?.apps.flatMap((app) => isApplicationEntry(app) ? [] : [app]) ?? []
|
|
200
|
+
if (manifest === null || services.length !== manifest.apps.length) {
|
|
201
|
+
throw new Error('the resolved platform proxy manifest is not one the proxy would accept')
|
|
202
|
+
}
|
|
203
|
+
return { apps: services }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The platform proxy's manifest: one installation entry per platform service. */
|
|
207
|
+
export interface PlatformProxyManifest {
|
|
208
|
+
apps: InstallationProxyApp[]
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* `FABRIKA_SERVICE_TABLE_DIGEST` for a service: the digest of the table this manifest publishes for it, which the
|
|
213
|
+
* service compares with its own and refuses to start under another.
|
|
214
|
+
*/
|
|
215
|
+
export const platformServiceTableDigest = (manifest: PlatformProxyManifest, appId: string): string => {
|
|
216
|
+
const entry = manifest.apps.find((app) => app.id === appId)
|
|
217
|
+
if (entry === undefined) {
|
|
218
|
+
throw new Error(`the platform proxy manifest fronts no \`${appId}\``)
|
|
219
|
+
}
|
|
220
|
+
return installationTableDigest(entry)
|
|
221
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The operator's local copy of every credential a bring-up creates.
|
|
2
|
+
//
|
|
3
|
+
// A cloud that stores a credential write-only hands nothing back, so this file is the copy the command
|
|
4
|
+
// writes before the first credential is placed: a run that fails later loses nothing.
|
|
5
|
+
|
|
6
|
+
import { existsSync } from 'node:fs'
|
|
7
|
+
import { open } from 'node:fs/promises'
|
|
8
|
+
import { isAbsolute, relative, resolve } from 'node:path'
|
|
9
|
+
|
|
10
|
+
/** `--secrets-file`: where the local copy goes. A new file, outside the source checkout. */
|
|
11
|
+
export const SECRETS_FILE_FLAG = '--secrets-file'
|
|
12
|
+
export const SECRETS_FILE_VARIABLE = 'FABRIKA_PLATFORM_SECRETS_FILE'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Settle the path before anything is contacted.
|
|
16
|
+
*
|
|
17
|
+
* Refused when it exists, because overwriting one would destroy another installation's only copy, and
|
|
18
|
+
* when it is inside the source checkout, where it would dirty the checkout a deploy uploads and could be
|
|
19
|
+
* committed.
|
|
20
|
+
*/
|
|
21
|
+
export const checkedSecretsFilePath = (path: string, sourceDir: string): string => {
|
|
22
|
+
const absolute = resolve(path)
|
|
23
|
+
const fromSource = relative(resolve(sourceDir), absolute)
|
|
24
|
+
if (fromSource === '' || (!fromSource.startsWith('..') && !isAbsolute(fromSource))) {
|
|
25
|
+
throw new Error(`${SECRETS_FILE_FLAG}=${path} is inside the source checkout — name a path outside it, so it is never uploaded or committed`)
|
|
26
|
+
}
|
|
27
|
+
if (existsSync(absolute)) {
|
|
28
|
+
throw new Error(`${SECRETS_FILE_FLAG}=${path} already exists — it may be another installation's only copy; name a new file`)
|
|
29
|
+
}
|
|
30
|
+
return absolute
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The file's text: comment lines, then shell-sourceable assignments, named as the commands that read
|
|
35
|
+
* them name them. `header` is the provider's own lines, each written as a `#` comment.
|
|
36
|
+
*/
|
|
37
|
+
export const renderSecretsFile = (header: readonly string[], assignments: readonly (readonly [string, string])[]): string => {
|
|
38
|
+
const lines = assignments.map(([name, value]) => {
|
|
39
|
+
// Single quotes keep every character literal; none of these values can contain one.
|
|
40
|
+
if (value.includes("'") || value.includes('\n')) {
|
|
41
|
+
throw new Error(`${name} cannot be written to the secrets file as one quoted line`)
|
|
42
|
+
}
|
|
43
|
+
return `${name}='${value}'`
|
|
44
|
+
})
|
|
45
|
+
return [
|
|
46
|
+
...header.map((line) => `# ${line}`),
|
|
47
|
+
'# Load it in a subshell: ( set -a && . <this file> && set +a && fabrika … )',
|
|
48
|
+
...lines,
|
|
49
|
+
'',
|
|
50
|
+
].join('\n')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Create the file with mode 0600, failing rather than replacing one that appeared since the check. */
|
|
54
|
+
export const writeSecretsFile = async (path: string, text: string): Promise<void> => {
|
|
55
|
+
const file = await open(path, 'wx', 0o600)
|
|
56
|
+
try {
|
|
57
|
+
await file.writeFile(text)
|
|
58
|
+
} finally {
|
|
59
|
+
await file.close()
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/secrets.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// The credentials a platform installation is seeded with, and their shapes.
|
|
2
|
+
//
|
|
3
|
+
// The shapes are not free choices. Each one is what the consuming code already parses:
|
|
4
|
+
//
|
|
5
|
+
// • `FABRIKA_IAM_SIGNING_KEYS` is a JSON array of PRIVATE JWKs, index 0 the active signer
|
|
6
|
+
// (`@fabrika/iam`'s `Signer.fromPrivateJwks`). ES256 / P-256, RFC 7638 thumbprint `kid`.
|
|
7
|
+
// • A `px_` key is IAM's own credential spelling, and every shared secret must be at least 32
|
|
8
|
+
// characters or the service refuses to boot — 32 random bytes at base64url is 43.
|
|
9
|
+
// • The vault KEK is 32 RAW BYTES at base64, not base64url: `@fabrika/control` imports it as an
|
|
10
|
+
// AES key, and base64url is a different alphabet.
|
|
11
|
+
//
|
|
12
|
+
// Nothing here re-reads an existing value: whether a run generates or reuses one is the provider's flow.
|
|
13
|
+
|
|
14
|
+
import { createHash, generateKeyPairSync, randomBytes } from 'node:crypto'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A 256-bit secret in base64url, never starting with `-` or `_`.
|
|
18
|
+
*
|
|
19
|
+
* The re-roll is copied from `@fabrika/local-stack`'s `prepare.ts` and it is not superstition: a
|
|
20
|
+
* credential beginning with `-` reads as a FLAG to anything that later takes it on a command line, no
|
|
21
|
+
* matter how it is quoted. One rolled password beginning with `-` made `mc alias set` print its usage
|
|
22
|
+
* and loop forever. It costs nothing and removes a bug that appears about one install in sixteen.
|
|
23
|
+
*/
|
|
24
|
+
export const randomSecret = (): string => {
|
|
25
|
+
for (;;) {
|
|
26
|
+
const value = randomBytes(32).toString('base64url')
|
|
27
|
+
if (!value.startsWith('-') && !value.startsWith('_')) {
|
|
28
|
+
return value
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The `px_` prefix IAM's own credentials carry, so a key looks like what it is wherever it turns up. */
|
|
34
|
+
export const randomPxKey = (): string => `px_${randomSecret()}`
|
|
35
|
+
|
|
36
|
+
/** The control plane's vault KEK. base64, NOT base64url: the vault imports these bytes as an AES key. */
|
|
37
|
+
export const generateVaultKey = (): string => randomBytes(32).toString('base64')
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One ES256 (EC P-256) private JWK with an RFC 7638 thumbprint `kid`, shaped exactly as IAM's
|
|
41
|
+
* `Signer.fromPrivateJwks` loads it.
|
|
42
|
+
*/
|
|
43
|
+
const generateEs256Jwk = (): JsonWebKey & { kid: string; alg: string; use: string } => {
|
|
44
|
+
const { privateKey } = generateKeyPairSync('ec', { namedCurve: 'P-256' })
|
|
45
|
+
const jwk = privateKey.export({ format: 'jwk' })
|
|
46
|
+
// RFC 7638 thumbprint over the canonical EC members, in lexicographic order (crv, kty, x, y).
|
|
47
|
+
const thumbprint = createHash('sha256').update(JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y })).digest('base64url')
|
|
48
|
+
return { ...jwk, kid: thumbprint, alg: 'ES256', use: 'sig' }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** IAM's token-signing material: a JSON array with one ES256 private JWK, index 0 the active signer. */
|
|
52
|
+
export const generateSigningKeys = (): string => JSON.stringify([generateEs256Jwk()])
|
|
53
|
+
|
|
54
|
+
/** Everything a container installation is seeded with. Several of these are shared by more than one service. */
|
|
55
|
+
export interface InstallationSecrets {
|
|
56
|
+
/** IAM's token-signing material: a JSON array of ES256 private JWKs, index 0 active. */
|
|
57
|
+
readonly signingKeys: string
|
|
58
|
+
/** Authenticates IAM's management `/rpc/*` surface. Held by IAM, control and Operations. */
|
|
59
|
+
readonly rpcKey: string
|
|
60
|
+
/** Authenticates IAM's `/auth/mint/*` surface, which the proxy calls on the cold path. */
|
|
61
|
+
readonly proxyKey: string
|
|
62
|
+
/** The `px_` admin bearer IAM admits as a synthetic global admin. `platform init` and `admin` take it. */
|
|
63
|
+
readonly provisioningKey: string
|
|
64
|
+
/** The catalog credential control and Operations share. No `px_` prefix — it is not an IAM credential. */
|
|
65
|
+
readonly operationsSyncKey: string
|
|
66
|
+
/** The control plane's vault KEK: 32 raw bytes at base64. Its loss is unrecoverable by design. */
|
|
67
|
+
readonly vaultKey: string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Roll one installation's secrets. Pure entropy in, no I/O, nothing persisted. */
|
|
71
|
+
export const generateInstallationSecrets = (): InstallationSecrets => ({
|
|
72
|
+
signingKeys: generateSigningKeys(),
|
|
73
|
+
rpcKey: randomPxKey(),
|
|
74
|
+
proxyKey: randomPxKey(),
|
|
75
|
+
provisioningKey: randomPxKey(),
|
|
76
|
+
operationsSyncKey: randomSecret(),
|
|
77
|
+
vaultKey: generateVaultKey(),
|
|
78
|
+
})
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// The environment fabrika's four platform services read, as one installation's values.
|
|
2
|
+
//
|
|
3
|
+
// The variable NAMES are the services' own (`@fabrika/iam`'s `node/runtime.ts`, Control's and Operations'
|
|
4
|
+
// configuration, the proxy's `readProxyEnv`), so they are the same on every cloud. What a provider supplies
|
|
5
|
+
// is where a service's data lives — a DSN or the cloud's reference to one — and where the proxy dials IAM
|
|
6
|
+
// privately; it adds the variables of its own cloud beside these and writes the result.
|
|
7
|
+
//
|
|
8
|
+
// IAM's Bun runtime reads `ISSUER`, `ENVIRONMENT` and the OIDC client's `OIDC_*` UNPREFIXED and everything else
|
|
9
|
+
// with the `FABRIKA_IAM_` prefix (`packages/iam/src/node/runtime.ts`). Both spellings below are the ones it reads.
|
|
10
|
+
|
|
11
|
+
import { FABRIKA_SERVICE_TABLE_DIGEST, IAM_PROXY_SECRET_VARIABLE } from '@fabrika/auth-core'
|
|
12
|
+
import { createHmac } from 'node:crypto'
|
|
13
|
+
import type { OidcClient } from './auth-methods'
|
|
14
|
+
import type { InstallationEmail } from './email'
|
|
15
|
+
import type { IamOAuthConfig } from './mcp'
|
|
16
|
+
import { type PlatformHosts, platformOrigin } from './proxy-manifest'
|
|
17
|
+
import type { InstallationSecrets } from './secrets'
|
|
18
|
+
|
|
19
|
+
/** The four services an installation configures. Each is a service of the platform's own topology. */
|
|
20
|
+
export type PlatformService = 'iam' | 'operations' | 'control' | 'proxy'
|
|
21
|
+
|
|
22
|
+
/** Each service's variables, in the order they are written. */
|
|
23
|
+
export type PlatformServiceEnvironment = Readonly<Record<PlatformService, ReadonlyMap<string, string>>>
|
|
24
|
+
|
|
25
|
+
/** IAM's OAuth configuration for MCP clients (ADR-0049): written only when the installation enables MCP authoring. */
|
|
26
|
+
export const IAM_OAUTH_CONFIG_VARIABLE = 'FABRIKA_IAM_OAUTH_CONFIG'
|
|
27
|
+
|
|
28
|
+
/** IAM's OIDC client secret, as IAM's Bun runtime reads it. The operator's credential: written sensitive, never generated. */
|
|
29
|
+
export const IAM_OIDC_CLIENT_SECRET_VARIABLE = 'OIDC_CLIENT_SECRET'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* How people sign in to IAM; `oidc` absent means OIDC is off. Neither method is not a value, as IAM refuses to boot
|
|
33
|
+
* with neither.
|
|
34
|
+
*/
|
|
35
|
+
export type InstallationAuthentication =
|
|
36
|
+
| { readonly passwordEnabled: true; readonly oidc?: OidcClient }
|
|
37
|
+
| { readonly passwordEnabled: false; readonly oidc: OidcClient }
|
|
38
|
+
|
|
39
|
+
/** An object store's coordinates, in the provider's own spelling (values or references to them). */
|
|
40
|
+
export interface PlatformStorage {
|
|
41
|
+
readonly bucket: string
|
|
42
|
+
readonly accessKeyId: string
|
|
43
|
+
readonly secretAccessKey: string
|
|
44
|
+
readonly endpoint: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Where each service's data lives. */
|
|
48
|
+
export interface PlatformData {
|
|
49
|
+
readonly iamDatabase: string
|
|
50
|
+
readonly controlDatabase: string
|
|
51
|
+
readonly operationsDatabase: string
|
|
52
|
+
/** Control's run logs. */
|
|
53
|
+
readonly runLogs: PlatformStorage
|
|
54
|
+
/** Operations' artifacts and source maps. */
|
|
55
|
+
readonly operationsBlobs: PlatformStorage
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Where the installation answers. Both the bring-up and every deploy derive their variables from these. */
|
|
59
|
+
export interface PlatformPlacementInput {
|
|
60
|
+
readonly environment: string
|
|
61
|
+
readonly scheme: 'http' | 'https'
|
|
62
|
+
readonly hosts: PlatformHosts
|
|
63
|
+
/** The private address the platform proxy dials IAM at, which owns the session (`host:port`). */
|
|
64
|
+
readonly iamUpstream: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** What `installationServiceEnvironment` needs. Everything in it is settled before the first write. */
|
|
68
|
+
export interface InstallationServiceInput extends PlatformPlacementInput {
|
|
69
|
+
readonly secrets: InstallationSecrets
|
|
70
|
+
readonly data: PlatformData
|
|
71
|
+
/** IAM sends its own mail with it, and Control hands it to applications whose job declares `mailer: 'installation'`. */
|
|
72
|
+
readonly email: InstallationEmail
|
|
73
|
+
readonly authentication: InstallationAuthentication
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* IAM's sign-in switches and, with OIDC on, its client. Both switches are STATED because IAM's own defaults are OIDC on
|
|
78
|
+
* and password off, and it refuses to boot with neither.
|
|
79
|
+
*
|
|
80
|
+
* Only the bring-up writes these. A deploy writes none of them and removes none, so a sign-in method an operator set
|
|
81
|
+
* by hand on an existing installation survives every later `platform deploy` and `platform upgrade`.
|
|
82
|
+
*/
|
|
83
|
+
const iamAuthentication = ({ passwordEnabled, oidc }: InstallationAuthentication): Array<[string, string]> => {
|
|
84
|
+
const switches: Array<[string, string]> = [
|
|
85
|
+
['FABRIKA_IAM_OIDC_ENABLED', String(oidc !== undefined)],
|
|
86
|
+
['FABRIKA_IAM_PASSWORD_ENABLED', String(passwordEnabled)],
|
|
87
|
+
]
|
|
88
|
+
if (oidc === undefined) return switches
|
|
89
|
+
return [...switches, ['OIDC_ISSUER', oidc.issuer], ['OIDC_CLIENT_ID', oidc.clientId], [IAM_OIDC_CLIENT_SECRET_VARIABLE, oidc.clientSecret]]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Compared byte-for-byte against a token's `iss`, so it is the PUBLIC issuer, never the private upstream. */
|
|
93
|
+
const proxySessionOwner = (input: PlatformPlacementInput): string =>
|
|
94
|
+
JSON.stringify({ issuer: platformOrigin(input.scheme, input.hosts.iam), upstream: input.iamUpstream })
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* EVERY fabrika variable a bring-up places, per service.
|
|
98
|
+
*
|
|
99
|
+
* The proxy manifest is deliberately absent: the deploy owns it, in the same run as the code it
|
|
100
|
+
* describes (ADR-0027).
|
|
101
|
+
*/
|
|
102
|
+
export const installationServiceEnvironment = (input: InstallationServiceInput): PlatformServiceEnvironment => {
|
|
103
|
+
const { secrets, data } = input
|
|
104
|
+
const iamOrigin = platformOrigin(input.scheme, input.hosts.iam)
|
|
105
|
+
const operationsOrigin = platformOrigin(input.scheme, input.hosts.operations)
|
|
106
|
+
const email = input.email.kind === 'none' ? [] : [...input.email.variables]
|
|
107
|
+
return {
|
|
108
|
+
iam: new Map([
|
|
109
|
+
['FABRIKA_IAM_DATABASE_URL', data.iamDatabase],
|
|
110
|
+
['ENVIRONMENT', input.environment],
|
|
111
|
+
['ISSUER', iamOrigin],
|
|
112
|
+
...iamAuthentication(input.authentication),
|
|
113
|
+
['FABRIKA_IAM_SIGNING_KEYS', secrets.signingKeys],
|
|
114
|
+
['FABRIKA_IAM_RPC_KEY', secrets.rpcKey],
|
|
115
|
+
['FABRIKA_IAM_PROXY_KEY', secrets.proxyKey],
|
|
116
|
+
['FABRIKA_IAM_PROVISIONING_KEY', secrets.provisioningKey],
|
|
117
|
+
...email,
|
|
118
|
+
]),
|
|
119
|
+
operations: new Map([
|
|
120
|
+
['FABRIKA_OPERATIONS_DATABASE_URL', data.operationsDatabase],
|
|
121
|
+
['FABRIKA_OPERATIONS_BLOB_BUCKET', data.operationsBlobs.bucket],
|
|
122
|
+
['FABRIKA_OPERATIONS_BLOB_ACCESS_KEY_ID', data.operationsBlobs.accessKeyId],
|
|
123
|
+
['FABRIKA_OPERATIONS_BLOB_SECRET_ACCESS_KEY', data.operationsBlobs.secretAccessKey],
|
|
124
|
+
['FABRIKA_OPERATIONS_BLOB_ENDPOINT', data.operationsBlobs.endpoint],
|
|
125
|
+
['FABRIKA_OPERATIONS_PUBLIC_HOST', input.hosts.operations],
|
|
126
|
+
['ENVIRONMENT', input.environment],
|
|
127
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
128
|
+
['OPERATIONS_SYNC_KEY', secrets.operationsSyncKey],
|
|
129
|
+
['FABRIKA_IAM_RPC_KEY', secrets.rpcKey],
|
|
130
|
+
]),
|
|
131
|
+
control: new Map([
|
|
132
|
+
['FABRIKA_CONTROL_DATABASE_URL', data.controlDatabase],
|
|
133
|
+
['ENVIRONMENT', input.environment],
|
|
134
|
+
// A bare HOST, not an origin: `controlPublicOrigin` accepts either.
|
|
135
|
+
['FABRIKA_CONTROL_DOMAIN', input.hosts.control],
|
|
136
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
137
|
+
['OPERATIONS_ARTIFACT_ORIGIN', operationsOrigin],
|
|
138
|
+
['FABRIKA_CONTROL_RUN_LOGS_BUCKET', data.runLogs.bucket],
|
|
139
|
+
['FABRIKA_CONTROL_RUN_LOGS_ACCESS_KEY_ID', data.runLogs.accessKeyId],
|
|
140
|
+
['FABRIKA_CONTROL_RUN_LOGS_SECRET_ACCESS_KEY', data.runLogs.secretAccessKey],
|
|
141
|
+
['FABRIKA_CONTROL_RUN_LOGS_ENDPOINT', data.runLogs.endpoint],
|
|
142
|
+
['FABRIKA_IAM_RPC_KEY', secrets.rpcKey],
|
|
143
|
+
['OPERATIONS_SYNC_KEY', secrets.operationsSyncKey],
|
|
144
|
+
['FABRIKA_CONTROL_VAULT_KEY', secrets.vaultKey],
|
|
145
|
+
['FABRIKA_IAM_PROVISIONING_KEY', secrets.provisioningKey],
|
|
146
|
+
...email,
|
|
147
|
+
]),
|
|
148
|
+
proxy: new Map([
|
|
149
|
+
['ENVIRONMENT', input.environment],
|
|
150
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
151
|
+
['FABRIKA_IAM_KEY', secrets.proxyKey],
|
|
152
|
+
['FABRIKA_PROXY_SESSION_OWNER_JSON', proxySessionOwner(input)],
|
|
153
|
+
]),
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The fabrika variables whose VALUE a bring-up generates, per service.
|
|
159
|
+
*
|
|
160
|
+
* Their presence on a live service is what "this project already holds an installation" means, and they
|
|
161
|
+
* are written as credentials. A provider adds the names of its own that carry a generated value.
|
|
162
|
+
*/
|
|
163
|
+
export const INSTALLATION_SECRET_VARIABLES: Readonly<Record<PlatformService, readonly string[]>> = {
|
|
164
|
+
iam: ['FABRIKA_IAM_SIGNING_KEYS', 'FABRIKA_IAM_RPC_KEY', 'FABRIKA_IAM_PROXY_KEY', 'FABRIKA_IAM_PROVISIONING_KEY'],
|
|
165
|
+
operations: ['OPERATIONS_SYNC_KEY', 'FABRIKA_IAM_RPC_KEY'],
|
|
166
|
+
control: ['FABRIKA_IAM_RPC_KEY', 'OPERATIONS_SYNC_KEY', 'FABRIKA_CONTROL_VAULT_KEY', 'FABRIKA_IAM_PROVISIONING_KEY'],
|
|
167
|
+
proxy: ['FABRIKA_IAM_KEY'],
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The secret the platform proxy sends IAM's route, so IAM honours the request id only from the proxy
|
|
172
|
+
* (ADR-0022 rule 5). Derived from the provisioning key, which every deploy holds, so each deploy writes the same value
|
|
173
|
+
* and nothing stores or reads one back. Never from the proxy key: namespace proxies hold that one.
|
|
174
|
+
*/
|
|
175
|
+
export const deriveIamProxySecret = (provisioningKey: string): string =>
|
|
176
|
+
createHmac('sha256', provisioningKey).update('fabrika-installation-proxy-secret:iam').digest('hex')
|
|
177
|
+
|
|
178
|
+
/** What a deploy derives the service environment from. */
|
|
179
|
+
export interface DeployedServiceInput extends PlatformPlacementInput {
|
|
180
|
+
/** `FABRIKA_IAM_PROVISIONING_KEY`, which the IAM proxy secret is derived from. */
|
|
181
|
+
readonly provisioningKey: string
|
|
182
|
+
/** The digest of the console table the deploy's manifest publishes (`platformServiceTableDigest`). */
|
|
183
|
+
readonly controlTableDigest: string
|
|
184
|
+
/** Present only when the installation enables MCP authoring. */
|
|
185
|
+
readonly oauthConfig?: IamOAuthConfig
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The per-installation configuration a deploy writes, derived from where the installation answers.
|
|
190
|
+
*
|
|
191
|
+
* IAM's OAuth configuration is present only when MCP authoring is enabled; without it the variable is left
|
|
192
|
+
* as it is, and the manifest — whose `/mcp` row then admits no OAuth token — is what turns MCP off.
|
|
193
|
+
*/
|
|
194
|
+
export const deployedServiceEnvironment = (input: DeployedServiceInput): PlatformServiceEnvironment => {
|
|
195
|
+
const iamOrigin = platformOrigin(input.scheme, input.hosts.iam)
|
|
196
|
+
const iamProxySecret = deriveIamProxySecret(input.provisioningKey)
|
|
197
|
+
const iam = new Map([
|
|
198
|
+
['ENVIRONMENT', input.environment],
|
|
199
|
+
// The `iss` of every minted token AND the OIDC redirect base, so it must be the address
|
|
200
|
+
// routed to the proxy in front of this service.
|
|
201
|
+
['ISSUER', iamOrigin],
|
|
202
|
+
[IAM_PROXY_SECRET_VARIABLE, iamProxySecret],
|
|
203
|
+
])
|
|
204
|
+
if (input.oauthConfig !== undefined) {
|
|
205
|
+
iam.set(IAM_OAUTH_CONFIG_VARIABLE, JSON.stringify(input.oauthConfig))
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
iam,
|
|
209
|
+
operations: new Map([
|
|
210
|
+
['ENVIRONMENT', input.environment],
|
|
211
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
212
|
+
['FABRIKA_OPERATIONS_PUBLIC_HOST', input.hosts.operations],
|
|
213
|
+
]),
|
|
214
|
+
proxy: new Map([
|
|
215
|
+
['ENVIRONMENT', input.environment],
|
|
216
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
217
|
+
['FABRIKA_PROXY_SESSION_OWNER_JSON', proxySessionOwner(input)],
|
|
218
|
+
// Caddy sends it on the session owner's route.
|
|
219
|
+
[IAM_PROXY_SECRET_VARIABLE, iamProxySecret],
|
|
220
|
+
]),
|
|
221
|
+
control: new Map([
|
|
222
|
+
['ENVIRONMENT', input.environment],
|
|
223
|
+
['FABRIKA_IAM_ISSUER', iamOrigin],
|
|
224
|
+
// A bare host: `controlPublicOrigin` accepts either spelling.
|
|
225
|
+
['FABRIKA_CONTROL_DOMAIN', input.hosts.control],
|
|
226
|
+
// Control picks its own table, MCP or not, by this digest, and refuses to start under another.
|
|
227
|
+
[FABRIKA_SERVICE_TABLE_DIGEST, input.controlTableDigest],
|
|
228
|
+
['OPERATIONS_ARTIFACT_ORIGIN', platformOrigin(input.scheme, input.hosts.operations)],
|
|
229
|
+
]),
|
|
230
|
+
}
|
|
231
|
+
}
|
package/types/src/admin.d.ts
CHANGED
|
@@ -58,7 +58,10 @@ export interface FirstAdministratorRequest {
|
|
|
58
58
|
*/
|
|
59
59
|
readonly reissueEnrollment?: boolean;
|
|
60
60
|
}
|
|
61
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* What happened to the password enrollment. Only `issued` carries a URL, and only once. `single-sign-on`: the
|
|
63
|
+
* installation offers no password, so none was issued, and the administrator signs in through the identity provider.
|
|
64
|
+
*/
|
|
62
65
|
export type EnrollmentOutcome = {
|
|
63
66
|
readonly state: 'issued';
|
|
64
67
|
readonly url: string;
|
|
@@ -71,6 +74,8 @@ export type EnrollmentOutcome = {
|
|
|
71
74
|
readonly state: 'outstanding';
|
|
72
75
|
} | {
|
|
73
76
|
readonly state: 'already-set';
|
|
77
|
+
} | {
|
|
78
|
+
readonly state: 'single-sign-on';
|
|
74
79
|
};
|
|
75
80
|
export interface FirstAdministratorResult {
|
|
76
81
|
readonly principalId: string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type CommandEnv } from './options';
|
|
2
|
+
export declare const AUTH_METHODS_FLAG = "--auth-methods";
|
|
3
|
+
export declare const OIDC_ISSUER_FLAG = "--oidc-issuer";
|
|
4
|
+
export declare const OIDC_CLIENT_ID_FLAG = "--oidc-client-id";
|
|
5
|
+
/** The installer's input names. IAM's own names differ per runtime; the provider maps them. */
|
|
6
|
+
export declare const OIDC_ISSUER_INPUT = "FABRIKA_IAM_OIDC_ISSUER";
|
|
7
|
+
export declare const OIDC_CLIENT_ID_INPUT = "FABRIKA_IAM_OIDC_CLIENT_ID";
|
|
8
|
+
/** A credential: environment only, and no flag. */
|
|
9
|
+
export declare const OIDC_CLIENT_SECRET_INPUT = "FABRIKA_IAM_OIDC_CLIENT_SECRET";
|
|
10
|
+
/** At least one method: IAM refuses to boot with neither, so neither is not a value. */
|
|
11
|
+
export type AuthMethods = {
|
|
12
|
+
readonly oidcEnabled: true;
|
|
13
|
+
readonly passwordEnabled: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
readonly oidcEnabled: false;
|
|
16
|
+
readonly passwordEnabled: true;
|
|
17
|
+
};
|
|
18
|
+
/** The OIDC client IAM signs people in with. */
|
|
19
|
+
export interface OidcClient {
|
|
20
|
+
readonly issuer: string;
|
|
21
|
+
readonly clientId: string;
|
|
22
|
+
/** Never logged, never printed. */
|
|
23
|
+
readonly clientSecret: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* `--auth-methods`, or the pair of switches the environment carries.
|
|
27
|
+
*
|
|
28
|
+
* With neither, `fallback` when the provider has one, and an error naming both ways in when it has none.
|
|
29
|
+
*/
|
|
30
|
+
export declare const readAuthMethods: (argv: readonly string[], env: CommandEnv, fallback?: AuthMethods) => AuthMethods;
|
|
31
|
+
/** An issuer IAM can discover `/.well-known/openid-configuration` under. The value is a public URL, so it is quoted. */
|
|
32
|
+
export declare const checkedOidcIssuer: (issuer: string) => string;
|
|
33
|
+
/** A flag that only means something under a selection the run did not make is refused, not ignored. */
|
|
34
|
+
export declare const refuseFlags: (argv: readonly string[], flags: readonly string[], why: string) => void;
|
|
@@ -29,6 +29,11 @@ export interface EnvironmentEffects {
|
|
|
29
29
|
}
|
|
30
30
|
/** Create the GitHub Environment and populate its secrets + variables. */
|
|
31
31
|
export declare function configureEnvironment(config: EnvironmentConfig, effects?: EnvironmentEffects): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* The variables an Environment already holds, or null when the repository or the Environment does not exist. Any other
|
|
34
|
+
* failure throws: a caller decides from the answer whether an installation already exists, and must not guess.
|
|
35
|
+
*/
|
|
36
|
+
export declare function environmentVariables(repo: string, environment: string, gh?: GhAttempt): Promise<Readonly<Record<string, string>> | null>;
|
|
32
37
|
/** One reviewer in a PUT body: GitHub's GET nests the id under `reviewer`, its PUT wants it flat. */
|
|
33
38
|
interface EnvironmentReviewer {
|
|
34
39
|
readonly type: string;
|