Skip to content

Commit 9419a3e

Browse files
Merge pull request #176 from bklawans/master
Adding the ability to not treat 4xx response codes as errors that sho…
2 parents 8584f63 + 144917a commit 9419a3e

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

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) {
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) {
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) {
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,5 +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-
259258
}

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)