omp-config-crypt 0.1.0 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +23 -6
  2. package/index.js +165 -10
  3. package/package.json +20 -5
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # omp-config-crypt
2
2
 
3
- Encrypted export/import of omp user config and instruction files. Secrets are
4
- included as-is; the archive itself is the only protection.
3
+ Encrypted export/import of omp user config and instruction files, locally or
4
+ through a cloud token. Secrets are included as-is; the archive itself is the
5
+ only protection.
5
6
 
6
7
  ## Archive
7
8
 
@@ -12,13 +13,31 @@ included as-is; the archive itself is the only protection.
12
13
  Included: `config.yml`, `models.yml`, `mcp.json`, `AGENTS.md`, `STYLE.md`,
13
14
  `RULES.md`, and everything under `memories/`.
14
15
 
15
- ## Commands
16
+ ## Local commands
16
17
 
17
18
  ```
18
19
  /config-export [out.enc]
19
20
  /config-import <file.enc>
20
21
  ```
21
22
 
23
+ ## Cloud commands
24
+
25
+ ```
26
+ /config-push gist:new # create a secret gist
27
+ /config-push gist:<id> # update it
28
+ /config-push https://… # PUT to any endpoint
29
+ /config-pull gist:<id>
30
+ /config-pull https://…
31
+ ```
32
+
33
+ Token: asked by the command when `OMP_CFG_CLOUD_TOKEN` (or `GITHUB_TOKEN`) is
34
+ unset, and never written into the archive or to disk. For plain HTTP targets the
35
+ token is optional and an empty answer skips it, so presigned S3/R2 URLs work as-is.
36
+
37
+ GitHub needs a token with the `gist` scope. Plain HTTP targets receive
38
+ `Authorization: Bearer <token>` when the variable is set, which covers WebDAV
39
+ behind a bearer proxy and most object-storage gateways.
40
+
22
41
  Passphrase: `OMP_CONFIG_PASS`, or the prompt shown by the command.
23
42
 
24
43
  Import backs up every overwritten file to `<agent-dir>/.import-backup-<ts>/`
@@ -27,7 +46,5 @@ and asks for a restart so settings reload.
27
46
  ## Install
28
47
 
29
48
  ```
30
- omp plugin install /root/omp-config-crypt
49
+ omp plugin install omp-config-crypt
31
50
  ```
32
-
33
- Requires a new session before the commands appear.
package/index.js CHANGED
@@ -41,8 +41,8 @@ function collect(root) {
41
41
  return out;
42
42
  }
43
43
 
