oneie 5.6.0 → 5.7.1
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/README.md +29 -0
- package/dist/package.json +2 -2
- package/dist/src/index.js +15 -15
- package/dist/src/init.d.ts +10 -1
- package/dist/src/init.js +26 -11
- package/dist/src/join.d.ts +1 -1
- package/dist/src/join.js +2 -1
- package/dist/src/key-publish.d.ts +40 -0
- package/dist/src/key-publish.js +232 -0
- package/dist/src/key-seal.d.ts +21 -0
- package/dist/src/key-seal.js +142 -0
- package/dist/src/key.js +22 -5
- package/dist/src/lib/age-plugin.d.ts +51 -0
- package/dist/src/lib/age-plugin.js +130 -0
- package/dist/src/lib/env-sealed.d.ts +53 -0
- package/dist/src/lib/env-sealed.js +184 -0
- package/dist/src/lib/invoked-as.js +8 -1
- package/dist/src/lib/journey.d.ts +137 -0
- package/dist/src/lib/journey.js +232 -0
- package/dist/src/lib/key-receipt.d.ts +48 -0
- package/dist/src/lib/key-receipt.js +116 -0
- package/dist/src/lib/lock-lookup.d.ts +40 -0
- package/dist/src/lib/lock-lookup.js +92 -0
- package/dist/src/lib/message-seal.d.ts +63 -0
- package/dist/src/lib/message-seal.js +150 -0
- package/dist/src/lib/vault-read.d.ts +24 -0
- package/dist/src/lib/vault-read.js +77 -0
- package/dist/src/media.d.ts +15 -0
- package/dist/src/media.js +126 -4
- package/dist/src/secrets-device.d.ts +32 -0
- package/dist/src/secrets-device.js +127 -0
- package/dist/src/secrets-give.d.ts +125 -0
- package/dist/src/secrets-give.js +236 -0
- package/dist/src/secrets-run.d.ts +31 -0
- package/dist/src/secrets-run.js +182 -0
- package/dist/src/secrets.d.ts +32 -0
- package/dist/src/secrets.js +197 -0
- package/dist/src/start.d.ts +15 -0
- package/dist/src/start.js +204 -0
- package/dist/src/wallet.d.ts +11 -0
- package/dist/src/wallet.js +14 -6
- package/dist/src/write.d.ts +43 -0
- package/dist/src/write.js +174 -0
- package/package.json +2 -2
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-device.ts — `one secrets add-device`: make this Mac a reader of your sealed files
|
|
3
|
+
* (text/secrets.md deliverable 3; text/secrets-todo.md § C3).
|
|
4
|
+
*
|
|
5
|
+
* Runs `age-plugin-se keygen --access-control current-biometry -o <identity>`: a key born
|
|
6
|
+
* in the Mac's Secure Enclave that never leaves the chip. The identity file is only a
|
|
7
|
+
* handle to that key (the chip holds the secret), written mode 600 and never overwritten
|
|
8
|
+
* without --replace. The `age1se1…` lock is then appended to ~/.one/secrets-readers.json,
|
|
9
|
+
* where C1's `seal` finds it.
|
|
10
|
+
*
|
|
11
|
+
* The access control is NOT a flag. Real use is always `current-biometry`; the test seam
|
|
12
|
+
* below is a module value the command line cannot reach.
|
|
13
|
+
*
|
|
14
|
+
* Nothing the identity holds is read or printed here: the lock comes from
|
|
15
|
+
* `age-plugin-se recipients -i`, and age-plugin-se's own output is discarded.
|
|
16
|
+
*/
|
|
17
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { homedir, hostname } from 'node:os';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
import { jsonMode } from './lib/client.js';
|
|
21
|
+
import { keygen, PLUGIN_LOCK_RE, recipientOf, requireAge } from './lib/age-plugin.js';
|
|
22
|
+
import { fail, readersFile } from './secrets.js';
|
|
23
|
+
/** The Secure Enclave identity file. ONE_SECRETS_DEVICE moves it (tests). */
|
|
24
|
+
export function deviceIdentityFile() {
|
|
25
|
+
return process.env.ONE_SECRETS_DEVICE?.trim() || join(homedir(), '.one', 'secrets-device.txt');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Test seam. Only a test module can set it; there is no flag and no env var for it, so a
|
|
29
|
+
* person at a terminal always gets the finger rule. A key made with `none` opens without Touch ID.
|
|
30
|
+
*/
|
|
31
|
+
export const deviceSeam = { accessControl: 'current-biometry' };
|
|
32
|
+
const badReaders = (path) => ({ ok: false, error: `${path} is not a list of age1… locks`, code: 'bad_readers_file' });
|
|
33
|
+
function lockOf(e) {
|
|
34
|
+
const l = typeof e === 'string' ? e : e?.lock;
|
|
35
|
+
return typeof l === 'string' ? l.trim() : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** Both file shapes C1 reads: a JSON array, or `{ "readers": [...] }`. No file is a new list. */
|
|
38
|
+
function readReaders(path) {
|
|
39
|
+
if (!existsSync(path))
|
|
40
|
+
return { shape: 'new', items: [] };
|
|
41
|
+
let parsed;
|
|
42
|
+
try {
|
|
43
|
+
parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return badReaders(path);
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(parsed))
|
|
49
|
+
return { shape: 'list', items: parsed };
|
|
50
|
+
const list = parsed?.readers;
|
|
51
|
+
if (Array.isArray(list))
|
|
52
|
+
return { shape: 'object', items: list };
|
|
53
|
+
return badReaders(path);
|
|
54
|
+
}
|
|
55
|
+
function writeReaders(path, doc, original) {
|
|
56
|
+
const body = doc.shape === 'object' ? { ...original, readers: doc.items } : doc.items;
|
|
57
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
58
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
59
|
+
writeFileSync(tmp, JSON.stringify(body, null, 2) + '\n', { mode: 0o600 });
|
|
60
|
+
renameSync(tmp, path);
|
|
61
|
+
}
|
|
62
|
+
export async function addDeviceAction(o, c) {
|
|
63
|
+
const identity = deviceIdentityFile();
|
|
64
|
+
const readersPath = readersFile();
|
|
65
|
+
const bins = requireAge();
|
|
66
|
+
if (typeof bins === 'string') {
|
|
67
|
+
return fail(c, { ok: false, error: bins === 'no_plugin' ? 'the `age-plugin-se` program is not installed — brew install age-plugin-se' : 'the `age` program is not installed — brew install age', code: bins });
|
|
68
|
+
}
|
|
69
|
+
const doc = readReaders(readersPath);
|
|
70
|
+
if ('ok' in doc)
|
|
71
|
+
return fail(c, doc);
|
|
72
|
+
let oldLock = null;
|
|
73
|
+
try {
|
|
74
|
+
lstatSync(identity);
|
|
75
|
+
if (!o.replace)
|
|
76
|
+
return fail(c, { ok: false, error: `${identity} already exists — this Mac is already a reader (pass --replace to make a new key)`, code: 'device_exists' });
|
|
77
|
+
oldLock = recipientOf(identity);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e.code !== 'ENOENT')
|
|
81
|
+
return fail(c, { ok: false, error: `cannot check ${identity}`, code: 'failed' });
|
|
82
|
+
}
|
|
83
|
+
const tmp = `${identity}.tmp-${process.pid}`;
|
|
84
|
+
try {
|
|
85
|
+
mkdirSync(dirname(identity), { recursive: true, mode: 0o700 });
|
|
86
|
+
const made = keygen(tmp, deviceSeam.accessControl);
|
|
87
|
+
if (made !== true) {
|
|
88
|
+
// No Secure Enclave (or no biometry) on this machine: a fixed message, exit 3.
|
|
89
|
+
fail(c, { ok: false, error: 'this Mac could not make a Secure Enclave key — it has no Secure Enclave, or no Touch ID set up', code: 'no_secure_enclave' });
|
|
90
|
+
process.exitCode = 3;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
chmodSync(tmp, 0o600);
|
|
94
|
+
if ((statSync(tmp).mode & 0o777) !== 0o600)
|
|
95
|
+
return fail(c, { ok: false, error: `${identity} could not be made private (mode 600)`, code: 'bad_mode' });
|
|
96
|
+
const lock = recipientOf(tmp);
|
|
97
|
+
if (!lock || !PLUGIN_LOCK_RE.test(lock))
|
|
98
|
+
return fail(c, { ok: false, error: 'could not read the lock of the new key', code: 'failed' });
|
|
99
|
+
renameSync(tmp, identity);
|
|
100
|
+
// Re-reading after the keygen: the file may have changed under a long Touch ID wait.
|
|
101
|
+
const fresh = readReaders(readersPath);
|
|
102
|
+
if ('ok' in fresh)
|
|
103
|
+
return fail(c, fresh);
|
|
104
|
+
const original = existsSync(readersPath) ? JSON.parse(readFileSync(readersPath, 'utf8')) : undefined;
|
|
105
|
+
const name = o.name?.trim() || hostname();
|
|
106
|
+
const items = fresh.items.filter((e) => {
|
|
107
|
+
const l = lockOf(e);
|
|
108
|
+
return l !== lock && !(oldLock && l === oldLock);
|
|
109
|
+
});
|
|
110
|
+
items.push({ lock, name, added: new Date().toISOString() });
|
|
111
|
+
writeReaders(readersPath, { shape: fresh.shape, items }, original);
|
|
112
|
+
if (jsonMode(c))
|
|
113
|
+
process.stdout.write(JSON.stringify({ ok: true, lock, name, identity, replaced: Boolean(oldLock) }) + '\n');
|
|
114
|
+
else {
|
|
115
|
+
process.stdout.write(`device ${lock}\n`);
|
|
116
|
+
process.stdout.write(`name ${name}\nidentity ${identity} (a handle — the key stays in the chip)\n`);
|
|
117
|
+
process.stdout.write('re-seal your .env to add this Mac as a reader\n');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
fail(c, { ok: false, error: `could not add this Mac: ${e.code ?? 'failed'}`, code: 'failed' });
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
if (existsSync(tmp))
|
|
125
|
+
unlinkSync(tmp);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-give.ts — `one secrets give NAME`: give a key to ONE from the terminal
|
|
3
|
+
* (text/secrets.md deliverable 6; text/secrets-todo.md § C8).
|
|
4
|
+
*
|
|
5
|
+
* In order, all on this machine until the last step:
|
|
6
|
+
*
|
|
7
|
+
* 1 ONE's lock, read from ONE's vault (the single entry of its `readers`, plan D5) through
|
|
8
|
+
* `@oneie/sdk/secrets-give` `readOneLock`. No answer is `no_one_lock`; a `--lock` that
|
|
9
|
+
* differs is `lock_mismatch`. There is no constant to fall back to, ever.
|
|
10
|
+
* 2 the value — from `.env.sealed` (opened in memory with your paper key, the C2 way) or
|
|
11
|
+
* from piped stdin with `--stdin`. NEVER from argv: a word after NAME refuses exit 2,
|
|
12
|
+
* unread, so a key never sits in shell history or `ps`
|
|
13
|
+
* 3 sealed to ONE's lock alone (`sealForOne`, which reads ONE's vault itself and seals only to
|
|
14
|
+
* what it names — a `--lock` that differs is `lock_mismatch` there too), over the canonical
|
|
15
|
+
* plaintext naming this vault, NAME and --provider, so the blob is no other vault's, name's or
|
|
16
|
+
* provider's key; the value dropped
|
|
17
|
+
* 4 signed by an AGENT's own ed25519 key (`--signer-key <file>`, `giveData`) over
|
|
18
|
+
* `canonicalSigned(payload)`. Your master key never signs a give: `--key` opens the
|
|
19
|
+
* sealed file and does nothing else, and `--signer person` exits 2 — a person's touch is
|
|
20
|
+
* on the seal page (C9)
|
|
21
|
+
* 5 `secrets:give` through ask(), under your API key: the web side adds who you are and
|
|
22
|
+
* forwards to the opener. The answer names the last four characters and nothing more.
|
|
23
|
+
*
|
|
24
|
+
* The body names who gives: `--as <actor>` — the actor your API key is, which the web side attests;
|
|
25
|
+
* the opener refuses `subject_mismatch` for any other, so a captured give resent under another
|
|
26
|
+
* session is never theirs.
|
|
27
|
+
*
|
|
28
|
+
* ONE's lock has one source: the reader of ONE's vault. Until C14 publishes it, the shipped reader
|
|
29
|
+
* answers no lock and this command refuses `no_one_lock` — there is no environment or file
|
|
30
|
+
* override (a test injects a reader through `giveLoaders.oneVault`, in code).
|
|
31
|
+
*
|
|
32
|
+
* A rotate names what it replaces: `--replaces <sigHash>` (the `sigHash` of the last give, or the
|
|
33
|
+
* hash a `stale` refusal prints), signed into the body; a first give sends null. A captured give
|
|
34
|
+
* re-sent after a rotate is therefore stale at the opener, never a rollback.
|
|
35
|
+
*
|
|
36
|
+
* Exits 0 given (or checked) · 1 refused · 2 usage. Prints no value, no line of the file, no key.
|
|
37
|
+
*/
|
|
38
|
+
import { Command } from 'commander';
|
|
39
|
+
/** The part of `@oneie/sdk/secrets-give` this command uses, typed here so a typecheck does not need that SDK built. */
|
|
40
|
+
export interface OneVaultReader {
|
|
41
|
+
(): Promise<{
|
|
42
|
+
readers: string[];
|
|
43
|
+
sealedHash: string | null;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
export interface GiveSdk {
|
|
47
|
+
readOneLock(reader: OneVaultReader): Promise<{
|
|
48
|
+
ok: true;
|
|
49
|
+
lock: string;
|
|
50
|
+
} | {
|
|
51
|
+
ok: false;
|
|
52
|
+
error: 'no_one_lock';
|
|
53
|
+
detail: string;
|
|
54
|
+
}>;
|
|
55
|
+
sealForOne(value: string, bind: {
|
|
56
|
+
vault: string;
|
|
57
|
+
name: string;
|
|
58
|
+
provider: string;
|
|
59
|
+
}, reader: OneVaultReader, lock?: string): Promise<{
|
|
60
|
+
ok: true;
|
|
61
|
+
sealed: string;
|
|
62
|
+
} | {
|
|
63
|
+
ok: false;
|
|
64
|
+
error: 'no_one_lock' | 'lock_mismatch';
|
|
65
|
+
detail: string;
|
|
66
|
+
}>;
|
|
67
|
+
giveData(input: {
|
|
68
|
+
vault: string;
|
|
69
|
+
name: string;
|
|
70
|
+
provider: string;
|
|
71
|
+
sealed: string;
|
|
72
|
+
mode?: 'give' | 'check';
|
|
73
|
+
allowFlagged?: boolean;
|
|
74
|
+
replaces?: string | null;
|
|
75
|
+
subject: string;
|
|
76
|
+
}, secret: Uint8Array): {
|
|
77
|
+
signer: {
|
|
78
|
+
kind: 'agent';
|
|
79
|
+
sig: string;
|
|
80
|
+
signer: string;
|
|
81
|
+
};
|
|
82
|
+
payload: {
|
|
83
|
+
type: 'give';
|
|
84
|
+
vault: string;
|
|
85
|
+
body: Record<string, unknown>;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
decodeSignerKey(text: string): Uint8Array | null;
|
|
89
|
+
fixtureReader(f?: {
|
|
90
|
+
oneLock: string | null;
|
|
91
|
+
sealPageHash: string | null;
|
|
92
|
+
}): OneVaultReader;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Swappable in tests (the message-seal.ts `loaders` shape): the cli resolves `@oneie/sdk` through
|
|
96
|
+
* its own node_modules, which can be a published or main-tree SDK older than `./secrets-give`;
|
|
97
|
+
* a test serves the source. `oneVault` is the reader of ONE's vault and the ONLY source of ONE's
|
|
98
|
+
* lock — the IC#7 fixture until C14 reaches testnet, which answers no lock, so a give refuses
|
|
99
|
+
* `no_one_lock`. Nothing in the environment or on disk can name another lock (C8 repair 3); a
|
|
100
|
+
* test injects a reader by assigning this field. `fetch` is the one network call.
|
|
101
|
+
*/
|
|
102
|
+
export declare const giveLoaders: {
|
|
103
|
+
sdk: () => Promise<GiveSdk>;
|
|
104
|
+
oneVault: (sdk: GiveSdk) => Promise<OneVaultReader>;
|
|
105
|
+
fetch: (input: string, init: RequestInit) => Promise<Response>;
|
|
106
|
+
};
|
|
107
|
+
interface GiveOpts {
|
|
108
|
+
provider?: string;
|
|
109
|
+
vault?: string;
|
|
110
|
+
file: string;
|
|
111
|
+
stdin?: boolean;
|
|
112
|
+
key?: string;
|
|
113
|
+
signer: string;
|
|
114
|
+
signerKey?: string;
|
|
115
|
+
lock?: string;
|
|
116
|
+
check?: boolean;
|
|
117
|
+
allowFlagged?: boolean;
|
|
118
|
+
replaces?: string;
|
|
119
|
+
as?: string;
|
|
120
|
+
api?: string;
|
|
121
|
+
}
|
|
122
|
+
export declare function giveAction(name: string, extra: string[], o: GiveOpts, c: Command): Promise<void>;
|
|
123
|
+
/** The `give` subcommand, fresh per call (a Command has one parent). */
|
|
124
|
+
export declare function buildGive(): Command;
|
|
125
|
+
export {};
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-give.ts — `one secrets give NAME`: give a key to ONE from the terminal
|
|
3
|
+
* (text/secrets.md deliverable 6; text/secrets-todo.md § C8).
|
|
4
|
+
*
|
|
5
|
+
* In order, all on this machine until the last step:
|
|
6
|
+
*
|
|
7
|
+
* 1 ONE's lock, read from ONE's vault (the single entry of its `readers`, plan D5) through
|
|
8
|
+
* `@oneie/sdk/secrets-give` `readOneLock`. No answer is `no_one_lock`; a `--lock` that
|
|
9
|
+
* differs is `lock_mismatch`. There is no constant to fall back to, ever.
|
|
10
|
+
* 2 the value — from `.env.sealed` (opened in memory with your paper key, the C2 way) or
|
|
11
|
+
* from piped stdin with `--stdin`. NEVER from argv: a word after NAME refuses exit 2,
|
|
12
|
+
* unread, so a key never sits in shell history or `ps`
|
|
13
|
+
* 3 sealed to ONE's lock alone (`sealForOne`, which reads ONE's vault itself and seals only to
|
|
14
|
+
* what it names — a `--lock` that differs is `lock_mismatch` there too), over the canonical
|
|
15
|
+
* plaintext naming this vault, NAME and --provider, so the blob is no other vault's, name's or
|
|
16
|
+
* provider's key; the value dropped
|
|
17
|
+
* 4 signed by an AGENT's own ed25519 key (`--signer-key <file>`, `giveData`) over
|
|
18
|
+
* `canonicalSigned(payload)`. Your master key never signs a give: `--key` opens the
|
|
19
|
+
* sealed file and does nothing else, and `--signer person` exits 2 — a person's touch is
|
|
20
|
+
* on the seal page (C9)
|
|
21
|
+
* 5 `secrets:give` through ask(), under your API key: the web side adds who you are and
|
|
22
|
+
* forwards to the opener. The answer names the last four characters and nothing more.
|
|
23
|
+
*
|
|
24
|
+
* The body names who gives: `--as <actor>` — the actor your API key is, which the web side attests;
|
|
25
|
+
* the opener refuses `subject_mismatch` for any other, so a captured give resent under another
|
|
26
|
+
* session is never theirs.
|
|
27
|
+
*
|
|
28
|
+
* ONE's lock has one source: the reader of ONE's vault. Until C14 publishes it, the shipped reader
|
|
29
|
+
* answers no lock and this command refuses `no_one_lock` — there is no environment or file
|
|
30
|
+
* override (a test injects a reader through `giveLoaders.oneVault`, in code).
|
|
31
|
+
*
|
|
32
|
+
* A rotate names what it replaces: `--replaces <sigHash>` (the `sigHash` of the last give, or the
|
|
33
|
+
* hash a `stale` refusal prints), signed into the body; a first give sends null. A captured give
|
|
34
|
+
* re-sent after a rotate is therefore stale at the opener, never a rollback.
|
|
35
|
+
*
|
|
36
|
+
* Exits 0 given (or checked) · 1 refused · 2 usage. Prints no value, no line of the file, no key.
|
|
37
|
+
*/
|
|
38
|
+
import { readFileSync } from 'node:fs';
|
|
39
|
+
import { resolve } from 'node:path';
|
|
40
|
+
import { Command } from 'commander';
|
|
41
|
+
import { askPath } from './lib/api-path.js';
|
|
42
|
+
import { authHeaders, jsonMode, resolveBase } from './lib/client.js';
|
|
43
|
+
import { parseSealed } from './lib/env-sealed.js';
|
|
44
|
+
import { ageModule, io, loadSecretsKey, MAX_INPUT_BYTES } from './lib/message-seal.js';
|
|
45
|
+
const GIVE_SPEC = '@oneie/sdk/secrets-give'; // a variable, so a bundler/vite does not resolve it at build time
|
|
46
|
+
/**
|
|
47
|
+
* Swappable in tests (the message-seal.ts `loaders` shape): the cli resolves `@oneie/sdk` through
|
|
48
|
+
* its own node_modules, which can be a published or main-tree SDK older than `./secrets-give`;
|
|
49
|
+
* a test serves the source. `oneVault` is the reader of ONE's vault and the ONLY source of ONE's
|
|
50
|
+
* lock — the IC#7 fixture until C14 reaches testnet, which answers no lock, so a give refuses
|
|
51
|
+
* `no_one_lock`. Nothing in the environment or on disk can name another lock (C8 repair 3); a
|
|
52
|
+
* test injects a reader by assigning this field. `fetch` is the one network call.
|
|
53
|
+
*/
|
|
54
|
+
export const giveLoaders = {
|
|
55
|
+
sdk: () => import(/* @vite-ignore */ GIVE_SPEC),
|
|
56
|
+
oneVault: async (sdk) => sdk.fixtureReader(),
|
|
57
|
+
fetch: (input, init) => fetch(input, init),
|
|
58
|
+
};
|
|
59
|
+
const NAME_RE = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/;
|
|
60
|
+
const VAULT_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
61
|
+
const HASH_RE = /^[0-9a-f]{64}$/;
|
|
62
|
+
const MAX_VALUE = 64 * 1024;
|
|
63
|
+
function fail(cmd, f, exit = 1) {
|
|
64
|
+
if (jsonMode(cmd))
|
|
65
|
+
process.stdout.write(JSON.stringify(f) + '\n');
|
|
66
|
+
else
|
|
67
|
+
process.stderr.write(`error ${f.error}\n`);
|
|
68
|
+
process.exitCode = exit;
|
|
69
|
+
}
|
|
70
|
+
export async function giveAction(name, extra, o, c) {
|
|
71
|
+
// the value is never an argument — refused before anything is read, and the word is never echoed
|
|
72
|
+
if (extra.length > 0) {
|
|
73
|
+
return fail(c, { ok: false, error: 'a key is never an argument (it would sit in your shell history) — pipe it with --stdin, or keep it in .env.sealed', code: 'value_in_argv' }, 2);
|
|
74
|
+
}
|
|
75
|
+
if (o.signer === 'person')
|
|
76
|
+
return fail(c, { ok: false, error: 'the touch is on the seal page (C9)', code: 'person_signs_on_seal_page' }, 2);
|
|
77
|
+
if (o.signer !== 'agent')
|
|
78
|
+
return fail(c, { ok: false, error: '--signer is agent (your agent’s own key) — a person signs on the seal page (C9)', code: 'usage' }, 2);
|
|
79
|
+
if (!NAME_RE.test(name))
|
|
80
|
+
return fail(c, { ok: false, error: 'NAME is a .env name: letters, digits and _', code: 'usage' }, 2);
|
|
81
|
+
if (!o.provider)
|
|
82
|
+
return fail(c, { ok: false, error: '--provider is required (openai · anthropic · stripe · github · dataforseo)', code: 'usage' }, 2);
|
|
83
|
+
if (!o.as || o.as.trim().length === 0 || o.as.length > 256) {
|
|
84
|
+
return fail(c, { ok: false, error: 'name who gives: --as <actor> — the actor your API key is (ONE refuses any other as subject_mismatch)', code: 'usage' }, 2);
|
|
85
|
+
}
|
|
86
|
+
if (o.replaces !== undefined && !HASH_RE.test(o.replaces))
|
|
87
|
+
return fail(c, { ok: false, error: '--replaces is the sigHash of the record ONE holds: 64 lower-case hex', code: 'usage' }, 2);
|
|
88
|
+
// only an agent's own ed25519 key signs; the master (--key / ONE_WALLET_MNEMONIC) opens the file and nothing else
|
|
89
|
+
if (!o.signerKey) {
|
|
90
|
+
return fail(c, { ok: false, error: 'only an agent’s own ed25519 key signs a give (--signer-key <file>); your master key never does', code: 'no_signer' });
|
|
91
|
+
}
|
|
92
|
+
const sdk = await giveLoaders.sdk();
|
|
93
|
+
let signerText;
|
|
94
|
+
try {
|
|
95
|
+
signerText = readFileSync(resolve(o.signerKey), 'utf8');
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return fail(c, { ok: false, error: `cannot read ${o.signerKey}`, code: 'bad_signer_key' });
|
|
99
|
+
}
|
|
100
|
+
const secret = sdk.decodeSignerKey(signerText);
|
|
101
|
+
signerText = '';
|
|
102
|
+
if (!secret)
|
|
103
|
+
return fail(c, { ok: false, error: `${o.signerKey} is not an ed25519 key (suiprivkey1… or 64 hex) — a mnemonic or a key file never signs here`, code: 'bad_signer_key' });
|
|
104
|
+
try {
|
|
105
|
+
// 1 — ONE's lock, from ONE's vault; never a constant, never the flag alone (checked here before
|
|
106
|
+
// anything is opened, and again by sealForOne, which seals to nothing else)
|
|
107
|
+
const reader = await giveLoaders.oneVault(sdk);
|
|
108
|
+
const one = await sdk.readOneLock(reader);
|
|
109
|
+
if (!one.ok)
|
|
110
|
+
return fail(c, { ok: false, error: `${one.detail} — nothing was sealed`, code: 'no_one_lock' });
|
|
111
|
+
if (o.lock !== undefined && o.lock.trim() !== one.lock) {
|
|
112
|
+
return fail(c, { ok: false, error: 'that is not the lock ONE’s vault names — nothing was sealed', code: 'lock_mismatch' });
|
|
113
|
+
}
|
|
114
|
+
// the vault: --vault, else the sealed file's header
|
|
115
|
+
let vault = o.vault;
|
|
116
|
+
let sealedText = null;
|
|
117
|
+
if (!o.stdin || !vault) {
|
|
118
|
+
try {
|
|
119
|
+
const b = readFileSync(resolve(o.file));
|
|
120
|
+
if (b.length > MAX_INPUT_BYTES)
|
|
121
|
+
return fail(c, { ok: false, error: `${o.file} is too big`, code: 'too_big' });
|
|
122
|
+
sealedText = b.toString('utf8');
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
if (!o.stdin)
|
|
126
|
+
return fail(c, { ok: false, error: `cannot read ${o.file}`, code: 'no_input' });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const parsed = sealedText !== null ? parseSealed(sealedText) : null;
|
|
130
|
+
if (parsed && 'ok' in parsed && !o.stdin)
|
|
131
|
+
return fail(c, parsed);
|
|
132
|
+
if (!vault && parsed && !('ok' in parsed))
|
|
133
|
+
vault = parsed.vault;
|
|
134
|
+
if (!vault || !VAULT_RE.test(vault))
|
|
135
|
+
return fail(c, { ok: false, error: 'name your vault: --vault 0x… (64 hex), or a # vault line in the sealed file', code: 'bad_vault' });
|
|
136
|
+
vault = vault.toLowerCase();
|
|
137
|
+
// 2 — the value, in memory only
|
|
138
|
+
let value;
|
|
139
|
+
if (o.stdin) {
|
|
140
|
+
const bytes = await io.readStdin();
|
|
141
|
+
if (bytes.length > MAX_VALUE)
|
|
142
|
+
return fail(c, { ok: false, error: 'stdin is too long to be a key', code: 'too_big' });
|
|
143
|
+
value = new TextDecoder().decode(bytes).replace(/\r?\n$/, '');
|
|
144
|
+
bytes.fill(0);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
if (!parsed || 'ok' in parsed)
|
|
148
|
+
return fail(c, { ok: false, error: `cannot read ${o.file}`, code: 'no_input' });
|
|
149
|
+
const entry = parsed.entries.find((e) => e.name === name);
|
|
150
|
+
if (!entry)
|
|
151
|
+
return fail(c, { ok: false, error: `${name} is not in ${o.file}`, code: 'no_such_name' });
|
|
152
|
+
const k = await loadSecretsKey(o.key);
|
|
153
|
+
if ('ok' in k)
|
|
154
|
+
return fail(c, k);
|
|
155
|
+
try {
|
|
156
|
+
const age = await ageModule();
|
|
157
|
+
let plain;
|
|
158
|
+
try {
|
|
159
|
+
plain = age.decrypt(entry.file, [k.secret]);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return fail(c, { ok: false, error: `${name} does not open with this key`, code: 'no_identity' });
|
|
163
|
+
}
|
|
164
|
+
value = new TextDecoder().decode(plain);
|
|
165
|
+
plain.fill(0);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
k.secret.fill(0);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (value.length === 0)
|
|
172
|
+
return fail(c, { ok: false, error: 'the value is empty', code: 'bad_key' });
|
|
173
|
+
// 3 + 4 — bound to this vault and NAME, sealed to the lock ONE's vault names alone, then signed by the agent's key
|
|
174
|
+
const s = await sdk.sealForOne(value, { vault, name, provider: o.provider }, reader, o.lock?.trim() ?? one.lock);
|
|
175
|
+
value = '';
|
|
176
|
+
if (!s.ok)
|
|
177
|
+
return fail(c, { ok: false, error: `${s.detail} — nothing was sealed`, code: s.error });
|
|
178
|
+
const data = sdk.giveData({ vault, name, provider: o.provider, sealed: s.sealed, mode: o.check ? 'check' : 'give', allowFlagged: o.allowFlagged === true, replaces: o.replaces ?? null, subject: o.as.trim() }, secret);
|
|
179
|
+
// 5 — secrets:give through ask(); the web side adds who you are
|
|
180
|
+
const base = resolveBase({ api: o.api });
|
|
181
|
+
let res;
|
|
182
|
+
try {
|
|
183
|
+
res = await giveLoaders.fetch(askPath(base, 'secrets:give'), { method: 'POST', headers: authHeaders(), body: JSON.stringify({ data }) });
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return fail(c, { ok: false, error: `could not reach ${base}`, code: 'unreachable' });
|
|
187
|
+
}
|
|
188
|
+
let json = {};
|
|
189
|
+
try {
|
|
190
|
+
json = (await res.json());
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
/* the status speaks */
|
|
194
|
+
}
|
|
195
|
+
const r = json['result'] ?? json;
|
|
196
|
+
if (!res.ok || r['ok'] !== true) {
|
|
197
|
+
const code = String(r['refused'] ?? r['error'] ?? `http_${res.status}`);
|
|
198
|
+
const detail = typeof r['detail'] === 'string' ? ` (${r['detail']})` : '';
|
|
199
|
+
// stale: ONE holds another record for this name — its sigHash is what a rotate signs as --replaces
|
|
200
|
+
const hint = code === 'stale' && typeof r['detail'] === 'string' && HASH_RE.test(r['detail']) ? ` — to replace it, run again with --replaces ${r['detail']}` : '';
|
|
201
|
+
return fail(c, { ok: false, error: `ONE did not take ${name}: ${code}${detail}${hint}`, code });
|
|
202
|
+
}
|
|
203
|
+
const given = (r['given'] ?? {});
|
|
204
|
+
const stored = r['stored'] === true;
|
|
205
|
+
if (jsonMode(c))
|
|
206
|
+
process.stdout.write(JSON.stringify({ ok: true, stored, given }) + '\n');
|
|
207
|
+
else {
|
|
208
|
+
const tail = given.last4 ? `…${given.last4}` : '';
|
|
209
|
+
process.stdout.write(`${stored ? 'given' : 'checked'} ${name} ${tail} ${given.provider ?? o.provider} check ${given.check?.status ?? '?'}${given.flagged ? ` flagged: ${given.flagged}` : ''}${stored && given.sigHash ? ` sigHash ${given.sigHash}` : ''}\n`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
finally {
|
|
213
|
+
secret.fill(0);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/** The `give` subcommand, fresh per call (a Command has one parent). */
|
|
217
|
+
export function buildGive() {
|
|
218
|
+
return new Command('give')
|
|
219
|
+
.description('Give a key to ONE — sealed here to the lock ONE’s vault names, signed by your agent’s key; afterwards only its last four characters show')
|
|
220
|
+
.argument('<name>', 'The .env name of the key, e.g. OPENAI_API_KEY')
|
|
221
|
+
.argument('[extra...]')
|
|
222
|
+
.requiredOption('--provider <name>', 'openai · anthropic · stripe · github · dataforseo')
|
|
223
|
+
.option('--vault <id>', 'Your vault (0x…); default: the # vault line of the sealed file')
|
|
224
|
+
.option('--file <file>', 'The sealed file the value is read from', '.env.sealed')
|
|
225
|
+
.option('--stdin', 'Read the value from piped stdin instead of the sealed file')
|
|
226
|
+
.option('--key <file>', 'Key file that opens the sealed file (else ONE_WALLET_MNEMONIC). It never signs')
|
|
227
|
+
.option('--signer <kind>', 'agent (the only signer here); a person signs on the seal page (C9)', 'agent')
|
|
228
|
+
.option('--signer-key <file>', 'Your agent’s own ed25519 key: suiprivkey1… or 64 hex')
|
|
229
|
+
.option('--lock <age1…>', 'The lock you expect ONE to have; refused unless ONE’s vault names the same')
|
|
230
|
+
.option('--check', 'Check the key works and what it can do; store nothing')
|
|
231
|
+
.option('--allow-flagged', 'Store a key Check it flags: one that can make other keys (an admin key, a full Stripe secret key, a GitHub admin: or key-making scope) or a GitHub token whose scopes cannot be read (scopes_unknown)')
|
|
232
|
+
.option('--as <actor>', 'Who gives: the actor your API key is — signed into the body and checked against the caller ONE attests')
|
|
233
|
+
.option('--replaces <sigHash>', 'Rotate: the sigHash of the record ONE holds for NAME (printed by the last give, or by a stale refusal)')
|
|
234
|
+
.option('--api <url>', 'API base (default: ONE_API_URL or https://one.ie)')
|
|
235
|
+
.action((name, extra, o, c) => giveAction(name, extra ?? [], o, c));
|
|
236
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-run.ts — `one run -- <cmd…>` and `one secrets run -- <cmd…>`; `one secrets hash`.
|
|
3
|
+
* (text/secrets.md deliverable 2; text/secrets-todo.md § C2.)
|
|
4
|
+
*
|
|
5
|
+
* Opens `.env.sealed` once, hands the values to ONE child process through its environment,
|
|
6
|
+
* and writes nothing anywhere. The order is the point:
|
|
7
|
+
*
|
|
8
|
+
* 1. read the file's exact bytes, parse the header (no key yet)
|
|
9
|
+
* 2. sha256 of those bytes must equal the `sealed_hash` of the vault named by `# vault`
|
|
10
|
+
* — a file the vault does not know opens NOTHING (the key is not even loaded)
|
|
11
|
+
* 3. open every value with the owner's paper identity, in memory
|
|
12
|
+
* 4. spawn the child with `{ ...process.env, ...values }`, stdio inherited
|
|
13
|
+
* 5. zero the decrypted buffers; exit with the child's exit code
|
|
14
|
+
*
|
|
15
|
+
* There is no flag that skips step 2. `process.env` of this process is never changed, no
|
|
16
|
+
* value is printed, and an error names a variable or a line number, never a value or the key.
|
|
17
|
+
*/
|
|
18
|
+
import { Command } from 'commander';
|
|
19
|
+
interface RunOpts {
|
|
20
|
+
file: string;
|
|
21
|
+
key?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function runAction(cmdArgs: string[], o: RunOpts, c: Command): Promise<void>;
|
|
24
|
+
/** The `run` subcommand, fresh per call (a Command has one parent). */
|
|
25
|
+
export declare function buildRun(): Command;
|
|
26
|
+
/** The top-level `one run`. */
|
|
27
|
+
export declare const runCmd: () => Command;
|
|
28
|
+
/** `one secrets hash [file]` — the sha256 an owner sets on their vault. Names no value, needs no key. */
|
|
29
|
+
export declare function hashAction(file: string, _o: unknown, c: Command): Promise<void>;
|
|
30
|
+
export declare function buildHash(): Command;
|
|
31
|
+
export {};
|