@duct-sdk/sdk 0.5.3 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +19 -41
- package/assets/duct-wordmark.svg +7 -7
- package/dist/adapters/react/shell.js +33 -1
- package/dist/cli/index.js +179 -36
- package/dist/core/config-roundtrip.test.d.ts +1 -0
- package/dist/core/config-roundtrip.test.js +132 -0
- package/dist/core/config.d.ts +253 -25
- package/dist/core/config.js +31 -67
- package/dist/core/endpoint-guard.d.ts +27 -0
- package/dist/core/endpoint-guard.js +71 -0
- package/dist/core/endpoint-guard.test.d.ts +1 -0
- package/dist/core/endpoint-guard.test.js +57 -0
- package/dist/core/env-diagnostics.d.ts +13 -0
- package/dist/core/env-diagnostics.js +63 -0
- package/dist/core/env-diagnostics.test.d.ts +1 -0
- package/dist/core/env-diagnostics.test.js +31 -0
- package/dist/core/profile.d.ts +44 -0
- package/dist/core/profile.js +128 -0
- package/dist/core/profile.test.d.ts +1 -0
- package/dist/core/profile.test.js +95 -0
- package/dist/core/readiness-checks.d.ts +113 -0
- package/dist/core/readiness-checks.js +110 -0
- package/dist/core/readiness-checks.test.d.ts +1 -0
- package/dist/core/readiness-checks.test.js +49 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +1 -1
- package/package.json +4 -3
package/dist/core/config.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// @duct/sdk — Config Definition and Validation
|
|
3
3
|
// ============================================================
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { ActionManifestSchema, iso8601Schema, } from "@duct-sdk/contracts";
|
|
6
|
+
export { ActionManifestSchema, ManifestActionSchema, SensitivityLevelSchema, ErrorEnvelopeSchema, ConfirmationStateSchema, } from "@duct-sdk/contracts";
|
|
5
7
|
// ----------------------------------------------------------------
|
|
6
8
|
// Zod Schemas
|
|
7
9
|
// ----------------------------------------------------------------
|
|
@@ -43,73 +45,15 @@ const RouteManifestSchema = z.object({
|
|
|
43
45
|
requiresAuth: z.boolean(),
|
|
44
46
|
deeplinkable: z.boolean(),
|
|
45
47
|
methods: z.array(z.string()).optional(),
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const ApprovalLogicalOpSchema = z.enum(["and", "or"]);
|
|
56
|
-
const ApprovalPredicateSchema = z.lazy(() => z.union([
|
|
57
|
-
z.object({
|
|
58
|
-
op: ApprovalLogicalOpSchema,
|
|
59
|
-
rules: z.array(ApprovalPredicateSchema).min(1),
|
|
60
|
-
}),
|
|
61
|
-
z.object({
|
|
62
|
-
op: z.literal("in"),
|
|
63
|
-
param: z.string().min(1),
|
|
64
|
-
values: z.array(z.union([z.string(), z.number(), z.boolean()])).min(1),
|
|
65
|
-
}),
|
|
66
|
-
z.object({
|
|
67
|
-
op: ApprovalCompareOpSchema,
|
|
68
|
-
param: z.string().min(1),
|
|
69
|
-
value: z.union([z.string(), z.number(), z.boolean()]),
|
|
70
|
-
}),
|
|
71
|
-
]));
|
|
72
|
-
const ActionLimitPolicySchema = z.discriminatedUnion("type", [
|
|
73
|
-
z.object({
|
|
74
|
-
type: z.literal("rate"),
|
|
75
|
-
maxPerWindow: z.number().int().positive(),
|
|
76
|
-
windowSeconds: z.number().int().positive(),
|
|
77
|
-
}),
|
|
78
|
-
z.object({
|
|
79
|
-
type: z.literal("count"),
|
|
80
|
-
maxCount: z.number().int().positive(),
|
|
81
|
-
}),
|
|
82
|
-
]);
|
|
83
|
-
const iso8601Schema = z
|
|
84
|
-
.string()
|
|
85
|
-
.datetime({ offset: true, message: "Must be ISO 8601 with timezone offset" });
|
|
86
|
-
const ActionParamSchema = z.object({
|
|
87
|
-
name: z.string().min(1),
|
|
88
|
-
type: z.enum(["string", "number", "boolean", "object", "array"]),
|
|
89
|
-
required: z.boolean(),
|
|
90
|
-
description: z.string().optional(),
|
|
91
|
-
schema: z.record(z.unknown()).optional(),
|
|
92
|
-
});
|
|
93
|
-
const ActionManifestSchema = z.object({
|
|
94
|
-
id: z
|
|
95
|
-
.string()
|
|
96
|
-
.min(1)
|
|
97
|
-
.regex(/^[a-z_][a-z0-9_]*$/, "Action IDs must be snake_case"),
|
|
98
|
-
name: z.string().min(1),
|
|
99
|
-
description: z.string().min(1),
|
|
100
|
-
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]),
|
|
101
|
-
path: z.string().min(1).startsWith("/"),
|
|
102
|
-
agentAccessible: z.boolean(),
|
|
103
|
-
sideEffects: z.boolean(),
|
|
104
|
-
sensitivityLevel: SensitivityLevelSchema,
|
|
105
|
-
params: z.array(ActionParamSchema).default([]),
|
|
106
|
-
responseSchema: z.record(z.unknown()).optional(),
|
|
107
|
-
requiresAuth: z.boolean(),
|
|
108
|
-
rateLimit: z.number().positive().optional(),
|
|
109
|
-
validFrom: iso8601Schema.optional(),
|
|
110
|
-
validUntil: iso8601Schema.optional(),
|
|
111
|
-
approveIf: ApprovalPredicateSchema.optional(),
|
|
112
|
-
actionLimit: ActionLimitPolicySchema.optional(),
|
|
48
|
+
updatedAt: z.string().optional(),
|
|
49
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
50
|
+
evidence: z.array(z.string()).optional(),
|
|
51
|
+
searchAction: z
|
|
52
|
+
.object({
|
|
53
|
+
actionId: z.string().min(1),
|
|
54
|
+
paramMap: z.record(z.string()),
|
|
55
|
+
})
|
|
56
|
+
.optional(),
|
|
113
57
|
});
|
|
114
58
|
const AuthStrategySchema = z.enum([
|
|
115
59
|
"nextauth",
|
|
@@ -133,6 +77,8 @@ const AuthSchemaSchema = z.object({
|
|
|
133
77
|
.regex(/^[a-z_][a-z0-9_]*$/, "loginDeeplinkId must be snake_case")
|
|
134
78
|
.optional(),
|
|
135
79
|
loginRoutePath: z.string().min(1).startsWith("/").optional(),
|
|
80
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
81
|
+
evidence: z.array(z.string()).optional(),
|
|
136
82
|
});
|
|
137
83
|
const StateFieldSchema = z.object({
|
|
138
84
|
key: z.string().min(1),
|
|
@@ -156,10 +102,12 @@ const DeeplinkDefinitionSchema = z.object({
|
|
|
156
102
|
stateFields: z.array(StateFieldSchema).default([]),
|
|
157
103
|
expirySeconds: z.number().positive().default(300),
|
|
158
104
|
requiresAuth: z.boolean(),
|
|
105
|
+
openInNewTab: z.boolean().optional(),
|
|
159
106
|
intent: z.string().optional(),
|
|
160
107
|
pageResolver: PageResolverSchema.optional(),
|
|
161
108
|
allowFetch: z.boolean().optional(),
|
|
162
109
|
contentSelector: z.string().optional(),
|
|
110
|
+
updatedAt: z.string().optional(),
|
|
163
111
|
});
|
|
164
112
|
const AgentPermissionLevelSchema = z.enum([
|
|
165
113
|
"none",
|
|
@@ -192,6 +140,21 @@ const WidgetConfigSchema = z.object({
|
|
|
192
140
|
width: z.union([z.number(), z.string()]).optional(),
|
|
193
141
|
height: z.union([z.number(), z.string()]).optional(),
|
|
194
142
|
customCSS: z.string().optional(),
|
|
143
|
+
compactionTurns: z.number().int().min(0).optional(),
|
|
144
|
+
maxToolIterations: z.number().int().positive().optional(),
|
|
145
|
+
maxPlanSteps: z.number().int().positive().optional(),
|
|
146
|
+
});
|
|
147
|
+
const DataModelEntitySchema = z.object({
|
|
148
|
+
id: z.string().min(1),
|
|
149
|
+
label: z.string().min(1),
|
|
150
|
+
discoveryAction: z.string().min(1),
|
|
151
|
+
fetchAction: z.string().min(1),
|
|
152
|
+
indexKey: z.string().min(1),
|
|
153
|
+
labelKey: z.string().min(1),
|
|
154
|
+
dimensions: z.array(z.string()).optional(),
|
|
155
|
+
});
|
|
156
|
+
const DataModelSchema = z.object({
|
|
157
|
+
entities: z.array(DataModelEntitySchema),
|
|
195
158
|
});
|
|
196
159
|
const ProductConfigSchema = z.object({
|
|
197
160
|
name: z.string().min(1),
|
|
@@ -214,6 +177,7 @@ export const DuctConfigSchema = z.object({
|
|
|
214
177
|
deeplinks: z.array(DeeplinkDefinitionSchema).default([]),
|
|
215
178
|
agentPermissions: AgentPermissionsSchema,
|
|
216
179
|
widget: WidgetConfigSchema.optional(),
|
|
180
|
+
dataModel: DataModelSchema.optional(),
|
|
217
181
|
registry_visibility: z.enum(['public', 'private']).optional().default('private'),
|
|
218
182
|
intershell_enabled: z.boolean().optional().default(false),
|
|
219
183
|
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EndpointProfile } from "./endpoints.js";
|
|
2
|
+
export type DuctProfile = "local" | "hosted" | (string & {});
|
|
3
|
+
export declare function getDuctProfile(): DuctProfile;
|
|
4
|
+
export declare function isLocalDuctProfile(profile?: DuctProfile): boolean;
|
|
5
|
+
export declare function isLocalhostUrl(url: string): boolean;
|
|
6
|
+
export declare function allowsLocalhostEndpoints(): boolean;
|
|
7
|
+
export interface ResolvedServiceEndpoints {
|
|
8
|
+
ductUrl: string;
|
|
9
|
+
authUrl: string;
|
|
10
|
+
apiBase: string;
|
|
11
|
+
shellUrl: string;
|
|
12
|
+
registryUrl: string;
|
|
13
|
+
dashboardUrl: string;
|
|
14
|
+
docsUrl: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function resolveServiceEndpoints(profile?: EndpointProfile | null): ResolvedServiceEndpoints;
|
|
17
|
+
export declare class HostedLocalhostEndpointError extends Error {
|
|
18
|
+
readonly localhostEndpoints: Array<{
|
|
19
|
+
key: string;
|
|
20
|
+
url: string;
|
|
21
|
+
}>;
|
|
22
|
+
constructor(localhostEndpoints: Array<{
|
|
23
|
+
key: string;
|
|
24
|
+
url: string;
|
|
25
|
+
}>);
|
|
26
|
+
}
|
|
27
|
+
export declare function assertHostedEndpointsNotLocalhost(profile?: EndpointProfile | null): void;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Hosted-mode endpoint guard
|
|
3
|
+
// ============================================================
|
|
4
|
+
import { getActiveApiBase, getAuthUrl, getConfiguredDashboard, getDocsUrl, getRegistryUrl, getShellRuntimeUrl, } from "./endpoints.js";
|
|
5
|
+
import { resolveActiveProfileName } from "./profile.js";
|
|
6
|
+
export function getDuctProfile() {
|
|
7
|
+
return resolveActiveProfileName();
|
|
8
|
+
}
|
|
9
|
+
export function isLocalDuctProfile(profile = getDuctProfile()) {
|
|
10
|
+
return profile.trim().toLowerCase() === "local";
|
|
11
|
+
}
|
|
12
|
+
export function isLocalhostUrl(url) {
|
|
13
|
+
if (!url.trim())
|
|
14
|
+
return false;
|
|
15
|
+
try {
|
|
16
|
+
const host = new URL(url).hostname.toLowerCase();
|
|
17
|
+
return (host === "localhost" ||
|
|
18
|
+
host === "127.0.0.1" ||
|
|
19
|
+
host === "::1" ||
|
|
20
|
+
host.endsWith(".localhost"));
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function allowsLocalhostEndpoints() {
|
|
27
|
+
if (isLocalDuctProfile())
|
|
28
|
+
return true;
|
|
29
|
+
const flag = (process.env["DUCT_ALLOW_LOCALHOST"] ?? "").trim().toLowerCase();
|
|
30
|
+
return flag === "true" || flag === "1" || flag === "yes";
|
|
31
|
+
}
|
|
32
|
+
export function resolveServiceEndpoints(profile) {
|
|
33
|
+
return {
|
|
34
|
+
ductUrl: (process.env["DUCT_URL"] ?? "").trim(),
|
|
35
|
+
authUrl: getAuthUrl(),
|
|
36
|
+
apiBase: getActiveApiBase(profile),
|
|
37
|
+
shellUrl: getShellRuntimeUrl(),
|
|
38
|
+
registryUrl: getRegistryUrl(),
|
|
39
|
+
dashboardUrl: getConfiguredDashboard(profile),
|
|
40
|
+
docsUrl: getDocsUrl(),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export class HostedLocalhostEndpointError extends Error {
|
|
44
|
+
localhostEndpoints;
|
|
45
|
+
constructor(localhostEndpoints) {
|
|
46
|
+
const detail = localhostEndpoints.map(({ key, url }) => `${key}=${url}`).join(", ");
|
|
47
|
+
super(`Hosted mode cannot use localhost service URLs (${detail}). ` +
|
|
48
|
+
"Set DUCT_URL to your platform origin, use DUCT_PROFILE=local for local dev, " +
|
|
49
|
+
"or set DUCT_ALLOW_LOCALHOST=true to override.");
|
|
50
|
+
this.name = "HostedLocalhostEndpointError";
|
|
51
|
+
this.localhostEndpoints = localhostEndpoints;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function assertHostedEndpointsNotLocalhost(profile) {
|
|
55
|
+
if (allowsLocalhostEndpoints())
|
|
56
|
+
return;
|
|
57
|
+
const endpoints = resolveServiceEndpoints(profile);
|
|
58
|
+
const serviceKeys = [
|
|
59
|
+
"authUrl",
|
|
60
|
+
"apiBase",
|
|
61
|
+
"shellUrl",
|
|
62
|
+
"registryUrl",
|
|
63
|
+
"dashboardUrl",
|
|
64
|
+
];
|
|
65
|
+
const localhostEndpoints = serviceKeys
|
|
66
|
+
.map((key) => ({ key, url: endpoints[key] }))
|
|
67
|
+
.filter(({ url }) => url && isLocalhostUrl(url));
|
|
68
|
+
if (localhostEndpoints.length > 0) {
|
|
69
|
+
throw new HostedLocalhostEndpointError(localhostEndpoints);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { allowsLocalhostEndpoints, assertHostedEndpointsNotLocalhost, getDuctProfile, HostedLocalhostEndpointError, isLocalhostUrl, } from "./endpoint-guard.js";
|
|
4
|
+
const ORIGINAL_ENV = { ...process.env };
|
|
5
|
+
function restoreEnv() {
|
|
6
|
+
for (const key of Object.keys(process.env)) {
|
|
7
|
+
if (!(key in ORIGINAL_ENV))
|
|
8
|
+
delete process.env[key];
|
|
9
|
+
}
|
|
10
|
+
for (const [key, value] of Object.entries(ORIGINAL_ENV)) {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
delete process.env[key];
|
|
13
|
+
else
|
|
14
|
+
process.env[key] = value;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
describe("endpoint-guard", () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
delete process.env.DUCT_PROFILE;
|
|
20
|
+
delete process.env.DUCT_ALLOW_LOCALHOST;
|
|
21
|
+
delete process.env.DUCT_URL;
|
|
22
|
+
delete process.env.DUCT_AUTH_URL;
|
|
23
|
+
delete process.env.DUCT_API_URL;
|
|
24
|
+
delete process.env.DUCT_SHELL_URL;
|
|
25
|
+
delete process.env.DUCT_DASHBOARD_URL;
|
|
26
|
+
delete process.env.DUCT_REGISTRY_URL;
|
|
27
|
+
});
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
restoreEnv();
|
|
30
|
+
});
|
|
31
|
+
it("detects localhost URLs", () => {
|
|
32
|
+
assert.equal(isLocalhostUrl("http://localhost:3002"), true);
|
|
33
|
+
assert.equal(isLocalhostUrl("http://127.0.0.1:3001"), true);
|
|
34
|
+
assert.equal(isLocalhostUrl("https://api.example.com"), false);
|
|
35
|
+
});
|
|
36
|
+
it("treats DUCT_PROFILE=local as allowing localhost", () => {
|
|
37
|
+
process.env.DUCT_PROFILE = "local";
|
|
38
|
+
assert.equal(getDuctProfile(), "local");
|
|
39
|
+
assert.equal(allowsLocalhostEndpoints(), true);
|
|
40
|
+
assert.doesNotThrow(() => assertHostedEndpointsNotLocalhost());
|
|
41
|
+
});
|
|
42
|
+
it("fails hosted mode when endpoints resolve to localhost", () => {
|
|
43
|
+
assert.throws(() => assertHostedEndpointsNotLocalhost(), (err) => {
|
|
44
|
+
assert.ok(err instanceof HostedLocalhostEndpointError);
|
|
45
|
+
assert.ok(err.localhostEndpoints.length > 0);
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it("allows localhost in hosted mode when DUCT_ALLOW_LOCALHOST=true", () => {
|
|
50
|
+
process.env.DUCT_ALLOW_LOCALHOST = "true";
|
|
51
|
+
assert.doesNotThrow(() => assertHostedEndpointsNotLocalhost());
|
|
52
|
+
});
|
|
53
|
+
it("passes hosted mode when DUCT_URL points to a remote origin", () => {
|
|
54
|
+
process.env.DUCT_URL = "https://acme.duct.dev";
|
|
55
|
+
assert.doesNotThrow(() => assertHostedEndpointsNotLocalhost());
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EndpointProfile } from "./endpoints.js";
|
|
2
|
+
export declare function shouldRedactEnvKey(key: string): boolean;
|
|
3
|
+
export declare function redactEnvValue(key: string, value: string): string;
|
|
4
|
+
export interface EnvDiagnosticLine {
|
|
5
|
+
key: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function collectEnvDiagnosticLines(profile?: EndpointProfile | null, options?: {
|
|
9
|
+
redact?: boolean;
|
|
10
|
+
}): EnvDiagnosticLine[];
|
|
11
|
+
export declare function formatEnvDiagnostics(profile?: EndpointProfile | null, options?: {
|
|
12
|
+
redact?: boolean;
|
|
13
|
+
}): string;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — CLI env diagnostics (redacted output)
|
|
3
|
+
// ============================================================
|
|
4
|
+
import { resolveServiceEndpoints, getDuctProfile } from "./endpoint-guard.js";
|
|
5
|
+
const SECRET_ENV_KEYS = new Set([
|
|
6
|
+
"DUCT_API_KEY",
|
|
7
|
+
"DUCT_SECRET_KEY",
|
|
8
|
+
"ANTHROPIC_API_KEY",
|
|
9
|
+
"OPENAI_API_KEY",
|
|
10
|
+
"OPENROUTER_API_KEY",
|
|
11
|
+
"JWT_SECRET",
|
|
12
|
+
"INTERNAL_SERVICE_SECRET",
|
|
13
|
+
"SHELL_RUNTIME_SECRET",
|
|
14
|
+
"NPM_TOKEN",
|
|
15
|
+
"NODE_AUTH_TOKEN",
|
|
16
|
+
]);
|
|
17
|
+
const SECRET_ENV_PATTERN = /(secret|token|password|api[_-]?key|auth)/i;
|
|
18
|
+
export function shouldRedactEnvKey(key) {
|
|
19
|
+
return SECRET_ENV_KEYS.has(key) || SECRET_ENV_PATTERN.test(key);
|
|
20
|
+
}
|
|
21
|
+
export function redactEnvValue(key, value) {
|
|
22
|
+
if (!value)
|
|
23
|
+
return value;
|
|
24
|
+
if (!shouldRedactEnvKey(key))
|
|
25
|
+
return value;
|
|
26
|
+
if (value.length <= 8)
|
|
27
|
+
return "***";
|
|
28
|
+
return `${value.slice(0, 4)}...${value.slice(-4)}`;
|
|
29
|
+
}
|
|
30
|
+
export function collectEnvDiagnosticLines(profile, options) {
|
|
31
|
+
const redact = options?.redact ?? false;
|
|
32
|
+
const lines = [];
|
|
33
|
+
const endpoints = resolveServiceEndpoints(profile);
|
|
34
|
+
lines.push({ key: "DUCT_PROFILE", value: getDuctProfile() || "(default: hosted)" });
|
|
35
|
+
lines.push({ key: "DUCT_URL", value: endpoints.ductUrl || "(not set)" });
|
|
36
|
+
lines.push({ key: "DUCT_AUTH_URL", value: endpoints.authUrl });
|
|
37
|
+
lines.push({ key: "DUCT_API_URL", value: endpoints.apiBase });
|
|
38
|
+
lines.push({ key: "DUCT_SHELL_URL", value: endpoints.shellUrl });
|
|
39
|
+
lines.push({ key: "DUCT_REGISTRY_URL", value: endpoints.registryUrl });
|
|
40
|
+
lines.push({ key: "DUCT_DASHBOARD_URL", value: endpoints.dashboardUrl });
|
|
41
|
+
lines.push({ key: "DUCT_DOCS_URL", value: endpoints.docsUrl });
|
|
42
|
+
const trackedKeys = [
|
|
43
|
+
"DUCT_SHELL_ID",
|
|
44
|
+
"DUCT_API_KEY",
|
|
45
|
+
"DUCT_SECRET_KEY",
|
|
46
|
+
"DUCT_ALLOW_LOCALHOST",
|
|
47
|
+
];
|
|
48
|
+
for (const key of trackedKeys) {
|
|
49
|
+
const raw = process.env[key];
|
|
50
|
+
if (raw === undefined)
|
|
51
|
+
continue;
|
|
52
|
+
lines.push({
|
|
53
|
+
key,
|
|
54
|
+
value: redact ? redactEnvValue(key, raw) : raw,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
export function formatEnvDiagnostics(profile, options) {
|
|
60
|
+
return collectEnvDiagnosticLines(profile, options)
|
|
61
|
+
.map(({ key, value }) => `${key}=${value}`)
|
|
62
|
+
.join("\n");
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { collectEnvDiagnosticLines, redactEnvValue, shouldRedactEnvKey, } from "./env-diagnostics.js";
|
|
4
|
+
describe("env-diagnostics", () => {
|
|
5
|
+
it("flags secret-like env keys for redaction", () => {
|
|
6
|
+
assert.equal(shouldRedactEnvKey("DUCT_SECRET_KEY"), true);
|
|
7
|
+
assert.equal(shouldRedactEnvKey("DUCT_SHELL_ID"), false);
|
|
8
|
+
assert.equal(shouldRedactEnvKey("MY_AUTH_TOKEN"), true);
|
|
9
|
+
});
|
|
10
|
+
it("redacts secret values while preserving shape", () => {
|
|
11
|
+
assert.equal(redactEnvValue("DUCT_API_KEY", "dk_live_abcdefghijklmnop"), "dk_l...mnop");
|
|
12
|
+
assert.equal(redactEnvValue("DUCT_SHELL_ID", "shell_acme_prod"), "shell_acme_prod");
|
|
13
|
+
});
|
|
14
|
+
it("collectEnvDiagnosticLines redacts tracked secrets when requested", () => {
|
|
15
|
+
const previous = process.env.DUCT_API_KEY;
|
|
16
|
+
process.env.DUCT_API_KEY = "dk_live_secretvalue123456";
|
|
17
|
+
try {
|
|
18
|
+
const lines = collectEnvDiagnosticLines(null, { redact: true });
|
|
19
|
+
const apiKeyLine = lines.find((line) => line.key === "DUCT_API_KEY");
|
|
20
|
+
assert.ok(apiKeyLine);
|
|
21
|
+
assert.match(apiKeyLine.value, /\.\.\./);
|
|
22
|
+
assert.doesNotMatch(apiKeyLine.value, /secretvalue123456/);
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
if (previous === undefined)
|
|
26
|
+
delete process.env.DUCT_API_KEY;
|
|
27
|
+
else
|
|
28
|
+
process.env.DUCT_API_KEY = previous;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type DuctProfileName = string;
|
|
2
|
+
export interface ServiceProfileUrls {
|
|
3
|
+
ductUrl?: string;
|
|
4
|
+
authUrl?: string;
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
shellUrl?: string;
|
|
7
|
+
registryUrl?: string;
|
|
8
|
+
dashboardUrl?: string;
|
|
9
|
+
docsUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface DuctProfileFile {
|
|
12
|
+
active?: string;
|
|
13
|
+
profiles?: Record<string, ServiceProfileUrls>;
|
|
14
|
+
}
|
|
15
|
+
export declare const DEFAULT_PROFILE_NAME = "hosted";
|
|
16
|
+
/** Built-in URL presets — hosted leaves URLs empty so DUCT_URL derivation applies. */
|
|
17
|
+
export declare const BUILTIN_PROFILE_URLS: Record<string, ServiceProfileUrls>;
|
|
18
|
+
export declare function getDuctProfileFilePath(customPath?: string): string;
|
|
19
|
+
export declare function readProfileFileSync(profilePath?: string): DuctProfileFile | null;
|
|
20
|
+
export declare function writeProfileFileSync(file: DuctProfileFile, profilePath?: string): void;
|
|
21
|
+
export declare function listProfileNames(file?: DuctProfileFile | null): string[];
|
|
22
|
+
export declare function resolveActiveProfileName(file?: DuctProfileFile | null, env?: NodeJS.ProcessEnv): DuctProfileName;
|
|
23
|
+
export declare function resolveProfileUrls(profileName: string, file?: DuctProfileFile | null): ServiceProfileUrls;
|
|
24
|
+
export declare function applyProfileUrlsToEnv(profileName: string, options?: {
|
|
25
|
+
file?: DuctProfileFile | null;
|
|
26
|
+
env?: NodeJS.ProcessEnv;
|
|
27
|
+
/** Keys present in the shell environment before .env / profile loads — never overwritten. */
|
|
28
|
+
protectedKeys?: ReadonlySet<string>;
|
|
29
|
+
}): void;
|
|
30
|
+
/**
|
|
31
|
+
* Load ~/.duct/profile.json and apply the active profile's URLs to process.env.
|
|
32
|
+
* Returns the resolved active profile name.
|
|
33
|
+
*/
|
|
34
|
+
export declare function applyActiveProfileEnvSync(options?: {
|
|
35
|
+
profilePath?: string;
|
|
36
|
+
protectedKeys?: ReadonlySet<string>;
|
|
37
|
+
env?: NodeJS.ProcessEnv;
|
|
38
|
+
}): DuctProfileName;
|
|
39
|
+
export declare function setActiveProfileSync(profileName: string, options?: {
|
|
40
|
+
profilePath?: string;
|
|
41
|
+
}): DuctProfileFile;
|
|
42
|
+
export declare function ensureProfileExists(profileName: string, urls?: ServiceProfileUrls, options?: {
|
|
43
|
+
profilePath?: string;
|
|
44
|
+
}): DuctProfileFile;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Platform profile discovery (SDK-01)
|
|
3
|
+
//
|
|
4
|
+
// Named profiles map service URLs for local vs hosted stacks.
|
|
5
|
+
// Resolution order for the active profile name:
|
|
6
|
+
// 1. DUCT_PROFILE env (shell wins over files)
|
|
7
|
+
// 2. ~/.duct/profile.json "active"
|
|
8
|
+
// 3. "hosted" (default)
|
|
9
|
+
//
|
|
10
|
+
// URL resolution per key (DUCT_URL, DUCT_API_URL, …):
|
|
11
|
+
// 1. Existing process.env (shell or project .env)
|
|
12
|
+
// 2. Active named profile in ~/.duct/profile.json
|
|
13
|
+
// 3. Built-in preset for local / hosted
|
|
14
|
+
// ============================================================
|
|
15
|
+
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import os from "node:os";
|
|
18
|
+
export const DEFAULT_PROFILE_NAME = "hosted";
|
|
19
|
+
/** Built-in URL presets — hosted leaves URLs empty so DUCT_URL derivation applies. */
|
|
20
|
+
export const BUILTIN_PROFILE_URLS = {
|
|
21
|
+
local: {
|
|
22
|
+
authUrl: "http://localhost:3001",
|
|
23
|
+
apiUrl: "http://localhost:3002",
|
|
24
|
+
shellUrl: "http://localhost:3000",
|
|
25
|
+
registryUrl: "http://localhost:3003",
|
|
26
|
+
dashboardUrl: "http://localhost:3004",
|
|
27
|
+
docsUrl: "http://localhost:3005/docs",
|
|
28
|
+
},
|
|
29
|
+
hosted: {},
|
|
30
|
+
};
|
|
31
|
+
const PROFILE_ENV_MAP = [
|
|
32
|
+
{ profileKey: "ductUrl", envKey: "DUCT_URL" },
|
|
33
|
+
{ profileKey: "authUrl", envKey: "DUCT_AUTH_URL" },
|
|
34
|
+
{ profileKey: "apiUrl", envKey: "DUCT_API_URL" },
|
|
35
|
+
{ profileKey: "shellUrl", envKey: "DUCT_SHELL_URL" },
|
|
36
|
+
{ profileKey: "registryUrl", envKey: "DUCT_REGISTRY_URL" },
|
|
37
|
+
{ profileKey: "dashboardUrl", envKey: "DUCT_DASHBOARD_URL" },
|
|
38
|
+
{ profileKey: "docsUrl", envKey: "DUCT_DOCS_URL" },
|
|
39
|
+
];
|
|
40
|
+
export function getDuctProfileFilePath(customPath) {
|
|
41
|
+
return customPath ?? path.join(os.homedir(), ".duct", "profile.json");
|
|
42
|
+
}
|
|
43
|
+
export function readProfileFileSync(profilePath) {
|
|
44
|
+
const filePath = getDuctProfileFilePath(profilePath);
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(readFileSync(filePath, "utf8"));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export function writeProfileFileSync(file, profilePath) {
|
|
53
|
+
const filePath = getDuctProfileFilePath(profilePath);
|
|
54
|
+
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
55
|
+
writeFileSync(filePath, `${JSON.stringify(file, null, 2)}\n`, "utf8");
|
|
56
|
+
}
|
|
57
|
+
export function listProfileNames(file) {
|
|
58
|
+
const names = new Set(Object.keys(BUILTIN_PROFILE_URLS));
|
|
59
|
+
if (file?.profiles) {
|
|
60
|
+
for (const name of Object.keys(file.profiles)) {
|
|
61
|
+
names.add(name);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return [...names].sort();
|
|
65
|
+
}
|
|
66
|
+
export function resolveActiveProfileName(file, env = process.env) {
|
|
67
|
+
const fromEnv = (env["DUCT_PROFILE"] ?? "").trim();
|
|
68
|
+
if (fromEnv)
|
|
69
|
+
return fromEnv;
|
|
70
|
+
const fromFile = (file?.active ?? "").trim();
|
|
71
|
+
if (fromFile)
|
|
72
|
+
return fromFile;
|
|
73
|
+
return DEFAULT_PROFILE_NAME;
|
|
74
|
+
}
|
|
75
|
+
export function resolveProfileUrls(profileName, file) {
|
|
76
|
+
const builtin = BUILTIN_PROFILE_URLS[profileName] ?? {};
|
|
77
|
+
const fromFile = file?.profiles?.[profileName] ?? {};
|
|
78
|
+
return { ...builtin, ...fromFile };
|
|
79
|
+
}
|
|
80
|
+
export function applyProfileUrlsToEnv(profileName, options) {
|
|
81
|
+
const env = options?.env ?? process.env;
|
|
82
|
+
const protectedKeys = options?.protectedKeys;
|
|
83
|
+
const urls = resolveProfileUrls(profileName, options?.file);
|
|
84
|
+
for (const { profileKey, envKey } of PROFILE_ENV_MAP) {
|
|
85
|
+
if (protectedKeys?.has(envKey))
|
|
86
|
+
continue;
|
|
87
|
+
if ((env[envKey] ?? "").trim())
|
|
88
|
+
continue;
|
|
89
|
+
const value = urls[profileKey];
|
|
90
|
+
if (value)
|
|
91
|
+
env[envKey] = value;
|
|
92
|
+
}
|
|
93
|
+
if (!protectedKeys?.has("DUCT_PROFILE") && !(env["DUCT_PROFILE"] ?? "").trim()) {
|
|
94
|
+
env["DUCT_PROFILE"] = profileName;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Load ~/.duct/profile.json and apply the active profile's URLs to process.env.
|
|
99
|
+
* Returns the resolved active profile name.
|
|
100
|
+
*/
|
|
101
|
+
export function applyActiveProfileEnvSync(options) {
|
|
102
|
+
const file = readProfileFileSync(options?.profilePath);
|
|
103
|
+
const profileName = resolveActiveProfileName(file, options?.env);
|
|
104
|
+
applyProfileUrlsToEnv(profileName, {
|
|
105
|
+
file,
|
|
106
|
+
env: options?.env,
|
|
107
|
+
protectedKeys: options?.protectedKeys,
|
|
108
|
+
});
|
|
109
|
+
return profileName;
|
|
110
|
+
}
|
|
111
|
+
export function setActiveProfileSync(profileName, options) {
|
|
112
|
+
const filePath = getDuctProfileFilePath(options?.profilePath);
|
|
113
|
+
const file = readProfileFileSync(filePath) ?? { profiles: {} };
|
|
114
|
+
file.active = profileName;
|
|
115
|
+
if (!file.profiles)
|
|
116
|
+
file.profiles = {};
|
|
117
|
+
writeProfileFileSync(file, filePath);
|
|
118
|
+
return file;
|
|
119
|
+
}
|
|
120
|
+
export function ensureProfileExists(profileName, urls = {}, options) {
|
|
121
|
+
const filePath = getDuctProfileFilePath(options?.profilePath);
|
|
122
|
+
const file = readProfileFileSync(filePath) ?? { profiles: {} };
|
|
123
|
+
if (!file.profiles)
|
|
124
|
+
file.profiles = {};
|
|
125
|
+
file.profiles[profileName] = { ...(file.profiles[profileName] ?? {}), ...urls };
|
|
126
|
+
writeProfileFileSync(file, filePath);
|
|
127
|
+
return file;
|
|
128
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|