@knowttl/tenable-axi 0.1.5 → 0.1.7
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/README.md +4 -3
- package/dist/src/attack-catalogue.d.ts +3 -3
- package/dist/src/attack-export-catalogue.js +3 -2
- package/dist/src/attack-export.js +16 -25
- package/dist/src/catalogue.d.ts +32 -0
- package/dist/src/catalogue.js +12 -0
- package/dist/src/cli.d.ts +4 -0
- package/dist/src/cli.js +45 -7
- package/dist/src/export-wait.d.ts +40 -0
- package/dist/src/export-wait.js +24 -0
- package/dist/src/exposure.d.ts +4 -0
- package/dist/src/exposure.js +12 -5
- package/dist/src/inventory-catalogue.js +8 -0
- package/dist/src/inventory-export-catalogue.js +4 -3
- package/dist/src/inventory-export.d.ts +1 -0
- package/dist/src/inventory-export.js +25 -9
- package/dist/src/inventory.js +36 -2
- package/dist/src/local-commands.js +5 -4
- package/dist/src/raw.d.ts +4 -0
- package/dist/src/raw.js +85 -0
- package/dist/src/setup.js +3 -3
- package/dist/src/transport-core.d.ts +8 -1
- package/dist/src/transport-core.js +45 -9
- package/dist/src/writes-core.d.ts +15 -1
- package/dist/src/writes-core.js +23 -13
- package/dist/src/writes.d.ts +11 -1
- package/docs/commands.md +85 -9
- package/package.json +7 -7
- package/skills/tenable-axi/SKILL.md +18 -7
package/dist/src/inventory.js
CHANGED
|
@@ -36,13 +36,13 @@ export async function inventoryCommand(argv, store, transportFactory = createTra
|
|
|
36
36
|
const usage = (message) => {
|
|
37
37
|
throw new AxiError(message, "VALIDATION_ERROR", [
|
|
38
38
|
entry ? `Valid flags for ${name}: ${Object.keys(entry.flags).map((flag) => `--${flag}`).join(", ")}`
|
|
39
|
-
: "Valid commands: asset list/show/properties, finding list/show/properties/summary, software list/properties",
|
|
39
|
+
: "Valid commands: asset list/show/properties/investigate, finding list/show/properties/summary, software list/properties",
|
|
40
40
|
]);
|
|
41
41
|
};
|
|
42
42
|
if (!entry)
|
|
43
43
|
usage("Unknown inventory command");
|
|
44
|
-
const kind = argv[0];
|
|
45
44
|
const verb = argv[1];
|
|
45
|
+
const kind = (verb === "investigate" ? "finding" : argv[0]);
|
|
46
46
|
const flags = new Map();
|
|
47
47
|
for (let index = 2; index < argv.length; index++) {
|
|
48
48
|
const flag = argv[index];
|
|
@@ -60,6 +60,8 @@ export async function inventoryCommand(argv, store, transportFactory = createTra
|
|
|
60
60
|
if (get("help"))
|
|
61
61
|
return { command: name, description: entry.description, flags: entry.flags,
|
|
62
62
|
defaultFields: entry.defaultFields, examples: entry.examples };
|
|
63
|
+
if (verb === "investigate" && !get("query"))
|
|
64
|
+
usage("--query is required to select an asset");
|
|
63
65
|
if (get("all") && get("limit"))
|
|
64
66
|
usage("--all and --limit are mutually exclusive");
|
|
65
67
|
if (get("cursor") && get("offset"))
|
|
@@ -179,6 +181,38 @@ export async function inventoryCommand(argv, store, transportFactory = createTra
|
|
|
179
181
|
if (cursor && cursor.scope !== scope)
|
|
180
182
|
usage("Cursor belongs to a different profile, operation, filters, sort, or fields");
|
|
181
183
|
const transport = transportFactory({ origin: profile.origin, keys: store.credentials(profile) });
|
|
184
|
+
if (verb === "investigate") {
|
|
185
|
+
const selectors = ["config", "profile", "full"].flatMap((flag) => get(flag) === undefined ? []
|
|
186
|
+
: flag === "full" ? ["--full"] : [`--${flag}`, get(flag)]);
|
|
187
|
+
const lookup = await inventoryCommand(["asset", "list", ...selectors, "--query", get("query"), "--limit", "2"], store, () => transport);
|
|
188
|
+
const assets = lookup.assets;
|
|
189
|
+
if (assets.length > 1)
|
|
190
|
+
return { lookup, complete: false,
|
|
191
|
+
error: { code: "ASSET_AMBIGUOUS", message: "Multiple assets matched; select an exact asset ID from the candidates" },
|
|
192
|
+
help: [`Run tenable-axi finding list --filter 'asset_id = ["<asset-id>"]' ${selectors.map(quote).join(" ")}`.trim()] };
|
|
193
|
+
if (!lookup.complete)
|
|
194
|
+
return { lookup, complete: false, reason: lookup.reason,
|
|
195
|
+
...(lookup.error ? { error: lookup.error } : {}) };
|
|
196
|
+
if (!assets.length)
|
|
197
|
+
return { lookup, complete: false,
|
|
198
|
+
error: { code: "NOT_FOUND", message: "No asset matched the selected query" } };
|
|
199
|
+
const asset = assets[0];
|
|
200
|
+
if (typeof asset.id !== "string" || !asset.id.length)
|
|
201
|
+
throw new TransportError({ code: "RESPONSE_INVALID",
|
|
202
|
+
message: "Asset lookup is missing the pivot identifier", operation: "inventory-assets-search" });
|
|
203
|
+
const findingFlags = ["fields", "extra-properties", "limit", "all"].flatMap((flag) => get(flag) === undefined ? []
|
|
204
|
+
: flag === "all" ? ["--all"] : [`--${flag}`, get(flag)]);
|
|
205
|
+
try {
|
|
206
|
+
const findings = await inventoryCommand(["finding", "list", ...selectors, ...findingFlags,
|
|
207
|
+
"--filter", `asset_id = ${JSON.stringify([asset.id])}`], store, () => transport);
|
|
208
|
+
return { asset, lookup, ...findings };
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (!(error instanceof TransportError))
|
|
212
|
+
throw error;
|
|
213
|
+
return { asset, lookup, findings: [], count: 0, total: null, complete: false, reason: "READ_FAILED", ...error.toJSON() };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
182
216
|
if (verb === "summary") {
|
|
183
217
|
const counts = { severity: [], sensor_type: [] };
|
|
184
218
|
try {
|
|
@@ -3,9 +3,10 @@ import { catalogue } from "./catalogue.js";
|
|
|
3
3
|
export function localCommand(argv, store) {
|
|
4
4
|
const name = argv.slice(0, 2).join(" ");
|
|
5
5
|
const entry = catalogue[name];
|
|
6
|
-
const usage = () => {
|
|
7
|
-
throw new AxiError(
|
|
8
|
-
|
|
6
|
+
const usage = (message = "Unknown command, argument, duplicate or invalid flag") => {
|
|
7
|
+
throw new AxiError(message, "VALIDATION_ERROR", [
|
|
8
|
+
entry ? `Valid flags for ${name}: ${Object.keys(entry.flags).map((flag) => `--${flag}`).join(", ")}`
|
|
9
|
+
: "Valid commands: auth status, profile list, config path; valid flags: --config <path>, --profile <name>, --fields <names>, --full, --help",
|
|
9
10
|
]);
|
|
10
11
|
};
|
|
11
12
|
if (!entry || name === "home")
|
|
@@ -14,7 +15,7 @@ export function localCommand(argv, store) {
|
|
|
14
15
|
for (let index = 2; index < argv.length; index++) {
|
|
15
16
|
const flag = argv[index];
|
|
16
17
|
if (!flag.startsWith("--") || !Object.hasOwn(entry.flags, flag.slice(2)) || flags.has(flag))
|
|
17
|
-
usage();
|
|
18
|
+
usage(`Unknown argument, flag, or duplicate flag: ${flag}`);
|
|
18
19
|
if (flag === "--help" || flag === "--full")
|
|
19
20
|
flags.set(flag, "true");
|
|
20
21
|
else {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { TransportFactory } from "./exposure.js";
|
|
2
|
+
import { type WriteCoordinatorFactory } from "./inventory-export.js";
|
|
3
|
+
import type { credentialStore } from "./profiles.js";
|
|
4
|
+
export declare function rawCommand(argv: string[], store: ReturnType<typeof credentialStore>, factory?: TransportFactory, writeFactory?: WriteCoordinatorFactory, output?: (chunk: Uint8Array, contentType: string | null, signal: AbortSignal) => void | Promise<void>): Promise<Record<string, unknown>>;
|
package/dist/src/raw.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { AxiError } from "axi-sdk-js";
|
|
2
|
+
import { closeSync, openSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { catalogue } from "./catalogue.js";
|
|
5
|
+
import { bodyFile } from "./inventory-export.js";
|
|
6
|
+
import { createTransport } from "./transport.js";
|
|
7
|
+
import { createWriteCoordinator } from "./writes.js";
|
|
8
|
+
const accessControlRoute = /(?:^|\/)(?:users?|(?:access-)?groups?|roles?|permissions?|access-control|credentials?|share|(?:[a-z0-9._~]+-)*keys?|apikeys?)(?:\/|$)/i;
|
|
9
|
+
const apiPath = z.string()
|
|
10
|
+
.regex(/^\/[A-Za-z0-9._~/-]+$/)
|
|
11
|
+
.refine((path) => !/(?:^|\/)\.{1,2}(?:\/|$)|\/\//.test(path))
|
|
12
|
+
.refine((path) => !accessControlRoute.test(path));
|
|
13
|
+
const writeMethod = z.enum(["POST", "PUT", "PATCH", "DELETE"]);
|
|
14
|
+
export async function rawCommand(argv, store, factory = createTransport, writeFactory = createWriteCoordinator, output = () => { }) {
|
|
15
|
+
const name = argv.slice(0, 2).join(" ");
|
|
16
|
+
const entry = catalogue[name];
|
|
17
|
+
const usage = (message) => {
|
|
18
|
+
throw new AxiError(message, "VALIDATION_ERROR", [entry
|
|
19
|
+
? `Valid flags for ${name}: ${Object.keys(entry.flags).map((flag) => `--${flag}`).join(", ")}`
|
|
20
|
+
: "Valid commands: raw get, raw write"]);
|
|
21
|
+
};
|
|
22
|
+
if (!entry)
|
|
23
|
+
usage("Unknown raw command");
|
|
24
|
+
const flags = new Map();
|
|
25
|
+
for (let index = 2; index < argv.length; index++) {
|
|
26
|
+
const flag = argv[index];
|
|
27
|
+
if (!flag.startsWith("--") || !Object.hasOwn(entry.flags, flag.slice(2)) || flags.has(flag))
|
|
28
|
+
usage(`Unknown argument, flag, or duplicate flag: ${flag}`);
|
|
29
|
+
const value = ["--help", "--execute", "--dry-run"].includes(flag) ? "true" : argv[++index];
|
|
30
|
+
if (!value || value.startsWith("--"))
|
|
31
|
+
usage(`Missing value for ${flag}`);
|
|
32
|
+
flags.set(flag, value);
|
|
33
|
+
}
|
|
34
|
+
const get = (key) => flags.get(`--${key}`);
|
|
35
|
+
if (get("help"))
|
|
36
|
+
return { command: name, ...entry };
|
|
37
|
+
const path = apiPath.safeParse(get("path")).data ?? usage("--path must be an absolute Tenable API path of letters, digits, dots, dashes, underscores, tildes and slashes, without dot segments or access-control routes");
|
|
38
|
+
const params = new URLSearchParams(get("query") ?? "");
|
|
39
|
+
const query = Object.fromEntries([...new Set(params.keys())].map((key) => [key, params.getAll(key)]));
|
|
40
|
+
if (argv[1] === "get") {
|
|
41
|
+
const profile = store.select(store.load(get("config")).config, get("profile"));
|
|
42
|
+
const transport = factory({ keys: store.credentials(profile), origin: profile.origin });
|
|
43
|
+
const out = get("out");
|
|
44
|
+
let fd;
|
|
45
|
+
let completed = false;
|
|
46
|
+
try {
|
|
47
|
+
const response = await transport.request("raw-get", { raw: { method: "GET", path }, query, rawOutput: async (contentType, signal) => {
|
|
48
|
+
if (out)
|
|
49
|
+
fd = openSync(out, "wx", 0o600);
|
|
50
|
+
if (!out)
|
|
51
|
+
await output(new Uint8Array(), contentType, signal);
|
|
52
|
+
return (chunk) => { if (fd !== undefined)
|
|
53
|
+
writeFileSync(fd, chunk);
|
|
54
|
+
else
|
|
55
|
+
return output(chunk, contentType, signal); };
|
|
56
|
+
} });
|
|
57
|
+
completed = true;
|
|
58
|
+
return out ? { status: response.status, contentType: response.contentType, output: out } : {};
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
if (fd !== undefined) {
|
|
62
|
+
closeSync(fd);
|
|
63
|
+
if (!completed)
|
|
64
|
+
unlinkSync(out);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const method = writeMethod.safeParse(get("method")).data ?? usage("--method must be POST, PUT, PATCH or DELETE");
|
|
69
|
+
let body;
|
|
70
|
+
if (get("body-file")) {
|
|
71
|
+
try {
|
|
72
|
+
body = bodyFile(get("body-file"));
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
usage("Cannot read --body-file as JSON within the 1 MiB limit");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const result = await writeFactory({ config: get("config"), profile: get("profile") }).run("raw-write", { query, body }, { execute: get("execute") === "true", dryRun: get("dry-run") === "true", confirm: get("confirm"), intentId: get("intent-id") }, { method, path }, async (contentType, signal) => {
|
|
79
|
+
await output(new Uint8Array(), contentType, signal);
|
|
80
|
+
return (chunk) => output(chunk, contentType, signal);
|
|
81
|
+
});
|
|
82
|
+
if (result.kind !== "success")
|
|
83
|
+
return result;
|
|
84
|
+
return {};
|
|
85
|
+
}
|
package/dist/src/setup.js
CHANGED
|
@@ -4,8 +4,8 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import { catalogue } from "./catalogue.js";
|
|
5
5
|
import { installHooks } from "./hook-install.js";
|
|
6
6
|
export function setupHooks(argv) {
|
|
7
|
-
const usage = () => {
|
|
8
|
-
throw new AxiError(
|
|
7
|
+
const usage = (message = "Invalid hook setup command or flags") => {
|
|
8
|
+
throw new AxiError(message, "VALIDATION_ERROR", [
|
|
9
9
|
"Valid commands: setup hooks; valid flags: --scope user|project, --help",
|
|
10
10
|
]);
|
|
11
11
|
};
|
|
@@ -16,7 +16,7 @@ export function setupHooks(argv) {
|
|
|
16
16
|
for (let index = 2; index < argv.length; index++) {
|
|
17
17
|
const flag = argv[index];
|
|
18
18
|
if (!["--help", "--scope"].includes(flag) || seen.has(flag))
|
|
19
|
-
usage();
|
|
19
|
+
usage(`Unknown argument, flag, or duplicate flag: ${flag}`);
|
|
20
20
|
seen.add(flag);
|
|
21
21
|
if (flag === "--scope") {
|
|
22
22
|
const value = argv[++index];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
export declare const TENABLE_ORIGIN = "https://cloud.tenable.com";
|
|
3
3
|
export type ApiKeys = Readonly<{
|
|
4
4
|
accessKey: string;
|
|
@@ -15,6 +15,10 @@ export type OperationBinding = Readonly<{
|
|
|
15
15
|
permissionHelp: string;
|
|
16
16
|
validationHelp: string;
|
|
17
17
|
}>;
|
|
18
|
+
export type RawRoute = Readonly<{
|
|
19
|
+
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
20
|
+
path: string;
|
|
21
|
+
}>;
|
|
18
22
|
export type RequestOptions = {
|
|
19
23
|
pathParams?: Readonly<Record<string, string>>;
|
|
20
24
|
query?: Readonly<Record<string, string | number | boolean | readonly string[]>>;
|
|
@@ -23,12 +27,15 @@ export type RequestOptions = {
|
|
|
23
27
|
signal?: AbortSignal;
|
|
24
28
|
authorization?: unknown;
|
|
25
29
|
retry?: false;
|
|
30
|
+
raw?: RawRoute;
|
|
31
|
+
rawOutput?: (contentType: string | null, signal: AbortSignal) => ((chunk: Uint8Array) => void | Promise<void>) | undefined | Promise<((chunk: Uint8Array) => void | Promise<void>) | undefined>;
|
|
26
32
|
};
|
|
27
33
|
export type Transport = {
|
|
28
34
|
prepare(operation: string, options?: RequestOptions): PreparedRequest;
|
|
29
35
|
request(operation: string, options?: RequestOptions): Promise<{
|
|
30
36
|
status: number;
|
|
31
37
|
body: unknown;
|
|
38
|
+
contentType?: string | null;
|
|
32
39
|
}>;
|
|
33
40
|
};
|
|
34
41
|
export type PreparedRequest = Readonly<{
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { ResponseDiagnostics } from "./response-diagnostics.js";
|
|
2
3
|
export const TENABLE_ORIGIN = "https://cloud.tenable.com";
|
|
3
4
|
export class TransportError extends Error {
|
|
@@ -88,17 +89,26 @@ export function constructTransport(options, dependencies) {
|
|
|
88
89
|
}
|
|
89
90
|
return url;
|
|
90
91
|
}
|
|
91
|
-
function
|
|
92
|
+
function resolve(operation, input) {
|
|
93
|
+
if (input.raw) {
|
|
94
|
+
return { method: input.raw.method, path: input.raw.path, effect: input.raw.method === "GET" ? "read" : "write",
|
|
95
|
+
query: Object.keys(input.query ?? {}), decoder: z.unknown(), permissionHelp: "Check that the key has the permission for this route",
|
|
96
|
+
validationHelp: "Check the path, query and body against the Tenable API reference" };
|
|
97
|
+
}
|
|
92
98
|
const binding = Object.hasOwn(bindings, operation) ? bindings[operation] : undefined;
|
|
93
99
|
if (!binding)
|
|
94
100
|
fail("OPERATION_BLOCKED", "Operation has no executable catalogue binding");
|
|
101
|
+
return binding;
|
|
102
|
+
}
|
|
103
|
+
function prepare(operation, input = {}) {
|
|
104
|
+
const binding = resolve(operation, input);
|
|
95
105
|
const url = destination(operation, binding, input).href;
|
|
96
106
|
const body = input.body === undefined ? undefined : JSON.stringify(input.body);
|
|
97
107
|
return Object.freeze({ operation, method: binding.method, url, body, effect: binding.effect });
|
|
98
108
|
}
|
|
99
109
|
async function request(operation, input = {}) {
|
|
100
110
|
const { url, body } = prepare(operation, input);
|
|
101
|
-
const record =
|
|
111
|
+
const record = resolve(operation, input);
|
|
102
112
|
const canonical = new URL(url);
|
|
103
113
|
canonical.searchParams.sort();
|
|
104
114
|
const identity = JSON.stringify([operation, canonical.href, body]);
|
|
@@ -127,19 +137,39 @@ export function constructTransport(options, dependencies) {
|
|
|
127
137
|
let response;
|
|
128
138
|
let text;
|
|
129
139
|
let file;
|
|
140
|
+
let contents;
|
|
141
|
+
let streamed = false;
|
|
142
|
+
const waitForOutput = async (value) => {
|
|
143
|
+
let cancel;
|
|
144
|
+
const aborted = new Promise((_, reject) => {
|
|
145
|
+
cancel = () => reject(new Error("Request aborted"));
|
|
146
|
+
controller.signal.addEventListener("abort", cancel, { once: true });
|
|
147
|
+
if (controller.signal.aborted)
|
|
148
|
+
cancel();
|
|
149
|
+
});
|
|
150
|
+
try {
|
|
151
|
+
return await Promise.race([value, aborted]);
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
controller.signal.removeEventListener("abort", cancel);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
130
157
|
try {
|
|
131
158
|
response = await dependencies.fetch(url, {
|
|
132
159
|
method: record.method, redirect: "error", signal: controller.signal,
|
|
133
|
-
headers: { "X-ApiKeys": header, Accept: record.responseMode === "file" ? "application/octet-stream" : "application/json", ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
|
|
160
|
+
headers: { "X-ApiKeys": header, Accept: input.raw ? "*/*" : record.responseMode === "file" ? "application/octet-stream" : "application/json", ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
|
|
134
161
|
...(body === undefined ? {} : { body }),
|
|
135
162
|
});
|
|
136
163
|
if (response.status >= 300 && response.status < 400) {
|
|
137
164
|
await response.body?.cancel();
|
|
138
165
|
fail("DESTINATION_DENIED", "Tenable redirects are refused", operation, [], { status: response.status });
|
|
139
166
|
}
|
|
140
|
-
const reader = response.body?.getReader();
|
|
141
167
|
const chunks = [];
|
|
142
|
-
const
|
|
168
|
+
const output = input.raw && response.status >= 200 && response.status < 300
|
|
169
|
+
? await waitForOutput(input.rawOutput?.(response.headers.get("content-type"), controller.signal)) : undefined;
|
|
170
|
+
streamed = output !== undefined;
|
|
171
|
+
const reader = response.body?.getReader();
|
|
172
|
+
const downloading = (record.responseMode === "file" || !!input.raw) && response.status >= 200 && response.status < 300;
|
|
143
173
|
const ceiling = downloading ? 128 * 1024 * 1024 : 20 * 1024 * 1024;
|
|
144
174
|
if (downloading && Number(response.headers.get("content-length")) > ceiling) {
|
|
145
175
|
await reader?.cancel();
|
|
@@ -155,16 +185,19 @@ export function constructTransport(options, dependencies) {
|
|
|
155
185
|
controller.abort();
|
|
156
186
|
fail("BYTE_BUDGET_EXCEEDED", "Invocation decoded response byte budget exceeded", operation);
|
|
157
187
|
}
|
|
158
|
-
|
|
188
|
+
if (output)
|
|
189
|
+
await waitForOutput(output(chunk.value));
|
|
190
|
+
else
|
|
191
|
+
chunks.push(chunk.value);
|
|
159
192
|
}
|
|
160
193
|
}
|
|
161
194
|
finally {
|
|
162
195
|
await reader?.cancel().catch(() => { });
|
|
163
196
|
}
|
|
164
|
-
|
|
165
|
-
if (downloading)
|
|
197
|
+
contents = Buffer.concat(chunks);
|
|
198
|
+
if (record.responseMode === "file" && downloading)
|
|
166
199
|
file = contents;
|
|
167
|
-
text = downloading ? "" : contents.toString("utf8");
|
|
200
|
+
text = record.responseMode === "file" && downloading ? "" : contents.toString("utf8");
|
|
168
201
|
}
|
|
169
202
|
catch (error) {
|
|
170
203
|
if (error instanceof TransportError)
|
|
@@ -183,6 +216,9 @@ export function constructTransport(options, dependencies) {
|
|
|
183
216
|
if (now() >= deadline)
|
|
184
217
|
fail(record.effect === "write" ? "OUTCOME_UNKNOWN" : "DEADLINE_EXCEEDED", "Invocation deadline exceeded", operation);
|
|
185
218
|
if (response.status >= 200 && response.status < 300) {
|
|
219
|
+
const contentType = response.headers.get("content-type");
|
|
220
|
+
if (input.raw)
|
|
221
|
+
return { status: response.status, body: streamed ? undefined : contents, contentType };
|
|
186
222
|
if (record.responseMode === "file") {
|
|
187
223
|
if (response.status === 204)
|
|
188
224
|
fail("EXPORT_NOT_READY", "Export chunk is not ready; check status before downloading", operation);
|
|
@@ -50,6 +50,10 @@ export type MutationOptions = {
|
|
|
50
50
|
confirm?: string;
|
|
51
51
|
intentId?: string;
|
|
52
52
|
};
|
|
53
|
+
export type RawWriteRoute = Readonly<{
|
|
54
|
+
method: "POST" | "PUT" | "PATCH" | "DELETE";
|
|
55
|
+
path: string;
|
|
56
|
+
}>;
|
|
53
57
|
export declare function constructWriteCoordinator(options: WriteCoordinatorOptions, dependencies: {
|
|
54
58
|
operations: Readonly<Record<string, MutationOperation>>;
|
|
55
59
|
bindings: Readonly<Record<string, OperationBinding>>;
|
|
@@ -63,7 +67,7 @@ export declare function constructWriteCoordinator(options: WriteCoordinatorOptio
|
|
|
63
67
|
allowWrites: boolean;
|
|
64
68
|
operations: readonly string[];
|
|
65
69
|
}>;
|
|
66
|
-
run: (operation: string, input: unknown, flags?: MutationOptions) => Promise<{
|
|
70
|
+
run: (operation: string, input: unknown, flags?: MutationOptions, raw?: RawWriteRoute, rawOutput?: RequestOptions["rawOutput"]) => Promise<{
|
|
67
71
|
kind: "dry-run";
|
|
68
72
|
preview: Readonly<{
|
|
69
73
|
profile: string;
|
|
@@ -78,6 +82,7 @@ export declare function constructWriteCoordinator(options: WriteCoordinatorOptio
|
|
|
78
82
|
auditId?: undefined;
|
|
79
83
|
status?: undefined;
|
|
80
84
|
response?: undefined;
|
|
85
|
+
contentType?: undefined;
|
|
81
86
|
} | {
|
|
82
87
|
kind: "noop";
|
|
83
88
|
preview: Readonly<{
|
|
@@ -93,6 +98,7 @@ export declare function constructWriteCoordinator(options: WriteCoordinatorOptio
|
|
|
93
98
|
auditId?: undefined;
|
|
94
99
|
status?: undefined;
|
|
95
100
|
response?: undefined;
|
|
101
|
+
contentType?: undefined;
|
|
96
102
|
} | {
|
|
97
103
|
kind: "success";
|
|
98
104
|
preview: Readonly<{
|
|
@@ -108,5 +114,13 @@ export declare function constructWriteCoordinator(options: WriteCoordinatorOptio
|
|
|
108
114
|
auditId: `${string}-${string}-${string}-${string}-${string}`;
|
|
109
115
|
status: number;
|
|
110
116
|
response: unknown;
|
|
117
|
+
contentType: string | null | undefined;
|
|
111
118
|
}>;
|
|
119
|
+
reader: {
|
|
120
|
+
request: (name: string, options?: RequestOptions) => Promise<{
|
|
121
|
+
status: number;
|
|
122
|
+
body: unknown;
|
|
123
|
+
contentType?: string | null;
|
|
124
|
+
}>;
|
|
125
|
+
};
|
|
112
126
|
};
|
package/dist/src/writes-core.js
CHANGED
|
@@ -5,6 +5,14 @@ import { z } from "zod";
|
|
|
5
5
|
import { credentialStore } from "./profiles.js";
|
|
6
6
|
import { privateFiles } from "./private-file.js";
|
|
7
7
|
import { constructTransport, TransportError } from "./transport-core.js";
|
|
8
|
+
function rawWriteDefinition(route) {
|
|
9
|
+
return {
|
|
10
|
+
input: z.strictObject({ query: z.record(z.string(), z.union([z.string(), z.array(z.string())])).optional(), body: z.unknown().optional() }),
|
|
11
|
+
prepare: (input) => ({ target: { kind: `${route.method} ${route.path}`, payload: true }, request: input }),
|
|
12
|
+
effect: `Send ${route.method} ${route.path}`,
|
|
13
|
+
readBackHelp: ["Read the target through its owning Tenable API before any new intent; never replay this intent"],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
8
16
|
function canonical(value) {
|
|
9
17
|
const normalize = (value) => {
|
|
10
18
|
if (Array.isArray(value))
|
|
@@ -64,6 +72,12 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
64
72
|
},
|
|
65
73
|
});
|
|
66
74
|
}
|
|
75
|
+
const reader = { request: async (name, options) => {
|
|
76
|
+
const connection = sender();
|
|
77
|
+
if (connection.prepare(name, options).effect !== "read")
|
|
78
|
+
fail("OPERATION_BLOCKED", "State readers cannot send writes", name);
|
|
79
|
+
return connection.request(name, options);
|
|
80
|
+
} };
|
|
67
81
|
function fail(code, message, operation, help = [], auditId) {
|
|
68
82
|
throw new TransportError({ code, message, operation, ...(auditId ? { auditId } : {}) }, help);
|
|
69
83
|
}
|
|
@@ -123,10 +137,10 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
123
137
|
}
|
|
124
138
|
}
|
|
125
139
|
}
|
|
126
|
-
async function run(operation, input, flags = {}) {
|
|
140
|
+
async function run(operation, input, flags = {}, raw, rawOutput) {
|
|
127
141
|
flags = { ...flags };
|
|
128
|
-
const definition = Object.hasOwn(definitions, operation) ? definitions[operation] : undefined;
|
|
129
|
-
if (!definition || bindings[operation]?.effect !== "write")
|
|
142
|
+
const definition = raw ? rawWriteDefinition(raw) : Object.hasOwn(definitions, operation) ? definitions[operation] : undefined;
|
|
143
|
+
if (!definition || (!raw && bindings[operation]?.effect !== "write"))
|
|
130
144
|
fail("OPERATION_BLOCKED", "Operation has no reviewed write binding", operation);
|
|
131
145
|
const parsed = definition.input.safeParse(input);
|
|
132
146
|
if (!parsed.success)
|
|
@@ -134,7 +148,7 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
134
148
|
const prepared = definition.prepare(JSON.parse(canonical(parsed.data)));
|
|
135
149
|
const request = JSON.parse(canonical(prepared.request));
|
|
136
150
|
const target = JSON.parse(canonical(prepared.target));
|
|
137
|
-
const securityField = (value) => !!value && typeof value === "object" && Object.entries(value).some(([key, child]) => /^(?:access_control|permissions
|
|
151
|
+
const securityField = (value) => !!value && typeof value === "object" && Object.entries(value).some(([key, child]) => /^(?:acl|access_control|permissions?|roles|credentials|api_keys)$/i.test(key) || securityField(child));
|
|
138
152
|
if (securityField(request.body))
|
|
139
153
|
fail("VALIDATION_ERROR", "Security administration payloads are blocked", operation);
|
|
140
154
|
const uuid = z.string().uuid();
|
|
@@ -151,15 +165,10 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
151
165
|
if (!scope.operations.includes(operation))
|
|
152
166
|
fail("OPERATION_NOT_WRITABLE", "Operation is outside the configured write scope", operation);
|
|
153
167
|
const connection = sender();
|
|
154
|
-
const plan = connection.prepare(operation, request);
|
|
168
|
+
const plan = connection.prepare(operation, { ...request, raw });
|
|
155
169
|
const { effect: _effect, ...identity } = plan;
|
|
156
170
|
const confirmationTarget = "payload" in target ? { kind: target.kind, digest: digest(canonical(request)) } : target;
|
|
157
171
|
const confirm = canonical([scope.profile, operation, confirmationTarget]);
|
|
158
|
-
const reader = { request: async (name, options) => {
|
|
159
|
-
if (connection.prepare(name, options).effect !== "read")
|
|
160
|
-
fail("OPERATION_BLOCKED", "State readers cannot send writes", operation);
|
|
161
|
-
return connection.request(name, options);
|
|
162
|
-
} };
|
|
163
172
|
const current = await definition.current?.(reader, parsed.data);
|
|
164
173
|
const state = canonical(current?.state ?? null);
|
|
165
174
|
const preview = store.redactor.payload({ profile: scope.profile, operation, target, effect: definition.effect,
|
|
@@ -196,7 +205,7 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
196
205
|
authorizations.set(authorization, canonical(identity));
|
|
197
206
|
let response;
|
|
198
207
|
try {
|
|
199
|
-
response = await connection.request(operation, { ...request, authorization });
|
|
208
|
+
response = await connection.request(operation, { ...request, raw, rawOutput, authorization });
|
|
200
209
|
}
|
|
201
210
|
catch (error) {
|
|
202
211
|
authorizations.delete(authorization);
|
|
@@ -209,7 +218,8 @@ export function constructWriteCoordinator(options, dependencies) {
|
|
|
209
218
|
throw new TransportError({ ...upstream.error, auditId }, upstream.help);
|
|
210
219
|
}
|
|
211
220
|
append("outcome", "SUCCESS", response.status);
|
|
212
|
-
return { kind: "success", preview, auditId, status: response.status,
|
|
221
|
+
return { kind: "success", preview, auditId, status: response.status,
|
|
222
|
+
response: Buffer.isBuffer(response.body) && raw ? response.body : store.redactor.payload(response.body), contentType: response.contentType };
|
|
213
223
|
}
|
|
214
|
-
return { scope, run };
|
|
224
|
+
return { scope, run, reader };
|
|
215
225
|
}
|
package/dist/src/writes.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare function createWriteCoordinator(options?: WriteCoordinatorOptions
|
|
|
8
8
|
allowWrites: boolean;
|
|
9
9
|
operations: readonly string[];
|
|
10
10
|
}>;
|
|
11
|
-
run: (operation: string, input: unknown, flags?: import("./writes-core.js").MutationOptions) => Promise<{
|
|
11
|
+
run: (operation: string, input: unknown, flags?: import("./writes-core.js").MutationOptions, raw?: import("./writes-core.js").RawWriteRoute, rawOutput?: import("./transport-core.js").RequestOptions["rawOutput"]) => Promise<{
|
|
12
12
|
kind: "dry-run";
|
|
13
13
|
preview: Readonly<{
|
|
14
14
|
profile: string;
|
|
@@ -23,6 +23,7 @@ export declare function createWriteCoordinator(options?: WriteCoordinatorOptions
|
|
|
23
23
|
auditId?: undefined;
|
|
24
24
|
status?: undefined;
|
|
25
25
|
response?: undefined;
|
|
26
|
+
contentType?: undefined;
|
|
26
27
|
} | {
|
|
27
28
|
kind: "noop";
|
|
28
29
|
preview: Readonly<{
|
|
@@ -38,6 +39,7 @@ export declare function createWriteCoordinator(options?: WriteCoordinatorOptions
|
|
|
38
39
|
auditId?: undefined;
|
|
39
40
|
status?: undefined;
|
|
40
41
|
response?: undefined;
|
|
42
|
+
contentType?: undefined;
|
|
41
43
|
} | {
|
|
42
44
|
kind: "success";
|
|
43
45
|
preview: Readonly<{
|
|
@@ -53,5 +55,13 @@ export declare function createWriteCoordinator(options?: WriteCoordinatorOptions
|
|
|
53
55
|
auditId: `${string}-${string}-${string}-${string}-${string}`;
|
|
54
56
|
status: number;
|
|
55
57
|
response: unknown;
|
|
58
|
+
contentType: string | null | undefined;
|
|
56
59
|
}>;
|
|
60
|
+
reader: {
|
|
61
|
+
request: (name: string, options?: import("./transport-core.js").RequestOptions) => Promise<{
|
|
62
|
+
status: number;
|
|
63
|
+
body: unknown;
|
|
64
|
+
contentType?: string | null;
|
|
65
|
+
}>;
|
|
66
|
+
};
|
|
57
67
|
};
|