Skip to content

Commit 2e8cf71

Browse files
authored
Add custom Vault Authentication Path when using k8s login method (#27)
Signed-off-by: dttung2905 <[email protected]>
1 parent f32c8ba commit 2e8cf71

File tree

1 file changed

+29
-2
lines changed
  • src/main/java/io/github/jopenlibs/vault/api

1 file changed

+29
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,12 +987,39 @@ public AuthResponse loginByGithub(final String githubToken, final String githubA
987987
// TODO: Needs integration test coverage if possible
988988
public AuthResponse loginByJwt(final String provider, final String role, final String jwt)
989989
throws VaultException {
990+
991+
return loginByJwt(provider, role, jwt, "auth/" + provider);
992+
}
993+
994+
/**
995+
* <p>Basic login operation to authenticate to an JWT backend with custom authentication path. Example usage:</p>
996+
*
997+
* <blockquote>
998+
* <pre>{@code
999+
* final AuthResponse response = vault.auth().loginByJwt("kubernetes", "dev", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "custom/path");
1000+
*
1001+
* final String token = response.getAuthClientToken();
1002+
* }</pre>
1003+
* </blockquote>
1004+
*
1005+
* @param provider Provider of JWT token.
1006+
* @param role The gcp role used for authentication
1007+
* @param jwt The JWT token for the role
1008+
* @param authPath The Authentication Path for Vault
1009+
* @return The auth token, with additional response metadata
1010+
* @throws VaultException If any error occurs, or unexpected response received from Vault
1011+
*/
1012+
// TODO: Needs integration test coverage if possible
1013+
public AuthResponse loginByJwt(final String provider, final String role, final String jwt,
1014+
String authPath)
1015+
throws VaultException {
1016+
9901017
return retry(attempt -> {
9911018
// HTTP request to Vault
9921019
final String requestJson = Json.object().add("role", role).add("jwt", jwt)
9931020
.toString();
9941021
final RestResponse restResponse = new Rest()
995-
.url(config.getAddress() + "/v1/auth/" + provider + "/login")
1022+
.url(config.getAddress() + "/v1/" + authPath + "/login")
9961023
.header("X-Vault-Namespace", this.nameSpace)
9971024
.header("X-Vault-Request", "true")
9981025
.body(requestJson.getBytes(StandardCharsets.UTF_8))
@@ -1042,7 +1069,7 @@ public AuthResponse loginByGCP(final String role, final String jwt) throws Vault
10421069

10431070

10441071
/**
1045-
* Basic login operation to authenticate to an kubernetes backend. Example usage:
1072+
* Basic login operation to authenticate to a kubernetes backend. Example usage:
10461073
*
10471074
* <blockquote>
10481075
*

0 commit comments

Comments
 (0)