@coffre/conformance 0.1.6 → 0.1.8

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.
@@ -1,321 +0,0 @@
1
- // What can be checked of an instance that is already running, from outside
2
- // and without writing anything: as no one, and with a service token the
3
- // operator set up for it, which reads one canary. The local run takes the
4
- // same checks against the deployment it booted, so they run in CI too.
5
- //
6
- // Each run with a token adds a few entries to the instance's audit log,
7
- // which is append-only, so they stay: the canary's read, and the reads it
8
- // was refused. It changes nothing else.
9
- import { CoffreError, type AuthInfo, type CoffreClient } from '@coffre/client';
10
-
11
- import { bearer } from '../browser.ts';
12
- import { expect, refused, Skip, type Report } from '../report.ts';
13
- import { everyRoute, getCalls, getUrls } from '../routes.ts';
14
- import { headers, reachable } from './surface.ts';
15
-
16
- /** The page anyone may open. Every other page sends no one to /login. */
17
- const OPEN_PAGES = ['/login'];
18
- /** The pages behind sign-in, whatever the instance holds. */
19
- const CLOSED_PAGES = [
20
- '/',
21
- '/account',
22
- '/access',
23
- '/audit',
24
- '/auth/device',
25
- '/settings',
26
- '/projects',
27
- '/users',
28
- '/tokens',
29
- '/unregistered',
30
- ];
31
-
32
- /** A made-up place and member, which no instance has. */
33
- const NOWHERE = { project: 'conformance-nowhere', environment: 'none' };
34
- const NOBODY = 'token:conformance-nobody';
35
-
36
- /** A secret the operator set up, and its value, for the token to read and to look for. */
37
- export type Canary = { project: string; environment: string; key: string; value: string };
38
-
39
- /** Parse `<project>/<environment>/<KEY>=<value>`, or the path alone with the value given apart. */
40
- export function parseCanary(text: string, value?: string): Canary {
41
- const at = text.indexOf('=');
42
- const path = at === -1 ? text : text.slice(0, at);
43
- const given = at === -1 ? value : text.slice(at + 1);
44
- const parts = path.split('/');
45
- if (parts.length !== 3 || parts.some((part) => part === '')) {
46
- throw new Error(`the canary is <project>/<environment>/<KEY>, not "${path}"`);
47
- }
48
- if (given === undefined || given === '') throw new Error('the canary has no value: pass it after =, on stdin, or in COFFRE_CONFORMANCE_CANARY');
49
- const [project, environment, key] = parts as [string, string, string];
50
- return { project, environment, key, value: given };
51
- }
52
-
53
- /** The checks anyone can run: no sign-in, nothing written. `health` false when the caller checked it already. */
54
- export async function anonymousChecks(report: Report, origin: string, options: { health: boolean }): Promise<void> {
55
- if (options.health) await report.check('health', {}, () => reachable(origin));
56
- await report.check('headers', {}, () => headers(origin));
57
- await report.check('anonymous api', {}, () => anonymousApi(origin));
58
- await report.check('forged cross-site', {}, () => forgedCrossSite(origin));
59
- await report.check('sign-in info', {}, () => signinInfo(origin));
60
- await report.check('anonymous answers', {}, () => anonymousAnswers(origin));
61
- }
62
-
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
- const api = setup.token === undefined ? undefined : bearer(origin, setup.token);
66
- const token = await report.check(`${prefix}token`, { api, canary: setup.canary }, ({ api, canary }) => whoIsToken(origin, api, canary));
67
- const needs = { api, canary: setup.canary, token, secret: setup.token };
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
- }
73
-
74
- // --- as no one ----------------------------------------------------------------
75
-
76
- /** Every route, whatever its method, turns away a caller with no credential, and says nothing else. */
77
- async function anonymousApi(origin: string): Promise<string> {
78
- const routes = everyRoute(origin);
79
- for (const { key, method, url } of routes) {
80
- const response = await fetch(url, {
81
- method,
82
- headers: { 'content-type': 'application/json' },
83
- body: method === 'GET' ? undefined : '{}',
84
- });
85
- const text = await response.text();
86
- expect(response.status === 401, `${key} answered ${response.status} to no one`, text);
87
- expect(onlyRefusal(text), `${key} refused no one with more than a refusal`, text);
88
- }
89
- return `${routes.length} routes, every method: 401, and nothing but the refusal`;
90
- }
91
-
92
- /**
93
- * A change sent with a session cookie from another site's page is refused
94
- * before the cookie is even looked at, so a made-up one shows it: a real
95
- * cookie would get the same answer. Behind Cloudflare Access the same holds
96
- * for its cookie and assertion.
97
- */
98
- async function forgedCrossSite(origin: string): Promise<string> {
99
- const changes = everyRoute(origin).filter((route) => route.method !== 'GET');
100
- const forged = {
101
- cookie: 'coffre_session=conformance-forged; __Host-coffre_session=conformance-forged; CF_Authorization=conformance-forged',
102
- 'cf-access-jwt-assertion': 'conformance-forged',
103
- origin: 'https://attacker.example',
104
- 'sec-fetch-site': 'cross-site',
105
- 'content-type': 'application/json',
106
- };
107
- for (const { key, method, url } of [...changes, { key: 'POST /auth/signout', method: 'POST', url: `${origin}/auth/signout` }]) {
108
- const response = await fetch(url, { method, headers: forged, body: '{}', redirect: 'manual' });
109
- const text = await response.text();
110
- expect(response.status === 403, `${key} from another site answered ${response.status}`, text);
111
- }
112
- return `${changes.length + 1} changes, sign-out included, from another site with a session cookie: 403`;
113
- }
114
-
115
- /** `GET /api/auth` names how to sign in, and nothing more. */
116
- async function signinInfo(origin: string): Promise<string> {
117
- const response = await fetch(`${origin}/api/auth`);
118
- expect(response.ok, `/api/auth answered ${response.status}`);
119
- const info = (await response.json()) as AuthInfo;
120
- expect(sameKeys(info, ['access', 'signin']), '/api/auth says more than how to sign in', info);
121
- expect((info.signin === null) !== (info.access === null), '/api/auth names neither or both ways in', info);
122
- if (info.access !== null) {
123
- expect(sameKeys(info.access, ['assertion']) && info.access.assertion === false, '/api/auth says more of Access than whether it vouched', info);
124
- return 'Cloudflare Access, and nothing more';
125
- }
126
- const signin = info.signin!;
127
- expect(sameKeys(signin, ['note', 'providers', 'title']), '/api/auth says more of its sign-in than a page needs', signin);
128
- for (const provider of signin.providers) {
129
- expect(sameKeys(provider, ['brand', 'id', 'label']), `/api/auth says more of ${provider.id} than its button`, provider);
130
- }
131
- return `coffre's sign-in through ${signin.providers.map((provider) => provider.id).join(', ')}, and nothing more`;
132
- }
133
-
134
- /**
135
- * Nothing that could be a stored value in what no one is shown. Without a
136
- * canary this is a best effort: the API's refusals are checked to be only
137
- * refusals above, every closed page must send no one to /login with an
138
- * empty body, and the open pages must carry no credential coffre issues.
139
- */
140
- async function anonymousAnswers(origin: string): Promise<string> {
141
- const pages = [...CLOSED_PAGES, `/projects/${NOWHERE.project}`, `/projects/${NOWHERE.project}/${NOWHERE.environment}`];
142
- for (const path of pages) {
143
- // Followed by hand, on this origin only, each hop without a body.
144
- let at = path;
145
- for (let hop = 0; at !== '/login'; hop++) {
146
- const response = await fetch(`${origin}${at}`, { redirect: 'manual' });
147
- const text = await response.text();
148
- const to = new URL(response.headers.get('location') ?? at, origin);
149
- expect(
150
- hop < 3 && response.status >= 300 && response.status < 400 && to.origin === origin,
151
- `${at} answered no one ${response.status}${response.headers.has('location') ? ` to ${to.href}` : ''}, not a redirect on to /login`,
152
- text.slice(0, 2000),
153
- );
154
- expect(text.trim().length < 512, `${at} sent no one a body with its redirect`, text.slice(0, 2000));
155
- at = to.pathname;
156
- }
157
- }
158
- for (const path of OPEN_PAGES) {
159
- const response = await fetch(`${origin}${path}`, { redirect: 'manual' });
160
- const text = await response.text();
161
- expect(response.status === 200, `${path} answered ${response.status}`);
162
- const issued = /coffre_(svc|cli|web)_[A-Za-z0-9_-]{20,}/.exec(text);
163
- expect(issued === null, `${path} carries a credential coffre issues`, issued?.[0]);
164
- }
165
- return `best effort, without a canary: ${pages.length} closed pages send no one to /login with nothing else; /login carries no credential`;
166
- }
167
-
168
- // --- with a token ---------------------------------------------------------------
169
-
170
- type Token = { member: string };
171
-
172
- /** The token is a service's, and reads the canary's environment. */
173
- async function whoIsToken(origin: string, api: CoffreClient, canary: Canary): Promise<{ detail: string; value: Token }> {
174
- const auth = (await (await fetch(`${origin}/api/auth`)).json()) as AuthInfo;
175
- if (auth.signin === null) throw new Skip("behind Cloudflare Access, coffre issues no service tokens: the token's checks need coffre's own sign-in");
176
- const me = await api.me();
177
- expect(me.principal.type === 'service', `the token is ${me.principal.type}:${me.principal.id}'s, not a service's`);
178
- const member = `token:${me.principal.id}`;
179
- const place = `${canary.project}/${canary.environment}`;
180
- const reads = me.environments.find((env) => `${env.project}/${env.environment}` === place);
181
- expect(reads?.permissions.includes('secret.read'), `${member} cannot read ${place}`, me.environments);
182
- const { keys } = await api.secrets.list(place);
183
- expect(keys.some((key) => key.key === canary.key), `${place} holds no ${canary.key}`, keys.map((key) => key.key));
184
- return { detail: `${member}, reading ${place}`, value: { member } };
185
- }
186
-
187
- /** Reveal the canary once, and find its `secret.read` in the audit log, by the reveal's request. */
188
- async function tokenReveal(api: CoffreClient, canary: Canary, token: Token): Promise<string> {
189
- const path = `${canary.project}/${canary.environment}`;
190
- const { operationId, values } = await api.secrets.reveal(`${path}/${canary.key}`);
191
- expect(values[canary.key] === canary.value, `${path}/${canary.key} is not the value given as the canary`);
192
- let entries;
193
- try {
194
- ({ entries } = await api.audit.list({ path, actor: token.member, limit: 50 }));
195
- } catch (error) {
196
- if (error instanceof CoffreError && error.status === 403) {
197
- throw new Skip(`the token holds no audit.read on ${canary.project}, so the reveal's entry cannot be looked for`);
198
- }
199
- throw error;
200
- }
201
- const read = entries.filter((entry) => entry.operationId === operationId);
202
- expect(read.length === 1, `the reveal is logged ${read.length} times, not once`, entries.slice(0, 5));
203
- const [entry] = read as [(typeof read)[number]];
204
- expect(
205
- entry.action === 'secret.read' && entry.decision === 'allow' && entry.key === canary.key && entry.requestId !== null,
206
- "the reveal's entry is not an allowed read of the canary, under a request",
207
- entry,
208
- );
209
- return `one secret.read of ${canary.key}, entry ${entry.seq}, under request ${entry.requestId}`;
210
- }
211
-
212
- /**
213
- * The canary's value, in any answer to a GET route as the token or as no
214
- * one, or in any page. The reveal is the only answer allowed to carry it.
215
- */
216
- async function tokenScan(origin: string, token: string, canary: Canary): Promise<string> {
217
- const api = bearer(origin, token);
218
- const me = await api.me();
219
- const member = `token:${me.principal.id}`;
220
- const listed = me.environments.map(({ project, environment }) => ({ project, environment }));
221
- const places = [...listed, NOWHERE];
222
- const calls = getCalls({
223
- places,
224
- secrets: [{ project: canary.project, environment: canary.environment, key: canary.key }, { ...NOWHERE, key: 'NONE' }],
225
- members: [member, NOBODY, 'user:nobody@conformance.example'],
226
- services: [member, NOBODY],
227
- });
228
- const forms = [canary.value, Buffer.from(canary.value).toString('base64'), Buffer.from(canary.value).toString('hex')];
229
- const leaks: string[] = [];
230
- const callers: [string, Record<string, string>][] = [
231
- ['the token', { authorization: `Bearer ${token}` }],
232
- ['no one', {}],
233
- ];
234
- let answers = 0;
235
- const look = async (what: string, url: string, headers: Record<string, string>) => {
236
- const text = await (await fetch(url, { headers, redirect: 'manual' })).text();
237
- answers++;
238
- if (forms.some((form) => text.includes(form))) leaks.push(what);
239
- };
240
- for (const { key, url } of getUrls(origin, calls)) {
241
- for (const [name, headers] of callers) await look(`${key} as ${name}`, url, headers);
242
- }
243
- const pages = [
244
- ...OPEN_PAGES,
245
- ...CLOSED_PAGES,
246
- ...listed.flatMap(({ project, environment }) => [`/projects/${project}`, `/projects/${project}/${environment}`]),
247
- ];
248
- for (const path of new Set(pages)) {
249
- for (const [name, headers] of callers) await look(`the page ${path} as ${name}`, `${origin}${path}`, headers);
250
- }
251
- expect(leaks.length === 0, "the canary's value was found outside its reveal", leaks);
252
- return `${answers} answers from ${Object.keys(calls).length} GET routes and ${new Set(pages).size} pages, as the token and as no one: no value`;
253
- }
254
-
255
- /** The token reaches the canary's project and nothing else: not another environment, project or member. */
256
- async function tokenScope(api: CoffreClient, canary: Canary): Promise<string> {
257
- const { projects } = await api.projects.list();
258
- const seen = projects.map((project) => project.slug);
259
- expect(seen.length === 1 && seen[0] === canary.project, `the token sees ${seen.join(', ') || 'no project'}, not only ${canary.project}`);
260
- const me = await api.me();
261
- const own = `${canary.project}/${canary.environment}`;
262
- const reads = me.environments.filter((env) => env.permissions.includes('secret.read')).map((env) => `${env.project}/${env.environment}`);
263
- expect(reads.length === 1 && reads[0] === own, `the token reads ${reads.join(', ')}, not only ${own}`);
264
- const more = [...new Set(me.environments.flatMap((env) => env.permissions))].filter(
265
- (permission) => permission !== 'secret.read' && permission !== 'audit.read',
266
- );
267
- expect(more.length === 0, `the token holds more than reading: ${more.join(', ')}`);
268
-
269
- const others = projects[0]!.environments.map((env) => `${canary.project}/${env.slug}`).filter((path) => path !== own);
270
- const nowhere = `${NOWHERE.project}/${NOWHERE.environment}`;
271
- let refusals = 0;
272
- for (const path of [...others, nowhere]) {
273
- await refused(`the token listed ${path}`, api.secrets.list(path));
274
- await refused(`the token revealed ${path}`, api.secrets.reveal(path));
275
- await refused(`the token read ${path}'s syncs`, api.syncs.list(path));
276
- refusals += 3;
277
- }
278
- // Its project's log is the token's to read, as `audit.read` there; another's is not.
279
- await refused(`the token read ${nowhere}'s audit`, api.audit.list({ path: nowhere, limit: 1 }));
280
- await refused('the token listed the members', api.members.list());
281
- await refused(`the token read ${NOBODY}`, api.members.get(NOBODY));
282
- refusals += 3;
283
- try {
284
- const { entries } = await api.audit.list({ limit: 500 });
285
- const outside = entries.filter((entry) => entry.project !== null && entry.project !== canary.project);
286
- expect(outside.length === 0, `the token reads audit entries about ${[...new Set(outside.map((entry) => entry.project))].join(', ')}`);
287
- } catch (error) {
288
- if (!(error instanceof CoffreError && error.status === 403)) throw error;
289
- }
290
- const elsewhere = others.length === 0 ? 'no other environment' : `${others.length} other environment${others.length === 1 ? '' : 's'}`;
291
- return `only ${own}; ${refusals} reads elsewhere refused: ${elsewhere} of ${canary.project}, a made-up place, the members`;
292
- }
293
-
294
- async function tokenVerification(api: CoffreClient): Promise<string> {
295
- let verified;
296
- try {
297
- verified = await api.audit.verify();
298
- } catch (error) {
299
- if (error instanceof CoffreError && error.status === 403) {
300
- throw new Skip('verification is for owners and root admins, which this token is not: not checked');
301
- }
302
- throw error;
303
- }
304
- expect(verified.ok, 'the audit log does not verify', verified);
305
- return `${verified.entries} entries verify, through entry ${verified.through}`;
306
- }
307
-
308
- // --- helpers --------------------------------------------------------------------
309
-
310
- /** `{ error, message }` and nothing else. */
311
- function onlyRefusal(text: string): boolean {
312
- try {
313
- return sameKeys(JSON.parse(text), ['error', 'message']);
314
- } catch {
315
- return false;
316
- }
317
- }
318
-
319
- function sameKeys(value: unknown, keys: string[]): boolean {
320
- return typeof value === 'object' && value !== null && JSON.stringify(Object.keys(value).sort()) === JSON.stringify(keys);
321
- }
@@ -1,303 +0,0 @@
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
- }