Bumping versions

This commit is contained in:
buildmaster
2021-01-12 09:59:54 +00:00
parent 4ba93e7ea8
commit dfcd3b17c0
6 changed files with 63 additions and 97 deletions

View File

@@ -37,8 +37,7 @@ public class VaultEncryptionAutoConfiguration {
@Bean
public VaultEnvironmentEncryptor vaultEnvironmentEncryptor(
SpringVaultEnvironmentRepository vaultEnvironmentRepository) {
return new VaultEnvironmentEncryptor(
vaultEnvironmentRepository.getKeyValueTemplate());
return new VaultEnvironmentEncryptor(vaultEnvironmentRepository.getKeyValueTemplate());
}
}

View File

@@ -58,8 +58,7 @@ public class VaultEnvironmentEncryptor implements EnvironmentEncryptor {
for (Map.Entry<Object, Object> entry : new LinkedHashSet<>(map.entrySet())) {
Object key = entry.getKey();
String name = key.toString();
if (entry.getValue() != null
&& entry.getValue().toString().startsWith("{vault}")) {
if (entry.getValue() != null && entry.getValue().toString().startsWith("{vault}")) {
String value = entry.getValue().toString();
map.remove(key);
try {
@@ -81,8 +80,7 @@ public class VaultEnvironmentEncryptor implements EnvironmentEncryptor {
throw new RuntimeException("Wrong format");
}
if (StringUtils.isEmpty(parts[0])
|| StringUtils.isEmpty(parts[1])) {
if (StringUtils.isEmpty(parts[0]) || StringUtils.isEmpty(parts[1])) {
throw new RuntimeException("Wrong format");
}
@@ -95,21 +93,19 @@ public class VaultEnvironmentEncryptor implements EnvironmentEncryptor {
VaultResponse vaultResponse = loadedVaultKeys.get(vaultKey);
if (vaultResponse == null
|| (vaultResponse.getData() == null || !vaultResponse
.getData().containsKey(vaultParamName))) {
if (vaultResponse == null || (vaultResponse.getData() == null
|| !vaultResponse.getData().containsKey(vaultParamName))) {
value = null;
}
else {
value = vaultResponse.getData().get(vaultParamName)
.toString();
value = vaultResponse.getData().get(vaultParamName).toString();
}
}
catch (Exception e) {
value = "<n/a>";
name = "invalid." + name;
String message = "Cannot resolve key: " + key + " ("
+ e.getClass() + ": " + e.getMessage() + ")";
String message = "Cannot resolve key: " + key + " (" + e.getClass() + ": " + e.getMessage()
+ ")";
if (logger.isDebugEnabled()) {
logger.debug(message, e);
}

View File

@@ -50,16 +50,14 @@ public class PcfClientAuthenticationProvider extends SpringVaultClientAuthentica
builder.instanceCertificate(new ResourceCredentialSupplier(pcfProperties.getInstanceCertificate()));
}
else {
builder.instanceCertificate(new ResourceCredentialSupplier(
resolveEnvVariable("CF_INSTANCE_CERT")));
builder.instanceCertificate(new ResourceCredentialSupplier(resolveEnvVariable("CF_INSTANCE_CERT")));
}
if (pcfProperties.getInstanceKey() != null) {
builder.instanceKey(new ResourceCredentialSupplier(pcfProperties.getInstanceKey()));
}
else {
builder.instanceKey(new ResourceCredentialSupplier(
resolveEnvVariable("CF_INSTANCE_KEY")));
builder.instanceKey(new ResourceCredentialSupplier(resolveEnvVariable("CF_INSTANCE_KEY")));
}
return new PcfAuthentication(builder.build(), vaultRestOperations);
@@ -70,8 +68,7 @@ public class PcfClientAuthenticationProvider extends SpringVaultClientAuthentica
String value = System.getenv(name);
if (StringUtils.isEmpty(value)) {
throw new IllegalStateException(
String.format("Environment variable %s not set", name));
throw new IllegalStateException(String.format("Environment variable %s not set", name));
}
return value;

View File

@@ -26,7 +26,6 @@ import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

View File

@@ -42,20 +42,18 @@ public class VaultEnvironmentEncryptorTests {
String secret = "mysecret";
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
when(keyValueTemplate.get("accounts/mypay"))
.thenReturn(withVaultResponse("access_key", secret));
when(keyValueTemplate.get("accounts/mypay")).thenReturn(withVaultResponse("access_key", secret));
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a", Collections.<Object, Object>singletonMap(
environment.getName(), "{vault}:accounts/mypay#access_key")));
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), "{vault}:accounts/mypay#access_key")));
// then
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource()
.get(environment.getName())).isEqualTo(secret);
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource().get(environment.getName()))
.isEqualTo(secret);
}
@Test
@@ -64,20 +62,18 @@ public class VaultEnvironmentEncryptorTests {
String secret = "mysecret";
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
when(keyValueTemplate.get("accounts/mypay"))
.thenReturn(withVaultResponse("access_key", secret));
when(keyValueTemplate.get("accounts/mypay")).thenReturn(withVaultResponse("access_key", secret));
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a", Collections.<Object, Object>singletonMap(
environment.getName(), "{vault}:accounts/mypay#another_key")));
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), "{vault}:accounts/mypay#another_key")));
// then
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource()
.get(environment.getName())).isNull();
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource().get(environment.getName()))
.isNull();
}
@Test
@@ -87,17 +83,16 @@ public class VaultEnvironmentEncryptorTests {
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), value)));
environment
.add(new PropertySource("a", Collections.<Object, Object>singletonMap(environment.getName(), value)));
// then
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource()
.get(environment.getName())).isEqualTo(value);
assertThat(encryptor.decrypt(environment).getPropertySources().get(0).getSource().get(environment.getName()))
.isEqualTo(value);
}
@Test
@@ -107,21 +102,19 @@ public class VaultEnvironmentEncryptorTests {
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), value)));
environment
.add(new PropertySource("a", Collections.<Object, Object>singletonMap(environment.getName(), value)));
// then
Environment processedEnvironment = encryptor.decrypt(environment);
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get("invalid." + environment.getName())).isEqualTo("<n/a>");
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get("invalid." + environment.getName()))
.isEqualTo("<n/a>");
}
@Test
@@ -131,21 +124,19 @@ public class VaultEnvironmentEncryptorTests {
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), value)));
environment
.add(new PropertySource("a", Collections.<Object, Object>singletonMap(environment.getName(), value)));
// then
Environment processedEnvironment = encryptor.decrypt(environment);
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get("invalid." + environment.getName())).isEqualTo("<n/a>");
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get("invalid." + environment.getName()))
.isEqualTo("<n/a>");
}
@Test
@@ -155,21 +146,19 @@ public class VaultEnvironmentEncryptorTests {
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), value)));
environment
.add(new PropertySource("a", Collections.<Object, Object>singletonMap(environment.getName(), value)));
// then
Environment processedEnvironment = encryptor.decrypt(environment);
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get("invalid." + environment.getName())).isEqualTo("<n/a>");
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get("invalid." + environment.getName()))
.isEqualTo("<n/a>");
}
@Test
@@ -179,21 +168,19 @@ public class VaultEnvironmentEncryptorTests {
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(
keyValueTemplate);
VaultEnvironmentEncryptor encryptor = new VaultEnvironmentEncryptor(keyValueTemplate);
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), value)));
environment
.add(new PropertySource("a", Collections.<Object, Object>singletonMap(environment.getName(), value)));
// then
Environment processedEnvironment = encryptor.decrypt(environment);
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource()
.get("invalid." + environment.getName())).isEqualTo("<n/a>");
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get(environment.getName())).isNull();
assertThat(processedEnvironment.getPropertySources().get(0).getSource().get("invalid." + environment.getName()))
.isEqualTo("<n/a>");
}
private VaultResponse withVaultResponse(String key, Object value) {

View File

@@ -337,11 +337,8 @@ public class JGitEnvironmentRepositoryTests {
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url"))
.thenReturn("http://example/git");
when(statusCommand.call())
.thenThrow(new JGitInternalException("Short read of block."))
.thenReturn(status);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException("Short read of block.")).thenReturn(status);
when(status.isClean()).thenReturn(true);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.setMode(any())).thenReturn(resetCommand);
@@ -368,10 +365,8 @@ public class JGitEnvironmentRepositoryTests {
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url"))
.thenReturn("http://example/git");
when(statusCommand.call())
.thenThrow(new JGitInternalException("Short read of block."));
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException("Short read of block."));
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
@@ -383,8 +378,7 @@ public class JGitEnvironmentRepositoryTests {
assertThat(shouldPull).as("shouldPull did not fail").isFalse();
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected")
.isEqualTo("Short read of block.");
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo("Short read of block.");
}
}
@@ -402,10 +396,8 @@ public class JGitEnvironmentRepositoryTests {
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url"))
.thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException(mockThrownMessage))
.thenReturn(status);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException(mockThrownMessage)).thenReturn(status);
when(status.isClean()).thenReturn(true);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.call()).thenThrow(new GitAPIException("") {
@@ -419,8 +411,7 @@ public class JGitEnvironmentRepositoryTests {
Assertions.fail("shouldPull did not fail");
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected")
.isEqualTo(mockThrownMessage);
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo(mockThrownMessage);
}
}
@@ -437,10 +428,8 @@ public class JGitEnvironmentRepositoryTests {
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url"))
.thenReturn("http://example/git");
when(statusCommand.call())
.thenThrow(new JGitInternalException(mockThrownMessage));
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException(mockThrownMessage));
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
@@ -451,8 +440,7 @@ public class JGitEnvironmentRepositoryTests {
Assertions.fail("shouldPull did not fail");
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected")
.isEqualTo(mockThrownMessage);
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo(mockThrownMessage);
}
}