Skip to content

Tolerate trailing slash #1211

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 3 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Enabled asynchronous token refreshes by default ([#1208](https://github.com/databricks/databricks-sdk-go/pull/1208)).

### Bug Fixes
* Tolerate trailing slashes in hostnames in `Config`.

### Documentation

Expand Down
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,17 @@ func (c *Config) getOidcEndpoints(ctx context.Context) (*u2m.OAuthAuthorizationS
oauthClient := &u2m.BasicOAuthEndpointSupplier{
Client: c.refreshClient,
}
host := c.CanonicalHostName()
if c.IsAccountClient() {
return oauthClient.GetAccountOAuthEndpoints(ctx, c.Host, c.AccountID)
return oauthClient.GetAccountOAuthEndpoints(ctx, host, c.AccountID)
}
return oauthClient.GetWorkspaceOAuthEndpoints(ctx, c.Host)
return oauthClient.GetWorkspaceOAuthEndpoints(ctx, host)
}

func (c *Config) getOAuthArgument() (u2m.OAuthArgument, error) {
host := c.CanonicalHostName()
if c.IsAccountClient() {
return u2m.NewBasicAccountOAuthArgument(c.Host, c.AccountID)
return u2m.NewBasicAccountOAuthArgument(host, c.AccountID)
}
return u2m.NewBasicWorkspaceOAuthArgument(c.Host)
return u2m.NewBasicWorkspaceOAuthArgument(host)
}
150 changes: 126 additions & 24 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,136 @@ func TestAuthenticate_InvalidHostSet(t *testing.T) {
}

func TestConfig_getOidcEndpoints_account(t *testing.T) {
c := &Config{
Host: "https://accounts.cloud.databricks.com",
AccountID: "abc",
tests := []struct {
name string
host string
accountID string
}{
{
name: "without trailing slash",
host: "https://accounts.cloud.databricks.com",
accountID: "abc",
},
{
name: "with trailing slash",
host: "https://accounts.cloud.databricks.com/",
accountID: "abc",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Host: tt.host,
AccountID: tt.accountID,
}
got, err := c.getOidcEndpoints(context.Background())
assert.NoError(t, err)
assert.Equal(t, &u2m.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://accounts.cloud.databricks.com/oidc/accounts/abc/v1/authorize",
TokenEndpoint: "https://accounts.cloud.databricks.com/oidc/accounts/abc/v1/token",
}, got)
})
}
got, err := c.getOidcEndpoints(context.Background())
assert.NoError(t, err)
assert.Equal(t, &u2m.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://accounts.cloud.databricks.com/oidc/accounts/abc/v1/authorize",
TokenEndpoint: "https://accounts.cloud.databricks.com/oidc/accounts/abc/v1/token",
}, got)
}

func TestConfig_getOidcEndpoints_workspace(t *testing.T) {
c := &Config{
Host: "https://myworkspace.cloud.databricks.com",
HTTPTransport: fixtures.SliceTransport{
{
Method: "GET",
Resource: "/oidc/.well-known/oauth-authorization-server",
Status: 200,
Response: `{"authorization_endpoint": "https://myworkspace.cloud.databricks.com/oidc/v1/authorize", "token_endpoint": "https://myworkspace.cloud.databricks.com/oidc/v1/token"}`,
},
tests := []struct {
name string
host string
}{
{
name: "without trailing slash",
host: "https://myworkspace.cloud.databricks.com",
},
{
name: "with trailing slash",
host: "https://myworkspace.cloud.databricks.com/",
},
}
got, err := c.getOidcEndpoints(context.Background())
assert.NoError(t, err)
assert.Equal(t, &u2m.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://myworkspace.cloud.databricks.com/oidc/v1/authorize",
TokenEndpoint: "https://myworkspace.cloud.databricks.com/oidc/v1/token",
}, got)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Host: tt.host,
HTTPTransport: fixtures.SliceTransport{
{
Method: "GET",
Resource: "/oidc/.well-known/oauth-authorization-server",
Status: 200,
Response: `{"authorization_endpoint": "https://myworkspace.cloud.databricks.com/oidc/v1/authorize", "token_endpoint": "https://myworkspace.cloud.databricks.com/oidc/v1/token"}`,
},
},
}
got, err := c.getOidcEndpoints(context.Background())
assert.NoError(t, err)
assert.Equal(t, &u2m.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://myworkspace.cloud.databricks.com/oidc/v1/authorize",
TokenEndpoint: "https://myworkspace.cloud.databricks.com/oidc/v1/token",
}, got)
})
}
}

func TestConfig_getOAuthArgument_account(t *testing.T) {
tests := []struct {
name string
host string
accountID string
}{
{
name: "without trailing slash",
host: "https://accounts.cloud.databricks.com",
accountID: "abc",
},
{
name: "with trailing slash",
host: "https://accounts.cloud.databricks.com/",
accountID: "abc",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Host: tt.host,
AccountID: tt.accountID,
}
rawGot, err := c.getOAuthArgument()
assert.NoError(t, err)
got, ok := rawGot.(u2m.BasicAccountOAuthArgument)
assert.True(t, ok)
assert.Equal(t, "https://accounts.cloud.databricks.com", got.GetAccountHost())
assert.Equal(t, "abc", got.GetAccountId())
})
}
}

func TestConfig_getOAuthArgument_workspace(t *testing.T) {
tests := []struct {
name string
host string
}{
{
name: "without trailing slash",
host: "https://myworkspace.cloud.databricks.com",
},
{
name: "with trailing slash",
host: "https://myworkspace.cloud.databricks.com/",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Host: tt.host,
}
rawGot, err := c.getOAuthArgument()
assert.NoError(t, err)
got, ok := rawGot.(u2m.BasicWorkspaceOAuthArgument)
assert.True(t, ok)
assert.Equal(t, "https://myworkspace.cloud.databricks.com", got.GetWorkspaceHost())
})
}
}
Loading