-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathApp.java
More file actions
84 lines (57 loc) · 2.84 KB
/
App.java
File metadata and controls
84 lines (57 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
}
}
}