unbound-cli 1.22.0 → 1.22.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/package.json
CHANGED
package/src/commands/doctor.js
CHANGED
|
@@ -10,7 +10,13 @@ const claudePaths = require('../claudePaths');
|
|
|
10
10
|
// Validate the stored API key against the backend. Returns one of:
|
|
11
11
|
// valid | invalid | not-logged-in | unverified (network/other error).
|
|
12
12
|
async function checkApiKey() {
|
|
13
|
-
if (!config.isLoggedIn())
|
|
13
|
+
if (!config.isLoggedIn()) {
|
|
14
|
+
if (!config.getDeviceKey()) return { state: 'not-logged-in', detail: 'no API key stored — run `unbound login`' };
|
|
15
|
+
const result = await api.validateApiKey(config.getDeviceKey());
|
|
16
|
+
if (result === 'valid') return { state: 'valid', detail: 'device key verified against the backend' };
|
|
17
|
+
if (result === 'invalid') return { state: 'invalid', detail: 'the stored device key was rejected (401) — run `unbound login`' };
|
|
18
|
+
return { state: 'unverified', detail: 'the backend did not confirm the key' };
|
|
19
|
+
}
|
|
14
20
|
try {
|
|
15
21
|
const deviceSerial = await getDeviceSerial();
|
|
16
22
|
const privileges = await api.get('/api/v1/users/privileges/', { query: { device_serial: deviceSerial } });
|
|
@@ -168,7 +174,7 @@ Examples:
|
|
|
168
174
|
console.log('');
|
|
169
175
|
}
|
|
170
176
|
if (!tampered.length) {
|
|
171
|
-
if (configMismatch || blankConfigDir) { process.exitCode = 1; return; }
|
|
177
|
+
if (configMismatch || blankConfigDir || key.state === 'invalid') { process.exitCode = 1; return; }
|
|
172
178
|
if (!anyInstalled) console.log(`${C.yellow('No tools are set up yet')} — nothing to fix. Run ${C.bold('unbound setup')} first.`);
|
|
173
179
|
else output.success('Nothing to fix — all set up tools are healthy.');
|
|
174
180
|
console.log('');
|
package/src/commands/status.js
CHANGED
|
@@ -79,6 +79,12 @@ Examples:
|
|
|
79
79
|
pairs.push(['Role', role || '-']);
|
|
80
80
|
if (token) pairs.push(['Token', token]);
|
|
81
81
|
}
|
|
82
|
+
let deviceKey = null;
|
|
83
|
+
if (!loggedIn && config.getDeviceKey()) {
|
|
84
|
+
deviceKey = await api.validateApiKey(config.getDeviceKey(), { baseUrl: config.getBaseUrl() });
|
|
85
|
+
if (deviceKey === 'valid') connectivity = 'Connected';
|
|
86
|
+
pairs.push(['Device key', { valid: 'Valid', invalid: 'Invalid or expired', unknown: 'Could not verify' }[deviceKey]]);
|
|
87
|
+
}
|
|
82
88
|
pairs.push(['Backend URL', config.getBaseUrl()]);
|
|
83
89
|
pairs.push(['Frontend URL', config.getFrontendUrl()]);
|
|
84
90
|
pairs.push(['Gateway URL', config.getGatewayUrl()]);
|
|
@@ -104,6 +110,7 @@ Examples:
|
|
|
104
110
|
output.json({
|
|
105
111
|
config_file: config.CONFIG_FILE,
|
|
106
112
|
logged_in: loggedIn,
|
|
113
|
+
device_key: deviceKey,
|
|
107
114
|
email: loggedIn ? (cfg.email || null) : null,
|
|
108
115
|
organization: loggedIn ? (cfg.org_name || null) : null,
|
|
109
116
|
role: role,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const { test } = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const http = require('http');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { spawn } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const ROOT = path.join(__dirname, '..');
|
|
10
|
+
|
|
11
|
+
function startServer(status) {
|
|
12
|
+
const server = http.createServer((req, res) => {
|
|
13
|
+
res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
14
|
+
res.end(JSON.stringify(status === 200 ? { is_admin: false, is_member: true } : { error: 'Invalid API key' }));
|
|
15
|
+
});
|
|
16
|
+
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve({ server, port: server.address().port })));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Stubs the OS keychain so only the config file's device key is visible.
|
|
20
|
+
function writeKeychainStub(dir) {
|
|
21
|
+
const stub = path.join(dir, 'no-keychain.js');
|
|
22
|
+
fs.writeFileSync(stub, `require(${JSON.stringify(path.join(ROOT, 'src', 'keychain.js'))}).get = () => null;\n`);
|
|
23
|
+
return stub;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function runStatus(port, config) {
|
|
27
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'status-dk-'));
|
|
28
|
+
fs.mkdirSync(path.join(home, '.unbound'));
|
|
29
|
+
const cfg = { ...config, base_url: `http://127.0.0.1:${port}` };
|
|
30
|
+
fs.writeFileSync(path.join(home, '.unbound', 'config.json'), JSON.stringify(cfg));
|
|
31
|
+
const stub = writeKeychainStub(home);
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
const child = spawn('node', ['-r', stub, path.join(ROOT, 'src', 'index.js'), 'status', '--json'], {
|
|
34
|
+
env: { ...process.env, HOME: home, UNBOUND_TOKEN: '', UNBOUND_API_URL: '', UNBOUND_TELEMETRY: '0', UNBOUND_NO_UPDATE_CHECK: '1' },
|
|
35
|
+
});
|
|
36
|
+
let stdout = '';
|
|
37
|
+
child.stdout.on('data', (d) => { stdout += d; });
|
|
38
|
+
child.on('close', (code) => {
|
|
39
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
40
|
+
resolve({ code, json: stdout ? JSON.parse(stdout) : null });
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test('device key only: status reports it valid and connected', async () => {
|
|
46
|
+
const { server, port } = await startServer(200);
|
|
47
|
+
try {
|
|
48
|
+
const r = await runStatus(port, { device_token: 'device-key-1234' });
|
|
49
|
+
assert.equal(r.code, 0);
|
|
50
|
+
assert.equal(r.json.logged_in, false);
|
|
51
|
+
assert.equal(r.json.device_key, 'valid');
|
|
52
|
+
assert.equal(r.json.api_status, 'Connected');
|
|
53
|
+
} finally {
|
|
54
|
+
server.close();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('device key rejected by the backend: status reports it invalid', async () => {
|
|
59
|
+
const { server, port } = await startServer(401);
|
|
60
|
+
try {
|
|
61
|
+
const r = await runStatus(port, { device_token: 'device-key-1234' });
|
|
62
|
+
assert.equal(r.json.device_key, 'invalid');
|
|
63
|
+
assert.notEqual(r.json.api_status, 'Connected');
|
|
64
|
+
} finally {
|
|
65
|
+
server.close();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('no key at all: status shows no device key row', async () => {
|
|
70
|
+
const { server, port } = await startServer(200);
|
|
71
|
+
try {
|
|
72
|
+
const r = await runStatus(port, {});
|
|
73
|
+
assert.equal(r.json.logged_in, false);
|
|
74
|
+
assert.equal(r.json.device_key, null);
|
|
75
|
+
} finally {
|
|
76
|
+
server.close();
|
|
77
|
+
}
|
|
78
|
+
});
|