Skip to content

Commit 89affe4

Browse files
author
saville
committed
Only support child tokens for AppRole credentials currently
1 parent 67a16c5 commit 89affe4

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/main/java/com/datapipe/jenkins/vault/credentials/AbstractVaultTokenCredentialWithExpiration.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,21 @@ protected Auth getVaultAuth(@NonNull Vault vault) {
4343
}
4444

4545
/**
46-
* Retrieves a new token with specific policies if a list of requested policies is provided.
46+
* Should be implemented by classes that support creating child tokens.
47+
* @return true if child tokens can be used, false otherwise
48+
*/
49+
protected boolean supportsChildTokens() {
50+
return false;
51+
}
52+
53+
/**
54+
* Retrieves a new child token with specific policies if a list of requested policies is provided.
4755
* @param vault the vault instance
4856
* @param policies the policies list
4957
* @return the new token or null if no policies are defined
5058
*/
51-
protected String getTokenWithPolicies(Vault vault, List<String> policies) {
52-
if (policies == null || policies.isEmpty()) {
59+
protected String getChildToken(Vault vault, List<String> policies) {
60+
if (!supportsChildTokens() || policies == null || policies.isEmpty()) {
5361
return null;
5462
}
5563
Auth auth = getVaultAuth(vault);
@@ -90,7 +98,7 @@ public Vault authorizeWithVault(VaultConfig config, List<String> policies) {
9098
config.token(tokenCache.get(cacheKey));
9199

92100
// After current token is configured, try to retrieve a new child token with limited policies
93-
String childToken = getTokenWithPolicies(vault, policies);
101+
String childToken = getChildToken(vault, policies);
94102
if (childToken != null) {
95103
// A new token was generated, put it in the cache and configure vault
96104
tokenCache.put(cacheKey, childToken);

src/main/java/com/datapipe/jenkins/vault/credentials/VaultAppRoleCredential.java

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public String getPath() {
4747
return path;
4848
}
4949

50+
@Override
51+
protected boolean supportsChildTokens() {
52+
return true;
53+
}
54+
5055
@Override
5156
public String getToken(Auth auth) {
5257
try {

src/test/java/com/datapipe/jenkins/vault/credentials/AbstractVaultTokenCredentialWithExpirationTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ protected ExampleVaultTokenCredentialWithExpiration(Vault vault) {
160160
this.vault = vault;
161161
}
162162

163+
@Override
164+
protected boolean supportsChildTokens() {
165+
return true;
166+
}
167+
163168
@Override
164169
protected Vault getVault(VaultConfig config) {
165170
return vault;

0 commit comments

Comments
 (0)