Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom Vault Authentication Path when using k8s login method #27

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/main/java/io/github/jopenlibs/vault/api/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -987,12 +987,39 @@ public AuthResponse loginByGithub(final String githubToken, final String githubA
// TODO: Needs integration test coverage if possible
public AuthResponse loginByJwt(final String provider, final String role, final String jwt)
throws VaultException {

return loginByJwt(provider, role, jwt, "auth/" + provider);
}

/**
* <p>Basic login operation to authenticate to an JWT backend with custom authentication path. Example usage:</p>
*
* <blockquote>
* <pre>{@code
* final AuthResponse response = vault.auth().loginByJwt("kubernetes", "dev", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "custom/path");
*
* final String token = response.getAuthClientToken();
* }</pre>
* </blockquote>
*
* @param provider Provider of JWT token.
* @param role The gcp role used for authentication
* @param jwt The JWT token for the role
* @param authPath The Authentication Path for Vault
* @return The auth token, with additional response metadata
* @throws VaultException If any error occurs, or unexpected response received from Vault
*/
// TODO: Needs integration test coverage if possible
public AuthResponse loginByJwt(final String provider, final String role, final String jwt,
String authPath)
throws VaultException {

return retry(attempt -> {
// HTTP request to Vault
final String requestJson = Json.object().add("role", role).add("jwt", jwt)
.toString();
final RestResponse restResponse = new Rest()
.url(config.getAddress() + "/v1/auth/" + provider + "/login")
.url(config.getAddress() + "/v1/" + authPath + "/login")
.header("X-Vault-Namespace", this.nameSpace)
.header("X-Vault-Request", "true")
.body(requestJson.getBytes(StandardCharsets.UTF_8))
Expand Down Expand Up @@ -1042,7 +1069,7 @@ public AuthResponse loginByGCP(final String role, final String jwt) throws Vault


/**
* Basic login operation to authenticate to an kubernetes backend. Example usage:
* Basic login operation to authenticate to a kubernetes backend. Example usage:
*
* <blockquote>
*
Expand Down