Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit ed268e3

Browse files
authored
Bugfix/process vault data generically (#63)
* Process Vault data genericaly and overwrite parse response body so it doesn't leak data if there is an error * update version
1 parent cee08ec commit ed268e3

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
group=com.nike
1818
artifactId=cerberus-lifecycle-cli
19-
version=3.2.0
19+
version=3.2.1

src/main/java/com/nike/cerberus/client/CerberusAdminClient.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
package com.nike.cerberus.client;
1818

1919
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;
2024
import com.nike.cerberus.domain.cms.SafeDepositBox;
2125
import com.nike.cerberus.domain.cms.SdbMetadataResult;
2226
import com.nike.vault.client.UrlResolver;
2327
import com.nike.vault.client.VaultAdminClient;
2428
import com.nike.vault.client.VaultClientException;
29+
import com.nike.vault.client.VaultServerException;
2530
import com.nike.vault.client.auth.VaultCredentialsProvider;
2631
import com.nike.vault.client.http.HttpHeader;
2732
import com.nike.vault.client.http.HttpMethod;
@@ -55,6 +60,11 @@ public class CerberusAdminClient extends VaultAdminClient {
5560
protected UrlResolver vaultUrlResolver;
5661
protected ObjectMapper objectMapper;
5762

63+
protected final Gson gson = new GsonBuilder()
64+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
65+
.disableHtmlEscaping()
66+
.create();
67+
5868
/**
5969
* Explicit constructor that allows for full control over construction of the Vault client.
6070
*
@@ -176,4 +186,50 @@ protected <M> M parseCmsResponseBody(final Response response, final Class<M> res
176186
throw new VaultClientException("Error parsing the response body from CMS", e);
177187
}
178188
}
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+
}
179235
}

src/main/java/com/nike/cerberus/domain/cms/SafeDepositBox.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class SafeDepositBox {
3636
private String lastUpdatedBy;
3737
private Map<String, String> userGroupPermissions;
3838
private Map<String, String> iamRolePermissions;
39-
private Map<String, Map<String, String>> data = new HashMap<>();
39+
private Map<String, Map<String, Object>> data = new HashMap<>();
4040

4141
public String getName() {
4242
return name;
@@ -134,11 +134,11 @@ public void setIamRolePermissions(Map<String, String> iamRolePermissions) {
134134
this.iamRolePermissions = iamRolePermissions;
135135
}
136136

137-
public Map<String, Map<String, String>> getData() {
137+
public Map<String, Map<String, Object>> getData() {
138138
return data;
139139
}
140140

141-
public void setData(Map<String, Map<String, String>> data) {
141+
public void setData(Map<String, Map<String, Object>> data) {
142142
this.data = data;
143143
}
144144
}

src/main/java/com/nike/cerberus/operation/core/CreateCerberusBackupOperation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void run(CreateCerberusBackupCommand command) {
133133
CerberusSdbMetadata cerberusSdbMetadata = new CerberusSdbMetadata();
134134
for (SafeDepositBox sdb : sdbMetadataList) {
135135
log.info(String.format("Backing up %s", sdb.getName()));
136-
Map<String, Map<String, String>> vaultData = recurseVault(sdb.getPath(), new HashMap<>());
136+
Map<String, Map<String, Object>> vaultData = recurseVault(sdb.getPath(), new HashMap<>());
137137
sdb.setData(vaultData);
138138
String key = sdb.getName().toLowerCase().replaceAll("\\W+", "-");
139139
saveDataToS3(sdb, prefix, key, regionsToStoreBackups);
@@ -217,7 +217,7 @@ private CerberusSdbMetadata processMetadata(SafeDepositBox sdb, final CerberusSd
217217
newMetadata.getUniqueNonOwnerGroups().add(userGroup);
218218
});
219219

220-
Map<String, Map<String, String>> vaultNodes = sdb.getData();
220+
Map<String, Map<String, Object>> vaultNodes = sdb.getData();
221221
newMetadata.setNumberOfDataNodes(newMetadata.getNumberOfDataNodes() + vaultNodes.size());
222222
vaultNodes.forEach((path, kvPairs) -> {
223223
newMetadata.setNumberOfKeyValuePairs(newMetadata.getNumberOfKeyValuePairs() + kvPairs.size());
@@ -231,7 +231,7 @@ private CerberusSdbMetadata processMetadata(SafeDepositBox sdb, final CerberusSd
231231
* @param path The path to recurse
232232
* @return Map of Vault path Strings to Maps of String, String containing the secret kv pairs
233233
*/
234-
private Map<String, Map<String, String>> recurseVault(String path, Map<String, Map<String, String>> data) {
234+
private Map<String, Map<String, Object>> recurseVault(String path, Map<String, Map<String, Object>> data) {
235235
List<String> keys = getKeys(path);
236236

237237
keys.forEach(key -> {
@@ -263,8 +263,8 @@ private List<String> getKeys(String path) {
263263
* @param path The path of data to download
264264
* @return The data map
265265
*/
266-
private Map<String, String> getData(String path) {
267-
VaultResponse response = cerberusAdminClient.read(path);
266+
private Map<String, Object> getData(String path) {
267+
CerberusAdminClient.GenericVaultResponse response = cerberusAdminClient.readDataGenerically(path);
268268
return response.getData();
269269
}
270270

0 commit comments

Comments
 (0)