Skip to content
Merged
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
</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>
Expand Down
42 changes: 35 additions & 7 deletions src/main/java/io/vaultproject/javaclientexample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
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
Expand All @@ -19,11 +24,12 @@ public static void main( String[] args ) throws VaultException
* 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();
Expand All @@ -33,24 +39,46 @@ public static void main( String[] args ) throws VaultException
System.out.format( "Write request response : " + writeResponse.getRestResponse().getStatus() +"\n");

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

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

//Encrypt plaintext
String usingKey = "test";

String plainText = "test input";

Map<String, String> ciphertext = vault.encryptSecret(usingKey, plainText);
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(usingKey, ciphertext.get("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);
}


}
}
42 changes: 42 additions & 0 deletions src/main/java/io/vaultproject/javaclientexample/VaultSecret.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,48 @@ public String decryptSecret(String key, String cipherText){


}

public void rotateKeys(String key) {
String transit = "transit/keys/"+key+"/rotate";

Map<String, String> empty = new HashMap<String, String>();

LogicalResponse writeResponse = null;
try {

// Write operation
writeResponse = vault.logical().write(transit, empty);

System.out.println("keys rotated: " + writeResponse.getRestResponse().getStatus() );

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




}

public void createKeys(String key) {
String transit = "transit/keys/"+key;

Map<String, String> empty = new HashMap<String, String>();

LogicalResponse writeResponse = null;
try {

// Write operation
writeResponse = vault.logical().write(transit, empty);

// System.out.println("keys created: " + writeResponse.getRestResponse().getStatus() );

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


}


}