Skip to content

Commit e62bd9d

Browse files
authored
add possibility to pass preconfigured http client to reuse http/tls resources for consequent requests (#44)
1 parent b1a53b7 commit e62bd9d

9 files changed

Lines changed: 214 additions & 48 deletions

File tree

src/main/java/io/github/jopenlibs/vault/VaultConfig.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.jopenlibs.vault;
22

33
import java.io.Serializable;
4+
import java.net.http.HttpClient;
45
import java.util.Map;
56
import java.util.concurrent.ConcurrentHashMap;
67

@@ -43,6 +44,7 @@ public class VaultConfig implements Serializable {
4344
private Integer globalEngineVersion;
4445
private String nameSpace;
4546
private EnvironmentLoader environmentLoader;
47+
private HttpClient httpClient;
4648

4749
/**
4850
* <p>The code used to load environment variables is encapsulated here, so that a mock version
@@ -278,6 +280,19 @@ public VaultConfig prefixPath(String prefixPath) {
278280
return prefixPathDepth(countElements + 1);
279281
}
280282

283+
/**
284+
* <p>Set a preconfigured HttpClient instance to use by REST API calls. This allows to reuse
285+
* http resources (connections, worker threads) between calls. If a preconfigured HttpClient is specified, then
286+
* sslConfig and openTimeout values passed to VaultConfig are ignored.
287+
*
288+
* @param httpClient preconfigured http client instance
289+
* @return VaultConfig
290+
*/
291+
public VaultConfig httpClient(HttpClient httpClient) {
292+
this.httpClient = httpClient;
293+
return this;
294+
}
295+
281296
/**
282297
* <p>Sets the maximum number of times that an API operation will retry upon failure.</p>
283298
*
@@ -318,7 +333,6 @@ void setEngineVersion(final Integer engineVersion) {
318333
this.globalEngineVersion = engineVersion;
319334
}
320335

321-
322336
/**
323337
* <p>This is the terminating method in the builder pattern. The method that validates all of
324338
* the fields that has been set already, uses environment variables when available to populate
@@ -414,4 +428,8 @@ public String getNameSpace() {
414428
public int getPrefixPathDepth() {
415429
return prefixPathDepth;
416430
}
431+
432+
public HttpClient getHttpClient() {
433+
return httpClient;
434+
}
417435
}

src/main/java/io/github/jopenlibs/vault/api/Auth.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.github.jopenlibs.vault.response.LookupResponse;
1212
import io.github.jopenlibs.vault.response.UnwrapResponse;
1313
import io.github.jopenlibs.vault.response.WrapResponse;
14-
import io.github.jopenlibs.vault.rest.Rest;
1514
import io.github.jopenlibs.vault.rest.RestResponse;
1615
import java.io.Serializable;
1716
import java.nio.charset.StandardCharsets;
@@ -376,7 +375,7 @@ public AuthResponse createToken(final TokenRequest tokenRequest, final String to
376375
final String url = urlBuilder.toString();
377376

378377
// HTTP request to Vault
379-
final RestResponse restResponse = new Rest()//NOPMD
378+
final RestResponse restResponse = getRest()//NOPMD
380379
.url(url)
381380
.header("X-Vault-Token", config.getToken())
382381
.header("X-Vault-Namespace", this.nameSpace)
@@ -437,7 +436,7 @@ public AuthResponse loginByAppID(final String path, final String appId, final St
437436
// HTTP request to Vault
438437
final String requestJson = Json.object().add("app_id", appId).add("user_id", userId)
439438
.toString();
440-
final RestResponse restResponse = new Rest()//NOPMD
439+
final RestResponse restResponse = getRest()//NOPMD
441440
.url(config.getAddress() + "/v1/auth/" + path)
442441
.header("X-Vault-Namespace", this.nameSpace)
443442
.body(requestJson.getBytes(StandardCharsets.UTF_8))
@@ -525,7 +524,7 @@ public AuthResponse loginByAppRole(final String path, final String roleId,
525524
// HTTP request to Vault
526525
final String requestJson = Json.object().add("role_id", roleId)
527526
.add("secret_id", secretId).toString();
528-
final RestResponse restResponse = new Rest()//NOPMD
527+
final RestResponse restResponse = getRest()//NOPMD
529528
.url(config.getAddress() + "/v1/auth/" + path + "/login")
530529
.header("X-Vault-Namespace", this.nameSpace)
531530
.header("X-Vault-Request", "true")
@@ -602,7 +601,7 @@ public AuthResponse loginByUserPass(final String username, final String password
602601
return retry(attempt -> {
603602
// HTTP request to Vault
604603
final String requestJson = Json.object().add("password", password).toString();
605-
final RestResponse restResponse = new Rest()//NOPMD
604+
final RestResponse restResponse = getRest()//NOPMD
606605
.url(config.getAddress() + "/v1/auth/" + mount + "/login/" + username)
607606
.header("X-Vault-Namespace", this.nameSpace)
608607
.header("X-Vault-Request", "true")
@@ -721,7 +720,7 @@ public AuthResponse loginByAwsEc2(final String role, final String identity,
721720
}
722721
final String requestJson = request.toString();
723722

724-
final RestResponse restResponse = new Rest()//NOPMD
723+
final RestResponse restResponse = getRest()//NOPMD
725724
.url(config.getAddress() + "/v1/auth/" + mount + "/login")
726725
.body(requestJson.getBytes(StandardCharsets.UTF_8))
727726
.header("X-Vault-Namespace", this.nameSpace)
@@ -789,7 +788,7 @@ public AuthResponse loginByAwsEc2(final String role, final String pkcs7, final S
789788
request.add("nonce", nonce);
790789
}
791790
final String requestJson = request.toString();
792-
final RestResponse restResponse = new Rest()//NOPMD
791+
final RestResponse restResponse = getRest()//NOPMD
793792
.url(config.getAddress() + "/v1/auth/" + mount + "/login")
794793
.header("X-Vault-Namespace", this.nameSpace)
795794
.header("X-Vault-Request", "true")
@@ -866,7 +865,7 @@ public AuthResponse loginByAwsIam(final String role, final String iamRequestUrl,
866865
request.add("role", role);
867866
}
868867
final String requestJson = request.toString();
869-
final RestResponse restResponse = new Rest()//NOPMD
868+
final RestResponse restResponse = getRest()//NOPMD
870869
.url(config.getAddress() + "/v1/auth/" + mount + "/login")
871870
.header("X-Vault-Namespace", this.nameSpace)
872871
.header("X-Vault-Request", "true")
@@ -939,7 +938,7 @@ public AuthResponse loginByGithub(final String githubToken, final String githubA
939938
return retry(attempt -> {
940939
// HTTP request to Vault
941940
final String requestJson = Json.object().add("token", githubToken).toString();
942-
final RestResponse restResponse = new Rest()//NOPMD
941+
final RestResponse restResponse = getRest()//NOPMD
943942
.url(config.getAddress() + "/v1/auth/" + mount + "/login")
944943
.header("X-Vault-Namespace", this.nameSpace)
945944
.header("X-Vault-Request", "true")
@@ -1020,7 +1019,7 @@ public AuthResponse loginByJwt(final String provider, final String role, final S
10201019
// HTTP request to Vault
10211020
final String requestJson = Json.object().add("role", role).add("jwt", jwt)
10221021
.toString();
1023-
final RestResponse restResponse = new Rest()
1022+
final RestResponse restResponse = getRest()
10241023
.url(config.getAddress() + "/v1/" + authPath + "/login")
10251024
.header("X-Vault-Namespace", this.nameSpace)
10261025
.header("X-Vault-Request", "true")
@@ -1179,7 +1178,7 @@ public AuthResponse loginByCert(final String certAuthMount) throws VaultExceptio
11791178
final String mount = certAuthMount != null ? certAuthMount : "cert";
11801179

11811180
return retry(attempt -> {
1182-
final RestResponse restResponse = new Rest()//NOPMD
1181+
final RestResponse restResponse = getRest()//NOPMD
11831182
.url(config.getAddress() + "/v1/auth/" + mount + "/login")
11841183
.header("X-Vault-Namespace", this.nameSpace)
11851184
.header("X-Vault-Request", "true")
@@ -1251,7 +1250,7 @@ public AuthResponse renewSelf(final long increment, final String tokenAuthMount)
12511250
return retry(attempt -> {
12521251
// HTTP request to Vault
12531252
final String requestJson = Json.object().add("increment", increment).toString();
1254-
final RestResponse restResponse = new Rest()//NOPMD
1253+
final RestResponse restResponse = getRest()//NOPMD
12551254
.url(config.getAddress() + "/v1/auth/" + mount + "/renew-self")
12561255
.header("X-Vault-Token", config.getToken())
12571256
.header("X-Vault-Namespace", this.nameSpace)
@@ -1307,7 +1306,7 @@ public LookupResponse lookupSelf(final String tokenAuthMount) throws VaultExcept
13071306

13081307
return retry(attempt -> {
13091308
// HTTP request to Vault
1310-
final RestResponse restResponse = new Rest()//NOPMD
1309+
final RestResponse restResponse = getRest()//NOPMD
13111310
.url(config.getAddress() + "/v1/auth/" + mount + "/lookup-self")
13121311
.header("X-Vault-Token", config.getToken())
13131312
.header("X-Vault-Namespace", this.nameSpace)
@@ -1384,7 +1383,7 @@ public void revokeSelf(final String tokenAuthMount) throws VaultException {
13841383

13851384
retry(attempt -> {
13861385
// HTTP request to Vault
1387-
final RestResponse restResponse = new Rest()//NOPMD
1386+
final RestResponse restResponse = getRest()//NOPMD
13881387
.url(config.getAddress() + "/v1/auth/" + mount + "/revoke-self")
13891388
.header("X-Vault-Token", config.getToken())
13901389
.header("X-Vault-Namespace", this.nameSpace)

src/main/java/io/github/jopenlibs/vault/api/Debug.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public HealthResponse health(
9494

9595
return retry(attempt -> {
9696
// Build an HTTP request for Vault
97-
final Rest rest = new Rest()//NOPMD
97+
final Rest rest = getRest()//NOPMD
9898
.url(config.getAddress() + "/v1/" + path)
9999
.header("X-Vault-Token", config.getToken())
100100
.header("X-Vault-Namespace", this.nameSpace)

src/main/java/io/github/jopenlibs/vault/api/Logical.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.github.jopenlibs.vault.json.JsonObject;
77
import io.github.jopenlibs.vault.json.JsonValue;
88
import io.github.jopenlibs.vault.response.LogicalResponse;
9-
import io.github.jopenlibs.vault.rest.Rest;
109
import io.github.jopenlibs.vault.rest.RestResponse;
1110
import java.nio.charset.StandardCharsets;
1211
import java.util.Arrays;
@@ -85,7 +84,7 @@ private LogicalResponse read(final String path, final logicalOperations operatio
8584
throws VaultException {
8685
return retry(attempt -> {
8786
// Make an HTTP request to Vault
88-
final RestResponse restResponse = new Rest()//NOPMD
87+
final RestResponse restResponse = getRest()//NOPMD
8988
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(path,
9089
config.getPrefixPathDepth(), operation))
9190
.header("X-Vault-Token", config.getToken())
@@ -142,7 +141,7 @@ public LogicalResponse read(final String path, Boolean shouldRetry, final Intege
142141
attempt -> {
143142
// Make an HTTP request to Vault
144143
final RestResponse restResponse =
145-
new Rest() //NOPMD
144+
getRest() //NOPMD
146145
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(
147146
path,
148147
config.getPrefixPathDepth(), logicalOperations.readV2))
@@ -275,7 +274,7 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
275274
}
276275
}
277276
// Make an HTTP request to Vault
278-
final RestResponse restResponse = new Rest()//NOPMD
277+
final RestResponse restResponse = getRest()//NOPMD
279278
.url(config.getAddress() + "/v1/" + adjustPathForReadOrWrite(path,
280279
config.getPrefixPathDepth(), operation))
281280
.body(jsonObjectToWriteFromEngineVersion(operation, requestJson).toString()
@@ -368,7 +367,7 @@ private LogicalResponse delete(final String path, final Logical.logicalOperation
368367
throws VaultException {
369368
return retry(attempt -> {
370369
// Make an HTTP request to Vault
371-
final RestResponse restResponse = new Rest()//NOPMD
370+
final RestResponse restResponse = getRest()//NOPMD
372371
.url(config.getAddress() + "/v1/" + adjustPathForDelete(path,
373372
config.getPrefixPathDepth(), operation))
374373
.header("X-Vault-Token", config.getToken())
@@ -418,7 +417,7 @@ public LogicalResponse delete(final String path, final int[] versions) throws Va
418417
return retry(attempt -> {
419418
// Make an HTTP request to Vault
420419
JsonObject versionsToDelete = new JsonObject().add("versions", versions);
421-
final RestResponse restResponse = new Rest()//NOPMD
420+
final RestResponse restResponse = getRest()//NOPMD
422421
.url(config.getAddress() + "/v1/" + adjustPathForVersionDelete(path,
423422
config.getPrefixPathDepth()))
424423
.header("X-Vault-Token", config.getToken())
@@ -478,7 +477,7 @@ public LogicalResponse unDelete(final String path, final int[] versions) throws
478477
return retry(attempt -> {
479478
// Make an HTTP request to Vault
480479
JsonObject versionsToUnDelete = new JsonObject().add("versions", versions);
481-
final RestResponse restResponse = new Rest() //NOPMD
480+
final RestResponse restResponse = getRest() //NOPMD
482481
.url(config.getAddress() + "/v1/" + adjustPathForVersionUnDelete(path,
483482
config.getPrefixPathDepth()))
484483
.header("X-Vault-Token", config.getToken())
@@ -525,7 +524,7 @@ public LogicalResponse destroy(final String path, final int[] versions) throws V
525524
return retry(attempt -> {
526525
// Make an HTTP request to Vault
527526
JsonObject versionsToDestroy = new JsonObject().add("versions", versions);
528-
final RestResponse restResponse = new Rest()//NOPMD
527+
final RestResponse restResponse = getRest()//NOPMD
529528
.url(config.getAddress() + "/v1/" + adjustPathForVersionDestroy(path,
530529
config.getPrefixPathDepth()))
531530
.header("X-Vault-Token", config.getToken())
@@ -562,7 +561,7 @@ public LogicalResponse upgrade(final String kvPath) throws VaultException {
562561
// Make an HTTP request to Vault
563562
JsonObject kvToUpgrade = new JsonObject().add("options",
564563
new JsonObject().add("version", 2));
565-
final RestResponse restResponse = new Rest()//NOPMD
564+
final RestResponse restResponse = getRest()//NOPMD
566565
.url(config.getAddress() + "/v1/sys/mounts/" + (kvPath.replaceAll("/", "")
567566
+ "/tune"))
568567
.header("X-Vault-Token", config.getToken())

src/main/java/io/github/jopenlibs/vault/api/OperationsBase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.jopenlibs.vault.VaultConfig;
44
import io.github.jopenlibs.vault.VaultException;
5+
import io.github.jopenlibs.vault.rest.Rest;
56

67

78
/**
@@ -45,6 +46,10 @@ static <T> T retry(final EndpointOperation<T> op, int retryCount, long retryInte
4546
}
4647
}
4748

49+
protected Rest getRest() {
50+
return new Rest(config.getHttpClient());
51+
}
52+
4853
public interface EndpointOperation<T> {
4954

5055
/**
@@ -64,4 +69,5 @@ private static void sleep(long delay) {
6469
e.printStackTrace();
6570
}
6671
}
72+
6773
}

src/main/java/io/github/jopenlibs/vault/api/database/Database.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.github.jopenlibs.vault.json.Json;
77
import io.github.jopenlibs.vault.json.JsonObject;
88
import io.github.jopenlibs.vault.response.DatabaseResponse;
9-
import io.github.jopenlibs.vault.rest.Rest;
109
import io.github.jopenlibs.vault.rest.RestResponse;
1110
import java.nio.charset.StandardCharsets;
1211
import java.util.List;
@@ -91,7 +90,7 @@ public DatabaseResponse createOrUpdateRole(final String roleName,
9190
return retry(attempt -> {
9291
final String requestJson = roleOptionsToJson(options);
9392

94-
final RestResponse restResponse = new Rest()//NOPMD
93+
final RestResponse restResponse = getRest()//NOPMD
9594
.url(String.format("%s/v1/%s/roles/%s", config.getAddress(), this.mountPath,
9695
roleName))
9796
.header("X-Vault-Token", config.getToken())
@@ -137,7 +136,7 @@ public DatabaseResponse createOrUpdateRole(final String roleName,
137136
*/
138137
public DatabaseResponse getRole(final String roleName) throws VaultException {
139138
return retry(attempt -> {
140-
final RestResponse restResponse = new Rest()//NOPMD
139+
final RestResponse restResponse = getRest()//NOPMD
141140
.url(String.format("%s/v1/%s/roles/%s", config.getAddress(), this.mountPath,
142141
roleName))
143142
.header("X-Vault-Token", config.getToken())
@@ -190,7 +189,7 @@ public DatabaseResponse revoke(final String serialNumber) throws VaultException
190189
}
191190
final String requestJson = jsonObject.toString();
192191

193-
final RestResponse restResponse = new Rest()//NOPMD
192+
final RestResponse restResponse = getRest()//NOPMD
194193
.url(String.format("%s/v1/%s/revoke", config.getAddress(), this.mountPath))
195194
.header("X-Vault-Token", config.getToken())
196195
.header("X-Vault-Namespace", this.nameSpace)
@@ -235,7 +234,7 @@ public DatabaseResponse revoke(final String serialNumber) throws VaultException
235234
*/
236235
public DatabaseResponse deleteRole(final String roleName) throws VaultException {
237236
return retry(attempt -> {
238-
final RestResponse restResponse = new Rest()//NOPMD
237+
final RestResponse restResponse = getRest()//NOPMD
239238
.url(String.format("%s/v1/%s/roles/%s", config.getAddress(), this.mountPath,
240239
roleName))
241240
.header("X-Vault-Token", config.getToken())
@@ -282,7 +281,7 @@ public DatabaseResponse deleteRole(final String roleName) throws VaultException
282281
*/
283282
public DatabaseResponse creds(final String roleName) throws VaultException {
284283
return retry(attempt -> {
285-
final RestResponse restResponse = new Rest()//NOPMD
284+
final RestResponse restResponse = getRest()//NOPMD
286285
.url(String.format("%s/v1/%s/creds/%s", config.getAddress(), this.mountPath,
287286
roleName))
288287
.header("X-Vault-Token", config.getToken())

0 commit comments

Comments
 (0)