-
Notifications
You must be signed in to change notification settings - Fork 36
Move cloud-based credential filtering into DefaultCredentialsProvider #690
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
Open
hectorcast-db
wants to merge
2
commits into
main
Choose a base branch
from
move-cloud-filtering-to-default-credentials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−14
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| package com.databricks.sdk.core; | ||
|
|
||
| import com.databricks.sdk.core.oauth.*; | ||
| import com.databricks.sdk.core.utils.Cloud; | ||
| import com.databricks.sdk.support.InternalApi; | ||
| import com.google.common.base.Strings; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -18,6 +22,21 @@ | |
| public class DefaultCredentialsProvider implements CredentialsProvider { | ||
| private static final Logger LOG = LoggerFactory.getLogger(DefaultCredentialsProvider.class); | ||
|
|
||
| // cloudRequirements declares the cloud each strategy requires. DefaultCredentialsProvider uses | ||
| // this to skip cloud-specific strategies in auto-detect mode when the host cloud does not match. | ||
| // Cloud filtering is bypassed when authType is explicitly set. | ||
| private static final Map<String, Cloud> CLOUD_REQUIREMENTS; | ||
|
|
||
| static { | ||
| Map<String, Cloud> m = new HashMap<>(); | ||
| m.put("github-oidc-azure", Cloud.AZURE); | ||
| m.put("azure-client-secret", Cloud.AZURE); | ||
| m.put("azure-cli", Cloud.AZURE); | ||
| m.put("google-credentials", Cloud.GCP); | ||
| m.put("google-id", Cloud.GCP); | ||
| CLOUD_REQUIREMENTS = Collections.unmodifiableMap(m); | ||
| } | ||
|
|
||
| /* List of credential providers that will be tried in sequence */ | ||
| private List<CredentialsProvider> providers = new ArrayList<>(); | ||
|
|
||
|
|
@@ -40,6 +59,11 @@ public NamedIDTokenSource(String name, IDTokenSource idTokenSource) { | |
|
|
||
| public DefaultCredentialsProvider() {} | ||
|
|
||
| /** For testing: creates a provider with a fixed list of credential providers. */ | ||
| DefaultCredentialsProvider(List<CredentialsProvider> providers) { | ||
| this.providers = new ArrayList<>(providers); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is only used in testing, is it possible to move it to tests rather than define here? |
||
|
|
||
| /** | ||
| * Returns the current authentication type being used | ||
| * | ||
|
|
@@ -60,14 +84,25 @@ public String authType() { | |
| @Override | ||
| public synchronized HeaderFactory configure(DatabricksConfig config) { | ||
| addDefaultCredentialsProviders(config); | ||
| boolean explicitAuthType = config.getAuthType() != null && !config.getAuthType().isEmpty(); | ||
| for (CredentialsProvider provider : providers) { | ||
| if (config.getAuthType() != null | ||
| && !config.getAuthType().isEmpty() | ||
| && !provider.authType().equals(config.getAuthType())) { | ||
| if (explicitAuthType && !provider.authType().equals(config.getAuthType())) { | ||
| LOG.info( | ||
| "Ignoring {} auth, because {} is preferred", provider.authType(), config.getAuthType()); | ||
| continue; | ||
| } | ||
| // In auto-detect mode, skip cloud-specific strategies whose required cloud does not match | ||
| // the detected host cloud. When authType is explicitly set, cloud filtering is bypassed so | ||
| // that users can request any strategy regardless of detected cloud (e.g. "azure-cli" on GCP). | ||
| if (!explicitAuthType) { | ||
| Cloud requiredCloud = CLOUD_REQUIREMENTS.get(provider.authType()); | ||
| if (requiredCloud != null | ||
| && config.getDatabricksEnvironment().getCloud() != requiredCloud) { | ||
| LOG.debug( | ||
| "Skipping \"{}\" auth: not configured for {}", provider.authType(), requiredCloud); | ||
| continue; | ||
| } | ||
| } | ||
| try { | ||
| LOG.info("Trying {} auth", provider.authType()); | ||
| HeaderFactory headerFactory = provider.configure(config); | ||
|
|
||
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
72 changes: 72 additions & 0 deletions
72
...bricks-sdk-java/src/test/java/com/databricks/sdk/core/DefaultCredentialsProviderTest.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,72 @@ | ||
| package com.databricks.sdk.core; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class DefaultCredentialsProviderTest { | ||
|
|
||
| /** A credentials provider that records whether configure() was called. */ | ||
| private static class RecordingCredentialsProvider implements CredentialsProvider { | ||
| private final String name; | ||
| private boolean called = false; | ||
|
|
||
| RecordingCredentialsProvider(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String authType() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public HeaderFactory configure(DatabricksConfig config) { | ||
| called = true; | ||
| return null; | ||
| } | ||
|
|
||
| public boolean wasCalled() { | ||
| return called; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * In auto-detect mode (authType not set), azure-cli must be skipped on a GCP host because the | ||
| * detected cloud does not match the strategy's required cloud (AZURE). | ||
| */ | ||
| @Test | ||
| void testCloudFiltering_SkipsOnCloudMismatch() { | ||
| RecordingCredentialsProvider azureCli = new RecordingCredentialsProvider("azure-cli"); | ||
| DefaultCredentialsProvider provider = | ||
| new DefaultCredentialsProvider(Collections.singletonList(azureCli)); | ||
|
|
||
| DatabricksConfig config = new DatabricksConfig().setHost("https://xyz.gcp.databricks.com/"); | ||
| // configure() throws because no provider succeeds; that's expected | ||
| assertThrows(DatabricksException.class, () -> provider.configure(config)); | ||
|
|
||
| assertFalse( | ||
| azureCli.wasCalled(), "azure-cli should be skipped on a GCP host in auto-detect mode"); | ||
| } | ||
|
|
||
| /** | ||
| * When authType is explicitly set, cloud filtering is bypassed so that the named strategy is | ||
| * always attempted regardless of the detected host cloud. | ||
| */ | ||
| @Test | ||
| void testCloudFiltering_BypassesOnExplicitAuthType() { | ||
| RecordingCredentialsProvider azureCli = new RecordingCredentialsProvider("azure-cli"); | ||
| DefaultCredentialsProvider provider = | ||
| new DefaultCredentialsProvider(Collections.singletonList(azureCli)); | ||
|
|
||
| DatabricksConfig config = | ||
| new DatabricksConfig().setHost("https://xyz.gcp.databricks.com/").setAuthType("azure-cli"); | ||
| // configure() throws because azure-cli returns null; that's expected | ||
| assertThrows(DatabricksException.class, () -> provider.configure(config)); | ||
|
|
||
| assertTrue( | ||
| azureCli.wasCalled(), | ||
| "azure-cli should be called when authType is explicitly set, even on a GCP host"); | ||
| } | ||
| } |
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.
Is this something we already document in our SDK docs?