-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathLogical.java
647 lines (611 loc) · 33.4 KB
/
Logical.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package com.bettercloud.vault.api;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.VaultException;
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.json.JsonValue;
import com.bettercloud.vault.response.LogicalResponse;
import com.bettercloud.vault.rest.Rest;
import com.bettercloud.vault.rest.RestException;
import com.bettercloud.vault.rest.RestResponse;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForDelete;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForList;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForReadOrWrite;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForVersionDelete;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForVersionDestroy;
import static com.bettercloud.vault.api.LogicalUtilities.adjustPathForVersionUnDelete;
import static com.bettercloud.vault.api.LogicalUtilities.jsonObjectToWriteFromEngineVersion;
/**
* <p>The implementing class for Vault's core/logical operations (e.g. read, write).</p>
*
* <p>This class is not intended to be constructed directly. Rather, it is meant to used by way of <code>Vault</code>
* in a DSL-style builder pattern. See the Javadoc comments of each <code>public</code> method for usage examples.</p>
*/
public class Logical {
private final VaultConfig config;
private String nameSpace;
public enum logicalOperations {authentication, deleteV1, deleteV2, destroy, listV1, listV2, readV1, readV2, writeV1, writeV2, unDelete, mount}
public Logical(final VaultConfig config) {
this.config = config;
if (this.config.getNameSpace() != null && !this.config.getNameSpace().isEmpty()) {
this.nameSpace = this.config.getNameSpace();
}
}
/**
* <p>Adds the requested namespace to the logical operation, which is then passed into the REST headers for said operation.</p>
*
* @param nameSpace The Vault namespace to access (e.g. <code>secret/</code>).
* @return The Logical instance, with the namespace set.
*/
public Logical withNameSpace(final String nameSpace) {
this.nameSpace = nameSpace;
return this;
}
/**
* <p>Basic read operation to retrieve a secret. A single secret key can map to multiple name-value pairs,
* which can be retrieved from the response object. E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final LogicalResponse response = vault.logical().read("secret/hello");
*
* final String value = response.getData().get("value");
* final String otherValue = response.getData().get("other_value");
* }</pre>
* </blockquote>
*
* @param path The Vault key value from which to read (e.g. <code>secret/hello</code>)
* @return The response information returned from Vault
* @throws VaultException If any errors occurs with the REST request (e.g. non-200 status code, invalid JSON payload,
* etc), and the maximum number of retries is exceeded.
*/
public LogicalResponse read(final String path) throws VaultException {
if (this.engineVersionForSecretPath(path).equals(2)) {
return read(path, true, logicalOperations.readV2);
} else return read(path, true, logicalOperations.readV1);
}
private LogicalResponse read(final String path, Boolean shouldRetry, final logicalOperations operation)
throws VaultException {
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(path, config.getPrefixPathDepth(), operation))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.get();
// Validate response - don't treat 4xx class errors as exceptions, we want to return an error as the response
if (restResponse.getStatus() != 200 && !(restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, operation);
} catch (RuntimeException | VaultException | RestException e) {
if (!shouldRetry)
throw new VaultException(e);
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Basic read operation to retrieve a specified secret version for KV engine version 2. A single secret key version
* can map to multiple name-value pairs, which can be retrieved from the response object. E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final LogicalResponse response = vault.logical().read("secret/hello", true, 1);
*
* final String value = response.getData().get("value");
* final String otherValue = response.getData().get("other_value");
* }</pre>
* </blockquote>
*
* @param path The Vault key value from which to read (e.g. <code>secret/hello</code>
* @param shouldRetry Whether to try more than once
* @param version The Integer version number of the secret to read, e.g. "1"
* @return The response information returned from Vault
* @throws VaultException If any errors occurs with the REST request (e.g. non-200 status code, invalid JSON payload,
* etc), and the maximum number of retries is exceeded.
*/
public LogicalResponse read(final String path, Boolean shouldRetry, final Integer version) throws VaultException {
if (this.engineVersionForSecretPath(path) != 2) {
throw new VaultException("Version reads are only supported in KV Engine version 2.");
}
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(path, config.getPrefixPathDepth(), logicalOperations.readV2))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.parameter("version", version.toString())
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.get();
// Validate response - don't treat 4xx class errors as exceptions, we want to return an error as the response
if (restResponse.getStatus() != 200 && !(restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, logicalOperations.readV2);
} catch (RuntimeException | VaultException | RestException e) {
if (!shouldRetry)
throw new VaultException(e);
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Basic operation to store secrets. Multiple name value pairs can be stored under the same secret key.
* E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final Map<String, String> nameValuePairs = new HashMap<String, Object>();
* nameValuePairs.put("value", "foo");
* nameValuePairs.put("other_value", "bar");
*
* final LogicalResponse response = vault.logical().write("secret/hello", nameValuePairs);
* }</pre>
* </blockquote>
*
* <p>The values in these name-value pairs may be booleans, numerics, strings, or nested JSON objects. However,
* be aware that this method does not recursively parse any nested structures. If you wish to write arbitrary
* JSON objects to Vault... then you should parse them to JSON outside of this method, and pass them here as JSON
* strings.</p>
*
* @param path The Vault key value to which to write (e.g. <code>secret/hello</code>)
* @param nameValuePairs Secret name and value pairs to store under this Vault key (can be <code>null</code> for
* writing to keys that do not need or expect any fields to be specified)
* @return The response information received from Vault
* @throws VaultException If any errors occurs with the REST request, and the maximum number of retries is exceeded.
*/
public LogicalResponse write(final String path, final Map<String, Object> nameValuePairs) throws VaultException {
if (engineVersionForSecretPath(path).equals(2)) {
return write(path, nameValuePairs, logicalOperations.writeV2);
} else return write(path, nameValuePairs, logicalOperations.writeV1);
}
private LogicalResponse write(final String path, final Map<String, Object> nameValuePairs,
final logicalOperations operation) throws VaultException {
int retryCount = 0;
while (true) {
try {
JsonObject requestJson = Json.object();
if (nameValuePairs != null) {
for (final Map.Entry<String, Object> pair : nameValuePairs.entrySet()) {
final Object value = pair.getValue();
if (value == null) {
requestJson = requestJson.add(pair.getKey(), (String) null);
} else if (value instanceof Boolean) {
requestJson = requestJson.add(pair.getKey(), (Boolean) pair.getValue());
} else if (value instanceof Integer) {
requestJson = requestJson.add(pair.getKey(), (Integer) pair.getValue());
} else if (value instanceof Long) {
requestJson = requestJson.add(pair.getKey(), (Long) pair.getValue());
} else if (value instanceof Float) {
requestJson = requestJson.add(pair.getKey(), (Float) pair.getValue());
} else if (value instanceof Double) {
requestJson = requestJson.add(pair.getKey(), (Double) pair.getValue());
} else if (value instanceof JsonValue) {
requestJson = requestJson.add(pair.getKey(), (JsonValue) pair.getValue());
} else {
requestJson = requestJson.add(pair.getKey(), pair.getValue().toString());
}
}
}
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(path, config.getPrefixPathDepth(), operation))
.body(jsonObjectToWriteFromEngineVersion(operation, requestJson).toString().getBytes(StandardCharsets.UTF_8))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.post();
// HTTP Status should be either 200 (with content - e.g. PKI write) or 204 (no content)
final int restStatus = restResponse.getStatus();
if (restStatus == 200 || restStatus == 204 || (restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
return new LogicalResponse(restResponse, retryCount, operation);
} else {
throw new VaultException("Expecting HTTP status 204 or 200, but instead receiving " + restStatus
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8), restStatus);
}
} catch (Exception e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Retrieve a list of keys corresponding to key/value pairs at a given Vault path.</p>
*
* <p>Key values ending with a trailing-slash characters are sub-paths. Running a subsequent <code>list()</code>
* call, using the original path appended with this key, will retrieve all secret keys stored at that sub-path.</p>
*
* <p>This method returns only the secret keys, not values. To retrieve the actual stored value for a key,
* use <code>read()</code> with the key appended onto the original base path.</p>
*
* @param path The Vault key value at which to look for secrets (e.g. <code>secret</code>)
* @return A list of keys corresponding to key/value pairs at a given Vault path, or an empty list if there are none
* @throws VaultException If any errors occur, or unexpected response received from Vault
*/
public LogicalResponse list(final String path) throws VaultException {
if (engineVersionForSecretPath(path).equals(2)) {
return list(path, logicalOperations.listV2);
} else return list(path, logicalOperations.listV1);
}
private LogicalResponse list(final String path, final logicalOperations operation) throws VaultException {
LogicalResponse response = null;
try {
response = read(adjustPathForList(path, config.getPrefixPathDepth(), operation), true, operation);
} catch (final VaultException e) {
if (e.getHttpStatusCode() != 404) {
throw e;
}
}
return response;
}
/**
* <p>Deletes the key/value pair located at the provided path.</p>
*
* <p>If the path represents a sub-path, then all of its contents must be deleted prior to deleting the empty
* sub-path itself.</p>
*
* @param path The Vault key value to delete (e.g. <code>secret/hello</code>).
* @return The response information received from Vault
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
public LogicalResponse delete(final String path) throws VaultException {
if (engineVersionForSecretPath(path).equals(2)) {
return delete(path, logicalOperations.deleteV2);
} else return delete(path, logicalOperations.deleteV1);
}
private LogicalResponse delete(final String path, final Logical.logicalOperations operation) throws VaultException {
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForDelete(path, config.getPrefixPathDepth(), operation))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.delete();
// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, operation);
} catch (RuntimeException | VaultException | RestException e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Soft deletes the specified version of the key/value pair located at the provided path.</p>
* <p>
* Only supported for KV Engine version 2. If the data is desired, it can be recovered with a matching unDelete operation.
*
* <p>If the path represents a sub-path, then all of its contents must be deleted prior to deleting the empty
* sub-path itself.</p>
*
* @param path The Vault key value to delete (e.g. <code>secret/hello</code>).
* @param versions An array of Integers corresponding to the versions you wish to delete, e.g. [1, 2] etc.
* @return The response information received from Vault
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
public LogicalResponse delete(final String path, final int[] versions) throws VaultException {
if (this.engineVersionForSecretPath(path) != 2) {
throw new VaultException("Version deletes are only supported for KV Engine 2.");
}
intArrayCheck(versions);
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
JsonObject versionsToDelete = new JsonObject().add("versions", versions);
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForVersionDelete(path,config.getPrefixPathDepth()))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.body(versionsToDelete.toString().getBytes(StandardCharsets.UTF_8))
.post();
// Validate response
return getLogicalResponse(retryCount, restResponse);
} catch (RuntimeException | VaultException | RestException e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
private LogicalResponse getLogicalResponse(int retryCount, RestResponse restResponse) throws VaultException {
if (restResponse.getStatus() != 204) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, logicalOperations.deleteV2);
}
private void intArrayCheck(int[] versions) {
for (int i : versions) {
if (i < 1) {
throw new IllegalArgumentException("The document version must be 1 or greater.");
}
}
Arrays.sort(versions);
}
/**
* <p>Recovers a soft delete of the specified version of the key/value pair located at the provided path.</p>
* <p>
* Only supported for KV Engine version 2.
*
* @param path The Vault key value to undelete (e.g. <code>secret/hello</code>).
* @param versions An array of Integers corresponding to the versions you wish to undelete, e.g. [1, 2] etc.
* @return The response information received from Vault
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
public LogicalResponse unDelete(final String path, final int[] versions) throws VaultException {
if (this.engineVersionForSecretPath(path) != 2) {
throw new VaultException("Version undeletes are only supported for KV Engine 2.");
}
intArrayCheck(versions);
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
JsonObject versionsToUnDelete = new JsonObject().add("versions", versions);
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForVersionUnDelete(path,config.getPrefixPathDepth()))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.body(versionsToUnDelete.toString().getBytes(StandardCharsets.UTF_8))
.post();
// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, logicalOperations.unDelete);
} catch (RuntimeException | VaultException | RestException e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Performs a hard delete of the specified version of the key/value pair located at the provided path.</p>
* <p>
* Only supported for KV Engine version 2. There are no recovery options for the specified version of the data deleted
* in this method.
*
* @param path The Vault key value to destroy (e.g. <code>secret/hello</code>).
* @param versions An array of Integers corresponding to the versions you wish to destroy, e.g. [1, 2] etc.
* @return The response information received from Vault
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
public LogicalResponse destroy(final String path, final int[] versions) throws VaultException {
if (this.engineVersionForSecretPath(path) != 2) {
throw new VaultException("Secret destroys are only supported for KV Engine 2.");
}
intArrayCheck(versions);
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
JsonObject versionsToDestroy = new JsonObject().add("versions", versions);
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + adjustPathForVersionDestroy(path,config.getPrefixPathDepth()))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.body(versionsToDestroy.toString().getBytes(StandardCharsets.UTF_8))
.post();
// Validate response
return getLogicalResponse(retryCount, restResponse);
} catch (RuntimeException | VaultException | RestException e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
/**
* <p>Performs an upgrade of the secrets engine version of the specified KV store to version 2.</p>
* <p>
* There is no downgrading this operation back to version 1.
*
* @param kvPath The Vault kv engine to upgrade (e.g. <code>secret/</code>).
* @return The response information received from Vault
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
public LogicalResponse upgrade(final String kvPath) throws VaultException {
if (this.engineVersionForSecretPath(kvPath) == 2) {
throw new VaultException("This KV engine is already version 2.");
}
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
JsonObject kvToUpgrade = new JsonObject().add("options", new JsonObject().add("version", 2));
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/mounts/" + (kvPath.replaceAll("/", "") + "/tune"))
.header("X-Vault-Token", config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.body(kvToUpgrade.toString().getBytes(StandardCharsets.UTF_8))
.post();
// Validate response
if (restResponse.getStatus() != 200) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount, logicalOperations.authentication);
} catch (RuntimeException | VaultException | RestException e) {
// If there are retries to perform, then pause for the configured interval and then execute the loop again...
if (retryCount < config.getMaxRetries()) {
retryCount++;
try {
final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds();
Thread.sleep(retryIntervalMilliseconds);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if (e instanceof VaultException) {
// ... otherwise, give up.
throw (VaultException) e;
} else {
throw new VaultException(e);
}
}
}
}
private Integer engineVersionForSecretPath(final String secretPath) {
if (!this.config.getSecretsEnginePathMap().isEmpty()) {
return this.config.getSecretsEnginePathMap().containsKey(secretPath + "/") ?
Integer.valueOf(this.config.getSecretsEnginePathMap().get(secretPath + "/"))
: this.config.getGlobalEngineVersion();
}
return this.config.getGlobalEngineVersion();
}
/**
* <p>Provides the version of the secrets engine of the specified path, e.g. 1 or 2.</p>
* First checks if the Vault config secrets engine path map contains the path.
* If not, then defaults to the Global Engine version fallback.
* <p>
*
* @param path The Vault secret path to check (e.g. <code>secret/</code>).
* @return The response information received from Vault
*/
public Integer getEngineVersionForSecretPath(final String path) {
return this.engineVersionForSecretPath(path);
}
}