Class VaultTesting
java.lang.Object
codes.grow.packages.vault.testing.VaultTesting
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));
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record -
Method Summary
Modifier and TypeMethodDescriptionstatic VaultTesting.InitResultinit(VaultClient client, int secretShares, int secretThreshold) Initialises a new Shamir-sealed server (PUT sys/init).static VaultTesting.InitResultinitRecovery(VaultClient client, int recoveryShares, int recoveryThreshold) Initialises a server with an auto-unseal (KMS / transit) seal using recovery shares.static booleaninitStatus(VaultClient client) Whether the server is initialised:GET sys/init.static SealStatusresetUnseal(VaultClient client) Discards the unseal keys entered so far (PUT sys/unseal {"reset": true}).static voidseal(VaultClient client) Seals the server (PUT sys/seal, needs a root or sudo token on the client).static SealStatusunseal(VaultClient client, Secret key) Enters one unseal key (hex or base64):PUT sys/unseal.static HealthResponsewaitForActive(VaultClient client, Duration timeout) Pollssys/healthevery 250 ms until the node is active (initialised, unsealed and leader).
-
Method Details
-
initStatus
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 raisesValidationException("already initialized"). Never retried.- Parameters:
client- client of the serversecretShares- unseal key sharessecretThreshold- 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 serverrecoveryShares- recovery key sharesrecoveryThreshold- recovery threshold- Returns:
- the recovery keys and root token
-
unseal
Enters one unseal key (hex or base64):PUT sys/unseal.- Parameters:
client- client of the serverkey- unseal key- Returns:
- the seal status after the key
-
resetUnseal
Discards the unseal keys entered so far (PUT sys/unseal {"reset": true}).- Parameters:
client- client of the server- Returns:
- the seal status
-
seal
Seals the server (PUT sys/seal, needs a root or sudo token on the client).- Parameters:
client- client of the server
-
waitForActive
Pollssys/healthevery 250 ms until the node is active (initialised, unsealed and leader).- Parameters:
client- client of the servertimeout- maximum wait- Returns:
- the health of the active node
- Throws:
TimeoutException- when it does not become active in time
-