@di-framework/cli-plugin-platform 6.1.0 → 6.1.2
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 +127 -2
- package/dist/assets/http-adapter.js +2 -5
- package/dist/assets/sqlite/BUILD-INFO.txt +1 -1
- package/dist/assets/wit/deps/di-framework-pglite/package.wit +175 -0
- package/dist/bindings.js +7 -6
- package/dist/build.d.ts +8 -0
- package/dist/build.js +56 -4
- package/dist/console/cluster.d.ts +3 -0
- package/dist/console/cluster.js +57 -4
- package/dist/deploy.d.ts +6 -0
- package/dist/deploy.js +23 -5
- package/dist/deps.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/node-compat/console.js +16 -1
- package/dist/node-compat/crypto-subtle.d.ts +6 -0
- package/dist/node-compat/crypto-subtle.js +41 -4
- package/dist/node-compat/crypto.d.ts +3 -2
- package/dist/node-compat/crypto.js +66 -17
- package/dist/node-compat/fs.d.ts +24 -2
- package/dist/node-compat/fs.js +137 -15
- package/dist/node-compat/node-errors.d.ts +20 -0
- package/dist/node-compat/node-errors.js +73 -0
- package/dist/node-compat/storage.d.ts +3 -3
- package/dist/node-compat/wasi-random.d.ts +21 -1
- package/dist/node-compat/wasi-random.js +45 -12
- package/dist/pglite.d.ts +106 -0
- package/dist/pglite.js +347 -0
- package/dist/project.d.ts +11 -0
- package/dist/project.js +14 -1
- package/dist/secret-admission.d.ts +21 -0
- package/dist/secret-admission.js +29 -0
- package/dist/wit.d.ts +3 -0
- package/dist/wit.js +12 -0
- package/dist/workload.d.ts +29 -0
- package/dist/workload.js +157 -39
- package/package.json +2 -2
package/dist/project.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type PgliteConfig } from './pglite.js';
|
|
1
2
|
import { type WorkloadEntry } from './workload-members.js';
|
|
2
3
|
export declare const CONFIG_FILE_NAME = "di-framework.config.json";
|
|
3
4
|
export type WasmcloudProject = {
|
|
@@ -36,9 +37,19 @@ export type WasmcloudProject = {
|
|
|
36
37
|
* `di-framework.dev/logs: "false"` WorkloadDeployment annotation. Absent means on.
|
|
37
38
|
*/
|
|
38
39
|
logs?: false;
|
|
40
|
+
/**
|
|
41
|
+
* Embedded PostgreSQL provider composed into the guest, from `"pglite"`. The deployed
|
|
42
|
+
* component is exclusive and gets persistent storage at `storageMount`.
|
|
43
|
+
*/
|
|
44
|
+
pglite?: PgliteConfig;
|
|
39
45
|
};
|
|
40
46
|
export declare function findUp(startDirectory: string, fileName: string): string | undefined;
|
|
41
47
|
/** Paths from the config must stay inside the project root. */
|
|
42
48
|
export declare function resolveInside(projectRoot: string, value: string, label: string, configPath: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* {@link resolveInside}, then the same check on the resolved real path, so a symlink inside
|
|
51
|
+
* the project cannot point outside it. A missing file is left for the caller to report.
|
|
52
|
+
*/
|
|
53
|
+
export declare function resolveRealInside(projectRoot: string, value: string, label: string, configPath: string): string;
|
|
43
54
|
export declare function asWitIdentifier(value: string): string;
|
|
44
55
|
export declare function loadProject(startDirectory: string): WasmcloudProject;
|
package/dist/project.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { accessSync, constants, existsSync, readFileSync } from 'node:fs';
|
|
1
|
+
import { accessSync, constants, existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
2
2
|
import { isIP } from 'node:net';
|
|
3
3
|
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
4
4
|
import { CommandFailure } from '@di-framework/cli-extension';
|
|
5
|
+
import { parsePgliteConfig } from './pglite.js';
|
|
5
6
|
import { discoverWorkloadEntry } from './workload-members.js';
|
|
6
7
|
export const CONFIG_FILE_NAME = 'di-framework.config.json';
|
|
7
8
|
export function findUp(startDirectory, fileName) {
|
|
@@ -31,6 +32,16 @@ export function resolveInside(projectRoot, value, label, configPath) {
|
|
|
31
32
|
}
|
|
32
33
|
return absolutePath;
|
|
33
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* {@link resolveInside}, then the same check on the resolved real path, so a symlink inside
|
|
37
|
+
* the project cannot point outside it. A missing file is left for the caller to report.
|
|
38
|
+
*/
|
|
39
|
+
export function resolveRealInside(projectRoot, value, label, configPath) {
|
|
40
|
+
const absolutePath = resolveInside(projectRoot, value, label, configPath);
|
|
41
|
+
if (existsSync(absolutePath))
|
|
42
|
+
resolveInside(realpathSync(projectRoot), realpathSync(absolutePath), label, configPath);
|
|
43
|
+
return absolutePath;
|
|
44
|
+
}
|
|
34
45
|
export function asWitIdentifier(value) {
|
|
35
46
|
const collapsed = value.toLowerCase().replace(/[^a-z0-9-]+/g, '-');
|
|
36
47
|
let start = 0;
|
|
@@ -111,6 +122,7 @@ export function loadProject(startDirectory) {
|
|
|
111
122
|
: config.ingress !== false && config.http !== false;
|
|
112
123
|
const cronMode = config.cronMode === 'external' ? 'external' : undefined;
|
|
113
124
|
const persistentStorage = config.persistentStorage === true;
|
|
125
|
+
const pglite = parsePgliteConfig(config.pglite, configPath, (path) => resolveRealInside(projectRoot, path, 'pglite.wasm', configPath));
|
|
114
126
|
return {
|
|
115
127
|
applicationName: config.name,
|
|
116
128
|
...(workload ? { workload, workloadEntry } : {}),
|
|
@@ -118,6 +130,7 @@ export function loadProject(startDirectory) {
|
|
|
118
130
|
...(cronMode ? { cronMode } : {}),
|
|
119
131
|
...(persistentStorage ? { persistentStorage: true } : {}),
|
|
120
132
|
...(config.logs === false ? { logs: false } : {}),
|
|
133
|
+
...(pglite ? { pglite } : {}),
|
|
121
134
|
...(config.allowedIpNameLookups === undefined
|
|
122
135
|
? {}
|
|
123
136
|
: { allowedIpNameLookups: config.allowedIpNameLookups }),
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Denial messages of the platform's Secret admission policies, matched by substring in kubectl
|
|
3
|
+
* stderr (kubectl prefixes them with the policy and binding names). The source of truth is
|
|
4
|
+
* di-framework/platform `platform/platform/src/tenancy/admission.ts`:
|
|
5
|
+
* `SECRET_UPDATE_KEYS_MESSAGE` and `SECRET_UPDATE_MANAGED_MESSAGE` of the `tenant-secret-update`
|
|
6
|
+
* ValidatingAdmissionPolicy (platform#112, PR #114), and the message of the older
|
|
7
|
+
* `backend-config` policy, which a managed name can trip first. If the platform rewords one,
|
|
8
|
+
* the matching mapping here silently falls through to a generic error, so change both together.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SECRET_UPDATE_KEYS_MESSAGE = "tenant users may update a Secret only if it keeps every existing data key";
|
|
11
|
+
export declare const SECRET_UPDATE_MANAGED_MESSAGE = "tenant users cannot update platform-managed di-binding-*/di-bs-* Secrets";
|
|
12
|
+
export declare const BACKEND_CONFIG_MANAGED_MESSAGE = "di-tenant-stock, di-platform-routes, di-bs-*, and di-binding-* ConfigMaps/Secrets are managed by the platform controller";
|
|
13
|
+
export declare const MANAGED_SECRET_DENIALS: readonly ["tenant users cannot update platform-managed di-binding-*/di-bs-* Secrets", "di-tenant-stock, di-platform-routes, di-bs-*, and di-binding-* ConfigMaps/Secrets are managed by the platform controller"];
|
|
14
|
+
/**
|
|
15
|
+
* The Kubernetes Status reason of a failed kubectl request. kubectl prints most failures as
|
|
16
|
+
* `Error from server (<Reason>): …`, but an `Invalid` (422) one as
|
|
17
|
+
* `The <Kind> "<name>" is invalid: …` or `The request is invalid: …`. Matching the reason
|
|
18
|
+
* rather than free text keeps a resource name such as `notfound-creds` from changing how an
|
|
19
|
+
* error is classified.
|
|
20
|
+
*/
|
|
21
|
+
export declare function kubectlStatusReason(stderr: string): string | undefined;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Denial messages of the platform's Secret admission policies, matched by substring in kubectl
|
|
3
|
+
* stderr (kubectl prefixes them with the policy and binding names). The source of truth is
|
|
4
|
+
* di-framework/platform `platform/platform/src/tenancy/admission.ts`:
|
|
5
|
+
* `SECRET_UPDATE_KEYS_MESSAGE` and `SECRET_UPDATE_MANAGED_MESSAGE` of the `tenant-secret-update`
|
|
6
|
+
* ValidatingAdmissionPolicy (platform#112, PR #114), and the message of the older
|
|
7
|
+
* `backend-config` policy, which a managed name can trip first. If the platform rewords one,
|
|
8
|
+
* the matching mapping here silently falls through to a generic error, so change both together.
|
|
9
|
+
*/
|
|
10
|
+
export const SECRET_UPDATE_KEYS_MESSAGE = 'tenant users may update a Secret only if it keeps every existing data key';
|
|
11
|
+
export const SECRET_UPDATE_MANAGED_MESSAGE = 'tenant users cannot update platform-managed di-binding-*/di-bs-* Secrets';
|
|
12
|
+
export const BACKEND_CONFIG_MANAGED_MESSAGE = 'di-tenant-stock, di-platform-routes, di-bs-*, and di-binding-* ConfigMaps/Secrets are managed by the platform controller';
|
|
13
|
+
export const MANAGED_SECRET_DENIALS = [
|
|
14
|
+
SECRET_UPDATE_MANAGED_MESSAGE,
|
|
15
|
+
BACKEND_CONFIG_MANAGED_MESSAGE,
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* The Kubernetes Status reason of a failed kubectl request. kubectl prints most failures as
|
|
19
|
+
* `Error from server (<Reason>): …`, but an `Invalid` (422) one as
|
|
20
|
+
* `The <Kind> "<name>" is invalid: …` or `The request is invalid: …`. Matching the reason
|
|
21
|
+
* rather than free text keeps a resource name such as `notfound-creds` from changing how an
|
|
22
|
+
* error is classified.
|
|
23
|
+
*/
|
|
24
|
+
export function kubectlStatusReason(stderr) {
|
|
25
|
+
const reason = /^Error from server \(([A-Za-z]+)\)/m.exec(stderr)?.[1];
|
|
26
|
+
if (reason !== undefined)
|
|
27
|
+
return reason;
|
|
28
|
+
return /^The (?:request|[A-Za-z]+ "[^"]*") is invalid: /m.test(stderr) ? 'Invalid' : undefined;
|
|
29
|
+
}
|
package/dist/wit.d.ts
CHANGED
|
@@ -24,6 +24,9 @@ export declare const DI_SQLITE_VERSION = "0.1.0";
|
|
|
24
24
|
export declare const DI_SQLITE_INTERFACE = "database";
|
|
25
25
|
export declare const DI_SQLITE_TYPES_INTERFACE = "types";
|
|
26
26
|
export declare const SQLITE_SOURCE = "sqlite-backend";
|
|
27
|
+
export declare const DI_PGLITE_PACKAGE = "di-framework:pglite";
|
|
28
|
+
export declare const DI_PGLITE_VERSION = "0.2.0";
|
|
29
|
+
export declare const PGLITE_SOURCE = "pglite-backend";
|
|
27
30
|
export type WitDirection = 'import' | 'export';
|
|
28
31
|
/**
|
|
29
32
|
* One adapter or binding's WIT needs. A requirement is an interface group:
|
package/dist/wit.js
CHANGED
|
@@ -27,6 +27,9 @@ export const DI_SQLITE_VERSION = '0.1.0';
|
|
|
27
27
|
export const DI_SQLITE_INTERFACE = 'database';
|
|
28
28
|
export const DI_SQLITE_TYPES_INTERFACE = 'types';
|
|
29
29
|
export const SQLITE_SOURCE = 'sqlite-backend';
|
|
30
|
+
export const DI_PGLITE_PACKAGE = 'di-framework:pglite';
|
|
31
|
+
export const DI_PGLITE_VERSION = '0.2.0';
|
|
32
|
+
export const PGLITE_SOURCE = 'pglite-backend';
|
|
30
33
|
export const HTTP_ADAPTER_REQUIREMENTS = [
|
|
31
34
|
{
|
|
32
35
|
package: WASI_HTTP_PACKAGE,
|
|
@@ -148,6 +151,15 @@ export function runtimeRequirementsFromJavaScript(source) {
|
|
|
148
151
|
source: SQLITE_SOURCE,
|
|
149
152
|
});
|
|
150
153
|
}
|
|
154
|
+
if (source.includes(`${DI_PGLITE_PACKAGE}/database@${DI_PGLITE_VERSION}`)) {
|
|
155
|
+
requirements.push({
|
|
156
|
+
package: DI_PGLITE_PACKAGE,
|
|
157
|
+
version: DI_PGLITE_VERSION,
|
|
158
|
+
interfaces: ['types', 'database'],
|
|
159
|
+
direction: 'import',
|
|
160
|
+
source: PGLITE_SOURCE,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
151
163
|
return requirements;
|
|
152
164
|
}
|
|
153
165
|
/** Guest console lines; the host binds its logging plugin only when this is declared. */
|
package/dist/workload.d.ts
CHANGED
|
@@ -80,10 +80,39 @@ export interface WorkloadManifestOptions {
|
|
|
80
80
|
};
|
|
81
81
|
/** Secret name providing control-plane credentials (merged into WASI environment). */
|
|
82
82
|
controlSecretName?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Digest of the control token written on this deploy. Rendered as
|
|
85
|
+
* DI_CONTROL_TOKEN_REVISION so a rotated token changes the template and rolls the workload.
|
|
86
|
+
*/
|
|
87
|
+
controlTokenRevision?: string;
|
|
83
88
|
/** Explicit environment config values (string map for localResources.environment.config). */
|
|
84
89
|
environment?: Record<string, string>;
|
|
85
90
|
}
|
|
86
91
|
export declare function renderWorkloadManifest(project: WasmcloudProject, connection: ClusterConnection, image: string, requirements?: readonly WitRequirement[], bindings?: readonly BindingRecord[], options?: WorkloadManifestOptions | boolean, cronJobs?: readonly DiscoveredCronJob[], queueHandlers?: readonly DiscoveredQueueHandler[]): string;
|
|
92
|
+
/** Appended to a deploy failure that happened after the control Secret was rewritten. */
|
|
93
|
+
export declare const CONTROL_TOKEN_ROTATED_NOTICE = "the control token was rotated; re-run deploy to roll the workload onto it";
|
|
94
|
+
/** Control Secret written on deploy: its name and a digest that identifies the token. */
|
|
95
|
+
export type ControlSecretWrite = {
|
|
96
|
+
name: string;
|
|
97
|
+
revision: string;
|
|
98
|
+
};
|
|
99
|
+
/** The full control Secret the CLI owns; nothing in it is read back from the cluster. */
|
|
100
|
+
export declare function controlSecretDocument(workloadName: string, namespace: string, token: string): Record<string, unknown>;
|
|
101
|
+
/**
|
|
102
|
+
* Tenant developers may create, update and delete Secrets but never get, list, watch or
|
|
103
|
+
* patch them (platform#112). The control Secret is therefore written blind: POST the full
|
|
104
|
+
* document and, if it already exists, PUT the same document without a resourceVersion. A
|
|
105
|
+
* Secret allows such an unconditional update, so no read is needed to learn the version,
|
|
106
|
+
* and the Secret never disappears the way delete-then-create would make it. Both requests
|
|
107
|
+
* go through `--raw` because `kubectl replace -f` GETs the object first to fill in its
|
|
108
|
+
* resourceVersion. The token is regenerated on every deploy since the old one cannot be read.
|
|
109
|
+
*/
|
|
110
|
+
export declare function writeControlSecret(project: WasmcloudProject, connection: ClusterConnection, deps: WasmcloudDeps): Promise<ControlSecretWrite>;
|
|
111
|
+
/**
|
|
112
|
+
* Deletes the control Secret by name. A label-selected delete would LIST Secrets and the
|
|
113
|
+
* default `--wait=true` would GET it afterwards, both forbidden to tenant developers.
|
|
114
|
+
*/
|
|
115
|
+
export declare function deleteControlSecret(project: WasmcloudProject, connection: ClusterConnection, deps: WasmcloudDeps): Promise<void>;
|
|
87
116
|
export declare function applyWorkload(project: WasmcloudProject, connection: ClusterConnection, image: string, io: CliIo, deps: WasmcloudDeps): Promise<string>;
|
|
88
117
|
export declare function deleteWorkload(project: WasmcloudProject, connection: ClusterConnection, io: CliIo, deps: WasmcloudDeps): Promise<void>;
|
|
89
118
|
export declare function waitForReady(project: WasmcloudProject, connection: ClusterConnection, deps: WasmcloudDeps, io?: CliIo): Promise<void>;
|
package/dist/workload.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
3
4
|
import { join } from 'node:path';
|
|
4
|
-
import { CommandFailure } from '@di-framework/cli-extension';
|
|
5
|
+
import { CommandFailure, isCommandFailure, } from '@di-framework/cli-extension';
|
|
5
6
|
import { discoverActors } from './actors.js';
|
|
6
7
|
import { discoverBindings, requirementsFromBindings } from './bindings.js';
|
|
8
|
+
import { sanitizePublicText } from './console/errors.js';
|
|
7
9
|
import { discoverScheduledJobs } from './cron.js';
|
|
8
10
|
import { applyWorkloadEgress, removeWorkloadEgress, reportEgressStatus, usesPlatformEgress, warnIfTenantHostLacksTls, } from './egress.js';
|
|
9
11
|
import { hostInterfacesFromRequirements, renderHostInterfacesYaml } from './host-interface.js';
|
|
10
12
|
import { captureKubectl, runKubectl } from './kubernetes.js';
|
|
11
13
|
import { applyManagedBindings, cleanupManagedBindings } from './managed-bindings.js';
|
|
14
|
+
import { EXCLUSIVE_INSTANCE_KEY, isWithin, PGLITE_DATA_DIR_ENV, warnIfHostIgnoresExclusive, } from './pglite.js';
|
|
12
15
|
import { asWitIdentifier } from './project.js';
|
|
13
16
|
import { discoverQueueHandlers, isQueueWorkerProject } from './queues.js';
|
|
17
|
+
import { kubectlStatusReason, SECRET_UPDATE_KEYS_MESSAGE } from './secret-admission.js';
|
|
14
18
|
import { defaultProjectRequirements, guestLoggingRequirement, queueProjectRequirements, } from './wit.js';
|
|
15
19
|
export const MANAGED_BY_LABEL = 'di-framework';
|
|
16
20
|
export const WAIT_ATTEMPTS = 30;
|
|
@@ -83,8 +87,11 @@ export function renderWorkloadManifest(project, connection, image, requirements
|
|
|
83
87
|
const resolvedHandlers = queueHandlers.length > 0 ? queueHandlers : discoverQueueHandlers(project);
|
|
84
88
|
const isWorker = isQueueWorkerProject(project, resolvedHandlers);
|
|
85
89
|
const hasQueues = resolvedHandlers.length > 0;
|
|
86
|
-
const
|
|
87
|
-
|
|
90
|
+
const pglite = project.pglite;
|
|
91
|
+
// An embedded PostgreSQL data directory needs persistent storage whatever the caller says.
|
|
92
|
+
const needsPersistentStorage = pglite !== undefined ||
|
|
93
|
+
(opts.hasPersistentStorage ??
|
|
94
|
+
(hasActors || hasQueues || isWorker || project.persistentStorage === true));
|
|
88
95
|
const needsControlHttp = hasActors || cronJobs.length > 0 || hasQueues || isWorker;
|
|
89
96
|
const publicIngress = project.ingress !== false && !isWorker;
|
|
90
97
|
// Cluster Service is required for cron invokers and queue/actor control even when
|
|
@@ -92,7 +99,9 @@ export function renderWorkloadManifest(project, connection, image, requirements
|
|
|
92
99
|
const hasHttp = publicIngress || needsControlHttp || needsPersistentStorage;
|
|
93
100
|
if (needsPersistentStorage) {
|
|
94
101
|
if (opts.replicas !== undefined && opts.replicas !== 1) {
|
|
95
|
-
throw new CommandFailure('WASMCLOUD_STORAGE_REPLICA_CONSTRAINT',
|
|
102
|
+
throw new CommandFailure('WASMCLOUD_STORAGE_REPLICA_CONSTRAINT', pglite === undefined
|
|
103
|
+
? 'SQLite-backed workloads require replicas: 1 because the WASI VFS lacks file locking'
|
|
104
|
+
: 'PGlite workloads require replicas: 1 because each database directory allows one opener', 2, { replicas: opts.replicas });
|
|
96
105
|
}
|
|
97
106
|
}
|
|
98
107
|
const name = deploymentResourceName(project);
|
|
@@ -104,7 +113,11 @@ export function renderWorkloadManifest(project, connection, image, requirements
|
|
|
104
113
|
].join('\n');
|
|
105
114
|
const volumeName = opts.storageVolume?.volumeName ?? 'app-storage';
|
|
106
115
|
const mountPath = opts.storageVolume?.mountPath ??
|
|
116
|
+
pglite?.storageMount ??
|
|
107
117
|
(hasActors ? `${DEFAULT_STORAGE_MOUNT}/actors` : DEFAULT_STORAGE_MOUNT);
|
|
118
|
+
if (pglite !== undefined && !isWithin(pglite.dataDir, mountPath)) {
|
|
119
|
+
throw new CommandFailure('WASMCLOUD_PGLITE_STORAGE_MISMATCH', `pglite.dataDir ${pglite.dataDir} is outside the storage mount ${mountPath}`, 2, { dataDir: pglite.dataDir, mountPath });
|
|
120
|
+
}
|
|
108
121
|
const hostPath = opts.storageVolume?.hostPath ?? hostStoragePath(storageDirectoryName(project));
|
|
109
122
|
const platformStorage = needsPersistentStorage && usesPlatformStorage(connection);
|
|
110
123
|
if (platformStorage && !PLATFORM_STORAGE_MOUNTS.includes(mountPath)) {
|
|
@@ -125,6 +138,8 @@ export function renderWorkloadManifest(project, connection, image, requirements
|
|
|
125
138
|
}
|
|
126
139
|
if (hasActors)
|
|
127
140
|
environment.ACTOR_STORAGE_DIR = mountPath;
|
|
141
|
+
if (pglite !== undefined)
|
|
142
|
+
environment[PGLITE_DATA_DIR_ENV] = pglite.dataDir;
|
|
128
143
|
if (needsPersistentStorage && !hasActors) {
|
|
129
144
|
environment.QUEUE_DB_PATH = `${mountPath}/queue.db`;
|
|
130
145
|
environment.MIGRATION_DB_PATH = `${mountPath}/migrations.db`;
|
|
@@ -132,6 +147,9 @@ export function renderWorkloadManifest(project, connection, image, requirements
|
|
|
132
147
|
const controlSecretName = opts.controlSecretName ?? (hasHttp ? controlSecretResourceName(name) : undefined);
|
|
133
148
|
const clusterHttpHost = `${name}.${connection.namespace}.svc.cluster.local`;
|
|
134
149
|
const advertisedHttpHost = publicIngress ? project.applicationName : clusterHttpHost;
|
|
150
|
+
if (controlSecretName !== undefined && opts.controlTokenRevision !== undefined) {
|
|
151
|
+
environment.DI_CONTROL_TOKEN_REVISION = opts.controlTokenRevision;
|
|
152
|
+
}
|
|
135
153
|
if (hasHttp) {
|
|
136
154
|
environment.DI_CONTROL_REJECT_FORWARDED = '1';
|
|
137
155
|
environment.DI_CONTROL_HTTP_HOST = [name, clusterHttpHost].join(',');
|
|
@@ -194,6 +212,11 @@ spec:
|
|
|
194
212
|
else if (ipNameLookups !== undefined) {
|
|
195
213
|
localResourcesLines.push(' localResources:');
|
|
196
214
|
}
|
|
215
|
+
// The component itself is the exclusive unit (#140): never the service, which admission
|
|
216
|
+
// rejects, and never a separately linked provider, which the host refuses to load.
|
|
217
|
+
if (pglite !== undefined) {
|
|
218
|
+
localResourcesLines.push(' config:', ` ${EXCLUSIVE_INSTANCE_KEY}: "true"`);
|
|
219
|
+
}
|
|
197
220
|
if (needsPersistentStorage && !platformStorage) {
|
|
198
221
|
localResourcesLines.push(' volumeMounts:');
|
|
199
222
|
localResourcesLines.push(` - name: ${volumeName}`);
|
|
@@ -325,30 +348,96 @@ ${labels.replace(/^/gm, ' ')}
|
|
|
325
348
|
}
|
|
326
349
|
return sections.join('\n---\n') + '\n';
|
|
327
350
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
351
|
+
/** Appended to a deploy failure that happened after the control Secret was rewritten. */
|
|
352
|
+
export const CONTROL_TOKEN_ROTATED_NOTICE = 'the control token was rotated; re-run deploy to roll the workload onto it';
|
|
353
|
+
/** The full control Secret the CLI owns; nothing in it is read back from the cluster. */
|
|
354
|
+
export function controlSecretDocument(workloadName, namespace, token) {
|
|
355
|
+
const encode = (value) => Buffer.from(value, 'utf8').toString('base64');
|
|
356
|
+
return {
|
|
357
|
+
apiVersion: 'v1',
|
|
358
|
+
kind: 'Secret',
|
|
359
|
+
metadata: {
|
|
360
|
+
name: controlSecretResourceName(workloadName),
|
|
361
|
+
namespace,
|
|
362
|
+
labels: {
|
|
363
|
+
'app.kubernetes.io/managed-by': MANAGED_BY_LABEL,
|
|
364
|
+
'app.kubernetes.io/name': workloadName,
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
type: 'Opaque',
|
|
368
|
+
data: {
|
|
369
|
+
DI_CONTROL_TOKEN: encode(token),
|
|
370
|
+
DI_CONTROL_IDENTITY: encode(workloadName),
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function secretsPath(namespace, name) {
|
|
375
|
+
const collection = `/api/v1/namespaces/${encodeURIComponent(namespace)}/secrets`;
|
|
376
|
+
return name === undefined ? collection : `${collection}/${encodeURIComponent(name)}`;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Tenant developers may create, update and delete Secrets but never get, list, watch or
|
|
380
|
+
* patch them (platform#112). The control Secret is therefore written blind: POST the full
|
|
381
|
+
* document and, if it already exists, PUT the same document without a resourceVersion. A
|
|
382
|
+
* Secret allows such an unconditional update, so no read is needed to learn the version,
|
|
383
|
+
* and the Secret never disappears the way delete-then-create would make it. Both requests
|
|
384
|
+
* go through `--raw` because `kubectl replace -f` GETs the object first to fill in its
|
|
385
|
+
* resourceVersion. The token is regenerated on every deploy since the old one cannot be read.
|
|
386
|
+
*/
|
|
387
|
+
export async function writeControlSecret(project, connection, deps) {
|
|
388
|
+
const workloadName = deploymentResourceName(project);
|
|
389
|
+
const name = controlSecretResourceName(workloadName);
|
|
390
|
+
const token = randomBytes(32).toString('base64url');
|
|
391
|
+
const directory = mkdtempSync(join(tmpdir(), 'di-control-secret-'));
|
|
392
|
+
try {
|
|
393
|
+
const path = join(directory, 'secret.json');
|
|
394
|
+
writeFileSync(path, JSON.stringify(controlSecretDocument(workloadName, connection.namespace, token)), { mode: 0o600 });
|
|
395
|
+
const created = await captureKubectl(deps, connection, ['create', '--raw', secretsPath(connection.namespace), '-f', path], project.projectRoot);
|
|
396
|
+
if (created.exitCode !== 0) {
|
|
397
|
+
if (kubectlStatusReason(created.stderr) !== 'AlreadyExists') {
|
|
398
|
+
throw controlSecretWriteFailed('kubectl create', name, connection, created, token);
|
|
399
|
+
}
|
|
400
|
+
const replaced = await captureKubectl(deps, connection, ['replace', '--raw', secretsPath(connection.namespace, name), '-f', path], project.projectRoot);
|
|
401
|
+
if (replaced.exitCode !== 0) {
|
|
402
|
+
throw controlSecretWriteFailed('kubectl replace', name, connection, replaced, token);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
finally {
|
|
407
|
+
rmSync(directory, { recursive: true, force: true });
|
|
342
408
|
}
|
|
409
|
+
return { name, revision: createHash('sha256').update(token).digest('hex').slice(0, 16) };
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* A failed control Secret write, with kubectl's reason. Kubernetes errors for these requests
|
|
413
|
+
* name the object, not its data, but the stderr is still scrubbed of the token (raw and
|
|
414
|
+
* base64) and of credential-shaped text before it is shown, in case a response echoes the body.
|
|
415
|
+
*/
|
|
416
|
+
function controlSecretWriteFailed(command, name, connection, result, token) {
|
|
417
|
+
const stderr = sanitizePublicText([token, Buffer.from(token, 'utf8').toString('base64')]
|
|
418
|
+
.reduce((text, secret) => text.split(secret).join('[redacted]'), result.stderr)
|
|
419
|
+
.trim());
|
|
420
|
+
if (stderr.includes(SECRET_UPDATE_KEYS_MESSAGE)) {
|
|
421
|
+
// The platform's `tenant-secret-update` policy refuses a replace that drops a key, so a
|
|
422
|
+
// control Secret someone added keys to can no longer be rotated in place.
|
|
423
|
+
return new CommandFailure('WASMCLOUD_CONTROL_SECRET_FOREIGN_KEYS', `The control Secret ${name} has keys this CLI didn't write; delete it with ` +
|
|
424
|
+
`\`kubectl delete secret ${name} --namespace ${connection.namespace}\` and redeploy`, 2, { name, namespace: connection.namespace, command, exitCode: result.exitCode, stderr });
|
|
425
|
+
}
|
|
426
|
+
const reason = stderr.length > 0 ? `: ${stderr}` : '';
|
|
427
|
+
return new CommandFailure('WASMCLOUD_TOOL_FAILED', `${command} failed with exit code ${result.exitCode}${reason}`, 3, { command, exitCode: result.exitCode, stderr });
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Deletes the control Secret by name. A label-selected delete would LIST Secrets and the
|
|
431
|
+
* default `--wait=true` would GET it afterwards, both forbidden to tenant developers.
|
|
432
|
+
*/
|
|
433
|
+
export async function deleteControlSecret(project, connection, deps) {
|
|
343
434
|
await runKubectl(deps, connection, [
|
|
344
|
-
'
|
|
435
|
+
'delete',
|
|
345
436
|
'secret',
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
'--overwrite',
|
|
437
|
+
controlSecretResourceName(deploymentResourceName(project)),
|
|
438
|
+
'--ignore-not-found',
|
|
439
|
+
'--wait=false',
|
|
350
440
|
], project.projectRoot);
|
|
351
|
-
return secretName;
|
|
352
441
|
}
|
|
353
442
|
export async function applyWorkload(project, connection, image, io, deps) {
|
|
354
443
|
const bindings = discoverBindings(project, deps);
|
|
@@ -356,7 +445,10 @@ export async function applyWorkload(project, connection, image, io, deps) {
|
|
|
356
445
|
const isWorker = isQueueWorkerProject(project, queueHandlers);
|
|
357
446
|
const hasActors = discoverActors(project).length > 0 || project.actors === true;
|
|
358
447
|
const cronJobs = discoverScheduledJobs(project.projectRoot);
|
|
359
|
-
const hasPersistentStorage = hasActors ||
|
|
448
|
+
const hasPersistentStorage = hasActors ||
|
|
449
|
+
queueHandlers.length > 0 ||
|
|
450
|
+
project.persistentStorage === true ||
|
|
451
|
+
project.pglite !== undefined;
|
|
360
452
|
const needsHttp = (project.ingress !== false && !isWorker) ||
|
|
361
453
|
hasActors ||
|
|
362
454
|
cronJobs.length > 0 ||
|
|
@@ -380,6 +472,7 @@ export async function applyWorkload(project, connection, image, io, deps) {
|
|
|
380
472
|
source: 'workload-service',
|
|
381
473
|
});
|
|
382
474
|
await warnIfTenantHostLacksTls(project, connection, io, deps);
|
|
475
|
+
await warnIfHostIgnoresExclusive(project, connection, io, deps);
|
|
383
476
|
const associations = await applyManagedBindings(project, connection, bindings, deps);
|
|
384
477
|
const egress = await applyWorkloadEgress(project, connection, deps);
|
|
385
478
|
if (egress !== undefined)
|
|
@@ -389,17 +482,28 @@ export async function applyWorkload(project, connection, image, io, deps) {
|
|
|
389
482
|
hasQueues: queueHandlers.length > 0,
|
|
390
483
|
hasPersistentStorage,
|
|
391
484
|
});
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
485
|
+
const controlSecret = needsHttp ? await writeControlSecret(project, connection, deps) : undefined;
|
|
486
|
+
// From here on the cron invokers already use the new token, which the running workload
|
|
487
|
+
// rejects until it rolls out with this manifest.
|
|
488
|
+
let path;
|
|
489
|
+
try {
|
|
490
|
+
const manifest = renderWorkloadManifest(project, connection, image, requirements, bindings, {
|
|
491
|
+
hasActors,
|
|
492
|
+
hasPersistentStorage,
|
|
493
|
+
controlSecretName: controlSecret?.name,
|
|
494
|
+
controlTokenRevision: controlSecret?.revision,
|
|
495
|
+
}, cronJobs, queueHandlers);
|
|
496
|
+
path = generatedManifestPath(project);
|
|
497
|
+
mkdirSync(join(project.projectRoot, '.di-framework', 'deploy'), { recursive: true });
|
|
498
|
+
writeFileSync(path, manifest);
|
|
499
|
+
const name = deploymentResourceName(project);
|
|
500
|
+
io.stdout.write(`Applying WorkloadDeployment ${name} in ${connection.namespace}...\n`);
|
|
501
|
+
await runKubectl(deps, connection, ['apply', '-f', path], project.projectRoot);
|
|
502
|
+
await waitForReady(project, connection, deps, io);
|
|
503
|
+
}
|
|
504
|
+
catch (error) {
|
|
505
|
+
throw controlSecret === undefined ? error : withRotatedTokenNotice(error);
|
|
506
|
+
}
|
|
403
507
|
await cleanupManagedBindings(project, connection, associations, deps);
|
|
404
508
|
if (egress === undefined)
|
|
405
509
|
await removeWorkloadEgress(project, connection, deps);
|
|
@@ -407,6 +511,18 @@ export async function applyWorkload(project, connection, image, io, deps) {
|
|
|
407
511
|
await reportEgressStatus(project, connection, io, deps);
|
|
408
512
|
return path;
|
|
409
513
|
}
|
|
514
|
+
/**
|
|
515
|
+
* A deploy that fails after the control Secret was written leaves the running workload on the
|
|
516
|
+
* old token while its cron invokers (which read the Secret when they start) carry the new one,
|
|
517
|
+
* so every cron invocation is refused until a deploy succeeds. Say so in the failure.
|
|
518
|
+
*/
|
|
519
|
+
function withRotatedTokenNotice(error) {
|
|
520
|
+
if (!isCommandFailure(error))
|
|
521
|
+
return error;
|
|
522
|
+
// Every failure on this path (kubectl, readiness) carries an object of details.
|
|
523
|
+
const details = error.details;
|
|
524
|
+
return new CommandFailure(error.code, `${error.message}; ${CONTROL_TOKEN_ROTATED_NOTICE}`, error.exitCode, { ...details, controlTokenRotated: true });
|
|
525
|
+
}
|
|
410
526
|
async function assertStorageOwnership(project, connection, deps, flags) {
|
|
411
527
|
if (!flags.hasActors && !flags.hasQueues && !flags.hasPersistentStorage)
|
|
412
528
|
return;
|
|
@@ -441,13 +557,15 @@ export async function deleteWorkload(project, connection, io, deps) {
|
|
|
441
557
|
io.stdout.write(`Removing WorkloadDeployment ${name} from ${connection.namespace}...\n`);
|
|
442
558
|
await runKubectl(deps, connection, [
|
|
443
559
|
'delete',
|
|
444
|
-
`${WORKLOAD_DEPLOYMENT_RESOURCE},service,cronjob
|
|
560
|
+
`${WORKLOAD_DEPLOYMENT_RESOURCE},service,cronjob`,
|
|
445
561
|
'-l',
|
|
446
562
|
`app.kubernetes.io/name=${name}`,
|
|
447
563
|
'--ignore-not-found',
|
|
448
564
|
'--wait=true',
|
|
449
565
|
'--timeout=180s',
|
|
450
566
|
], project.projectRoot);
|
|
567
|
+
// After the workload and its cron invokers are gone, so nothing still mounts the token.
|
|
568
|
+
await deleteControlSecret(project, connection, deps);
|
|
451
569
|
await cleanupManagedBindings(project, connection, new Set(), deps);
|
|
452
570
|
await removeWorkloadEgress(project, connection, deps);
|
|
453
571
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@di-framework/cli-plugin-platform",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.2",
|
|
4
4
|
"description": "di-framework CLI extension that builds, serves, and deploys DI Framework apps as platform WebAssembly components.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@bytecodealliance/jco": "1.33.0",
|
|
43
43
|
"@di-framework/cli-extension": "^5",
|
|
44
|
-
"@di-framework/componentize-qjs": "0.4.4-di.
|
|
44
|
+
"@di-framework/componentize-qjs": "0.4.4-di.5",
|
|
45
45
|
"@noble/ciphers": "2.4.0",
|
|
46
46
|
"@noble/curves": "2.4.0",
|
|
47
47
|
"@noble/hashes": "2.4.0",
|