Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to not treat 4xx response codes as errors that sho… #176

Merged
merged 2 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/com/bettercloud/vault/api/Logical.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private LogicalResponse read(final String path, Boolean shouldRetry, final logic
.sslContext(config.getSslConfig().getSslContext())
.get();

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

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

// HTTP Status should be either 200 (with content - e.g. PKI write) or 204 (no content)
final int restStatus = restResponse.getStatus();
if (restStatus == 200 || restStatus == 204) {
if (restStatus == 200 || restStatus == 204 || (restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
return new LogicalResponse(restResponse, retryCount, operation);
} else {
throw new VaultException("Expecting HTTP status 204 or 200, but instead receiving " + restStatus
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/bettercloud/vault/VaultConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,4 @@ public void testConfigBuilder_WithNamespace() throws VaultException {
VaultConfig vaultConfig = new VaultConfig().nameSpace("namespace").address("address").build();
Assert.assertEquals(vaultConfig.getNameSpace(), "namespace");
}

}
24 changes: 24 additions & 0 deletions src/test/java/com/bettercloud/vault/VaultTests.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.bettercloud.vault;

import com.bettercloud.vault.response.LogicalResponse;
import com.bettercloud.vault.vault.VaultTestUtils;
import com.bettercloud.vault.vault.mock.MockVault;
import org.eclipse.jetty.server.Server;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;


/**
* Unit tests for the various <code>Vault</code> constructors.
Expand Down Expand Up @@ -88,4 +94,22 @@ public void kvEngineMapIsHonored() throws VaultException {
Assert.assertEquals(String.valueOf(1), vault.logical().getEngineVersionForSecretPath("kv-v1").toString());
Assert.assertEquals(String.valueOf(2), vault.logical().getEngineVersionForSecretPath("notInMap").toString());
}

@Test
public void testConfigBuiler_WithInvalidRequestAsNonError() throws Exception {
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/\"]}");
final Server server = VaultTestUtils.initHttpMockVault(mockVault);
server.start();

final VaultConfig vaultConfig = new VaultConfig()
.address("http://127.0.0.1:8999")
.token("mock_token")
.build();
final Vault vault = new Vault(vaultConfig);

LogicalResponse response = vault.logical().read("path/that/does/not/exist/");
VaultTestUtils.shutdownMockVault(server);
Assert.assertEquals(403, response.getRestResponse().getStatus());
Assert.assertEquals(0, response.getRetries());
}
}