Skip to content

Commit

Permalink
Changed the list() methods to return a LogicalResult so the status co…
Browse files Browse the repository at this point in the history
…de can be checked. Added new method getListData() to LogicalResult
  • Loading branch information
Barry Klawans authored and jetersen committed Aug 26, 2019
1 parent a684096 commit 3231da3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
19 changes: 3 additions & 16 deletions src/main/java/com/bettercloud/vault/api/Logical.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
* @return A list of keys corresponding to key/value pairs at a given Vault path, or an empty list if there are none
* @throws VaultException If any errors occur, or unexpected response received from Vault
*/
public List<String> list(final String path) throws VaultException {
public LogicalResponse list(final String path) throws VaultException {
if (engineVersionForSecretPath(path).equals(2)) {
return list(path, logicalOperations.listV2);
} else return list(path, logicalOperations.listV1);
}

private List<String> list(final String path, final logicalOperations operation) throws VaultException {
private LogicalResponse list(final String path, final logicalOperations operation) throws VaultException {
LogicalResponse response = null;
try {
response = read(adjustPathForList(path, operation), true, operation);
Expand All @@ -321,20 +321,7 @@ private List<String> list(final String path, final logicalOperations operation)
}
}

final List<String> returnValues = new ArrayList<>();
if (
response != null
&& response.getRestResponse().getStatus() != 404
&& response.getData() != null
&& response.getData().get("keys") != null
) {

final JsonArray keys = Json.parse(response.getData().get("keys")).asArray();
for (int index = 0; index < keys.size(); index++) {
returnValues.add(keys.get(index).asString());
}
}
return returnValues;
return response;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/bettercloud/vault/response/LogicalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.bettercloud.vault.api.Logical;
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonArray;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.json.JsonValue;
import com.bettercloud.vault.rest.RestResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -16,6 +19,7 @@
public class LogicalResponse extends VaultResponse {

private Map<String, String> data = new HashMap<>();
private List<String> listData = new ArrayList<>();
private JsonObject dataObject = null;
private String leaseId;
private Boolean renewable;
Expand All @@ -36,6 +40,10 @@ public Map<String, String> getData() {
return data;
}

public List<String> getListData() {
return listData;
}

public JsonObject getDataObject() {
return dataObject;
}
Expand Down Expand Up @@ -83,6 +91,20 @@ private void parseResponseData(final Logical.logicalOperations operation) {
data.put(member.getName(), jsonValue.toString());
}
}
// For list operations convert the array of keys to a list of values
if (operation.equals(Logical.logicalOperations.listV1) || operation.equals(Logical.logicalOperations.listV2)) {
if (
getRestResponse().getStatus() != 404
&& data.get("keys") != null
) {

final JsonArray keys = Json.parse(data.get("keys")).asArray();
for (int index = 0; index < keys.size(); index++) {
listData.add(keys.get(index).asString());
}
}

}
} catch (Exception ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void testList() throws VaultException {
testMap.put("value", "world");

vault.logical().write("secret/hello", testMap);
final List<String> keys = vault.logical().list("secret");
final List<String> keys = vault.logical().list("secret").getListData();
assertTrue(keys.contains("hello"));
}

Expand All @@ -180,7 +180,7 @@ public void testListKVEngineV1() throws VaultException {
testMap.put("value", "world");

vault.logical().write("kv-v1/hello", testMap);
final List<String> keys = vault.logical().list("kv-v1");
final List<String> keys = vault.logical().list("kv-v1").getListData();
assertTrue(keys.contains("hello"));
}

Expand All @@ -196,9 +196,9 @@ public void testDelete() throws VaultException {
testMap.put("value", "world");

vault.logical().write("secret/hello", testMap);
assertTrue(vault.logical().list("secret").contains("hello"));
assertTrue(vault.logical().list("secret").getListData().contains("hello"));
vault.logical().delete("secret/hello");
assertFalse(vault.logical().list("secret").contains("hello"));
assertFalse(vault.logical().list("secret").getListData().contains("hello"));
}

/**
Expand All @@ -213,9 +213,9 @@ public void testDeleteKVEngineV1() throws VaultException {
testMap.put("value", "world");

vault.logical().write("kv-v1/hello", testMap);
assertTrue(vault.logical().list("kv-v1").contains("hello"));
assertTrue(vault.logical().list("kv-v1").getListData().contains("hello"));
vault.logical().delete("kv-v1/hello");
assertFalse(vault.logical().list("kv-v1").contains("hello"));
assertFalse(vault.logical().list("kv-v1").getListData().contains("hello"));
}

/**
Expand Down Expand Up @@ -295,8 +295,8 @@ public void testDeleteExceptionMessageIncludesErrorsReturnedByVault() throws Vau
@Test
public void testListPermissionDeniedReturnedByVault() throws VaultException {
final Vault vault = container.getVault(NONROOT_TOKEN);
List<String> list = vault.logical().list("secret/null");
assertEquals(list.size(), 0);
LogicalResponse response = vault.logical().list("secret/null");
assertEquals(404, response.getRestResponse().getStatus());
}

/**
Expand Down

0 comments on commit 3231da3

Please sign in to comment.