Repository navigation
Expand file tree
/
Copy pathauth_plugin_test.go
More file actions
306 lines (295 loc) · 9.53 KB
/
Copy pathauth_plugin_test.go
File metadata and controls
306 lines (295 loc) · 9.53 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
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2023 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"context"
"crypto/rsa"
"crypto/tls"
"errors"
"math/big"
"net"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestSimpleAuthRejectsContinuation(t *testing.T) {
nextPacket, err := (simpleAuth{}).ContinuationAuth(context.Background(), nil, nil, &AuthContext{})
if err != ErrMalformPkt {
t.Fatalf("expected ErrMalformPkt, got %v", err)
}
if nextPacket != nil {
t.Errorf("expected no response packet, got %v", nextPacket)
}
}
func TestRegisterAuthPluginFactoryLifetime(t *testing.T) {
var calls atomic.Int32
const name = "test_auth_factory_lifetime"
registerTestAuthPlugin(t, name, func() AuthPlugin {
calls.Add(1)
return &authTestPlugin{}
})
if calls.Load() != 0 {
t.Fatal("registration called the factory")
}
one, ok := globalPluginRegistry.GetPlugin(name)
if !ok {
t.Fatal("plugin was not registered")
}
two, ok := globalPluginRegistry.GetPlugin(name)
if !ok || one == two || calls.Load() != 2 {
t.Fatal("lookups did not create independent plugin instances")
}
RegisterAuthPlugin(name, func() AuthPlugin { return &nativePasswordPlugin{} })
if plugin, ok := globalPluginRegistry.GetPlugin(name); !ok {
t.Fatal("replacement was not registered")
} else if _, ok := plugin.(*nativePasswordPlugin); !ok {
t.Fatalf("replacement returned %T", plugin)
}
}
func TestRegisterAuthPluginOverridesBuiltins(t *testing.T) {
for _, name := range []string{
"mysql_native_password", "mysql_old_password", "mysql_clear_password",
"sha256_password", "caching_sha2_password", "client_ed25519",
} {
t.Run(name, func(t *testing.T) {
if _, ok := globalPluginRegistry.GetPlugin(name); !ok {
t.Fatal("built-in plugin is not registered")
}
for _, stage := range []string{"greeting", "switch"} {
t.Run(stage, func(t *testing.T) {
want := errors.New("replacement plugin called")
calls := 0
registerTestAuthPlugin(t, name, func() AuthPlugin {
return &authTestPlugin{init: func(context.Context, []byte, *AuthContext) ([]byte, error) {
calls++
return nil, want
}}
})
greeting := name
wantWrites := 0
if stage == "switch" {
greeting = "mysql_native_password"
if name == greeting {
greeting = "caching_sha2_password"
}
wantWrites = 1
}
caps := clientMySQL | clientProtocol41 | clientSecureConn | clientPluginAuth | clientPluginAuthLenEncClientData
mock := &mockConn{
data: authTestHandshake(greeting, caps),
queuedReplies: [][]byte{authFallbackSwitch(name)},
maxReads: 2,
}
cfg := NewConfig()
cfg.Passwd = "secret"
cfg.AllowOldPasswords = true
cfg.AllowCleartextPasswords = true
cfg.DialFunc = func(context.Context, string, string) (net.Conn, error) { return mock, nil }
c, err := NewConnector(cfg)
if err != nil {
t.Fatal(err)
}
conn, err := c.Connect(t.Context())
if conn != nil {
conn.Close()
}
if !errors.Is(err, want) || calls != 1 || mock.writes != wantWrites {
t.Fatalf("Connect = %v, calls = %d, writes = %d; want %v, 1, %d", err, calls, mock.writes, want, wantWrites)
}
})
}
})
}
}
func TestAuthPluginFactoryOutsideLock(t *testing.T) {
r := newPluginRegistry()
var calls atomic.Int32
r.Register("outer", func() AuthPlugin {
// Factories may consult or update the registry, even when multiple
// connections start authentication concurrently.
r.Register("inner", func() AuthPlugin { return &nativePasswordPlugin{} })
plugin, _ := r.GetPlugin("inner")
calls.Add(1)
return plugin
})
var wg sync.WaitGroup
for range 20 {
wg.Add(1)
go func() {
defer wg.Done()
r.GetPlugin("outer")
}()
}
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
if calls.Load() != 20 {
t.Fatalf("factory calls = %d, want 20", calls.Load())
}
case <-time.After(5 * time.Second):
t.Fatal("factory was called with the registry locked")
}
}
func TestAuthContextSnapshot(t *testing.T) {
cfg := NewConfig()
cfg.User = "user"
cfg.Passwd = "first-factor"
cfg.Net = "unix"
cfg.TLS = &tls.Config{}
cfg.pubKey = &rsa.PublicKey{N: big.NewInt(12345), E: 65537}
auth := newAuthContext(cfg, "selected-factor", false)
// Later configuration changes must not change an active exchange.
cfg.User = "changed"
cfg.Passwd = "changed"
cfg.Net = "tcp"
cfg.pubKey.N.SetInt64(99)
cfg.pubKey.E = 3
if auth.User() != "user" || auth.Password() != "selected-factor" || !auth.UnixSocket() {
t.Fatal("authentication snapshot changed with the configuration")
}
if auth.TLS() {
t.Fatal("configured TLS must not be reported as an established TLS connection")
}
key := auth.ServerPublicKey()
if key.N.Int64() != 12345 || key.E != 65537 {
t.Fatal("authentication snapshot shares the configured RSA key")
}
key.N.SetInt64(42)
key.E = 17
key = auth.ServerPublicKey()
if key.N.Int64() != 12345 || key.E != 65537 {
t.Fatal("ServerPublicKey returned a mutable reference into the snapshot")
}
if (&AuthContext{}).ServerPublicKey() != nil {
t.Fatal("expected nil for an unconfigured RSA key")
}
}
func TestInitAuthPolicy(t *testing.T) {
for _, tc := range []struct {
plugin string
want error
}{
{"mysql_native_password", ErrNativePassword},
{"mysql_old_password", ErrOldPassword},
{"mysql_clear_password", ErrCleartextPassword},
} {
t.Run(tc.plugin, func(t *testing.T) {
_, mc := newRWMockConn(1)
mc.cfg.AllowNativePasswords = false
mc.cfg.Passwd = "password"
auth := newAuthContext(mc.cfg, mc.cfg.Passwd, false)
plugin, response, err := mc.initAuth(context.Background(), tc.plugin, nil, auth)
if err != tc.want || plugin != nil || response != nil {
t.Fatalf("initAuth() = (%T, %v, %v), want (nil, nil, %v)", plugin, response, err, tc.want)
}
})
}
}
func TestAuthPluginCancellation(t *testing.T) {
for _, stage := range []string{"initial", "continuation"} {
t.Run(stage, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
entered := make(chan struct{})
waitForCancel := func(gotCtx context.Context, _ []byte, _ *AuthContext) ([]byte, error) {
if gotCtx != ctx {
return nil, errors.New("plugin did not receive the connection context")
}
close(entered)
<-gotCtx.Done()
return nil, gotCtx.Err()
}
const name = "test_auth_cancel"
factory := func() AuthPlugin {
return &authTestPlugin{
init: waitForCancel,
next: func(ctx context.Context, packet, _ []byte, auth *AuthContext) ([]byte, error) {
return waitForCancel(ctx, packet, auth)
},
}
}
registerTestAuthPlugin(t, name, factory)
conn, mc := newRWMockConn(2)
auth := newAuthContext(mc.cfg, mc.cfg.Passwd, false)
conn.data = makePacket(2, []byte{iAuthMoreData, 7})
conn.maxReads = 1
result := make(chan error, 1)
go func() {
if stage == "initial" {
_, _, err := mc.initAuth(ctx, name, nil, auth)
result <- err
} else {
result <- mc.handleAuthResult(ctx, nil, factory(), auth)
}
}()
select {
case <-entered:
cancel()
case err := <-result:
t.Fatalf("plugin returned without waiting for cancellation: %v", err)
case <-ctx.Done():
t.Fatal("plugin was not called")
}
if err := <-result; err != context.Canceled {
t.Fatalf("got %v, want context.Canceled", err)
}
if len(conn.written) != 0 {
t.Error("sent authentication data after cancellation")
}
})
}
}
func TestAuthSwitchContextSnapshot(t *testing.T) {
conn, mc := newRWMockConn(2)
mc.cfg.User = "user"
mc.cfg.Passwd = "configured-password"
auth := newAuthContext(mc.cfg, "selected-password", true)
// A switch must keep the selected credentials and established transport,
// even if the original configuration changes.
mc.cfg.User = "changed"
mc.cfg.Passwd = "changed"
ctx := t.Context()
var switchedAuth *AuthContext
continued := false
const name = "test_auth_switch_context"
registerTestAuthPlugin(t, name, func() AuthPlugin {
return &authTestPlugin{
init: func(gotCtx context.Context, seed []byte, gotAuth *AuthContext) ([]byte, error) {
if gotCtx != ctx || gotAuth == auth || gotAuth.User() != "user" || gotAuth.Password() != "selected-password" || !gotAuth.TLS() || string(seed) != "challenge" {
return nil, errors.New("auth switch did not preserve the authentication snapshot")
}
switchedAuth = gotAuth
return []byte{42}, nil
},
next: func(gotCtx context.Context, packet, seed []byte, gotAuth *AuthContext) ([]byte, error) {
if gotCtx != ctx || gotAuth != switchedAuth || string(seed) != "challenge" {
return nil, errors.New("continuation did not retain the switched context")
}
continued = true
return nil, nil // Read the queued OK without sending a response.
},
}
})
payload := append([]byte{iEOF}, name...)
payload = append(payload, 0)
payload = append(payload, "challenge\x00"...)
conn.data = makePacket(2, payload)
conn.queuedReplies = [][]byte{append(
makePacket(4, []byte{iAuthMoreData, 7}),
makePacket(5, []byte{0, 0, 0, 2, 0, 0, 0})...,
)}
conn.maxReads = 2
if err := mc.handleAuthResult(ctx, nil, &nativePasswordPlugin{}, auth); err != nil {
t.Fatal(err)
}
if !continued || conn.writes != 1 {
t.Fatalf("continued = %v, writes = %d; want true, 1", continued, conn.writes)
}
}