|
6 | 6 | import com.bettercloud.vault.api.Logical; |
7 | 7 | import com.bettercloud.vault.api.Seal; |
8 | 8 | import com.bettercloud.vault.api.pki.Pki; |
| 9 | +import com.bettercloud.vault.json.Json; |
| 10 | +import com.bettercloud.vault.json.JsonObject; |
| 11 | +import com.bettercloud.vault.json.JsonValue; |
| 12 | +import com.bettercloud.vault.rest.Rest; |
| 13 | +import com.bettercloud.vault.rest.RestException; |
| 14 | +import com.bettercloud.vault.rest.RestResponse; |
| 15 | + |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
9 | 19 |
|
10 | 20 | /** |
11 | 21 | * <p>The Vault driver class, the primary interface through which dependent applications will access Vault.</p> |
@@ -50,16 +60,72 @@ public class Vault { |
50 | 60 | * Construct a Vault driver instance with the provided config settings. |
51 | 61 | * |
52 | 62 | * @param vaultConfig Configuration settings for Vault interaction (e.g. server address, token, etc) |
| 63 | + * If the VaultConfig Engine version path map is not supplied in the config, default to global KV |
| 64 | + * engine version 2. |
53 | 65 | */ |
54 | 66 | public Vault(final VaultConfig vaultConfig) { |
55 | 67 | this.vaultConfig = vaultConfig; |
| 68 | + if (this.vaultConfig.getNameSpace() != null && !this.vaultConfig.getNameSpace().isEmpty()) { |
| 69 | + System.out.println(String.format("The NameSpace %s has been bound to this Vault instance. Please keep this in mind when running operations.", this.vaultConfig.getNameSpace())); |
| 70 | + } |
| 71 | + if (this.vaultConfig.getSecretsEnginePathMap().isEmpty() && this.vaultConfig.getGlobalEngineVersion() == null) { |
| 72 | + System.out.println("Constructing a Vault instance with no provided Engine version, defaulting to version 2."); |
| 73 | + this.vaultConfig.setEngineVersion(2); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Construct a Vault driver instance with the provided config settings, and use the provided global KV Engine version for all secrets. |
| 79 | + */ |
| 80 | + public Vault(final VaultConfig vaultConfig, final Integer engineVersion) { |
| 81 | + if (engineVersion < 1 || engineVersion > 2) { |
| 82 | + throw new IllegalArgumentException("The Engine version must be '1' or '2', the version supplied was: '" |
| 83 | + + engineVersion + "'."); |
| 84 | + } |
| 85 | + vaultConfig.setEngineVersion(engineVersion); |
| 86 | + this.vaultConfig = vaultConfig; |
| 87 | + if (this.vaultConfig.getNameSpace() != null && !this.vaultConfig.getNameSpace().isEmpty()) { |
| 88 | + System.out.println(String.format("The Namespace %s has been bound to this Vault instance. Please keep this in mind when running operations.", this.vaultConfig.getNameSpace())); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Construct a Vault driver instance with the provided config settings. |
| 94 | + * |
| 95 | + * @param vaultConfig Configuration settings for Vault interaction (e.g. server address, token, etc) |
| 96 | + * If the Secrets engine version path map is not provided, or does not contain the |
| 97 | + * requested secret, fall back to the global version supplied. |
| 98 | + * @param useSecretsEnginePathMap Whether to use a provided KV Engine version map from the Vault config, or generate one. |
| 99 | + * If a secrets KV Engine version map is not supplied, use Vault APIs to determine the |
| 100 | + * KV Engine version for each secret. This call requires admin rights. |
| 101 | + * @param globalFallbackVersion The Integer version of the KV Engine to use as a global fallback. |
| 102 | + */ |
| 103 | + public Vault(final VaultConfig vaultConfig, final Boolean useSecretsEnginePathMap, final Integer globalFallbackVersion) |
| 104 | + throws VaultException { |
| 105 | + this.vaultConfig = vaultConfig; |
| 106 | + if (this.vaultConfig.getNameSpace() != null && !this.vaultConfig.getNameSpace().isEmpty()) { |
| 107 | + System.out.println(String.format("The Namespace %s has been bound to this Vault instance. Please keep this in mind when running operations.", this.vaultConfig.getNameSpace())); |
| 108 | + } |
| 109 | + this.vaultConfig.setEngineVersion(globalFallbackVersion); |
| 110 | + if (useSecretsEnginePathMap && this.vaultConfig.getSecretsEnginePathMap().isEmpty()) { |
| 111 | + try { |
| 112 | + System.out.println("No secrets Engine version map was supplied, attempting to generate one."); |
| 113 | + final Map<String, String> secretsEnginePathMap = collectSecretEngineVersions(); |
| 114 | + assert secretsEnginePathMap != null; |
| 115 | + this.vaultConfig.getSecretsEnginePathMap().putAll(secretsEnginePathMap); |
| 116 | + } catch (Exception e) { |
| 117 | + throw new VaultException(String.format("An Engine KV version map was not supplied, and unable to determine " + |
| 118 | + "KV Engine " + |
| 119 | + "version, " + "due to exception: %s", e.getMessage() + ". Do you have admin rights?")); |
| 120 | + } |
| 121 | + } |
56 | 122 | } |
57 | 123 |
|
58 | 124 | /** |
59 | 125 | * This method is chained ahead of endpoints (e.g. <code>logical()</code>, <code>auth()</code>, |
60 | 126 | * etc... to specify retry rules for any API operations invoked on that endpoint. |
61 | 127 | * |
62 | | - * @param maxRetries The number of times that API operations will be retried when a failure occurs |
| 128 | + * @param maxRetries The number of times that API operations will be retried when a failure occurs |
63 | 129 | * @param retryIntervalMilliseconds The number of milliseconds that the driver will wait in between retries |
64 | 130 | * @return This object, with maxRetries and retryIntervalMilliseconds populated |
65 | 131 | */ |
@@ -146,4 +212,61 @@ public Debug debug() { |
146 | 212 | public Seal seal() { |
147 | 213 | return new Seal(vaultConfig); |
148 | 214 | } |
| 215 | + |
| 216 | + /** |
| 217 | + * Makes a REST call to Vault, to collect information on which secret engine version (if any) is used by each available |
| 218 | + * mount point. Possibilities are: |
| 219 | + * |
| 220 | + * <ul> |
| 221 | + * <li>"2" - A mount point running on Vault 0.10 or higher, configured to use the engine 2 API</li> |
| 222 | + * <li>"1" - A mount point running on Vault 0.10 or higher, configured to use the engine 1 API</li> |
| 223 | + * <li>"unknown" - A mount point running on an older version of Vault. Can more or less be treated as "1".</li> |
| 224 | + * </ul> |
| 225 | + * <p> |
| 226 | + * IMPORTANT: Whichever authentication mechanism is being used with the <code>VaultConfig</code> object, that principal |
| 227 | + * needs permission to access the <code>/v1/sys/mounts</code> REST endpoint. |
| 228 | + * |
| 229 | + * @return A map of mount points (e.g. "/secret") to secret engine version numbers (e.g. "2") |
| 230 | + */ |
| 231 | + private Map<String, String> collectSecretEngineVersions() { |
| 232 | + try { |
| 233 | + final RestResponse restResponse = new Rest()//NOPMD |
| 234 | + .url(vaultConfig.getAddress() + "/v1/sys/mounts") |
| 235 | + .header("X-Vault-Token", vaultConfig.getToken()) |
| 236 | + .optionalHeader("X-Vault-Namespace", this.vaultConfig.getNameSpace()) |
| 237 | + .connectTimeoutSeconds(vaultConfig.getOpenTimeout()) |
| 238 | + .readTimeoutSeconds(vaultConfig.getReadTimeout()) |
| 239 | + .sslVerification(vaultConfig.getSslConfig().isVerify()) |
| 240 | + .sslContext(vaultConfig.getSslConfig().getSslContext()) |
| 241 | + .get(); |
| 242 | + if (restResponse.getStatus() != 200) { |
| 243 | + return null; |
| 244 | + } |
| 245 | + |
| 246 | + final String jsonString = new String(restResponse.getBody(), StandardCharsets.UTF_8); |
| 247 | + final Map<String, String> data = new HashMap<>(); |
| 248 | + final JsonObject jsonData = Json.parse(jsonString).asObject().get("data").asObject(); |
| 249 | + for (JsonObject.Member member : jsonData) { |
| 250 | + final String name = member.getName(); |
| 251 | + String version = "unknown"; |
| 252 | + |
| 253 | + final JsonValue options = member.getValue().asObject().get("options"); |
| 254 | + if (options != null && options.isObject()) { |
| 255 | + final JsonValue ver = options.asObject().get("version"); |
| 256 | + if (ver != null && ver.isString()) { |
| 257 | + version = ver.asString(); |
| 258 | + } |
| 259 | + } |
| 260 | + data.put(name, version); |
| 261 | + } |
| 262 | + return data; |
| 263 | + } catch (RestException e) { |
| 264 | + System.err.print(String.format("Unable to retrieve the KV Engine secrets, due to exception: %s", e.getMessage())); |
| 265 | + return null; |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + public Map<String, String> getSecretEngineVersions() { |
| 270 | + return this.collectSecretEngineVersions(); |
| 271 | + } |
149 | 272 | } |
0 commit comments