Snippets
Created by
Tom Paulus
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.log4j.Logger;
import java.util.HashMap;
/**
* Manages Requests that go to the Vault, which stores all sensitive key data, like API Keys, Password, etc.
* An AppRole ID and Secret ID are required for the connection to be established. All "Application Names" are expected
* to be stored under the "secret" backend.
* <p>
* Uses "VAULT_ADDR", "VAULT_ROLE", "VAULT_SECRET", and "WELCOME_APP" environment variables to set the Vault Configuration.
*
* @author Tom Paulus
* Created on 11/3/16.
*/
public class Vault {
final private static String VAULT_ADDR = System.getenv("VAULT_ADDR").endsWith("/") ? System.getenv("VAULT_ADDR") : System.getenv("VAULT_ADDR") + "/";
final private static String ROLE_ID = System.getenv("VAULT_ROLE");
final private static String SECRET_ID = System.getenv("VAULT_SECRET");
final private static String APP_NAME = System.getenv("WELCOME_APP");
final private static Logger LOGGER = Logger.getLogger(Vault.class);
private static String token = null;
private static String getToken() {
Gson gson = new Gson();
if (token == null) {
AppRoleTokenRequest request = new AppRoleTokenRequest(ROLE_ID, SECRET_ID);
HttpResponse vaultResponse;
try {
LOGGER.debug("Requesting New Token via AppRole");
vaultResponse = Unirest
.post(VAULT_ADDR + "v1/auth/approle/login")
.body(gson.toJson(request))
.asJson();
} catch (UnirestException e) {
LOGGER.error("Problem making request to AppRole Login endpoint at Vault", e);
return null;
}
if (vaultResponse.getStatus() != 200) {
LOGGER.error(String.format("Problem Getting Login Token from Vault. HTTP Status: %d", vaultResponse.getStatus()));
return null;
}
Response response = gson.fromJson(vaultResponse.getBody().toString(), Response.class);
LOGGER.info("Retrieved Token from Vault");
LOGGER.debug(String.format("Token: %s", response.getToken()));
token = response.getToken();
} else {
HttpResponse vaultResponse;
try {
LOGGER.debug("Reviewing Token via Renew Request to Vault");
LOGGER.debug(String.format("Current Token: %s", token));
vaultResponse = Unirest
.post(VAULT_ADDR + "v1/auth/token/renew-self")
.header("X-Vault-Token", token)
.asJson();
} catch (UnirestException e) {
LOGGER.error("Problem making request to Renew Vault Token", e);
return null;
}
Response response = gson.fromJson(vaultResponse.getBody().toString(), Response.class);
if (vaultResponse.getStatus() != 200) {
LOGGER.warn(String.format("Problem Renewing Token HTTP Status: %d", vaultResponse.getStatus()));
token = null;
} else {
LOGGER.info("Renewed Vault Token");
if (!token.equals(response.getToken())) {
LOGGER.info("Token has changed");
LOGGER.debug(String.format("New Token: %s", response.getToken()));
} else {
LOGGER.debug("Token was not changed during renew");
}
token = response.getToken();
}
if (token == null) token = getToken();
}
return token;
}
public static boolean testConnection() {
String value = getParam("hello", "value");
if (value != null && value.isEmpty()) {
LOGGER.warn("There is no value saved for the key \"secret\\hello\"");
}
LOGGER.info(String.format("Value for \"secret\\hello\" is \"%s\"", value));
return value != null && !value.isEmpty();
}
/**
* Get Parameter form the Default Application (whose name is an environment variable)
*
* @param parameterName {@link String} Parameter Name
* @return {@link String} Parameter Value
*/
public static String getParam(final String parameterName) {
return getParam(APP_NAME, parameterName);
}
/**
* Retrieve Param from Key Server
*
* @param applicationName {@link String} Application that the parameter is associated with
* @param parameterName {@link String} Parameter Name
* @return {@link String} Parameter Value
*/
public static String getParam(final String applicationName, final String parameterName) {
HttpResponse vaultResponse;
Gson gson = new Gson();
try {
LOGGER.debug(String.format("Requesting Secret Node: \"%s\" Value: \"%s\" from Vault", applicationName, parameterName));
vaultResponse = Unirest
.get(VAULT_ADDR + "v1/secret/" + applicationName.toLowerCase())
.header("X-Vault-Token", getToken())
.asJson();
} catch (UnirestException e) {
LOGGER.error("Problem making request to AppRole Login endpoint at Vault", e);
return null;
}
SecretRequest request = gson.fromJson(vaultResponse.getBody().toString(), SecretRequest.class);
return request.data.get(parameterName);
}
private static class AppRoleTokenRequest {
String role_id;
String secret_id;
public AppRoleTokenRequest(String role_id, String secret_id) {
this.role_id = role_id;
this.secret_id = secret_id;
}
}
private static class Response {
Auth auth;
public String getToken() {
return auth.client_token;
}
private static class Auth {
String client_token;
}
}
private static class SecretRequest {
HashMap<String, String> data;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.