|
| 1 | +package com.codeheadsystems.minidirectory.migration; |
| 2 | + |
| 3 | +import com.codeheadsystems.minidirectory.model.GrantSpec; |
| 4 | +import com.codeheadsystems.minidirectory.secret.Argon2Settings; |
| 5 | +import com.codeheadsystems.minidirectory.secret.Argon2SecretHasher; |
| 6 | +import com.codeheadsystems.minidirectory.secret.SecretHash; |
| 7 | +import com.codeheadsystems.minidirectory.service.DirectoryService; |
| 8 | +import com.codeheadsystems.minidirectory.store.DirectoryDocument; |
| 9 | +import com.codeheadsystems.minidirectory.store.JsonStore; |
| 10 | +import com.codeheadsystems.minidirectory.util.RandomIds; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import tools.jackson.databind.JsonNode; |
| 18 | +import tools.jackson.databind.ObjectMapper; |
| 19 | + |
| 20 | +/** |
| 21 | + * One-time migration of mini-idp's old client registry ({@code clients.json}) into mini-directory as |
| 22 | + * service accounts. Each client record becomes a {@code SERVICE_ACCOUNT} {@code Account}, preserving |
| 23 | + * its id (so issued token subjects are unchanged), its Argon2id secret hash (so existing client |
| 24 | + * secrets keep working — the hash is imported verbatim and verified later under its own recorded |
| 25 | + * parameters), its enabled flag, and its authorization mapped to mini-directory grants: the |
| 26 | + * control-plane flag becomes {@code admin}, and each {@code groups[].operations[]} becomes a |
| 27 | + * {@code GrantSpec(action = operation, resource = keyGroup)} — the generalized form mini-policy and |
| 28 | + * mini-idp's reconstruction agree on. |
| 29 | + * |
| 30 | + * <p>Idempotent: an id already present in the directory is skipped (so a re-run after a partial |
| 31 | + * migration is safe). Reads {@code clients.json} as plain JSON — no dependency on mini-idp — and |
| 32 | + * never logs secret material (only ids and counts). |
| 33 | + * |
| 34 | + * <pre> |
| 35 | + * mini-directory-migrate --clients-file ~/.mini-idp/clients.json --data-dir ~/.mini-directory |
| 36 | + * </pre> |
| 37 | + */ |
| 38 | +public final class ClientRegistryMigration { |
| 39 | + |
| 40 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 41 | + |
| 42 | + private ClientRegistryMigration() { |
| 43 | + } |
| 44 | + |
| 45 | + /** @param args {@code --clients-file <clients.json> --data-dir <directory data dir>}. */ |
| 46 | + public static void main(final String[] args) { |
| 47 | + try { |
| 48 | + final Path clientsFile = required(args, "--clients-file"); |
| 49 | + final Path dataDir = required(args, "--data-dir"); |
| 50 | + final Result result = run(clientsFile, dataDir); |
| 51 | + System.out.println("Migrated " + result.migrated() + " client(s) into " |
| 52 | + + dataDir.resolve("directory.json") + "; skipped " + result.skipped() + " already present."); |
| 53 | + } catch (final IllegalArgumentException e) { |
| 54 | + System.err.println("Migration error: " + e.getMessage()); |
| 55 | + System.exit(64); |
| 56 | + } catch (final IOException e) { |
| 57 | + System.err.println("I/O error: " + e.getMessage()); |
| 58 | + System.exit(74); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Run the migration. |
| 64 | + * |
| 65 | + * @param clientsFile the mini-idp {@code clients.json}. |
| 66 | + * @param dataDir the mini-directory data dir (holds/creates {@code directory.json}). |
| 67 | + * @return how many records were migrated and how many were skipped (already present). |
| 68 | + */ |
| 69 | + public static Result run(final Path clientsFile, final Path dataDir) throws IOException { |
| 70 | + final DirectoryService directory = new DirectoryService( |
| 71 | + new JsonStore<>(dataDir.resolve("directory.json"), DirectoryDocument.class), |
| 72 | + new Argon2SecretHasher(Argon2Settings.defaults()), new RandomIds()); |
| 73 | + |
| 74 | + final JsonNode root = MAPPER.readTree(Files.readAllBytes(clientsFile)); |
| 75 | + final JsonNode clients = root.get("clients"); |
| 76 | + int migrated = 0; |
| 77 | + int skipped = 0; |
| 78 | + if (clients != null) { |
| 79 | + for (final JsonNode client : clients) { |
| 80 | + if (importOne(directory, client)) { |
| 81 | + migrated++; |
| 82 | + } else { |
| 83 | + skipped++; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return new Result(migrated, skipped); |
| 88 | + } |
| 89 | + |
| 90 | + private static boolean importOne(final DirectoryService directory, final JsonNode client) { |
| 91 | + final String clientId = text(client, "clientId"); |
| 92 | + if (clientId == null) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + final JsonNode authorization = client.get("authorization"); |
| 96 | + final boolean admin = authorization != null && authorization.has("controlPlane") |
| 97 | + && authorization.get("controlPlane").asBoolean(); |
| 98 | + final List<GrantSpec> grants = grantsOf(authorization); |
| 99 | + final SecretHash secretHash = secretHashOf(client.get("secretHash")); |
| 100 | + final boolean enabled = !client.has("enabled") || client.get("enabled").asBoolean(); |
| 101 | + try { |
| 102 | + directory.importServiceAccount(clientId, text(client, "displayName"), admin, enabled, |
| 103 | + List.of(), List.of(), grants, secretHash); |
| 104 | + return true; |
| 105 | + } catch (final IllegalStateException alreadyExists) { |
| 106 | + return false; // idempotent re-run |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** Map a mini-idp {@code Authorization} ({control + groups[].operations}) to flat GrantSpecs. */ |
| 111 | + private static List<GrantSpec> grantsOf(final JsonNode authorization) { |
| 112 | + final List<GrantSpec> grants = new ArrayList<>(); |
| 113 | + if (authorization == null || authorization.get("grants") == null) { |
| 114 | + return grants; |
| 115 | + } |
| 116 | + for (final JsonNode group : authorization.get("grants")) { |
| 117 | + final String keyGroup = text(group, "keyGroup"); |
| 118 | + final JsonNode operations = group.get("operations"); |
| 119 | + if (keyGroup == null || operations == null) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + for (final JsonNode op : operations) { |
| 123 | + grants.add(new GrantSpec(op.asString(), keyGroup)); |
| 124 | + } |
| 125 | + } |
| 126 | + return grants; |
| 127 | + } |
| 128 | + |
| 129 | + private static SecretHash secretHashOf(final JsonNode node) { |
| 130 | + if (node == null) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + return new SecretHash(text(node, "algorithm"), text(node, "saltBase64"), text(node, "hashBase64"), |
| 134 | + node.get("memoryKiB").asInt(), node.get("iterations").asInt(), node.get("parallelism").asInt()); |
| 135 | + } |
| 136 | + |
| 137 | + private static String text(final JsonNode node, final String field) { |
| 138 | + return node.has(field) && !node.get(field).isNull() ? node.get(field).asString() : null; |
| 139 | + } |
| 140 | + |
| 141 | + private static Path required(final String[] args, final String flag) { |
| 142 | + for (int i = 0; i < args.length - 1; i++) { |
| 143 | + if (args[i].equals(flag)) { |
| 144 | + return Paths.get(args[i + 1]); |
| 145 | + } |
| 146 | + } |
| 147 | + throw new IllegalArgumentException("missing required flag " + flag); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * The migration outcome. |
| 152 | + * |
| 153 | + * @param migrated how many client records were imported as service accounts. |
| 154 | + * @param skipped how many were skipped because their id already existed (idempotent re-run). |
| 155 | + */ |
| 156 | + public record Result(int migrated, int skipped) { |
| 157 | + } |
| 158 | +} |
0 commit comments