-
Notifications
You must be signed in to change notification settings - Fork 33
Support Databricks Workload Identity Federation for GitHub tokens #423
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/GitHubOidcTokenSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.databricks.sdk.core.oauth; | ||
|
|
||
| import com.databricks.sdk.core.DatabricksException; | ||
| import com.databricks.sdk.core.http.HttpClient; | ||
| import com.databricks.sdk.core.http.Request; | ||
| import com.databricks.sdk.core.http.Response; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import java.io.IOException; | ||
|
|
||
| public class GitHubOidcTokenSupplier { | ||
|
|
||
| private final ObjectMapper mapper = new ObjectMapper(); | ||
| private final HttpClient httpClient; | ||
| private final String idTokenRequestUrl; | ||
| private final String idTokenRequestToken; | ||
| private final String tokenAudience; | ||
|
|
||
| public GitHubOidcTokenSupplier( | ||
| HttpClient httpClient, | ||
| String idTokenRequestUrl, | ||
| String idTokenRequestToken, | ||
| String tokenAudience) { | ||
| this.httpClient = httpClient; | ||
| this.idTokenRequestUrl = idTokenRequestUrl; | ||
| this.idTokenRequestToken = idTokenRequestToken; | ||
| this.tokenAudience = tokenAudience; | ||
| } | ||
|
|
||
| /** Checks if the required parameters are present to request a GitHub's OIDC token. */ | ||
| public Boolean enabled() { | ||
| return idTokenRequestUrl != null && idTokenRequestToken != null; | ||
| } | ||
|
|
||
| /** | ||
| * Requests a GitHub's OIDC token. | ||
| * | ||
| * @return A GitHub OIDC token. | ||
| */ | ||
| public String getOidcToken() { | ||
| if (!enabled()) { | ||
| throw new DatabricksException("Failed to request ID token: missing required parameters"); | ||
| } | ||
|
|
||
| String requestUrl = idTokenRequestUrl; | ||
| if (tokenAudience != null) { | ||
| requestUrl += "&audience=" + tokenAudience; | ||
| } | ||
|
|
||
| Request req = | ||
| new Request("GET", requestUrl).withHeader("Authorization", "Bearer " + idTokenRequestToken); | ||
|
|
||
| Response resp; | ||
| try { | ||
| resp = httpClient.execute(req); | ||
| } catch (IOException e) { | ||
| throw new DatabricksException( | ||
| "Failed to request ID token from " + requestUrl + ":" + e.getMessage(), e); | ||
| } | ||
|
|
||
| if (resp.getStatusCode() != 200) { | ||
| throw new DatabricksException( | ||
| "Failed to request ID token: status code " | ||
| + resp.getStatusCode() | ||
| + ", response body: " | ||
| + resp.getBody().toString()); | ||
| } | ||
|
|
||
| ObjectNode jsonResp; | ||
| try { | ||
| jsonResp = mapper.readValue(resp.getBody(), ObjectNode.class); | ||
| } catch (IOException e) { | ||
| throw new DatabricksException( | ||
| "Failed to request ID token: corrupted token: " + e.getMessage()); | ||
| } | ||
|
|
||
| return jsonResp.get("value").textValue(); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...s-sdk-java/src/main/java/com/databricks/sdk/core/oauth/GithubOidcCredentialsProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.databricks.sdk.core.oauth; | ||
|
|
||
| import com.databricks.sdk.core.CredentialsProvider; | ||
| import com.databricks.sdk.core.DatabricksConfig; | ||
| import com.databricks.sdk.core.DatabricksException; | ||
| import com.databricks.sdk.core.HeaderFactory; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * GithubOidcCredentialsProvider uses a Token Supplier to get a GitHub OIDC JWT Token and exchanges | ||
| * it for a Databricks Token. | ||
| */ | ||
| public class GithubOidcCredentialsProvider implements CredentialsProvider { | ||
|
|
||
| @Override | ||
| public String authType() { | ||
| return "github-oidc"; | ||
| } | ||
|
|
||
| @Override | ||
| public HeaderFactory configure(DatabricksConfig config) throws DatabricksException { | ||
| GitHubOidcTokenSupplier idTokenProvider = | ||
| new GitHubOidcTokenSupplier( | ||
| config.getHttpClient(), | ||
| config.getActionsIdTokenRequestUrl(), | ||
| config.getActionsIdTokenRequestToken(), | ||
| config.getTokenAudience()); | ||
|
|
||
| if (!idTokenProvider.enabled() || config.getHost() == null || config.getClientId() == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String endpointUrl; | ||
|
|
||
| try { | ||
| endpointUrl = config.getOidcEndpoints().getTokenEndpoint(); | ||
| } catch (IOException e) { | ||
| throw new DatabricksException("Unable to fetch OIDC endpoint: " + e.getMessage(), e); | ||
| } | ||
|
|
||
| ClientCredentials clientCredentials = | ||
| new ClientCredentials.Builder() | ||
| .withHttpClient(config.getHttpClient()) | ||
| .withClientId(config.getClientId()) | ||
| .withTokenUrl(endpointUrl) | ||
| .withScopes(Collections.singletonList("all-apis")) | ||
| .withAuthParameterPosition(AuthParameterPosition.HEADER) | ||
| .withEndpointParametersSupplier( | ||
| () -> | ||
| new ImmutableMap.Builder<String, String>() | ||
| .put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt") | ||
| .put("subject_token", idTokenProvider.getOidcToken()) | ||
| .put("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange") | ||
| .build()) | ||
| .build(); | ||
|
|
||
| return () -> { | ||
| Map<String, String> headers = new HashMap<>(); | ||
| headers.put("Authorization", "Bearer " + clientCredentials.getToken().getAccessToken()); | ||
| return headers; | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required for WIF/OIDC