Skip to content

Commit a5857ba

Browse files
committed
Fix namespace configuration issue
1 parent 0eb2be3 commit a5857ba

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

src/main/java/org/wso2/carbon/securevault/hashicorp/repository/HashiCorpSecretRepository.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,16 @@ public String getSecret(String alias) {
187187
public String getSecretFromVault(String address, String accessToken, Integer engineVersion, String namespace,
188188
String path) throws HashiCorpVaultException {
189189
try {
190-
VaultConfig config = new VaultConfig().address(address).token(accessToken).engineVersion(engineVersion)
191-
.build();
192-
190+
VaultConfig vaultConfig = new VaultConfig().address(address).token(accessToken)
191+
.engineVersion(engineVersion);
192+
String configuredNamespace = getConfiguredNamespace(namespace);
193+
// Configure namespace only when it is explicitly provided.
194+
if (configuredNamespace != null) {
195+
vaultConfig.nameSpace(configuredNamespace);
196+
}
197+
VaultConfig config = vaultConfig.build();
193198
Vault vault = new Vault(config);
194199
Logical logical = vault.logical();
195-
if (StringUtils.isNotEmpty(namespace)) {
196-
logical = logical.withNameSpace(namespace);
197-
}
198200
return logical.read(path).getData().get(VALUE_PARAMETER);
199201

200202
} catch (VaultException e) {
@@ -332,7 +334,13 @@ private void setTextFileName() {
332334
*/
333335
private String retrieveServiceToken(String roleId, String secretId) throws HashiCorpVaultException {
334336
try {
335-
final VaultConfig config = new VaultConfig().address(address).engineVersion(engineVersion).build();
337+
VaultConfig vaultConfig = new VaultConfig().address(address).engineVersion(engineVersion);
338+
String configuredNamespace = getConfiguredNamespace(namespace);
339+
// Configure namespace only when it is explicitly provided.
340+
if (configuredNamespace != null) {
341+
vaultConfig.nameSpace(configuredNamespace);
342+
}
343+
final VaultConfig config = vaultConfig.build();
336344

337345
Vault vault = new Vault(config);
338346
AuthResponse response = vault.auth().loginByAppRole(roleId, secretId);
@@ -348,6 +356,20 @@ private String retrieveServiceToken(String roleId, String secretId) throws Hashi
348356
}
349357
}
350358

359+
/**
360+
* Returns a trimmed Vault namespace when one is configured.
361+
*
362+
* @param namespace The Vault namespace to be trimmed and returned.
363+
* @return A trimmed Vault namespace when one is configured, else null.
364+
*/
365+
private String getConfiguredNamespace(String namespace) {
366+
367+
if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(namespace.trim())) {
368+
return null;
369+
}
370+
return namespace.trim();
371+
}
372+
351373
/**
352374
* Util method to Read the root token from the text file.
353375
*

0 commit comments

Comments
 (0)