Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/void/src/cli/platform-access-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,28 @@ async function verifyHealthException(
}
}

/** Keep the same path-only exception used by project Zero Trust available during bootstrap. */
export async function ensurePlatformAccessHealthChecks(input: {
type AccessHealthInput = {
plan: PlatformInstallPlan;
manifest: PlatformInstallationManifest;
client: Partial<Pick<PlatformCloudflareClient, 'request' | 'apiBaseUrl'>>;
checkpoint: Readonly<Record<string, string>>;
save: (encoded: string) => void;
}): Promise<void> {
};

export function verifyPlatformAccessHealthChecks(input: AccessHealthInput): Promise<void> {
return reconcilePlatformAccessHealthChecks(input);
}

/** Keep the same path-only exception used by project Zero Trust available during bootstrap. */
export function ensurePlatformAccessHealthChecks(
input: AccessHealthInput & { save: (encoded: string) => void },
): Promise<void> {
return reconcilePlatformAccessHealthChecks(input, input.save);
}

async function reconcilePlatformAccessHealthChecks(
input: AccessHealthInput,
writeCheckpoint?: (encoded: string) => void,
): Promise<void> {
// The server owns ongoing app access after setup. Its token may have been
// rotated or its settings changed by an administrator since installation.
const appAccess =
Expand Down Expand Up @@ -131,7 +145,7 @@ export async function ensurePlatformAccessHealthChecks(input: {
) {
throw new Error('platform: Access health recovery data belongs to a different installation.');
}
const save = () => input.save(JSON.stringify(state));
const save = () => writeCheckpoint?.(JSON.stringify(state));
const inventory = await access.list('apps');
for (const { key, destination } of platformAccessHealthTargets(input.plan)) {
const expected = {
Expand Down Expand Up @@ -190,6 +204,9 @@ export async function ensurePlatformAccessHealthChecks(input: {
'platform: An Access health application with this installation identity already exists. Inspect it before resuming.',
);
}
if (!writeCheckpoint) {
continue;
}
recorded = { name, fingerprint, pending: true };
state.resources[key] = recorded;
save();
Expand Down
12 changes: 9 additions & 3 deletions packages/void/src/cli/platform-access-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,15 @@ export function activatePlatformAccessCredentials(
encoded: string | undefined,
origins: ReadonlyArray<string>,
): void {
if (!encoded) {
return;
if (encoded) {
process.env.VOID_ACCESS_CREDENTIALS = mergePlatformAccessCredentials(encoded, origins);
}
}

export function mergePlatformAccessCredentials(
encoded: string,
origins: ReadonlyArray<string>,
): string {
const entries: unknown = JSON.parse(encoded);
if (!entries || typeof entries !== 'object' || Array.isArray(entries)) {
throw new Error('platform: Invalid saved Access credentials.');
Expand All @@ -401,7 +407,7 @@ export function activatePlatformAccessCredentials(
if (!current || typeof current !== 'object' || Array.isArray(current)) {
throw new Error('platform: VOID_ACCESS_CREDENTIALS must contain an origin-keyed object.');
}
process.env.VOID_ACCESS_CREDENTIALS = JSON.stringify({ ...current, ...entries });
return JSON.stringify({ ...current, ...entries });
}

export async function preparePlatformAccessSetup(input: {
Expand Down
175 changes: 117 additions & 58 deletions packages/void/src/cli/platform-install-preparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
} from '@void/platform';
import type { PlatformDeploymentManifest } from '@void/platform';
import type { PlatformRuntimeManifest } from '@void/platform/installer';
import { ensurePlatformAccessHealthChecks } from './platform-access-health.ts';
import {
ensurePlatformAccessHealthChecks,
verifyPlatformAccessHealthChecks,
} from './platform-access-health.ts';
import { verifyInstalledPlatformAccessProtection } from './platform-access-preflight.ts';
import {
activatePlatformAccessCredentials,
mergePlatformAccessCredentials,
isPlatformAuthenticationPreparationKey,
} from './platform-access-setup.ts';
import { PlatformAppAccessCredentialError } from './platform-app-access.ts';
Expand All @@ -27,6 +30,7 @@ import type {
PlatformDatabaseLifecycleLock,
PlatformInstallerDependencies,
PlatformInstallRunState,
PlatformLiveAuthenticationState,
} from './platform-installer-types.ts';
import {
readPendingInstallationSecrets,
Expand All @@ -49,25 +53,17 @@ function withAuthentication(
});
}

export async function prepareInstallationAuthentication(input: {
readonly state: PlatformInstallRunState;
readonly releaseDatabaseLock: PlatformDatabaseLifecycleLock;
readonly runtime: PlatformRuntimeManifest;
readonly authenticationProtocolTransition: boolean;
readonly now: () => string;
readonly dependencies: PlatformInstallerDependencies;
}): Promise<void> {
const {
state,
releaseDatabaseLock,
runtime,
authenticationProtocolTransition,
now,
dependencies,
} = input;
const { workerSecretUpdateIntent } = state;
type InstallationAuthenticationInput = Readonly<{
state: PlatformInstallRunState;
runtime: PlatformRuntimeManifest;
authenticationProtocolTransition: boolean;
dependencies: PlatformInstallerDependencies;
}>;

const liveAuthentication = await releaseDatabaseLock.readAuthenticationState?.();
function applyInstallationAuthentication(
{ state, runtime, authenticationProtocolTransition }: InstallationAuthenticationInput,
liveAuthentication: PlatformLiveAuthenticationState | null | undefined,
): void {
if (liveAuthentication) {
state.authenticationEnforcementActive = liveAuthentication.sessionsRequired;
if (liveAuthentication.sessionsRequired && runtime.authenticationProtocolVersion !== 1) {
Expand Down Expand Up @@ -106,25 +102,104 @@ export async function prepareInstallationAuthentication(input: {
}),
};
}
const activateAccessCredentials = () => {
if (!state.secrets.accessCredentialsJson) {
return;
}

async function verifyInstallationAccessSetup(
{ state, dependencies }: InstallationAuthenticationInput,
liveAuthentication: PlatformLiveAuthenticationState | null | undefined,
): Promise<void> {
const protection = liveAuthentication
? liveAuthentication.protection
: state.secrets.authenticationSetupJson
? parsePlatformAuthenticationSetup(JSON.parse(state.secrets.authenticationSetupJson))
.protection
: null;
if (!protection) {
throw new Error(
'platform: The active Access protection configuration is unavailable. Restore its database and encrypted setup checkpoint before maintenance.',
);
}
dependencies.onProgress?.('Verifying current Access policies and protected origins');
await (dependencies.verifyAccessProtection ?? verifyInstalledPlatformAccessProtection)({
client: dependencies.client,
manifest: state.manifest,
plan: state.plan,
dashboardUrl: platformDashboardUrl(state.plan.deployment),
protection,
checkpoint: secretCheckpoint(state.secrets, state.workerSecretUpdateIntent),
});
}

function installationAccessCredentials(state: PlatformInstallRunState): string | undefined {
if (!state.secrets.accessCredentialsJson) {
return undefined;
}
const credentials: unknown = JSON.parse(state.secrets.accessCredentialsJson);
if (!credentials || typeof credentials !== 'object' || Array.isArray(credentials)) {
throw new Error('platform: Invalid saved Access credentials.');
}
// Lifecycle HTTP calls use API/proxy origins. Retain optional dashboard credentials
// in recovery storage without activating them, including after protection removal.
const origins = [state.plan.apiUrl, state.plan.proxyUrl];
return mergePlatformAccessCredentials(
JSON.stringify(
Object.fromEntries(
Object.entries(credentials).filter(([origin]) => origins.includes(origin)),
),
),
origins,
);
}

export async function preflightInstallationAuthentication(
input: InstallationAuthenticationInput,
lock: Pick<PlatformDatabaseLifecycleLock, 'readAuthenticationState'>,
): Promise<PlatformLiveAuthenticationState | null> {
const liveAuthentication = (await lock.readAuthenticationState?.()) ?? null;
// Use the same deployment decisions as preparation while keeping the current checkpoint.
const preview = { ...input, state: { ...input.state } };
applyInstallationAuthentication(preview, liveAuthentication);
installationAccessCredentials(preview.state);
checkpointAuthenticationResources(
preview.state.manifest,
preview.state.secrets,
preview.state.manifest.updatedAt,
);
if (preview.state.plan.deployment.authentication.accessProtection) {
await verifyInstallationAccessSetup(preview, liveAuthentication);
if (!input.dependencies.prepareHealthAccess) {
await verifyPlatformAccessHealthChecks({
client: preview.dependencies.client,
manifest: preview.state.manifest,
plan: preview.state.plan,
checkpoint: secretCheckpoint(preview.state.secrets, preview.state.workerSecretUpdateIntent),
});
}
const credentials: unknown = JSON.parse(state.secrets.accessCredentialsJson);
if (!credentials || typeof credentials !== 'object' || Array.isArray(credentials)) {
throw new Error('platform: Invalid saved Access credentials.');
}
return liveAuthentication;
}

export async function prepareInstallationAuthentication(
input: InstallationAuthenticationInput &
Readonly<{
releaseDatabaseLock: PlatformDatabaseLifecycleLock;
now: () => string;
checkedAuthentication?: PlatformLiveAuthenticationState | null;
}>,
): Promise<void> {
const { state, releaseDatabaseLock, runtime, now, dependencies, checkedAuthentication } = input;
const { authenticationProtocolTransition } = input;
const { workerSecretUpdateIntent } = state;
const liveAuthentication =
checkedAuthentication === undefined
? await releaseDatabaseLock.readAuthenticationState?.()
: checkedAuthentication;
applyInstallationAuthentication(input, liveAuthentication);
const activateAccessCredentials = () => {
const credentials = installationAccessCredentials(state);
if (credentials !== undefined) {
process.env.VOID_ACCESS_CREDENTIALS = credentials;
}
// Lifecycle HTTP calls use API/proxy origins. Retain optional dashboard credentials
// in recovery storage without activating them, including after protection removal.
const origins = [state.plan.apiUrl, state.plan.proxyUrl];
activatePlatformAccessCredentials(
JSON.stringify(
Object.fromEntries(
Object.entries(credentials).filter(([origin]) => origins.includes(origin)),
),
),
origins,
);
};
activateAccessCredentials();
state.manifest = checkpointAuthenticationResources(state.manifest, state.secrets, now());
Expand Down Expand Up @@ -282,27 +357,11 @@ export async function prepareInstallationAuthentication(input: {
});
}
}
if (state.plan.deployment.authentication.accessProtection) {
const protection = liveAuthentication
? liveAuthentication.protection
: state.secrets.authenticationSetupJson
? parsePlatformAuthenticationSetup(JSON.parse(state.secrets.authenticationSetupJson))
.protection
: null;
if (!protection) {
throw new Error(
'platform: The active Access protection configuration is unavailable. Restore its database and encrypted setup checkpoint before maintenance.',
);
}
dependencies.onProgress?.('Verifying current Access policies and protected origins');
await (dependencies.verifyAccessProtection ?? verifyInstalledPlatformAccessProtection)({
client: dependencies.client,
manifest: state.manifest,
plan: state.plan,
dashboardUrl: platformDashboardUrl(state.plan.deployment),
protection,
checkpoint: secretCheckpoint(state.secrets, workerSecretUpdateIntent),
});
if (
checkedAuthentication === undefined &&
state.plan.deployment.authentication.accessProtection
) {
await verifyInstallationAccessSetup(input, liveAuthentication);
}
if (
state.plan.deployment.authentication.accessProtection ||
Expand Down
Loading