Skip to content
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
2 changes: 1 addition & 1 deletion config/auth_m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c M2mCredentials) Configure(ctx context.Context, cfg *Config) (credentials
ClientSecret: cfg.ClientSecret,
AuthStyle: oauth2.AuthStyleInHeader,
TokenURL: endpoints.TokenEndpoint,
Scopes: []string{"all-apis"},
Scopes: cfg.GetScopes(),
}).TokenSource(ctx)

visitor := refreshableVisitor(ts)
Expand Down
82 changes: 80 additions & 2 deletions config/auth_m2m_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package config

import (
"errors"
"net/http"
"net/url"
"testing"

"github.com/databricks/databricks-sdk-go/credentials/u2m"
"github.com/databricks/databricks-sdk-go/httpclient/fixtures"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -90,5 +91,82 @@ func TestM2mNotSupported(t *testing.T) {
},
},
})
require.ErrorIs(t, err, u2m.ErrOAuthNotSupported)
if !errors.Is(err, u2m.ErrOAuthNotSupported) {
t.Errorf("got error %v, want %v", err, u2m.ErrOAuthNotSupported)
}
}

func TestM2M_Scopes(t *testing.T) {
tests := []struct {
name string
scopes []string
want string
}{
{
name: "nil scopes uses default",
scopes: nil,
want: "all-apis",
},
{
name: "empty scopes uses default",
scopes: []string{},
want: "all-apis",
},
{
name: "single scope",
scopes: []string{"dashboards"},
want: "dashboards",
},
{
name: "multiple scopes are sorted",
scopes: []string{"jobs", "files:read", "mlflow"},
want: "files:read jobs mlflow",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transport := fixtures.MappingTransport{
"GET /oidc/.well-known/oauth-authorization-server": {
Response: u2m.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://localhost/auth",
TokenEndpoint: "https://localhost/token",
},
},
"POST /token": {
// The scope assertion: verifies the SDK sends the correct scope parameter.
ExpectedRequest: url.Values{
"grant_type": {"client_credentials"},
"scope": {tt.want},
},
Response: oauth2.Token{
TokenType: "Bearer",
AccessToken: "test-token",
},
},
}

cfg := &Config{
Host: "a",
ClientID: "b",
ClientSecret: "c",
AuthType: "oauth-m2m",
Scopes: tt.scopes,
HTTPTransport: transport,
ConfigFile: "/dev/null",
}

req, err := http.NewRequest("GET", "http://localhost", nil)
if err != nil {
t.Fatalf("http.NewRequest(): unexpected error: %v", err)
}
err = cfg.Authenticate(req)
if err != nil {
t.Fatalf("Authenticate(): unexpected error: %v", err)
}
if got, want := req.Header.Get("Authorization"), "Bearer test-token"; got != want {
t.Errorf("Authorization header: got %q, want %q", got, want)
}
})
}
}
Loading