@coffre/conformance 0.1.5 → 0.1.6
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/dist/main.js +588 -224
- package/package.json +3 -3
- package/src/checks/live.ts +7 -7
- package/src/checks/probe.ts +303 -0
- package/src/main.ts +26 -2
- package/src/report.ts +12 -3
- package/src/suite.ts +16 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coffre/conformance",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Checks a coffre deployment from outside: what it must never do, whatever code it runs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"jose": "6.2.3",
|
|
33
33
|
"pg": "8.16.3",
|
|
34
|
-
"@coffre/db": "0.1.
|
|
34
|
+
"@coffre/db": "0.1.6"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "26.1.1",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"oauth4webapi": "3.8.8",
|
|
40
40
|
"tsdown": "0.23.0",
|
|
41
41
|
"typescript": "5.9.3",
|
|
42
|
-
"@coffre/client": "0.1.
|
|
42
|
+
"@coffre/client": "0.1.6"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsdown",
|
package/src/checks/live.ts
CHANGED
|
@@ -60,15 +60,15 @@ export async function anonymousChecks(report: Report, origin: string, options: {
|
|
|
60
60
|
await report.check('anonymous answers', {}, () => anonymousAnswers(origin));
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
/** The checks with a service token that reads the canary, and audits its project. */
|
|
64
|
-
export async function tokenChecks(report: Report, origin: string, setup: { token?: string; canary?: Canary }): Promise<void> {
|
|
63
|
+
/** The checks with a service token that reads the canary, and audits its project. `prefix` names them apart, run twice. */
|
|
64
|
+
export async function tokenChecks(report: Report, origin: string, setup: { token?: string; canary?: Canary }, prefix = ''): Promise<void> {
|
|
65
65
|
const api = setup.token === undefined ? undefined : bearer(origin, setup.token);
|
|
66
|
-
const token = await report.check(
|
|
66
|
+
const token = await report.check(`${prefix}token`, { api, canary: setup.canary }, ({ api, canary }) => whoIsToken(origin, api, canary));
|
|
67
67
|
const needs = { api, canary: setup.canary, token, secret: setup.token };
|
|
68
|
-
await report.check(
|
|
69
|
-
await report.check(
|
|
70
|
-
await report.check(
|
|
71
|
-
await report.check(
|
|
68
|
+
await report.check(`${prefix}token reveal`, needs, ({ api, canary, token }) => tokenReveal(api, canary, token));
|
|
69
|
+
await report.check(`${prefix}token scan`, needs, ({ canary, secret }) => tokenScan(origin, secret, canary));
|
|
70
|
+
await report.check(`${prefix}token scope`, needs, ({ api, canary }) => tokenScope(api, canary));
|
|
71
|
+
await report.check(`${prefix}token verification`, { api }, ({ api }) => tokenVerification(api));
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
// --- as no one ----------------------------------------------------------------
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// `probe --sign-in`: the token tier, without setting it up by hand. An owner
|
|
2
|
+
// signs the probe in through a device login, as `coffre login` does; the
|
|
3
|
+
// probe makes or finds its own place, issues its service a fresh credential
|
|
4
|
+
// and writes a fresh canary, runs the token tier as that credential, then,
|
|
5
|
+
// as the owner, verifies the whole audit chain. However it ends, the
|
|
6
|
+
// credential is revoked and the session ended. Neither the credential nor
|
|
7
|
+
// the canary is ever printed or written down.
|
|
8
|
+
//
|
|
9
|
+
// What stays: the project, the environment and the service, which the next
|
|
10
|
+
// run finds, the canary's versions, and the run's entries in the audit log.
|
|
11
|
+
import { spawn } from 'node:child_process';
|
|
12
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
13
|
+
|
|
14
|
+
import { CoffreError, type CoffreClient } from '@coffre/client';
|
|
15
|
+
|
|
16
|
+
import { bearer } from '../browser.ts';
|
|
17
|
+
import { expect, Failure, refused, Report } from '../report.ts';
|
|
18
|
+
import { tokenChecks, type Canary } from './live.ts';
|
|
19
|
+
import { canary, type Person } from './people.ts';
|
|
20
|
+
|
|
21
|
+
/** The probe's own place on an instance, made the first time, and found after. Nothing else is touched. */
|
|
22
|
+
export const PROBE = {
|
|
23
|
+
project: 'conformance',
|
|
24
|
+
environment: 'live',
|
|
25
|
+
service: 'token:conformance-probe',
|
|
26
|
+
// Its own key, beside a CANARY an operator may keep for a probe with a token from CI.
|
|
27
|
+
key: 'SIGN_IN_CANARY',
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
/** A device login to approve: the address to open, and the code it must show. */
|
|
31
|
+
export type DeviceLogin = { url: string; code: string };
|
|
32
|
+
|
|
33
|
+
/** How a device login gets approved: by a person in a browser, or, in the local run, by the admin's own session. */
|
|
34
|
+
export type Approve = (login: DeviceLogin) => void | Promise<void>;
|
|
35
|
+
|
|
36
|
+
/** A session from a device login: its credential, which never leaves this process, and who it is. */
|
|
37
|
+
type Session = { token: string; api: CoffreClient; who: string };
|
|
38
|
+
|
|
39
|
+
/** What the run made, to undo at its end: the credential it issued, if it got that far. */
|
|
40
|
+
type Issued = { member: string; id: string };
|
|
41
|
+
|
|
42
|
+
/** A device login, approved by `approve`, polled until it is, as `coffre login` polls. */
|
|
43
|
+
async function deviceSignIn(origin: string, approve: Approve): Promise<string> {
|
|
44
|
+
const json = { 'content-type': 'application/json' };
|
|
45
|
+
const started = await fetch(`${origin}/api/auth/device`, {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: json,
|
|
48
|
+
body: JSON.stringify({ client_label: 'coffre-conformance probe' }),
|
|
49
|
+
});
|
|
50
|
+
expect(started.ok, `${origin} did not start a device login: ${started.status}`);
|
|
51
|
+
const device = (await started.json()) as { device_code: string; user_code: string; verification_uri_complete: string; interval: number; expires_in: number };
|
|
52
|
+
await approve({ url: device.verification_uri_complete, code: device.user_code });
|
|
53
|
+
const deadline = Date.now() + device.expires_in * 1000;
|
|
54
|
+
let wait = Math.max(device.interval, 1) * 1000;
|
|
55
|
+
// The first look comes at once: the local run has approved already.
|
|
56
|
+
for (let first = true; Date.now() < deadline; first = false) {
|
|
57
|
+
if (!first) await new Promise((resolve) => setTimeout(resolve, wait));
|
|
58
|
+
const polled = await fetch(`${origin}/api/auth/device/token`, { method: 'POST', headers: json, body: JSON.stringify({ device_code: device.device_code }) }).catch(() => null);
|
|
59
|
+
if (polled === null) continue;
|
|
60
|
+
const answer = (await polled.json().catch(() => ({}))) as { access_token?: string; error?: string };
|
|
61
|
+
if (polled.ok && answer.access_token !== undefined) return answer.access_token;
|
|
62
|
+
if (polled.status === 429 || answer.error === 'slow_down') wait += 5000;
|
|
63
|
+
else if (answer.error === 'access_denied') throw new Failure('the device login was declined in the browser: nothing was made');
|
|
64
|
+
else if (answer.error === 'expired_token') break;
|
|
65
|
+
else if (answer.error !== 'authorization_pending') throw new Failure(`the device login failed: ${answer.error ?? polled.status}`);
|
|
66
|
+
}
|
|
67
|
+
throw new Failure('the device login expired before it was approved: nothing was made');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** End a session, as `coffre logout` does. One already ended is ended. */
|
|
71
|
+
async function endSession(origin: string, token: string): Promise<void> {
|
|
72
|
+
const response = await fetch(`${origin}/api/auth/logout`, { method: 'POST', headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' } });
|
|
73
|
+
expect(response.ok || response.status === 401, `${origin} did not end the session: ${response.status}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Signed in, as an owner or a root admin, or stopped, the session ended, before anything is made. */
|
|
77
|
+
async function signIn(origin: string, approve: Approve): Promise<{ detail: string; value: Session }> {
|
|
78
|
+
const token = await deviceSignIn(origin, approve);
|
|
79
|
+
const api = bearer(origin, token);
|
|
80
|
+
const me = await api.me();
|
|
81
|
+
const who = me.principal.id;
|
|
82
|
+
if (me.instanceRole === 'user') {
|
|
83
|
+
await endSession(origin, token);
|
|
84
|
+
throw new Failure(`${who} is neither an owner nor a root admin of ${origin}: the probe made nothing, and ended the session`);
|
|
85
|
+
}
|
|
86
|
+
return { detail: `${who}, ${me.instanceRole === 'owner' ? 'an owner' : 'a root admin'}, through a device login`, value: { token, api, who } };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Make what is missing of the probe's place, and the service's two grants; then a fresh canary, and a fresh credential. */
|
|
90
|
+
async function setUp(api: CoffreClient, issued: Issued[]): Promise<{ detail: string; value: { token: string; canary: Canary; made: string[] } }> {
|
|
91
|
+
const place = `${PROBE.project}/${PROBE.environment}`;
|
|
92
|
+
const made: string[] = [];
|
|
93
|
+
const { projects } = await api.projects.list();
|
|
94
|
+
const project = projects.find(({ slug }) => slug === PROBE.project);
|
|
95
|
+
expect(project?.archivedAt == null, `${PROBE.project} is archived: unarchive it, or the probe has nowhere to run`);
|
|
96
|
+
if (project === undefined) {
|
|
97
|
+
await api.projects.create(PROBE.project, { name: 'Conformance' });
|
|
98
|
+
made.push(`the project ${PROBE.project}`);
|
|
99
|
+
}
|
|
100
|
+
const environment = project?.environments.find(({ slug }) => slug === PROBE.environment);
|
|
101
|
+
expect(environment?.details?.archivedAt == null, `${place} is archived: unarchive it, or the probe has nowhere to run`);
|
|
102
|
+
if (environment === undefined) {
|
|
103
|
+
await api.environments.create(place, { name: 'Live' });
|
|
104
|
+
made.push(`the environment ${place}`);
|
|
105
|
+
}
|
|
106
|
+
if ((await api.members.add(PROBE.service)).created) made.push(`the service ${PROBE.service}`);
|
|
107
|
+
const { changes } = await api.access.set(PROBE.service, { [place]: 'viewer', [PROBE.project]: 'auditor' });
|
|
108
|
+
const granted = Object.entries(changes).filter(([, change]) => change !== 'unchanged');
|
|
109
|
+
if (granted.length > 0) made.push(`its grants on ${granted.map(([where]) => where).join(' and ')}`);
|
|
110
|
+
const value = canary();
|
|
111
|
+
await api.secrets.set(place, { [PROBE.key]: value });
|
|
112
|
+
const credential = await api.tokens.issue(PROBE.service, { label: 'coffre-conformance probe, one run', expiresInDays: 1 });
|
|
113
|
+
issued.push({ member: PROBE.service, id: credential.id });
|
|
114
|
+
return {
|
|
115
|
+
detail: `${made.length === 0 ? 'all found' : `made ${made.join(', ')}`}; a fresh ${place}/${PROBE.key} and a fresh credential for ${PROBE.service}`,
|
|
116
|
+
value: { token: credential.token, canary: { project: PROBE.project, environment: PROBE.environment, key: PROBE.key, value }, made },
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** What the whole audit chain says, read by the owner, which a token cannot be. */
|
|
121
|
+
async function verification(api: CoffreClient): Promise<string> {
|
|
122
|
+
const verified = await api.audit.verify();
|
|
123
|
+
expect(verified.ok, 'the audit log does not verify', verified);
|
|
124
|
+
return `the whole chain verifies, as the signed-in owner: ${verified.entries} entries, through entry ${verified.through}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The signed-in tier: sign in, set up, the token tier, verification as the
|
|
129
|
+
* owner; and, however it ends, a Ctrl-C included, the credential revoked and
|
|
130
|
+
* the session ended. `prefix` names its checks apart from the local run's
|
|
131
|
+
* own. Returns the session's credential, ended, for the local run to check.
|
|
132
|
+
*/
|
|
133
|
+
export async function signedInChecks(report: Report, origin: string, approve: Approve, prefix = ''): Promise<ProbeRun | undefined> {
|
|
134
|
+
const session = await report.check(`${prefix}sign-in`, {}, () => signIn(origin, approve));
|
|
135
|
+
if (session === undefined) return undefined;
|
|
136
|
+
const issued: Issued[] = [];
|
|
137
|
+
let ended: Promise<string> | null = null;
|
|
138
|
+
const end = () => (ended ??= cleanUp(origin, session, issued));
|
|
139
|
+
// Ctrl-C, or a SIGTERM: the credential and the session go before the process does.
|
|
140
|
+
const interrupted = (code: number) => () => void end().finally(() => process.exit(code));
|
|
141
|
+
const onInt = interrupted(130);
|
|
142
|
+
const onTerm = interrupted(143);
|
|
143
|
+
process.once('SIGINT', onInt);
|
|
144
|
+
process.once('SIGTERM', onTerm);
|
|
145
|
+
let live: { token: string; made: string[] } | undefined;
|
|
146
|
+
try {
|
|
147
|
+
live = await report.check(`${prefix}probe setup`, {}, () => setUp(session.api, issued));
|
|
148
|
+
await tokenChecks(report, origin, live ?? {}, prefix);
|
|
149
|
+
await report.check(`${prefix}owner verification`, {}, () => verification(session.api));
|
|
150
|
+
} finally {
|
|
151
|
+
process.off('SIGINT', onInt);
|
|
152
|
+
process.off('SIGTERM', onTerm);
|
|
153
|
+
await report.check(`${prefix}clean-up`, {}, end);
|
|
154
|
+
}
|
|
155
|
+
return live === undefined ? undefined : { session: session.token, credential: live.token, made: live.made };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A run's session and credential, both ended by its end, for the local run to try; and what its setup made. */
|
|
159
|
+
export type ProbeRun = { session: string; credential: string; made: string[] };
|
|
160
|
+
|
|
161
|
+
/** Revoke what the run issued, end its session, and say what stays. */
|
|
162
|
+
async function cleanUp(origin: string, session: Session, issued: Issued[]): Promise<string> {
|
|
163
|
+
for (const { member, id } of issued) await session.api.tokens.revoke(member, id);
|
|
164
|
+
const { tokens } = await session.api.tokens.list(PROBE.service).catch(() => ({ tokens: [] as unknown[] }));
|
|
165
|
+
await endSession(origin, session.token);
|
|
166
|
+
const others = tokens.length === 0 ? 'no working credential' : `${tokens.length} credential${tokens.length === 1 ? '' : 's'} this run did not issue, left as they are`;
|
|
167
|
+
return (
|
|
168
|
+
`${issued.length === 0 ? 'nothing issued' : 'the credential revoked'}, the session ended. Staying: ${PROBE.project}/${PROBE.environment} and its ` +
|
|
169
|
+
`${PROBE.key}, ${PROBE.service} with ${others}, and this run's entries in the audit log`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Open `url` in this machine's browser, if it has one: on a server, the printed address serves. */
|
|
174
|
+
export function openBrowser(url: string): void {
|
|
175
|
+
const [command, ...args] =
|
|
176
|
+
process.platform === 'darwin' ? ['open', url] : process.platform === 'win32' ? ['cmd', '/c', 'start', '', url] : ['xdg-open', url];
|
|
177
|
+
try {
|
|
178
|
+
const child = spawn(command!, args, { stdio: 'ignore', detached: true });
|
|
179
|
+
child.on('error', () => {});
|
|
180
|
+
child.unref();
|
|
181
|
+
} catch {
|
|
182
|
+
// No opener: the printed address serves.
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// --- the local run's checks of it ----------------------------------------------
|
|
187
|
+
|
|
188
|
+
/** The local run approves as the person signing in: their own session approves the code, as their browser would. */
|
|
189
|
+
function approvedBy(person: Person): Approve {
|
|
190
|
+
return async ({ code }) => {
|
|
191
|
+
await person.api.deviceLogins.decide(code, true);
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** What the probe must leave as it was: every member and their grants, but its service; every project and environment. */
|
|
196
|
+
async function everythingElse(api: CoffreClient): Promise<string> {
|
|
197
|
+
const { members } = await api.members.list();
|
|
198
|
+
const { projects } = await api.projects.list();
|
|
199
|
+
return JSON.stringify({
|
|
200
|
+
members: members.filter(({ member }) => member !== PROBE.service),
|
|
201
|
+
projects: projects.map(({ slug, archivedAt, environments }) => ({ slug, archivedAt, environments: environments.map(({ slug: env }) => env) })),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Signed in as a user, the probe says so, and makes nothing: no service, no session left. Returns what it must leave as it was. */
|
|
206
|
+
export async function probeAsUser(origin: string, user: Person, admin: Person): Promise<{ detail: string; value: string }> {
|
|
207
|
+
const before = await everythingElse(admin.api);
|
|
208
|
+
const quiet = new Report(19, true);
|
|
209
|
+
await signedInChecks(quiet, origin, approvedBy(user));
|
|
210
|
+
const [signIn] = quiet.results;
|
|
211
|
+
expect(quiet.results.length === 1 && signIn?.status === 'FAIL', 'the probe went on past a user', quiet.results);
|
|
212
|
+
expect(signIn.line.includes('is neither an owner nor a root admin') && signIn.line.includes('made nothing'), 'the probe did not say plainly why it stopped', signIn.line);
|
|
213
|
+
const service = await admin.api.members.get(PROBE.service).then(
|
|
214
|
+
() => 'there',
|
|
215
|
+
(error: unknown) => (error instanceof CoffreError && error.status === 404 ? 'absent' : Promise.reject(error)),
|
|
216
|
+
);
|
|
217
|
+
expect(service === 'absent', `${PROBE.service} was made for a user`);
|
|
218
|
+
const { sessions } = await user.api.sessions.list();
|
|
219
|
+
expect(!sessions.some(({ label }) => label === 'coffre-conformance probe'), "the user's probe session was left open", sessions);
|
|
220
|
+
expect((await everythingElse(admin.api)) === before, 'the probe changed a member, a grant, a project or an environment for a user');
|
|
221
|
+
return { detail: `${user.email}, a user, is told plainly; nothing made, the session ended`, value: before };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** One run of `probe --sign-in`, as the admin: every check of it passes. Its lines, summed up. */
|
|
225
|
+
export async function probeRun(origin: string, admin: Person): Promise<{ detail: string; value: ProbeRun }> {
|
|
226
|
+
const quiet = new Report(19, true);
|
|
227
|
+
const run = await signedInChecks(quiet, origin, approvedBy(admin));
|
|
228
|
+
const failed = quiet.results.filter(({ status }) => status === 'FAIL');
|
|
229
|
+
expect(run !== undefined && failed.length === 0, `the probe failed: ${failed.map(({ name }) => name).join(', ')}`, quiet.results);
|
|
230
|
+
const said = JSON.stringify(quiet.results);
|
|
231
|
+
expect(!said.includes(run.credential) && !said.includes(run.session) && !/coffre-canary-/.test(said), "the probe's lines show its credential, its session or its canary");
|
|
232
|
+
const skipped = quiet.results.filter(({ status }) => status === 'skip').map(({ name }) => name);
|
|
233
|
+
return {
|
|
234
|
+
detail: `${quiet.results.length - skipped.length} checks ok${skipped.length === 0 ? '' : `, ${skipped.join(', ')} skipped`}; ${run.made.length === 0 ? 'all found' : `made ${run.made.join(', ')}`}`,
|
|
235
|
+
value: run,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* After two runs: the second found everything the first made; each run's
|
|
241
|
+
* credential and session refuse, and the service holds no credential; the
|
|
242
|
+
* canary an operator keeps beside is as it was; nothing else changed; the
|
|
243
|
+
* runs' reads are in the audit log.
|
|
244
|
+
*/
|
|
245
|
+
export async function probeLeftovers(origin: string, admin: Person, runs: ProbeRun[], before: string, kept: Canary): Promise<string> {
|
|
246
|
+
expect(runs[0]!.made.includes(`the service ${PROBE.service}`), 'the first run did not make its service', runs[0]!.made);
|
|
247
|
+
expect(runs.slice(1).every(({ made }) => made.length === 0), 'a later run made something again', runs.map(({ made }) => made));
|
|
248
|
+
for (const { session, credential } of runs) {
|
|
249
|
+
for (const [what, token] of [['session', session], ['credential', credential]] as const) {
|
|
250
|
+
await refused(`a run's ${what} still worked after it`, bearer(origin, token).me());
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const { tokens } = await admin.api.tokens.list(PROBE.service);
|
|
254
|
+
expect(tokens.length === 0, `${PROBE.service} still holds ${tokens.length} credential(s)`, tokens);
|
|
255
|
+
const place = `${kept.project}/${kept.environment}`;
|
|
256
|
+
const { values } = await admin.api.secrets.reveal(`${place}/${kept.key}`);
|
|
257
|
+
expect(values[kept.key] === kept.value, `${place}/${kept.key}, kept beside the probe's own, was changed`);
|
|
258
|
+
expect((await everythingElse(admin.api)) === before, 'the runs changed a member, a grant, a project or an environment besides their own');
|
|
259
|
+
const { entries } = await admin.api.audit.list({ actor: PROBE.service, limit: 50 });
|
|
260
|
+
expect(entries.length > 0, `the audit log holds no entry by ${PROBE.service}`);
|
|
261
|
+
return `the second run found all; ${runs.length} sessions and credentials refuse; ${PROBE.service} holds none; ${place}/${kept.key} untouched; nothing else changed; ${entries.length} entries by ${PROBE.service} stay`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** What a credential coffre issues, or a canary, looks like in a run's output. */
|
|
265
|
+
const SHOWN = /coffre_(svc|cli|web)_[A-Za-z0-9_-]{8,}|coffre-canary-[0-9a-f]{8,}/;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The command itself, `coffre-conformance probe <url> --sign-in`, as the
|
|
269
|
+
* admin approves it, interrupted mid-run by a Ctrl-C: it exits 130 with its
|
|
270
|
+
* credential revoked and its session ended, and nothing it printed shows
|
|
271
|
+
* either, or its canary.
|
|
272
|
+
*/
|
|
273
|
+
export async function probeInterrupted(origin: string, admin: Person): Promise<string> {
|
|
274
|
+
// The command this process runs as, from its sources or its build.
|
|
275
|
+
const child = spawn(process.execPath, [...process.execArgv, process.argv[1]!, 'probe', origin, '--sign-in'], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
276
|
+
let output = '';
|
|
277
|
+
child.stdout.setEncoding('utf8').on('data', (chunk: string) => (output += chunk));
|
|
278
|
+
child.stderr.setEncoding('utf8').on('data', (chunk: string) => (output += chunk));
|
|
279
|
+
const exited = new Promise<number | null>((resolve) => child.on('exit', (code) => resolve(code)));
|
|
280
|
+
try {
|
|
281
|
+
const waitFor = async (pattern: RegExp, what: string) => {
|
|
282
|
+
for (let tries = 0; !pattern.test(output); tries += 1) {
|
|
283
|
+
expect(tries < 600 && child.exitCode === null, `the probe never got to ${what}`, output);
|
|
284
|
+
await sleep(50);
|
|
285
|
+
}
|
|
286
|
+
return pattern.exec(output)!;
|
|
287
|
+
};
|
|
288
|
+
const [, code] = await waitFor(/shows the code (\S+)\./, 'its device login');
|
|
289
|
+
await admin.api.deviceLogins.decide(code!, true);
|
|
290
|
+
await waitFor(/ok {4}token reveal/, 'its token checks');
|
|
291
|
+
child.kill('SIGINT');
|
|
292
|
+
const status = await exited;
|
|
293
|
+
expect(status === 130, `the interrupted probe exited ${status}, not 130`, output);
|
|
294
|
+
} finally {
|
|
295
|
+
if (child.exitCode === null) child.kill('SIGKILL');
|
|
296
|
+
}
|
|
297
|
+
const { tokens } = await admin.api.tokens.list(PROBE.service);
|
|
298
|
+
expect(tokens.length === 0, `the interrupted run left ${PROBE.service} ${tokens.length} credential(s)`, tokens);
|
|
299
|
+
const { sessions } = await admin.api.sessions.list();
|
|
300
|
+
expect(!sessions.some(({ label }) => label === 'coffre-conformance probe'), "the interrupted run's session was left open", sessions);
|
|
301
|
+
expect(!SHOWN.test(output), 'the probe printed a credential or a canary', SHOWN.exec(output)?.[0]);
|
|
302
|
+
return 'Ctrl-C mid-run: exit 130, its credential revoked and its session ended; nothing it printed shows either, or its canary';
|
|
303
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { resolve } from 'node:path';
|
|
|
6
6
|
import { parseArgs } from 'node:util';
|
|
7
7
|
|
|
8
8
|
import { parseCanary, type Canary } from './checks/live.ts';
|
|
9
|
+
import { openBrowser, PROBE } from './checks/probe.ts';
|
|
9
10
|
import { boot, type Kind } from './harness.ts';
|
|
10
11
|
import { Failure } from './report.ts';
|
|
11
12
|
import { conform, probe } from './suite.ts';
|
|
@@ -15,6 +16,7 @@ const USAGE = `usage:
|
|
|
15
16
|
--vault-runtime <coffre_vault_runtime URL> [options]
|
|
16
17
|
coffre-conformance node [<dir>] [options]
|
|
17
18
|
coffre-conformance probe <url> [--token <service token> --canary <project>/<env>/<KEY>[=<value>]]
|
|
19
|
+
coffre-conformance probe <url> --sign-in
|
|
18
20
|
|
|
19
21
|
<dir> the deployment, as \`coffre init\` wrote it (default: here)
|
|
20
22
|
--port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
|
|
@@ -24,7 +26,7 @@ const USAGE = `usage:
|
|
|
24
26
|
--vault-runtime <url>
|
|
25
27
|
workers: the same server as coffre_vault_runtime, the login the vault runs as
|
|
26
28
|
|
|
27
|
-
probe checks a running instance from outside
|
|
29
|
+
probe checks a running instance from outside:
|
|
28
30
|
as no one health, headers, every route refusing no one, changes from
|
|
29
31
|
another site refused, /api/auth, nothing like a value shown
|
|
30
32
|
with --token a service token that reads the canary's environment and
|
|
@@ -33,10 +35,18 @@ probe checks a running instance from outside, and writes nothing to it:
|
|
|
33
35
|
--token or COFFRE_TOKEN
|
|
34
36
|
--canary the value after =, or in COFFRE_CONFORMANCE_CANARY, or on
|
|
35
37
|
stdin, so it stays out of the shell's history
|
|
38
|
+
with --sign-in an owner approves a device login in the browser, as for
|
|
39
|
+
\`coffre login\`; the probe finds or makes conformance/live
|
|
40
|
+
and token:conformance-probe, issues it a credential, writes
|
|
41
|
+
a fresh canary, runs the token's checks, then verifies the
|
|
42
|
+
whole audit log as the owner. The credential is revoked and
|
|
43
|
+
the session ended however the run ends; neither it nor the
|
|
44
|
+
canary is ever shown
|
|
36
45
|
|
|
37
46
|
Each run with a token adds a few entries to the instance's audit log, which
|
|
38
47
|
is append-only, so they stay: the canary's read, and the reads it was refused.
|
|
39
|
-
It changes nothing else
|
|
48
|
+
It changes nothing else. A run with --sign-in leaves its project, environment
|
|
49
|
+
and service for the next, and writes its canary and its grants.`;
|
|
40
50
|
|
|
41
51
|
const { values, positionals } = parseArgs({
|
|
42
52
|
allowPositionals: true,
|
|
@@ -48,6 +58,7 @@ const { values, positionals } = parseArgs({
|
|
|
48
58
|
'vault-runtime': { type: 'string' },
|
|
49
59
|
token: { type: 'string' },
|
|
50
60
|
canary: { type: 'string' },
|
|
61
|
+
'sign-in': { type: 'boolean', default: false },
|
|
51
62
|
help: { type: 'boolean', short: 'h' },
|
|
52
63
|
},
|
|
53
64
|
});
|
|
@@ -57,6 +68,19 @@ if (values.help || command === undefined) exit(values.help ? 0 : 2, USAGE);
|
|
|
57
68
|
if (command === 'probe') {
|
|
58
69
|
if (target === undefined) exit(2, USAGE);
|
|
59
70
|
const origin = new URL(target).origin;
|
|
71
|
+
if (values['sign-in']) {
|
|
72
|
+
if (values.token !== undefined || values.canary !== undefined) exit(2, '--sign-in sets up its own token and canary: no --token or --canary with it');
|
|
73
|
+
console.log(`coffre-conformance: probing ${origin}, as no one and signed in`);
|
|
74
|
+
console.log(` (it keeps ${PROBE.project}/${PROBE.environment} and ${PROBE.service} for the next run, and its reads stay in the audit log, for good)`);
|
|
75
|
+
const failed = await probe(origin, {
|
|
76
|
+
signIn: ({ url, code }) => {
|
|
77
|
+
console.log(`\n Approve the probe's sign-in, as an owner, at\n\n ${url}\n\n and check that it shows the code ${code}. Waiting for you…\n`);
|
|
78
|
+
// On a terminal, as `coffre login`: from a script or CI, the printed address serves.
|
|
79
|
+
if (process.stdout.isTTY) openBrowser(url);
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
exit(failed.length === 0 ? 0 : 1, summary(failed));
|
|
83
|
+
}
|
|
60
84
|
const token = values.token ?? process.env.COFFRE_TOKEN;
|
|
61
85
|
let canary: Canary | undefined;
|
|
62
86
|
if (token !== undefined) {
|
package/src/report.ts
CHANGED
|
@@ -49,17 +49,25 @@ export async function until(what: string, probe: () => Promise<boolean>, seconds
|
|
|
49
49
|
type Passed<T> = string | { detail: string; value: T };
|
|
50
50
|
|
|
51
51
|
export class Report {
|
|
52
|
-
readonly #results: { name: string; status: 'ok' | 'FAIL' | 'skip' }[] = [];
|
|
52
|
+
readonly #results: { name: string; status: 'ok' | 'FAIL' | 'skip'; line: string }[] = [];
|
|
53
53
|
readonly #width: number;
|
|
54
|
+
readonly #quiet: boolean;
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
/** `quiet`: keep the lines rather than print them, for a check made of checks to sum up. */
|
|
57
|
+
constructor(width = 19, quiet = false) {
|
|
56
58
|
this.#width = width;
|
|
59
|
+
this.#quiet = quiet;
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
get failed(): string[] {
|
|
60
63
|
return this.#results.filter((result) => result.status === 'FAIL').map((result) => result.name);
|
|
61
64
|
}
|
|
62
65
|
|
|
66
|
+
/** Each check's name, what came of it, and its line. */
|
|
67
|
+
get results(): readonly { name: string; status: 'ok' | 'FAIL' | 'skip'; line: string }[] {
|
|
68
|
+
return this.#results;
|
|
69
|
+
}
|
|
70
|
+
|
|
63
71
|
/**
|
|
64
72
|
* Run one check, and print what came of it. `needs` holds what earlier
|
|
65
73
|
* checks produced: if any is missing, this one is skipped rather than run
|
|
@@ -91,7 +99,8 @@ export class Report {
|
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
#print(name: string, status: 'ok' | 'FAIL' | 'skip', line: string, detail?: unknown): void {
|
|
94
|
-
this.#results.push({ name, status });
|
|
102
|
+
this.#results.push({ name, status, line });
|
|
103
|
+
if (this.#quiet) return;
|
|
95
104
|
const text = ` ${status.padEnd(5)} ${name.padEnd(this.#width)} ${line}`;
|
|
96
105
|
if (status !== 'FAIL') {
|
|
97
106
|
console.log(text);
|
package/src/suite.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { refusedCheckpoint, missingCheckpoint, middleCut } from './checks/readin
|
|
|
12
12
|
import { editedGeneration, forgedCredential, forgedIdentity, forgedApproval } from './checks/signin.ts';
|
|
13
13
|
import { canaryScan } from './checks/canaries.ts';
|
|
14
14
|
import { anonymousChecks, tokenChecks, type Canary } from './checks/live.ts';
|
|
15
|
+
import { type Approve, probeAsUser, probeInterrupted, probeLeftovers, probeRun, signedInChecks } from './checks/probe.ts';
|
|
15
16
|
import { pageLoad, personas, setUp, setUpLive, signInAdmin } from './checks/people.ts';
|
|
16
17
|
import { health } from './checks/surface.ts';
|
|
17
18
|
|
|
@@ -36,6 +37,15 @@ export async function conform(deployment: Deployment, options: { bulkLimit: numb
|
|
|
36
37
|
await report.check('cross-site', all, ({ people, canaries }) => crossSite(people, canaries));
|
|
37
38
|
const live = await report.check('live setup', { admin, canaries }, ({ admin, canaries }) => setUpLive(admin, canaries));
|
|
38
39
|
await tokenChecks(report, deployment.origin, live ?? {});
|
|
40
|
+
// `probe --sign-in`, as an owner runs it against an instance: a user is turned away, two runs, and what stays.
|
|
41
|
+
const origin = deployment.origin;
|
|
42
|
+
const before = await report.check('probe as a user', { admin, people }, ({ admin, people }) => probeAsUser(origin, people.reader, admin));
|
|
43
|
+
const first = await report.check('probe --sign-in', { admin, before }, ({ admin }) => probeRun(origin, admin));
|
|
44
|
+
const second = await report.check('probe again', { admin, first }, ({ admin }) => probeRun(origin, admin));
|
|
45
|
+
await report.check('probe interrupted', { admin, second }, ({ admin }) => probeInterrupted(origin, admin));
|
|
46
|
+
await report.check('probe leftovers', { admin, before, first, second, live }, ({ admin, before, first, second, live }) =>
|
|
47
|
+
probeLeftovers(origin, admin, [first, second], before, live.canary),
|
|
48
|
+
);
|
|
39
49
|
await report.check('offboarding', all, ({ people, canaries }) => offboarding(deployment, people, canaries));
|
|
40
50
|
await report.check('bulk limit', { people }, ({ people }) => bulkLimit(people, options.bulkLimit));
|
|
41
51
|
|
|
@@ -74,12 +84,14 @@ export async function conform(deployment: Deployment, options: { bulkLimit: numb
|
|
|
74
84
|
|
|
75
85
|
/**
|
|
76
86
|
* What can be checked of an instance someone else runs: as no one, and with
|
|
77
|
-
* a token that reads a canary when given one
|
|
78
|
-
*
|
|
87
|
+
* a token that reads a canary when given one, or, with `signIn`, as an owner
|
|
88
|
+
* who signs the probe in and lets it set up its own token. With a token,
|
|
89
|
+
* nothing is written but the audit log's entries for the token's reads.
|
|
79
90
|
*/
|
|
80
|
-
export async function probe(origin: string, live: { token?: string; canary?: Canary }): Promise<string[]> {
|
|
91
|
+
export async function probe(origin: string, live: { token?: string; canary?: Canary; signIn?: Approve }): Promise<string[]> {
|
|
81
92
|
const report = new Report();
|
|
82
93
|
await anonymousChecks(report, origin, { health: true });
|
|
83
|
-
if (live.
|
|
94
|
+
if (live.signIn !== undefined) await signedInChecks(report, origin, live.signIn);
|
|
95
|
+
else if (live.token !== undefined) await tokenChecks(report, origin, live);
|
|
84
96
|
return report.failed;
|
|
85
97
|
}
|