import static java.text.MessageFormat.format; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.InetAddress; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.attribute.PosixFilePermissions; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Base64; import java.util.HashSet; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; class bootstripper { private final static String operating_system = System.getProperty("os.name"); private final static boolean win32 = operating_system.toLowerCase().contains("win"); private final static String filter_name = "dotfiles-aes"; private final static String key_file = "dotfiles-aes.key"; private final static String salt_file = ".dotfiles-aes-salt"; private final static String secrets_file = ".secrets"; private final static String attributes_begin = "# BEGIN bootstripper secrets"; private final static String attributes_end = "# END bootstripper secrets"; private final static byte[] aes_magic = new byte[] { 'D', 'F', 'A', 'E', 'S', '1' }; private final static int salt_bytes = 32; private final static int derived_key_bits = 512; private final static int pbkdf2_iterations = 310_000; private final static List warnings = new ArrayList<>(); private final static List errors = new ArrayList<>(); public static void main(String[] arguments) throws Throwable { var command = "bootstrap"; var dorkfile_path_input = "."; if (arguments.length == 1) { if (is_command(arguments[0])) command = arguments[0]; else dorkfile_path_input = arguments[0]; } else if (arguments.length == 2) { if (!is_command(arguments[0])) { usage(); throw fail(); } command = arguments[0]; dorkfile_path_input = arguments[1]; } else if (arguments.length > 2) { usage(); throw fail(); } var root = Path.of(dorkfile_path_input).toRealPath(); if ("init-crypto".equals(command)) { init_crypto(root); return; } if ("check".equals(command)) { setup_crypto(root, false); check(root); return; } if ("pre-commit".equals(command)) { setup_crypto(root, false); pre_commit(root); return; } if ("install-hooks".equals(command)) { require_git_repo(root); install_hooks(root); return; } log("Assuming dorkfiles root repo in {0}.", emphasize_local(root.toString())); if ("clean".equals(command)) { var symlinks_file = symlinks_file(root); delete_symlinks(root, symlinks_file); return; } setup_crypto(root, true); install_hooks(root); check(root); var symlinks_file = symlinks_file(root); create_symlinks(root, symlinks_file); print_attention(false); } static boolean is_command(String argument) { return "bootstrap".equals(argument) || "clean".equals(argument) || "check".equals(argument) || "pre-commit".equals(argument) || "install-hooks".equals(argument) || "init-crypto".equals(argument); } static void init_crypto(Path root) throws Throwable { require_git_repo(root); var salt = root.resolve(salt_file); if (Files.exists(salt)) { log("Crypto salt already exists: {0}", emphasize_local(salt_file)); return; } var bytes = new byte[salt_bytes]; new SecureRandom().nextBytes(bytes); Files.writeString(salt, Base64.getEncoder().encodeToString(bytes) + "\n", StandardCharsets.UTF_8); log("Created crypto salt: {0}", emphasize_local(salt_file)); } static void setup_crypto(Path root, boolean restore) throws Throwable { require_git_repo(root); var secrets = read_secrets(root); if (secrets.isEmpty()) return; install_filter(root); write_attributes(root, secrets); ensure_key(root); self_test_aes(root); if (restore) restore_tracked_secrets(root, secrets); apply_secret_permissions(root, secrets); } static void require_git_repo(Path root) throws Throwable { run(root, List.of("git", "rev-parse", "--is-inside-work-tree"), null, false); } static Path git_common_path(Path root, String path) throws Throwable { var common = run(root, List.of("git", "rev-parse", "--git-common-dir"), null, false).text().strip(); var common_path = Path.of(common); if (!common_path.isAbsolute()) common_path = root.resolve(common_path).normalize(); return common_path.resolve(path).toAbsolutePath(); } static Path key_path(Path root) throws Throwable { return git_common_path(root, key_file); } static String key_argument(Path root) throws Throwable { return key_path(root).toString(); } static String aes_tool_argument(Path root) { return root.resolve("tools/aes.java").toAbsolutePath().toString(); } static List read_secrets(Path root) throws IOException { var path = root.resolve(secrets_file); if (!Files.exists(path)) return List.of(); var secrets = new ArrayList(); for (var raw : Files.readAllLines(path)) { var line = raw.strip(); if (line.isEmpty() || line.startsWith("#")) continue; if (line.contains(" ") || line.contains("\t") || line.startsWith("/") || line.contains("\\")) throw new IllegalArgumentException("invalid secret path: " + line); secrets.add(line); } return secrets; } static void install_filter(Path root) throws Throwable { var key = quote_shell(key_argument(root)); var aes = quote_shell(aes_tool_argument(root)); run(root, List.of("git", "config", "--local", "filter." + filter_name + ".clean", "java " + aes + " encrypt --key-file " + key), null, false); run(root, List.of("git", "config", "--local", "filter." + filter_name + ".smudge", "java " + aes + " decrypt --key-file " + key), null, false); run(root, List.of("git", "config", "--local", "filter." + filter_name + ".required", "true"), null, false); } static String quote_shell(String argument) { return "'" + argument.replace("'", "'\\''") + "'"; } static void write_attributes(Path root, List secrets) throws Throwable { var attributes = git_common_path(root, "info/attributes"); Files.createDirectories(attributes.getParent()); var previous = Files.exists(attributes) ? Files.readString(attributes) : ""; var lines = new ArrayList(); var in_block = false; for (var line : previous.split("\n", -1)) { if (attributes_begin.equals(line)) { in_block = true; continue; } if (attributes_end.equals(line)) { in_block = false; continue; } if (!in_block && !line.isEmpty()) lines.add(line); } lines.add(attributes_begin); for (var secret : secrets) lines.add(secret + " -text filter=" + filter_name + " -diff"); lines.add(attributes_end); Files.writeString(attributes, String.join("\n", lines) + "\n", StandardCharsets.UTF_8); } static void ensure_key(Path root) throws Throwable { var key = key_path(root); if (Files.exists(key)) return; Files.createDirectories(key.getParent()); var salt = read_salt(root); var passphrase = passphrase(); try { var factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); var spec = new PBEKeySpec(passphrase, salt, pbkdf2_iterations, derived_key_bits); var encoded = factory.generateSecret(spec).getEncoded(); Files.writeString(key, Base64.getEncoder().encodeToString(encoded) + "\n", StandardCharsets.UTF_8); chmod(key, "rw-------"); log("Cached dotfiles AES key in {0}", emphasize_local(key.toString())); } finally { Arrays.fill(passphrase, '\0'); } } static byte[] read_salt(Path root) throws IOException { var path = root.resolve(salt_file); if (!Files.exists(path)) throw new IllegalStateException("Missing " + salt_file + ". Run `java bootstripper.java init-crypto` once for this repo."); return Base64.getDecoder().decode(Files.readString(path).trim()); } static char[] passphrase() { var from_env = System.getenv("DOTFILES_AES_PASSPHRASE"); if (from_env != null && !from_env.isEmpty()) return from_env.toCharArray(); var console = System.console(); if (console == null) throw new IllegalStateException("No console available. Set DOTFILES_AES_PASSPHRASE for non-interactive bootstrap."); var first = console.readPassword("[%s]:", emphasize_global("Dotfiles AES passphrase")); var second = console.readPassword("[%s]:", emphasize_global("Repeat passphrase")); if (!Arrays.equals(first, second)) throw new IllegalStateException("Passphrases do not match."); Arrays.fill(second, '\0'); return first; } static void self_test_aes(Path root) throws Throwable { var plain = "bootstripper self-test".getBytes(StandardCharsets.UTF_8); var key = key_argument(root); var aes = aes_tool_argument(root); var encrypted = run(root, List.of("java", aes, "encrypt", "--key-file", key), plain, false).stdout; if (!starts_with(encrypted, aes_magic)) throw fail("AES self-test did not produce expected magic header."); var decrypted = run(root, List.of("java", aes, "decrypt", "--key-file", key), encrypted, false).stdout; if (!Arrays.equals(plain, decrypted)) throw fail("AES self-test failed to round-trip."); } static void restore_tracked_secrets(Path root, List secrets) throws Throwable { var tracked = new ArrayList(); for (var secret : secrets) { var result = run(root, List.of("git", "cat-file", "-e", "HEAD:" + secret), null, false, false); if (result.exit == 0) { tracked.add(secret); var path = root.resolve(secret); if (Files.exists(path) && starts_with(Files.readAllBytes(path), aes_magic)) Files.delete(path); } } if (tracked.isEmpty()) return; var command = new ArrayList(); command.addAll(List.of("git", "restore", "--source=HEAD", "--worktree", "--")); command.addAll(tracked); run(root, command, null, false); } static void apply_secret_permissions(Path root, List secrets) throws IOException { for (var secret : secrets) { var path = root.resolve(secret); if (Files.exists(path) && secret.startsWith("ssh/") && secret.endsWith(".key")) chmod(path, "rw-------"); } } static void install_hooks(Path root) throws Throwable { var hooks = git_common_path(root, "hooks"); Files.createDirectories(hooks); var hook = hooks.resolve("pre-commit"); Files.writeString(hook, "#!/bin/sh\ncd \"$(git rev-parse --show-toplevel)\" || exit 1\nexec java bootstripper.java pre-commit\n", StandardCharsets.UTF_8); chmod(hook, "rwxr-xr-x"); log("Installed Git hook: {0}", emphasize_local(".git/hooks/pre-commit")); } static void pre_commit(Path root) throws Throwable { check(root); } static void check(Path root) throws Throwable { var secrets = read_secrets(root); if (!secrets.isEmpty()) { check_filter(root); check_attributes(root, secrets); check_worktree_secrets_decrypted(root, secrets); check_key_not_tracked(root); check_staged_secrets(root, secrets); } check_symlink_sources(root); check_staged_plaintext_patterns(root); log("OK bootstripper check passed."); } static void check_staged_plaintext_patterns(Path root) throws Throwable { var diff = run(root, List.of("git", "diff", "--cached", "-U0"), null, false).text(); var patterns = List.of( Pattern.compile("gh[pousr]_[A-Za-z0-9_]{20,}"), Pattern.compile("github_personal_access_token\\s*\\\":\\s*\\\"[^ secrets) throws Throwable { for (var secret : secrets) { var output = run(root, List.of("git", "check-attr", "text", "filter", "diff", "--", secret), null, false).text(); if (!output.contains(secret + ": text: unset")) throw fail("Missing -text attribute for {0}: {1}", secret, output.strip()); if (!output.contains(secret + ": filter: " + filter_name)) throw fail("Missing filter attribute for {0}: {1}", secret, output.strip()); if (!output.contains(secret + ": diff: unset")) throw fail("Missing -diff attribute for {0}: {1}", secret, output.strip()); } } static void check_worktree_secrets_decrypted(Path root, List secrets) throws Throwable { for (var secret : secrets) { var path = root.resolve(secret); if (Files.exists(path) && starts_with(Files.readAllBytes(path), aes_magic)) throw fail("Worktree secret is encrypted; smudge failed: {0}", secret); } } static void check_key_not_tracked(Path root) throws Throwable { var bad = run(root, List.of("git", "ls-files", "--", ".git/" + key_file, key_file), null, false).text().strip(); if (!bad.isEmpty()) throw fail("Key file is tracked: {0}", key_file); var staged = run(root, List.of("git", "diff", "--cached", "--name-only", "--", ".git/" + key_file, key_file), null, false).text().strip(); if (!staged.isEmpty()) throw fail("Key file is staged: {0}", key_file); } static void check_staged_secrets(Path root, List secrets) throws Throwable { for (var secret : secrets) { var status = run(root, List.of("git", "diff", "--cached", "--name-status", "--", secret), null, false).text().strip(); if (status.isEmpty() || status.startsWith("D")) continue; var blob = run(root, List.of("git", "show", ":" + secret), null, false).stdout; if (!starts_with(blob, aes_magic)) throw fail("Staged secret is not encrypted: {0}", secret); run(root, List.of("java", aes_tool_argument(root), "decrypt", "--key-file", key_argument(root)), blob, false); } } static void check_symlink_sources(Path root) throws Throwable { try (var stream = Files.list(root)) { var files = stream.filter(path -> path.getFileName().toString().endsWith(".symlinks")).collect(Collectors.toList()); for (var file : files) { var targets = new HashSet(); var lines = Files.readAllLines(file); String source = null; var source_line = 0; var line_number = 0; for (var raw : lines) { line_number++; var line = raw.strip(); if (line.isEmpty()) continue; if (source == null) { source = line; source_line = line_number; continue; } var source_path = root.resolve(source); if (!Files.exists(source_path, LinkOption.NOFOLLOW_LINKS)) throw fail("Missing symlink source: {0}:{1}: {2}", file.getFileName(), source_line, source); var target_path = expand_target(line).normalize(); if (!targets.add(target_path)) throw fail("Duplicate symlink target: {0}:{1}: {2}", file.getFileName(), line_number, line); source = null; } if (source != null) throw fail("Missing symlink target after: {0}: {1}", file.getFileName(), source); } } } static Path symlinks_file(Path root) throws Throwable { var hostname = hostname(); var manifest = symlinks_manifest(root, hostname); log("Hostname: {0}", emphasize_global(hostname)); log("Symlinks manifest: {0}", emphasize_global(manifest + ".symlinks")); var symlinks_file = root.resolve(manifest + ".symlinks"); if (!Files.exists(symlinks_file)) throw fail("Expected a file containing symlinks at: {0}. Found nothing!", symlinks_file); return symlinks_file; } static String symlinks_manifest(Path root, String hostname) throws IOException { var explicit = System.getenv("DOTFILES_SYMLINKS"); if (explicit != null && !explicit.isBlank()) return explicit.replaceFirst("\\.symlinks$", ""); return hostname; } static void create_symlinks(Path root, Path symlinks_file) throws Throwable { log("Creating symlinks as defined in {0} file.", emphasize_local(symlinks_file.getFileName().toString())); var lines = Files.readAllLines(symlinks_file); for_each_mapping(lines, (source, target) -> link(root, source, target)); } static void delete_symlinks(Path root, Path symlinks_file) throws Throwable { log("Deleting symlinks as defined in {0} file.", emphasize_local(symlinks_file.getFileName().toString())); var lines = Files.readAllLines(symlinks_file); for_each_mapping(lines, (source, target) -> delete(root, source, target)); } static void delete(Path root, String source, String target) throws Throwable { var source_path = root.resolve(source).toAbsolutePath(); var target_path = expand_target(target); if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) { if (Files.isSymbolicLink(target_path)) { log("\nOK {0} exists and points to {1}, a symlink indeed!\nDeleting {0}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString())); Files.delete(target_path); } else { log("\nNOPE {0} exists, but it is not a symlink! Clean manually.", target_path.toString()); } } } static void link(Path root, String source, String target) throws Throwable { var source_path = root.resolve(source).toAbsolutePath().normalize(); if (!Files.exists(source_path)) { log("\n{0} does not exist anymore in my dorkfiles. Have I yeeted it?", emphasize_global(source_path.toString())); return; } var expected = source_path.toRealPath(); require_inside_home(expected, "Symlink source"); var target_path = expand_target(target); if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) { if (Files.isSymbolicLink(target_path)) { var raw_actual = symlink_target_path(target_path); if (!inside_home(raw_actual)) throw fail("Target symlink points outside HOME: {0} -> {1}. Move it manually.", target_path, raw_actual); if (!Files.exists(target_path)) { log("\nRELINK {0}: broken symlink.", emphasize_global(target_path.toString())); remove_owned_symlink(target_path); } else { var actual = target_path.toRealPath(); if (actual.equals(expected)) { log("\nOK {0} -> {1}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString())); return; } if (!inside_home(actual)) throw fail("Target symlink points outside HOME: {0} -> {1}. Move it manually.", target_path, actual); else { if (!actual.startsWith(root.toRealPath())) throw fail("Target symlink points outside dorkfiles: {0} -> {1}. Move it manually.", target_path, actual); log("\nRELINK {0}: {1} -> {2}.", emphasize_global(target_path.toString()), emphasize_local(actual.toString()), emphasize_local(expected.toString())); remove_owned_symlink(target_path); } } } else if (Files.exists(target_path)) { var actual = target_path.toRealPath(); if (actual.equals(expected)) { log("\nOK {0} resolves to {1}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString())); return; } var backup = sibling_backup_path(target_path); var message = format("BACKUP {0} TO {1}.", target_path, backup); log("\n{0}", message); warn(message); Files.move(target_path, backup); } else { log("\nNOPE {0} exists, but it points into nirvana. Removing broken link!", target_path.toString()); Files.delete(target_path); } } log("\nLINK {0} TO {1}.", emphasize_local(source_path.toString()), emphasize_local(target_path.toString())); Files.createDirectories(target_path.getParent()); if (target_path.getParent().endsWith(".ssh")) chmod(target_path.getParent(), "rwx------"); Files.createSymbolicLink(target_path, source_path); } static void remove_owned_symlink(Path target_path) throws Throwable { if (!win32) { Files.delete(target_path); return; } var retired = sibling_path(target_path, "replaced"); Files.move(target_path, retired); try { Files.delete(retired); } catch (IOException error) { log("WIN32 kept retired link for later cleanup: {0}", retired); } } static Path symlink_target_path(Path target_path) throws IOException { var link = Files.readSymbolicLink(target_path); var resolved = link.isAbsolute() ? link : target_path.getParent().resolve(link); return resolved.toAbsolutePath().normalize(); } static Path sibling_backup_path(Path target_path) throws Throwable { return sibling_path(target_path, "backup"); } static Path sibling_path(Path target_path, String kind) throws Throwable { var stamp = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss").format(LocalDateTime.now()); var base = target_path.resolveSibling(target_path.getFileName() + ".dorkfiles-" + kind + "-" + stamp); var candidate = base; var suffix = 1; while (Files.exists(candidate, LinkOption.NOFOLLOW_LINKS)) { candidate = Path.of(base + "." + suffix); suffix++; } return candidate; } static Path expand_target(String target) throws Throwable { var path = switch (target) { case "~" -> target_home_path(); default -> target.startsWith("~/") ? target_home_path().resolve(target.substring(2)) : Path.of(target).toAbsolutePath(); }; path = path.normalize(); require_inside_home(path, "Symlink target"); return path; } static Path target_home_path() { return Path.of(System.getProperty("user.home")).toAbsolutePath().normalize(); } static Path home_path() { var home = System.getenv("HOME"); if (home == null || home.isBlank()) home = System.getProperty("user.home"); return Path.of(home).toAbsolutePath().normalize(); } static boolean inside_home(Path path) { return path.toAbsolutePath().normalize().startsWith(home_path()); } static void require_inside_home(Path path, String label) throws Throwable { if (!inside_home(path)) throw fail("{0} outside HOME: {1}", label, path.toAbsolutePath().normalize()); } static String hostname() throws Exception { var computername = System.getenv("COMPUTERNAME"); if (computername != null && !computername.isBlank()) return computername; var wsl_distro = System.getenv("WSL_DISTRO_NAME"); if (wsl_distro != null && !wsl_distro.isBlank()) return wsl_distro; var hostname = System.getenv("HOSTNAME"); if (hostname != null && !hostname.isBlank() && !"localhost".equals(hostname)) return hostname; var detected = InetAddress.getLocalHost().getHostName(); if ("localhost".equals(detected) && System.getenv("PREFIX") != null) return "termux"; return detected; } static void chmod(Path path, String permissions) throws IOException { if (win32) return; try { Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(permissions)); } catch (UnsupportedOperationException ignored) { } } static boolean starts_with(byte[] bytes, byte[] prefix) { if (bytes.length < prefix.length) return false; for (var index = 0; index < prefix.length; index++) if (bytes[index] != prefix[index]) return false; return true; } static Result run(Path directory, List command, byte[] input, boolean inherit_io) throws Throwable { return run(directory, command, input, inherit_io, true); } static Result run(Path directory, List command, byte[] input, boolean inherit_io, boolean fail) throws Throwable { var process_builder = new ProcessBuilder(command).directory(directory.toFile()); if (inherit_io) process_builder.inheritIO(); var process = process_builder.start(); if (input != null) { try (var stdin = process.getOutputStream()) { stdin.write(input); } } else { process.getOutputStream().close(); } var stdout = inherit_io ? new byte[0] : process.getInputStream().readAllBytes(); var stderr = inherit_io ? new byte[0] : process.getErrorStream().readAllBytes(); var exit = process.waitFor(); var result = new Result(exit, stdout, stderr); if (fail && exit != 0) throw fail("Command failed ({0}): {1}\n{2}", exit, String.join(" ", command), result.error().strip()); return result; } static void usage() { log("Bootstraps bugabingas {0}.", emphasize_local("dorkfiles")); log("{0}: java bootstripper.java [init-crypto|check|pre-commit|install-hooks|clean] [dotfiles root path]", emphasize_global("Usage")); } static String emphasize_global(String message) { return "\033[4m" + message + "\033[0m"; } static String emphasize_local(String message) { return "\033[3m" + message + "\033[0m"; } static void warn(String message) { warnings.add(message); } static void error(String message) { errors.add(message); } static void print_attention(boolean fatal) { if (warnings.isEmpty() && errors.isEmpty()) return; var color = fatal || !errors.isEmpty() ? "\033[31m" : "\033[33m"; System.out.println(); System.out.println(color + "ATTENTION"); for (var warning : warnings) System.out.println("- " + warning); for (var error : errors) System.out.println("- " + error); System.out.println("\033[0m"); } static Throwable fail(String message, Object... arguments) { error(format(message, arguments)); return fail(); } static Throwable fail() { print_attention(true); System.exit(-1); return new Throwable("use 'throw fail()' for control flow"); } static void log(String message, Object... arguments) { System.out.println(format(message, arguments)); } static interface ThrowUp { default Throwable gurgh(A a, B b) { try { apply(a, b); return null; } catch (Throwable vomit) { return vomit; } } void apply(A a, B b) throws Throwable; } static void for_each_mapping(List lines, ThrowUp block) throws Throwable { String source = null; String target = null; for (String line : lines) { if (line.isBlank()) continue; if (source == null) source = line; else target = line; if (target != null) { var vomit = block.gurgh(source, target); if (vomit != null) throw vomit; source = null; target = null; } } } static class Result { final int exit; final byte[] stdout; final byte[] stderr; Result(int exit, byte[] stdout, byte[] stderr) { this.exit = exit; this.stdout = stdout; this.stderr = stderr; } String text() { return new String(stdout, StandardCharsets.UTF_8); } String error() { return new String(stderr, StandardCharsets.UTF_8); } } }