-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcache_test.go
More file actions
276 lines (217 loc) · 8.46 KB
/
Copy pathcache_test.go
File metadata and controls
276 lines (217 loc) · 8.46 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
package storage
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/cli/libs/env"
"github.com/databricks/databricks-sdk-go/credentials/u2m"
"github.com/databricks/databricks-sdk-go/credentials/u2m/cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)
// stubCache is a test double for cache.TokenCache that records the source
// it was constructed from. It lets the tests confirm which factory ran.
type stubCache struct{ source string }
func (stubCache) Store(string, *oauth2.Token) error { return nil }
func (stubCache) Lookup(string) (*oauth2.Token, error) { return nil, cache.ErrNotFound }
func fakeFactories(t *testing.T) cacheFactories {
t.Helper()
return cacheFactories{
newFile: func(context.Context) (cache.TokenCache, error) { return stubCache{source: "file"}, nil },
newKeyring: func() cache.TokenCache { return stubCache{source: "keyring"} },
probeKeyring: func() error { return nil },
}
}
// hermetic isolates the test from the caller's real env vars and
// .databrickscfg so ResolveStorageMode starts from a clean default.
func hermetic(t *testing.T) {
t.Helper()
t.Setenv(EnvVar, "")
t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "databrickscfg"))
}
func TestResolveCache_DefaultsToPlaintextFile(t *testing.T) {
hermetic(t)
ctx := t.Context()
got, mode, err := resolveCacheWith(ctx, "", fakeFactories(t))
require.NoError(t, err)
assert.Equal(t, StorageModePlaintext, mode)
assert.Equal(t, "file", got.(stubCache).source)
}
func TestResolveCache_OverrideSecureUsesKeyring(t *testing.T) {
hermetic(t)
ctx := t.Context()
got, mode, err := resolveCacheWith(ctx, StorageModeSecure, fakeFactories(t))
require.NoError(t, err)
assert.Equal(t, StorageModeSecure, mode)
assert.Equal(t, "keyring", got.(stubCache).source)
}
func TestResolveCache_EnvVarSelectsSecure(t *testing.T) {
hermetic(t)
ctx := env.Set(t.Context(), EnvVar, "secure")
got, mode, err := resolveCacheWith(ctx, "", fakeFactories(t))
require.NoError(t, err)
assert.Equal(t, StorageModeSecure, mode)
assert.Equal(t, "keyring", got.(stubCache).source)
}
func TestResolveCache_PlaintextOverrideUsesFile(t *testing.T) {
hermetic(t)
ctx := t.Context()
got, mode, err := resolveCacheWith(ctx, StorageModePlaintext, fakeFactories(t))
require.NoError(t, err)
assert.Equal(t, StorageModePlaintext, mode)
assert.Equal(t, "file", got.(stubCache).source)
}
func TestResolveCache_InvalidOverrideReturnsError(t *testing.T) {
hermetic(t)
ctx := t.Context()
_, _, err := resolveCacheWith(ctx, StorageMode("bogus"), fakeFactories(t))
require.Error(t, err)
assert.Contains(t, err.Error(), `unsupported storage mode "bogus"`)
}
func TestResolveCache_InvalidEnvReturnsError(t *testing.T) {
hermetic(t)
ctx := env.Set(t.Context(), EnvVar, "bogus")
_, _, err := resolveCacheWith(ctx, "", fakeFactories(t))
require.Error(t, err)
assert.Contains(t, err.Error(), "DATABRICKS_AUTH_STORAGE")
}
func TestResolveCache_FileFactoryErrorPropagates(t *testing.T) {
hermetic(t)
ctx := t.Context()
boom := errors.New("disk full")
factories := cacheFactories{
newFile: func(context.Context) (cache.TokenCache, error) { return nil, boom },
newKeyring: func() cache.TokenCache { return stubCache{source: "keyring"} },
probeKeyring: func() error { return nil },
}
_, _, err := resolveCacheWith(ctx, StorageModePlaintext, factories)
require.Error(t, err)
assert.ErrorIs(t, err, boom)
}
func TestResolveCacheForLogin_PlaintextSkipsProbe(t *testing.T) {
hermetic(t)
ctx := t.Context()
probed := false
f := fakeFactories(t)
f.probeKeyring = func() error {
probed = true
return nil
}
got, mode, err := resolveCacheForLoginWith(ctx, StorageModePlaintext, f)
require.NoError(t, err)
assert.Equal(t, StorageModePlaintext, mode)
assert.Equal(t, "file", got.(stubCache).source)
assert.False(t, probed, "probe must not run when mode is already plaintext")
}
func TestResolveCacheForLogin_SecureProbeOK(t *testing.T) {
hermetic(t)
ctx := env.Set(t.Context(), EnvVar, "secure")
got, mode, err := resolveCacheForLoginWith(ctx, "", fakeFactories(t))
require.NoError(t, err)
assert.Equal(t, StorageModeSecure, mode)
assert.Equal(t, "keyring", got.(stubCache).source)
}
func TestResolveCacheForLogin_ExplicitEnvSecure_ProbeFail_Errors(t *testing.T) {
hermetic(t)
ctx := env.Set(t.Context(), EnvVar, "secure")
configPath := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
f := fakeFactories(t)
f.probeKeyring = func() error { return errors.New("no keyring") }
_, _, err := resolveCacheForLoginWith(ctx, "", f)
require.Error(t, err)
assert.ErrorContains(t, err, "secure storage was requested")
persisted, gerr := databrickscfg.GetConfiguredAuthStorage(ctx, configPath)
require.NoError(t, gerr)
assert.Equal(t, "", persisted, "env-set secure must not be persisted as plaintext")
}
func TestResolveCacheForLogin_ExplicitConfigSecure_ProbeFail_Errors(t *testing.T) {
hermetic(t)
ctx := t.Context()
configPath := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
require.NoError(t, os.WriteFile(configPath, []byte("[__settings__]\nauth_storage = secure\n"), 0o600))
f := fakeFactories(t)
f.probeKeyring = func() error { return errors.New("no keyring") }
_, _, err := resolveCacheForLoginWith(ctx, "", f)
require.Error(t, err)
assert.ErrorContains(t, err, "secure storage was requested")
persisted, gerr := databrickscfg.GetConfiguredAuthStorage(ctx, configPath)
require.NoError(t, gerr)
assert.Equal(t, "secure", persisted, "config-set secure must not be silently rewritten")
}
func TestResolveCacheForLogin_ExplicitOverrideSecure_ProbeFail_Errors(t *testing.T) {
hermetic(t)
ctx := t.Context()
f := fakeFactories(t)
f.probeKeyring = func() error { return errors.New("no keyring") }
_, _, err := resolveCacheForLoginWith(ctx, StorageModeSecure, f)
require.Error(t, err)
assert.ErrorContains(t, err, "secure storage was requested")
}
func TestApplyLoginFallback_DefaultSecure_ProbeFail_FallsBackAndPersists(t *testing.T) {
hermetic(t)
ctx := t.Context()
configPath := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
f := fakeFactories(t)
f.probeKeyring = func() error { return errors.New("no keyring") }
got, mode, err := applyLoginFallback(ctx, StorageModeSecure, false, f)
require.NoError(t, err)
assert.Equal(t, StorageModePlaintext, mode)
assert.Equal(t, "file", got.(stubCache).source)
persisted, err := databrickscfg.GetConfiguredAuthStorage(ctx, configPath)
require.NoError(t, err)
assert.Equal(t, "plaintext", persisted, "default-mode fallback must persist auth_storage = plaintext")
}
func TestApplyLoginFallback_ExplicitSecure_ProbeFail_Errors(t *testing.T) {
hermetic(t)
ctx := t.Context()
configPath := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
f := fakeFactories(t)
f.probeKeyring = func() error { return errors.New("no keyring") }
_, _, err := applyLoginFallback(ctx, StorageModeSecure, true, f)
require.Error(t, err)
assert.ErrorContains(t, err, "secure storage was requested")
persisted, gerr := databrickscfg.GetConfiguredAuthStorage(ctx, configPath)
require.NoError(t, gerr)
assert.Equal(t, "", persisted, "explicit-secure error must not write config")
}
func TestWrapForOAuthArgument(t *testing.T) {
const (
host = "https://example.com"
profileKey = "myprofile"
)
arg, err := u2m.NewProfileWorkspaceOAuthArgument(host, profileKey)
require.NoError(t, err)
cases := []struct {
name string
mode StorageMode
wantWrap bool
wantHostKey bool
}{
{"plaintext wraps and mirrors under host key", StorageModePlaintext, true, true},
{"secure returns inner unchanged; no host-key mirror", StorageModeSecure, false, false},
{"unknown returns inner unchanged; no host-key mirror", StorageModeUnknown, false, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
inner := newMemoryCache()
got := WrapForOAuthArgument(inner, tc.mode, arg)
_, wrapped := got.(*DualWritingTokenCache)
assert.Equal(t, tc.wantWrap, wrapped, "wrapper presence")
tok := &oauth2.Token{AccessToken: "abc"}
require.NoError(t, got.Store(profileKey, tok))
primary, err := inner.Lookup(profileKey)
require.NoError(t, err, "primary key must always be written")
assert.Equal(t, tok, primary)
_, err = inner.Lookup(host)
if tc.wantHostKey {
require.NoError(t, err, "host-key mirror expected in plaintext mode")
} else {
assert.ErrorIs(t, err, cache.ErrNotFound, "no host-key mirror expected outside plaintext mode")
}
})
}
}