Skip to content

Commit 343b3e5

Browse files
authored
Improve engine version detection (#70)
1 parent 30d90a9 commit 343b3e5

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/main/java/io/github/jopenlibs/vault/api/Logical.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,39 @@ public LogicalResponse upgrade(final String kvPath) throws VaultException {
596596
});
597597
}
598598

599+
/**
600+
* <p>Searches the secrets engine path map for the specified path.</p>
601+
*
602+
* For example, if the path map contains:
603+
* <pre>
604+
* "cubbyhole/" -> "unknown"
605+
* "identity/" -> "unknown"
606+
* "sys/" -> "unknown"
607+
* "secret/" -> "2"
608+
* </pre>
609+
*
610+
* and the secret path is "secret/myapp/config", this method will check, in order:
611+
* <ul>
612+
* <li>"secret/myapp/config/" - not found</li>
613+
* <li>"secret/myapp/" - not found</li>
614+
* <li>"secret/" - found, engine version 2</li>
615+
* </ul>
616+
*
617+
* @param secretPath The Vault secret path to check (e.g. <code>secret/hello</code>).
618+
* @return the detected engine version (1 or 2), or the global default if not found
619+
*/
599620
private Integer engineVersionForSecretPath(final String secretPath) {
600-
if (!this.config.getSecretsEnginePathMap().isEmpty()) {
601-
return this.config.getSecretsEnginePathMap().containsKey(secretPath + "/") ?
602-
Integer.valueOf(this.config.getSecretsEnginePathMap().get(secretPath + "/"))
603-
: this.config.getGlobalEngineVersion();
621+
final Map<String, String> pathMap = this.config.getSecretsEnginePathMap();
622+
if (!pathMap.isEmpty()) {
623+
int idx = secretPath.length();
624+
do {
625+
final String prefix = secretPath.substring(0, idx);
626+
final String version = pathMap.get(prefix + '/');
627+
if (version != null && !version.equals("unknown")) {
628+
return Integer.parseInt(version);
629+
}
630+
idx = prefix.lastIndexOf('/');
631+
} while (idx != -1);
604632
}
605633
return this.config.getGlobalEngineVersion();
606634
}

src/test/java/io/github/jopenlibs/vault/VaultTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ public void testVaultWithEmptyKVEnginePathMap() throws VaultException {
7070
Assert.assertNull(vault);
7171
}
7272

73+
@Test
74+
public void testVaultWithUnknownKVEnginePathMap() throws VaultException {
75+
Map<String, String> engineKVMap = new HashMap<>();
76+
engineKVMap.put("secret/", "unknown");
77+
VaultConfig vaultConfig = new VaultConfig().secretsEnginePathMap(engineKVMap);
78+
Vault vault = Vault.create(vaultConfig, true, 1);
79+
Assert.assertNotNull(vault);
80+
Assert.assertEquals(String.valueOf(1),
81+
vault.logical().getEngineVersionForSecretPath("secret").toString());
82+
}
83+
7384
@Test
7485
public void testVaultWithoutKVEnginePathMap() throws VaultException {
7586
Map<String, String> engineKVMap = new HashMap<>();
@@ -97,6 +108,24 @@ public void kvEngineMapIsHonored() throws VaultException {
97108
vault.logical().getEngineVersionForSecretPath("notInMap").toString());
98109
}
99110

111+
@Test
112+
public void testVaultWithPrefixedKVEnginePathMap() throws VaultException {
113+
Map<String, String> engineKVMap = new HashMap<>();
114+
engineKVMap.put("secret/", "2");
115+
engineKVMap.put("other/mount/", "2");
116+
VaultConfig vaultConfig = new VaultConfig().secretsEnginePathMap(engineKVMap);
117+
Vault vault = Vault.create(vaultConfig, true, 1);
118+
Assert.assertNotNull(vault);
119+
Assert.assertEquals(String.valueOf(2),
120+
vault.logical().getEngineVersionForSecretPath("secret/path/to/credential").toString());
121+
Assert.assertEquals(String.valueOf(2),
122+
vault.logical().getEngineVersionForSecretPath("other/mount/path/to/credential").toString());
123+
Assert.assertEquals(String.valueOf(1),
124+
vault.logical().getEngineVersionForSecretPath("other").toString());
125+
Assert.assertEquals(String.valueOf(1),
126+
vault.logical().getEngineVersionForSecretPath("notInMap").toString());
127+
}
128+
100129
@Test
101130
public void testConfigBuiler_WithInvalidRequestAsNonError() throws Exception {
102131
final MockVault mockVault = new MockVault(403,

0 commit comments

Comments
 (0)