unbound-cli 1.21.2 → 1.21.3

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 CHANGED
@@ -318,7 +318,7 @@ Listing needs Admin, Manager or Analytics Viewer. `mark-fp` needs Admin.
318
318
  | `unbound flags data-security` | PII and secrets a guardrail found (the detected values and prompts are never shown) |
319
319
  | `unbound flags data-security mark-fp <action-id>` | Mark a detection as a false positive |
320
320
  | `unbound trace <trace-id>` | The log, terminal runs and data security detections for one trace ID (prompts and detected values are never shown) |
321
- | `unbound false-positive list\|mark\|unmark <trace-id>` | Any user: show, mark or unmark a false positive on their own findings (alias `fp`; admins can act on any finding in their org) |
321
+ | `unbound false-positive list\|mark\|unmark <trace-id>` | Any user: show, mark or unmark a false positive on their own findings (alias `fp`; needs no login on a device set up through MDM; admins can act on any finding in their org) |
322
322
 
323
323
  ```bash
324
324
  unbound flags terminal --action block,warn --time-window SEVEN_DAYS
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unbound-cli",
3
- "version": "1.21.2",
3
+ "version": "1.21.3",
4
4
  "description": "CLI tool for Unbound - AI Gateway management",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -29,14 +29,29 @@ function chooseFinding(findings, mark, findingId) {
29
29
  return candidates[0];
30
30
  }
31
31
 
32
+ // The login key first, then the device key MDM setup left in config.json, so a user can skip `unbound login`.
33
+ async function call(method, path, opts) {
34
+ const keys = [...new Set([config.getApiKey(), config.getDeviceKey()].filter(Boolean))];
35
+ if (keys.length === 0) throw new Error('Not logged in. Run `unbound login` first.');
36
+ let refused;
37
+ for (const apiKey of keys) {
38
+ try {
39
+ return await api[method](path, { ...opts, apiKey });
40
+ } catch (err) {
41
+ if (err.statusCode !== 401 && err.statusCode !== 403) throw err;
42
+ refused = err;
43
+ }
44
+ }
45
+ throw refused;
46
+ }
47
+
32
48
  async function fetchFindings(trace) {
33
- const data = await api.get(`${PATH}?trace_id=${trace}`);
49
+ const data = await call('get', `${PATH}?trace_id=${trace}`);
34
50
  return data.findings || [];
35
51
  }
36
52
 
37
53
  async function run(fn) {
38
54
  try {
39
- if (!config.isLoggedIn()) throw new Error('Not logged in. Run `unbound login` first.');
40
55
  await fn();
41
56
  } catch (err) {
42
57
  output.error(err.message);
@@ -66,7 +81,7 @@ async function setMark(traceValue, opts, mark) {
66
81
  output.info('Cancelled.');
67
82
  return;
68
83
  }
69
- const data = await api.post(PATH, {
84
+ const data = await call('post', PATH, {
70
85
  body: { trace_id: trace, guardrail_action_id: finding.id, is_false_positive: mark },
71
86
  });
72
87
  if (opts.json) {
@@ -127,4 +142,4 @@ Examples:
127
142
  .action((trace, opts) => run(() => setMark(trace, opts, false)));
128
143
  }
129
144
 
130
- module.exports = { register, chooseFinding };
145
+ module.exports = { register, chooseFinding, call };
@@ -114,3 +114,38 @@ test('an invalid trace ID never reaches the backend', async () => {
114
114
  server.close();
115
115
  }
116
116
  });
117
+
118
+ function freshCall({ cliKey, deviceKey, refuse = [] }) {
119
+ for (const m of ['../src/commands/false-positive', '../src/api', '../src/config']) delete require.cache[require.resolve(m)];
120
+ const api = require('../src/api');
121
+ const config = require('../src/config');
122
+ const used = [];
123
+ config.getApiKey = () => cliKey || null;
124
+ config.getDeviceKey = () => deviceKey || null;
125
+ api.get = async (_path, opts) => {
126
+ used.push(opts.apiKey);
127
+ if (refuse.includes(opts.apiKey)) throw new api.ApiError(401, { error: 'no' });
128
+ return { findings: [] };
129
+ };
130
+ return { call: require('../src/commands/false-positive').call, used };
131
+ }
132
+
133
+ test('a user with only the device key from MDM setup needs no login', async () => {
134
+ const { call, used } = freshCall({ deviceKey: 'device-key' });
135
+ await call('get', PATH);
136
+ assert.deepEqual(used, ['device-key']);
137
+ });
138
+
139
+ test('the login key is tried first, and the device key only if it is refused', async () => {
140
+ const ok = freshCall({ cliKey: 'cli-key', deviceKey: 'device-key' });
141
+ await ok.call('get', PATH);
142
+ assert.deepEqual(ok.used, ['cli-key']);
143
+ const expired = freshCall({ cliKey: 'cli-key', deviceKey: 'device-key', refuse: ['cli-key'] });
144
+ await expired.call('get', PATH);
145
+ assert.deepEqual(expired.used, ['cli-key', 'device-key']);
146
+ });
147
+
148
+ test('with no key at all it says to log in, and a refused key surfaces the refusal', async () => {
149
+ await assert.rejects(freshCall({}).call('get', PATH), /unbound login/);
150
+ await assert.rejects(freshCall({ deviceKey: 'd', refuse: ['d'] }).call('get', PATH), (e) => e.statusCode === 401);
151
+ });