Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): name the real cause when the infra status check fails

Yudhi Armyndharis1cf3cd62b788
diff --git a/scripts/smoke-test-docker-proxy.sh b/scripts/smoke-test-docker-proxy.sh
index 68d9d84e2..289aca3d6 100755
--- a/scripts/smoke-test-docker-proxy.sh
+++ b/scripts/smoke-test-docker-proxy.sh
@@ -1,14 +1,14 @@
#!/bin/sh
# Smoke test: verify openwa-api can list containers via docker-socket-proxy.
# Run this after `docker compose up -d` with the stack fully started.
-# Usage: ./scripts/smoke-test-docker-proxy.sh <admin-api-key>
+# Usage: ./scripts/smoke-test-docker-proxy.sh <unscoped-admin-api-key>
set -e
API_KEY="${1:-}"
BASE_URL="${BASE_URL:-http://localhost:2785}"
if [ -z "$API_KEY" ]; then
- echo "Usage: $0 <admin-api-key>" >&2
+ echo "Usage: $0 <unscoped-admin-api-key>" >&2
exit 1
fi
@@ -28,7 +28,13 @@ RESPONSE=$(curl -s -w '\n%{http_code}' \
"$BASE_URL/api/infra/status" || true)
CODE=$(printf '%s\n' "$RESPONSE" | tail -n 1)
if [ "$CODE" != "200" ]; then
- echo "FAIL: /api/infra/status returned HTTP $CODE (expected 200; the key must be an ADMIN key)" >&2
+ # Only an auth failure is about the key. The route also refuses a session-scoped ADMIN key and one
+ # used from outside its IP allow-list.
+ HINT=''
+ case "$CODE" in
+ 401 | 403) HINT='; the key must be an unscoped ADMIN key allowed from this IP' ;;
+ esac
+ echo "FAIL: /api/infra/status returned HTTP $CODE (expected 200$HINT)" >&2
exit 1
fi
echo "Response: $(printf '%s\n' "$RESPONSE" | sed '$d')"
diff --git a/scripts/smoke-test-docker-proxy.spec.js b/scripts/smoke-test-docker-proxy.spec.js
new file mode 100644
index 000000000..e06c83795
--- /dev/null
+++ b/scripts/smoke-test-docker-proxy.spec.js
@@ -0,0 +1,50 @@
+/**
+ * Unit tests for the failure hint in scripts/smoke-test-docker-proxy.sh (node:test, no deps).
+ * Run: `npm run test:scripts`.
+ *
+ * A shim curl answers /api/health with 200 and /api/infra/status with SHIM_CODE, so the admin-API
+ * check fails without a running stack and its message can be read.
+ */
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { spawnSync } = require('child_process');
+const fs = require('fs');
+const os = require('os');
+const path = require('path');
+
+const SCRIPT = path.join(__dirname, 'smoke-test-docker-proxy.sh');
+
+function infraFailure(code) {
+ const bin = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-proxy-smoke-'));
+ fs.writeFileSync(
+ path.join(bin, 'curl'),
+ '#!/bin/sh\ncase "$*" in\n */api/health*) printf 200 ;;\n *) printf \'{}\\n%s\' "$SHIM_CODE" ;;\nesac\n',
+ { mode: 0o755 },
+ );
+ const result = spawnSync('sh', [SCRIPT, 'key'], {
+ env: { ...process.env, PATH: `${bin}${path.delimiter}${process.env.PATH}`, SHIM_CODE: code },
+ encoding: 'utf8',
+ });
+ fs.rmSync(bin, { recursive: true, force: true });
+ assert.equal(result.status, 1);
+ return result.stderr.trim();
+}
+
+test('a rejected key is told the whole requirement, not only the role', () => {
+ // The controller refuses a session-scoped ADMIN key, or one used from outside its IP allow-list,
+ // with the same 403 a non-admin key gets.
+ for (const code of ['401', '403']) {
+ assert.equal(
+ infraFailure(code),
+ `FAIL: /api/infra/status returned HTTP ${code} (expected 200; the key must be an unscoped ADMIN key allowed from this IP)`,
+ );
+ }
+});
+
+test('a server error or no answer carries no key hint', () => {
+ for (const code of ['000', '500', '503']) {
+ assert.equal(infraFailure(code), `FAIL: /api/infra/status returned HTTP ${code} (expected 200)`);
+ }
+});