Skip to content

Commit 1b22b34

Browse files
committed
Add optionalHeader for NameSpaces.
Remove UTF8 encoding for headers. Update tests. Add tests.
1 parent 799b9fc commit 1b22b34

19 files changed

Lines changed: 391 additions & 433 deletions

src/main/java/com/bettercloud/vault/Vault.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ private Map<String, String> collectSecretEngineVersions() {
224224
final RestResponse restResponse = new Rest()//NOPMD
225225
.url(vaultConfig.getAddress() + "/v1/sys/mounts")
226226
.header("X-Vault-Token", vaultConfig.getToken())
227+
.optionalHeader("X-Vault-Namespace", this.vaultConfig.getNameSpace())
227228
.connectTimeoutSeconds(vaultConfig.getOpenTimeout())
228229
.readTimeoutSeconds(vaultConfig.getReadTimeout())
229230
.sslVerification(vaultConfig.getSslConfig().isVerify())

src/main/java/com/bettercloud/vault/VaultConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class VaultConfig implements Serializable {
5050
private int retryIntervalMilliseconds;
5151
@Getter
5252
private Integer globalEngineVersion;
53+
@Getter
54+
private String nameSpace;
5355
private EnvironmentLoader environmentLoader;
5456

5557
/**
@@ -73,6 +75,20 @@ public VaultConfig environmentLoader(final EnvironmentLoader environmentLoader)
7375
return this;
7476
}
7577

78+
/**
79+
* <p>Optional. Sets a global namespace to the Vault server instance, if desired. Otherwise, namespace can be applied individually to any read / write / auth call.
80+
*
81+
* <p>Namespace support requires Vault Enterprise Pro, please see https://learn.hashicorp.com/vault/operations/namespaces</p>
82+
*
83+
* @param nameSpace The namespace to use globally in this VaultConfig instance.
84+
* @return This object, with the namespace populated, ready for additional builder-pattern method calls or else
85+
* finalization with the build() method
86+
*/
87+
public VaultConfig nameSpace(final String nameSpace) {
88+
this.nameSpace = nameSpace;
89+
return this;
90+
}
91+
7692
/**
7793
* <p>Sets the KV Secrets Engine version of the Vault server instance.
7894
*

src/main/java/com/bettercloud/vault/api/Auth.java

Lines changed: 63 additions & 164 deletions
Large diffs are not rendered by default.

src/main/java/com/bettercloud/vault/api/Debug.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class Debug {
2727

2828
public Debug(final VaultConfig config) {
2929
this.config = config;
30+
if (this.config.getNameSpace() != null && !this.config.getNameSpace().isEmpty()) {
31+
this.nameSpace = this.config.getNameSpace();
32+
}
3033
}
3134

3235
public Debug withNameSpace(final String nameSpace) {
@@ -96,9 +99,7 @@ public HealthResponse health(
9699
if (config.getToken() != null) {
97100
rest.header("X-Vault-Token", config.getToken());
98101
}
99-
if (this.nameSpace != null && !this.nameSpace.isEmpty()) {
100-
rest.header("X-Vault-Namespace", this.nameSpace);
101-
}
102+
rest.optionalHeader("X-Vault-Namespace", this.nameSpace);
102103
// Add params if present
103104
if (standbyOk != null) rest.parameter("standbyok", standbyOk.toString());
104105
if (activeCode != null) rest.parameter("activecode", activeCode.toString());

src/main/java/com/bettercloud/vault/api/Leases.java

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class Leases {
2626

2727
public Leases(final VaultConfig config) {
2828
this.config = config;
29+
if (this.config.getNameSpace() != null && !this.config.getNameSpace().isEmpty()) {
30+
this.nameSpace = this.config.getNameSpace();
31+
}
2932
}
3033

3134
public Leases withNameSpace(final String nameSpace) {
@@ -51,22 +54,15 @@ public VaultResponse revoke(final String leaseId) throws VaultException {
5154
int retryCount = 0;
5255
while (true) {
5356
try {
54-
final RestResponse restResponse;
55-
final Rest rest = new Rest()//NOPMD
57+
final RestResponse restResponse = new Rest()//NOPMD
5658
.url(config.getAddress() + "/v1/sys/revoke/" + leaseId)
5759
.header("X-Vault-Token", config.getToken())
60+
.optionalHeader("X-Vault-Namespace", this.nameSpace)
5861
.connectTimeoutSeconds(config.getOpenTimeout())
5962
.readTimeoutSeconds(config.getReadTimeout())
6063
.sslVerification(config.getSslConfig().isVerify())
61-
.sslContext(config.getSslConfig().getSslContext());
62-
63-
if (this.nameSpace != null && !this.nameSpace.isEmpty()) {
64-
restResponse = rest
65-
.header("X-Vault-Namespace", this.nameSpace)
66-
.put();
67-
} else {
68-
restResponse = rest.put();
69-
}
64+
.sslContext(config.getSslConfig().getSslContext())
65+
.put();
7066

7167
// Validate response
7268
if (restResponse.getStatus() != 204) {
@@ -113,22 +109,15 @@ public VaultResponse revokePrefix(final String prefix) throws VaultException {
113109
int retryCount = 0;
114110
while (true) {
115111
try {
116-
final RestResponse restResponse;
117-
final Rest rest = new Rest()//NOPMD
112+
final RestResponse restResponse = new Rest()//NOPMD
118113
.url(config.getAddress() + "/v1/sys/revoke-prefix/" + prefix)
119114
.header("X-Vault-Token", config.getToken())
115+
.optionalHeader("X-Vault-Namespace", this.nameSpace)
120116
.connectTimeoutSeconds(config.getOpenTimeout())
121117
.readTimeoutSeconds(config.getReadTimeout())
122118
.sslVerification(config.getSslConfig().isVerify())
123-
.sslContext(config.getSslConfig().getSslContext());
124-
125-
if (this.nameSpace != null && !this.nameSpace.isEmpty()) {
126-
restResponse = rest
127-
.header("X-Vault-Namespace", this.nameSpace)
128-
.put();
129-
} else {
130-
restResponse = rest.put();
131-
}
119+
.sslContext(config.getSslConfig().getSslContext())
120+
.put();
132121

133122
// Validate response
134123
if (restResponse.getStatus() != 204) {
@@ -178,22 +167,15 @@ public VaultResponse revokeForce(final String prefix) throws VaultException {
178167
int retryCount = 0;
179168
while (true) {
180169
try {
181-
final RestResponse restResponse;
182-
final Rest rest = new Rest()//NOPMD
170+
final RestResponse restResponse = new Rest()//NOPMD
183171
.url(config.getAddress() + "/v1/sys/revoke-force/" + prefix)
184172
.header("X-Vault-Token", config.getToken())
173+
.optionalHeader("X-Vault-Namespace", this.nameSpace)
185174
.connectTimeoutSeconds(config.getOpenTimeout())
186175
.readTimeoutSeconds(config.getReadTimeout())
187176
.sslVerification(config.getSslConfig().isVerify())
188-
.sslContext(config.getSslConfig().getSslContext());
189-
190-
if (this.nameSpace != null && !this.nameSpace.isEmpty()) {
191-
restResponse = rest
192-
.header("X-Vault-Namespace", this.nameSpace)
193-
.put();
194-
} else {
195-
restResponse = rest.put();
196-
}
177+
.sslContext(config.getSslConfig().getSslContext())
178+
.put();
197179

198180
// Validate response
199181
if (restResponse.getStatus() != 204) {
@@ -248,23 +230,16 @@ public VaultResponse renew(final String leaseId, final long increment) throws Va
248230
while (true) {
249231
try {
250232
final String requestJson = Json.object().add("increment", increment).toString();
251-
final RestResponse restResponse;
252-
final Rest rest = new Rest()//NOPMD
233+
final RestResponse restResponse = new Rest()//NOPMD
253234
.url(config.getAddress() + "/v1/sys/renew/" + leaseId)
254235
.header("X-Vault-Token", config.getToken())
236+
.optionalHeader("X-Vault-Namespace", this.nameSpace)
255237
.body(increment < 0 ? null : requestJson.getBytes(StandardCharsets.UTF_8))
256238
.connectTimeoutSeconds(config.getOpenTimeout())
257239
.readTimeoutSeconds(config.getReadTimeout())
258240
.sslVerification(config.getSslConfig().isVerify())
259-
.sslContext(config.getSslConfig().getSslContext());
260-
261-
if (this.nameSpace != null && !this.nameSpace.isEmpty()) {
262-
restResponse = rest
263-
.header("X-Vault-Namespace", this.nameSpace)
264-
.post();
265-
} else {
266-
restResponse = rest.post();
267-
}
241+
.sslContext(config.getSslConfig().getSslContext())
242+
.post();
268243

269244
// Validate response
270245
if (restResponse.getStatus() != 200) {

0 commit comments

Comments
 (0)