@yaks/secrets 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/_dist/graph.d.ts +9 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +67 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/op.d.ts +37 -0
- package/_dist/op.d.ts.map +1 -0
- package/_dist/plugin.d.ts +19 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/records.d.ts +11 -0
- package/_dist/records.d.ts.map +1 -0
- package/_dist/ref.d.ts +4 -0
- package/_dist/ref.d.ts.map +1 -0
- package/_dist/reveal.d.ts +14 -0
- package/_dist/reveal.d.ts.map +1 -0
- package/_dist/sealed.d.ts +10 -0
- package/_dist/sealed.d.ts.map +1 -0
- package/_dist/sentinel.d.ts +9 -0
- package/_dist/sentinel.d.ts.map +1 -0
- package/_dist/signed.d.ts +16 -0
- package/_dist/signed.d.ts.map +1 -0
- package/_dist/vault.d.ts +49 -0
- package/_dist/vault.d.ts.map +1 -0
- package/_dist/views.d.ts +3 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/deno.json +1 -0
- package/graph.js +12 -0
- package/graph.js.map +1 -0
- package/graph.ts +21 -0
- package/mod.js +67 -0
- package/mod.js.map +1 -0
- package/mod.ts +83 -0
- package/op.js +170 -0
- package/op.js.map +1 -0
- package/op.ts +188 -0
- package/package.json +51 -4
- package/plugin.js +429 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +464 -0
- package/reads.js +51 -0
- package/reads.js.map +1 -0
- package/reads.ts +56 -0
- package/records.js +45 -0
- package/records.js.map +1 -0
- package/records.ts +58 -0
- package/ref.js +7 -0
- package/ref.js.map +1 -0
- package/ref.ts +11 -0
- package/reveal.js +50 -0
- package/reveal.js.map +1 -0
- package/reveal.ts +98 -0
- package/sealed.js +35 -0
- package/sealed.js.map +1 -0
- package/sealed.ts +31 -0
- package/sentinel.js +66 -0
- package/sentinel.js.map +1 -0
- package/sentinel.ts +97 -0
- package/signed.js +107 -0
- package/signed.js.map +1 -0
- package/signed.ts +151 -0
- package/vault.js +67 -0
- package/vault.js.map +1 -0
- package/vault.ts +130 -0
- package/views.js +27 -0
- package/views.js.map +1 -0
- package/views.ts +34 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +36 -0
- package/vocab.ts +17 -0
- package/README.md +0 -3
package/mod.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/secrets — secrets that flow through the graph and are kept somewhere
|
|
3
|
+
* else.
|
|
4
|
+
*
|
|
5
|
+
* A secret is an entity wearing `secret{name, value}`. It is written with its
|
|
6
|
+
* value like anything else; the plugin takes the value out in the first phase
|
|
7
|
+
* of the write and puts the secret's handle — a random token it keeps for as
|
|
8
|
+
* long as the secret lives — where it was, and once the write commits it seals
|
|
9
|
+
* the value into a vault. The graph, its journal, its
|
|
10
|
+
* sync and its backups only ever hold the handle, which is safe to show anyone.
|
|
11
|
+
* Code that calls out is handed the sentinel instead: the handle hashed under
|
|
12
|
+
* the vault's salt, the one string a swap on the way out replaces with the
|
|
13
|
+
* value.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
17
|
+
* import { type Bundle, graph } from '@yaks/graph'
|
|
18
|
+
* import { ram } from '@yaks/ram'
|
|
19
|
+
* import { effectDoc } from '@yaks/effects'
|
|
20
|
+
* import { ramVault, reveal, sealed, secrets, secretsDoc } from '@yaks/secrets'
|
|
21
|
+
*
|
|
22
|
+
* let vocab = loadVocab([secretsDoc, effectDoc])
|
|
23
|
+
* let vault = ramVault()
|
|
24
|
+
* let write = (b: Bundle[]) => g.apply(b, { trusted: true })
|
|
25
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [secrets(vault, write, vocab)] })
|
|
26
|
+
* await g.apply([sealed('MAIL_TOKEN', 'cf-token')])
|
|
27
|
+
* // g.read('.secret') → value: '[set]'
|
|
28
|
+
* // await reveal(vault, 'MAIL_TOKEN') → 'cf-token'
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Trusted code asks for a value by name ({@link reveal}); a config names one
|
|
32
|
+
* as `{"secret": "NAME"}` (@yaks/cli). A name nothing was written for falls
|
|
33
|
+
* back to the environment source the host supplies. A kept `op://` reference
|
|
34
|
+
* stays unresolved; interactive commands can read one explicitly with
|
|
35
|
+
* `opRead` from `@yaks/secrets/op`, which starts the `op` command and so
|
|
36
|
+
* never loads where there is no operating system.
|
|
37
|
+
*
|
|
38
|
+
* This package says what a vault is ({@link Vault}) and keeps one in memory;
|
|
39
|
+
* a vault that stores somewhere lives with that storage: private files on a
|
|
40
|
+
* box in @yaks/cli, a D1 database in @yaks/d1. The host hands its vault to the
|
|
41
|
+
* plugin (`@yaks/secrets/graph`).
|
|
42
|
+
* While a value is on its way to the vault its secret wears `provisional`
|
|
43
|
+
* (@yaks/effects). A failed seal leaves `failed{reason}` (@yaks/kernel).
|
|
44
|
+
* Hosts compose these vocabularies to record sealing's progress and outcome.
|
|
45
|
+
*
|
|
46
|
+
* Rotations and removals retain previous values in the vault's trash. Active
|
|
47
|
+
* reads and sentinels cannot use trashed values. `vault.trash()` lists metadata
|
|
48
|
+
* without values. Trusted code reads a retained value with `past(id)` without
|
|
49
|
+
* changing it or activating it; it can write that value through the ordinary
|
|
50
|
+
* graph sealing path while keeping the retained version for old snapshots.
|
|
51
|
+
* `restore(id)`
|
|
52
|
+
* recovers a version, retaining any displaced
|
|
53
|
+
* active value, and `purge(id)` permanently removes only that trashed version.
|
|
54
|
+
* Hosts expose purging through a person's explicit confirmation. Restoring a
|
|
55
|
+
* graph secret with its retained handle also restores its most recent value
|
|
56
|
+
* when no active value exists.
|
|
57
|
+
*
|
|
58
|
+
* @module
|
|
59
|
+
*/ export { secretsDoc } from "./vocab.js";
|
|
60
|
+
export { carries, retryable, SECRET, secrets } from "./plugin.js";
|
|
61
|
+
export { handle, isHandle, PREFIX, SENTINEL, sentinel, sentinels, swap } from "./sentinel.js";
|
|
62
|
+
export { queue, ramVault } from "./vault.js";
|
|
63
|
+
export { isOpRef } from "./ref.js";
|
|
64
|
+
export { peek, reveal, reveals, sentinelOf } from "./reveal.js";
|
|
65
|
+
export { sealed, secretEid, secretEids, unsealed } from "./sealed.js";
|
|
66
|
+
export { records } from "./records.js";
|
|
67
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0DC,GAED,SAAS,UAAU,qBAAoB;AACvC,SAAS,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,sBAAqB;AACjE,SACE,MAAM,EACN,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,IAAI,wBACgB;AACtB,SAEE,KAAK,EACL,QAAQ,qBAIS;AACnB,SAAS,OAAO,mBAAkB;AAClC,SAAS,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,sBAAmC;AAC7E,SAAS,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,sBAAqB;AACrE,SAAuB,OAAO,uBAAsB"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/secrets — secrets that flow through the graph and are kept somewhere
|
|
3
|
+
* else.
|
|
4
|
+
*
|
|
5
|
+
* A secret is an entity wearing `secret{name, value}`. It is written with its
|
|
6
|
+
* value like anything else; the plugin takes the value out in the first phase
|
|
7
|
+
* of the write and puts the secret's handle — a random token it keeps for as
|
|
8
|
+
* long as the secret lives — where it was, and once the write commits it seals
|
|
9
|
+
* the value into a vault. The graph, its journal, its
|
|
10
|
+
* sync and its backups only ever hold the handle, which is safe to show anyone.
|
|
11
|
+
* Code that calls out is handed the sentinel instead: the handle hashed under
|
|
12
|
+
* the vault's salt, the one string a swap on the way out replaces with the
|
|
13
|
+
* value.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
17
|
+
* import { type Bundle, graph } from '@yaks/graph'
|
|
18
|
+
* import { ram } from '@yaks/ram'
|
|
19
|
+
* import { effectDoc } from '@yaks/effects'
|
|
20
|
+
* import { ramVault, reveal, sealed, secrets, secretsDoc } from '@yaks/secrets'
|
|
21
|
+
*
|
|
22
|
+
* let vocab = loadVocab([secretsDoc, effectDoc])
|
|
23
|
+
* let vault = ramVault()
|
|
24
|
+
* let write = (b: Bundle[]) => g.apply(b, { trusted: true })
|
|
25
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [secrets(vault, write, vocab)] })
|
|
26
|
+
* await g.apply([sealed('MAIL_TOKEN', 'cf-token')])
|
|
27
|
+
* // g.read('.secret') → value: '[set]'
|
|
28
|
+
* // await reveal(vault, 'MAIL_TOKEN') → 'cf-token'
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Trusted code asks for a value by name ({@link reveal}); a config names one
|
|
32
|
+
* as `{"secret": "NAME"}` (@yaks/cli). A name nothing was written for falls
|
|
33
|
+
* back to the environment source the host supplies. A kept `op://` reference
|
|
34
|
+
* stays unresolved; interactive commands can read one explicitly with
|
|
35
|
+
* `opRead` from `@yaks/secrets/op`, which starts the `op` command and so
|
|
36
|
+
* never loads where there is no operating system.
|
|
37
|
+
*
|
|
38
|
+
* This package says what a vault is ({@link Vault}) and keeps one in memory;
|
|
39
|
+
* a vault that stores somewhere lives with that storage: private files on a
|
|
40
|
+
* box in @yaks/cli, a D1 database in @yaks/d1. The host hands its vault to the
|
|
41
|
+
* plugin (`@yaks/secrets/graph`).
|
|
42
|
+
* While a value is on its way to the vault its secret wears `provisional`
|
|
43
|
+
* (@yaks/effects). A failed seal leaves `failed{reason}` (@yaks/kernel).
|
|
44
|
+
* Hosts compose these vocabularies to record sealing's progress and outcome.
|
|
45
|
+
*
|
|
46
|
+
* Rotations and removals retain previous values in the vault's trash. Active
|
|
47
|
+
* reads and sentinels cannot use trashed values. `vault.trash()` lists metadata
|
|
48
|
+
* without values. Trusted code reads a retained value with `past(id)` without
|
|
49
|
+
* changing it or activating it; it can write that value through the ordinary
|
|
50
|
+
* graph sealing path while keeping the retained version for old snapshots.
|
|
51
|
+
* `restore(id)`
|
|
52
|
+
* recovers a version, retaining any displaced
|
|
53
|
+
* active value, and `purge(id)` permanently removes only that trashed version.
|
|
54
|
+
* Hosts expose purging through a person's explicit confirmation. Restoring a
|
|
55
|
+
* graph secret with its retained handle also restores its most recent value
|
|
56
|
+
* when no active value exists.
|
|
57
|
+
*
|
|
58
|
+
* @module
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
export { secretsDoc } from './vocab.js'
|
|
62
|
+
export { carries, retryable, SECRET, secrets } from './plugin.js'
|
|
63
|
+
export {
|
|
64
|
+
handle,
|
|
65
|
+
isHandle,
|
|
66
|
+
PREFIX,
|
|
67
|
+
SENTINEL,
|
|
68
|
+
sentinel,
|
|
69
|
+
sentinels,
|
|
70
|
+
swap,
|
|
71
|
+
} from './sentinel.js'
|
|
72
|
+
export {
|
|
73
|
+
type Local,
|
|
74
|
+
queue,
|
|
75
|
+
ramVault,
|
|
76
|
+
type Sealed,
|
|
77
|
+
type Trashed,
|
|
78
|
+
type Vault,
|
|
79
|
+
} from './vault.js'
|
|
80
|
+
export { isOpRef } from './ref.js'
|
|
81
|
+
export { peek, reveal, reveals, sentinelOf, type Sources } from './reveal.js'
|
|
82
|
+
export { sealed, secretEid, secretEids, unsealed } from './sealed.js'
|
|
83
|
+
export { type Records, records } from './records.js'
|
package/op.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Explicit 1Password reads for a person's interactive command, and the
|
|
2
|
+
// keeper that writes a key given to the box into 1Password. Runtime
|
|
3
|
+
// secret readers (reveal and peek) never start op. A read uses a cleared
|
|
4
|
+
// environment, closed stdin, bounded output and the caller's abort signal.
|
|
5
|
+
import { command } from "@yaks/process/command";
|
|
6
|
+
import { env } from "@yaks/process/env";
|
|
7
|
+
import { isNotFound } from "@yaks/file/io";
|
|
8
|
+
import { reveal } from "./reveal.js";
|
|
9
|
+
let MAX_OUT = 65_536;
|
|
10
|
+
// All `op` may see of this process's environment: where to find itself, and
|
|
11
|
+
// the session a person or a service account signed it into.
|
|
12
|
+
let opEnv = (env)=>{
|
|
13
|
+
let out = {
|
|
14
|
+
PATH: env('PATH') ?? '/usr/local/bin:/usr/bin:/bin'
|
|
15
|
+
};
|
|
16
|
+
for (let name of [
|
|
17
|
+
'HOME',
|
|
18
|
+
'XDG_CONFIG_HOME',
|
|
19
|
+
'OP_SERVICE_ACCOUNT_TOKEN'
|
|
20
|
+
]){
|
|
21
|
+
let value = env(name);
|
|
22
|
+
if (value) out[name] = value;
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
let dec = new TextDecoder();
|
|
27
|
+
// What op answered: the value, or its own words as the error.
|
|
28
|
+
let answer = (out)=>{
|
|
29
|
+
if (!out.success) {
|
|
30
|
+
throw new Error(dec.decode(out.stderr.slice(0, MAX_OUT)).trim() || 'op read failed');
|
|
31
|
+
}
|
|
32
|
+
return dec.decode(out.stdout.slice(0, MAX_OUT));
|
|
33
|
+
};
|
|
34
|
+
/** `op read`, awaited under the signal it is given. */ export let opRead = (environment)=>async (reference, signal)=>{
|
|
35
|
+
let out = await command('op', {
|
|
36
|
+
args: [
|
|
37
|
+
'read',
|
|
38
|
+
'--no-newline',
|
|
39
|
+
reference
|
|
40
|
+
],
|
|
41
|
+
clearEnv: true,
|
|
42
|
+
env: opEnv(environment ?? env.get),
|
|
43
|
+
stdin: 'null',
|
|
44
|
+
stdout: 'piped',
|
|
45
|
+
stderr: 'piped',
|
|
46
|
+
signal
|
|
47
|
+
}).output().catch((error)=>{
|
|
48
|
+
throw isNotFound(error) ? new Error('op is not installed or not on PATH') : error;
|
|
49
|
+
});
|
|
50
|
+
return answer(out);
|
|
51
|
+
};
|
|
52
|
+
/** `op`, run with the environment {@link opRead} gives it and `input` on its
|
|
53
|
+
* stdin, so a value never rides its arguments. */ export let opRun = (environment)=>async (args, input)=>{
|
|
54
|
+
let child = command('op', {
|
|
55
|
+
args,
|
|
56
|
+
clearEnv: true,
|
|
57
|
+
env: opEnv(environment ?? env.get),
|
|
58
|
+
stdin: input == null ? 'null' : 'piped',
|
|
59
|
+
stdout: 'piped',
|
|
60
|
+
stderr: 'piped'
|
|
61
|
+
}).spawn();
|
|
62
|
+
if (input != null) {
|
|
63
|
+
let w = child.stdin.getWriter();
|
|
64
|
+
await w.write(new TextEncoder().encode(input));
|
|
65
|
+
await w.close();
|
|
66
|
+
}
|
|
67
|
+
return await child.output();
|
|
68
|
+
};
|
|
69
|
+
/** Where a key goes in 1Password: `<vault>/<item>`, or `<vault>/<item>/<field>`
|
|
70
|
+
* for a field other than an API credential's `credential`. */ let placeOf = (at)=>{
|
|
71
|
+
let [vault, item, field = 'credential', ...rest] = at.split('/');
|
|
72
|
+
if (!vault || !item || rest.length) {
|
|
73
|
+
throw new Error(`a 1Password place is <vault>/<item>[/<field>], not ${at}`);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
vault,
|
|
77
|
+
item,
|
|
78
|
+
field
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/** Keeps a key in 1Password at `<vault>/<item>[/<field>]`: the item's field is
|
|
82
|
+
* set where the item stands, and an API credential item is made where it does
|
|
83
|
+
* not. The value goes to `op` on its stdin, inside an item's JSON, never in
|
|
84
|
+
* its arguments.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { assertEquals } from '@std/assert'
|
|
88
|
+
* let calls: [string[], string | undefined][] = []
|
|
89
|
+
* let run: OpRun = (args, input) => {
|
|
90
|
+
* calls.push([args, input])
|
|
91
|
+
* let stuck = args[1] == 'get'
|
|
92
|
+
* return Promise.resolve({
|
|
93
|
+
* success: !stuck,
|
|
94
|
+
* stdout: new Uint8Array(),
|
|
95
|
+
* stderr: new TextEncoder().encode(stuck ? '"Logs" isn\'t an item' : ''),
|
|
96
|
+
* })
|
|
97
|
+
* }
|
|
98
|
+
* await opKeep(run)('Shared/Logs', 'k')
|
|
99
|
+
* assertEquals(calls[1][0], ['item', 'create', '--vault', 'Shared', '-'])
|
|
100
|
+
* assertEquals(JSON.parse(calls[1][1]!).fields[0].value, 'k')
|
|
101
|
+
* ```
|
|
102
|
+
*/ export let opKeep = (run = opRun())=>async (at, value)=>{
|
|
103
|
+
let { vault, item, field } = placeOf(at);
|
|
104
|
+
let got = await run([
|
|
105
|
+
'item',
|
|
106
|
+
'get',
|
|
107
|
+
item,
|
|
108
|
+
'--vault',
|
|
109
|
+
vault,
|
|
110
|
+
'--format',
|
|
111
|
+
'json'
|
|
112
|
+
]);
|
|
113
|
+
if (!got.success) {
|
|
114
|
+
let why = dec.decode(got.stderr);
|
|
115
|
+
if (!/isn't an item/.test(why)) answer(got);
|
|
116
|
+
let made = {
|
|
117
|
+
title: item,
|
|
118
|
+
category: 'API_CREDENTIAL',
|
|
119
|
+
fields: [
|
|
120
|
+
{
|
|
121
|
+
id: field,
|
|
122
|
+
label: field,
|
|
123
|
+
type: 'CONCEALED',
|
|
124
|
+
value
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
};
|
|
128
|
+
answer(await run([
|
|
129
|
+
'item',
|
|
130
|
+
'create',
|
|
131
|
+
'--vault',
|
|
132
|
+
vault,
|
|
133
|
+
'-'
|
|
134
|
+
], JSON.stringify(made)));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
let held = JSON.parse(answer(got));
|
|
138
|
+
let fields = held.fields ?? [];
|
|
139
|
+
let at_ = fields.find((f)=>f.id == field || f.label == field);
|
|
140
|
+
if (at_) at_.value = value;
|
|
141
|
+
else fields.push({
|
|
142
|
+
id: field,
|
|
143
|
+
label: field,
|
|
144
|
+
type: 'CONCEALED',
|
|
145
|
+
value
|
|
146
|
+
});
|
|
147
|
+
answer(await run([
|
|
148
|
+
'item',
|
|
149
|
+
'edit',
|
|
150
|
+
held.id ?? item,
|
|
151
|
+
'--vault',
|
|
152
|
+
vault
|
|
153
|
+
], JSON.stringify({
|
|
154
|
+
...held,
|
|
155
|
+
fields
|
|
156
|
+
})));
|
|
157
|
+
};
|
|
158
|
+
/** The `1password` keeper a host lends (@yaks/host/keepers): {@link opKeep}
|
|
159
|
+
* signed in as the service account whose token is `OP_SERVICE_ACCOUNT_TOKEN`
|
|
160
|
+
* in `vault`, else in the environment, read at each keep. */ export let opKeeper = (vault)=>({
|
|
161
|
+
keep: async (at, value)=>{
|
|
162
|
+
let token = await reveal(vault, TOKEN);
|
|
163
|
+
if (!token) {
|
|
164
|
+
throw new Error(`${TOKEN} is in neither the vault nor the environment`);
|
|
165
|
+
}
|
|
166
|
+
await opKeep(opRun((name)=>name == TOKEN ? token : env.get(name)))(at, value);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
let TOKEN = 'OP_SERVICE_ACCOUNT_TOKEN';
|
|
170
|
+
//# sourceMappingURL=op.js.map
|
package/op.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"op.js","sources":["./op.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,oEAAoE;AACpE,yEAAyE;AACzE,2EAA2E;AAC3E,SAAS,OAAO,qCAA0C;AAC1D,SAAS,GAAG,iCAAsC;AAClD,SAAS,UAAU,6BAAkC;AACrD,SAAS,MAAM,sBAAqB;AAMpC,IAAI,UAAU;AAId,4EAA4E;AAC5E,4DAA4D;AAC5D,IAAI,QAAQ,CAAC;EACX,IAAI,MAA8B;IAChC,MAAM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AAO;EAGvC,EAAC;AAED;;2DAE2D,GAC3D,OAAO,IAAI,WAAW,CAAC,QAAiB,CAAC;IACvC,MAAM,OAAO,IAAY;MACvB,IAAI,QAAQ,MAAM,OAAO,OAAO;MAChC,IAAI,CAAC,OAAO;QACV,MAAM,IAAI,MAAM,GAAG,MAAM,4CAA4C,CAAC;MACxE;MACA,MAAM,OAAO,MAAM,CAAC,OAAS,QAAQ,QAAQ,QAAQ,IAAI,GAAG,CAAC,QAC3D,IACA;IAEJ;EACF,CAAC,EAAC;AAEF,IAAI,QAAQ"}
|
package/op.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Explicit 1Password reads for a person's interactive command, and the
|
|
2
|
+
// keeper that writes a key given to the box into 1Password. Runtime
|
|
3
|
+
// secret readers (reveal and peek) never start op. A read uses a cleared
|
|
4
|
+
// environment, closed stdin, bounded output and the caller's abort signal.
|
|
5
|
+
import { command } from '@yaks/process/command'
|
|
6
|
+
import { env } from '@yaks/process/env'
|
|
7
|
+
import { isNotFound } from '@yaks/file/io'
|
|
8
|
+
import { reveal } from './reveal.js'
|
|
9
|
+
import type { Vault } from './vault.ts'
|
|
10
|
+
|
|
11
|
+
/** Resolve a reference to its value. Injectable, so a test needs no `op`. */
|
|
12
|
+
export type OpRead = (reference: string, signal: AbortSignal) => Promise<string>
|
|
13
|
+
|
|
14
|
+
let MAX_OUT = 65_536
|
|
15
|
+
|
|
16
|
+
type Env = (name: string) => string | undefined
|
|
17
|
+
|
|
18
|
+
// All `op` may see of this process's environment: where to find itself, and
|
|
19
|
+
// the session a person or a service account signed it into.
|
|
20
|
+
let opEnv = (env: Env) => {
|
|
21
|
+
let out: Record<string, string> = {
|
|
22
|
+
PATH: env('PATH') ?? '/usr/local/bin:/usr/bin:/bin',
|
|
23
|
+
}
|
|
24
|
+
for (let name of ['HOME', 'XDG_CONFIG_HOME', 'OP_SERVICE_ACCOUNT_TOKEN']) {
|
|
25
|
+
let value = env(name)
|
|
26
|
+
if (value) out[name] = value
|
|
27
|
+
}
|
|
28
|
+
return out
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let dec = new TextDecoder()
|
|
32
|
+
|
|
33
|
+
// What op answered: the value, or its own words as the error.
|
|
34
|
+
let answer = (
|
|
35
|
+
out: { success: boolean; stdout: Uint8Array; stderr: Uint8Array },
|
|
36
|
+
): string => {
|
|
37
|
+
if (!out.success) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
dec.decode(out.stderr.slice(0, MAX_OUT)).trim() || 'op read failed',
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
return dec.decode(out.stdout.slice(0, MAX_OUT))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `op read`, awaited under the signal it is given. */
|
|
46
|
+
export let opRead =
|
|
47
|
+
(environment?: Env): OpRead => async (reference, signal) => {
|
|
48
|
+
let out = await command('op', {
|
|
49
|
+
args: ['read', '--no-newline', reference],
|
|
50
|
+
clearEnv: true,
|
|
51
|
+
env: opEnv(environment ?? env.get),
|
|
52
|
+
stdin: 'null',
|
|
53
|
+
stdout: 'piped',
|
|
54
|
+
stderr: 'piped',
|
|
55
|
+
signal,
|
|
56
|
+
}).output().catch((error) => {
|
|
57
|
+
throw isNotFound(error)
|
|
58
|
+
? new Error('op is not installed or not on PATH')
|
|
59
|
+
: error
|
|
60
|
+
})
|
|
61
|
+
return answer(out)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** One `op` command with its input on stdin, answered with its output. */
|
|
65
|
+
export type OpRun = (
|
|
66
|
+
args: string[],
|
|
67
|
+
input?: string,
|
|
68
|
+
) => Promise<{ success: boolean; stdout: Uint8Array; stderr: Uint8Array }>
|
|
69
|
+
|
|
70
|
+
/** `op`, run with the environment {@link opRead} gives it and `input` on its
|
|
71
|
+
* stdin, so a value never rides its arguments. */
|
|
72
|
+
export let opRun = (environment?: Env): OpRun => async (args, input) => {
|
|
73
|
+
let child = command('op', {
|
|
74
|
+
args,
|
|
75
|
+
clearEnv: true,
|
|
76
|
+
env: opEnv(environment ?? env.get),
|
|
77
|
+
stdin: input == null ? 'null' : 'piped',
|
|
78
|
+
stdout: 'piped',
|
|
79
|
+
stderr: 'piped',
|
|
80
|
+
}).spawn()
|
|
81
|
+
if (input != null) {
|
|
82
|
+
let w = child.stdin.getWriter()
|
|
83
|
+
await w.write(new TextEncoder().encode(input))
|
|
84
|
+
await w.close()
|
|
85
|
+
}
|
|
86
|
+
return await child.output()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Where a key goes in 1Password: `<vault>/<item>`, or `<vault>/<item>/<field>`
|
|
90
|
+
* for a field other than an API credential's `credential`. */
|
|
91
|
+
let placeOf = (at: string) => {
|
|
92
|
+
let [vault, item, field = 'credential', ...rest] = at.split('/')
|
|
93
|
+
if (!vault || !item || rest.length) {
|
|
94
|
+
throw new Error(`a 1Password place is <vault>/<item>[/<field>], not ${at}`)
|
|
95
|
+
}
|
|
96
|
+
return { vault, item, field }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type Item = {
|
|
100
|
+
id?: string
|
|
101
|
+
title?: string
|
|
102
|
+
category?: string
|
|
103
|
+
fields?: { id?: string; label?: string; type?: string; value?: string }[]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Keeps a key in 1Password at `<vault>/<item>[/<field>]`: the item's field is
|
|
107
|
+
* set where the item stands, and an API credential item is made where it does
|
|
108
|
+
* not. The value goes to `op` on its stdin, inside an item's JSON, never in
|
|
109
|
+
* its arguments.
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { assertEquals } from '@std/assert'
|
|
113
|
+
* let calls: [string[], string | undefined][] = []
|
|
114
|
+
* let run: OpRun = (args, input) => {
|
|
115
|
+
* calls.push([args, input])
|
|
116
|
+
* let stuck = args[1] == 'get'
|
|
117
|
+
* return Promise.resolve({
|
|
118
|
+
* success: !stuck,
|
|
119
|
+
* stdout: new Uint8Array(),
|
|
120
|
+
* stderr: new TextEncoder().encode(stuck ? '"Logs" isn\'t an item' : ''),
|
|
121
|
+
* })
|
|
122
|
+
* }
|
|
123
|
+
* await opKeep(run)('Shared/Logs', 'k')
|
|
124
|
+
* assertEquals(calls[1][0], ['item', 'create', '--vault', 'Shared', '-'])
|
|
125
|
+
* assertEquals(JSON.parse(calls[1][1]!).fields[0].value, 'k')
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export let opKeep = (run: OpRun = opRun()) =>
|
|
129
|
+
async (
|
|
130
|
+
at: string,
|
|
131
|
+
value: string,
|
|
132
|
+
): Promise<void> => {
|
|
133
|
+
let { vault, item, field } = placeOf(at)
|
|
134
|
+
let got = await run([
|
|
135
|
+
'item',
|
|
136
|
+
'get',
|
|
137
|
+
item,
|
|
138
|
+
'--vault',
|
|
139
|
+
vault,
|
|
140
|
+
'--format',
|
|
141
|
+
'json',
|
|
142
|
+
])
|
|
143
|
+
if (!got.success) {
|
|
144
|
+
let why = dec.decode(got.stderr)
|
|
145
|
+
if (!/isn't an item/.test(why)) answer(got)
|
|
146
|
+
let made: Item = {
|
|
147
|
+
title: item,
|
|
148
|
+
category: 'API_CREDENTIAL',
|
|
149
|
+
fields: [{ id: field, label: field, type: 'CONCEALED', value }],
|
|
150
|
+
}
|
|
151
|
+
answer(
|
|
152
|
+
await run(
|
|
153
|
+
['item', 'create', '--vault', vault, '-'],
|
|
154
|
+
JSON.stringify(made),
|
|
155
|
+
),
|
|
156
|
+
)
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
let held = JSON.parse(answer(got)) as Item
|
|
160
|
+
let fields = held.fields ?? []
|
|
161
|
+
let at_ = fields.find((f) => f.id == field || f.label == field)
|
|
162
|
+
if (at_) at_.value = value
|
|
163
|
+
else fields.push({ id: field, label: field, type: 'CONCEALED', value })
|
|
164
|
+
answer(
|
|
165
|
+
await run(
|
|
166
|
+
['item', 'edit', held.id ?? item, '--vault', vault],
|
|
167
|
+
JSON.stringify({ ...held, fields }),
|
|
168
|
+
),
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The `1password` keeper a host lends (@yaks/host/keepers): {@link opKeep}
|
|
173
|
+
* signed in as the service account whose token is `OP_SERVICE_ACCOUNT_TOKEN`
|
|
174
|
+
* in `vault`, else in the environment, read at each keep. */
|
|
175
|
+
export let opKeeper = (vault: Vault) => ({
|
|
176
|
+
keep: async (at: string, value: string): Promise<void> => {
|
|
177
|
+
let token = await reveal(vault, TOKEN)
|
|
178
|
+
if (!token) {
|
|
179
|
+
throw new Error(`${TOKEN} is in neither the vault nor the environment`)
|
|
180
|
+
}
|
|
181
|
+
await opKeep(opRun((name) => name == TOKEN ? token : env.get(name)))(
|
|
182
|
+
at,
|
|
183
|
+
value,
|
|
184
|
+
)
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
let TOKEN = 'OP_SERVICE_ACCOUNT_TOKEN'
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/secrets",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/secrets",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./vocab": {
|
|
12
|
+
"types": "./_dist/vocab.d.ts",
|
|
13
|
+
"default": "./vocab.js"
|
|
14
|
+
},
|
|
15
|
+
"./op": {
|
|
16
|
+
"types": "./_dist/op.d.ts",
|
|
17
|
+
"default": "./op.js"
|
|
18
|
+
},
|
|
19
|
+
"./sealed": {
|
|
20
|
+
"types": "./_dist/sealed.d.ts",
|
|
21
|
+
"default": "./sealed.js"
|
|
22
|
+
},
|
|
23
|
+
"./vault": {
|
|
24
|
+
"types": "./_dist/vault.d.ts",
|
|
25
|
+
"default": "./vault.js"
|
|
26
|
+
},
|
|
27
|
+
"./graph": {
|
|
28
|
+
"types": "./_dist/graph.d.ts",
|
|
29
|
+
"default": "./graph.js"
|
|
30
|
+
},
|
|
31
|
+
"./views": {
|
|
32
|
+
"types": "./_dist/views.d.ts",
|
|
33
|
+
"default": "./views.js"
|
|
34
|
+
},
|
|
35
|
+
"./signed": {
|
|
36
|
+
"types": "./_dist/signed.d.ts",
|
|
37
|
+
"default": "./signed.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"license": "Apache-2.0",
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@yaks/effects": "0.2.36",
|
|
43
|
+
"@yaks/file": "0.2.36",
|
|
44
|
+
"@yaks/fp": "0.2.36",
|
|
45
|
+
"@yaks/graph": "0.2.36",
|
|
46
|
+
"@yaks/process": "0.2.36",
|
|
47
|
+
"@yaks/query": "0.2.36",
|
|
48
|
+
"@yaks/render": "0.2.36",
|
|
49
|
+
"@yaks/vocab": "0.2.36"
|
|
50
|
+
},
|
|
51
|
+
"description": "Secrets that flow through the graph and are kept somewhere else: a write's value is sealed into a vault and the graph keeps a random handle in its place.",
|
|
52
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
53
|
+
}
|