@drsamonline/vault 0.0.0-stage → 0.12.2
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/package.json +23 -4
- package/src/index.js +162 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drsamonline/vault",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.2",
|
|
4
|
+
"description": "Paid-credential vault: AES-256-GCM sealed at rest, opened only when the paid gate is on.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js",
|
|
10
|
+
"./src/*": "./src/*"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git://github.com/drsamonline/sui.git",
|
|
18
|
+
"directory": "packages/vault"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/drsamonline/sui#readme",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @drsamonline/vault — the paid-credential vault (B6.7).
|
|
3
|
+
*
|
|
4
|
+
* The air-gap has two halves. The gate (I4) decides *whether* paid credentials
|
|
5
|
+
* are read at all. This is the other half: *where* they live, sealed with
|
|
6
|
+
* AES-256-GCM through the vendored crypto-kit, so a copied vault file is not a
|
|
7
|
+
* set of keys.
|
|
8
|
+
*
|
|
9
|
+
* Honest limits, stated here rather than in a footnote:
|
|
10
|
+
* - The passphrase is generated on first run and kept in a 0600 file beside the
|
|
11
|
+
* vault. That protects against a leaked or committed vault file. It does not
|
|
12
|
+
* protect against someone who already has your user account.
|
|
13
|
+
* - Bring your own passphrase with SUI_VAULT_PASSPHRASE and the keyfile is
|
|
14
|
+
* never written.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { dirname, join } from 'node:path';
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import { loadKit } from '@drsamonline/kits';
|
|
21
|
+
|
|
22
|
+
const FORMAT = 1;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {{
|
|
26
|
+
* path: string, keyPath: string, passphrase: string|null,
|
|
27
|
+
* exists: () => boolean,
|
|
28
|
+
* init: (opts?: {force?: boolean}) => Promise<{created: boolean, keyPath: string|null}>,
|
|
29
|
+
* read: () => Promise<Record<string, string>>,
|
|
30
|
+
* write: (keys: Record<string, string>) => Promise<void>,
|
|
31
|
+
* set: (provider: string, key: string) => Promise<void>,
|
|
32
|
+
* remove: (provider: string) => Promise<boolean>,
|
|
33
|
+
* list: () => Promise<string[]>,
|
|
34
|
+
* source: 'env' | 'keyfile' | 'none'
|
|
35
|
+
* }} Vault
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {{path: string, keyPath?: string, env?: NodeJS.ProcessEnv}} opts
|
|
40
|
+
* @returns {Vault}
|
|
41
|
+
*/
|
|
42
|
+
export function createVault(opts) {
|
|
43
|
+
const path = opts.path;
|
|
44
|
+
const keyPath = opts.keyPath ?? join(dirname(path), 'vault.key');
|
|
45
|
+
const env = opts.env ?? process.env;
|
|
46
|
+
|
|
47
|
+
const passphrase = env.SUI_VAULT_PASSPHRASE?.trim() ? env.SUI_VAULT_PASSPHRASE.trim() : null;
|
|
48
|
+
const source = passphrase ? 'env' : existsSync(keyPath) ? 'keyfile' : 'none';
|
|
49
|
+
|
|
50
|
+
function currentPassphrase() {
|
|
51
|
+
if (passphrase) return passphrase;
|
|
52
|
+
if (existsSync(keyPath)) return readFileSync(keyPath, 'utf8').trim();
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
path,
|
|
58
|
+
keyPath,
|
|
59
|
+
passphrase,
|
|
60
|
+
source,
|
|
61
|
+
exists: () => existsSync(path),
|
|
62
|
+
|
|
63
|
+
async init(initOpts = {}) {
|
|
64
|
+
if (existsSync(path) && !initOpts.force) {
|
|
65
|
+
return { created: false, keyPath: passphrase ? null : keyPath };
|
|
66
|
+
}
|
|
67
|
+
let writtenKey = null;
|
|
68
|
+
let phrase = currentPassphrase();
|
|
69
|
+
if (!phrase) {
|
|
70
|
+
phrase = randomBytes(32).toString('base64');
|
|
71
|
+
mkdirSync(dirname(keyPath), { recursive: true });
|
|
72
|
+
writeFileSync(keyPath, phrase + '\n', { mode: 0o600 });
|
|
73
|
+
try {
|
|
74
|
+
chmodSync(keyPath, 0o600);
|
|
75
|
+
} catch {
|
|
76
|
+
/* a filesystem without POSIX modes: the file is still outside the repo */
|
|
77
|
+
}
|
|
78
|
+
writtenKey = keyPath;
|
|
79
|
+
}
|
|
80
|
+
await writeWith(phrase, {});
|
|
81
|
+
return { created: true, keyPath: writtenKey };
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async read() {
|
|
85
|
+
if (!existsSync(path)) return {};
|
|
86
|
+
const phrase = currentPassphrase();
|
|
87
|
+
if (!phrase) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`The vault at ${path} exists but there is no passphrase. Set SUI_VAULT_PASSPHRASE, or restore ${keyPath}.`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const CryptoKit = loadKit('crypto-kit');
|
|
93
|
+
const sealed = JSON.parse(readFileSync(path, 'utf8'));
|
|
94
|
+
if (sealed.format !== FORMAT || !sealed.sealed) {
|
|
95
|
+
throw new Error(`The vault at ${path} is not in a format this version understands.`);
|
|
96
|
+
}
|
|
97
|
+
let plaintext;
|
|
98
|
+
try {
|
|
99
|
+
plaintext = await CryptoKit.openString(sealed.sealed, phrase);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Error('The vault could not be opened: the passphrase does not match. Nothing was changed.');
|
|
102
|
+
}
|
|
103
|
+
const parsed = JSON.parse(plaintext);
|
|
104
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async write(keys) {
|
|
108
|
+
const phrase = currentPassphrase();
|
|
109
|
+
if (!phrase) throw new Error('No vault passphrase available. Run `sui vault init` first.');
|
|
110
|
+
await writeWith(phrase, keys);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
async set(provider, key) {
|
|
114
|
+
const keys = await this.read();
|
|
115
|
+
keys[provider] = key;
|
|
116
|
+
await this.write(keys);
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
async remove(provider) {
|
|
120
|
+
const keys = await this.read();
|
|
121
|
+
if (!(provider in keys)) return false;
|
|
122
|
+
delete keys[provider];
|
|
123
|
+
await this.write(keys);
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
async list() {
|
|
128
|
+
return Object.keys(await this.read()).sort();
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
async function writeWith(phrase, keys) {
|
|
133
|
+
const CryptoKit = loadKit('crypto-kit');
|
|
134
|
+
const sealed = await CryptoKit.sealString(JSON.stringify(keys), phrase);
|
|
135
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
136
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
137
|
+
writeFileSync(tmp, JSON.stringify({ format: FORMAT, sealed, updatedAt: new Date().toISOString() }, null, 2) + '\n', {
|
|
138
|
+
mode: 0o600,
|
|
139
|
+
});
|
|
140
|
+
renameSync(tmp, path);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Read the vault for a gateway startup. Returns provider -> key, or an empty map
|
|
146
|
+
* with a reason when the vault cannot be opened. Never throws: a locked vault
|
|
147
|
+
* must not stop the gateway from serving free routes.
|
|
148
|
+
*
|
|
149
|
+
* @param {{path: string, keyPath?: string, env?: NodeJS.ProcessEnv}} opts
|
|
150
|
+
* @returns {Promise<{keys: Record<string, string>, reason: string|null}>}
|
|
151
|
+
*/
|
|
152
|
+
export async function openVaultForStartup(opts) {
|
|
153
|
+
const vault = createVault(opts);
|
|
154
|
+
if (!vault.exists()) return { keys: {}, reason: 'no vault file (paid keys are not configured)' };
|
|
155
|
+
try {
|
|
156
|
+
const keys = await vault.read();
|
|
157
|
+
const count = Object.keys(keys).length;
|
|
158
|
+
return { keys, reason: count ? `${count} provider key(s) opened` : 'vault is empty' };
|
|
159
|
+
} catch (err) {
|
|
160
|
+
return { keys: {}, reason: /** @type {any} */ (err).message };
|
|
161
|
+
}
|
|
162
|
+
}
|
package/README.md
DELETED