44
- /** Write an encrypted archive of the config tree. */
45
- export function exportEncrypted(outFile, pass, root = configRoot()) {
44
+ /** Build the encrypted archive in memory. */
45
+ export function buildArchive(pass, root = configRoot()) {
46
46
  if (!pass) throw new Error("empty passphrase");
47
47
  const files = collect(root).map((rel) => {
48
48
  const abs = path.join(root, rel);
@@ -56,16 +56,12 @@ export function exportEncrypted(outFile, pass, root = configRoot()) {
56
56
  const cipher = createCipheriv("aes-256-gcm", key, iv);
57
57
  const ct = Buffer.concat([cipher.update(plain), cipher.final()]);
58
58
  const blob = Buffer.concat([MAGIC, salt, iv, cipher.getAuthTag(), ct]);
59
- const abs = path.resolve(outFile);
60
- fs.mkdirSync(path.dirname(abs), { recursive: true });
61
- fs.writeFileSync(abs, blob, { mode: 0o600 });
62
- return { outFile: abs, files: files.length, bytes: blob.length };
59
+ return { blob, files: files.length };
63
60
  }
64
61
 
65
- /** Restore files from an encrypted archive, backing up overwritten ones. */
66
- export function importEncrypted(srcFile, pass, root = configRoot()) {
62
+ /** Restore an encrypted archive buffer into the config tree. */
63
+ export function restoreArchive(blob, pass, root = configRoot()) {
67
64
  if (!pass) throw new Error("empty passphrase");
68
- const blob = fs.readFileSync(srcFile);
69
65
  if (!blob.subarray(0, MAGIC.length).equals(MAGIC)) throw new Error("not an omp-config-crypt archive");
70
66
  let o = MAGIC.length;
71
67
  const salt = blob.subarray(o, (o += 16));
@@ -95,6 +91,20 @@ export function importEncrypted(srcFile, pass, root = configRoot()) {
95
91
  return { files: restored, backupDir: fs.existsSync(backupDir) ? backupDir : null };
96
92
  }
97
93
 
94
+ /** Write an encrypted archive of the config tree. */
95
+ export function exportEncrypted(outFile, pass, root = configRoot()) {
96
+ const { blob, files } = buildArchive(pass, root);
97
+ const abs = path.resolve(outFile);
98
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
99
+ fs.writeFileSync(abs, blob, { mode: 0o600 });
100
+ return { outFile: abs, files, bytes: blob.length };
101
+ }
102
+
103
+ /** Restore files from an encrypted archive, backing up overwritten ones. */
104
+ export function importEncrypted(srcFile, pass, root = configRoot()) {
105
+ return restoreArchive(fs.readFileSync(srcFile), pass, root);
106
+ }
107
+
98
108
  async function askPass(ctx, title) {
99
109
  if (process.env.OMP_CONFIG_PASS) return process.env.OMP_CONFIG_PASS;
100
110
  const input = ctx?.ui?.input;
@@ -114,7 +124,107 @@ async function askPass(ctx, title) {
114
124
  throw new Error("no passphrase: set OMP_CONFIG_PASS or answer the prompt");
115
125
  }
116
126
 
117
- /** Plugin entry: /config-export and /config-import. */
127
+ /** Token from the environment when present. */
128
+ function envToken() {
129
+ return process.env.OMP_CFG_CLOUD_TOKEN || process.env.GITHUB_TOKEN || null;
130
+ }
131
+
132
+ /** Token from env, else asked at command time (never persisted). */
133
+ async function askToken(ctx, title, required) {
134
+ const fromEnv = envToken();
135
+ if (fromEnv) return fromEnv;
136
+ const input = ctx?.ui?.input;
137
+ if (typeof input === "function") {
138
+ let value;
139
+ try {
140
+ value = await input.call(ctx.ui, { title, prompt: title, placeholder: required ? "token" : "token (empty = none)" });
141
+ } catch {
142
+ try {
143
+ value = await input.call(ctx.ui, title);
144
+ } catch {
145
+ value = undefined;
146
+ }
147
+ }
148
+ if (typeof value === "string" && value.trim()) return value.trim();
149
+ if (!required) return null;
150
+ }
151
+ if (required) throw new Error("token required: set OMP_CFG_CLOUD_TOKEN or enter it when asked");
152
+ return null;
153
+ }
154
+
155
+ const GIST_FILE = "omp-config.enc.b64";
156
+
157
+ async function gh(token, pathname, init = {}) {
158
+ const res = await fetch(`https://api.github.com${pathname}`, {
159
+ ...init,
160
+ signal: AbortSignal.timeout(30_000),
161
+ headers: {
162
+ accept: "application/vnd.github+json",
163
+ authorization: `Bearer ${token}`,
164
+ "content-type": "application/json",
165
+ ...(init.headers || {}),
166
+ },
167
+ });
168
+ if (!res.ok) throw new Error(`GitHub API ${res.status}: ${(await res.text()).slice(0, 200)}`);
169
+ return res.json();
170
+ }
171
+
172
+ /** Push the archive to a secret gist; returns its id and URL. */
173
+ export async function pushGist(id, pass, root = configRoot(), token = envToken()) {
174
+ if (!token) throw new Error("GitHub token required");
175
+ const { blob, files } = buildArchive(pass, root);
176
+ const content = blob.toString("base64");
177
+ const body = { files: { [GIST_FILE]: { content } } };
178
+ const data = id && id !== "new"
179
+ ? await gh(token, `/gists/${id}`, { method: "PATCH", body: JSON.stringify(body) })
180
+ : await gh(token, `/gists`, {
181
+ method: "POST",
182
+ body: JSON.stringify({ description: "omp config (encrypted)", public: false, ...body }),
183
+ });
184
+ return { id: data.id, url: data.html_url, files, bytes: blob.length };
185
+ }
186
+
187
+ /** Pull the archive from a secret gist. */
188
+ export async function pullGist(id, pass, root = configRoot(), token = envToken()) {
189
+ if (!token) throw new Error("GitHub token required");
190
+ const data = await gh(token, `/gists/${id}`);
191
+ const content = data?.files?.[GIST_FILE]?.content;
192
+ if (!content) throw new Error(`gist ${id} has no ${GIST_FILE}`);
193
+ return restoreArchive(Buffer.from(content, "base64"), pass, root);
194
+ }
195
+
196
+ /** Push to any endpoint accepting PUT (presigned S3, WebDAV, custom). */
197
+ export async function pushHttp(url, pass, root = configRoot(), token = envToken()) {
198
+ const { blob, files } = buildArchive(pass, root);
199
+ const headers = { "content-type": "application/octet-stream" };
200
+ if (token) headers.authorization = `Bearer ${token}`;
201
+ const res = await fetch(url, {
202
+ method: "PUT",
203
+ body: blob,
204
+ signal: AbortSignal.timeout(30_000),
205
+ headers,
206
+ });
207
+ if (!res.ok) throw new Error(`PUT ${res.status}: ${(await res.text()).slice(0, 200)}`);
208
+ return { url, files, bytes: blob.length };
209
+ }
210
+
211
+ /** Pull from any endpoint serving the archive. */
212
+ export async function pullHttp(url, pass, root = configRoot(), token = envToken()) {
213
+ const res = await fetch(url, {
214
+ signal: AbortSignal.timeout(30_000),
215
+ headers: token ? { authorization: `Bearer ${token}` } : {},
216
+ });
217
+ if (!res.ok) throw new Error(`GET ${res.status}: ${(await res.text()).slice(0, 200)}`);
218
+ return restoreArchive(Buffer.from(await res.arrayBuffer()), pass, root);
219
+ }
220
+
221
+ function parseSpec(spec) {
222
+ if (spec.startsWith("gist:")) return { kind: "gist", id: spec.slice(5) || "new" };
223
+ if (/^https?:\/\//.test(spec)) return { kind: "http", url: spec };
224
+ throw new Error("expected gist:<id|new> or an https URL");
225
+ }
226
+
227
+ /** Plugin entry: local and cloud config portability. */
118
228
  export default function (pi) {
119
229
  pi.registerCommand("config-export", {
120
230
  description: "Encrypt omp config + instructions to a file",
@@ -147,4 +257,49 @@ export default function (pi) {
147
257
  }
148
258
  },
149
259
  });
260
+
261
+ pi.registerCommand("config-push", {
262
+ description: "Encrypt and upload config to gist:<id|new> or an https URL",
263
+ handler: async (args, ctx) => {
264
+ const spec = (args || "").trim();
265
+ if (!spec) {
266
+ ctx.ui.notify("usage: /config-push gist:new | gist:<id> | https://…", "error");
267
+ return;
268
+ }
269
+ try {
270
+ const target = parseSpec(spec);
271
+ const pass = await askPass(ctx, "Push passphrase");
272
+ const token = await askToken(ctx, "Cloud token", target.kind === "gist");
273
+ const r = target.kind === "gist"
274
+ ? await pushGist(target.id, pass, configRoot(), token)
275
+ : await pushHttp(target.url, pass, configRoot(), token);
276
+ const where = r.url ? `${r.url} (id ${r.id})` : r.url;
277
+ ctx.ui.notify(`pushed ${r.files} files (${r.bytes} B) → ${where}`, "info");
278
+ } catch (error) {
279
+ ctx.ui.notify(`config-push failed: ${error?.message ?? error}`, "error");
280
+ }
281
+ },
282
+ });
283
+
284
+ pi.registerCommand("config-pull", {
285
+ description: "Download and decrypt config from gist:<id> or an https URL",
286
+ handler: async (args, ctx) => {
287
+ const spec = (args || "").trim();
288
+ if (!spec) {
289
+ ctx.ui.notify("usage: /config-pull gist:<id> | https://…", "error");
290
+ return;
291
+ }
292
+ try {
293
+ const target = parseSpec(spec);
294
+ const pass = await askPass(ctx, "Pull passphrase");
295
+ const token = await askToken(ctx, "Cloud token", target.kind === "gist");
296
+ const r = target.kind === "gist"
297
+ ? await pullGist(target.id, pass, configRoot(), token)
298
+ : await pullHttp(target.url, pass, configRoot(), token);
299
+ ctx.ui.notify(`restored ${r.files} files${r.backupDir ? ` (backup: ${r.backupDir})` : ""}; restart omp`, "info");
300
+ } catch (error) {
301
+ ctx.ui.notify(`config-pull failed: ${error?.message ?? error}`, "error");
302
+ }
303
+ },
304
+ });
150
305
  }
package/package.json CHANGED
@@ -1,15 +1,30 @@
1
1
  {
2
2
  "name": "omp-config-crypt",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Encrypted export/import of omp config and instructions",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
- "keywords": ["omp", "oh-my-pi", "plugin", "config", "sync", "encryption"],
8
- "files": ["index.js", "README.md", "LICENSE"],
7
+ "keywords": [
8
+ "omp",
9
+ "oh-my-pi",
10
+ "plugin",
11
+ "config",
12
+ "sync",
13
+ "encryption"
14
+ ],
15
+ "files": [
16
+ "index.js",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
9
20
  "omp": {
10
- "extensions": ["./index.js"]
21
+ "extensions": [
22
+ "./index.js"
23
+ ]
11
24
  },
12
25
  "pi": {
13
- "extensions": ["./index.js"]
26
+ "extensions": [
27
+ "./index.js"
28
+ ]
14
29
  }
15
30
  }