@knowttl/tenable-axi 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +347 -2
- package/axi-credential.schema.json +28 -0
- package/bin/tenable-axi-hook.js +13 -0
- package/bin/tenable-axi.js +8 -0
- package/bin/version.js +3 -0
- package/dist/src/attack-catalogue.d.ts +176 -0
- package/dist/src/attack-catalogue.js +71 -0
- package/dist/src/attack-export-catalogue.d.ts +29 -0
- package/dist/src/attack-export-catalogue.js +58 -0
- package/dist/src/attack-export.d.ts +5 -0
- package/dist/src/attack-export.js +151 -0
- package/dist/src/attack.d.ts +3 -0
- package/dist/src/attack.js +287 -0
- package/dist/src/catalogue.d.ts +150 -0
- package/dist/src/catalogue.js +68 -0
- package/dist/src/cli.d.ts +11 -0
- package/dist/src/cli.js +61 -0
- package/dist/src/export-file.d.ts +8 -0
- package/dist/src/export-file.js +157 -0
- package/dist/src/exposure-contracts.d.ts +31 -0
- package/dist/src/exposure-contracts.js +31 -0
- package/dist/src/exposure.d.ts +4 -0
- package/dist/src/exposure.js +323 -0
- package/dist/src/hook-install.d.ts +1 -0
- package/dist/src/hook-install.js +132 -0
- package/dist/src/inventory/schema.d.ts +34 -0
- package/dist/src/inventory/schema.js +18 -0
- package/dist/src/inventory-catalogue.d.ts +55 -0
- package/dist/src/inventory-catalogue.js +62 -0
- package/dist/src/inventory-export-catalogue.d.ts +96 -0
- package/dist/src/inventory-export-catalogue.js +47 -0
- package/dist/src/inventory-export.d.ts +5 -0
- package/dist/src/inventory-export.js +164 -0
- package/dist/src/inventory.d.ts +3 -0
- package/dist/src/inventory.js +285 -0
- package/dist/src/local-commands.d.ts +2 -0
- package/dist/src/local-commands.js +44 -0
- package/dist/src/platform-tag-catalogue.d.ts +101 -0
- package/dist/src/platform-tag-catalogue.js +180 -0
- package/dist/src/platform-tag.d.ts +4 -0
- package/dist/src/platform-tag.js +322 -0
- package/dist/src/private-file.d.ts +18 -0
- package/dist/src/private-file.js +156 -0
- package/dist/src/profiles.d.ts +116 -0
- package/dist/src/profiles.js +147 -0
- package/dist/src/redact.d.ts +7 -0
- package/dist/src/redact.js +41 -0
- package/dist/src/response-catalogue.d.ts +41 -0
- package/dist/src/response-catalogue.js +134 -0
- package/dist/src/response.d.ts +4 -0
- package/dist/src/response.js +227 -0
- package/dist/src/setup.d.ts +1 -0
- package/dist/src/setup.js +39 -0
- package/dist/src/transport-core.d.ts +85 -0
- package/dist/src/transport-core.js +274 -0
- package/dist/src/transport.d.ts +4 -0
- package/dist/src/transport.js +6 -0
- package/dist/src/writes-core.d.ts +112 -0
- package/dist/src/writes-core.js +215 -0
- package/dist/src/writes.d.ts +57 -0
- package/dist/src/writes.js +5 -0
- package/docs/commands.md +1377 -0
- package/inventory/capabilities.json +3728 -0
- package/package.json +55 -4
- package/skills/tenable-axi/SKILL.md +101 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AxiError } from "axi-sdk-js";
|
|
2
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { catalogue } from "./catalogue.js";
|
|
5
|
+
import { installHooks } from "./hook-install.js";
|
|
6
|
+
export function setupHooks(argv) {
|
|
7
|
+
const usage = () => {
|
|
8
|
+
throw new AxiError("Invalid hook setup command or flags", "VALIDATION_ERROR", [
|
|
9
|
+
"Valid commands: setup hooks; valid flags: --scope user|project, --help",
|
|
10
|
+
]);
|
|
11
|
+
};
|
|
12
|
+
if (argv[1] !== "hooks")
|
|
13
|
+
usage();
|
|
14
|
+
let scope = "user";
|
|
15
|
+
const seen = new Set();
|
|
16
|
+
for (let index = 2; index < argv.length; index++) {
|
|
17
|
+
const flag = argv[index];
|
|
18
|
+
if (!["--help", "--scope"].includes(flag) || seen.has(flag))
|
|
19
|
+
usage();
|
|
20
|
+
seen.add(flag);
|
|
21
|
+
if (flag === "--scope") {
|
|
22
|
+
const value = argv[++index];
|
|
23
|
+
if (value !== "user" && value !== "project")
|
|
24
|
+
usage();
|
|
25
|
+
scope = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (seen.has("--help"))
|
|
29
|
+
return { command: "setup hooks", ...catalogue["setup hooks"] };
|
|
30
|
+
const entry = fileURLToPath(new URL("../../bin/tenable-axi-hook.js", import.meta.url));
|
|
31
|
+
if (!existsSync(entry))
|
|
32
|
+
throw new AxiError("Hook entry point is missing", "HOOK_INSTALL_FAILED", ["Reinstall @knowttl/tenable-axi, then run tenable-axi setup hooks"]);
|
|
33
|
+
const failures = [];
|
|
34
|
+
installHooks(realpathSync(entry), scope, (message) => { failures.push(message); });
|
|
35
|
+
if (failures.length)
|
|
36
|
+
throw new AxiError("Agent hook setup did not complete", "HOOK_INSTALL_FAILED", failures);
|
|
37
|
+
return { hooks: { status: "installed", scope, integrations: "Claude Code, Codex, OpenCode" },
|
|
38
|
+
help: ["Restart your agent session; context uses TENABLE_AXI_PROFILE and TENABLE_AXI_CONFIG from the session environment"] };
|
|
39
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
export declare const TENABLE_ORIGIN = "https://cloud.tenable.com";
|
|
3
|
+
export type ApiKeys = Readonly<{
|
|
4
|
+
accessKey: string;
|
|
5
|
+
secretKey: string;
|
|
6
|
+
}>;
|
|
7
|
+
export type OperationBinding = Readonly<{
|
|
8
|
+
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
9
|
+
path: string;
|
|
10
|
+
effect: "read" | "write";
|
|
11
|
+
query: readonly string[];
|
|
12
|
+
paging?: readonly string[];
|
|
13
|
+
decoder: z.ZodType;
|
|
14
|
+
responseMode?: "file";
|
|
15
|
+
permissionHelp: string;
|
|
16
|
+
validationHelp: string;
|
|
17
|
+
}>;
|
|
18
|
+
export type RequestOptions = {
|
|
19
|
+
pathParams?: Readonly<Record<string, string>>;
|
|
20
|
+
query?: Readonly<Record<string, string | number | boolean | readonly string[]>>;
|
|
21
|
+
body?: unknown;
|
|
22
|
+
continuation?: string;
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
authorization?: unknown;
|
|
25
|
+
retry?: false;
|
|
26
|
+
};
|
|
27
|
+
export type Transport = {
|
|
28
|
+
prepare(operation: string, options?: RequestOptions): PreparedRequest;
|
|
29
|
+
request(operation: string, options?: RequestOptions): Promise<{
|
|
30
|
+
status: number;
|
|
31
|
+
body: unknown;
|
|
32
|
+
}>;
|
|
33
|
+
};
|
|
34
|
+
export type PreparedRequest = Readonly<{
|
|
35
|
+
operation: string;
|
|
36
|
+
method: string;
|
|
37
|
+
url: string;
|
|
38
|
+
body?: string;
|
|
39
|
+
effect: "read" | "write";
|
|
40
|
+
}>;
|
|
41
|
+
export type TransportOptions = {
|
|
42
|
+
keys: ApiKeys;
|
|
43
|
+
origin?: string;
|
|
44
|
+
};
|
|
45
|
+
export declare class TransportError extends Error {
|
|
46
|
+
readonly error: {
|
|
47
|
+
code: string;
|
|
48
|
+
message: string;
|
|
49
|
+
operation: string;
|
|
50
|
+
status?: number;
|
|
51
|
+
retryAfter?: number;
|
|
52
|
+
auditId?: string;
|
|
53
|
+
};
|
|
54
|
+
readonly help: readonly string[];
|
|
55
|
+
constructor(error: {
|
|
56
|
+
code: string;
|
|
57
|
+
message: string;
|
|
58
|
+
operation: string;
|
|
59
|
+
status?: number;
|
|
60
|
+
retryAfter?: number;
|
|
61
|
+
auditId?: string;
|
|
62
|
+
}, help?: readonly string[]);
|
|
63
|
+
toJSON(): {
|
|
64
|
+
help?: readonly string[] | undefined;
|
|
65
|
+
error: {
|
|
66
|
+
code: string;
|
|
67
|
+
message: string;
|
|
68
|
+
operation: string;
|
|
69
|
+
status?: number;
|
|
70
|
+
retryAfter?: number;
|
|
71
|
+
auditId?: string;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export declare function constructTransport(options: TransportOptions, dependencies: {
|
|
76
|
+
bindings: Readonly<Record<string, OperationBinding>>;
|
|
77
|
+
fetch: typeof globalThis.fetch;
|
|
78
|
+
now?: () => number;
|
|
79
|
+
consumeWriteAuthorization?: (authorization: unknown, request: {
|
|
80
|
+
operation: string;
|
|
81
|
+
method: string;
|
|
82
|
+
url: string;
|
|
83
|
+
body?: string;
|
|
84
|
+
}) => boolean;
|
|
85
|
+
}): Transport;
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
export const TENABLE_ORIGIN = "https://cloud.tenable.com";
|
|
2
|
+
export class TransportError extends Error {
|
|
3
|
+
error;
|
|
4
|
+
help;
|
|
5
|
+
constructor(error, help = []) {
|
|
6
|
+
super(error.message);
|
|
7
|
+
this.error = error;
|
|
8
|
+
this.help = help;
|
|
9
|
+
this.name = "TransportError";
|
|
10
|
+
}
|
|
11
|
+
toJSON() { return { error: this.error, ...(this.help.length ? { help: this.help } : {}) }; }
|
|
12
|
+
}
|
|
13
|
+
// Internal construction seam. Production supplies only catalogue bindings and the
|
|
14
|
+
// native fetch; test support supplies synthetic bindings and a loopback adapter.
|
|
15
|
+
export function constructTransport(options, dependencies) {
|
|
16
|
+
const fail = (code, message, operation = "transport", help = [], details = {}) => {
|
|
17
|
+
throw new TransportError({ code, message, operation, ...details }, help);
|
|
18
|
+
};
|
|
19
|
+
if ((options.origin ?? TENABLE_ORIGIN) !== TENABLE_ORIGIN) {
|
|
20
|
+
fail("CONFIG_INVALID", "Origin must be exactly https://cloud.tenable.com");
|
|
21
|
+
}
|
|
22
|
+
const { accessKey, secretKey } = options.keys;
|
|
23
|
+
if ([accessKey, secretKey].some((key) => !key || /[\s\x00-\x1f\x7f;=,]/u.test(key))) {
|
|
24
|
+
fail("CONFIG_INVALID", "API keys must be nonempty and contain no header delimiters or control characters");
|
|
25
|
+
}
|
|
26
|
+
const header = `accessKey=${accessKey}; secretKey=${secretKey}`;
|
|
27
|
+
const secrets = [accessKey, secretKey, header].flatMap((value) => [value, JSON.stringify(value).slice(1, -1), encodeURIComponent(value)]).sort((a, b) => b.length - a.length);
|
|
28
|
+
const redact = (text) => {
|
|
29
|
+
for (const secret of secrets)
|
|
30
|
+
text = text.split(secret).join("[REDACTED]");
|
|
31
|
+
return text.replace(/(?:accessKey|secretKey|api[_-]?key|authorization|password|token)["']?\s*[=:]\s*["']?[^\s,;}"']+/gi, "[REDACTED]").replace(/[\x00-\x1f\x7f]/g, " ").slice(0, 1000);
|
|
32
|
+
};
|
|
33
|
+
// Snapshot caller-owned bindings and keys before the first asynchronous send.
|
|
34
|
+
const bindings = Object.fromEntries(Object.entries(dependencies.bindings).map(([id, binding]) => [id, { ...binding, query: [...binding.query], paging: [...(binding.paging ?? [])] }]));
|
|
35
|
+
const now = dependencies.now ?? Date.now;
|
|
36
|
+
const deadline = now() + 20_000;
|
|
37
|
+
let requests = 0;
|
|
38
|
+
let bytes = 0;
|
|
39
|
+
const seen = new Set();
|
|
40
|
+
function destination(operation, binding, input) {
|
|
41
|
+
const params = input.pathParams ?? {};
|
|
42
|
+
const names = [...binding.path.matchAll(/\{([A-Za-z0-9_]+)\}/g)].map((match) => match[1]);
|
|
43
|
+
if (Object.keys(params).some((name) => !names.includes(name))) {
|
|
44
|
+
fail("VALIDATION_ERROR", "Unexpected path parameter", operation);
|
|
45
|
+
}
|
|
46
|
+
const path = binding.path.replace(/\{([A-Za-z0-9_]+)\}/g, (_, name) => {
|
|
47
|
+
const value = params[name];
|
|
48
|
+
if (!value || /[\s\x00-\x1f\x7f/\\%]/u.test(value) || value === "." || value === "..") {
|
|
49
|
+
fail("VALIDATION_ERROR", "Invalid or missing path parameter", operation);
|
|
50
|
+
}
|
|
51
|
+
return encodeURIComponent(value);
|
|
52
|
+
});
|
|
53
|
+
if (!path.startsWith("/") || path.startsWith("//") || /[?#\\]/.test(path)
|
|
54
|
+
|| path.split("/").some((part) => part === "." || part === "..")) {
|
|
55
|
+
fail("DESTINATION_DENIED", "Operation has an invalid route binding", operation);
|
|
56
|
+
}
|
|
57
|
+
const initial = new URL(path, TENABLE_ORIGIN);
|
|
58
|
+
for (const [key, value] of Object.entries(input.query ?? {})) {
|
|
59
|
+
if (!binding.query.includes(key))
|
|
60
|
+
fail("VALIDATION_ERROR", "Unsupported query parameter", operation);
|
|
61
|
+
for (const item of Array.isArray(value) ? value : [value])
|
|
62
|
+
initial.searchParams.append(key, String(item));
|
|
63
|
+
}
|
|
64
|
+
if (input.continuation === undefined)
|
|
65
|
+
return initial;
|
|
66
|
+
const next = input.continuation;
|
|
67
|
+
let url;
|
|
68
|
+
try {
|
|
69
|
+
url = new URL(next, initial);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return fail("DESTINATION_DENIED", "Invalid continuation destination", operation);
|
|
73
|
+
}
|
|
74
|
+
// Reject normalization tricks before URL parsing can hide traversal or ports.
|
|
75
|
+
if (/[\\\x00-\x20\x7f#]/.test(next) || /%2e|%2f|%5c/i.test(next)
|
|
76
|
+
|| next.split(/[/?]/).some((part) => part === "." || part === "..")
|
|
77
|
+
|| (next.startsWith("https://") && !next.startsWith(`${TENABLE_ORIGIN}/`))
|
|
78
|
+
|| url.origin !== TENABLE_ORIGIN || url.username || url.password || url.hash || url.pathname !== path
|
|
79
|
+
|| [...url.searchParams.keys()].some((key) => !binding.query.includes(key)
|
|
80
|
+
|| url.searchParams.getAll(key).length !== 1)) {
|
|
81
|
+
fail("DESTINATION_DENIED", "Continuation must retain the bound Tenable route", operation);
|
|
82
|
+
}
|
|
83
|
+
for (const key of new Set([...initial.searchParams.keys(), ...url.searchParams.keys()])) {
|
|
84
|
+
if (!binding.paging?.includes(key) && initial.searchParams.get(key) !== url.searchParams.get(key)) {
|
|
85
|
+
fail("DESTINATION_DENIED", "Continuation changed the request scope", operation);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return url;
|
|
89
|
+
}
|
|
90
|
+
function prepare(operation, input = {}) {
|
|
91
|
+
const binding = Object.hasOwn(bindings, operation) ? bindings[operation] : undefined;
|
|
92
|
+
if (!binding)
|
|
93
|
+
fail("OPERATION_BLOCKED", "Operation has no executable catalogue binding");
|
|
94
|
+
const url = destination(operation, binding, input).href;
|
|
95
|
+
const body = input.body === undefined ? undefined : JSON.stringify(input.body);
|
|
96
|
+
return Object.freeze({ operation, method: binding.method, url, body, effect: binding.effect });
|
|
97
|
+
}
|
|
98
|
+
async function request(operation, input = {}) {
|
|
99
|
+
const { url, body } = prepare(operation, input);
|
|
100
|
+
const record = bindings[operation];
|
|
101
|
+
const canonical = new URL(url);
|
|
102
|
+
canonical.searchParams.sort();
|
|
103
|
+
const identity = JSON.stringify([operation, canonical.href, body]);
|
|
104
|
+
if (input.continuation !== undefined && seen.has(identity)) {
|
|
105
|
+
fail("CONTINUATION_REPEATED", "The server repeated a continuation", operation);
|
|
106
|
+
}
|
|
107
|
+
if (record.effect === "write" && !dependencies.consumeWriteAuthorization?.(input.authorization, { operation, method: record.method, url, body })) {
|
|
108
|
+
fail("OPERATION_BLOCKED", "Writes require coordinator authorization", operation);
|
|
109
|
+
}
|
|
110
|
+
seen.add(identity);
|
|
111
|
+
let lastRetryAfter;
|
|
112
|
+
for (let attempt = 0;; attempt++) {
|
|
113
|
+
if (input.signal?.aborted)
|
|
114
|
+
fail("REQUEST_CANCELLED", "Request was cancelled", operation);
|
|
115
|
+
const remaining = deadline - now();
|
|
116
|
+
if (remaining <= 0)
|
|
117
|
+
fail("DEADLINE_EXCEEDED", "Invocation deadline exceeded", operation);
|
|
118
|
+
if (requests >= 20)
|
|
119
|
+
fail("REQUEST_BUDGET_EXCEEDED", "Invocation request budget exceeded", operation);
|
|
120
|
+
requests++;
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
const abort = () => controller.abort();
|
|
123
|
+
input.signal?.addEventListener("abort", abort, { once: true });
|
|
124
|
+
let timedOut = false;
|
|
125
|
+
const timer = setTimeout(() => { timedOut = true; controller.abort(); }, remaining);
|
|
126
|
+
let response;
|
|
127
|
+
let text;
|
|
128
|
+
let file;
|
|
129
|
+
try {
|
|
130
|
+
response = await dependencies.fetch(url, {
|
|
131
|
+
method: record.method, redirect: "error", signal: controller.signal,
|
|
132
|
+
headers: { "X-ApiKeys": header, Accept: record.responseMode === "file" ? "application/octet-stream" : "application/json", ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
|
|
133
|
+
...(body === undefined ? {} : { body }),
|
|
134
|
+
});
|
|
135
|
+
if (response.status >= 300 && response.status < 400) {
|
|
136
|
+
await response.body?.cancel();
|
|
137
|
+
fail("DESTINATION_DENIED", "Tenable redirects are refused", operation, [], { status: response.status });
|
|
138
|
+
}
|
|
139
|
+
const reader = response.body?.getReader();
|
|
140
|
+
const chunks = [];
|
|
141
|
+
const downloading = record.responseMode === "file" && response.status >= 200 && response.status < 300;
|
|
142
|
+
const ceiling = downloading ? 128 * 1024 * 1024 : 20 * 1024 * 1024;
|
|
143
|
+
if (downloading && Number(response.headers.get("content-length")) > ceiling) {
|
|
144
|
+
await reader?.cancel();
|
|
145
|
+
fail("BYTE_BUDGET_EXCEEDED", "Export encoded file exceeds 128 MiB", operation);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
while (reader) {
|
|
149
|
+
const chunk = await reader.read();
|
|
150
|
+
if (chunk.done)
|
|
151
|
+
break;
|
|
152
|
+
bytes += chunk.value.byteLength;
|
|
153
|
+
if (bytes > ceiling) {
|
|
154
|
+
controller.abort();
|
|
155
|
+
fail("BYTE_BUDGET_EXCEEDED", "Invocation decoded response byte budget exceeded", operation);
|
|
156
|
+
}
|
|
157
|
+
chunks.push(chunk.value);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
await reader?.cancel().catch(() => { });
|
|
162
|
+
}
|
|
163
|
+
const contents = Buffer.concat(chunks);
|
|
164
|
+
if (downloading)
|
|
165
|
+
file = contents;
|
|
166
|
+
text = downloading ? "" : contents.toString("utf8");
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
if (error instanceof TransportError)
|
|
170
|
+
throw error;
|
|
171
|
+
const code = record.effect === "write" ? "OUTCOME_UNKNOWN"
|
|
172
|
+
: input.signal?.aborted ? "REQUEST_CANCELLED" : timedOut ? "DEADLINE_EXCEEDED" : "TRANSPORT_FAILED";
|
|
173
|
+
fail(code, record.effect === "write" ? "Mutation outcome is unknown; read back before retrying"
|
|
174
|
+
: "Tenable request could not complete", operation, ["Check connectivity and retry the read when ready"]);
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
input.signal?.removeEventListener("abort", abort);
|
|
179
|
+
}
|
|
180
|
+
if (input.signal?.aborted)
|
|
181
|
+
fail(record.effect === "write" ? "OUTCOME_UNKNOWN" : "REQUEST_CANCELLED", "Request was cancelled", operation);
|
|
182
|
+
if (now() >= deadline)
|
|
183
|
+
fail(record.effect === "write" ? "OUTCOME_UNKNOWN" : "DEADLINE_EXCEEDED", "Invocation deadline exceeded", operation);
|
|
184
|
+
if (response.status >= 200 && response.status < 300) {
|
|
185
|
+
if (record.responseMode === "file") {
|
|
186
|
+
if (response.status === 204)
|
|
187
|
+
fail("EXPORT_NOT_READY", "Export chunk is not ready; check status before downloading", operation);
|
|
188
|
+
return { status: response.status, body: file };
|
|
189
|
+
}
|
|
190
|
+
let decoded;
|
|
191
|
+
try {
|
|
192
|
+
decoded = text ? JSON.parse(text) : undefined;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
fail("RESPONSE_INVALID", "Tenable response is not valid JSON", operation, [], { status: response.status });
|
|
196
|
+
}
|
|
197
|
+
const result = record.decoder.safeParse(decoded);
|
|
198
|
+
if (!result.success)
|
|
199
|
+
fail("RESPONSE_INVALID", "Tenable response does not match the selected view", operation, ["Check the operation response contract; no result was inferred"], { status: response.status });
|
|
200
|
+
return { status: response.status, body: result.data };
|
|
201
|
+
}
|
|
202
|
+
const status = response.status;
|
|
203
|
+
const delay = retryAfter(response.headers.get("retry-after"), now());
|
|
204
|
+
if (delay !== undefined)
|
|
205
|
+
lastRetryAfter = delay;
|
|
206
|
+
const retryable = input.retry !== false && record.effect === "read" && [429, 502, 503, 504].includes(status);
|
|
207
|
+
const wait = delay ?? (attempt === 0 ? 250 : 500);
|
|
208
|
+
if (retryable && attempt < 2 && requests < 20 && wait < deadline - now()) {
|
|
209
|
+
await new Promise((resolve, reject) => {
|
|
210
|
+
const cancel = () => {
|
|
211
|
+
clearTimeout(pause);
|
|
212
|
+
reject(new TransportError({ code: "REQUEST_CANCELLED", message: "Request was cancelled", operation }));
|
|
213
|
+
};
|
|
214
|
+
const pause = setTimeout(() => { input.signal?.removeEventListener("abort", cancel); resolve(); }, wait);
|
|
215
|
+
input.signal?.addEventListener("abort", cancel, { once: true });
|
|
216
|
+
if (input.signal?.aborted)
|
|
217
|
+
cancel();
|
|
218
|
+
});
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const code = status === 401 ? "AUTH_REQUIRED" : status === 403 ? "PERMISSION_DENIED"
|
|
222
|
+
: status === 404 ? "NOT_FOUND" : status === 429 ? "RATE_LIMITED"
|
|
223
|
+
: [400, 409, 422].includes(status) ? "VALIDATION_ERROR" : "REQUEST_FAILED";
|
|
224
|
+
const hint = status === 401 ? "Check both SecretRefs and run tenable-axi auth status"
|
|
225
|
+
: status === 403 ? record.permissionHelp : status === 404 ? "Check the resource identifier and operation scope"
|
|
226
|
+
: status === 429 ? "Retry after the reported delay; narrow reads to reduce requests" : record.validationHelp;
|
|
227
|
+
const message = status === 403 ? "Not licensed or not permitted for this key" : errorMessage(text) ?? `Tenable returned HTTP ${status}`;
|
|
228
|
+
fail(code, redact(message), operation, [hint], { status,
|
|
229
|
+
...(lastRetryAfter === undefined ? {} : { retryAfter: lastRetryAfter }) });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return { prepare, request };
|
|
233
|
+
}
|
|
234
|
+
function retryAfter(value, now) {
|
|
235
|
+
if (value === null)
|
|
236
|
+
return undefined;
|
|
237
|
+
const text = value.trim();
|
|
238
|
+
if (/^\d+$/.test(text)) {
|
|
239
|
+
const seconds = Number(text);
|
|
240
|
+
return Number.isSafeInteger(seconds * 1000) ? seconds * 1000 : undefined;
|
|
241
|
+
}
|
|
242
|
+
// The obsolete asctime HTTP-date has no zone but is defined in GMT.
|
|
243
|
+
const asctimeForm = /^[A-Za-z]{3} [A-Za-z]{3} (?: [1-9]|[12]\d|3[01]) \d{2}:\d{2}:\d{2} \d{4}$/.test(text);
|
|
244
|
+
const date = Date.parse(asctimeForm ? `${text} GMT` : text);
|
|
245
|
+
if (Number.isNaN(date))
|
|
246
|
+
return undefined;
|
|
247
|
+
const parsed = new Date(date);
|
|
248
|
+
const standard = parsed.toUTCString();
|
|
249
|
+
const [weekday, day, month, year, time] = standard.split(" ");
|
|
250
|
+
const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
251
|
+
const obsolete = `${weekdays[parsed.getUTCDay()]}, ${day}-${month}-${year.slice(-2)} ${time} GMT`;
|
|
252
|
+
const asctime = `${weekday.slice(0, 3)} ${month} ${String(Number(day)).padStart(2, " ")} ${time} ${year}`;
|
|
253
|
+
if (![standard, obsolete, asctime].includes(text))
|
|
254
|
+
return undefined;
|
|
255
|
+
return Math.max(0, date - now);
|
|
256
|
+
}
|
|
257
|
+
function errorMessage(text) {
|
|
258
|
+
let body;
|
|
259
|
+
try {
|
|
260
|
+
body = JSON.parse(text);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (!body || typeof body !== "object")
|
|
266
|
+
return undefined;
|
|
267
|
+
const record = body;
|
|
268
|
+
const detail = record.detail && typeof record.detail === "object" ? record.detail : {};
|
|
269
|
+
const nested = [record.error, record.errors].flatMap((value) => Array.isArray(value) ? value : [])
|
|
270
|
+
.flatMap((value) => value && typeof value === "object" && "message" in value ? [value.message] : []);
|
|
271
|
+
const messages = [record.message, detail.display_message, detail.msg, ...nested, record.error, record.detail]
|
|
272
|
+
.filter((value) => typeof value === "string" && value.trim().length > 0 && !/[<>]/.test(value));
|
|
273
|
+
return messages.length ? messages.join("; ") : undefined;
|
|
274
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Transport, type TransportOptions } from "./transport-core.js";
|
|
2
|
+
export { TENABLE_ORIGIN, TransportError } from "./transport-core.js";
|
|
3
|
+
export type { ApiKeys, OperationBinding, RequestOptions, Transport, TransportOptions } from "./transport-core.js";
|
|
4
|
+
export declare function createTransport(options: TransportOptions): Transport;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { transportBindings } from "./catalogue.js";
|
|
2
|
+
import { constructTransport } from "./transport-core.js";
|
|
3
|
+
export { TENABLE_ORIGIN, TransportError } from "./transport-core.js";
|
|
4
|
+
export function createTransport(options) {
|
|
5
|
+
return constructTransport(options, { bindings: transportBindings, fetch: globalThis.fetch });
|
|
6
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type CredentialContext } from "./profiles.js";
|
|
3
|
+
import { privateFiles } from "./private-file.js";
|
|
4
|
+
import { type OperationBinding, type RequestOptions, type Transport } from "./transport-core.js";
|
|
5
|
+
export type MutationTarget = {
|
|
6
|
+
kind: string;
|
|
7
|
+
id: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: string;
|
|
10
|
+
name: string;
|
|
11
|
+
} | {
|
|
12
|
+
kind: string;
|
|
13
|
+
ids: readonly string[];
|
|
14
|
+
} | {
|
|
15
|
+
kind: string;
|
|
16
|
+
payload: true;
|
|
17
|
+
};
|
|
18
|
+
export type MutationOperation = Readonly<{
|
|
19
|
+
input: z.ZodType;
|
|
20
|
+
prepare(input: unknown): {
|
|
21
|
+
target: MutationTarget;
|
|
22
|
+
request: Pick<RequestOptions, "pathParams" | "query" | "body">;
|
|
23
|
+
};
|
|
24
|
+
current?: (transport: Pick<Transport, "request">, input: unknown) => Promise<{
|
|
25
|
+
state: unknown;
|
|
26
|
+
noop?: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
effect: string;
|
|
29
|
+
readBackHelp: readonly string[];
|
|
30
|
+
}>;
|
|
31
|
+
export type MutationPreview = Readonly<{
|
|
32
|
+
profile: string;
|
|
33
|
+
operation: string;
|
|
34
|
+
target: MutationTarget;
|
|
35
|
+
effect: string;
|
|
36
|
+
current: unknown;
|
|
37
|
+
proposed: unknown;
|
|
38
|
+
confirm: string;
|
|
39
|
+
noop: boolean;
|
|
40
|
+
}>;
|
|
41
|
+
export type WriteCoordinatorOptions = {
|
|
42
|
+
config?: string;
|
|
43
|
+
profile?: string;
|
|
44
|
+
readOnly?: boolean;
|
|
45
|
+
approval?: (preview: MutationPreview) => boolean | Promise<boolean>;
|
|
46
|
+
};
|
|
47
|
+
export type MutationOptions = {
|
|
48
|
+
execute?: boolean;
|
|
49
|
+
dryRun?: boolean;
|
|
50
|
+
confirm?: string;
|
|
51
|
+
intentId?: string;
|
|
52
|
+
};
|
|
53
|
+
export declare function constructWriteCoordinator(options: WriteCoordinatorOptions, dependencies: {
|
|
54
|
+
operations: Readonly<Record<string, MutationOperation>>;
|
|
55
|
+
bindings: Readonly<Record<string, OperationBinding>>;
|
|
56
|
+
fetch: typeof globalThis.fetch;
|
|
57
|
+
credentials?: CredentialContext;
|
|
58
|
+
files?: ReturnType<typeof privateFiles>;
|
|
59
|
+
}): {
|
|
60
|
+
scope: Readonly<{
|
|
61
|
+
profile: string;
|
|
62
|
+
origin: "https://cloud.tenable.com";
|
|
63
|
+
allowWrites: boolean;
|
|
64
|
+
operations: readonly string[];
|
|
65
|
+
}>;
|
|
66
|
+
run: (operation: string, input: unknown, flags?: MutationOptions) => Promise<{
|
|
67
|
+
kind: "dry-run";
|
|
68
|
+
preview: Readonly<{
|
|
69
|
+
profile: string;
|
|
70
|
+
operation: string;
|
|
71
|
+
target: MutationTarget;
|
|
72
|
+
effect: string;
|
|
73
|
+
current: unknown;
|
|
74
|
+
proposed: unknown;
|
|
75
|
+
confirm: string;
|
|
76
|
+
noop: boolean;
|
|
77
|
+
}>;
|
|
78
|
+
auditId?: undefined;
|
|
79
|
+
status?: undefined;
|
|
80
|
+
response?: undefined;
|
|
81
|
+
} | {
|
|
82
|
+
kind: "noop";
|
|
83
|
+
preview: Readonly<{
|
|
84
|
+
profile: string;
|
|
85
|
+
operation: string;
|
|
86
|
+
target: MutationTarget;
|
|
87
|
+
effect: string;
|
|
88
|
+
current: unknown;
|
|
89
|
+
proposed: unknown;
|
|
90
|
+
confirm: string;
|
|
91
|
+
noop: boolean;
|
|
92
|
+
}>;
|
|
93
|
+
auditId?: undefined;
|
|
94
|
+
status?: undefined;
|
|
95
|
+
response?: undefined;
|
|
96
|
+
} | {
|
|
97
|
+
kind: "success";
|
|
98
|
+
preview: Readonly<{
|
|
99
|
+
profile: string;
|
|
100
|
+
operation: string;
|
|
101
|
+
target: MutationTarget;
|
|
102
|
+
effect: string;
|
|
103
|
+
current: unknown;
|
|
104
|
+
proposed: unknown;
|
|
105
|
+
confirm: string;
|
|
106
|
+
noop: boolean;
|
|
107
|
+
}>;
|
|
108
|
+
auditId: `${string}-${string}-${string}-${string}-${string}`;
|
|
109
|
+
status: number;
|
|
110
|
+
response: unknown;
|
|
111
|
+
}>;
|
|
112
|
+
};
|