Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>java-client-example</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.6
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
142 changes: 78 additions & 64 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.vaultproject.javaclientexample</groupId>
<artifactId>java-client-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>java-client-example</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bettercloud</groupId>
<artifactId>vault-java-driver</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>io.vaultproject.javaclientexample.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>io.vaultproject.javaclientexample.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.vaultproject.javaclientexample</groupId>
<artifactId>java-client-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>java-client-example</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bettercloud</groupId>
<artifactId>vault-java-driver</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>io.vaultproject.javaclientexample.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>io.vaultproject.javaclientexample.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
121 changes: 84 additions & 37 deletions src/main/java/io/vaultproject/javaclientexample/App.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,84 @@
package io.vaultproject.javaclientexample;
import com.bettercloud.vault.*;

/**
* Hello world from Vault!
*
*/
public class App
{
public static void main( String[] args ) throws VaultException
{
/* The com.bettercloud.vault driver automatically reads a
* a number of Environment Variables like VAULT_TOKEN or
* VAULT_ADDR, you should ensure those are set properly
* These are displayed just to ensure you have the
* right ones for demo purposes.
*/

String vaulttoken = System.getenv("VAULT_TOKEN");
String vaulthost = System.getenv("VAULT_ADDR");
System.out.format( "Using Vault Host %s\n", vaulthost);
System.out.format( "With Vault Token %s\n", vaulttoken );
/* This should be a separate method called from Main, then
* again for simplicity...
*/
final VaultConfig config = new VaultConfig().build();
final Vault vault = new Vault(config);
try {
final String value = vault.logical()
.read("secret/hello")
.getData().get("value");
System.out.format( "value key in secret/hello is " + value +"\n");
} catch(VaultException e) {
System.out.println("Exception thrown: " + e);
}
}
}
package io.vaultproject.javaclientexample;
import java.util.HashMap;
import java.util.Map;

import com.bettercloud.vault.*;
import com.bettercloud.vault.response.LogicalResponse;

/**
* Hello world from Vault!
*
*/
public class App
{

/* For Reference check out the Hashicorp Vault EaaS learn page:
https://learn.hashicorp.com/vault/encryption-as-a-service/eaas-transit
*/

public static void main( String[] args ) throws VaultException
{
/* The com.bettercloud.vault driver automatically reads a
* a number of Environment Variables like VAULT_TOKEN or
* VAULT_ADDR, you should ensure those are set properly
* These are displayed just to ensure you have the
* right ones for demo purposes.
*/


//Create secrets to save
Map<String, String> secrets = new HashMap<String, String>();
secrets.put("value", "world");


try {
VaultSecret vault = new VaultSecret();

//Write KV Secret
LogicalResponse writeResponse = vault.setKvSecret("secret/hello", secrets);
System.out.format( "Write request response : " + writeResponse.getRestResponse().getStatus() +"\n");

//read KV Secret
System.out.format( "value secret in secret/hello is " + vault.getKvSecret() +"\n");

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Create Encryption Keys
String encryptionKey = "demo";
vault.createKeys(encryptionKey);

//Encrypt plaintext

String plainText = "test input";

Map<String, String> ciphertext = vault.encryptSecret(encryptionKey, plainText);

System.out.format( "the encrypted Value is " + ciphertext.get("ciphertext") +"\n");


//Decrypt ciphertext
String plainTextResponse = vault.decryptSecret(encryptionKey, ciphertext.get("ciphertext"));
System.out.format( "the decrypted Value is " + plainTextResponse +"\n");


//Rotate Keys
vault.rotateKeys(encryptionKey);

//Encrypt plaintext after key rotate
plainText = "test 2";

Map<String, String> ciphertext2 = vault.encryptSecret(encryptionKey, plainText);

System.out.format( "the encrypted Value is " + ciphertext2.get("ciphertext") +"\n");

//Decrypt ciphertext
plainTextResponse = vault.decryptSecret(encryptionKey, ciphertext.get("ciphertext"));
System.out.format( "the decrypted Value is " + plainTextResponse +"\n");


} catch(VaultException e) {
System.out.println("Exception thrown: " + e);
}


}
}
Loading