-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathMounts.java
More file actions
384 lines (352 loc) · 17.5 KB
/
Mounts.java
File metadata and controls
384 lines (352 loc) · 17.5 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
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
package com.bettercloud.vault.api.mounts;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.VaultException;
import com.bettercloud.vault.response.MountResponse;
import com.bettercloud.vault.rest.Rest;
import com.bettercloud.vault.rest.RestResponse;
/**
* <p>The implementing class for operations on Vault's <code>/v1/sys/mounts/*</code> REST endpoints.</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 Mounts {
private final VaultConfig config;
public Mounts(final VaultConfig config) {
this.config = config;
}
/**
* <p>Operation to list all the mounted secrets engines. Relies on an authentication token being present in
* the <code>VaultConfig</code> instance.</p>
*
* <p>The list of mount points information will be populated in the <code>mounts</code> field of the <code>MountResponse</code>
* return value in the <code>Map<String, Mount></code> format. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final VaultConfig config = new VaultConfig.address(...).token(...).build();
* final Vault vault = new Vault(config);
*
* final MountResponse response = vault.mounts().list();
* final Map<String, Mount> mounts = response.getMounts();
* }</pre>
* </blockquote>
*
* @return A container for the information returned by Vault
*
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public MountResponse list() throws VaultException {
int retryCount = 0;
while (true) {
try {
final RestResponse restResponse = new Rest()//NOPMD
.url(String.format("%s/v1/sys/mounts", config.getAddress()))
.optionalHeader("X-Vault-Token", config.getToken())
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.get();
// Validate restResponse
if (restResponse.getStatus() != 200) {
String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)";
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus());
}
return new MountResponse(restResponse, retryCount, true);
} 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>Operation to enable secrets engine at given path. Relies on an authentication token being present in
* the <code>VaultConfig</code> instance.</p>
*
* <p>This method accepts a <code>MountConfig</code> parameter, containing optional settings for the mount
* creation operation. Example usage:</p>
*
* <p>A successful operation will return a 204 HTTP status. A <code>VaultException</code> will be thrown if
* mount point already exists, or if any other problem occurs. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final VaultConfig config = new VaultConfig.address(...).token(...).build();
* final Vault vault = new Vault(config);
*
* final MountPayload payload = new MountPayload()
* .defaultLeaseTtl(TimeToLive.of(86400, TimeUnit.SECONDS))
* .maxLeaseTtl(TimeToLive.of(86400, TimeUnit.SECONDS))
* .description("description for pki engine");
*
* final MountResponse response = vault.mounts().enable("pki/mount/point/path", MountType.PKI, payload);
*
* assertEquals(204, response.getRestResponse().getStatus();
* }</pre>
* </blockquote>
*
* @param path The path to enable secret engine on.
* @param type The type of secret engine to enable.
* @param payload The <code>MountPayload</code> instance to use to create secret engine.
*
* @return A container for the information returned by Vault
*
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public MountResponse enable(final String path, final MountType type, final MountPayload payload) throws VaultException {
int retryCount = 0;
while (true) {
try {
if (type == null) {
throw new VaultException("Mount type is missing");
}
if (payload == null) {
throw new VaultException("MountPayload is missing");
}
final String requestJson = payload.toEnableJson(type).toString();
final RestResponse restResponse = new Rest()//NOPMD
.url(String.format("%s/v1/sys/mounts/%s", config.getAddress(), path))
.optionalHeader("X-Vault-Token", config.getToken())
.body(requestJson.getBytes("UTF-8"))
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.post();
// Validate restResponse
if (restResponse.getStatus() != 204) {
String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)";
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus());
}
return new MountResponse(restResponse, retryCount, false);
} 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>Operation to disable secrets engine mount point of given path. Relies on an authentication token being
* present in the <code>VaultConfig</code> instance.</p>
*
* <p>A successful operation will return a 204 HTTP status. A <code>VaultException</code> will be thrown if
* the mount point not exist, or if any other problem occurs. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final VaultConfig config = new VaultConfig.address(...).token(...).build();
* final Vault vault = new Vault(config);
*
* final MountResponse response = vault.mounts().disable("pki/mount/point/path");
*
* assertEquals(204, response.getRestResponse().getStatus();
* }</pre>
* </blockquote>
*
* @param path The path to disable secret engine on.
*
* @return A container for the information returned by Vault
*
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public MountResponse disable(final String path) throws VaultException {
int retryCount = 0;
while (true) {
try {
final RestResponse restResponse = new Rest()//NOPMD
.url(String.format("%s/v1/sys/mounts/%s", config.getAddress(), path))
.optionalHeader("X-Vault-Token", config.getToken())
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.delete();
// Validate restResponse
if (restResponse.getStatus() != 204) {
String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)";
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus());
}
return new MountResponse(restResponse, retryCount, false);
} 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>Operation to read secrets engine mount point's configuration of given path. Relies on an authentication
* token being present in the <code>VaultConfig</code> instance.</p>
*
* <p>The mount point information will be populated in the <code>mount</code> field of the <code>MountResponse</code>
* return value. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final VaultConfig config = new VaultConfig.address(...).token(...).build();
* final Vault vault = new Vault(config);
*
* final MountResponse response = vault.mounts().read("pki/mount/point/path");
* final Mount mount = response.getMount();
* final MountConfig mountConfig = mount.getConfig();
* }</pre>
* </blockquote>
*
* @param path The path to read secret engine's configuration from.
*
* @return A container for the information returned by Vault
*
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public MountResponse read(final String path) throws VaultException {
int retryCount = 0;
while (true) {
try {
final RestResponse restResponse = new Rest()//NOPMD
.url(String.format("%s/v1/sys/mounts/%s/tune", config.getAddress(), path))
.optionalHeader("X-Vault-Token", config.getToken())
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.get();
// Validate restResponse
if (restResponse.getStatus() != 200 && restResponse.getStatus() != 404) {
String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)";
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus());
}
return new MountResponse(restResponse, retryCount, false);
} 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>Operation to tune secrets engine mount point's configuration of given path. Relies on an authentication
* token being present in the <code>VaultConfig</code> instance.</p>
*
* <p>This the method accepts a <code>MountConfig</code> parameter, containing optional settings for the mount
* tune operation. Example usage:</p>
*
* <p>A successful operation will return a 204 HTTP status. A <code>VaultException</code> will be thrown if
* the mount point not exist, or if any other problem occurs. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final VaultConfig config = new VaultConfig.address(...).token(...).build();
* final Vault vault = new Vault(config);
*
* final MountPayload payload = new MountPayload()
* .defaultLeaseTtl(TimeToLive.of(12, TimeUnit.HOURS))
* .maxLeaseTtl(TimeToLive.of(12, TimeUnit.HOURS))
* .description("description of pki");
*
* final MountResponse response = vault.mounts().tune("pki/mount/point/path", configs);
*
* assertEquals(204, response.getRestResponse().getStatus();
* }</pre>
* </blockquote>
*
* @param path The path to tune secret engine's configuration on.
* @param payload The <code>MountPayload</code> instance to use to tune secret engine.
*
* @return A container for the information returned by Vault
*
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public MountResponse tune(final String path, final MountPayload payload) throws VaultException {
int retryCount = 0;
while (true) {
try {
if (payload == null) {
throw new VaultException("MountPayload is missing");
}
final String requestJson = payload.toTuneJson().toString();
final RestResponse restResponse = new Rest()//NOPMD
.url(String.format("%s/v1/sys/mounts/%s/tune", config.getAddress(), path))
.optionalHeader("X-Vault-Token", config.getToken())
.body(requestJson.getBytes("UTF-8"))
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.post();
// Validate restResponse
if (restResponse.getStatus() != 204) {
String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)";
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus());
}
return new MountResponse(restResponse, retryCount, false);
} 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);
}
}
}
}
}