-
Notifications
You must be signed in to change notification settings - Fork 33
Infer azure tenant ID. #482
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
renaudhartert-db
merged 10 commits into
databricks:main
from
sreekanth-db:infer-azure-tenant-id
Aug 19, 2025
+268
−6
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9c7ae67
infer azure tenant id
sreekanth-db 56859bf
unit test fix
sreekanth-db 312c32a
changelog
sreekanth-db f06c006
addressing review comments
sreekanth-db cf281fd
moved inferring logic to azure utils
sreekanth-db 9f9f7a1
code formatting
sreekanth-db 22adeed
azure utils throwing exception
sreekanth-db 21ba14f
Merge branch 'main' into infer-azure-tenant-id
renaudhartert-db 0bb96a2
removing logger from azureutils
sreekanth-db ea7ebc3
Merge branch 'main' into infer-azure-tenant-id
renaudhartert-db 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
172 changes: 172 additions & 0 deletions
172
databricks-sdk-java/src/test/java/com/databricks/sdk/core/utils/AzureUtilsTest.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,172 @@ | ||
| package com.databricks.sdk.core.utils; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import com.databricks.sdk.core.DatabricksConfig; | ||
| import com.databricks.sdk.core.DatabricksException; | ||
| import com.databricks.sdk.core.FixtureServer; | ||
| import com.databricks.sdk.core.commons.CommonsHttpClient; | ||
| import java.io.IOException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class AzureUtilsTest { | ||
|
|
||
| @Test | ||
| public void testInferTenantId404() throws IOException { | ||
| try (FixtureServer server = new FixtureServer().with("GET", "/aad/auth", "", 404)) { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost(server.getUrl()); | ||
| config.setAzureWorkspaceResourceId( | ||
| "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
|
|
||
| DatabricksException exception = | ||
| assertThrows( | ||
| DatabricksException.class, | ||
| () -> { | ||
| AzureUtils.inferTenantId(config); | ||
| }); | ||
| assertEquals( | ||
| "Failed to infer Azure tenant ID from " + server.getUrl() + "/aad/auth", | ||
| exception.getMessage()); | ||
|
|
||
| assertNotNull(exception.getCause()); | ||
| assertInstanceOf(DatabricksException.class, exception.getCause()); | ||
| DatabricksException cause = (DatabricksException) exception.getCause(); | ||
| assertEquals( | ||
| "Expected redirect (302) from " + server.getUrl() + "/aad/auth, got status code: 404", | ||
| cause.getMessage()); | ||
|
|
||
| assertNull(config.getAzureTenantId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdNoLocationHeader() throws IOException { | ||
| try (FixtureServer server = new FixtureServer().with("GET", "/aad/auth", "", 302)) { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost(server.getUrl()); | ||
| config.setAzureWorkspaceResourceId( | ||
| "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
|
|
||
| DatabricksException exception = | ||
| assertThrows( | ||
| DatabricksException.class, | ||
| () -> { | ||
| AzureUtils.inferTenantId(config); | ||
| }); | ||
| assertEquals( | ||
| "Failed to infer Azure tenant ID from " + server.getUrl() + "/aad/auth", | ||
| exception.getMessage()); | ||
|
|
||
| assertNotNull(exception.getCause()); | ||
| assertInstanceOf(DatabricksException.class, exception.getCause()); | ||
| DatabricksException cause = (DatabricksException) exception.getCause(); | ||
| assertEquals( | ||
| "No Location header in redirect response from " + server.getUrl() + "/aad/auth", | ||
| cause.getMessage()); | ||
|
|
||
| assertNull(config.getAzureTenantId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdUnparsableLocationHeader() throws IOException { | ||
| FixtureServer.FixtureMapping fixture = | ||
| new FixtureServer.FixtureMapping.Builder() | ||
| .validateMethod("GET") | ||
| .validatePath("/aad/auth") | ||
| .withRedirect("https://unexpected-location", 302) | ||
| .build(); | ||
|
|
||
| try (FixtureServer server = new FixtureServer().with(fixture)) { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost(server.getUrl()); | ||
| config.setAzureWorkspaceResourceId( | ||
| "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
|
|
||
| DatabricksException exception = | ||
| assertThrows( | ||
| DatabricksException.class, | ||
| () -> { | ||
| AzureUtils.inferTenantId(config); | ||
| }); | ||
| assertEquals( | ||
| "Failed to infer Azure tenant ID from " + server.getUrl() + "/aad/auth", | ||
| exception.getMessage()); | ||
|
|
||
| assertNotNull(exception.getCause()); | ||
| assertInstanceOf(DatabricksException.class, exception.getCause()); | ||
| DatabricksException cause = (DatabricksException) exception.getCause(); | ||
| assertEquals( | ||
| "Failed to parse tenant ID from URL https://unexpected-location", cause.getMessage()); | ||
|
|
||
| assertNull(config.getAzureTenantId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdHappyPath() throws IOException { | ||
| FixtureServer.FixtureMapping fixture = | ||
| new FixtureServer.FixtureMapping.Builder() | ||
| .validateMethod("GET") | ||
| .validatePath("/aad/auth") | ||
| .withRedirect("https://login.microsoftonline.com/test-tenant-id/oauth2/authorize", 302) | ||
| .build(); | ||
|
|
||
| try (FixtureServer server = new FixtureServer().with(fixture)) { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost(server.getUrl()); | ||
| config.setAzureWorkspaceResourceId( | ||
| "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
| String result = AzureUtils.inferTenantId(config); | ||
| assertEquals("test-tenant-id", result); | ||
| assertNull(config.getAzureTenantId()); // Config should remain unchanged | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdSkipsWhenNotAzure() { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost("https://my-workspace.cloud.databricks.com"); // non-azure host | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
|
|
||
| DatabricksException exception = | ||
| assertThrows( | ||
| DatabricksException.class, | ||
| () -> { | ||
| AzureUtils.inferTenantId(config); | ||
| }); | ||
| assertEquals("Cannot infer tenant ID: workspace is not Azure", exception.getMessage()); | ||
| assertNull(config.getAzureTenantId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdSkipsWhenAlreadySet() { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHost("https://adb-123.0.azuredatabricks.net"); | ||
| config.setAzureTenantId("existing-tenant-id"); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
| String result = AzureUtils.inferTenantId(config); | ||
| assertEquals("existing-tenant-id", result); | ||
| assertEquals("existing-tenant-id", config.getAzureTenantId()); // Config should remain unchanged | ||
| } | ||
|
|
||
| @Test | ||
| public void testInferTenantIdSkipsWhenNoHost() { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); | ||
|
|
||
| DatabricksException exception = | ||
| assertThrows( | ||
| DatabricksException.class, | ||
| () -> { | ||
| AzureUtils.inferTenantId(config); | ||
| }); | ||
| assertEquals("Cannot infer tenant ID: host is missing", exception.getMessage()); | ||
| assertNull(config.getAzureTenantId()); | ||
| } | ||
| } |
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.
Also remove
private static final Logger logger = LoggerFactory.getLogger(AzureUtils.class);