Repository navigation
Expand file tree
/
Copy pathLogical.java
More file actions
354 lines (328 loc) · 17 KB
/
Copy pathLogical.java
File metadata and controls
354 lines (328 loc) · 17 KB
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
package com.bettercloud.vault.api;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.VaultException;
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonArray;
import com.bettercloud.vault.json.JsonObject;
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;
/**
* <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;
public Logical(final VaultConfig config) {
this.config = config;
}
/**
* <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 {
return read(path, config.getToken());
}
public LogicalResponse read(final String path, String token) throws VaultException {
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + path)
.header("X-Vault-Token", token)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.get();
// Validate response
if (restResponse.getStatus() != 200) {
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
+ "\nResponse body: " + new String(restResponse.getBody(), "UTF-8"), restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount);
} catch (RuntimeException | VaultException | RestException | UnsupportedEncodingException e) {
if(shouldRetry == false)
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 {
return write(path, nameValuePairs, config.getToken());
}
public LogicalResponse write(final String path, final Map<String, Object> nameValuePairs, String token) 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 {
requestJson = requestJson.add(pair.getKey(), pair.getValue().toString());
}
}
}
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + path)
.body(requestJson.toString().getBytes("UTF-8"))
.header("X-Vault-Token", token)
.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) {
return new LogicalResponse(restResponse, retryCount);
} else {
throw new VaultException("Expecting HTTP status 204 or 200, but instead receiving " + restStatus
+ "\nResponse body: " + new String(restResponse.getBody(), "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 List<String> list(final String path) throws VaultException {
return list(path, config.getToken());
}
public List<String> list(final String path, String token) throws VaultException {
final String fullPath = path == null ? "list=true" : path + "?list=true";
LogicalResponse response = null;
try {
response = read(fullPath, token);
} catch (final VaultException e) {
if (e.getHttpStatusCode() != 404) {
throw e;
}
}
final List<String> returnValues = new ArrayList<>();
if (
response != null
&& response.getRestResponse().getStatus() != 404
&& response.getData() != null
&& response.getData().get("keys") != null
) {
final JsonArray keys = Json.parse(response.getData().get("keys")).asArray();
for (int index = 0; index < keys.size(); index++) {
returnValues.add(keys.get(index).asString());
}
}
return returnValues;
}
/**
* <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 {
return delete(path, config.getToken());
}
public LogicalResponse delete(final String path, String token) throws VaultException {
int retryCount = 0;
while (true) {
try {
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/" + path)
.header("X-Vault-Token", token)
.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(), "UTF-8"), restResponse.getStatus());
}
return new LogicalResponse(restResponse, retryCount);
} catch (RuntimeException | VaultException | RestException | UnsupportedEncodingException 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);
}
}
}
}
public List<String> getCapabilitiesSelf(final String path) throws VaultException {
return getCapabilitiesSelf(path, config.getToken());
}
public List<String> getCapabilitiesSelf(final String path, String token) throws VaultException {
final List<String> returnValues = new ArrayList<>();
LogicalResponse response = null;
try {
int retryCount = 0;
while (true) {
try {
final String payload = String.format("{\"path\":\"%s\"}", path);
// Make an HTTP request to Vault
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/capabilities-self")
.header("X-Vault-Token", token)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.body(payload.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(), "UTF-8"), restResponse.getStatus());
}
response = new LogicalResponse(restResponse, retryCount);
if (response != null
&& response.getRestResponse().getStatus() != 404
&& response.getData() != null
&& response.getData().get("capabilities") != null) {
final JsonArray keys = Json.parse(response.getData().get("capabilities")).asArray();
for (int index = 0; index < keys.size(); index++) {
returnValues.add(keys.get(index).asString());
}
}
return returnValues;
} catch (RuntimeException | VaultException | RestException | UnsupportedEncodingException 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);
}
}
}
} catch (final VaultException e) {
if (e.getHttpStatusCode() != 404) {
throw e;
}
}
return returnValues;
}
}