Skip to content

Commit 3231da3

Browse files
Barry Klawansjetersen
authored andcommitted
Changed the list() methods to return a LogicalResult so the status code can be checked. Added new method getListData() to LogicalResult
1 parent a684096 commit 3231da3

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/main/java/com/bettercloud/vault/api/Logical.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
305305
* @return A list of keys corresponding to key/value pairs at a given Vault path, or an empty list if there are none
306306
* @throws VaultException If any errors occur, or unexpected response received from Vault
307307
*/
308-
public List<String> list(final String path) throws VaultException {
308+
public LogicalResponse list(final String path) throws VaultException {
309309
if (engineVersionForSecretPath(path).equals(2)) {
310310
return list(path, logicalOperations.listV2);
311311
} else return list(path, logicalOperations.listV1);
312312
}
313313

314-
private List<String> list(final String path, final logicalOperations operation) throws VaultException {
314+
private LogicalResponse list(final String path, final logicalOperations operation) throws VaultException {
315315
LogicalResponse response = null;
316316
try {
317317
response = read(adjustPathForList(path, operation), true, operation);
@@ -321,20 +321,7 @@ private List<String> list(final String path, final logicalOperations operation)
321321
}
322322
}
323323

324-
final List<String> returnValues = new ArrayList<>();
325-
if (
326-
response != null
327-
&& response.getRestResponse().getStatus() != 404
328-
&& response.getData() != null
329-
&& response.getData().get("keys") != null
330-
) {
331-
332-
final JsonArray keys = Json.parse(response.getData().get("keys")).asArray();
333-
for (int index = 0; index < keys.size(); index++) {
334-
returnValues.add(keys.get(index).asString());
335-
}
336-
}
337-
return returnValues;
324+
return response;
338325
}
339326

340327
/**

src/main/java/com/bettercloud/vault/response/LogicalResponse.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.bettercloud.vault.api.Logical;
44
import com.bettercloud.vault.json.Json;
5+
import com.bettercloud.vault.json.JsonArray;
56
import com.bettercloud.vault.json.JsonObject;
67
import com.bettercloud.vault.json.JsonValue;
78
import com.bettercloud.vault.rest.RestResponse;
89
import java.nio.charset.StandardCharsets;
10+
import java.util.ArrayList;
911
import java.util.HashMap;
12+
import java.util.List;
1013
import java.util.Map;
1114

1215
/**
@@ -16,6 +19,7 @@
1619
public class LogicalResponse extends VaultResponse {
1720

1821
private Map<String, String> data = new HashMap<>();
22+
private List<String> listData = new ArrayList<>();
1923
private JsonObject dataObject = null;
2024
private String leaseId;
2125
private Boolean renewable;
@@ -36,6 +40,10 @@ public Map<String, String> getData() {
3640
return data;
3741
}
3842

43+
public List<String> getListData() {
44+
return listData;
45+
}
46+
3947
public JsonObject getDataObject() {
4048
return dataObject;
4149
}
@@ -83,6 +91,20 @@ private void parseResponseData(final Logical.logicalOperations operation) {
8391
data.put(member.getName(), jsonValue.toString());
8492
}
8593
}
94+
// For list operations convert the array of keys to a list of values
95+
if (operation.equals(Logical.logicalOperations.listV1) || operation.equals(Logical.logicalOperations.listV2)) {
96+
if (
97+
getRestResponse().getStatus() != 404
98+
&& data.get("keys") != null
99+
) {
100+
101+
final JsonArray keys = Json.parse(data.get("keys")).asArray();
102+
for (int index = 0; index < keys.size(); index++) {
103+
listData.add(keys.get(index).asString());
104+
}
105+
}
106+
107+
}
86108
} catch (Exception ignored) {
87109
}
88110
}

src/test-integration/java/com/bettercloud/vault/api/LogicalTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void testList() throws VaultException {
164164
testMap.put("value", "world");
165165

166166
vault.logical().write("secret/hello", testMap);
167-
final List<String> keys = vault.logical().list("secret");
167+
final List<String> keys = vault.logical().list("secret").getListData();
168168
assertTrue(keys.contains("hello"));
169169
}
170170

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

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

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

198198
vault.logical().write("secret/hello", testMap);
199-
assertTrue(vault.logical().list("secret").contains("hello"));
199+
assertTrue(vault.logical().list("secret").getListData().contains("hello"));
200200
vault.logical().delete("secret/hello");
201-
assertFalse(vault.logical().list("secret").contains("hello"));
201+
assertFalse(vault.logical().list("secret").getListData().contains("hello"));
202202
}
203203

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

215215
vault.logical().write("kv-v1/hello", testMap);
216-
assertTrue(vault.logical().list("kv-v1").contains("hello"));
216+
assertTrue(vault.logical().list("kv-v1").getListData().contains("hello"));
217217
vault.logical().delete("kv-v1/hello");
218-
assertFalse(vault.logical().list("kv-v1").contains("hello"));
218+
assertFalse(vault.logical().list("kv-v1").getListData().contains("hello"));
219219
}
220220

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

302302
/**

0 commit comments

Comments
 (0)