diff --git a/pom.xml b/pom.xml index f56ee2a..4157760 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,16 @@ + + org.apache.maven.plugins + maven-site-plugin + 3.3 + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.7 + org.apache.maven.plugins maven-assembly-plugin diff --git a/src/main/java/io/vaultproject/javaclientexample/App.java b/src/main/java/io/vaultproject/javaclientexample/App.java index 2990cd7..fcca6f3 100644 --- a/src/main/java/io/vaultproject/javaclientexample/App.java +++ b/src/main/java/io/vaultproject/javaclientexample/App.java @@ -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 @@ -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 secrets = new HashMap(); secrets.put("value", "world"); - - - + try { VaultSecret vault = new VaultSecret(); @@ -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 ciphertext = vault.encryptSecret(usingKey, plainText); + Map 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 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); } + + } } diff --git a/src/main/java/io/vaultproject/javaclientexample/VaultSecret.java b/src/main/java/io/vaultproject/javaclientexample/VaultSecret.java index 25c8ae6..8b79382 100644 --- a/src/main/java/io/vaultproject/javaclientexample/VaultSecret.java +++ b/src/main/java/io/vaultproject/javaclientexample/VaultSecret.java @@ -105,6 +105,48 @@ public String decryptSecret(String key, String cipherText){ } + + public void rotateKeys(String key) { + String transit = "transit/keys/"+key+"/rotate"; + + Map empty = new HashMap(); + + 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 empty = new HashMap(); + + 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); + } + + + } }