Skip to content

Commit

Permalink
Retrieve vault paths from environment
Browse files Browse the repository at this point in the history
Remove hardcoded path within Vault where script expects Jenkins
configuration, replacing it with using a value from environment.
Currently only a single path is supported.
  • Loading branch information
Torinthiel committed Feb 3, 2020
1 parent 9ed0a77 commit 6d6dbb7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The following configuration variables are supported:
* CASCB_VAULT_URL - The URL to Vault server
* CASCB_VAULT_USER - The username used to login
* CASCB_VAULT_PW - The password used to login
* CASCB_VAULT_PATHS - Comma-separated list of paths from which plugin should retrieve configuration
* CASCB_VAULT_FILE - Path to properties file that will be scanned for above variables

Each of the variables is supported with either `CASCB_` prefix as indicated in
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ docker run -ti \
-e CASCB_VAULT_URL=http://172.17.0.2:8200 \
-e CASCB_VAULT_USER=jenkins \
-e CASCB_VAULT_PW=S3cRet \
-e CASCB_VAULT_PATHS=secret/jenkins/config \
torinthiel/jenkins-bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum Configs {
VAULT_URL,
VAULT_USER,
VAULT_PW,
VAULT_PATHS,
VAULT_FILE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class VaultAccessor {
}

void configureVault() {
String vaultUrl = configVars.get(VAULT_URL).orElseThrow({new IllegalArgumentException("CASCB_VAULT_URL not provided")})
String vaultUrl = getOrThrow(VAULT_URL)
VaultConfig config = new VaultConfig()
.address(vaultUrl)
.build()
Expand All @@ -49,10 +49,15 @@ class VaultAccessor {
}

void readVariables(VaultConfig config) {
def data = vault.logical().read("secret/jenkins/config").getData()
def path = getOrThrow(VAULT_PATHS)
def data = vault.logical().read(path).getData()
values.putAll(data)
}

private String getOrThrow(Configs configName) {
return configVars.get(configName).orElseThrow({new IllegalArgumentException("CASCB_${configName} not provided")})
}

String getValue(VaultConfigKey key) {
return values.get(key.path)
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/pl/torinthiel/jenkins/bootstrap/SmokeIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void prepareJenkinsContainer() {
.withEnv("CASCB_VAULT_URL", "http://vault:8200/")
.withEnv("CASCB_VAULT_USER", "jenkins")
.withEnv("CASCB_VAULT_PW", "S3cRet")
.withEnv("CASCB_VAULT_PATHS", "secret/jenkins/config")
.withExposedPorts(JENKINS_PORT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.util.Optional;
import java.util.function.Function;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -37,6 +39,7 @@ void setUp() {
config.addMapping(Configs.VAULT_URL, "random_url");
config.addMapping(Configs.VAULT_USER, "username");
config.addMapping(Configs.VAULT_PW, "password");
config.addMapping(Configs.VAULT_PATHS, "secret/jenkins/config");
}

@Test
Expand Down Expand Up @@ -75,6 +78,33 @@ void shouldNotAskTwiceForValue() throws VaultException {
verify(vault.logical(), times(2)).read("secret/jenkins/config");
}

@Test
void shouldReadVaultPathFromEnv() throws VaultException {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("cascb_ssh_key", "Wrong value");
Map<String, String> resultsMap = new HashMap<>();
resultsMap.put("cascb_ssh_key", "Correct value");
when(vault.logical().read("secret/jenkins/config").getData()).thenReturn(errorMap);
when(vault.logical().read("secret/jenkins/correct").getData()).thenReturn(resultsMap);
config.addMapping(Configs.VAULT_PATHS, "secret/jenkins/correct");

VaultAccessor acc = new VaultAccessor(config, factory);
acc.configureVault();

String retVal = acc.getValue(VaultConfigKey.SSH_KEY);
assertEquals("Correct value", retVal);
}

@Test
void shouldThrowErrorWhenRequiredParamMissing() {
config.removeMapping(Configs.VAULT_URL);

Assertions.assertThrows(IllegalArgumentException.class, (Executable) () -> {
VaultAccessor acc = new VaultAccessor(config, factory);
acc.configureVault();
}, "CASCB_VAULT_URL is not provided"
);
}
}

class MockConfigVars implements Retriever {
Expand All @@ -88,4 +118,8 @@ public Optional<String> get(Configs configName) {
public void addMapping(Configs key, String value) {
config.put(key, value);
}

public void removeMapping(Configs key) {
config.remove(key);
}
}

0 comments on commit 6d6dbb7

Please sign in to comment.