-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathprofiles_test.go
More file actions
321 lines (286 loc) · 9.89 KB
/
profiles_test.go
File metadata and controls
321 lines (286 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package auth
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProfiles(t *testing.T) {
ctx := t.Context()
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
// Create a config file with a profile
err := databrickscfg.SaveToProfile(ctx, &config.Config{
ConfigFile: configFile,
Profile: "profile1",
Host: "abc.cloud.databricks.com",
Token: "token1",
AuthType: "pat",
})
require.NoError(t, err)
// Let the environment think we're using another profile
t.Setenv("DATABRICKS_HOST", "https://def.cloud.databricks.com")
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
// Load the profile
profile := &profileMetadata{Name: "profile1"}
profile.Load(ctx, configFile, true)
// Check the profile
assert.Equal(t, "profile1", profile.Name)
assert.Equal(t, "https://abc.cloud.databricks.com", profile.Host)
assert.Equal(t, "aws", profile.Cloud)
assert.Equal(t, "pat", profile.AuthType)
}
func TestProfilesDefaultMarker(t *testing.T) {
ctx := t.Context()
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
// Create two profiles.
for _, name := range []string{"profile-a", "profile-b"} {
err := databrickscfg.SaveToProfile(ctx, &config.Config{
ConfigFile: configFile,
Profile: name,
Host: "https://" + name + ".cloud.databricks.com",
Token: "token",
})
require.NoError(t, err)
}
// Set profile-a as the default.
err := databrickscfg.SetDefaultProfile(ctx, "profile-a", configFile)
require.NoError(t, err)
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
// Read back the default profile and verify.
defaultProfile, err := databrickscfg.GetDefaultProfile(ctx, configFile)
require.NoError(t, err)
assert.Equal(t, "profile-a", defaultProfile)
}
// newSPOGServer creates a mock SPOG server that returns account-scoped OIDC.
// It serves both validation endpoints since SPOG workspace profiles (with a
// real workspace_id) need CurrentUser.Me, while account profiles need
// Workspaces.List. The workspace-only newWorkspaceServer omits the account
// endpoint to prove routing correctness for non-SPOG hosts.
func newSPOGServer(t *testing.T, accountID string) *httptest.Server {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/databricks-config":
_ = json.NewEncoder(w).Encode(map[string]any{
"account_id": accountID,
"oidc_endpoint": r.Host + "/oidc/accounts/" + accountID,
})
case "/api/2.0/accounts/" + accountID + "/workspaces":
_ = json.NewEncoder(w).Encode([]map[string]any{})
case "/api/2.0/preview/scim/v2/Me":
// SPOG workspace profiles also need CurrentUser.Me to succeed.
_ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)
return server
}
// newWorkspaceServer creates a mock workspace server that returns workspace-scoped
// OIDC and only serves the workspace validation endpoint. The account validation
// endpoint returns 404 to prove the workspace path was taken.
func newWorkspaceServer(t *testing.T, accountID string) *httptest.Server {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/databricks-config":
_ = json.NewEncoder(w).Encode(map[string]any{
"account_id": accountID,
"oidc_endpoint": r.Host + "/oidc",
})
case "/api/2.0/preview/scim/v2/Me":
_ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)
return server
}
func TestProfileLoadSPOGConfigType(t *testing.T) {
spogServer := newSPOGServer(t, "spog-acct")
wsServer := newWorkspaceServer(t, "ws-acct")
cases := []struct {
name string
host string
accountID string
workspaceID string
wantValid bool
}{
{
name: "SPOG account profile validated as account",
host: spogServer.URL,
accountID: "spog-acct",
wantValid: true,
},
{
name: "SPOG workspace profile validated as workspace",
host: spogServer.URL,
accountID: "spog-acct",
workspaceID: "ws-123",
wantValid: true,
},
{
name: "SPOG profile with workspace_id=none validated as account",
host: spogServer.URL,
accountID: "spog-acct",
workspaceID: "none",
wantValid: true,
},
{
name: "classic workspace with account_id from discovery stays workspace",
host: wsServer.URL,
accountID: "ws-acct",
wantValid: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
content := "[test-profile]\nhost = " + tc.host + "\ntoken = test-token\n"
if tc.accountID != "" {
content += "account_id = " + tc.accountID + "\n"
}
if tc.workspaceID != "" {
content += "workspace_id = " + tc.workspaceID + "\n"
}
require.NoError(t, os.WriteFile(configFile, []byte(content), 0o600))
p := &profileMetadata{
Name: "test-profile",
Host: tc.host,
AccountID: tc.accountID,
}
p.Load(t.Context(), configFile, false)
assert.Equal(t, tc.wantValid, p.Valid, "Valid mismatch")
assert.NotEmpty(t, p.Host, "Host should be set")
assert.NotEmpty(t, p.AuthType, "AuthType should be set")
})
}
}
func TestProfileLoadUnifiedHostFallback(t *testing.T) {
// When Experimental_IsUnifiedHost is set but .well-known is unreachable,
// ConfigType() returns InvalidConfig. The fallback should reclassify as
// AccountConfig so the profile is still validated.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/databricks-config":
w.WriteHeader(http.StatusNotFound)
case "/api/2.0/accounts/unified-acct/workspaces":
_ = json.NewEncoder(w).Encode([]map[string]any{})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
content := "[unified-profile]\nhost = " + server.URL + "\ntoken = test-token\naccount_id = unified-acct\nexperimental_is_unified_host = true\n"
require.NoError(t, os.WriteFile(configFile, []byte(content), 0o600))
p := &profileMetadata{
Name: "unified-profile",
Host: server.URL,
AccountID: "unified-acct",
}
p.Load(t.Context(), configFile, false)
assert.True(t, p.Valid, "unified host profile should be valid via fallback")
assert.NotEmpty(t, p.Host)
assert.NotEmpty(t, p.AuthType)
}
func TestProfileLoadSPOGAccountWithDiscovery(t *testing.T) {
// Supplementary SPOG case: a host with account-scoped OIDC from discovery
// is validated as account config (via Workspaces.List, not CurrentUser.Me).
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/databricks-config":
_ = json.NewEncoder(w).Encode(map[string]any{
"account_id": "classic-acct",
"oidc_endpoint": r.Host + "/oidc/accounts/classic-acct",
})
case "/api/2.0/accounts/classic-acct/workspaces":
_ = json.NewEncoder(w).Encode([]map[string]any{})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
content := "[acct-profile]\nhost = " + server.URL + "\ntoken = test-token\naccount_id = classic-acct\n"
require.NoError(t, os.WriteFile(configFile, []byte(content), 0o600))
p := &profileMetadata{
Name: "acct-profile",
Host: server.URL,
AccountID: "classic-acct",
}
p.Load(t.Context(), configFile, false)
assert.True(t, p.Valid, "classic account profile should be valid")
assert.NotEmpty(t, p.Host)
assert.NotEmpty(t, p.AuthType)
}
func TestProfileLoadNoDiscoveryStaysWorkspace(t *testing.T) {
// When .well-known returns 404 and Experimental_IsUnifiedHost is false,
// the SPOG override should NOT trigger even if account_id is set. The
// profile should stay WorkspaceConfig and validate via CurrentUser.Me.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/databricks-config":
w.WriteHeader(http.StatusNotFound)
case "/api/2.0/preview/scim/v2/Me":
_ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
t.Setenv("HOME", dir)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", dir)
}
content := "[ws-profile]\nhost = " + server.URL + "\ntoken = test-token\naccount_id = some-acct\n"
require.NoError(t, os.WriteFile(configFile, []byte(content), 0o600))
p := &profileMetadata{
Name: "ws-profile",
Host: server.URL,
AccountID: "some-acct",
}
p.Load(t.Context(), configFile, false)
assert.True(t, p.Valid, "should validate as workspace when discovery is unavailable")
assert.NotEmpty(t, p.Host)
assert.Equal(t, "pat", p.AuthType)
}