-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathLeases.java
More file actions
273 lines (255 loc) · 12.9 KB
/
Leases.java
File metadata and controls
273 lines (255 loc) · 12.9 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
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.response.VaultResponse;
import com.bettercloud.vault.rest.Rest;
import com.bettercloud.vault.rest.RestResponse;
import java.nio.charset.StandardCharsets;
/**
* <p>The implementing class for operations on REST endpoints, under the "Leases" section of the Vault HTTP API
* docs (https://www.vaultproject.io/docs/http/index.html).</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 Leases {
private final VaultConfig config;
private String nameSpace;
public Leases(final VaultConfig config) {
this.config = config;
if (this.config.getNameSpace() != null && !this.config.getNameSpace().isEmpty()) {
this.nameSpace = this.config.getNameSpace();
}
}
public Leases withNameSpace(final String nameSpace) {
this.nameSpace = nameSpace;
return this;
}
/**
* <p>Immediately revokes a secret associated with a given lease. E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final VaultResponse response = vault.leases().revoke("7c63da27-a56b-3e3b-377d-ef74630a6d0b");
* assertEquals(204, response.getRestResponse().getStatus());
* }</pre>
* </blockquote>
*
* @param leaseId A lease ID associated with the secret to be revoked
* @return The response information returned from Vault
* @throws VaultException If an error occurs, or unexpected reponse received from Vault
*/
public VaultResponse revoke(final String leaseId) throws VaultException {
int retryCount = 0;
while (true) {
try {
/**
* 2019-03-21
* Changed the Lease revoke url due to invalid path. Vault deprecated the original
* path (/v1/sys/revoke) in favor of a new leases mount point (/v1/sys/leases/revoke)
* https://github.com/hashicorp/vault/blob/master/CHANGELOG.md#080-august-9th-2017
*/
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/leases/revoke/" + leaseId)
.optionalHeader("X-Vault-Token", config.getToken())
.optionalHeader("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.put();
// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException("Expecting HTTP status 204, but instead receiving " + restResponse.getStatus(), restResponse.getStatus());
}
return new VaultResponse(restResponse, retryCount);
} 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>Revokes all secrets (via a lease ID prefix) or tokens (via the tokens' path property) generated under a
* given prefix immediately. This requires sudo capability and access to it should be tightly controlled as it
* can be used to revoke very large numbers of secrets/tokens at once. E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final VaultResponse response = vault.leases().revokePrefix("aws");
* assertEquals(204, response.getRestResponse().getStatus());
* }</pre>
* </blockquote>
*
* @param prefix A Vault path prefix, for which all secrets beneath it should be revoked
* @return The response information returned from Vault
* @throws VaultException If an error occurs, or unexpected reponse received from Vault
*/
public VaultResponse revokePrefix(final String prefix) throws VaultException {
int retryCount = 0;
while (true) {
try {
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/revoke-prefix/" + prefix)
.optionalHeader("X-Vault-Token", config.getToken())
.optionalHeader("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.put();
// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException("Expecting HTTP status 204, but instead receiving " + restResponse.getStatus(), restResponse.getStatus());
}
return new VaultResponse(restResponse, retryCount);
} 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>Revokes all secrets or tokens generated under a given prefix immediately. Unlike revokePrefix(String),
* this method ignores backend errors encountered during revocation. This is potentially very dangerous and should
* only be used in specific emergency situations where errors in the backend or the connected backend service
* prevent normal revocation. By ignoring these errors, Vault abdicates responsibility for ensuring that the
* issued credentials or secrets are properly revoked and/or cleaned up. Access to this endpoint should be tightly
* controlled. E.g.:</p>
*
* <blockquote>
* <pre>{@code
* final VaultResponse response = vault.leases().revokePrefix("aws");
* assertEquals(204, response.getRestResponse().getStatus());
* }</pre>
* </blockquote>
*
* @param prefix A Vault path prefix, for which all secrets beneath it should be revoked
* @return The response information returned from Vault
* @throws VaultException If an error occurs, or unexpected reponse received from Vault
*/
public VaultResponse revokeForce(final String prefix) throws VaultException {
int retryCount = 0;
while (true) {
try {
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/revoke-force/" + prefix)
.optionalHeader("X-Vault-Token", config.getToken())
.optionalHeader("X-Vault-Namespace", this.nameSpace)
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.put();
// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException("Expecting HTTP status 204, but instead receiving " + restResponse.getStatus(), restResponse.getStatus());
}
return new VaultResponse(restResponse, retryCount);
} 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>Renews a given secret lease.</p>
*
* <blockquote>
* <pre>{@code
* final VaultResponse response = vault.leases().renew("mongodb/creds/myapp/cd7f9834-b870-9ebc-3da5-27bf9cdc42ad");
* assertEquals(200, response.getRestResponse().getStatus());
* }</pre>
* </blockquote>
*
* @param leaseId A lease ID associated with a secret
* @param increment A requested amount of time in seconds to extend the lease. This is advisory.
* @return The response information returned from Vault
* @throws VaultException The response information returned from Vault
*/
public VaultResponse renew(final String leaseId, final long increment) throws VaultException {
// TODO: Update the integration test suite to provide coverate for this
// The "generic" backend does not support support lease renewal. The only other backend
// available when we were using Vault in "dev mode" was the "pki" backend, which does
// support renewal of credentials, etc. But lease renewal in this context is talking about
// secrets. Now that the integration tests use a "real" Vault instance hosted in a Docker
// container, we can revisit this.
int retryCount = 0;
while (true) {
try {
final String requestJson = Json.object().add("increment", increment).toString();
final RestResponse restResponse = new Rest()//NOPMD
.url(config.getAddress() + "/v1/sys/renew/" + leaseId)
.optionalHeader("X-Vault-Token", config.getToken())
.optionalHeader("X-Vault-Namespace", this.nameSpace)
.body(increment < 0 ? null : requestJson.getBytes(StandardCharsets.UTF_8))
.connectTimeoutSeconds(config.getOpenTimeout())
.readTimeoutSeconds(config.getReadTimeout())
.sslVerification(config.getSslConfig().isVerify())
.sslContext(config.getSslConfig().getSslContext())
.post();
// Validate response
if (restResponse.getStatus() != 200) {
throw new VaultException("Expecting HTTP status 200, but instead receiving " + restResponse.getStatus(), restResponse.getStatus());
}
return new VaultResponse(restResponse, retryCount);
} catch (Exception e) {
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);
}
}
}
}
}