Skip to content

Commit 144917a

Browse files
author
Barry Klawans
committed
Made not treating 400 class status codes as errors the default behavior
1 parent 85e093d commit 144917a

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class VaultConfig implements Serializable {
4040
private int retryIntervalMilliseconds;
4141
private Integer globalEngineVersion;
4242
private String nameSpace;
43-
private Boolean treatInvalidRequestsAsErrors = true;
4443
private EnvironmentLoader environmentLoader;
4544

4645
/**
@@ -208,26 +207,6 @@ public VaultConfig readTimeout(final Integer readTimeout) {
208207
return this;
209208
}
210209

211-
/**
212-
* <p>Specifies whether 4xx class status codes should be treated as errors or invalid calls.</p>
213-
*
214-
* <p>If 4xx status codes are treated as errors, any call that returns them will retry (if specified)
215-
* and will result in an exception being thrown. If disabled, a 4xx return code does not retry, does
216-
* not generate an exception, and instead returns the error in the response body</p>
217-
*
218-
* <p>If you set this to <code>false</code> you need to check the response code before using the data
219-
* in a response</p>
220-
*
221-
* <p>The default behavior is to treat 4xx status codes as errors.</p>
222-
*
223-
* @param treatInvalidRequestsAsErrors Should 4xx class status codes be treated as errors.
224-
* @return This object, with treatInvalidRequestsAsErrors populated, ready for additional builder-pattern method calls or else finalization with the build() method
225-
*/
226-
public VaultConfig treatInvalidRequestsAsErrors(final Boolean treatInvalidRequestsAsErrors) {
227-
this.treatInvalidRequestsAsErrors = treatInvalidRequestsAsErrors;
228-
return this;
229-
}
230-
231210
/**
232211
* <p>Sets the maximum number of times that an API operation will retry upon failure.</p>
233212
*
@@ -335,10 +314,6 @@ public Integer getReadTimeout() {
335314
return readTimeout;
336315
}
337316

338-
public Boolean isTreatInvalidRequestsAsErrors() {
339-
return treatInvalidRequestsAsErrors;
340-
}
341-
342317
public int getMaxRetries() {
343318
return maxRetries;
344319
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ private LogicalResponse read(final String path, Boolean shouldRetry, final logic
9191
.sslContext(config.getSslConfig().getSslContext())
9292
.get();
9393

94-
// Validate response
95-
if (restResponse.getStatus() != 200 && !(!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
94+
// Validate response - don't treat 4xx class errors as exceptions, we want to return an error as the response
95+
if (restResponse.getStatus() != 200 && !(restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
9696
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
9797
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
9898
restResponse.getStatus());
@@ -160,8 +160,8 @@ public LogicalResponse read(final String path, Boolean shouldRetry, final Intege
160160
.sslContext(config.getSslConfig().getSslContext())
161161
.get();
162162

163-
// Validate response
164-
if (restResponse.getStatus() != 200 && !(!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
163+
// Validate response - don't treat 4xx class errors as exceptions, we want to return an error as the response
164+
if (restResponse.getStatus() != 200 && !(restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
165165
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
166166
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
167167
restResponse.getStatus());
@@ -261,7 +261,7 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
261261

262262
// HTTP Status should be either 200 (with content - e.g. PKI write) or 204 (no content)
263263
final int restStatus = restResponse.getStatus();
264-
if (restStatus == 200 || restStatus == 204 || (!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
264+
if (restStatus == 200 || restStatus == 204 || (restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
265265
return new LogicalResponse(restResponse, retryCount, operation);
266266
} else {
267267
throw new VaultException("Expecting HTTP status 204 or 200, but instead receiving " + restStatus

src/test/java/com/bettercloud/vault/VaultConfigTests.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,4 @@ public void testConfigBuilder_WithNamespace() throws VaultException {
255255
VaultConfig vaultConfig = new VaultConfig().nameSpace("namespace").address("address").build();
256256
Assert.assertEquals(vaultConfig.getNameSpace(), "namespace");
257257
}
258-
259-
@Test
260-
public void testConfigBuiler_WithInvalidRequestAsNonError() throws VaultException {
261-
VaultConfig vaultConfig = new VaultConfig().address("address").token("token").treatInvalidRequestsAsErrors(false).build();
262-
assertEquals("address", vaultConfig.getAddress());
263-
assertEquals("token", vaultConfig.getToken());
264-
assertEquals(Boolean.FALSE, vaultConfig.isTreatInvalidRequestsAsErrors());
265-
266-
}
267258
}

src/test/java/com/bettercloud/vault/VaultTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.bettercloud.vault;
22

3+
import com.bettercloud.vault.response.LogicalResponse;
4+
import com.bettercloud.vault.vault.VaultTestUtils;
5+
import com.bettercloud.vault.vault.mock.MockVault;
6+
import org.eclipse.jetty.server.Server;
37
import org.junit.Assert;
48
import org.junit.Test;
59

610
import java.util.HashMap;
711
import java.util.Map;
812

13+
import static junit.framework.TestCase.assertEquals;
14+
915

1016
/**
1117
* Unit tests for the various <code>Vault</code> constructors.
@@ -88,4 +94,22 @@ public void kvEngineMapIsHonored() throws VaultException {
8894
Assert.assertEquals(String.valueOf(1), vault.logical().getEngineVersionForSecretPath("kv-v1").toString());
8995
Assert.assertEquals(String.valueOf(2), vault.logical().getEngineVersionForSecretPath("notInMap").toString());
9096
}
97+
98+
@Test
99+
public void testConfigBuiler_WithInvalidRequestAsNonError() throws Exception {
100+
final MockVault mockVault = new MockVault(403, "{\"errors\":[\"preflight capability check returned 403, please ensure client's policies grant access to path \"path/that/does/not/exist/\"]}");
101+
final Server server = VaultTestUtils.initHttpMockVault(mockVault);
102+
server.start();
103+
104+
final VaultConfig vaultConfig = new VaultConfig()
105+
.address("http://127.0.0.1:8999")
106+
.token("mock_token")
107+
.build();
108+
final Vault vault = new Vault(vaultConfig);
109+
110+
LogicalResponse response = vault.logical().read("path/that/does/not/exist/");
111+
VaultTestUtils.shutdownMockVault(server);
112+
Assert.assertEquals(403, response.getRestResponse().getStatus());
113+
Assert.assertEquals(0, response.getRetries());
114+
}
91115
}

0 commit comments

Comments
 (0)