9 9 public static void main(
String[] args) throws
Exception {
10 10 if (args.length
> 0 && args[
0].equals(
"--inside")) inside();
11 + else if (args.length
> 0 && args[
0].equals(
"--crypto")) {
12 + root
= Path.of(run(
null,
"git",
"rev-parse",
"--show-toplevel").trim());
13 + freshCryptoChecks();
14 17 static void host() throws
Exception {
80 83 if (
!run(root,
"git",
"show",
"HEAD:nushell/secrets.nu").startsWith(
"DFAES1")) fail(
"committed-encrypted");
81 84 ok(
"committed-encrypted");
86 + run(root,
"mise",
"run",
"setup-crypto");
83 87 var hosts
= allHosts();
84 88 System.out.println(
"manifests=" + String.join(
" ", hosts));
85 89 for (
var host
: hosts) runManifest(host);
93 97 run(root,
".git/hooks/pre-commit");
94 98 ok(
"pre-commit-hook");
100 + freshCryptoChecks();
97 101 System.out.println(
"RESULT:ok");
146 150 ok(
"clean:" + host);
149 - static void freshCryptoSmoke() throws
Exception {
150 - var home
= Path.of(
System.getProperty(
"user.home"),
"fresh-crypto-home");
153 + static void freshCryptoChecks() throws
Exception {
154 + var home
= Files.createTempDirectory(
Path.of(
System.getProperty(
"user.home")),
"dotfiles-crypto-test-");
156 + freshCryptoChecks(home);
162 + static void freshCryptoChecks(
Path home) throws
Exception {
151 163 var repo
= home.resolve(
"repo");
153 164 Files.createDirectories(repo.resolve(
"tools"));
154 165 Files.createDirectories(home);
155 166 Files.copy(root.resolve(
"bootstripper.java"), repo.resolve(
"bootstripper.java"));
156 167 Files.copy(root.resolve(
"tools/aes.java"), repo.resolve(
"tools/aes.java"));
168 + Files.copy(root.resolve(
"mise.toml"), repo.resolve(
"mise.toml"));
169 + Files.createDirectories(repo.resolve(
"scripts"));
170 + Files.copy(root.resolve(
"scripts/mise_shell.java"), repo.resolve(
"scripts/mise_shell.java"));
157 171 Files.writeString(repo.resolve(
".secrets"),
"secret.txt\n");
158 172 Files.writeString(repo.resolve(
"mini.symlinks"),
"dummy.txt\n~/dummy.txt\n");
159 173 Files.writeString(repo.resolve(
"secret.txt"),
"alpha=base\nmiddle=stable\nomega=base\n");
161 175 run(repo,
"git",
"init",
"--quiet");
162 176 run(repo,
"git",
"config",
"user.name",
"Dotfiles Test");
163 177 run(repo,
"git",
"config",
"user.email",
"dotfiles@example.invalid");
178 + Files.writeString(repo.resolve(
".git/info/attributes"),
"*.bin -diff\n# BEGIN bootstripper secrets\nsecret.txt -text filter=dotfiles-aes -diff merge=dotfiles-aes\n# END bootstripper secrets\n");
164 179 var env
= Map.of(
"DOTFILES_AES_PASSPHRASE",
"test",
"JAVA_TOOL_OPTIONS",
"-Duser.home=" + home);
180 + run(repo,
"mise",
"trust",
"--yes");
181 + linkOnlyChecks(repo, home, env);
165 182 run(repo, env,
"java",
"bootstripper.java",
"init-crypto");
183 + run(repo, env,
"mise",
"run",
"setup-crypto");
166 184 run(repo, env,
"java",
"bootstripper.java",
"check");
167 - run(repo, env,
"git",
"add",
".dotfiles-aes-salt",
".secrets",
"bootstripper.java",
"tools/aes.java",
"mini.symlinks",
"dummy.txt",
"secret.txt");
168 - run(repo, env,
"java",
"bootstripper.java",
"check");
185 + if (
!Files.readString(repo.resolve(
".gitattributes")).contains(
"secret.txt -text filter=dotfiles-aes -diff merge=dotfiles-aes")) fail(
"tracked-attributes-generated");
186 + if (
!Files.readString(repo.resolve(
".git/info/attributes")).equals(
"*.bin -diff\n")) fail(
"local-attributes-migrated");
187 + if (
!runExpectFail(repo, env,
"java",
"bootstripper.java",
"pre-commit").contains(
".secrets must match the staged version")) fail(
"unstaged-secret-manifest-not-rejected");
188 + run(repo, env,
"git",
"add",
".dotfiles-aes-salt",
".gitattributes",
".secrets",
"bootstripper.java",
"tools/aes.java",
"mini.symlinks",
"dummy.txt",
"secret.txt");
189 + run(repo, env,
"java",
"bootstripper.java",
"pre-commit");
169 190 if (
!run(repo,
"git",
"show",
":secret.txt").startsWith(
"DFAES1")) fail(
"fresh-staged-encrypted");
170 191 run(repo,
"git",
"commit",
"--quiet",
"-m",
"base");
171 192 var base
= run(repo,
"git",
"rev-parse",
"HEAD").trim();
194 + Files.writeString(repo.resolve(
".gitattributes"),
Files.readString(repo.resolve(
".gitattributes"))
+ "*.tmp -diff\n");
195 + if (
!runExpectFail(repo, env,
"java",
"bootstripper.java",
"pre-commit").contains(
".gitattributes must match the staged version")) fail(
"unstaged-attributes-not-rejected");
196 + run(repo,
"git",
"restore",
".gitattributes");
197 + Files.writeString(repo.resolve(
".secrets"),
"secret.txt\nincoming-secret.txt\n");
198 + Files.writeString(repo.resolve(
"incoming-secret.txt"),
"incoming=plain\n");
199 + run(repo, env,
"java",
"bootstripper.java",
"check");
200 + run(repo,
"git",
"add",
".secrets",
".gitattributes",
"incoming-secret.txt");
201 + run(repo, env,
"java",
"bootstripper.java",
"pre-commit");
202 + run(repo,
"git",
"commit",
"--quiet",
"-m",
"add encrypted secret");
203 + var incoming
= run(repo,
"git",
"rev-parse",
"HEAD").trim();
204 + run(repo,
"git",
"switch",
"--quiet",
"--detach", base);
205 + run(repo,
"git",
"merge",
"--quiet",
"--ff-only", incoming);
206 + if (
!Files.readString(repo.resolve(
"incoming-secret.txt")).equals(
"incoming=plain\n")) fail(
"same-pull-secret-not-smudged");
207 + ok(
"same-pull-secret-smudged");
209 + cryptoRestoreChecks(repo, env);
173 211 run(repo,
"git",
"switch",
"--quiet",
"-c",
"secret-other");
174 212 Files.writeString(repo.resolve(
"secret.txt"),
"alpha=other\nmiddle=stable\nomega=base\n");
175 213 run(repo,
"git",
"add",
"secret.txt");
212 250 wslEnv.put(
"WSL_DISTRO_NAME",
"mini");
213 251 wslEnv.put(
"HOSTNAME",
"ignored");
214 252 run(repo, wslEnv,
"java",
"bootstripper.java",
"bootstrap");
253 + if (
!Files.readString(repo.resolve(
"secret.txt")).equals(
"not a real secret\n")) fail(
"bootstrap-overwrote-local-secret");
215 254 if (
!Files.isSymbolicLink(wslHome.resolve(
"dummy.txt"))) fail(
"fresh-wsl-distro-manifest");
216 255 run(repo, wslEnv,
"java",
"bootstripper.java",
"clean");
217 256 var env2
= new HashMap<>(env); env2.put(
"HOSTNAME",
"mini");
224 263 ok(
"fresh-crypto");
266 + static void linkOnlyChecks(
Path repo,
Path home,
Map<
String,
String> env) throws
Exception {
267 + var linkEnv
= new HashMap<>(env);
268 + linkEnv.put(
"DOTFILES_SYMLINKS",
"mini");
269 + var config
= Files.readString(repo.resolve(
".git/config"));
270 + var attributes
= Files.readString(repo.resolve(
".git/info/attributes"));
271 + var secret
= Files.readString(repo.resolve(
"secret.txt"));
272 + for (
var attempt
= 0; attempt
< 2; attempt
++) run(repo, linkEnv,
"mise",
"run",
"link");
273 + if (
!Files.isSymbolicLink(home.resolve(
"dummy.txt"))
|| !Files.isSameFile(home.resolve(
"dummy.txt"), repo.resolve(
"dummy.txt"))) fail(
"link-only-target");
274 + if (
!Files.readString(repo.resolve(
"secret.txt")).equals(secret)) fail(
"link-only-secret-changed");
275 + if (
!Files.readString(repo.resolve(
".git/config")).equals(config)) fail(
"link-only-config-changed");
276 + if (
!Files.readString(repo.resolve(
".git/info/attributes")).equals(attributes)) fail(
"link-only-attributes-changed");
277 + for (
var path
: List.of(
".git/dotfiles-aes.key",
".gitattributes",
".git/hooks/pre-commit"))
278 + if (
Files.exists(repo.resolve(path))) fail(
"link-only-created:" + path);
279 + Files.delete(home.resolve(
"dummy.txt"));
281 + var manifest
= repo.resolve(
"mini.symlinks");
282 + var original
= Files.readString(manifest);
283 + for (
var invalid
: List.of(
"missing.txt\n~/missing.txt\n",
"dummy.txt\n",
"dummy.txt\n~/dummy.txt\n",
"dummy.txt\n/etc/dotfiles-test\n")) {
284 + Files.writeString(manifest, original
+ invalid);
285 + runExpectFail(repo, linkEnv,
"mise",
"run",
"link");
286 + if (
Files.exists(home.resolve(
"dummy.txt"),
LinkOption.
NOFOLLOW_LINKS)) fail(
"link-before-validation");
288 + Files.writeString(manifest, original);
289 + ok(
"link-only-and-manifest-validation");
292 + static void cryptoRestoreChecks(
Path repo,
Map<
String,
String> env) throws
Exception {
293 + var first
= repo.resolve(
"secret.txt");
294 + var second
= repo.resolve(
"incoming-secret.txt");
295 + var firstPlain
= Files.readAllBytes(first);
296 + var secondPlain
= Files.readAllBytes(second);
297 + run(repo, env,
"java",
"tools/aes.java",
"encrypt",
"--key-file",
".git/dotfiles-aes.key",
"secret.txt",
"encrypted-secret.txt");
298 + var encrypted
= Files.readAllBytes(repo.resolve(
"encrypted-secret.txt"));
299 + Files.delete(repo.resolve(
"encrypted-secret.txt"));
300 + Files.write(first, encrypted);
301 + var index
= Files.readAllBytes(repo.resolve(
".git/index"));
302 + run(repo, env,
"mise",
"run",
"setup-crypto");
303 + if (
!Arrays.equals(
Files.readAllBytes(first), firstPlain)) fail(
"explicit-crypto-decryption");
304 + if (
!Arrays.equals(
Files.readAllBytes(repo.resolve(
".git/index")), index)) fail(
"crypto-changed-index");
305 + var modified
= Files.getLastModifiedTime(first);
306 + run(repo, env,
"mise",
"run",
"setup-crypto");
307 + if (
!Files.getLastModifiedTime(first).equals(modified)) fail(
"crypto-rewrote-plaintext");
309 + for (
var change
: List.of(
"plaintext",
"ciphertext",
"deleted",
"staged",
"staged-deletion")) {
310 + Files.write(first, encrypted);
311 + Files.write(second, secondPlain);
313 + case "plaintext",
"staged" -> Files.writeString(second,
"local change\n");
314 + case "ciphertext" -> Files.write(second, encrypted);
315 + case "deleted" -> Files.delete(second);
316 + case "staged-deletion" -> run(repo,
"git",
"rm",
"--cached",
"--quiet",
"incoming-secret.txt");
318 + if (change.equals(
"staged")) run(repo,
"git",
"add",
"incoming-secret.txt");
319 + var before
= Files.exists(second)
? Files.readAllBytes(second)
: null;
320 + index
= Files.readAllBytes(repo.resolve(
".git/index"));
321 + if (
!runExpectFail(repo, env,
"mise",
"run",
"setup-crypto").contains(
"Refusing to restore locally changed secret")) fail(
"crypto-accepted:" + change);
322 + if (
!Arrays.equals(
Files.readAllBytes(first), encrypted)) fail(
"crypto-partial-restore:" + change);
323 + if (before
== null ? Files.exists(second)
: !Arrays.equals(
Files.readAllBytes(second), before)) fail(
"crypto-overwrote:" + change);
324 + if (
!Arrays.equals(
Files.readAllBytes(repo.resolve(
".git/index")), index)) fail(
"crypto-changed-index:" + change);
325 + run(repo,
"git",
"reset",
"--quiet",
"HEAD",
"--",
"incoming-secret.txt");
326 + ok(
"crypto-refuses:" + change);
328 + var linkEnv
= new HashMap<>(env);
329 + linkEnv.put(
"DOTFILES_SYMLINKS",
"mini");
330 + run(repo, linkEnv,
"mise",
"run",
"link");
331 + if (
!Arrays.equals(
Files.readAllBytes(first), encrypted)) fail(
"link-decrypted-secret");
332 + run(repo, linkEnv,
"java",
"bootstripper.java",
"clean");
333 + Files.write(first, firstPlain);
334 + Files.write(second, secondPlain);
335 + ok(
"explicit-crypto-preserves-local-changes");
227 338 interface Mapping {
void accept(
String source,
String target)
throws Exception; }
228 339 static void forEachMapping(
Path manifest,
Mapping mapping) throws
Exception {
229 340 String source
= null;