iit-mcp-autotask 0.54.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/.env.example +123 -0
- package/LICENSE +21 -0
- package/README.md +788 -0
- package/capabilities.json +487 -0
- package/dist/access/gate.d.ts +34 -0
- package/dist/access/gate.js +76 -0
- package/dist/access/grants.d.ts +20 -0
- package/dist/access/grants.js +35 -0
- package/dist/access/write-env.d.ts +41 -0
- package/dist/access/write-env.js +166 -0
- package/dist/auth.d.ts +74 -0
- package/dist/auth.js +129 -0
- package/dist/base-hosts.d.ts +38 -0
- package/dist/base-hosts.js +70 -0
- package/dist/config/load.d.ts +28 -0
- package/dist/config/load.js +105 -0
- package/dist/config/schema.d.ts +166 -0
- package/dist/config/schema.js +311 -0
- package/dist/credential.d.ts +28 -0
- package/dist/credential.js +32 -0
- package/dist/doctor.d.ts +112 -0
- package/dist/doctor.js +154 -0
- package/dist/http-guard.d.ts +57 -0
- package/dist/http-guard.js +134 -0
- package/dist/http.d.ts +50 -0
- package/dist/http.js +290 -0
- package/dist/lifecycle.d.ts +29 -0
- package/dist/lifecycle.js +52 -0
- package/dist/logger.d.ts +26 -0
- package/dist/logger.js +28 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +283 -0
- package/dist/policy.d.ts +54 -0
- package/dist/policy.js +80 -0
- package/dist/tools/aggregate.d.ts +16 -0
- package/dist/tools/aggregate.js +64 -0
- package/dist/tools/capabilities-manifest.d.ts +4 -0
- package/dist/tools/capabilities-manifest.js +94 -0
- package/dist/tools/catalog-data.d.ts +116 -0
- package/dist/tools/catalog-data.js +207 -0
- package/dist/tools/clock.d.ts +17 -0
- package/dist/tools/clock.js +43 -0
- package/dist/tools/composites.d.ts +36 -0
- package/dist/tools/composites.js +305 -0
- package/dist/tools/context/client.d.ts +41 -0
- package/dist/tools/context/client.js +139 -0
- package/dist/tools/context/derive.d.ts +9 -0
- package/dist/tools/context/derive.js +32 -0
- package/dist/tools/context/scope.d.ts +24 -0
- package/dist/tools/context/scope.js +180 -0
- package/dist/tools/context/types.d.ts +141 -0
- package/dist/tools/context/types.js +2 -0
- package/dist/tools/dispatch.d.ts +78 -0
- package/dist/tools/dispatch.js +272 -0
- package/dist/tools/field-pointer.d.ts +25 -0
- package/dist/tools/field-pointer.js +45 -0
- package/dist/tools/guide.d.ts +30 -0
- package/dist/tools/guide.js +97 -0
- package/dist/tools/lists.d.ts +9 -0
- package/dist/tools/lists.js +205 -0
- package/dist/tools/meta.d.ts +22 -0
- package/dist/tools/meta.js +709 -0
- package/dist/tools/output-file.d.ts +26 -0
- package/dist/tools/output-file.js +140 -0
- package/dist/tools/rank.d.ts +21 -0
- package/dist/tools/rank.js +57 -0
- package/dist/tools/reasons.d.ts +33 -0
- package/dist/tools/reasons.js +121 -0
- package/dist/tools/register.d.ts +23 -0
- package/dist/tools/register.js +50 -0
- package/dist/tools/resolves.d.ts +28 -0
- package/dist/tools/resolves.js +399 -0
- package/dist/tools/results/envelope.d.ts +55 -0
- package/dist/tools/results/envelope.js +63 -0
- package/dist/tools/results/errors.d.ts +147 -0
- package/dist/tools/results/errors.js +384 -0
- package/dist/tools/results/untrusted.d.ts +129 -0
- package/dist/tools/results/untrusted.js +218 -0
- package/dist/tools/revenue-rollup.d.ts +8 -0
- package/dist/tools/revenue-rollup.js +112 -0
- package/dist/tools/schemas.d.ts +467 -0
- package/dist/tools/schemas.js +444 -0
- package/dist/tools/scope-guard.d.ts +2 -0
- package/dist/tools/scope-guard.js +11 -0
- package/dist/tools/server-version.d.ts +3 -0
- package/dist/tools/server-version.js +14 -0
- package/dist/tools/server.d.ts +92 -0
- package/dist/tools/server.js +170 -0
- package/dist/tools/surface.d.ts +37 -0
- package/dist/tools/surface.js +94 -0
- package/dist/tools/tsv.d.ts +52 -0
- package/dist/tools/tsv.js +95 -0
- package/dist/tools/write/apply.d.ts +40 -0
- package/dist/tools/write/apply.js +500 -0
- package/dist/tools/write/checks.d.ts +39 -0
- package/dist/tools/write/checks.js +137 -0
- package/dist/tools/write/common.d.ts +23 -0
- package/dist/tools/write/common.js +33 -0
- package/dist/tools/write/facts.d.ts +52 -0
- package/dist/tools/write/facts.js +151 -0
- package/dist/tools/write/outcome.d.ts +23 -0
- package/dist/tools/write/outcome.js +67 -0
- package/dist/tools/write/parse.d.ts +37 -0
- package/dist/tools/write/parse.js +166 -0
- package/dist/tools/write/payload.d.ts +44 -0
- package/dist/tools/write/payload.js +119 -0
- package/dist/tools/write/preview.d.ts +3 -0
- package/dist/tools/write/preview.js +317 -0
- package/dist/tools/write/snapshot.d.ts +68 -0
- package/dist/tools/write/snapshot.js +360 -0
- package/dist/tools/write-guidance.d.ts +174 -0
- package/dist/tools/write-guidance.js +317 -0
- package/dist/tools/write.d.ts +9 -0
- package/dist/tools/write.js +10 -0
- package/dist/write/canonical.d.ts +12 -0
- package/dist/write/canonical.js +31 -0
- package/dist/write/evaluate.d.ts +77 -0
- package/dist/write/evaluate.js +91 -0
- package/dist/write/hard-rules.d.ts +49 -0
- package/dist/write/hard-rules.js +48 -0
- package/dist/write/runtime.d.ts +37 -0
- package/dist/write/runtime.js +72 -0
- package/dist/write/store/audit-log.d.ts +13 -0
- package/dist/write/store/audit-log.js +25 -0
- package/dist/write/store/constants.d.ts +34 -0
- package/dist/write/store/constants.js +35 -0
- package/dist/write/store/files.d.ts +44 -0
- package/dist/write/store/files.js +157 -0
- package/dist/write/store/heartbeat.d.ts +9 -0
- package/dist/write/store/heartbeat.js +65 -0
- package/dist/write/store/ledger.d.ts +15 -0
- package/dist/write/store/ledger.js +86 -0
- package/dist/write/store/lifecycle.d.ts +35 -0
- package/dist/write/store/lifecycle.js +114 -0
- package/dist/write/store/lock.d.ts +97 -0
- package/dist/write/store/lock.js +464 -0
- package/dist/write/store/op-lock.d.ts +13 -0
- package/dist/write/store/op-lock.js +175 -0
- package/dist/write/store/plan-store.d.ts +7 -0
- package/dist/write/store/plan-store.js +48 -0
- package/dist/write/store/plans.d.ts +5 -0
- package/dist/write/store/plans.js +19 -0
- package/dist/write/store/types.d.ts +195 -0
- package/dist/write/store/types.js +2 -0
- package/dist/write/store.d.ts +6 -0
- package/dist/write/store.js +182 -0
- package/examples/agentgateway.write-gate.yaml +29 -0
- package/package.json +71 -0
- package/server.json +69 -0
- package/skills/ar-aging.md +65 -0
- package/skills/company-360.md +69 -0
- package/skills/contract-profitability.md +65 -0
- package/skills/crm-management.md +87 -0
- package/skills/filter-semantics.md +75 -0
- package/skills/invoice-export.md +61 -0
- package/skills/log-time-entry.md +77 -0
- package/skills/non-chargeable-work.md +67 -0
- package/skills/portfolio-margin.md +66 -0
- package/skills/recurring-revenue.md +65 -0
- package/skills/revenue-rollup.md +65 -0
- package/skills/slim-reads.md +67 -0
- package/skills/ticket-triage.md +64 -0
- package/skills/unbilled-work-review.md +66 -0
- package/skills/wip-hours.md +68 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type WriteShape } from '../write/hard-rules.js';
|
|
2
|
+
/**
|
|
3
|
+
* `confirm`: the server asks for every write (stdio). `allow`: the server asks only when a hard rule hits
|
|
4
|
+
* (stdio), never on HTTP. `unattended`: never asks, hard rules waived (stdio). `deny`: refused.
|
|
5
|
+
*/
|
|
6
|
+
export type WriteMode = 'allow' | 'deny' | 'confirm' | 'unattended';
|
|
7
|
+
export type Transport = 'stdio' | 'http';
|
|
8
|
+
/**
|
|
9
|
+
* `hardRule`: the hard rule that sends this op to `autotask_apply_destructive` (and, on stdio, to the form):
|
|
10
|
+
* `always` (SDK requiresApproval or sensitive) or `move` (a move that changes company or contract).
|
|
11
|
+
*/
|
|
12
|
+
export interface ResolvedOp {
|
|
13
|
+
mode: WriteMode;
|
|
14
|
+
rule: string;
|
|
15
|
+
shape: WriteShape;
|
|
16
|
+
hardRule?: 'always' | 'move';
|
|
17
|
+
dedup?: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
/** `expensePin`: the one submitter id a company-less `expense_reports.create` is admitted for (absent = no exception). */
|
|
20
|
+
export interface LoadedPolicy {
|
|
21
|
+
table: ReadonlyMap<string, ResolvedOp>;
|
|
22
|
+
expensePin?: number;
|
|
23
|
+
policyHash: string;
|
|
24
|
+
transport: Transport;
|
|
25
|
+
warnings: string[];
|
|
26
|
+
}
|
|
27
|
+
/** The hard rule an op hits whatever its payload (`always`), or the move escalation it may hit (`move`). */
|
|
28
|
+
export declare function hardRuleOf(key: string): 'always' | 'move' | undefined;
|
|
29
|
+
/** `transport` defaults to stdio for the unit tests; every boot caller passes MCP_TRANSPORT. */
|
|
30
|
+
export declare function resolveWritePolicy(env: {
|
|
31
|
+
write: string;
|
|
32
|
+
dedup?: string;
|
|
33
|
+
expensePin?: string;
|
|
34
|
+
transport?: Transport;
|
|
35
|
+
}): LoadedPolicy;
|
|
36
|
+
/**
|
|
37
|
+
* Whether the server itself may ask a person through this apply tool (stdio only: never on HTTP).
|
|
38
|
+
* `destructive`: autotask_apply_destructive (hard-rule plans under confirm or allow); otherwise
|
|
39
|
+
* autotask_apply (ordinary writes under confirm).
|
|
40
|
+
*/
|
|
41
|
+
export declare const anyGate: (loaded: LoadedPolicy, destructive: boolean) => boolean;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* access/write-env.ts — parse and resolve the env write grammar: `AUTOTASK_WRITE` (per-operation
|
|
3
|
+
* modes), `AUTOTASK_DEDUP` and the optional expense submitter pin.
|
|
4
|
+
* The transport decides who gates (#302): stdio takes `confirm` / `allow` / `unattended` / `deny`
|
|
5
|
+
* (the server asks the person); HTTP takes `allow` / `deny` only (the client or gateway asks).
|
|
6
|
+
* Pure: resolution needs only the capability registry. Every problem is collected into one ConfigError.
|
|
7
|
+
*/
|
|
8
|
+
import { ConfigError } from '../config/schema.js';
|
|
9
|
+
import { hashOf } from '../write/canonical.js';
|
|
10
|
+
import { hardRuleFor, MOVE_OPS, writeShape } from '../write/hard-rules.js';
|
|
11
|
+
import { RECORDS, recordById } from '../tools/catalog-data.js';
|
|
12
|
+
const MODES = ['allow', 'deny', 'confirm', 'unattended'];
|
|
13
|
+
const SERVER_GATED = ['confirm', 'unattended'];
|
|
14
|
+
const FAMILY = /^[a-z][a-z0-9_]*\.\*$/;
|
|
15
|
+
const EXACT = /^[A-Za-z][A-Za-z0-9_]*\.[A-Za-z][A-Za-z0-9]*$/;
|
|
16
|
+
const FIELD = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
17
|
+
/** Operations the preview/apply path serves: every write and destructive record the hard rules do not refuse. */
|
|
18
|
+
const servable = () => {
|
|
19
|
+
const out = new Map();
|
|
20
|
+
for (const r of RECORDS) {
|
|
21
|
+
if (hardRuleFor(r.id, r).never === undefined)
|
|
22
|
+
out.set(r.id, writeShape(r));
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
const fail = (name, problems) => {
|
|
27
|
+
if (problems.length > 0)
|
|
28
|
+
throw new ConfigError(`${name} is invalid:\n ${problems.join('\n ')}`);
|
|
29
|
+
};
|
|
30
|
+
/** `*:deny,tickets.*:allow,...` -> pattern -> mode, rejecting malformed entries. */
|
|
31
|
+
function parseWrite(raw, ops, transport, problems) {
|
|
32
|
+
const rules = new Map();
|
|
33
|
+
for (const entry of raw.split(',').map((e) => e.trim())) {
|
|
34
|
+
if (entry === '') {
|
|
35
|
+
problems.push('empty entry');
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const cut = entry.lastIndexOf(':');
|
|
39
|
+
const pattern = (cut < 0 ? entry : entry.slice(0, cut)).trim();
|
|
40
|
+
const mode = cut < 0 ? '' : entry.slice(cut + 1).trim();
|
|
41
|
+
if (mode === 'gate') {
|
|
42
|
+
problems.push(`${entry}: the gate mode was removed; ${transport === 'stdio' ? 'use :confirm' : 'on HTTP use :allow and gate in the client'}`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!MODES.includes(mode)) {
|
|
46
|
+
problems.push(`${entry}: mode must be ${transport === 'stdio' ? 'confirm, allow, unattended or deny' : 'allow or deny'}`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (transport === 'http' && SERVER_GATED.includes(mode)) {
|
|
50
|
+
problems.push(`${entry}: server-side gating is stdio only; on HTTP use allow/deny and gate in the client`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const exact = pattern !== '*' && !FAMILY.test(pattern) && EXACT.test(pattern);
|
|
54
|
+
if (pattern !== '*' && !FAMILY.test(pattern) && !exact) {
|
|
55
|
+
problems.push(`${pattern}: not *, family.* or an operation key`);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (rules.has(pattern)) {
|
|
59
|
+
problems.push(`${pattern}: listed twice`);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (FAMILY.test(pattern) && ![...ops.keys()].some((k) => k.startsWith(pattern.slice(0, -1)))) {
|
|
63
|
+
problems.push(`${pattern}: matches no servable write`);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (exact && !ops.has(pattern)) {
|
|
67
|
+
// every write is servable, so this is a read, a credential-minting op or an unknown key
|
|
68
|
+
const never = hardRuleFor(pattern, recordById(pattern)).never;
|
|
69
|
+
problems.push(`${pattern}: ${never ?? 'not a served write'}`);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
rules.set(pattern, mode);
|
|
73
|
+
}
|
|
74
|
+
return rules;
|
|
75
|
+
}
|
|
76
|
+
/** The hard rule an op hits whatever its payload (`always`), or the move escalation it may hit (`move`). */
|
|
77
|
+
export function hardRuleOf(key) {
|
|
78
|
+
if (hardRuleFor(key, recordById(key)).approval === 'always')
|
|
79
|
+
return 'always';
|
|
80
|
+
return MOVE_OPS.includes(key) ? 'move' : undefined;
|
|
81
|
+
}
|
|
82
|
+
/** `transport` defaults to stdio for the unit tests; every boot caller passes MCP_TRANSPORT. */
|
|
83
|
+
export function resolveWritePolicy(env) {
|
|
84
|
+
const transport = env.transport ?? 'stdio';
|
|
85
|
+
const ops = servable();
|
|
86
|
+
const problems = [];
|
|
87
|
+
const rules = parseWrite(env.write, ops, transport, problems);
|
|
88
|
+
fail('AUTOTASK_WRITE', problems);
|
|
89
|
+
const table = new Map();
|
|
90
|
+
for (const [key, shape] of [...ops].sort(([a], [b]) => (a < b ? -1 : 1))) {
|
|
91
|
+
const family = `${key.slice(0, key.indexOf('.'))}.*`;
|
|
92
|
+
const rule = [key, family, '*'].find((p) => rules.has(p));
|
|
93
|
+
if (rule === undefined)
|
|
94
|
+
continue;
|
|
95
|
+
const mode = rules.get(rule);
|
|
96
|
+
const hard = hardRuleOf(key);
|
|
97
|
+
table.set(key, { mode, rule, shape, ...(hard !== undefined ? { hardRule: hard } : {}) });
|
|
98
|
+
}
|
|
99
|
+
const dedupProblems = [];
|
|
100
|
+
if (env.dedup !== undefined) {
|
|
101
|
+
const seen = new Set();
|
|
102
|
+
for (const entry of env.dedup.split(',').map((e) => e.trim())) {
|
|
103
|
+
if (entry === '') {
|
|
104
|
+
dedupProblems.push('empty entry');
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const cut = entry.indexOf(':');
|
|
108
|
+
const op = (cut < 0 ? entry : entry.slice(0, cut)).trim();
|
|
109
|
+
const fields = cut < 0 ? [''] : entry.slice(cut + 1).trim().split('+');
|
|
110
|
+
if (seen.has(op)) {
|
|
111
|
+
dedupProblems.push(`${op}: listed twice`);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
seen.add(op);
|
|
115
|
+
const row = table.get(op);
|
|
116
|
+
if (row === undefined || row.mode === 'deny') {
|
|
117
|
+
dedupProblems.push(`${op}: not served (absent or denied in AUTOTASK_WRITE)`);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (row.shape !== 'create' && row.shape !== 'create_child') {
|
|
121
|
+
dedupProblems.push(`${op}: dedup applies to creates only`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!fields.every((f) => FIELD.test(f))) {
|
|
125
|
+
dedupProblems.push(`${op}: each field must be a plain field name joined by +`);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
table.set(op, { ...row, dedup: fields });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
fail('AUTOTASK_DEDUP', dedupProblems);
|
|
132
|
+
const pinProblems = [];
|
|
133
|
+
let expensePin;
|
|
134
|
+
if (env.expensePin !== undefined) {
|
|
135
|
+
if (!/^[1-9]\d{0,14}$/.test(env.expensePin))
|
|
136
|
+
pinProblems.push('use one positive integer resource id');
|
|
137
|
+
else if ((table.get('expense_reports.create')?.mode ?? 'deny') === 'deny')
|
|
138
|
+
pinProblems.push('needs expense_reports.create to be allow in AUTOTASK_WRITE');
|
|
139
|
+
else
|
|
140
|
+
expensePin = Number(env.expensePin);
|
|
141
|
+
}
|
|
142
|
+
fail('AUTOTASK_EXPENSE_REPORT_SUBMITTER_PIN', pinProblems);
|
|
143
|
+
const policyHash = hashOf({
|
|
144
|
+
ops: Object.fromEntries([...table].map(([k, v]) => [k, { mode: v.mode, dedup: v.dedup ?? null }])),
|
|
145
|
+
...(expensePin !== undefined ? { expensePin } : {}),
|
|
146
|
+
});
|
|
147
|
+
return { table, ...(expensePin !== undefined ? { expensePin } : {}), policyHash, transport, warnings: gatingWarnings(table, transport) };
|
|
148
|
+
}
|
|
149
|
+
const RULE_TEXT = { always: 'the always-confirm hard rule (SDK requiresApproval or sensitive)', move: 'the move hard rule (company or contract change)' };
|
|
150
|
+
/** The boot warnings: on HTTP the admitted hard-rule ops the client must gate; on stdio one line per unattended op that waives a hard rule. */
|
|
151
|
+
function gatingWarnings(table, transport) {
|
|
152
|
+
const hard = [...table].filter(([, v]) => v.hardRule !== undefined && v.mode !== 'deny');
|
|
153
|
+
if (transport === 'http') {
|
|
154
|
+
return hard.length === 0 ? [] : [`HTTP: this server never asks a person; the MCP client or gateway must gate these admitted hard-rule writes (stdio would confirm them): ${hard.map(([k]) => k).join(', ')}`];
|
|
155
|
+
}
|
|
156
|
+
return hard.filter(([, v]) => v.mode === 'unattended').map(([k, v]) => `${k}: unattended (from ${v.rule}) waives ${RULE_TEXT[v.hardRule]}; it runs with no confirmation`);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Whether the server itself may ask a person through this apply tool (stdio only: never on HTTP).
|
|
160
|
+
* `destructive`: autotask_apply_destructive (hard-rule plans under confirm or allow); otherwise
|
|
161
|
+
* autotask_apply (ordinary writes under confirm).
|
|
162
|
+
*/
|
|
163
|
+
export const anyGate = (loaded, destructive) => loaded.transport === 'stdio' &&
|
|
164
|
+
// an always-rule op only ever applies through the destructive tool; a move does so only when it escalates
|
|
165
|
+
[...loaded.table.values()].some((v) => (destructive ? v.hardRule !== undefined && (v.mode === 'confirm' || v.mode === 'allow') : v.mode === 'confirm' && v.hardRule !== 'always'));
|
|
166
|
+
//# sourceMappingURL=write-env.js.map
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* auth.ts — caller authentication for the HTTP transport.
|
|
3
|
+
*
|
|
4
|
+
* Five per-request headers carry the caller's scope:
|
|
5
|
+
*
|
|
6
|
+
* - `X-Autotask-Api-Integration-Code` / `X-Autotask-Username` / `X-Autotask-Secret` are the
|
|
7
|
+
* caller's Autotask identity tuple. Under `http` the server normally holds no Autotask
|
|
8
|
+
* credential of its own (an operator MAY set the three `AUTOTASK_*` variables as discouraged
|
|
9
|
+
* defaults for header-less requests — presented to the `AUTOTASK_BASE_URL` default origin
|
|
10
|
+
* only). The three values travel together, all-or-nothing: a request that presents one or two
|
|
11
|
+
* of them is malformed and fails closed (there is no per-field default fill — splicing a
|
|
12
|
+
* caller's code with the server's secret is the bug that fill would enable).
|
|
13
|
+
* - `X-Autotask-Impersonation-Resource-Id` re-scopes the authenticated identity to another
|
|
14
|
+
* resource in the tenant. It is a privilege surface: single-valued, absent when duplicated or
|
|
15
|
+
* blank, never in any tool input schema (no capability may elevate beyond the supplied
|
|
16
|
+
* transport credentials), and its value is logged at `debug` only (the boolean "an
|
|
17
|
+
* impersonation is in play" at `info`).
|
|
18
|
+
* - `X-Autotask-Base-Url` is the zone origin the identity tuple is presented to. Not a secret —
|
|
19
|
+
* the caller supplied it and only ever sees its own value — but it decides the destination,
|
|
20
|
+
* so it is scoped like one: per request, single-valued, absent when duplicated, and only ever
|
|
21
|
+
* presented to a host the allow-list (or the deployment default) names.
|
|
22
|
+
*
|
|
23
|
+
* Neither header ever appears in a tool's input schema.
|
|
24
|
+
*/
|
|
25
|
+
/** The header carrying the caller's ApiIntegrationCode (required for any tool call). */
|
|
26
|
+
export declare const AUTOTASK_INTEGRATION_CODE_HEADER = "x-autotask-api-integration-code";
|
|
27
|
+
/** The header carrying the caller's UserName (required for any tool call). */
|
|
28
|
+
export declare const AUTOTASK_USERNAME_HEADER = "x-autotask-username";
|
|
29
|
+
/** The header carrying the caller's Secret (required for any tool call; never logged). */
|
|
30
|
+
export declare const AUTOTASK_SECRET_HEADER = "x-autotask-secret";
|
|
31
|
+
/** The header carrying the caller's ImpersonationResourceId (optional). */
|
|
32
|
+
export declare const AUTOTASK_IMPERSONATION_HEADER = "x-autotask-impersonation-resource-id";
|
|
33
|
+
/** The header carrying the caller's zone base URL (optional; allow-listed). */
|
|
34
|
+
export declare const AUTOTASK_BASE_URL_HEADER = "x-autotask-base-url";
|
|
35
|
+
/** The gateway-validated client id (`AUTOTASK_HTTP_AUTH=gateway` only). */
|
|
36
|
+
export declare const GATEWAY_CLIENT_ID_HEADER = "x-autotask-client-id";
|
|
37
|
+
/** The gateway-validated per-user principal (`AUTOTASK_HTTP_AUTH=gateway` only; the Entra oid). */
|
|
38
|
+
export declare const GATEWAY_PRINCIPAL_HEADER = "x-autotask-principal";
|
|
39
|
+
/** The headers that carry per-request scope, and so must never be read from a duplicated pair. */
|
|
40
|
+
export declare const CREDENTIAL_HEADERS: readonly string[];
|
|
41
|
+
/** The caller's per-request identity, exactly as the transport supplied it. */
|
|
42
|
+
export interface RequestScope {
|
|
43
|
+
apiIntegrationCode?: string;
|
|
44
|
+
userName?: string;
|
|
45
|
+
secret?: string;
|
|
46
|
+
impersonationResourceId?: string;
|
|
47
|
+
baseUrl?: string;
|
|
48
|
+
}
|
|
49
|
+
/** Read the caller's per-request scope from request headers (all-or-nothing values, see module doc). */
|
|
50
|
+
export declare function readRequestScope(headers: Record<string, string | string[] | undefined>): RequestScope;
|
|
51
|
+
/**
|
|
52
|
+
* Read the identity the gateway stamped on the request. Callers must read it ONLY in gateway
|
|
53
|
+
* mode: anywhere else these headers are caller-controlled and mean nothing. Duplicated or
|
|
54
|
+
* malformed values are absent.
|
|
55
|
+
*/
|
|
56
|
+
export declare function readGatewayIdentity(headers: Record<string, string | string[] | undefined>): {
|
|
57
|
+
clientId?: string;
|
|
58
|
+
principal?: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* How many of the three identity members this scope presents.
|
|
62
|
+
*
|
|
63
|
+
* 0 = a header-less request (server default or credential-less placeholder); 3 = the caller's
|
|
64
|
+
* own identity; 1 or 2 = PARTIAL tuple -> the request is malformed and fails closed (there is no
|
|
65
|
+
* per-field default fill).
|
|
66
|
+
*/
|
|
67
|
+
export declare function credentialTupleCardinality(scope: RequestScope): number;
|
|
68
|
+
/**
|
|
69
|
+
* Collapse a web `Headers` bag into the lowercased record shape the readers expect.
|
|
70
|
+
*
|
|
71
|
+
* A header given twice becomes an array, which the readers treat as absent — see
|
|
72
|
+
* {@link readSingleHeader}. That is the whole reason this cannot just be `Object.fromEntries`.
|
|
73
|
+
*/
|
|
74
|
+
export declare function headerRecord(headers: Headers): Record<string, string | string[] | undefined>;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* auth.ts — caller authentication for the HTTP transport.
|
|
3
|
+
*
|
|
4
|
+
* Five per-request headers carry the caller's scope:
|
|
5
|
+
*
|
|
6
|
+
* - `X-Autotask-Api-Integration-Code` / `X-Autotask-Username` / `X-Autotask-Secret` are the
|
|
7
|
+
* caller's Autotask identity tuple. Under `http` the server normally holds no Autotask
|
|
8
|
+
* credential of its own (an operator MAY set the three `AUTOTASK_*` variables as discouraged
|
|
9
|
+
* defaults for header-less requests — presented to the `AUTOTASK_BASE_URL` default origin
|
|
10
|
+
* only). The three values travel together, all-or-nothing: a request that presents one or two
|
|
11
|
+
* of them is malformed and fails closed (there is no per-field default fill — splicing a
|
|
12
|
+
* caller's code with the server's secret is the bug that fill would enable).
|
|
13
|
+
* - `X-Autotask-Impersonation-Resource-Id` re-scopes the authenticated identity to another
|
|
14
|
+
* resource in the tenant. It is a privilege surface: single-valued, absent when duplicated or
|
|
15
|
+
* blank, never in any tool input schema (no capability may elevate beyond the supplied
|
|
16
|
+
* transport credentials), and its value is logged at `debug` only (the boolean "an
|
|
17
|
+
* impersonation is in play" at `info`).
|
|
18
|
+
* - `X-Autotask-Base-Url` is the zone origin the identity tuple is presented to. Not a secret —
|
|
19
|
+
* the caller supplied it and only ever sees its own value — but it decides the destination,
|
|
20
|
+
* so it is scoped like one: per request, single-valued, absent when duplicated, and only ever
|
|
21
|
+
* presented to a host the allow-list (or the deployment default) names.
|
|
22
|
+
*
|
|
23
|
+
* Neither header ever appears in a tool's input schema.
|
|
24
|
+
*/
|
|
25
|
+
/** The header carrying the caller's ApiIntegrationCode (required for any tool call). */
|
|
26
|
+
export const AUTOTASK_INTEGRATION_CODE_HEADER = 'x-autotask-api-integration-code';
|
|
27
|
+
/** The header carrying the caller's UserName (required for any tool call). */
|
|
28
|
+
export const AUTOTASK_USERNAME_HEADER = 'x-autotask-username';
|
|
29
|
+
/** The header carrying the caller's Secret (required for any tool call; never logged). */
|
|
30
|
+
export const AUTOTASK_SECRET_HEADER = 'x-autotask-secret';
|
|
31
|
+
/** The header carrying the caller's ImpersonationResourceId (optional). */
|
|
32
|
+
export const AUTOTASK_IMPERSONATION_HEADER = 'x-autotask-impersonation-resource-id';
|
|
33
|
+
/** The header carrying the caller's zone base URL (optional; allow-listed). */
|
|
34
|
+
export const AUTOTASK_BASE_URL_HEADER = 'x-autotask-base-url';
|
|
35
|
+
/** The gateway-validated client id (`AUTOTASK_HTTP_AUTH=gateway` only). */
|
|
36
|
+
export const GATEWAY_CLIENT_ID_HEADER = 'x-autotask-client-id';
|
|
37
|
+
/** The gateway-validated per-user principal (`AUTOTASK_HTTP_AUTH=gateway` only; the Entra oid). */
|
|
38
|
+
export const GATEWAY_PRINCIPAL_HEADER = 'x-autotask-principal';
|
|
39
|
+
/** The headers that carry per-request scope, and so must never be read from a duplicated pair. */
|
|
40
|
+
export const CREDENTIAL_HEADERS = [
|
|
41
|
+
AUTOTASK_INTEGRATION_CODE_HEADER,
|
|
42
|
+
AUTOTASK_USERNAME_HEADER,
|
|
43
|
+
AUTOTASK_SECRET_HEADER,
|
|
44
|
+
AUTOTASK_IMPERSONATION_HEADER,
|
|
45
|
+
AUTOTASK_BASE_URL_HEADER,
|
|
46
|
+
];
|
|
47
|
+
/**
|
|
48
|
+
* Read a single-valued header, tolerating Node's array-valued header shape.
|
|
49
|
+
*
|
|
50
|
+
* A duplicated header is treated as absent rather than trusting the first value: a request that
|
|
51
|
+
* presents two different credentials is malformed, and picking one is how request-smuggling bugs
|
|
52
|
+
* start.
|
|
53
|
+
*/
|
|
54
|
+
function readSingleHeader(headers, name) {
|
|
55
|
+
const raw = headers[name];
|
|
56
|
+
if (typeof raw === 'string')
|
|
57
|
+
return raw;
|
|
58
|
+
if (Array.isArray(raw) && raw.length === 1)
|
|
59
|
+
return raw[0];
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
/** A header value usable as a scope value (non-blank after trim), else `undefined`. */
|
|
63
|
+
function usable(value) {
|
|
64
|
+
if (value === undefined)
|
|
65
|
+
return undefined;
|
|
66
|
+
const trimmed = value.trim();
|
|
67
|
+
return trimmed === '' ? undefined : trimmed;
|
|
68
|
+
}
|
|
69
|
+
/** Read the caller's per-request scope from request headers (all-or-nothing values, see module doc). */
|
|
70
|
+
export function readRequestScope(headers) {
|
|
71
|
+
const code = usable(readSingleHeader(headers, AUTOTASK_INTEGRATION_CODE_HEADER));
|
|
72
|
+
const user = usable(readSingleHeader(headers, AUTOTASK_USERNAME_HEADER));
|
|
73
|
+
const secret = usable(readSingleHeader(headers, AUTOTASK_SECRET_HEADER));
|
|
74
|
+
const impersonation = usable(readSingleHeader(headers, AUTOTASK_IMPERSONATION_HEADER));
|
|
75
|
+
const base = usable(readSingleHeader(headers, AUTOTASK_BASE_URL_HEADER));
|
|
76
|
+
return {
|
|
77
|
+
...(code !== undefined ? { apiIntegrationCode: code } : {}),
|
|
78
|
+
...(user !== undefined ? { userName: user } : {}),
|
|
79
|
+
...(secret !== undefined ? { secret: secret } : {}),
|
|
80
|
+
...(impersonation !== undefined ? { impersonationResourceId: impersonation } : {}),
|
|
81
|
+
...(base !== undefined ? { baseUrl: base } : {}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const GATEWAY_ID = /^[A-Za-z0-9._@:-]{1,200}$/;
|
|
85
|
+
/**
|
|
86
|
+
* Read the identity the gateway stamped on the request. Callers must read it ONLY in gateway
|
|
87
|
+
* mode: anywhere else these headers are caller-controlled and mean nothing. Duplicated or
|
|
88
|
+
* malformed values are absent.
|
|
89
|
+
*/
|
|
90
|
+
export function readGatewayIdentity(headers) {
|
|
91
|
+
const valid = (name) => {
|
|
92
|
+
const v = usable(readSingleHeader(headers, name));
|
|
93
|
+
return v !== undefined && GATEWAY_ID.test(v) ? v : undefined;
|
|
94
|
+
};
|
|
95
|
+
const clientId = valid(GATEWAY_CLIENT_ID_HEADER);
|
|
96
|
+
const principal = valid(GATEWAY_PRINCIPAL_HEADER);
|
|
97
|
+
return { ...(clientId !== undefined ? { clientId } : {}), ...(principal !== undefined ? { principal } : {}) };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* How many of the three identity members this scope presents.
|
|
101
|
+
*
|
|
102
|
+
* 0 = a header-less request (server default or credential-less placeholder); 3 = the caller's
|
|
103
|
+
* own identity; 1 or 2 = PARTIAL tuple -> the request is malformed and fails closed (there is no
|
|
104
|
+
* per-field default fill).
|
|
105
|
+
*/
|
|
106
|
+
export function credentialTupleCardinality(scope) {
|
|
107
|
+
return [scope.apiIntegrationCode, scope.userName, scope.secret].filter((v) => v !== undefined).length;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Collapse a web `Headers` bag into the lowercased record shape the readers expect.
|
|
111
|
+
*
|
|
112
|
+
* A header given twice becomes an array, which the readers treat as absent — see
|
|
113
|
+
* {@link readSingleHeader}. That is the whole reason this cannot just be `Object.fromEntries`.
|
|
114
|
+
*/
|
|
115
|
+
export function headerRecord(headers) {
|
|
116
|
+
const out = {};
|
|
117
|
+
headers.forEach((value, name) => {
|
|
118
|
+
const key = name.toLowerCase();
|
|
119
|
+
const existing = out[key];
|
|
120
|
+
if (existing === undefined)
|
|
121
|
+
out[key] = value;
|
|
122
|
+
else if (Array.isArray(existing))
|
|
123
|
+
existing.push(value);
|
|
124
|
+
else
|
|
125
|
+
out[key] = [existing, value];
|
|
126
|
+
});
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* base-hosts.ts — the zone-origin discipline for caller-supplied Autotask base URLs.
|
|
3
|
+
*
|
|
4
|
+
* Under `http` a caller names the origin its identity tuple is presented to
|
|
5
|
+
* (`X-Autotask-Base-Url`), which makes this server an outbound HTTP client for whoever can reach
|
|
6
|
+
* the listener. The zone lives in the HOST (e.g. `webservices6.autotask.net`); the base path is
|
|
7
|
+
* fixed (`/atservicesrest`) and appended by the server. `AUTOTASK_ALLOWED_BASE_HOSTS` is what
|
|
8
|
+
* keeps a caller from pointing the server — and the caller's own credentials — at an internal
|
|
9
|
+
* host or a hostile one.
|
|
10
|
+
*/
|
|
11
|
+
/** The fixed Autotask REST base path. Never configurable (the zone lives in the host). */
|
|
12
|
+
export declare const AUTOTASK_BASE_PATH = "/atservicesrest";
|
|
13
|
+
/**
|
|
14
|
+
* Whether `baseUrl`'s host is permitted by `allowed`.
|
|
15
|
+
*
|
|
16
|
+
* An entry is an exact hostname (`webservices6.autotask.net`) or a `*.` suffix
|
|
17
|
+
* (`*.autotask.net`) that matches subdomains only, never the bare domain (`autotask.net` is NOT
|
|
18
|
+
* matched by `*.autotask.net`). Unset (`undefined`) permits everything at this level; the server
|
|
19
|
+
* factory layers the effective policy on top (default-only when a default origin is set; boot is
|
|
20
|
+
* refused otherwise unless the explicit lab-only opt-in is set). A URL that does not parse is
|
|
21
|
+
* refused rather than passed through, so the SDK's own error is never the gate.
|
|
22
|
+
*/
|
|
23
|
+
export declare function baseHostAllowed(baseUrl: string, allowed: readonly string[] | undefined): boolean;
|
|
24
|
+
/** The origin of `url`, or `undefined` when it does not parse. */
|
|
25
|
+
export declare function originOf(url: string): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Normalize a caller-supplied (or configured) base URL into the SDK's `baseUrl` contract:
|
|
28
|
+
* origin + the fixed `/atservicesrest` path.
|
|
29
|
+
*
|
|
30
|
+
* The value must be an https URL whose path is empty, `/` or the fixed base path itself (a
|
|
31
|
+
* value naming a DIFFERENT path is refused — the base path is not a knob). Unparseable values,
|
|
32
|
+
* non-https schemes and credential-bearing URLs all yield `undefined` (the caller is refused;
|
|
33
|
+
* nothing is handed to the SDK).
|
|
34
|
+
*/
|
|
35
|
+
export declare function normalizeBaseUrl(raw: string): {
|
|
36
|
+
origin: string;
|
|
37
|
+
baseUrl: string;
|
|
38
|
+
} | undefined;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* base-hosts.ts — the zone-origin discipline for caller-supplied Autotask base URLs.
|
|
3
|
+
*
|
|
4
|
+
* Under `http` a caller names the origin its identity tuple is presented to
|
|
5
|
+
* (`X-Autotask-Base-Url`), which makes this server an outbound HTTP client for whoever can reach
|
|
6
|
+
* the listener. The zone lives in the HOST (e.g. `webservices6.autotask.net`); the base path is
|
|
7
|
+
* fixed (`/atservicesrest`) and appended by the server. `AUTOTASK_ALLOWED_BASE_HOSTS` is what
|
|
8
|
+
* keeps a caller from pointing the server — and the caller's own credentials — at an internal
|
|
9
|
+
* host or a hostile one.
|
|
10
|
+
*/
|
|
11
|
+
/** The fixed Autotask REST base path. Never configurable (the zone lives in the host). */
|
|
12
|
+
export const AUTOTASK_BASE_PATH = '/atservicesrest';
|
|
13
|
+
/**
|
|
14
|
+
* Whether `baseUrl`'s host is permitted by `allowed`.
|
|
15
|
+
*
|
|
16
|
+
* An entry is an exact hostname (`webservices6.autotask.net`) or a `*.` suffix
|
|
17
|
+
* (`*.autotask.net`) that matches subdomains only, never the bare domain (`autotask.net` is NOT
|
|
18
|
+
* matched by `*.autotask.net`). Unset (`undefined`) permits everything at this level; the server
|
|
19
|
+
* factory layers the effective policy on top (default-only when a default origin is set; boot is
|
|
20
|
+
* refused otherwise unless the explicit lab-only opt-in is set). A URL that does not parse is
|
|
21
|
+
* refused rather than passed through, so the SDK's own error is never the gate.
|
|
22
|
+
*/
|
|
23
|
+
export function baseHostAllowed(baseUrl, allowed) {
|
|
24
|
+
if (allowed === undefined)
|
|
25
|
+
return true;
|
|
26
|
+
let host;
|
|
27
|
+
try {
|
|
28
|
+
host = new URL(baseUrl).hostname.toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return allowed.some((entry) => (entry.startsWith('*.') ? host.endsWith(entry.slice(1)) : host === entry));
|
|
34
|
+
}
|
|
35
|
+
/** The origin of `url`, or `undefined` when it does not parse. */
|
|
36
|
+
export function originOf(url) {
|
|
37
|
+
try {
|
|
38
|
+
return new URL(url).origin;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Normalize a caller-supplied (or configured) base URL into the SDK's `baseUrl` contract:
|
|
46
|
+
* origin + the fixed `/atservicesrest` path.
|
|
47
|
+
*
|
|
48
|
+
* The value must be an https URL whose path is empty, `/` or the fixed base path itself (a
|
|
49
|
+
* value naming a DIFFERENT path is refused — the base path is not a knob). Unparseable values,
|
|
50
|
+
* non-https schemes and credential-bearing URLs all yield `undefined` (the caller is refused;
|
|
51
|
+
* nothing is handed to the SDK).
|
|
52
|
+
*/
|
|
53
|
+
export function normalizeBaseUrl(raw) {
|
|
54
|
+
let url;
|
|
55
|
+
try {
|
|
56
|
+
url = new URL(raw);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (url.protocol !== 'https:')
|
|
62
|
+
return undefined;
|
|
63
|
+
if (url.username !== '' || url.password !== '')
|
|
64
|
+
return undefined;
|
|
65
|
+
if (url.pathname !== '' && url.pathname !== '/' && url.pathname !== AUTOTASK_BASE_PATH)
|
|
66
|
+
return undefined;
|
|
67
|
+
const origin = url.origin;
|
|
68
|
+
return { origin, baseUrl: origin + AUTOTASK_BASE_PATH };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=base-hosts.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Config } from './schema.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse and validate configuration from an environment-shaped record.
|
|
4
|
+
*
|
|
5
|
+
* Takes the environment as an argument (main.ts merges the CLI's operational overrides into it)
|
|
6
|
+
* rather than reading `process.env` directly, so the failure paths are testable without mutating
|
|
7
|
+
* global state.
|
|
8
|
+
*
|
|
9
|
+
* @throws {ConfigError} when any variable is missing or malformed. The message lists every
|
|
10
|
+
* problem at once — a boot loop that surfaces one missing variable per restart wastes real minutes.
|
|
11
|
+
*/
|
|
12
|
+
export declare function loadConfig(env?: Record<string, string | undefined>): Config;
|
|
13
|
+
/** The recognised AUTOTASK_* names: the config schema's AUTOTASK_ keys (derived, never hand-listed). */
|
|
14
|
+
export declare function recognisedAutotaskVars(): string[];
|
|
15
|
+
/** The set-but-unrecognised AUTOTASK_* env keys (stale 0.4.x-era vars the server never reads). */
|
|
16
|
+
export declare function unrecognisedAutotaskVars(env: Record<string, string | undefined>): string[];
|
|
17
|
+
/**
|
|
18
|
+
* The server-held credential defaults as a set (or `undefined` when any member is unset — the
|
|
19
|
+
* tuple is all-or-nothing on the config surface; see the module docblock for why).
|
|
20
|
+
*/
|
|
21
|
+
export declare function serverCredentialDefaults(config: Config): {
|
|
22
|
+
apiIntegrationCode: string;
|
|
23
|
+
userName: string;
|
|
24
|
+
secret: string;
|
|
25
|
+
impersonationResourceId?: string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
/** True when a full server-held credential tuple is present (the discouraged `http` default). */
|
|
28
|
+
export declare function hasServerCredentialDefaults(config: Config): boolean;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* config/ — the process's entire configuration surface, validated once at boot.
|
|
3
|
+
*
|
|
4
|
+
* Every knob is an environment variable (the CLI adds only operational parameters:
|
|
5
|
+
* `--transport`, `--host`, `--port`, `--version`, `--help` — never credentials). The server
|
|
6
|
+
* refuses to start on an invalid config rather than failing at the first tool call: a missing
|
|
7
|
+
* credential discovered by a model mid-conversation is a far worse failure than a container that
|
|
8
|
+
* never came up.
|
|
9
|
+
*
|
|
10
|
+
* The credential model (docs/architecture.md 7.2, approved): Autotask's three-header identity
|
|
11
|
+
* (`AUTOTASK_API_INTEGRATION_CODE` / `AUTOTASK_USERNAME` / `AUTOTASK_SECRET`) + the zone origin
|
|
12
|
+
* (`AUTOTASK_BASE_URL`; the base path `/atservicesrest` is fixed, never configurable) + an
|
|
13
|
+
* optional impersonation resource id. Under `stdio` the process environment IS the trust
|
|
14
|
+
* boundary; under `http` the same variables are OPTIONAL, DISCOURAGED defaults — a request
|
|
15
|
+
* without its own `X-Autotask-*` headers runs as this identity, and that identity is only ever
|
|
16
|
+
* presented to the `AUTOTASK_BASE_URL` default origin (never to a caller-named one).
|
|
17
|
+
*/
|
|
18
|
+
// `loadConfig`: the environment in, a validated Config out (issue #91).
|
|
19
|
+
import { credentialShapeProblem } from 'node-autotask';
|
|
20
|
+
import { malformedCredentialBootMessage } from '../credential.js';
|
|
21
|
+
import { CONFIG_FIELDS, ConfigError, schema } from './schema.js';
|
|
22
|
+
/**
|
|
23
|
+
* Parse and validate configuration from an environment-shaped record.
|
|
24
|
+
*
|
|
25
|
+
* Takes the environment as an argument (main.ts merges the CLI's operational overrides into it)
|
|
26
|
+
* rather than reading `process.env` directly, so the failure paths are testable without mutating
|
|
27
|
+
* global state.
|
|
28
|
+
*
|
|
29
|
+
* @throws {ConfigError} when any variable is missing or malformed. The message lists every
|
|
30
|
+
* problem at once — a boot loop that surfaces one missing variable per restart wastes real minutes.
|
|
31
|
+
*/
|
|
32
|
+
export function loadConfig(env = process.env) {
|
|
33
|
+
const parsed = schema.safeParse(env);
|
|
34
|
+
// The pre-dispatch credential shape gate (issue #4a). Runs BEFORE the schema verdict is thrown,
|
|
35
|
+
// so a malformed load is reported alongside (or instead of) the structural problems rather than
|
|
36
|
+
// being masked by them, and it names the SDK's problem + the load recipe in one message. Nothing
|
|
37
|
+
// here dials: the check is pure string inspection (the SDK's `credentialShapeProblem`, whose
|
|
38
|
+
// problem string is value-free).
|
|
39
|
+
const shapeIssues = [];
|
|
40
|
+
for (const name of ['AUTOTASK_API_INTEGRATION_CODE', 'AUTOTASK_USERNAME', 'AUTOTASK_SECRET', 'AUTOTASK_IMPERSONATION_RESOURCE_ID']) {
|
|
41
|
+
const raw = env[name];
|
|
42
|
+
if (raw === undefined || raw.trim() === '')
|
|
43
|
+
continue;
|
|
44
|
+
const problem = credentialShapeProblem(raw);
|
|
45
|
+
if (problem !== undefined)
|
|
46
|
+
shapeIssues.push(` ${name}: ${malformedCredentialBootMessage(name, problem, raw.length)}`);
|
|
47
|
+
}
|
|
48
|
+
if (parsed.success && shapeIssues.length === 0)
|
|
49
|
+
return parsed.data;
|
|
50
|
+
const problems = [
|
|
51
|
+
...(parsed.success ? [] : parsed.error.issues.map((i) => ` ${i.path.join('.') || '(config)'}: ${i.message}`)),
|
|
52
|
+
];
|
|
53
|
+
const all = [...problems, ...shapeIssues];
|
|
54
|
+
// #73: the stale-variable diagnostics (additive, throw path only — a valid config still boots;
|
|
55
|
+
// the section is never served on the success path).
|
|
56
|
+
const unrecognised = unrecognisedAutotaskVars(env);
|
|
57
|
+
const section = [
|
|
58
|
+
'AUTOTASK_* env vars:',
|
|
59
|
+
` recognised and set: ${Object.keys(CONFIG_FIELDS.shape).filter((k) => k.startsWith('AUTOTASK_') && env[k] !== undefined).sort().join(', ') || 'none'} — the vars this server actually reads`,
|
|
60
|
+
...(unrecognised.length > 0
|
|
61
|
+
? [
|
|
62
|
+
` unrecognised (ignored by this server): ${unrecognised.join(', ')}`,
|
|
63
|
+
'Unrecognised AUTOTASK_* vars are ignored by this server; if they belong to a newer (or older) server version, check the version this deployment runs, then upgrade or remove them.',
|
|
64
|
+
'Known legacy renames: AUTOTASK_API_CODE -> set AUTOTASK_API_INTEGRATION_CODE instead (the pinned SDK\'s canonical name).',
|
|
65
|
+
]
|
|
66
|
+
: []),
|
|
67
|
+
];
|
|
68
|
+
const tail = `\n\n${section.join('\n')}`;
|
|
69
|
+
throw new ConfigError(shapeIssues.length > 0
|
|
70
|
+
? `Invalid configuration (credential intake refused a value):\n${all.join('\n')}${tail}`
|
|
71
|
+
: `Invalid configuration:\n${all.join('\n')}${tail}`);
|
|
72
|
+
}
|
|
73
|
+
/** The recognised AUTOTASK_* names: the config schema's AUTOTASK_ keys (derived, never hand-listed). */
|
|
74
|
+
export function recognisedAutotaskVars() {
|
|
75
|
+
return Object.keys(CONFIG_FIELDS.shape).filter((k) => k.startsWith('AUTOTASK_')).sort();
|
|
76
|
+
}
|
|
77
|
+
/** The set-but-unrecognised AUTOTASK_* env keys (stale 0.4.x-era vars the server never reads). */
|
|
78
|
+
export function unrecognisedAutotaskVars(env) {
|
|
79
|
+
const recognised = new Set(recognisedAutotaskVars());
|
|
80
|
+
return Object.keys(env).filter((k) => k.startsWith('AUTOTASK_') && !recognised.has(k)).sort();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The server-held credential defaults as a set (or `undefined` when any member is unset — the
|
|
84
|
+
* tuple is all-or-nothing on the config surface; see the module docblock for why).
|
|
85
|
+
*/
|
|
86
|
+
export function serverCredentialDefaults(config) {
|
|
87
|
+
if (config.AUTOTASK_API_INTEGRATION_CODE === undefined ||
|
|
88
|
+
config.AUTOTASK_USERNAME === undefined ||
|
|
89
|
+
config.AUTOTASK_SECRET === undefined) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
apiIntegrationCode: config.AUTOTASK_API_INTEGRATION_CODE,
|
|
94
|
+
userName: config.AUTOTASK_USERNAME,
|
|
95
|
+
secret: config.AUTOTASK_SECRET,
|
|
96
|
+
...(config.AUTOTASK_IMPERSONATION_RESOURCE_ID !== undefined
|
|
97
|
+
? { impersonationResourceId: config.AUTOTASK_IMPERSONATION_RESOURCE_ID }
|
|
98
|
+
: {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** True when a full server-held credential tuple is present (the discouraged `http` default). */
|
|
102
|
+
export function hasServerCredentialDefaults(config) {
|
|
103
|
+
return serverCredentialDefaults(config) !== undefined;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=load.js.map
|