configre 2.1.9 → 2.2.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/README.md +11 -7
- package/bin/configre.js +77 -0
- package/index.js +3 -1
- package/package.json +5 -1
- package/secrets/index.js +15 -9
- package/secrets/register.js +2 -1
- package/skills/configre/SKILL.md +10 -4
- package/test/cli.test.js +155 -0
package/README.md
CHANGED
|
@@ -140,18 +140,22 @@ module.exports = {
|
|
|
140
140
|
};
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
-
Fill in the values in these local files. The production key overrides the shared key when that profile is selected, while `api.url` still comes from the public configuration. Configre generates `secrets.enc.json` and adds Git exclusions for the editable secret files when your application runs.
|
|
143
|
+
Fill in the values in these local files. The production key overrides the shared key when that profile is selected, while `api.url` still comes from the public configuration. Configre generates `secrets.enc.json` and adds Git exclusions for the editable secret files when your application or `npx configre sync config` runs.
|
|
144
144
|
|
|
145
145
|
### Share with a server
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
2. **Server:** pull and run. On the first run, Configre automatically commits and pushes the server's public key as `recipients/<profile>.pub` (or `<hostname>.pub` when no profile is specified).
|
|
149
|
-
3. **Local:** pull, run again to include that public key in the encrypted file, and push.
|
|
150
|
-
4. **Server:** pull and restart. The secrets are now available through `cfg`.
|
|
147
|
+
`configre sync` runs the same load as your application and reports what it did to the secrets, so neither machine has to start the application. Pass the configuration path relative to the current directory, and the same `--config=<profile>` as the application, if any.
|
|
151
148
|
|
|
152
|
-
|
|
149
|
+
1. **Local:** run `npx configre sync config` and push the generated encrypted file.
|
|
150
|
+
2. **Server:** pull and run `npx configre sync config --config=production`. Configre commits and pushes the server's public key as `recipients/production.pub` (or `recipients/<hostname>.pub` without `--config`).
|
|
151
|
+
3. **Local:** pull, run `npx configre sync config` again to include that public key in the encrypted file, and push.
|
|
152
|
+
4. **Server:** pull and restart. `npx configre sync config --config=production` now reports `Authorized`.
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
Starting the application performs the same steps silently. The server needs Git push access for registration. Until the updated encrypted file arrives, Configre loads public settings only.
|
|
155
|
+
|
|
156
|
+
To update secrets later, edit them locally, run `npx configre sync config`, and push. Pull and restart on the server. Registration happens only once per identity.
|
|
157
|
+
|
|
158
|
+
The command prints only names, paths and outcomes: `Created`, `Updated` or `Unchanged` for the encrypted file, `Registered`, `Already registered` or `Authorized` for the server. It exits with 1 when registration or loading fails, or when the configuration has no secrets, and with 2 for invalid arguments.
|
|
155
159
|
|
|
156
160
|
**Profiles organize secrets; they do not isolate access.** Authorized identities can decrypt all profiles in the encrypted file. Pulling a new registration and publishing the updated encrypted file grants that identity access.
|
|
157
161
|
|
package/bin/configre.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import Configre from "../index.js";
|
|
4
|
+
|
|
5
|
+
const usage = "Usage: configre sync <config-path> [--config=<profile>]";
|
|
6
|
+
const nextStep = "Next: on the machine with the editable secrets, pull, run configre sync and push the encrypted file; then pull here.";
|
|
7
|
+
|
|
8
|
+
function usageError(message) {
|
|
9
|
+
const error = new Error(`${message}\n${usage}`);
|
|
10
|
+
error.exitCode = 2;
|
|
11
|
+
return error;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function parseArgs(args) {
|
|
15
|
+
if (args.includes("-h") || args.includes("--help")) return { help: true };
|
|
16
|
+
const [command, ...rest] = args;
|
|
17
|
+
if (command !== "sync") {
|
|
18
|
+
throw usageError(command === undefined ? "Missing command" : `Unknown command: ${command}`);
|
|
19
|
+
}
|
|
20
|
+
const profiles = rest.filter(arg => arg.startsWith("--config="));
|
|
21
|
+
const unknown = rest.filter(arg => arg.startsWith("-") && !arg.startsWith("--config="));
|
|
22
|
+
const positional = rest.filter(arg => !arg.startsWith("-"));
|
|
23
|
+
if (unknown.length > 0) throw usageError(`Unknown option: ${unknown[0]}`);
|
|
24
|
+
if (profiles.length > 1) throw usageError("Pass --config=<profile> at most once");
|
|
25
|
+
if (profiles.length === 1 && profiles[0] === "--config=") throw usageError("--config requires a profile name");
|
|
26
|
+
if (positional.length !== 1) throw usageError("Pass exactly one config path");
|
|
27
|
+
return { configPath: positional[0], explicitProfile: profiles.length === 1 };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function display(filename) {
|
|
31
|
+
const relative = path.relative(process.cwd(), filename);
|
|
32
|
+
return relative && !relative.startsWith("..") && !path.isAbsolute(relative) ? relative : filename;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function report(config, configPath, explicitProfile) {
|
|
36
|
+
const outcome = config.secretsOutcome;
|
|
37
|
+
console.log(`Config: ${display(config.configFile)} (profile ${config.profile} from ${explicitProfile ? "--config" : "hostname"})`);
|
|
38
|
+
const lines = [];
|
|
39
|
+
switch (outcome.status) {
|
|
40
|
+
case "inactive":
|
|
41
|
+
throw new Error(`No secrets for ${configPath}: no base or profile secret file and no encrypted secrets file`);
|
|
42
|
+
case "created":
|
|
43
|
+
case "updated":
|
|
44
|
+
lines.push(`${outcome.status === "created" ? "Created" : "Updated"} ${display(outcome.encrypted)}`,
|
|
45
|
+
`Next: commit and push ${display(outcome.encrypted)}.`);
|
|
46
|
+
break;
|
|
47
|
+
case "unchanged":
|
|
48
|
+
lines.push(`Unchanged ${display(outcome.encrypted)}`);
|
|
49
|
+
break;
|
|
50
|
+
case "authorized":
|
|
51
|
+
lines.push(`Authorized to decrypt ${display(outcome.encrypted)}`);
|
|
52
|
+
break;
|
|
53
|
+
case "registered":
|
|
54
|
+
lines.push(`Registered ${display(outcome.recipient)} and pushed it to Git`, nextStep);
|
|
55
|
+
break;
|
|
56
|
+
case "already-registered":
|
|
57
|
+
lines.push(`Already registered ${display(outcome.recipient)}; not authorized yet`, nextStep);
|
|
58
|
+
break;
|
|
59
|
+
case "registration-failed":
|
|
60
|
+
throw new Error(outcome.reason);
|
|
61
|
+
default:
|
|
62
|
+
throw new Error(`Unexpected secrets outcome: ${outcome.status}`);
|
|
63
|
+
}
|
|
64
|
+
console.log(lines.join("\n"));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const options = parseArgs(process.argv.slice(2));
|
|
69
|
+
if (options.help) {
|
|
70
|
+
console.log(usage);
|
|
71
|
+
} else {
|
|
72
|
+
report(new Configre(path.resolve(options.configPath)), options.configPath, options.explicitProfile);
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error(error.message);
|
|
76
|
+
process.exitCode = error.exitCode ?? 1;
|
|
77
|
+
}
|
package/index.js
CHANGED
|
@@ -76,7 +76,9 @@ class ConfigreClass {
|
|
|
76
76
|
const configArg = process.argv.find(arg => arg.startsWith('--config='));
|
|
77
77
|
this.profile = configArg ? configArg.slice('--config='.length) : os.hostname();
|
|
78
78
|
this.profileSettings = this.loadProfileSettings();
|
|
79
|
-
|
|
79
|
+
const secrets = loadSecrets(pathOrDir, this.configFile, this.profile);
|
|
80
|
+
this.secretSettings = secrets.settings;
|
|
81
|
+
this.secretsOutcome = secrets.outcome;
|
|
80
82
|
} finally {
|
|
81
83
|
ConfigreClass._nesting -= 1;
|
|
82
84
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "configre",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "🔧 Effortlessly Tailor Your Settings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"lemonlog": "^1.2.2"
|
|
14
14
|
},
|
|
15
15
|
"main": "index.js",
|
|
16
|
+
"bin": {
|
|
17
|
+
"configre": "bin/configre.js"
|
|
18
|
+
},
|
|
16
19
|
"keywords": [
|
|
17
20
|
"json",
|
|
18
21
|
"config",
|
|
@@ -54,6 +57,7 @@
|
|
|
54
57
|
"files": [
|
|
55
58
|
"index.js",
|
|
56
59
|
"merge.js",
|
|
60
|
+
"bin/configre.js",
|
|
57
61
|
"secrets/*.js",
|
|
58
62
|
"test/*.js",
|
|
59
63
|
"skills/configre/SKILL.md",
|
package/secrets/index.js
CHANGED
|
@@ -131,20 +131,23 @@ function loadSecrets(configPath, configFile, profile) {
|
|
|
131
131
|
const hasEncrypted = !!stat(paths.encrypted);
|
|
132
132
|
const hasLocal = names.includes(path.basename(paths.local)) || (paths.directory &&
|
|
133
133
|
(names.includes(profile + ".dev.secret.cjs") || names.includes(profile + ".secret.cjs")));
|
|
134
|
-
if (!hasLocal && !hasEncrypted) return [];
|
|
134
|
+
if (!hasLocal && !hasEncrypted) return { settings: [], outcome: { status: "inactive" } };
|
|
135
135
|
|
|
136
136
|
const identity = loadIdentity();
|
|
137
137
|
try {
|
|
138
138
|
if (names.length === 0 && hasEncrypted) {
|
|
139
|
-
return
|
|
139
|
+
return {
|
|
140
|
+
settings: selectSecrets(validateBundle(decrypt(readJSON(paths.encrypted), identity)), paths, profile),
|
|
141
|
+
outcome: { status: "authorized", encrypted: paths.encrypted }
|
|
142
|
+
};
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
const bundle = withLock(paths.encrypted, () => {
|
|
145
|
+
const { bundle, status } = withLock(paths.encrypted, () => {
|
|
143
146
|
const names = localFiles(paths);
|
|
144
147
|
const hasEncrypted = !!stat(paths.encrypted);
|
|
145
148
|
const envelope = hasEncrypted ? readJSON(paths.encrypted) : null;
|
|
146
149
|
const previous = hasEncrypted ? validateBundle(decrypt(envelope, identity)) : null;
|
|
147
|
-
if (names.length === 0 && hasEncrypted) return previous;
|
|
150
|
+
if (names.length === 0 && hasEncrypted) return { bundle: previous, status: "authorized" };
|
|
148
151
|
|
|
149
152
|
prepareIgnore(paths);
|
|
150
153
|
const settings = { files: Object.fromEntries(names.map(name => [name, readSecret(path.join(paths.parent, name))])) };
|
|
@@ -152,26 +155,29 @@ function loadSecrets(configPath, configFile, profile) {
|
|
|
152
155
|
if (hasEncrypted) {
|
|
153
156
|
const previousRecipients = envelope.recipients.map(({ fingerprint, publicKey }) => ({ fingerprint, publicKey }));
|
|
154
157
|
if (isDeepStrictEqual(settings, previous) && isDeepStrictEqual(recipients, previousRecipients)) {
|
|
155
|
-
return settings;
|
|
158
|
+
return { bundle: settings, status: "unchanged" };
|
|
156
159
|
}
|
|
157
160
|
}
|
|
158
161
|
writeEncrypted(paths.encrypted, encrypt(settings, recipients));
|
|
159
162
|
log.info(hasEncrypted ? "Updated encrypted secrets file" : "Created encrypted secrets file", paths.encrypted);
|
|
160
|
-
return settings;
|
|
163
|
+
return { bundle: settings, status: hasEncrypted ? "updated" : "created" };
|
|
161
164
|
});
|
|
162
|
-
return selectSecrets(bundle, paths, profile);
|
|
165
|
+
return { settings: selectSecrets(bundle, paths, profile), outcome: { status, encrypted: paths.encrypted } };
|
|
163
166
|
} catch (error) {
|
|
164
167
|
if (error.code !== "CONFIGRE_NOT_AUTHORIZED") throw error;
|
|
165
168
|
let registration = "Public key is published in Git. The administrator must pull, reload Configre and publish secrets.enc.json; then pull the updated encrypted file and reload Configre.";
|
|
169
|
+
let outcome;
|
|
166
170
|
try {
|
|
167
|
-
registerRecipient(paths, identity, profile);
|
|
171
|
+
const { filename, published } = registerRecipient(paths, identity, profile);
|
|
172
|
+
outcome = { status: published ? "registered" : "already-registered", recipient: filename };
|
|
168
173
|
} catch (registrationError) {
|
|
169
174
|
registration = registrationError.code === "CONFIGRE_REGISTRATION_FAILED"
|
|
170
175
|
? registrationError.message
|
|
171
176
|
: "Automatic public-key registration failed; check Git and file permissions, then reload Configre to retry.";
|
|
177
|
+
outcome = { status: "registration-failed", reason: registration };
|
|
172
178
|
}
|
|
173
179
|
log.warn("Secrets not authorized; continuing with public settings only.", registration, "Public key:", identity.publicPath);
|
|
174
|
-
return [];
|
|
180
|
+
return { settings: [], outcome };
|
|
175
181
|
}
|
|
176
182
|
}
|
|
177
183
|
|
package/secrets/register.js
CHANGED
|
@@ -86,7 +86,7 @@ function registerRecipient(paths, identity, profile) {
|
|
|
86
86
|
if (stat(filename)) {
|
|
87
87
|
git(root, ["update-index", "--add", "--cacheinfo", "100644", match[1], relative], "reconciling the published public-key index entry");
|
|
88
88
|
}
|
|
89
|
-
return;
|
|
89
|
+
return { filename, published: false };
|
|
90
90
|
}
|
|
91
91
|
if (head !== remoteHead) {
|
|
92
92
|
throw registrationError("requires the local branch to match its upstream before publishing; synchronize the checkout without discarding local work");
|
|
@@ -114,6 +114,7 @@ function registerRecipient(paths, identity, profile) {
|
|
|
114
114
|
}
|
|
115
115
|
git(root, ["update-ref", branch, commit, head], "updating the local branch after publication");
|
|
116
116
|
git(root, ["update-index", "--add", "--cacheinfo", "100644", blob, relative], "updating the public-key index entry after publication");
|
|
117
|
+
return { filename, published: true };
|
|
117
118
|
} finally {
|
|
118
119
|
fs.rmSync(temporary, { recursive: true, force: true });
|
|
119
120
|
}
|
package/skills/configre/SKILL.md
CHANGED
|
@@ -63,10 +63,16 @@ module.exports = {
|
|
|
63
63
|
|
|
64
64
|
### Sharing and updates
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
Use `npx configre sync <config-path> [--config=<profile>]` for these steps instead of starting the application or writing `node -e` loaders.
|
|
67
|
+
It runs a normal load with the same profile rules and prints only paths and outcomes.
|
|
68
|
+
|
|
69
|
+
1. Local: create secret files and run `npx configre sync config`; it reports `Created` ciphertext and adds Git exclusions. Push the encrypted file.
|
|
70
|
+
2. Server: pull and run `npx configre sync config --config=<profile>`; it reports `Registered` after pushing `recipients/<profile>.pub`.
|
|
71
|
+
3. Local: pull, run `npx configre sync config` (reports `Updated`), and push the ciphertext.
|
|
72
|
+
4. Server: pull; `sync` reports `Authorized`. Restart. Later secret updates repeat the local sync/push and server pull/restart.
|
|
73
|
+
|
|
74
|
+
`Already registered` means the key is published but the ciphertext does not include it yet; `Unchanged` means nothing needed re-encryption.
|
|
75
|
+
Exit codes: 1 for failed registration or loading, or a configuration without secrets; 2 for invalid arguments.
|
|
70
76
|
|
|
71
77
|
Registration needs a branch matching its upstream and non-interactive push access. Without a Git identity, the commit is authored as `Configre <configre@localhost>`.
|
|
72
78
|
Pending authorization or failed registration returns public settings only; malformed secrets, identities, and ciphertext stop loading.
|
package/test/cli.test.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import { generatePrivateKey, parsePrivateKey } from "../secrets/crypto.js";
|
|
9
|
+
|
|
10
|
+
const bin = fileURLToPath(new URL("../bin/configre.js", import.meta.url));
|
|
11
|
+
const sentinel = "synthetic-secret-for-configre-cli-tests";
|
|
12
|
+
|
|
13
|
+
function environment(home) {
|
|
14
|
+
const env = { ...process.env, HOME: home, XDG_CONFIG_HOME: path.join(home, ".config"), GIT_CONFIG_NOSYSTEM: "1" };
|
|
15
|
+
delete env.DEBUG;
|
|
16
|
+
return env;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function fixture(t) {
|
|
20
|
+
const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "configre-cli-")));
|
|
21
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
22
|
+
const app = path.join(root, "app");
|
|
23
|
+
const config = path.join(app, "config");
|
|
24
|
+
fs.mkdirSync(config, { recursive: true });
|
|
25
|
+
fs.writeFileSync(path.join(config, "index.cjs"), 'module.exports = { api: { key: "", host: "default" } };');
|
|
26
|
+
return {
|
|
27
|
+
root, app, config,
|
|
28
|
+
homes: { admin: path.join(root, "admin-home"), server: path.join(root, "server-home") },
|
|
29
|
+
encrypted: path.join(config, "secrets.enc.json")
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function run(cwd, home, args) {
|
|
34
|
+
const result = spawnSync(process.execPath, [bin, ...args], { cwd, env: environment(home), encoding: "utf8" });
|
|
35
|
+
const output = result.stdout + result.stderr;
|
|
36
|
+
assert.equal(output.includes(sentinel), false);
|
|
37
|
+
assert.equal(/BEGIN [A-Z ]*KEY/.test(output), false);
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function git(directory, home, args) {
|
|
42
|
+
const result = spawnSync("git", ["-C", directory, ...args], { env: environment(home), encoding: "utf8" });
|
|
43
|
+
assert.equal(result.status, 0, result.stderr);
|
|
44
|
+
return result.stdout;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function writeSecrets(f) {
|
|
48
|
+
fs.writeFileSync(path.join(f.config, "index.secret.cjs"), `module.exports = { api: { key: ${JSON.stringify(sentinel)} } };\n`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test("invalid invocations print usage and exit with code 2", t => {
|
|
52
|
+
const f = fixture(t);
|
|
53
|
+
for (const args of [
|
|
54
|
+
[], ["register", "config"], ["sync"], ["sync", "config", "other"], ["sync", "config", "--config"],
|
|
55
|
+
["sync", "config", "--config="], ["sync", "config", "--config=a", "--config=b"], ["sync", "config", "--verbose"]
|
|
56
|
+
]) {
|
|
57
|
+
const result = run(f.app, f.homes.admin, args);
|
|
58
|
+
assert.equal(result.status, 2, args.join(" "));
|
|
59
|
+
assert.match(result.stderr, /Usage: configre sync <config-path> \[--config=<profile>\]/);
|
|
60
|
+
}
|
|
61
|
+
const help = run(f.app, f.homes.admin, ["sync", "--help"]);
|
|
62
|
+
assert.equal(help.status, 0);
|
|
63
|
+
assert.match(help.stdout, /^Usage: configre sync/);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("a config without secrets or an unknown path fails without creating an identity", t => {
|
|
67
|
+
const f = fixture(t);
|
|
68
|
+
const inactive = run(f.app, f.homes.admin, ["sync", "config"]);
|
|
69
|
+
assert.equal(inactive.status, 1);
|
|
70
|
+
assert.match(inactive.stdout, new RegExp(`^Config: config/index\\.cjs \\(profile ${os.hostname().replace(/[.]/g, "\\.")} from hostname\\)`));
|
|
71
|
+
assert.match(inactive.stderr, /No secrets for config/);
|
|
72
|
+
|
|
73
|
+
const missing = run(f.app, f.homes.admin, ["sync", "missing"]);
|
|
74
|
+
assert.equal(missing.status, 1);
|
|
75
|
+
assert.match(missing.stderr, /Could not load config/);
|
|
76
|
+
assert.equal(fs.existsSync(f.homes.admin), false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("the editing machine reports created, unchanged and updated encrypted files", t => {
|
|
80
|
+
const f = fixture(t);
|
|
81
|
+
writeSecrets(f);
|
|
82
|
+
|
|
83
|
+
const created = run(f.app, f.homes.admin, ["sync", "config", "--config=testhost"]);
|
|
84
|
+
assert.equal(created.status, 0, created.stderr);
|
|
85
|
+
assert.equal(created.stdout, [
|
|
86
|
+
"Config: config/index.cjs (profile testhost from --config)",
|
|
87
|
+
"Created config/secrets.enc.json",
|
|
88
|
+
"Next: commit and push config/secrets.enc.json.",
|
|
89
|
+
""
|
|
90
|
+
].join("\n"));
|
|
91
|
+
|
|
92
|
+
const unchanged = run(f.app, f.homes.admin, ["sync", "config", "--config=testhost"]);
|
|
93
|
+
assert.equal(unchanged.status, 0, unchanged.stderr);
|
|
94
|
+
assert.match(unchanged.stdout, /^Unchanged config\/secrets\.enc\.json$/m);
|
|
95
|
+
|
|
96
|
+
fs.writeFileSync(path.join(f.config, "recipients", "extra.pub"), parsePrivateKey(generatePrivateKey()).publicKey);
|
|
97
|
+
const updated = run(f.config, f.homes.admin, ["sync", ".", "--config=testhost"]);
|
|
98
|
+
assert.equal(updated.status, 0, updated.stderr);
|
|
99
|
+
assert.match(updated.stdout, /^Updated secrets\.enc\.json$/m);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("a server registers through Git and becomes authorized after the editing machine syncs", t => {
|
|
103
|
+
const f = fixture(t);
|
|
104
|
+
const { admin, server } = f.homes;
|
|
105
|
+
writeSecrets(f);
|
|
106
|
+
git(f.app, admin, ["init", "--quiet", "--initial-branch=main"]);
|
|
107
|
+
git(f.app, admin, ["config", "user.name", "Configre Test"]);
|
|
108
|
+
git(f.app, admin, ["config", "user.email", "configre-test@example.invalid"]);
|
|
109
|
+
assert.equal(run(f.app, admin, ["sync", "config", "--config=testhost"]).status, 0);
|
|
110
|
+
git(f.app, admin, ["add", "config/index.cjs", "config/.gitignore", "config/secrets.enc.json"]);
|
|
111
|
+
git(f.app, admin, ["commit", "--quiet", "-m", "Initial configuration"]);
|
|
112
|
+
const remote = path.join(f.root, "remote.git");
|
|
113
|
+
git(f.root, admin, ["init", "--bare", "--quiet", "--initial-branch=main", remote]);
|
|
114
|
+
git(f.app, admin, ["remote", "add", "origin", remote]);
|
|
115
|
+
git(f.app, admin, ["push", "--quiet", "--set-upstream", "origin", "main"]);
|
|
116
|
+
const checkout = path.join(f.root, "server");
|
|
117
|
+
git(f.root, server, ["clone", "--quiet", remote, checkout]);
|
|
118
|
+
|
|
119
|
+
const registered = run(checkout, server, ["sync", "config", "--config=web-01"]);
|
|
120
|
+
assert.equal(registered.status, 0, registered.stderr);
|
|
121
|
+
assert.match(registered.stdout, /^Registered config\/recipients\/web-01\.pub and pushed it to Git$/m);
|
|
122
|
+
assert.match(registered.stdout, /^Next: on the machine with the editable secrets/m);
|
|
123
|
+
assert.equal(git(remote, admin, ["diff-tree", "--no-commit-id", "--name-only", "-r", "main"]).trim(), "config/recipients/web-01.pub");
|
|
124
|
+
|
|
125
|
+
const pending = run(checkout, server, ["sync", "config", "--config=web-01"]);
|
|
126
|
+
assert.equal(pending.status, 0, pending.stderr);
|
|
127
|
+
assert.match(pending.stdout, /^Already registered config\/recipients\/web-01\.pub; not authorized yet$/m);
|
|
128
|
+
|
|
129
|
+
git(f.app, admin, ["pull", "--quiet", "--ff-only"]);
|
|
130
|
+
const updated = run(f.app, admin, ["sync", "config"]);
|
|
131
|
+
assert.equal(updated.status, 0, updated.stderr);
|
|
132
|
+
assert.match(updated.stdout, /^Updated config\/secrets\.enc\.json$/m);
|
|
133
|
+
git(f.app, admin, ["commit", "--quiet", "-am", "Authorize web-01"]);
|
|
134
|
+
git(f.app, admin, ["push", "--quiet"]);
|
|
135
|
+
git(checkout, server, ["pull", "--quiet", "--ff-only"]);
|
|
136
|
+
|
|
137
|
+
const authorized = run(checkout, server, ["sync", "config", "--config=web-01"]);
|
|
138
|
+
assert.equal(authorized.status, 0, authorized.stderr);
|
|
139
|
+
assert.match(authorized.stdout, /^Authorized to decrypt config\/secrets\.enc\.json$/m);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("an unauthorized machine that cannot register exits with the failure reason", t => {
|
|
143
|
+
const f = fixture(t);
|
|
144
|
+
writeSecrets(f);
|
|
145
|
+
assert.equal(run(f.app, f.homes.admin, ["sync", "config"]).status, 0);
|
|
146
|
+
const copy = path.join(f.root, "copy", "config");
|
|
147
|
+
fs.mkdirSync(copy, { recursive: true });
|
|
148
|
+
for (const name of ["index.cjs", "secrets.enc.json"]) {
|
|
149
|
+
fs.copyFileSync(path.join(f.config, name), path.join(copy, name));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const result = run(path.dirname(copy), f.homes.server, ["sync", "config", "--config=web-01"]);
|
|
153
|
+
assert.equal(result.status, 1);
|
|
154
|
+
assert.match(result.stderr, /not authorized; automatic registration failed while locating the Git repository/);
|
|
155
|
+
});
|