Class VaultTesting

java.lang.Object
codes.grow.packages.vault.testing.VaultTesting

public final class VaultTesting extends Object
Vault server lifecycle helpers for tests and bootstrap scripts: init, unseal, seal and waiting for an active node. Not for production code.

 try (VaultClient c = VaultClient.builder().url("http://hashicorp-vault-init:8200").build()) {
   VaultTesting.InitResult init = VaultTesting.init(c, 1, 1);
   VaultTesting.unseal(c, init.keys().get(0));
   VaultTesting.waitForActive(c, Duration.ofSeconds(30));
 }
 
  • Method Details

    • initStatus

      public static boolean initStatus(VaultClient client)
      Whether the server is initialised: GET sys/init.
      Parameters:
      client - client of the server
      Returns:
      true when initialised
    • init

      public static VaultTesting.InitResult init(VaultClient client, int secretShares, int secretThreshold)
      Initialises a new Shamir-sealed server (PUT sys/init). An initialised server raises ValidationException ("already initialized"). Never retried.
      Parameters:
      client - client of the server
      secretShares - unseal key shares
      secretThreshold - shares needed to unseal
      Returns:
      the keys and root token
    • initRecovery

      public static VaultTesting.InitResult initRecovery(VaultClient client, int recoveryShares, int recoveryThreshold)
      Initialises a server with an auto-unseal (KMS / transit) seal using recovery shares.
      Parameters:
      client - client of the server
      recoveryShares - recovery key shares
      recoveryThreshold - recovery threshold
      Returns:
      the recovery keys and root token
    • unseal

      public static SealStatus unseal(VaultClient client, Secret key)
      Enters one unseal key (hex or base64): PUT sys/unseal.
      Parameters:
      client - client of the server
      key - unseal key
      Returns:
      the seal status after the key
    • resetUnseal

      public static SealStatus resetUnseal(VaultClient client)
      Discards the unseal keys entered so far (PUT sys/unseal {"reset": true}).
      Parameters:
      client - client of the server
      Returns:
      the seal status
    • seal

      public static void seal(VaultClient client)
      Seals the server (PUT sys/seal, needs a root or sudo token on the client).
      Parameters:
      client - client of the server
    • waitForActive

      public static HealthResponse waitForActive(VaultClient client, Duration timeout)
      Polls sys/health every 250 ms until the node is active (initialised, unsealed and leader).
      Parameters:
      client - client of the server
      timeout - maximum wait
      Returns:
      the health of the active node
      Throws:
      TimeoutException - when it does not become active in time