@coffre/client 0.6.2 → 0.8.0
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/credential.d.ts +24 -0
- package/dist/credential.js +26 -0
- package/dist/{index-M5Gp4kSA.d.ts → index-DgII_R1y.d.ts} +425 -15
- package/dist/index.d.ts +2 -2
- package/dist/index.js +31 -1
- package/dist/routes.d.ts +1 -1
- package/dist/routes.js +6 -0
- package/package.json +6 -1
- package/src/api.ts +294 -15
- package/src/credential.ts +48 -0
- package/src/index.ts +40 -2
- package/src/routes.ts +6 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A credential through coffre's credential proxy, for an app `coffre run`
|
|
3
|
+
* started with stand-ins (docs/design/credential-proxy.md): where its
|
|
4
|
+
* client takes no base URL to point at the proxy, or the code is new.
|
|
5
|
+
*
|
|
6
|
+
* import { credential } from '@coffre/client/credential';
|
|
7
|
+
* const schumann = credential('schumann');
|
|
8
|
+
* const companies = await (await schumann.fetch('/companies?q=acme')).json();
|
|
9
|
+
*
|
|
10
|
+
* `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
|
|
11
|
+
* proxy by name, and `COFFRE_HANDLE`, the handle that stands in for the
|
|
12
|
+
* environment's values; the proxy injects the key, and the app never holds it.
|
|
13
|
+
*/
|
|
14
|
+
export type ProxiedCredential = {
|
|
15
|
+
/** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
|
|
16
|
+
url: string;
|
|
17
|
+
/** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
|
|
18
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Env = Record<string, string | undefined>;
|
|
22
|
+
|
|
23
|
+
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
24
|
+
export function credential(
|
|
25
|
+
name: string,
|
|
26
|
+
env: Env = (globalThis as { process?: { env: Env } }).process?.env ?? {},
|
|
27
|
+
fetcher: (input: string, init?: RequestInit) => Promise<Response> = (input, init) => fetch(input, init),
|
|
28
|
+
): ProxiedCredential {
|
|
29
|
+
let urls: Record<string, unknown>;
|
|
30
|
+
try {
|
|
31
|
+
urls = JSON.parse(env.COFFRE_CREDENTIALS ?? '{}') as Record<string, unknown>;
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error('COFFRE_CREDENTIALS is not what `coffre run` sets: run the app with `coffre run <project>/<environment> -- …`');
|
|
34
|
+
}
|
|
35
|
+
const url = urls[name];
|
|
36
|
+
const handle = env.COFFRE_HANDLE;
|
|
37
|
+
if (typeof url !== 'string' || handle === undefined) {
|
|
38
|
+
throw new Error(`no credential ${name} here: \`coffre run\` gives the app the credentials its environment holds, which the deployment's proxy names`);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
url,
|
|
42
|
+
fetch: (path, init = {}) => {
|
|
43
|
+
const headers = new Headers(init.headers);
|
|
44
|
+
headers.set('x-coffre-handle', handle);
|
|
45
|
+
return fetcher(`${url}${path.startsWith('/') ? '' : '/'}${path}`, { ...init, headers });
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* its own handling of Cloudflare Access redirects. A request that never
|
|
11
11
|
* reaches the instance throws `Unreachable`, which says why.
|
|
12
12
|
*/
|
|
13
|
-
import type { Api, AuthInfo, BindingPlan, BindingView, DeletionResult, DryRunOutcome, DryRunResult, SetResult, TokenPlan } from './api.ts';
|
|
13
|
+
import type { Api, AuthInfo, BindingPlan, BindingView, DeletionResult, DryRunOutcome, DryRunResult, InheritResult, KeysPlan, SetResult, TokenPlan } from './api.ts';
|
|
14
14
|
import { Unreachable } from './reach.ts';
|
|
15
15
|
|
|
16
16
|
export type {
|
|
@@ -30,21 +30,24 @@ export type {
|
|
|
30
30
|
DryRunOutcome,
|
|
31
31
|
DryRunResult,
|
|
32
32
|
IdentityRow,
|
|
33
|
+
InheritResult,
|
|
34
|
+
KeysPlan,
|
|
33
35
|
ListedReference,
|
|
34
36
|
Me,
|
|
35
37
|
Member,
|
|
36
38
|
MemberAccess,
|
|
37
39
|
MissingKey,
|
|
38
40
|
OffboardingReport,
|
|
41
|
+
PersonalConfig,
|
|
39
42
|
ProjectSummary,
|
|
40
43
|
ReferenceView,
|
|
41
44
|
RemovedMember,
|
|
42
45
|
SecretKey,
|
|
43
46
|
SecretVersion,
|
|
44
|
-
ServiceTokenRow,
|
|
45
47
|
SessionRow,
|
|
46
48
|
SetResult,
|
|
47
49
|
TokenPlan,
|
|
50
|
+
TokenRow,
|
|
48
51
|
WorkloadIds,
|
|
49
52
|
} from './api.ts';
|
|
50
53
|
|
|
@@ -202,6 +205,14 @@ export function createClient(options: ClientOptions) {
|
|
|
202
205
|
delete: (path: string) => call('DELETE /projects/:project/:environment', place(path)),
|
|
203
206
|
/** The keys its sibling environments have and it lacks, read or not, and those its team dismissed. */
|
|
204
207
|
missing: (path: string) => call('GET /projects/:project/:environment/missing', place(path)),
|
|
208
|
+
/** A project's personal configs, for whoever manages its environments: whose, of what, and their key names. */
|
|
209
|
+
personal: (project: string) => call('GET /personal/:project', place(project)),
|
|
210
|
+
/**
|
|
211
|
+
* A version of what reading it would open: it changes whenever a read
|
|
212
|
+
* would, and reads no value, so it is not logged. Poll it, and read
|
|
213
|
+
* only when it changed.
|
|
214
|
+
*/
|
|
215
|
+
version: (path: string) => call('GET /projects/:project/:environment/version', place(path)),
|
|
205
216
|
/** Dismiss missing keys (`true`) or restore them (`null`). */
|
|
206
217
|
dismiss: (path: string, patch: RouteInput<'PATCH /projects/:project/:environment/dismissals'>) =>
|
|
207
218
|
call('PATCH /projects/:project/:environment/dismissals', place(path), patch),
|
|
@@ -232,6 +243,12 @@ export function createClient(options: ClientOptions) {
|
|
|
232
243
|
call('PATCH /secrets/:project/:environment/:key', place(path), { key }),
|
|
233
244
|
update: (path: string, patch: RouteInput<'PATCH /secrets/:project/:environment/:key'>) =>
|
|
234
245
|
call('PATCH /secrets/:project/:environment/:key', place(path), patch),
|
|
246
|
+
/**
|
|
247
|
+
* In a fork, a key back to its parent's value (`true`), its own kept,
|
|
248
|
+
* or its own served again (`false`); `from` holds the parent's.
|
|
249
|
+
*/
|
|
250
|
+
inherit: (path: string, inherit: boolean) =>
|
|
251
|
+
call('PATCH /secrets/:project/:environment/:key', place(path), { inherit }) as Promise<InheritResult>,
|
|
235
252
|
},
|
|
236
253
|
|
|
237
254
|
/**
|
|
@@ -258,6 +275,10 @@ export function createClient(options: ClientOptions) {
|
|
|
258
275
|
remove: (member: string) => call('DELETE /members/:member', { member }),
|
|
259
276
|
},
|
|
260
277
|
|
|
278
|
+
/**
|
|
279
|
+
* A member's tokens: a service account's bearer tokens, or a person's
|
|
280
|
+
* personal tokens (`user:<email>`), which act as them within their scope.
|
|
281
|
+
*/
|
|
261
282
|
tokens: {
|
|
262
283
|
list: (member: string) => call('GET /members/:member/tokens', { member }),
|
|
263
284
|
/** What the token would reach, as the server would issue it now; issues nothing. */
|
|
@@ -271,6 +292,15 @@ export function createClient(options: ClientOptions) {
|
|
|
271
292
|
revoke: (member: string, id: string) => call('DELETE /members/:member/tokens/:id', { member, id }),
|
|
272
293
|
},
|
|
273
294
|
|
|
295
|
+
/**
|
|
296
|
+
* Handles: stand-ins a person's app holds in place of an environment's
|
|
297
|
+
* values, which only the credential proxy (`/use`) takes. The value is
|
|
298
|
+
* in the answer, and only there; it is revoked as a token is.
|
|
299
|
+
*/
|
|
300
|
+
handles: {
|
|
301
|
+
create: (input: RouteInput<'POST /handles'>) => call('POST /handles', {}, input),
|
|
302
|
+
},
|
|
303
|
+
|
|
274
304
|
/** Trust bindings: which CI runs may sign in as a service, by their platform's ID token. */
|
|
275
305
|
bindings: {
|
|
276
306
|
list: (member: string) => call('GET /members/:member/bindings', { member }),
|
|
@@ -281,6 +311,14 @@ export function createClient(options: ClientOptions) {
|
|
|
281
311
|
},
|
|
282
312
|
create: (member: string, input: RouteInput<'POST /members/:member/bindings'>) =>
|
|
283
313
|
call('POST /members/:member/bindings', { member }, input) as Promise<{ binding: BindingView; replaced: string[] }>,
|
|
314
|
+
/** The service's bindings on an issuer that new keys would replace, and the keys as checked; writes nothing. */
|
|
315
|
+
previewKeys: (member: string, input: RouteInput<'POST /members/:member/bindings/keys'>) => {
|
|
316
|
+
const [method, route] = address('POST /members/:member/bindings/keys', { member });
|
|
317
|
+
return send(method, `${route}?dryRun=1`, input) as Promise<KeysPlan>;
|
|
318
|
+
},
|
|
319
|
+
/** New keys, uploaded, for every binding of the service on the issuer: each replaced by one the same but for them. */
|
|
320
|
+
replaceKeys: (member: string, input: RouteInput<'POST /members/:member/bindings/keys'>) =>
|
|
321
|
+
call('POST /members/:member/bindings/keys', { member }, input) as Promise<{ bindings: BindingView[]; replaced: string[] }>,
|
|
284
322
|
remove: (member: string, id: string) => call('DELETE /members/:member/bindings/:id', { member, id }),
|
|
285
323
|
/** A GitHub repository's or GitLab project's IDs, which bindings name: a public one's, or one the deployment's GitHub App is installed on. */
|
|
286
324
|
lookup: (input: RouteInput<'GET /workloads/lookup'>) => call('GET /workloads/lookup', {}, input),
|
package/src/routes.ts
CHANGED
|
@@ -28,6 +28,8 @@ export function getCalls({ places, secrets, members, services }: Subjects): GetC
|
|
|
28
28
|
'GET /projects': [{ params: {} }],
|
|
29
29
|
'GET /secrets/:project/:environment': places.map((params) => ({ params })),
|
|
30
30
|
'GET /projects/:project/:environment/missing': places.map((params) => ({ params })),
|
|
31
|
+
'GET /personal/:project': [...new Set(places.map((params) => params.project))].map((project) => ({ params: { project } })),
|
|
32
|
+
'GET /projects/:project/:environment/version': places.map((params) => ({ params })),
|
|
31
33
|
'GET /secrets/:project/:environment/:key/versions': secrets.map((params) => ({ params })),
|
|
32
34
|
'GET /members': [
|
|
33
35
|
{ params: {} },
|
|
@@ -75,6 +77,8 @@ const EVERY_ROUTE: { [K in RouteKey]: true } = {
|
|
|
75
77
|
'PATCH /projects/:project/:environment': true,
|
|
76
78
|
'DELETE /projects/:project/:environment': true,
|
|
77
79
|
'GET /projects/:project/:environment/missing': true,
|
|
80
|
+
'GET /personal/:project': true,
|
|
81
|
+
'GET /projects/:project/:environment/version': true,
|
|
78
82
|
'PATCH /projects/:project/:environment/dismissals': true,
|
|
79
83
|
'GET /secrets/:project/:environment': true,
|
|
80
84
|
'PATCH /secrets/:project/:environment': true,
|
|
@@ -96,8 +100,10 @@ const EVERY_ROUTE: { [K in RouteKey]: true } = {
|
|
|
96
100
|
'GET /members/:member/tokens': true,
|
|
97
101
|
'POST /members/:member/tokens': true,
|
|
98
102
|
'DELETE /members/:member/tokens/:id': true,
|
|
103
|
+
'POST /handles': true,
|
|
99
104
|
'GET /members/:member/bindings': true,
|
|
100
105
|
'POST /members/:member/bindings': true,
|
|
106
|
+
'POST /members/:member/bindings/keys': true,
|
|
101
107
|
'DELETE /members/:member/bindings/:id': true,
|
|
102
108
|
'GET /workloads/lookup': true,
|
|
103
109
|
'GET /workloads/repositories': true,
|