|
17 | 17 | package com.nike.cerberus.client; |
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.google.gson.FieldNamingPolicy; |
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.GsonBuilder; |
| 23 | +import com.google.gson.JsonSyntaxException; |
20 | 24 | import com.nike.cerberus.domain.cms.SafeDepositBox; |
21 | 25 | import com.nike.cerberus.domain.cms.SdbMetadataResult; |
22 | 26 | import com.nike.vault.client.UrlResolver; |
23 | 27 | import com.nike.vault.client.VaultAdminClient; |
24 | 28 | import com.nike.vault.client.VaultClientException; |
| 29 | +import com.nike.vault.client.VaultServerException; |
25 | 30 | import com.nike.vault.client.auth.VaultCredentialsProvider; |
26 | 31 | import com.nike.vault.client.http.HttpHeader; |
27 | 32 | import com.nike.vault.client.http.HttpMethod; |
@@ -55,6 +60,11 @@ public class CerberusAdminClient extends VaultAdminClient { |
55 | 60 | protected UrlResolver vaultUrlResolver; |
56 | 61 | protected ObjectMapper objectMapper; |
57 | 62 |
|
| 63 | + protected final Gson gson = new GsonBuilder() |
| 64 | + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 65 | + .disableHtmlEscaping() |
| 66 | + .create(); |
| 67 | + |
58 | 68 | /** |
59 | 69 | * Explicit constructor that allows for full control over construction of the Vault client. |
60 | 70 | * |
@@ -176,4 +186,50 @@ protected <M> M parseCmsResponseBody(final Response response, final Class<M> res |
176 | 186 | throw new VaultClientException("Error parsing the response body from CMS", e); |
177 | 187 | } |
178 | 188 | } |
| 189 | + |
| 190 | + /** |
| 191 | + * Read operation for a specified path. Will return a {@link Map} of the data stored at the specified path. |
| 192 | + * If Vault returns an unexpected response code, a {@link VaultServerException} will be thrown with the code |
| 193 | + * and error details. If an unexpected I/O error is encountered, a {@link VaultClientException} will be thrown |
| 194 | + * wrapping the underlying exception. |
| 195 | + * |
| 196 | + * @param path Path to the data |
| 197 | + * @return Map of the data |
| 198 | + */ |
| 199 | + public GenericVaultResponse readDataGenerically(final String path) { |
| 200 | + final HttpUrl url = buildUrl(SECRET_PATH_PREFIX, path); |
| 201 | + log.debug("read: requestUrl={}", url); |
| 202 | + |
| 203 | + final Response response = execute(url, HttpMethod.GET, null); |
| 204 | + |
| 205 | + if (response.code() != HttpStatus.OK) { |
| 206 | + parseAndThrowErrorResponse(response); |
| 207 | + } |
| 208 | + |
| 209 | + return parseResponseBody(response, GenericVaultResponse.class); |
| 210 | + } |
| 211 | + |
| 212 | + public class GenericVaultResponse { |
| 213 | + private Map<String, Object> data; |
| 214 | + |
| 215 | + public Map<String, Object> getData() { |
| 216 | + return data; |
| 217 | + } |
| 218 | + |
| 219 | + public GenericVaultResponse setData(Map<String, Object> data) { |
| 220 | + this.data = data; |
| 221 | + return this; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + protected <M> M parseResponseBody(final Response response, final Class<M> responseClass) { |
| 226 | + final String responseBodyStr = responseBodyAsString(response); |
| 227 | + try { |
| 228 | + return gson.fromJson(responseBodyStr, responseClass); |
| 229 | + } catch (JsonSyntaxException e) { |
| 230 | + log.error("parseResponseBody: responseCode={}, requestUrl={}", |
| 231 | + response.code(), response.request().url()); |
| 232 | + throw new VaultClientException("Error parsing the response body from vault, response code: " + response.code(), e); |
| 233 | + } |
| 234 | + } |
179 | 235 | } |
0 commit comments