configre 2.1.8 → 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 +12 -8
- package/bin/configre.js +77 -0
- package/demo/config/hostname.cjs +0 -0
- package/demo/config/index.cjs +0 -0
- package/index.js +3 -1
- package/package.json +11 -6
- package/secrets/config.js +5 -1
- package/secrets/index.js +15 -9
- package/secrets/register.js +16 -2
- package/skills/configre/SKILL.md +11 -5
- package/test/cli.test.js +155 -0
- package/test/secrets.test.js +49 -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
|
|
|
@@ -221,7 +225,7 @@ For an individual configuration file, sidecars live beside it: `settings.secret.
|
|
|
221
225
|
|
|
222
226
|
### Identities and registration
|
|
223
227
|
|
|
224
|
-
Automatic registration requires a Git checkout with a configured upstream
|
|
228
|
+
Automatic registration requires a Git checkout with a configured upstream and credentials that allow a non-interactive push. The branch must match its upstream before a new registration. The registration commit uses the Git identity already configured; without one, it is authored as `Configre <configre@localhost>`, and Git config is never changed. Failed registrations are retried on the next load.
|
|
225
229
|
|
|
226
230
|
Each OS user has one identity, stored at `~/.config/configre/identity.pem` and `identity.pub`, reused across projects. Authorization is per project. A service running under another OS user needs its own registration.
|
|
227
231
|
|
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/demo/config/hostname.cjs
CHANGED
|
File without changes
|
package/demo/config/index.cjs
CHANGED
|
File without changes
|
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,15 +1,21 @@
|
|
|
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": {
|
|
7
7
|
"node": ">=22.13.0"
|
|
8
8
|
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node --test"
|
|
11
|
+
},
|
|
9
12
|
"dependencies": {
|
|
10
13
|
"lemonlog": "^1.2.2"
|
|
11
14
|
},
|
|
12
15
|
"main": "index.js",
|
|
16
|
+
"bin": {
|
|
17
|
+
"configre": "bin/configre.js"
|
|
18
|
+
},
|
|
13
19
|
"keywords": [
|
|
14
20
|
"json",
|
|
15
21
|
"config",
|
|
@@ -47,9 +53,11 @@
|
|
|
47
53
|
"url": "https://github.com/clasen/Configre/issues"
|
|
48
54
|
},
|
|
49
55
|
"homepage": "https://github.com/clasen/Configre#readme",
|
|
56
|
+
"packageManager": "pnpm@11.9.0",
|
|
50
57
|
"files": [
|
|
51
58
|
"index.js",
|
|
52
59
|
"merge.js",
|
|
60
|
+
"bin/configre.js",
|
|
53
61
|
"secrets/*.js",
|
|
54
62
|
"test/*.js",
|
|
55
63
|
"skills/configre/SKILL.md",
|
|
@@ -62,8 +70,5 @@
|
|
|
62
70
|
"demo/module/config/hostname.cjs",
|
|
63
71
|
"demo/secrets/demo.js",
|
|
64
72
|
"demo/secrets/config/index.cjs"
|
|
65
|
-
]
|
|
66
|
-
|
|
67
|
-
"test": "node --test"
|
|
68
|
-
}
|
|
69
|
-
}
|
|
73
|
+
]
|
|
74
|
+
}
|
package/secrets/config.js
CHANGED
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
|
@@ -31,6 +31,19 @@ function git(directory, args, operation, options = {}) {
|
|
|
31
31
|
return result.stdout;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function identityEnv(root) {
|
|
35
|
+
const env = {};
|
|
36
|
+
for (const role of ["AUTHOR", "COMMITTER"]) {
|
|
37
|
+
try {
|
|
38
|
+
git(root, ["var", `GIT_${role}_IDENT`], `resolving the Git ${role.toLowerCase()} identity`);
|
|
39
|
+
} catch {
|
|
40
|
+
env[`GIT_${role}_NAME`] = config.git.fallbackIdentity.name;
|
|
41
|
+
env[`GIT_${role}_EMAIL`] = config.git.fallbackIdentity.email;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return env;
|
|
45
|
+
}
|
|
46
|
+
|
|
34
47
|
function registerRecipient(paths, identity, profile) {
|
|
35
48
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(profile)) {
|
|
36
49
|
throw registrationError("requires a profile containing only letters, digits, dots, underscores or hyphens");
|
|
@@ -73,7 +86,7 @@ function registerRecipient(paths, identity, profile) {
|
|
|
73
86
|
if (stat(filename)) {
|
|
74
87
|
git(root, ["update-index", "--add", "--cacheinfo", "100644", match[1], relative], "reconciling the published public-key index entry");
|
|
75
88
|
}
|
|
76
|
-
return;
|
|
89
|
+
return { filename, published: false };
|
|
77
90
|
}
|
|
78
91
|
if (head !== remoteHead) {
|
|
79
92
|
throw registrationError("requires the local branch to match its upstream before publishing; synchronize the checkout without discarding local work");
|
|
@@ -86,7 +99,7 @@ function registerRecipient(paths, identity, profile) {
|
|
|
86
99
|
const blob = git(root, ["hash-object", "-w", "--stdin"], "storing the public key", { input: publicKey }).trim();
|
|
87
100
|
git(root, ["update-index", "--add", "--cacheinfo", "100644", blob, relative], "adding the public key to the isolated index", { env });
|
|
88
101
|
const tree = git(root, ["write-tree"], "building the registration tree", { env }).trim();
|
|
89
|
-
const commit = git(root, ["commit-tree", tree, "-p", head, "-m", `Configre: register ${profile}`], "creating the public-key commit
|
|
102
|
+
const commit = git(root, ["commit-tree", tree, "-p", head, "-m", `Configre: register ${profile}`], "creating the public-key commit", { env: identityEnv(root) }).trim();
|
|
90
103
|
if (fs.mkdirSync(directory, { recursive: true })) {
|
|
91
104
|
log.info("Created recipients directory", directory);
|
|
92
105
|
}
|
|
@@ -101,6 +114,7 @@ function registerRecipient(paths, identity, profile) {
|
|
|
101
114
|
}
|
|
102
115
|
git(root, ["update-ref", branch, commit, head], "updating the local branch after publication");
|
|
103
116
|
git(root, ["update-index", "--add", "--cacheinfo", "100644", blob, relative], "updating the public-key index entry after publication");
|
|
117
|
+
return { filename, published: true };
|
|
104
118
|
} finally {
|
|
105
119
|
fs.rmSync(temporary, { recursive: true, force: true });
|
|
106
120
|
}
|
package/skills/configre/SKILL.md
CHANGED
|
@@ -63,12 +63,18 @@ module.exports = {
|
|
|
63
63
|
|
|
64
64
|
### Sharing and updates
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
3. Local: pull, run to incorporate the public key, and push the updated ciphertext.
|
|
69
|
-
4. Server: pull and restart. Later secret updates repeat the local run/push and server pull/restart.
|
|
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.
|
|
70
68
|
|
|
71
|
-
|
|
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.
|
|
76
|
+
|
|
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.
|
|
73
79
|
Use isolated fixtures for verification unless live registration is authorized: loading can fetch, commit, and push.
|
|
74
80
|
Registration preserves unrelated changes, skips hooks, rejects key-name collisions, and retries failures on later loads.
|
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
|
+
});
|
package/test/secrets.test.js
CHANGED
|
@@ -837,6 +837,55 @@ test("a server publishes only its public key once and receives automatic authori
|
|
|
837
837
|
assert.equal(git(f.remote, ["rev-list", "--count", "main", "--", "config/recipients/testhost.pub"]).trim(), "1");
|
|
838
838
|
});
|
|
839
839
|
|
|
840
|
+
function isolateGitIdentity(t, f) {
|
|
841
|
+
const home = path.join(f.root, "git-home");
|
|
842
|
+
fs.mkdirSync(home);
|
|
843
|
+
const overrides = {
|
|
844
|
+
HOME: home,
|
|
845
|
+
GIT_CONFIG_GLOBAL: "/dev/null",
|
|
846
|
+
GIT_CONFIG_NOSYSTEM: "1",
|
|
847
|
+
GIT_AUTHOR_NAME: undefined,
|
|
848
|
+
GIT_AUTHOR_EMAIL: undefined,
|
|
849
|
+
GIT_COMMITTER_NAME: undefined,
|
|
850
|
+
GIT_COMMITTER_EMAIL: undefined,
|
|
851
|
+
EMAIL: undefined
|
|
852
|
+
};
|
|
853
|
+
const saved = Object.fromEntries(Object.keys(overrides).map(key => [key, process.env[key]]));
|
|
854
|
+
const apply = values => Object.entries(values).forEach(([key, value]) => {
|
|
855
|
+
if (value === undefined) delete process.env[key];
|
|
856
|
+
else process.env[key] = value;
|
|
857
|
+
});
|
|
858
|
+
apply(overrides);
|
|
859
|
+
t.after(() => apply(saved));
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function publishedIdentities(f) {
|
|
863
|
+
return git(f.remote, ["log", "-1", "--format=%an <%ae>%n%cn <%ce>", "main"]).trim().split("\n");
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
test("registration publishes with a fallback identity when Git has no usable author identity", t => {
|
|
867
|
+
const f = gitFixture(t);
|
|
868
|
+
isolateGitIdentity(t, f);
|
|
869
|
+
git(f.checkout, ["config", "--unset", "user.name"]);
|
|
870
|
+
git(f.checkout, ["config", "--unset", "user.email"]);
|
|
871
|
+
git(f.checkout, ["config", "user.useConfigOnly", "true"]);
|
|
872
|
+
const repositoryConfig = fs.readFileSync(path.join(f.checkout, ".git", "config"), "utf8");
|
|
873
|
+
f.useHome(1);
|
|
874
|
+
assertPublicOnly(f.consumer, f.logs, /public key is published in Git/i);
|
|
875
|
+
assert.equal(git(f.remote, ["diff-tree", "--no-commit-id", "--name-only", "-r", "main"]).trim(), "config/recipients/testhost.pub");
|
|
876
|
+
assert.deepEqual(publishedIdentities(f), ["Configre <configre@localhost>", "Configre <configre@localhost>"]);
|
|
877
|
+
assert.equal(git(f.checkout, ["rev-parse", "HEAD"]), git(f.remote, ["rev-parse", "main"]));
|
|
878
|
+
assert.equal(fs.readFileSync(path.join(f.checkout, ".git", "config"), "utf8"), repositoryConfig);
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
test("registration keeps the identity already configured in Git", t => {
|
|
882
|
+
const f = gitFixture(t);
|
|
883
|
+
isolateGitIdentity(t, f);
|
|
884
|
+
f.useHome(1);
|
|
885
|
+
assertPublicOnly(f.consumer, f.logs, /public key is published in Git/i);
|
|
886
|
+
assert.deepEqual(publishedIdentities(f), ["Configre Test <configre-test@example.invalid>", "Configre Test <configre-test@example.invalid>"]);
|
|
887
|
+
});
|
|
888
|
+
|
|
840
889
|
test("a rejected registration push preserves the branch and staging area and can be retried", t => {
|
|
841
890
|
const f = gitFixture(t);
|
|
842
891
|
const hook = path.join(f.remote, "hooks", "pre-receive");
|