@stage-labs/metro 0.1.0-beta.304 → 0.1.0-beta.305
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 +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +3 -4
- package/runtime/marketplace/plugin/METRO.md +1 -1
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +4 -28
- package/runtime/marketplace/plugin/bin/policy.mjs +1 -32
- package/runtime/node_modules/@metro-labs/core/package.json +0 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +3 -5
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +1 -7
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +5 -9
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +1 -3
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/enroll-api.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +9 -54
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +1 -13
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +0 -14
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/server-approvals.ts +14 -15
- package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/announced.ts +68 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/config.ts +3 -1
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +6 -5
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +2 -27
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +0 -5
- package/runtime/node_modules/@metro-labs/http/package.json +1 -2
- package/runtime/node_modules/@metro-labs/http/src/connector-grant.ts +0 -51
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +0 -1
- package/runtime/sdk-runner/runner.json +1 -1
- package/runtime/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +0 -104
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-adopt.ts +0 -57
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-relay.ts +0 -61
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime-discovery.ts +0 -115
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime.ts +0 -271
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-api.ts +0 -25
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-control.ts +0 -173
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-setting.ts +0 -60
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-watch.ts +0 -38
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync.ts +0 -208
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/publication.ts +0 -40
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-id.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +0 -90
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-session.ts +0 -10
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +0 -4
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +0 -287
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/server-keys.ts +0 -59
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +0 -1
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery-validation.ts +0 -116
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery.ts +0 -19
- package/runtime/node_modules/@metro-labs/http/src/connector-migration.ts +0 -178
- package/runtime/node_modules/@metro-labs/http/src/transfer-crypto.ts +0 -44
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
4
|
-
import { writeSecure } from '@metro-labs/core/secure-fs';
|
|
5
|
-
import { agentsDir } from '../agents/files.js';
|
|
6
|
-
import { serverKeysOf, type PluginRow } from './plugin-sync.js';
|
|
7
|
-
|
|
8
|
-
const invalid = (): Error => new Error('Invalid connector server keys. Restore the saved namespace mapping and initialization marker.');
|
|
9
|
-
|
|
10
|
-
function readKeys(path: string, marker: string): Map<string, string> | undefined {
|
|
11
|
-
try {
|
|
12
|
-
const marked = existsSync(marker);
|
|
13
|
-
if (marked && readFileSync(marker, 'utf8') !== '1\n') throw invalid();
|
|
14
|
-
if (!existsSync(path)) {
|
|
15
|
-
if (marked) throw invalid();
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
if (!marked) throw invalid();
|
|
19
|
-
return parseKeys(readFileSync(path, 'utf8'));
|
|
20
|
-
} catch { throw invalid(); }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function parseKeys(text: string): Map<string, string> {
|
|
24
|
-
const raw: unknown = JSON.parse(text);
|
|
25
|
-
if (!isRecord(raw) || raw.version !== 1 || !isRecord(raw.keys)) throw invalid();
|
|
26
|
-
const keys = new Map<string, string>();
|
|
27
|
-
const reserved = new Set<string>();
|
|
28
|
-
for (const [id, key] of Object.entries(raw.keys)) {
|
|
29
|
-
if (typeof key !== 'string' || !/^[a-z0-9-]+$/.test(key) || reserved.has(key)) throw invalid();
|
|
30
|
-
reserved.add(key);
|
|
31
|
-
keys.set(id, key);
|
|
32
|
-
}
|
|
33
|
-
return keys;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const sameKeys = (a: ReadonlyMap<string, string>, b: ReadonlyMap<string, string>): boolean =>
|
|
37
|
-
a.size === b.size && [...a].every(([id, key]) => b.get(id) === key);
|
|
38
|
-
|
|
39
|
-
function serverKeyFiles(dir = agentsDir()): { path: string; marker: string } {
|
|
40
|
-
return { path: join(dir, 'connector-server-keys.json'), marker: join(dir, 'connector-server-keys.initialized') };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function readServerKeys(dir = agentsDir()): Map<string, string> | undefined {
|
|
44
|
-
const { path, marker } = serverKeyFiles(dir);
|
|
45
|
-
return readKeys(path, marker);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function writeServerKeys(keys: ReadonlyMap<string, string>, dir = agentsDir()): void {
|
|
49
|
-
const { path, marker } = serverKeyFiles(dir);
|
|
50
|
-
if (!existsSync(marker)) writeSecure(marker, '1\n');
|
|
51
|
-
writeSecure(path, `${JSON.stringify({ version: 1, keys: Object.fromEntries(keys) }, null, 2)}\n`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function persistentServerKeys(rows: PluginRow[], dir = agentsDir()): Map<string, string> {
|
|
55
|
-
const saved = readServerKeys(dir);
|
|
56
|
-
const keys = serverKeysOf(rows, saved);
|
|
57
|
-
if (saved === undefined ? keys.size > 0 : !sameKeys(saved, keys)) writeServerKeys(keys, dir);
|
|
58
|
-
return keys;
|
|
59
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { listRemoteTools, type RemoteTool } from '@metro-labs/http/connector-tools';
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { parseId } from '@metro-labs/core/ids';
|
|
2
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
-
import { connectorName } from './connector-input.js';
|
|
4
|
-
import { connectorPolicyLayersOf, connectorPolicyOf, type ConnectorPolicy, type ConnectorPolicyLayers } from './connector-policy.js';
|
|
5
|
-
import { parsePublicConnectorUrl } from './connector-public-url.js';
|
|
6
|
-
import { isOrganizationId } from './workos-token.js';
|
|
7
|
-
|
|
8
|
-
export const CONNECTOR_DELIVERY_LEASE_MS = 90_000;
|
|
9
|
-
export const CONNECTOR_DELIVERY_MAX_BYTES = 1024 * 1024;
|
|
10
|
-
export const DELIVERY_PLAINTEXT_MAX = Math.floor((CONNECTOR_DELIVERY_MAX_BYTES - 1024) * 3 / 4);
|
|
11
|
-
|
|
12
|
-
export interface DeliveryContext {
|
|
13
|
-
owner: string;
|
|
14
|
-
agent: string;
|
|
15
|
-
keyId: string;
|
|
16
|
-
requestId: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface DeliveredConnector {
|
|
20
|
-
id: string;
|
|
21
|
-
name: string;
|
|
22
|
-
url: string;
|
|
23
|
-
auth: 'none' | 'header' | 'oauth';
|
|
24
|
-
headers: Record<string, string>;
|
|
25
|
-
policy: ConnectorPolicy;
|
|
26
|
-
policyLayers?: ConnectorPolicyLayers;
|
|
27
|
-
revision: string;
|
|
28
|
-
state: 'ready' | 'signin' | 'unavailable';
|
|
29
|
-
expiresAt?: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface DeliverySnapshot {
|
|
33
|
-
version: 1;
|
|
34
|
-
issuedAt: number;
|
|
35
|
-
expiresAt: number;
|
|
36
|
-
connectors: DeliveredConnector[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function deliveryBytes(value: unknown, length?: number): Buffer {
|
|
40
|
-
if (typeof value !== 'string' || !/^[A-Za-z0-9_-]+$/.test(value)) throw new Error('Invalid connector delivery encoding.');
|
|
41
|
-
const bytes = Buffer.from(value, 'base64url');
|
|
42
|
-
if (bytes.toString('base64url') !== value || (length !== undefined && bytes.length !== length)) throw new Error('Invalid connector delivery encoding.');
|
|
43
|
-
return bytes;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function deliveryRecord(value: unknown, fields: string[]): Record<string, unknown> {
|
|
47
|
-
if (!isRecord(value) || Object.keys(value).some((key) => !fields.includes(key))) throw new Error('Invalid connector delivery.');
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function deliveryContextBytes(context: DeliveryContext): Buffer {
|
|
52
|
-
const value = deliveryRecord(context, ['owner', 'agent', 'keyId', 'requestId']);
|
|
53
|
-
if (typeof value.owner !== 'string' || !isOrganizationId(value.owner) || parseId(value.agent) === null) throw new Error('Invalid connector delivery context.');
|
|
54
|
-
deliveryBytes(value.keyId, 32);
|
|
55
|
-
deliveryBytes(value.requestId, 32);
|
|
56
|
-
return Buffer.from(JSON.stringify(['metro-connector-delivery/v1', value.owner, value.agent, value.keyId, value.requestId]));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const timestamp = (value: unknown): value is number => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
60
|
-
|
|
61
|
-
function headerValue(name: string, text: unknown, auth: DeliveredConnector['auth']): string {
|
|
62
|
-
if (!/^[A-Za-z0-9!#$%&'*+.^_`|~-]{1,64}$/.test(name) || ['__proto__', 'constructor', 'prototype'].includes(name) || typeof text !== 'string' || !/^[\x20-\x7e]{1,16384}$/.test(text)) throw new Error('Invalid connector delivery headers.');
|
|
63
|
-
if (auth === 'oauth' && (name !== 'Authorization' || !/^Bearer [\x21-\x7e]+$/.test(text))) throw new Error('Invalid connector delivery access token.');
|
|
64
|
-
return text;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function headersOf(value: unknown, auth: DeliveredConnector['auth'], state: DeliveredConnector['state']): Record<string, string> {
|
|
68
|
-
if (!isRecord(value)) throw new Error('Invalid connector delivery headers.');
|
|
69
|
-
const entries = Object.entries(value);
|
|
70
|
-
const expected = auth !== 'none' && state === 'ready' ? 1 : 0;
|
|
71
|
-
if (entries.length !== expected) throw new Error('Invalid connector delivery headers.');
|
|
72
|
-
const out: Record<string, string> = {};
|
|
73
|
-
for (const [name, text] of entries) out[name] = headerValue(name, text, auth);
|
|
74
|
-
return out;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function authenticationOf(value: unknown): DeliveredConnector['auth'] {
|
|
78
|
-
if (value !== 'none' && value !== 'header' && value !== 'oauth') throw new Error('Invalid delivered connector authentication.');
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function stateOf(value: unknown, auth: DeliveredConnector['auth']): DeliveredConnector['state'] {
|
|
83
|
-
if (value !== 'ready' && value !== 'signin' && value !== 'unavailable') throw new Error('Invalid delivered connector state.');
|
|
84
|
-
if (value === 'signin' && auth !== 'oauth') throw new Error('Invalid delivered connector state.');
|
|
85
|
-
return value;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function urlOf(value: unknown): string {
|
|
89
|
-
parsePublicConnectorUrl(value);
|
|
90
|
-
if (typeof value !== 'string') throw new Error('Invalid delivered connector URL.');
|
|
91
|
-
return value;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function connectorOf(raw: unknown): DeliveredConnector {
|
|
95
|
-
const value = deliveryRecord(raw, ['id', 'name', 'url', 'auth', 'headers', 'policy', 'policyLayers', 'revision', 'state', 'expiresAt']);
|
|
96
|
-
const id = parseId(value.id);
|
|
97
|
-
if (id === null || typeof value.name !== 'string' || connectorName(value.name) !== value.name) throw new Error('Invalid delivered connector.');
|
|
98
|
-
const url = urlOf(value.url);
|
|
99
|
-
const auth = authenticationOf(value.auth);
|
|
100
|
-
const state = stateOf(value.state, auth);
|
|
101
|
-
deliveryBytes(value.revision, 32);
|
|
102
|
-
if (Object.hasOwn(value, 'expiresAt') && !timestamp(value.expiresAt)) throw new Error('Invalid delivered connector expiry.');
|
|
103
|
-
return { id, name: value.name, url, auth, state, revision: String(value.revision), headers: headersOf(value.headers, auth, state), policy: connectorPolicyOf(value.policy), ...(Object.hasOwn(value, 'policyLayers') ? { policyLayers: connectorPolicyLayersOf(value.policyLayers) } : {}), ...(timestamp(value.expiresAt) ? { expiresAt: value.expiresAt } : {}) };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function deliverySnapshotOf(raw: unknown): DeliverySnapshot {
|
|
107
|
-
const value = deliveryRecord(raw, ['version', 'issuedAt', 'expiresAt', 'connectors']);
|
|
108
|
-
if (value.version !== 1 || !timestamp(value.issuedAt) || !timestamp(value.expiresAt) || value.expiresAt <= value.issuedAt || value.expiresAt - value.issuedAt > CONNECTOR_DELIVERY_LEASE_MS || !Array.isArray(value.connectors)) throw new Error('Invalid connector delivery snapshot.');
|
|
109
|
-
const issuedAt = value.issuedAt;
|
|
110
|
-
const connectors = value.connectors.map((entry: unknown) => connectorOf(entry));
|
|
111
|
-
if (new Set(connectors.map((connector) => connector.id)).size !== connectors.length) throw new Error('Duplicate delivered connector.');
|
|
112
|
-
if (connectors.some((connector) => connector.state === 'ready' && connector.expiresAt !== undefined && connector.expiresAt <= issuedAt)) throw new Error('Expired delivered connector.');
|
|
113
|
-
const snapshot: DeliverySnapshot = { version: 1, issuedAt: value.issuedAt, expiresAt: value.expiresAt, connectors };
|
|
114
|
-
if (Buffer.byteLength(JSON.stringify(snapshot)) > DELIVERY_PLAINTEXT_MAX) throw new Error('Connector delivery is too large.');
|
|
115
|
-
return snapshot;
|
|
116
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { KeyObject } from 'node:crypto';
|
|
2
|
-
import { CONNECTOR_DELIVERY_MAX_BYTES, DELIVERY_PLAINTEXT_MAX, deliveryContextBytes, deliverySnapshotOf, type DeliveryContext, type DeliverySnapshot } from './connector-delivery-validation.js';
|
|
3
|
-
import { openTransfer, sealTransfer, type SealedTransfer } from './transfer-crypto.js';
|
|
4
|
-
|
|
5
|
-
export { CONNECTOR_DELIVERY_LEASE_MS, CONNECTOR_DELIVERY_MAX_BYTES, DELIVERY_PLAINTEXT_MAX, deliverySnapshotOf, type DeliveryContext, type DeliverySnapshot, type DeliveredConnector } from './connector-delivery-validation.js';
|
|
6
|
-
export { combineConnectorPolicies, connectorPolicyOf, effectiveConnectorPolicy, connectorPoliciesBlocked, validConnectorToolName, type ConnectorPolicy, type ConnectorPolicyLayers } from './connector-policy.js';
|
|
7
|
-
export type SealedDelivery = SealedTransfer;
|
|
8
|
-
|
|
9
|
-
export function sealConnectorDelivery(publicKey: string, context: DeliveryContext, snapshot: DeliverySnapshot): SealedDelivery {
|
|
10
|
-
const sealed = sealTransfer(publicKey, deliveryContextBytes(context), Buffer.from(JSON.stringify(deliverySnapshotOf(snapshot))));
|
|
11
|
-
if (Buffer.byteLength(JSON.stringify({ sealed })) > CONNECTOR_DELIVERY_MAX_BYTES) throw new Error('Connector delivery is too large.');
|
|
12
|
-
return sealed;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function openConnectorDelivery(privateKey: KeyObject, context: DeliveryContext, envelope: unknown): DeliverySnapshot {
|
|
16
|
-
if (Buffer.byteLength(JSON.stringify({ sealed: envelope })) > CONNECTOR_DELIVERY_MAX_BYTES) throw new Error('Connector delivery is too large.');
|
|
17
|
-
const plaintext = openTransfer(privateKey, deliveryContextBytes(context), envelope, DELIVERY_PLAINTEXT_MAX);
|
|
18
|
-
return deliverySnapshotOf(JSON.parse(plaintext.toString('utf8')) as unknown);
|
|
19
|
-
}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import type { KeyObject } from 'node:crypto';
|
|
2
|
-
import { parseId } from '@metro-labs/core/ids';
|
|
3
|
-
import { sealingKeyOf } from './agent-signature.js';
|
|
4
|
-
import { deliveryBytes, deliveryRecord } from './connector-delivery-validation.js';
|
|
5
|
-
import { connectorAuth, connectorClient, connectorName } from './connector-input.js';
|
|
6
|
-
import type { OAuthClient } from './connector-oauth.js';
|
|
7
|
-
import { portableGrant, type PortableGrant } from './connector-grant.js';
|
|
8
|
-
import { connectorPolicyOf, type ConnectorPolicy } from './connector-policy.js';
|
|
9
|
-
import { boundOAuthResource, parsePublicConnectorUrl } from './connector-public-url.js';
|
|
10
|
-
import { openTransfer, sealTransfer, type SealedTransfer } from './transfer-crypto.js';
|
|
11
|
-
import { isOrganizationId } from './workos-token.js';
|
|
12
|
-
|
|
13
|
-
export const CONNECTOR_MIGRATION_MAX_BYTES = 96 * 1024;
|
|
14
|
-
const PAYLOAD_MAX = 60 * 1024;
|
|
15
|
-
|
|
16
|
-
export interface MigrationContext {
|
|
17
|
-
owner: string;
|
|
18
|
-
agent: string;
|
|
19
|
-
keyId: string;
|
|
20
|
-
operationId: string;
|
|
21
|
-
sourceId: string;
|
|
22
|
-
destinationId: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface MigrationPayload {
|
|
26
|
-
name: string;
|
|
27
|
-
url: string;
|
|
28
|
-
auth: { kind: 'none' } | { kind: 'header'; name: string; value: string } | { kind: 'oauth'; grant?: PortableGrant };
|
|
29
|
-
client: OAuthClient | null;
|
|
30
|
-
oauthResource?: string;
|
|
31
|
-
policy: ConnectorPolicy;
|
|
32
|
-
sourceStamp: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface MigrationView {
|
|
36
|
-
operationId: string;
|
|
37
|
-
sourceId: string;
|
|
38
|
-
destinationId: string;
|
|
39
|
-
phase: 'preparing' | 'prepared' | 'active' | 'rolled_back';
|
|
40
|
-
readiness: 'pending' | 'ready' | 'signin' | 'changed' | 'unavailable' | 'refresh_uncertain';
|
|
41
|
-
grantImported?: boolean;
|
|
42
|
-
name: string | null;
|
|
43
|
-
auth: 'none' | 'header' | 'oauth' | null;
|
|
44
|
-
policy: ConnectorPolicy | null;
|
|
45
|
-
sourceStamp: string | null;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface MigrationResponse {
|
|
49
|
-
migration: MigrationView;
|
|
50
|
-
receiving?: { publicKey: string; context: MigrationContext };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type MigrationRequest = { action: 'begin' | 'status' | 'activate' | 'rollback'; operationId: string; sourceId: string } | { action: 'prepare' | 'handover'; operationId: string; sourceId: string; sealed: unknown };
|
|
54
|
-
|
|
55
|
-
const invalid = (): Error => new Error('Invalid connector migration.');
|
|
56
|
-
const idOf = (value: unknown): string => {
|
|
57
|
-
const id = parseId(value);
|
|
58
|
-
if (id === null) throw invalid();
|
|
59
|
-
return id;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export function migrationRequestOf(raw: unknown): MigrationRequest {
|
|
63
|
-
const value = deliveryRecord(raw, ['action', 'operationId', 'sourceId', 'sealed']);
|
|
64
|
-
const operationId = idOf(value.operationId);
|
|
65
|
-
const sourceId = idOf(value.sourceId);
|
|
66
|
-
if ((value.action === 'prepare' || value.action === 'handover') && Object.hasOwn(value, 'sealed')) return { action: value.action, operationId, sourceId, sealed: value.sealed };
|
|
67
|
-
if (Object.hasOwn(value, 'sealed')) throw invalid();
|
|
68
|
-
if (value.action !== 'begin' && value.action !== 'status' && value.action !== 'activate' && value.action !== 'rollback') throw invalid();
|
|
69
|
-
return { action: value.action, operationId, sourceId };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function contextOf(raw: unknown): MigrationContext {
|
|
73
|
-
const value = deliveryRecord(raw, ['owner', 'agent', 'keyId', 'operationId', 'sourceId', 'destinationId']);
|
|
74
|
-
if (typeof value.owner !== 'string' || !isOrganizationId(value.owner) || typeof value.keyId !== 'string') throw invalid();
|
|
75
|
-
deliveryBytes(value.keyId, 32);
|
|
76
|
-
return { owner: value.owner, agent: idOf(value.agent), keyId: value.keyId, operationId: idOf(value.operationId), sourceId: idOf(value.sourceId), destinationId: idOf(value.destinationId) };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function contextBytes(context: MigrationContext): Buffer {
|
|
80
|
-
const value = contextOf(context);
|
|
81
|
-
return Buffer.from(JSON.stringify(['metro-connector-migration/v1', value.owner, value.agent, value.keyId, value.operationId, value.sourceId, value.destinationId]));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function headerOf(value: Record<string, unknown>): MigrationPayload['auth'] {
|
|
85
|
-
const auth = connectorAuth(value.name, value.value);
|
|
86
|
-
if (auth.kind !== 'header' || auth.name !== value.name || auth.value !== value.value || ['__proto__', 'constructor', 'prototype'].includes(auth.name)) throw invalid();
|
|
87
|
-
return auth;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function authOf(raw: unknown, target: string): MigrationPayload['auth'] {
|
|
91
|
-
const value = deliveryRecord(raw, ['kind', 'name', 'value', 'grant']);
|
|
92
|
-
if (value.kind !== 'oauth' && Object.hasOwn(value, 'grant')) throw invalid();
|
|
93
|
-
if (value.kind === 'header') return headerOf(value);
|
|
94
|
-
if ((value.kind !== 'none' && value.kind !== 'oauth') || Object.hasOwn(value, 'name') || Object.hasOwn(value, 'value')) throw invalid();
|
|
95
|
-
if (value.kind === 'none' || value.grant === undefined) return { kind: value.kind };
|
|
96
|
-
return { kind: 'oauth', grant: portableGrant(value.grant, target) };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function clientOf(raw: unknown): OAuthClient | null {
|
|
100
|
-
if (raw === null) return null;
|
|
101
|
-
const value = deliveryRecord(raw, ['clientId', 'clientSecret']);
|
|
102
|
-
const client = connectorClient(value.clientId, value.clientSecret);
|
|
103
|
-
if (client === null || client.clientId !== value.clientId || client.clientSecret !== value.clientSecret) throw invalid();
|
|
104
|
-
return client;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function migrationClient(auth: MigrationPayload['auth'], input: Record<string, unknown>): OAuthClient | null {
|
|
108
|
-
const client = clientOf(input.client);
|
|
109
|
-
if (auth.kind !== 'oauth' && (client !== null || Object.hasOwn(input, 'oauthResource'))) throw invalid();
|
|
110
|
-
return client;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function migrationPayloadOf(raw: unknown): MigrationPayload {
|
|
114
|
-
const value = deliveryRecord(raw, ['name', 'url', 'auth', 'client', 'oauthResource', 'policy', 'sourceStamp']);
|
|
115
|
-
if (typeof value.name !== 'string' || connectorName(value.name) !== value.name || typeof value.url !== 'string' || typeof value.sourceStamp !== 'string') throw invalid();
|
|
116
|
-
parsePublicConnectorUrl(value.url);
|
|
117
|
-
deliveryBytes(value.sourceStamp, 32);
|
|
118
|
-
const auth = authOf(value.auth, value.url);
|
|
119
|
-
const client = migrationClient(auth, value);
|
|
120
|
-
const payload = { name: value.name, url: value.url, auth, client, ...(Object.hasOwn(value, 'oauthResource') ? { oauthResource: boundOAuthResource(value.url, value.oauthResource) } : {}), policy: connectorPolicyOf(value.policy), sourceStamp: value.sourceStamp };
|
|
121
|
-
if (Buffer.byteLength(JSON.stringify(payload)) > PAYLOAD_MAX) throw invalid();
|
|
122
|
-
return payload;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function phaseOf(value: unknown): MigrationView['phase'] {
|
|
126
|
-
if (value !== 'preparing' && value !== 'prepared' && value !== 'active' && value !== 'rolled_back') throw invalid();
|
|
127
|
-
return value;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function readinessOf(value: unknown): MigrationView['readiness'] {
|
|
131
|
-
if (value !== 'pending' && value !== 'ready' && value !== 'signin' && value !== 'changed' && value !== 'unavailable' && value !== 'refresh_uncertain') throw invalid();
|
|
132
|
-
return value;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function authKindOf(value: unknown): MigrationView['auth'] {
|
|
136
|
-
if (value !== null && value !== 'none' && value !== 'header' && value !== 'oauth') throw invalid();
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function nullableName(value: unknown): string | null {
|
|
141
|
-
if (value === null) return null;
|
|
142
|
-
if (typeof value !== 'string' || connectorName(value) !== value) throw invalid();
|
|
143
|
-
return value;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function nullableStamp(value: unknown): string | null {
|
|
147
|
-
if (value === null) return null;
|
|
148
|
-
if (typeof value !== 'string') throw invalid();
|
|
149
|
-
deliveryBytes(value, 32);
|
|
150
|
-
return value;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function viewOf(raw: unknown): MigrationView {
|
|
154
|
-
const view = deliveryRecord(raw, ['operationId', 'sourceId', 'destinationId', 'phase', 'readiness', 'name', 'auth', 'policy', 'sourceStamp', 'grantImported']);
|
|
155
|
-
if (view.grantImported !== undefined && typeof view.grantImported !== 'boolean') throw invalid();
|
|
156
|
-
return { ...(view.grantImported === undefined ? {} : { grantImported: view.grantImported }), operationId: idOf(view.operationId), sourceId: idOf(view.sourceId), destinationId: idOf(view.destinationId), phase: phaseOf(view.phase), readiness: readinessOf(view.readiness), name: nullableName(view.name), auth: authKindOf(view.auth), policy: view.policy === null ? null : connectorPolicyOf(view.policy), sourceStamp: nullableStamp(view.sourceStamp) };
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export function migrationResponseOf(raw: unknown): MigrationResponse {
|
|
160
|
-
const value = deliveryRecord(raw, ['migration', 'receiving']);
|
|
161
|
-
const migration = viewOf(value.migration);
|
|
162
|
-
if (!Object.hasOwn(value, 'receiving')) return { migration };
|
|
163
|
-
const receiving = deliveryRecord(value.receiving, ['publicKey', 'context']);
|
|
164
|
-
if (typeof receiving.publicKey !== 'string' || sealingKeyOf(receiving.publicKey) === null) throw invalid();
|
|
165
|
-
const context = contextOf(receiving.context);
|
|
166
|
-
if (context.operationId !== migration.operationId || context.sourceId !== migration.sourceId || context.destinationId !== migration.destinationId) throw invalid();
|
|
167
|
-
return { migration, receiving: { publicKey: receiving.publicKey, context } };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export function sealConnectorMigration(publicKey: string, context: MigrationContext, payload: MigrationPayload): SealedTransfer {
|
|
171
|
-
return sealTransfer(publicKey, contextBytes(context), Buffer.from(JSON.stringify(migrationPayloadOf(payload))));
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function openConnectorMigration(privateKey: KeyObject, context: MigrationContext, sealed: unknown): MigrationPayload {
|
|
175
|
-
if (Buffer.byteLength(JSON.stringify(sealed)) > CONNECTOR_MIGRATION_MAX_BYTES) throw invalid();
|
|
176
|
-
const plain = openTransfer(privateKey, contextBytes(context), sealed, PAYLOAD_MAX);
|
|
177
|
-
return migrationPayloadOf(JSON.parse(plain.toString('utf8')) as unknown);
|
|
178
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { createCipheriv, createDecipheriv, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, randomBytes, type KeyObject } from 'node:crypto';
|
|
2
|
-
import { rawPublicKey, sealingKeyOf } from './agent-signature.js';
|
|
3
|
-
import { deliveryBytes, deliveryRecord } from './connector-delivery-validation.js';
|
|
4
|
-
|
|
5
|
-
export interface SealedTransfer {
|
|
6
|
-
version: 1;
|
|
7
|
-
ephemeralKey: string;
|
|
8
|
-
iv: string;
|
|
9
|
-
tag: string;
|
|
10
|
-
ciphertext: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function cipherKey(privateKey: KeyObject, publicKey: KeyObject, context: Buffer, ephemeral: string, recipient: string): Buffer {
|
|
14
|
-
const shared = diffieHellman({ privateKey, publicKey });
|
|
15
|
-
if (!shared.some((byte) => byte !== 0)) throw new Error('Invalid connector transfer key.');
|
|
16
|
-
return Buffer.from(hkdfSync('sha256', shared, Buffer.concat([deliveryBytes(ephemeral, 32), deliveryBytes(recipient, 32)]), context, 32));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function sealTransfer(publicKey: string, aad: Buffer, bytes: Buffer): SealedTransfer {
|
|
20
|
-
const recipient = sealingKeyOf(publicKey);
|
|
21
|
-
if (recipient === null) throw new Error('Invalid connector transfer key.');
|
|
22
|
-
const pair = generateKeyPairSync('x25519');
|
|
23
|
-
const ephemeralKey = rawPublicKey(pair.publicKey);
|
|
24
|
-
const iv = randomBytes(12);
|
|
25
|
-
const cipher = createCipheriv('aes-256-gcm', cipherKey(pair.privateKey, recipient, aad, ephemeralKey, publicKey), iv);
|
|
26
|
-
cipher.setAAD(aad);
|
|
27
|
-
const ciphertext = Buffer.concat([cipher.update(bytes), cipher.final()]).toString('base64url');
|
|
28
|
-
return { version: 1, ephemeralKey, iv: iv.toString('base64url'), tag: cipher.getAuthTag().toString('base64url'), ciphertext };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function openTransfer(privateKey: KeyObject, aad: Buffer, envelope: unknown, maxBytes: number): Buffer {
|
|
32
|
-
if (privateKey.type !== 'private' || privateKey.asymmetricKeyType !== 'x25519') throw new Error('Invalid connector transfer key.');
|
|
33
|
-
const value = deliveryRecord(envelope, ['version', 'ephemeralKey', 'iv', 'tag', 'ciphertext']);
|
|
34
|
-
if (value.version !== 1 || typeof value.ephemeralKey !== 'string') throw new Error('Invalid connector transfer envelope.');
|
|
35
|
-
const ephemeral = sealingKeyOf(value.ephemeralKey);
|
|
36
|
-
if (ephemeral === null) throw new Error('Invalid connector transfer key.');
|
|
37
|
-
const ciphertext = deliveryBytes(value.ciphertext);
|
|
38
|
-
if (ciphertext.length > maxBytes) throw new Error('Connector transfer is too large.');
|
|
39
|
-
const key = cipherKey(privateKey, ephemeral, aad, value.ephemeralKey, rawPublicKey(createPublicKey(privateKey)));
|
|
40
|
-
const decipher = createDecipheriv('aes-256-gcm', key, deliveryBytes(value.iv, 12));
|
|
41
|
-
decipher.setAAD(aad);
|
|
42
|
-
decipher.setAuthTag(deliveryBytes(value.tag, 16));
|
|
43
|
-
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
44
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export interface ConnectorSyncReceipt {
|
|
2
|
-
state: 'waiting' | 'received' | 'expired' | 'unavailable';
|
|
3
|
-
lastReceivedAt: number | null;
|
|
4
|
-
leaseExpiresAt: number | null;
|
|
5
|
-
counts: { total: number; ready: number; unavailable: number } | null;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface ConnectorSyncStatus extends Omit<ConnectorSyncReceipt, 'state'> {
|
|
9
|
-
enabled: boolean;
|
|
10
|
-
source: 'saved' | 'environment' | 'default' | 'invalid' | 'scope_changed';
|
|
11
|
-
enrolled: boolean;
|
|
12
|
-
owner: string | null;
|
|
13
|
-
agentId: string | null;
|
|
14
|
-
scope: string;
|
|
15
|
-
liveEnabled: boolean;
|
|
16
|
-
state: 'off' | 'not_enrolled' | ConnectorSyncReceipt['state'];
|
|
17
|
-
}
|