repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

bootstripper.java

Raw
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.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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 = ".git/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;

	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;
		}

		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);
		check(root);
		var symlinks_file = symlinks_file(root);
		create_symlinks(root, symlinks_file);
	}

	static boolean is_command(String argument) {
		return "bootstrap".equals(argument) || "clean".equals(argument) || "check".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;
		require_normal_gitdir(root);
		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);
		install_pre_commit_hook(root);
	}

	static void require_git_repo(Path root) throws Throwable {
		run(root, List.of("git", "rev-parse", "--is-inside-work-tree"), null, false);
	}

	static void require_normal_gitdir(Path root) throws Throwable {
		if (!Files.isDirectory(root.resolve(".git")))
			throw fail("Expected normal .git directory. Linked worktrees are not supported yet.");
	}

	static List<String> read_secrets(Path root) throws IOException {
		var path = root.resolve(secrets_file);
		if (!Files.exists(path))
			return List.of();
		var secrets = new ArrayList<String>();
		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 {
		run(root, List.of("git", "config", "--local", "filter." + filter_name + ".clean",
				"java tools/aes.java encrypt --key-file " + key_file), null, false);
		run(root, List.of("git", "config", "--local", "filter." + filter_name + ".smudge",
				"java tools/aes.java decrypt --key-file " + key_file), null, false);
		run(root, List.of("git", "config", "--local", "filter." + filter_name + ".required", "true"), null, false);
	}

	static void write_attributes(Path root, List<String> secrets) throws IOException {
		var attributes = root.resolve(".git/info/attributes");
		Files.createDirectories(attributes.getParent());
		var previous = Files.exists(attributes) ? Files.readString(attributes) : "";
		var lines = new ArrayList<String>();
		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 = root.resolve(key_file);
		if (Files.exists(key))
			return;
		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_file));
		} 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 encrypted = run(root, List.of("java", "tools/aes.java", "encrypt", "--key-file", key_file), 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", "tools/aes.java", "decrypt", "--key-file", key_file), 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<String> secrets) throws Throwable {
		var tracked = new ArrayList<String>();
		for (var secret : secrets) {
			var result = run(root, List.of("git", "ls-files", "--error-unmatch", "--", 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<String>();
		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<String> 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_pre_commit_hook(Path root) throws IOException {
		var hooks = root.resolve(".git/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 check\n", StandardCharsets.UTF_8);
		chmod(hook, "rwxr-xr-x");
	}

	static void check(Path root) throws Throwable {
		var secrets = read_secrets(root);
		if (secrets.isEmpty())
			return;
		check_filter(root);
		check_attributes(root, secrets);
		check_key_not_tracked(root);
		check_staged_secrets(root, secrets);
		check_no_tresor_refs(root);
		check_staged_plaintext_patterns(root);
		log("OK bootstripper check passed.");
	}

	static void check_filter(Path root) throws Throwable {
		var clean = run(root, List.of("git", "config", "--local", "--get", "filter." + filter_name + ".clean"), null, false).text().strip();
		var smudge = run(root, List.of("git", "config", "--local", "--get", "filter." + filter_name + ".smudge"), null, false).text().strip();
		var required = run(root, List.of("git", "config", "--local", "--get", "filter." + filter_name + ".required"), null, false).text().strip();
		if (!clean.equals("java tools/aes.java encrypt --key-file " + key_file))
			throw fail("Unexpected clean filter: {0}", clean);
		if (!smudge.equals("java tools/aes.java decrypt --key-file " + key_file))
			throw fail("Unexpected smudge filter: {0}", smudge);
		if (!required.equals("true"))
			throw fail("Filter is not required.");
	}

	static void check_attributes(Path root, List<String> 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_key_not_tracked(Path root) throws Throwable {
		var bad = run(root, List.of("git", "ls-files", "--", 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", "--", 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<String> 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", "tools/aes.java", "decrypt", "--key-file", key_file), blob, false);
		}
	}

	static void check_no_tresor_refs(Path root) throws IOException {
		try (var stream = Files.list(root)) {
			var files = stream.filter(path -> path.getFileName().toString().endsWith(".symlinks")).collect(Collectors.toList());
			for (var file : files) {
				var line_number = 0;
				for (var line : Files.readAllLines(file)) {
					line_number++;
					if (line.strip().startsWith("tresor/"))
						throw new IllegalStateException("tresor reference remains: " + file.getFileName() + ":" + line_number);
				}
			}
		}
	}

	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*\\\"[^<G][^\\\"]+\\\""),
				Pattern.compile("oauth_(access|refresh)_token\\s*=\\s*\\S+"),
				Pattern.compile("Authorization\\s*[:=]\\s*Bearer\\s+[A-Za-z0-9._-]{20,}", Pattern.CASE_INSENSITIVE));
		for (var pattern : patterns) {
			var matcher = pattern.matcher(diff);
			if (matcher.find())
				throw fail("Potential plaintext secret in staged diff: {0}", matcher.group());
		}
	}

	static Path symlinks_file(Path root) throws Throwable {
		var hostname = hostname();
		log("Hostname: {0}", emphasize_global(hostname));
		var symlinks_file = root.resolve(hostname + ".symlinks");
		if (!Files.exists(symlinks_file))
			throw fail("Expected a file containing symlinks at: {0}. Found nothing!", symlinks_file);
		return symlinks_file;
	}

	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 home = System.getProperty("user.home");
		var source_path = root.resolve(source).toAbsolutePath();
		var target_path = Path.of(target.replace("~", home)).toAbsolutePath();

		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 home = System.getProperty("user.home");
		var source_path = root.resolve(source).toAbsolutePath();
		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 target_path = Path.of(target.replace("~", home)).toAbsolutePath();
		if (Files.exists(target_path, LinkOption.NOFOLLOW_LINKS)) {
			if (Files.exists(target_path)) {
				log("\nOK {0} already exists!\nNot linking {1}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString()));
				return;
			} 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 String hostname() throws Exception {
		var computername = System.getenv("COMPUTERNAME");
		if (computername != null && !computername.isBlank())
			return computername;
		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<String> command, byte[] input, boolean inherit_io) throws Throwable {
		return run(directory, command, input, inherit_io, true);
	}

	static Result run(Path directory, List<String> 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|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 Throwable fail(String message, Object... arguments) {
		log(message, arguments);
		return fail();
	}

	static Throwable fail() {
		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<A, B> {
		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<String> lines, ThrowUp<String, String> 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);
		}
	}
}