Retain Vault JSON data type in property source.

Closes gh-189.
This commit is contained in:
Mark Paluch
2018-01-17 15:55:09 +01:00
parent 3490cabbde
commit 5dd65c8fcd
14 changed files with 82 additions and 118 deletions

View File

@@ -15,16 +15,13 @@
*/
package org.springframework.cloud.vault.config.aws;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfiguration.AwsSecretBackendMetadataFactory.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.vault.config.VaultConfigOperations;
import org.springframework.cloud.vault.config.VaultConfigTemplate;
import org.springframework.cloud.vault.config.VaultProperties;
@@ -33,6 +30,10 @@ import org.springframework.cloud.vault.util.Settings;
import org.springframework.util.StringUtils;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfiguration.AwsSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the aws secret backend. This
* test requires AWS credentials and a region, see {@link #AWS_ACCESS_KEY} and
@@ -88,9 +89,9 @@ public class AwsSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations.read(forAws(aws))
Map<String, Object> secretProperties = configOperations.read(forAws(aws))
.getData();
assertThat(secretProperties).containsKeys("cloud.aws.credentials.accessKey",

View File

@@ -15,10 +15,6 @@
*/
package org.springframework.cloud.vault.config.consul;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfiguration.ConsulSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
@@ -42,6 +38,10 @@ import org.springframework.util.Base64Utils;
import org.springframework.vault.core.VaultOperations;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfiguration.ConsulSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the consul secret backend. This
* test requires a running Consul instance, see {@link #CONNECTION_URL}.
@@ -110,9 +110,9 @@ public class ConsulSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations.read(forConsul(consul))
Map<String, Object> secretProperties = configOperations.read(forConsul(consul))
.getData();
assertThat(secretProperties).containsKeys("spring.cloud.consul.token");

View File

@@ -15,10 +15,6 @@
*/
package org.springframework.cloud.vault.config.databases;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
@@ -34,6 +30,10 @@ import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.cloud.vault.util.Settings;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the cassandra secret backend.
* This test requires a running Cassandra instance, see {@link #CASSANDRA_HOST} and other
@@ -97,9 +97,9 @@ public class CassandraSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations
Map<String, Object> secretProperties = configOperations
.read(forDatabase(cassandra)).getData();
assertThat(secretProperties).containsKeys("spring.data.cassandra.username",

View File

@@ -15,10 +15,6 @@
*/
package org.springframework.cloud.vault.config.databases;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
@@ -36,6 +32,10 @@ import org.springframework.cloud.vault.util.Settings;
import org.springframework.cloud.vault.util.Version;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the mongodb secret backend.
* This test requires a running MongoDB instance, see {@link #ROOT_CREDENTIALS}.
@@ -89,9 +89,10 @@ public class MongoSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations.read(forDatabase(mongodb))
Map<String, Object> secretProperties = configOperations
.read(forDatabase(mongodb))
.getData();
assertThat(secretProperties).containsKeys("spring.data.mongodb.username",

View File

@@ -31,9 +31,9 @@ import org.springframework.cloud.vault.util.Settings;
import org.springframework.cloud.vault.util.Version;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.forDatabase;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the {@code database} secret
@@ -95,9 +95,9 @@ public class MySqlDatabaseSecretIntegrationTests extends IntegrationTestSupport
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations.read(forDatabase(mySql))
Map<String, Object> secretProperties = configOperations.read(forDatabase(mySql))
.getData();
assertThat(secretProperties).containsKeys("spring.datasource.username",

View File

@@ -15,16 +15,13 @@
*/
package org.springframework.cloud.vault.config.databases;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.vault.config.VaultConfigOperations;
import org.springframework.cloud.vault.config.VaultConfigTemplate;
import org.springframework.cloud.vault.config.VaultProperties;
@@ -33,6 +30,10 @@ import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.cloud.vault.util.Settings;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the mysql secret backend. This
* test requires a running MySQL instance, see {@link #ROOT_CREDENTIALS}.
@@ -82,9 +83,9 @@ public class MySqlSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations.read(forDatabase(mySql))
Map<String, Object> secretProperties = configOperations.read(forDatabase(mySql))
.getData();
assertThat(secretProperties).containsKeys("spring.datasource.username",

View File

@@ -15,16 +15,13 @@
*/
package org.springframework.cloud.vault.config.databases;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.vault.config.VaultConfigOperations;
import org.springframework.cloud.vault.config.VaultConfigTemplate;
import org.springframework.cloud.vault.config.VaultProperties;
@@ -33,6 +30,10 @@ import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.cloud.vault.util.Settings;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory.*;
/**
* Integration tests for
* {@link org.springframework.cloud.vault.config.VaultConfigTemplate} using the postgresql
@@ -91,13 +92,12 @@ public class PostgreSqlSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations
Map<String, Object> secretProperties = configOperations
.read(forDatabase(postgreSql)).getData();
assertThat(secretProperties).containsKeys("spring.datasource.username",
"spring.datasource.password");
}
}

View File

@@ -15,10 +15,6 @@
*/
package org.springframework.cloud.vault.config.rabbitmq;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfiguration.RabbitMqSecretBackendMetadataFactory.*;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
@@ -26,6 +22,7 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.vault.config.VaultConfigTemplate;
import org.springframework.cloud.vault.config.VaultProperties;
import org.springframework.cloud.vault.util.CanConnect;
@@ -34,6 +31,10 @@ import org.springframework.cloud.vault.util.Settings;
import org.springframework.cloud.vault.util.Version;
import org.springframework.vault.core.VaultOperations;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfiguration.RabbitMqSecretBackendMetadataFactory.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the rabbitmq secret backend.
* This test requires a running RabbitMQ instance, see {@link #RABBITMQ_URI}.
@@ -94,9 +95,9 @@ public class RabbitMqSecretIntegrationTests extends IntegrationTestSupport {
}
@Test
public void shouldCreateCredentialsCorrectly() throws Exception {
public void shouldCreateCredentialsCorrectly() {
Map<String, String> secretProperties = configOperations
Map<String, Object> secretProperties = configOperations
.read(forRabbitMq(rabbitmq)).getData();
assertThat(secretProperties).containsKeys("spring.rabbitmq.username",

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,8 +32,7 @@ import org.springframework.vault.core.util.PropertyTransformer;
*
* @author Mark Paluch
*/
public class PropertyNameTransformer extends PropertyTransformerSupport
implements PropertyTransformer {
public class PropertyNameTransformer implements PropertyTransformer {
private final Map<String, String> nameMapping = new HashMap<>();
@@ -59,24 +58,23 @@ public class PropertyNameTransformer extends PropertyTransformerSupport
}
@Override
public Map<String, String> transformProperties(Map<String, String> input) {
public Map<String, Object> transformProperties(Map<String, ? extends Object> input) {
if (input == null) {
return null;
}
Map<String, String> transformed = new LinkedHashMap<>(input.size(), 1);
Map<String, Object> transformed = new LinkedHashMap<>(input.size(), 1);
for (String key : input.keySet()) {
String value = input.get(key);
String translatedKey = key;
if (nameMapping.containsKey(key)) {
translatedKey = nameMapping.get(key);
}
transformed.put(translatedKey, value);
transformed.put(translatedKey, input.get(key));
}
return transformed;

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.vault.config;
import java.util.Map;
import org.springframework.vault.core.util.PropertyTransformer;
/**
* Implementation support class for classes implementing {@link PropertyTransformer}.
*
* @author Mark Paluch
*/
abstract class PropertyTransformerSupport implements PropertyTransformer {
@Override
public PropertyTransformer andThen(final PropertyTransformer after) {
final PropertyTransformer that = this;
return new PropertyTransformerSupport() {
@Override
public Map<String, String> transformProperties(Map<String, String> input) {
Map<String, String> processed = that.transformProperties(input);
return after.transformProperties(processed);
}
};
}
}

View File

@@ -25,5 +25,5 @@ import org.springframework.vault.support.VaultResponseSupport;
*
* @author Mark Paluch
*/
public class Secrets extends VaultResponseSupport<Map<String, String>> {
public class Secrets extends VaultResponseSupport<Map<String, Object>> {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ public class VaultConfigTemplate implements VaultConfigOperations {
return null;
}
Map<String, String> data = JsonMapFlattener.flatten(vaultResponse.getData());
Map<String, Object> data = JsonMapFlattener.flatten(vaultResponse.getData());
PropertyTransformer propertyTransformer = secretBackendMetadata
.getPropertyTransformer();
@@ -99,7 +99,7 @@ public class VaultConfigTemplate implements VaultConfigOperations {
return null;
}
private Secrets createSecrets(VaultResponse vaultResponse, Map<String, String> data) {
private Secrets createSecrets(VaultResponse vaultResponse, Map<String, Object> data) {
Secrets secrets = new Secrets();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ class VaultPropertySource extends EnumerablePropertySource<VaultConfigOperations
private final SecretBackendMetadata secretBackendMetadata;
private final Map<String, String> properties = new LinkedHashMap<>();
private final Map<String, Object> properties = new LinkedHashMap<>();
private Secrets secrets;

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.cloud.vault.config;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.cloud.vault.config.GenericSecretBackendMetadata.*;
import java.util.HashMap;
import java.util.Map;
@@ -27,6 +24,9 @@ import org.junit.Test;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.cloud.vault.util.Settings;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.cloud.vault.config.GenericSecretBackendMetadata.*;
/**
* Integration tests for {@link VaultConfigTemplate} using the generic secret backend.
*
@@ -38,26 +38,26 @@ public class GenericSecretIntegrationTests extends IntegrationTestSupport {
private VaultConfigOperations configOperations;
@Before
public void setUp() throws Exception {
public void setUp() {
vaultProperties.setFailFast(false);
prepare().getVaultOperations().write("secret/app-name", (Map) createData());
prepare().getVaultOperations().write("secret/app-name", createData());
configOperations = new VaultConfigTemplate(prepare().getVaultOperations(),
vaultProperties);
}
@Test
public void shouldReturnSecretsCorrectly() throws Exception {
public void shouldReturnSecretsCorrectly() {
Map<String, String> secretProperties = configOperations
Map<String, Object> secretProperties = configOperations
.read(create("secret", "app-name")).getData();
assertThat(secretProperties).containsAllEntriesOf(createExpectedMap());
}
@Test
public void shouldReturnNullIfNotFound() throws Exception {
public void shouldReturnNullIfNotFound() {
Secrets secrets = configOperations.read(create("secret", "missing"));
@@ -65,18 +65,24 @@ public class GenericSecretIntegrationTests extends IntegrationTestSupport {
}
private Map<String, Object> createData() {
Map<String, Object> data = new HashMap<>();
data.put("string", "value");
data.put("number", "1234");
data.put("number", 1234);
data.put("boolean", true);
return data;
}
private Map<String, String> createExpectedMap() {
Map<String, String> data = new HashMap<>();
private Map<String, Object> createExpectedMap() {
Map<String, Object> data = new HashMap<>();
data.put("string", "value");
data.put("number", "1234");
data.put("boolean", "true");
data.put("number", 1234);
data.put("boolean", true);
return data;
}
}