Skip to content

Usage Connection

Stefan Kalscheuer edited this page Oct 15, 2021 · 4 revisions

Java Vault Connector

Usage Examples

Connection

The package features an HTTP connector by default. To establish connection to your Vault cluster, the connector needs to be instantiated with the relevant parameters.

To do so, use the builder to configure your connector.

Simple instantiation

 // Instantiate using builder pattern style factory (TLS enabled by default)
 VaultConnector connector = HTTPVaultConnector.builder()
   .withHost("vault.example.com")   // Default: 127.0.0.1
   .withPort(8200)                  // Default: 8200
   .withTLS()                       // Default. Possible without TLS and with explicit version.
   .build();

Provide custom CA certificate

For internal sites or to enforce a specific CA you might provide a custom CA certificate to trust as Path or X509Certificate.

 VaultConnector connector = HTTPVaultConnector.builder()
   .withHost("vault.example.com")
   .withPort(8200)
   .withTrustedCA(Paths.get("/path/to/CA.pem"))
   .build();

Configuration from environment variables

It is also possible to provide the configuration externally through environment variables. This feature supports the default Vault environment variables:

  • VAULT_ADDR - URL to Vault cluster (e.g. https://vault.example.com:8200)
  • VAULT_CACERT - Path to custom CA certificate
  • VAULT_MAX_RETRIES - Maximum number of retries on connection failure
  • VAULT_TOKEN - Token for automatic authentication.
 VaultConnector connector = HTTPVaultConnector.builder()
   .fromEnv()
   .build();

 // Or with automatic authentication.
 VaultConnector connector = HTTPVaultConnector.builder()
   .fromEnv()
   .buildAndAuth();