Assert compatibility with Vault 0.5.2, 0.6.0 and 0.6.1
Support top level auth mount response and nested (within `data`) mount responses. Add build matrix to TravisCI. Adopt tests to earlier Vault responses. Fixes gh-2
This commit is contained in:
@@ -3,6 +3,12 @@ language: java
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
|
||||
env:
|
||||
matrix:
|
||||
- VAULT_VER=0.5.2
|
||||
- VAULT_VER=0.6.0
|
||||
- VAULT_VER=0.6.1
|
||||
|
||||
install:
|
||||
- mkdir -p download
|
||||
- src/test/bash/create_certificates.sh
|
||||
|
||||
@@ -17,10 +17,10 @@ package org.springframework.vault.core;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -40,6 +40,8 @@ import org.springframework.vault.support.VaultUnsealStatus;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
@@ -234,8 +236,7 @@ public class VaultSysTemplate implements VaultSysOperations {
|
||||
|
||||
private static class GetMounts implements SessionCallback<Map<String, VaultMount>> {
|
||||
|
||||
private static final ParameterizedTypeReference<VaultResponseSupport<Map<String, VaultMount>>> MOUNT_TYPE_REF = new ParameterizedTypeReference<VaultResponseSupport<Map<String, VaultMount>>>() {
|
||||
};
|
||||
private static final ParameterizedTypeReference<VaultMountsResponse> MOUNT_TYPE_REF = new ParameterizedTypeReference<VaultMountsResponse>() {};
|
||||
|
||||
private final String path;
|
||||
|
||||
@@ -246,15 +247,52 @@ public class VaultSysTemplate implements VaultSysOperations {
|
||||
@Override
|
||||
public Map<String, VaultMount> doWithVault(VaultOperations.VaultSession session) {
|
||||
|
||||
VaultResponseEntity<VaultResponseSupport<Map<String, VaultMount>>> response = session.exchange(path, HttpMethod.GET, null,
|
||||
MOUNT_TYPE_REF, Collections.<String, Object>emptyMap());
|
||||
VaultResponseEntity<VaultMountsResponse> response = session.exchange(path, HttpMethod.GET, null, MOUNT_TYPE_REF,
|
||||
Collections.<String, Object> emptyMap());
|
||||
|
||||
if (response.isSuccessful() && response.hasBody()) {
|
||||
return response.getBody().getData();
|
||||
|
||||
VaultMountsResponse body = response.getBody();
|
||||
|
||||
if (body.getData() != null) {
|
||||
return response.getBody().getData();
|
||||
}
|
||||
|
||||
return response.getBody().getTopLevelMounts();
|
||||
}
|
||||
|
||||
throw new VaultException(buildExceptionMessage(response));
|
||||
}
|
||||
|
||||
private static class VaultMountsResponse extends VaultResponseSupport<Map<String, VaultMount>> {
|
||||
|
||||
private Map<String, VaultMount> topLevelMounts = new HashMap<String, VaultMount>();
|
||||
|
||||
@JsonIgnore
|
||||
public Map<String, VaultMount> getTopLevelMounts() {
|
||||
return topLevelMounts;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@JsonAnySetter
|
||||
public void set(String name, Object value) {
|
||||
|
||||
if (value instanceof Map) {
|
||||
|
||||
Map<String, Object> map = (Map) value;
|
||||
|
||||
if (map.containsKey("type")) {
|
||||
|
||||
VaultMount vaultMount = new VaultMount((String) map.get("type"));
|
||||
vaultMount.setDescription((String) map.get("description"));
|
||||
vaultMount.setConfig((Map) map.get("config"));
|
||||
|
||||
topLevelMounts.put(name, vaultMount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Health implements VaultAccessor.RestTemplateCallback<VaultHealthResponse> {
|
||||
|
||||
@@ -26,11 +26,11 @@ import java.util.Map;
|
||||
import org.assertj.core.util.Files;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.vault.client.VaultClient;
|
||||
import org.springframework.vault.client.VaultEndpoint;
|
||||
import org.springframework.vault.client.VaultException;
|
||||
import org.springframework.vault.config.ClientHttpRequestFactoryFactory;
|
||||
import org.springframework.vault.core.VaultOperations;
|
||||
import org.springframework.vault.support.ClientOptions;
|
||||
@@ -84,7 +84,8 @@ public class ClientCertificateAuthenticationIntegrationTests extends Integration
|
||||
assertThat(login.getToken()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test(expected = VaultException.class)
|
||||
// Compatibility for Vault 0.6.0 and below. Vault 0.6.1 fixed that issue and we receive a VaultException here.
|
||||
@Test(expected = NestedRuntimeException.class)
|
||||
public void loginShouldFail() throws Exception {
|
||||
|
||||
ClientHttpRequestFactory clientHttpRequestFactory = ClientHttpRequestFactoryFactory.create(new ClientOptions(),
|
||||
|
||||
@@ -127,7 +127,8 @@ public class VaultTokenTemplateIntegrationTests extends IntegrationTestSupport {
|
||||
|
||||
VaultResponseEntity<String> response = lookupSelf(tokenResponse);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
assertThat(response.getStatusCode()).isIn(/* <= Vault 0.6.0 */ HttpStatus.BAD_REQUEST,
|
||||
/* >= Vault 0.6.1 */ HttpStatus.FORBIDDEN);
|
||||
assertThat(response.getMessage()).isEqualTo("permission denied");
|
||||
}
|
||||
|
||||
|
||||
@@ -24,9 +24,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.vault.client.VaultException;
|
||||
import org.springframework.vault.support.VaultTransitKeyConfiguration;
|
||||
import org.springframework.vault.support.VaultMount;
|
||||
import org.springframework.vault.support.VaultTransitKey;
|
||||
import org.springframework.vault.support.VaultTransitKeyConfiguration;
|
||||
import org.springframework.vault.support.VaultTransitKeyCreationRequest;
|
||||
import org.springframework.vault.support.VaultTransitRequest;
|
||||
import org.springframework.vault.util.IntegrationTestSupport;
|
||||
@@ -108,7 +108,7 @@ public class VaultTransitTemplateIntegrationTests extends IntegrationTestSupport
|
||||
transitOperations.deleteKey("hello-world");
|
||||
fail("Missing VaultException");
|
||||
} catch (VaultException e) {
|
||||
assertThat(e).hasMessageContaining("could not delete");
|
||||
assertThat(e).hasMessageContaining("Status 400");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
###########################################################################
|
||||
|
||||
|
||||
VAULT_VER="0.6.1"
|
||||
VAULT_VER="${VAULT_VER:-0.6.1}"
|
||||
UNAME=$(uname -s | tr '[:upper:]' '[:lower:]')
|
||||
VAULT_ZIP="vault_${VAULT_VER}_${UNAME}_amd64.zip"
|
||||
IGNORE_CERTS="${IGNORE_CERTS:-no}"
|
||||
|
||||
Reference in New Issue
Block a user