Skip to content

Commit a575f7b

Browse files
Bhargava-Kotharupedja4
authored andcommitted
BAEL-1784 Spring Vault - Bhargava.Kotharu@gmail.com (eugenp#5094)
* BAEL-1784 Spring Vault Initial Commit * Added Vault Process initalizer and integration tests * Added comments and code clean up * Skip IntegrationTest in pom file * Fixed failing integration test * Code indentation
1 parent 5a09cee commit a575f7b

11 files changed

Lines changed: 436 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@
11061106
<module>spring-remoting</module>
11071107
<module>spring-reactor</module>
11081108
<module>spring-vertx</module>
1109+
<module>spring-vault</module>
11091110
<module>spring-jinq</module>
11101111
<module>spring-rest-embedded-tomcat</module>
11111112
<module>testing-modules/testing</module>

‎spring-vault/pom.xml‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>spring-vault</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
<name>spring-vault</name>
13+
<description>Spring Vault sample project</description>
14+
15+
<parent>
16+
<artifactId>parent-boot-2</artifactId>
17+
<groupId>com.baeldung</groupId>
18+
<version>0.0.1-SNAPSHOT</version>
19+
<relativePath>../parent-boot-2</relativePath>
20+
</parent>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.vault</groupId>
29+
<artifactId>spring-vault-core</artifactId>
30+
<version>${spring.vault.core.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-databind</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-surefire-plugin</artifactId>
53+
<version>${maven-surefire-plugin.version}</version>
54+
<configuration>
55+
<excludes>
56+
<exclude>**/*IntegrationTest.java</exclude>
57+
</excludes>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
<properties>
64+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
65+
<spring.vault.core.version>2.0.1.RELEASE</spring.vault.core.version>
66+
</properties>
67+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.springvault;
2+
3+
public class Credentials {
4+
5+
private String username;
6+
private String password;
7+
8+
public Credentials() {
9+
10+
}
11+
12+
public Credentials(String username, String password) {
13+
this.username = username;
14+
this.password = password;
15+
}
16+
17+
public String getUsername() {
18+
return username;
19+
}
20+
21+
public String getPassword() {
22+
return password;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Credential [username=" + username + ", password=" + password + "]";
28+
}
29+
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.baeldung.springvault;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.vault.authentication.TokenAuthentication;
10+
import org.springframework.vault.client.VaultEndpoint;
11+
import org.springframework.vault.core.VaultTemplate;
12+
import org.springframework.vault.core.env.VaultPropertySource;
13+
import org.springframework.vault.support.VaultResponse;
14+
import org.springframework.vault.support.VaultResponseSupport;
15+
16+
/**
17+
* Sample service to demonstrate storing and retrieval of secrets.
18+
*
19+
* NOTE: We need to configure Vault and provide the Vault uri in the properties file.
20+
*
21+
*/
22+
@Service
23+
public class CredentialsService {
24+
25+
@Autowired
26+
private VaultTemplate vaultTemplate;
27+
28+
/**
29+
* To Secure Credentials
30+
* @param credentials
31+
* @return VaultResponse
32+
* @throws URISyntaxException
33+
*/
34+
public void secureCredentials(Credentials credentials) throws URISyntaxException {
35+
36+
vaultTemplate.write("credentials/myapp", credentials);
37+
}
38+
39+
/**
40+
* To Retrieve Credentials
41+
* @return Credentials
42+
* @throws URISyntaxException
43+
*/
44+
public Credentials accessCredentials() throws URISyntaxException {
45+
46+
VaultResponseSupport<Credentials> response = vaultTemplate.read("credentials/myapp", Credentials.class);
47+
return response.getData();
48+
}
49+
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.springvault;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Sample application to demonstrate basics of Spring Vault
8+
*
9+
*/
10+
@SpringBootApplication
11+
public class SpringVaultApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(SpringVaultApplication.class, args);
15+
}
16+
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.springvault;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.vault.authentication.ClientAuthentication;
5+
import org.springframework.vault.authentication.TokenAuthentication;
6+
import org.springframework.vault.client.VaultEndpoint;
7+
import org.springframework.vault.config.AbstractVaultConfiguration;
8+
9+
/**
10+
* Example class to configure Vault beans using AbstractVaultConfiguration
11+
*
12+
*/
13+
@Configuration
14+
public class VaultConfig extends AbstractVaultConfiguration {
15+
16+
@Override
17+
public ClientAuthentication clientAuthentication() {
18+
return new TokenAuthentication("00000000-0000-0000-0000-000000000000");
19+
}
20+
21+
@Override
22+
public VaultEndpoint vaultEndpoint() {
23+
return VaultEndpoint.create("host", 8020);
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.springvault;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.Import;
5+
import org.springframework.context.annotation.PropertySource;
6+
import org.springframework.vault.authentication.ClientAuthentication;
7+
import org.springframework.vault.authentication.TokenAuthentication;
8+
import org.springframework.vault.client.VaultEndpoint;
9+
import org.springframework.vault.config.AbstractVaultConfiguration;
10+
import org.springframework.vault.config.EnvironmentVaultConfiguration;
11+
12+
/**
13+
* Example class to configure Vault beans using EnvironmentVaultConfiguration
14+
*
15+
*/
16+
@Configuration
17+
@PropertySource(value = { "vault-config.properties" })
18+
@Import(value = EnvironmentVaultConfiguration.class)
19+
public class VaultEnvironmentConfig {
20+
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vault.uri=https://localhost:8200
2+
vault.token=00000000-0000-0000-0000-000000000000
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.baeldung.springvault;
2+
3+
import java.io.BufferedReader;
4+
import java.io.Closeable;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.Map;
8+
9+
/**
10+
*
11+
* This is a test class to initialize Vault.
12+
*/
13+
public class VaultInitializer implements Closeable {
14+
15+
private static final String UNSEAL_KEY = "Unseal Key:";
16+
private static final String ROOT_TOKEN = "Root Token:";
17+
18+
private Process vaultProcess;
19+
private String unSealKey;
20+
private String rootToken;
21+
22+
public String getRootToken() {
23+
return rootToken;
24+
}
25+
26+
public String getUnSealKey() {
27+
return unSealKey;
28+
}
29+
30+
public static final VaultInitializer initializeValut() {
31+
VaultInitializer vaultProcess = new VaultInitializer();
32+
vaultProcess.start();
33+
// Secrets is by default enabled.
34+
vaultProcess.enableSecrets();
35+
return vaultProcess;
36+
}
37+
38+
@SuppressWarnings("unused")
39+
private void enableSecrets() {
40+
System.out.println("Enabling Secrets at path credentials/myapp...");
41+
ProcessBuilder pb = new ProcessBuilder("vault", "secrets", "enable", "-path=credentials/myapp", "kv");
42+
Map<String, String> map = pb.environment();
43+
map.put("VAULT_ADDR", "http://127.0.0.1:8200");
44+
try {
45+
Process p = pb.inheritIO()
46+
.start();
47+
p.waitFor();
48+
} catch (IOException e) {
49+
System.out.println("unable to enableSecrets" + e);
50+
} catch (InterruptedException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
public void start() {
56+
System.out.println("starting vault");
57+
// This starts the vault server.
58+
ProcessBuilder pb = new ProcessBuilder("vault", "server", "-dev");
59+
60+
try {
61+
vaultProcess = pb.start();
62+
// wait for initialization to complete.
63+
Thread.sleep(5000);
64+
} catch (IOException e) {
65+
System.out.println("unable to start vault in new process" + e);
66+
} catch (InterruptedException e) {
67+
System.out.println("Thread interrupted " + e);
68+
}
69+
extractUnsealKeyAndToken();
70+
}
71+
72+
/**
73+
* To get the root token which is generated every time server is initialized.
74+
*/
75+
private void extractUnsealKeyAndToken() {
76+
BufferedReader reader = new BufferedReader(new InputStreamReader(vaultProcess.getInputStream()));
77+
StringBuilder builder = new StringBuilder();
78+
String line = null;
79+
boolean tokenExtracted = false;
80+
try {
81+
while ((line = reader.readLine()) != null) {
82+
builder.append(line);
83+
builder.append(System.getProperty("line.separator"));
84+
if (line.contains(UNSEAL_KEY)) {
85+
String tmp = line.replace(UNSEAL_KEY, "");
86+
unSealKey = tmp.trim();
87+
} else if (line.contains(ROOT_TOKEN)) {
88+
String tmp = line.replace(ROOT_TOKEN, "");
89+
rootToken = tmp.trim();
90+
tokenExtracted = true;
91+
}
92+
if (tokenExtracted)
93+
break;
94+
System.out.println(line);
95+
}
96+
} catch (IOException e) {
97+
System.out.println("unable to read vault output" + e);
98+
}
99+
100+
String result = builder.toString();
101+
102+
System.out.println("Unseal Key {}" + unSealKey);
103+
System.out.println("Root Token {}" + rootToken);
104+
System.out.println(result);
105+
}
106+
107+
@Override
108+
public void close() throws IOException {
109+
110+
System.out.println("stoping vault");
111+
vaultProcess.destroy();
112+
}
113+
}

0 commit comments

Comments
 (0)