Skip to content
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

Add acceptance tests for auth resolution #2285

Merged
merged 28 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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 acceptance/auth/basic/out.requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"headers":{"Authorization":["Basic dXNlcm5hbWU6cGFzc3dvcmQ="],"User-Agent":["cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/current-user_me cmd-exec-id/[UUID] auth/basic"]},"method":"GET","path":"/api/2.0/preview/scim/v2/Me","body":""}
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions acceptance/auth/basic/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id":"[USERID]",
"userName":"[USERNAME]"
}
8 changes: 8 additions & 0 deletions acceptance/auth/basic/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Unset the token which is configured by default
# in acceptance tests
export DATABRICKS_TOKEN=""

export DATABRICKS_USERNAME=username
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
export DATABRICKS_PASSWORD=password

$CLI current-user me
20 changes: 20 additions & 0 deletions acceptance/auth/basic/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
LocalOnly = true

RecordRequests = true
IncludeRequestHeaders = ["Authorization", "User-Agent"]

[[Repls]]
Old = '(linux|darwin|windows)'
New = '[OS]'

[[Repls]]
Old = " upstream/[A-Za-z0-9.-]+"
New = ""

[[Repls]]
Old = " upstream-version/[A-Za-z0-9.-]+"
New = ""

[[Repls]]
Old = " cicd/[A-Za-z0-9.-]+"
New = ""
3 changes: 3 additions & 0 deletions acceptance/auth/oauth/out.requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"headers":{"User-Agent":["cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]"]},"method":"GET","path":"/oidc/.well-known/oauth-authorization-server","body":""}
{"headers":{"Authorization":["Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="],"User-Agent":["cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]"]},"method":"POST","path":"/oidc/v1/token","body":"grant_type=client_credentials\u0026scope=all-apis"}
{"headers":{"Authorization":["Bearer oauth-token"],"User-Agent":["cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/current-user_me cmd-exec-id/[UUID] auth/oauth-m2m"]},"method":"GET","path":"/api/2.0/preview/scim/v2/Me","body":""}
4 changes: 4 additions & 0 deletions acceptance/auth/oauth/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id":"[USERID]",
"userName":"[USERNAME]"
}
8 changes: 8 additions & 0 deletions acceptance/auth/oauth/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Unset the token which is configured by default
# in acceptance tests
export DATABRICKS_TOKEN=""

export DATABRICKS_CLIENT_ID=client_id
export DATABRICKS_CLIENT_SECRET=client_secret

$CLI current-user me
20 changes: 20 additions & 0 deletions acceptance/auth/oauth/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
LocalOnly = true

RecordRequests = true
IncludeRequestHeaders = ["Authorization", "User-Agent"]

[[Repls]]
Old = '(linux|darwin|windows)'
New = '[OS]'

[[Repls]]
Old = " upstream/[A-Za-z0-9.-]+"
New = ""

[[Repls]]
Old = " upstream-version/[A-Za-z0-9.-]+"
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
New = ""

[[Repls]]
Old = " cicd/[A-Za-z0-9.-]+"
New = ""
1 change: 1 addition & 0 deletions acceptance/auth/pat/out.requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"headers":{"Authorization":["Bearer dapi1234"],"User-Agent":["cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/current-user_me cmd-exec-id/[UUID] auth/pat"]},"method":"GET","path":"/api/2.0/preview/scim/v2/Me","body":""}
4 changes: 4 additions & 0 deletions acceptance/auth/pat/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id":"[USERID]",
"userName":"[USERNAME]"
}
3 changes: 3 additions & 0 deletions acceptance/auth/pat/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export DATABRICKS_TOKEN=dapi1234

$CLI current-user me
20 changes: 20 additions & 0 deletions acceptance/auth/pat/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
LocalOnly = true

RecordRequests = true
IncludeRequestHeaders = ["Authorization", "User-Agent"]

[[Repls]]
Old = '(linux|darwin|windows)'
New = '[OS]'

[[Repls]]
Old = " upstream/[A-Za-z0-9.-]+"
New = ""

[[Repls]]
Old = " upstream-version/[A-Za-z0-9.-]+"
New = ""

[[Repls]]
Old = " cicd/[A-Za-z0-9.-]+"
New = ""
16 changes: 16 additions & 0 deletions acceptance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ func AddHandlers(server *testserver.Server) {
server.Handle("GET /api/2.1/jobs/list", func(fakeWorkspace *testserver.FakeWorkspace, r *http.Request) (any, int) {
return fakeWorkspace.JobsList()
})

server.Handle("GET /oidc/.well-known/oauth-authorization-server", func(fakeWorkspace *testserver.FakeWorkspace, r *http.Request) (any, int) {
return map[string]string{
"authorization_endpoint": server.URL + "oidc/v1/authorize",
"token_endpoint": server.URL + "/oidc/v1/token",
}, http.StatusOK
})

server.Handle("POST /oidc/v1/token", func(fakeWorkspace *testserver.FakeWorkspace, r *http.Request) (any, int) {
return map[string]string{
"access_token": "oauth-token",
"expires_in": "3600",
"scope": "all-apis",
"token_type": "Bearer",
}, http.StatusOK
})
}

func internalError(err error) (any, int) {
Expand Down
14 changes: 13 additions & 1 deletion libs/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,23 @@ func (s *Server) Handle(pattern string, handler HandlerFunc) {
}
}

var reqBody any
if json.Valid(body) {
// If the request body is a valid JSON, typecast it to json.RawMessage.
// This way json.Marshal will ignore the body and serialize it
// as is, which is what we want because the body is already a JSON.
reqBody = json.RawMessage(body)
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
} else {
// JSON marshal encodes []byte to base64. Typecase it to string
// to avoid this.
reqBody = string(body)
}

s.Requests = append(s.Requests, Request{
Headers: headers,
Method: r.Method,
Path: r.URL.Path,
Body: json.RawMessage(body),
Body: reqBody,
})

}
Expand Down