-
Notifications
You must be signed in to change notification settings - Fork 5
/
ldap_test.go
630 lines (595 loc) · 23.8 KB
/
ldap_test.go
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright (C) 2023 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package authenticator
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-ldap/ldap/v3"
"github.com/sftpgo/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
baseDN = "dc=glauth,dc=com"
username = "cn=serviceuser,dc=glauth,dc=com"
password = "mysecret"
searchQuery = "(&(objectClass=*)(uid=%username%))"
groupAttribute = "memberOf"
primaryGroupPrefix = "sftpgo_primary"
secondaryGroupPrefix = "sftpgo_secondary"
membershipGroupPrefix = "sftpgo_membership"
user1 = "user1"
user2 = "user2"
caCRT = `-----BEGIN CERTIFICATE-----
MIIE5jCCAs6gAwIBAgIBATANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhDZXJ0
QXV0aDAeFw0yMzAxMDMxMDIwNDdaFw0zMzAxMDMxMDMwNDZaMBMxETAPBgNVBAMT
CENlcnRBdXRoMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxq6Wl1Ih
hgvGdM8M2IVI7dwnv3yShJygZsnREQSEW0xeWJL5DtNeHCME5WByFUAlZKpePtW8
TNwln9DYDtgNSMiWwvO/wR0mXsyU8Ma4ZBMlX0oOkWo1Ff/M/u8YY9X78Vvwdt62
Yt7QmU5oUUW2HdAgh4AlhKJSjm3t0uDP5s54uvueL5bjChHwEb1ZGOtST9Zt86cj
YA/xtVHnDXCJbhohpzQI6dK96NegONZVDaxEohVCyYYOgI1I14Bxu0ZCMm5GjwoO
QohnUfEJ+BRgZqFpbsnYCE+PoVayVVFoLA+GMeqbQ2SHej1Pr1K0dbjUz6SAk8/+
DL7h8d+YAtflATsMtdsVJ4WzEfvZbSbiYKYmlVC6zk6ooXWadvQ5+aezVes9WMpH
YnAoScuKoeerRuKlsSU7u+XCmy/i7Hii5FwMrSvIL2GLtVE+tJFCTABA55OWZikt
ULMQfg3P2Hk3GFIE35M10mSjKQkGhz06WC5UQ7f2Xl9GzO6PqRSorzugewgMK6L4
SnN7XBFnUHHqx1bWNkUG8NPYB6Zs7UDHygemTWxqqxun43s501DNTSunCKIhwFbt
1ol5gOvYAFG+BXxnggBT815Mgz1Zht3S9CuprAgz0grNEwAYjRTm1PSaX3t8I1kv
oUUuMF6BzWLHJ66uZKOCsPs3ouGq+G3GfWUCAwEAAaNFMEMwDgYDVR0PAQH/BAQD
AgEGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFCj8lmcR7loB9zAP/feo
t1eIHWmIMA0GCSqGSIb3DQEBCwUAA4ICAQCu46fF0Tr2tZz1wkYt2Ty3OU77jcG9
zYU/7uxPqPC8OYIzQJrumXKLOkTYJXJ7k+7RQdsn/nbxdH1PbslNDD3Eid/sZsF/
dFdGR1ZYwXVQbpYwEd19CvJTALn9CyAZpMS8J2RJrmdScAeSSb0+nAGTYP7GvPm+
8ktOnrz3w8FtzTw+seuCW/DI/5UpfC9Jf+i/3XgxDozXWNW6YNOIw/CicyaqbBTk
5WFcJ0WJN+8qQurw8n+sOvQcNsuDTO7K3Tqu0wGTDUQKou7kiMX0UISRvd8roNOl
zvvokNQe4VgCGQA+Y2SxvSxVG1BaymYeNw/0Yxm7QiKSUI400V1iKIcpnIvIedJR
j2bGIlslVSV/P6zkRuF1srRVxTxSf1imEfs8J8mMhHB6DkOsP4Y93z5s6JZ0sPiM
eOb0CVKul/e1R0Kq23AdPf5eUv63RhfmokN1OsdarRKMFyHphWMxqGJXsSvRP+dl
3DaKeTDx/91OSWiMc+glHHKKJveMYQLeJ7GXmcxhuoBm6o4Coowgw8NFKMCtAsp0
ktvsQuhB3uFUterw/2ONsOChx7Ybu36Zk47TKBpktfxDQ578TVoZ7xWSAFqCPHvx
A5VSwAg7tdBvORfqQjhiJRnhwr50RaNQABTLS0l5Vsn2mitApPs7iKiIts2ieWsU
EsdgvPZR2e5IkA==
-----END CERTIFICATE-----`
)
var (
ldapURL = []string{"ldap://localhost:3893"}
ldapsURL = []string{"ldaps://localhost:3894"}
multipleLDAPURLs = []string{"ldap://localhost:3893", "ldap://localhost:3895"}
)
func TestLDAPAuthenticator(t *testing.T) {
baseDir := filepath.Clean(os.TempDir())
auth, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, false, baseDir, 2, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
true, 0))
require.NoError(t, err)
require.Nil(t, auth.tlsConfig.RootCAs)
_, err = auth.CheckUserAndPass(user1, "wrong", "", "", nil)
require.Error(t, err)
e, ok := err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.LDAPResultInvalidCredentials), e.ResultCode)
auth.SFTPGoUserRequirements = 1
_, err = auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.Error(t, err)
assert.Contains(t, err.Error(), "LDAP users not defined in SFTPGo are not allowed")
auth.SFTPGoUserRequirements = 0
userJSON, err := auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.NoError(t, err)
var user sdk.User
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(baseDir, user1), user.HomeDir)
require.Len(t, user.Groups, 2)
// empty password
_, err = auth.CheckUserAndPass(user1, "", "", "", nil)
require.ErrorIs(t, err, errInvalidCredentials)
// wrong password
_, err = auth.CheckUserAndPass(user1, "wrong", "", "", nil)
require.Error(t, err)
e, ok = err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.LDAPResultInvalidCredentials), e.ResultCode)
// auth ok
userJSON, err = auth.CheckUserAndPass(user1, password, "", "", userJSON)
require.NoError(t, err)
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(baseDir, user1), user.HomeDir)
require.Len(t, user.Groups, 2)
// auth with a different user
userJSON, err = auth.CheckUserAndPass(user2, password, "", "", []byte(`{"username":"user2"}`))
require.NoError(t, err)
user = sdk.User{}
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(baseDir, user2), user.HomeDir)
require.Len(t, user.Groups, 1)
// test keyboard interactive authentication
_, err = auth.CheckUserAndKeyboardInteractive("missing user", "", "", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "does not exist")
userJSON, err = auth.CheckUserAndKeyboardInteractive(user1, "", "", userJSON)
require.NoError(t, err)
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Len(t, user.Groups, 2)
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", nil, nil, 1)
require.NoError(t, err)
_, _, _, res, _, err := auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
// wrong password
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{"wrong"}, nil, 2)
require.Error(t, err)
e, ok = err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.LDAPResultInvalidCredentials), e.ResultCode)
// empty password
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{""}, nil, 2)
require.ErrorIs(t, err, errInvalidCredentials)
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{"wrong"}, nil, 2)
require.Error(t, err)
e, ok = err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.LDAPResultInvalidCredentials), e.ResultCode)
_, _, _, res, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
// no group
_, err = auth.CheckUserAndPass("serviceuser", password, "", "", []byte(`{"username":"serviceuser"}`))
require.Error(t, err)
assert.Contains(t, err.Error(), "users without group membership are not allowed")
}
func TestAuthFromCache(t *testing.T) {
baseDir := filepath.Clean(os.TempDir())
auth, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, false, baseDir, 2, searchQuery,
nil, nil, "", "", "", false, 0))
require.NoError(t, err)
require.Nil(t, auth.tlsConfig.RootCAs)
defer func() {
cache = nil
}()
userJSON, err := auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.NoError(t, err)
var user sdk.User
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(baseDir, user1), user.HomeDir)
require.Len(t, user.Groups, 0)
// empty password
_, err = auth.CheckUserAndPass(user1, "", "", "", nil)
require.ErrorIs(t, err, errInvalidCredentials)
// error from cache
_, err = auth.CheckUserAndPass(user1, "wrong", "", "", nil)
require.ErrorIs(t, err, errInvalidCredentials)
// auth ok from cache
userJSON, err = auth.CheckUserAndPass(user1, password, "", "", userJSON)
require.NoError(t, err)
require.Nil(t, userJSON)
// test keyboard interactive authentication
_, err = auth.CheckUserAndKeyboardInteractive("missing user", "", "", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "does not exist")
userJSON, err = auth.CheckUserAndKeyboardInteractive(user1, "", "", userJSON)
require.NoError(t, err)
require.Nil(t, userJSON)
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", nil, nil, 1)
require.NoError(t, err)
_, _, _, res, _, err := auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
// wrong password
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{"wrong"}, nil, 2)
require.ErrorIs(t, err, errInvalidCredentials)
// empty password
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{""}, nil, 2)
require.ErrorIs(t, err, errInvalidCredentials)
found := cache.Has(user1)
require.True(t, found)
// wait for cache expiration
time.Sleep(2100 * time.Millisecond)
found = cache.Has(user1)
require.False(t, found)
// keyboard interactive auth with no cache
_, _, _, res, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
found = cache.Has(user1)
require.True(t, found)
// keyboard interactive auth from cache
_, _, _, res, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
}
func TestPreserveUserChanges(t *testing.T) {
auth, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, false, "", 0, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
false, 0))
require.NoError(t, err)
userJSON, err := auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.NoError(t, err)
var user sdk.User
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Len(t, user.Groups, 2)
user.ID = 1
user.Password = password
userJSON, err = json.Marshal(user)
require.NoError(t, err)
userJSON, err = auth.CheckUserAndPass(user1, password, "", "", userJSON)
require.NoError(t, err)
require.Nil(t, userJSON)
// keyboard interactive
userJSON, err = json.Marshal(user)
require.NoError(t, err)
userJSON, err = auth.CheckUserAndKeyboardInteractive(user1, "", "", userJSON)
require.NoError(t, err)
require.Nil(t, userJSON)
}
func TestLDAPS(t *testing.T) {
auth, err := NewAuthenticator(getConfig(ldapsURL, baseDN, username, password, 0, true, "", 0, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
false, 0))
require.NoError(t, err)
l, err := auth.connect()
require.NoError(t, err)
err = l.Close()
require.NoError(t, err)
}
func TestLDAPConnectionErrors(t *testing.T) {
auth, err := NewAuthenticator(getConfig([]string{"ldap://localhost:3892"}, baseDN, username, password, 0, true, "", 0, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
false, 0))
require.NoError(t, err)
_, err = auth.CheckUserAndPass(user1, password, "", "", nil)
require.Error(t, err)
_, err = auth.CheckUserAndKeyboardInteractive(user1, "", "", nil)
require.Error(t, err)
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", nil, nil, 2)
require.Error(t, err)
}
func TestStartTLS(t *testing.T) {
// glauth does not support STARTTLS
auth, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 1, true, "", 0, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
false, 0))
require.NoError(t, err)
_, err = auth.connect()
require.Error(t, err)
e, ok := err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(2), e.ResultCode)
}
func TestValidation(t *testing.T) {
_, err := NewAuthenticator(getConfig(nil, "", "", "", 0, false, "", 0, "", nil, nil, "", "", "", false, 0))
require.Error(t, err)
assert.Contains(t, err.Error(), "dial URL is required")
_, err = NewAuthenticator(getConfig([]string{"", ""}, "", "", "", 0, false, "", 0, "", nil, nil, "", "", "", false, 0))
require.Error(t, err)
assert.Contains(t, err.Error(), "dial URL is required")
a := LDAPAuthenticator{
DialURLs: ldapURL,
}
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "base DN is required")
a.BaseDN = "dc=mylab,dc=local"
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "username is required")
a.Username = "username"
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "search query is required")
a.SearchQuery = "(&(objectClass=user)(sAMAccountType=805306368)(sAMAccountName=username)))"
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "search query must contain the username placeholder")
a.SearchQuery = "(&(objectClass=user)(sAMAccountType=805306368)(sAMAccountName=%username%)))"
err = a.validate()
require.NoError(t, err)
a.BaseDir = "relative"
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "users base dir must be an absolute path")
a.BaseDir = filepath.Clean(os.TempDir())
a.PrimaryGroupPrefix = "group_"
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "group attributes not set, group prefixes are ineffective")
a.PrimaryGroupPrefix = ""
a.RequireGroups = true
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "group attributes are required")
a.GroupAttributes = []string{"memberOf"}
err = a.validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "at least a group prefix is required")
a.PrimaryGroupPrefix = "sftpgo_primary"
err = a.validate()
require.NoError(t, err)
a.DialURLs = []string{"ldap://1.2.3.4:389", "ldap://1.2.3.4:389", "ldap://1.2.3.5:389"}
err = a.validate()
require.NoError(t, err)
assert.Len(t, a.DialURLs, 2)
assert.Contains(t, a.DialURLs, "ldap://1.2.3.4:389")
assert.Contains(t, a.DialURLs, "ldap://1.2.3.5:389")
a.Cleanup()
}
func TestUnsupportedAuthMethods(t *testing.T) {
a := LDAPAuthenticator{}
_, err := a.CheckUserAndTLSCert(user1, "", "", "", nil)
require.ErrorIs(t, err, errNotImplemented)
_, err = a.CheckUserAndPublicKey(user2, "", "", "", nil)
require.ErrorIs(t, err, errNotImplemented)
_, _, _, _, _, err = a.SendKeyboardAuthRequest("", user1, "", "", nil, nil, 100)
require.ErrorIs(t, err, errNotImplemented)
}
func TestUserToUpdate(t *testing.T) {
u := &sdk.User{
BaseUser: sdk.BaseUser{
ID: 1,
},
}
a := LDAPAuthenticator{
GroupAttributes: []string{"memberOf"},
PrimaryGroupPrefix: "sftpgo_primary",
SFTPGoUserRequirements: 1,
}
res := a.isUserToUpdate(u, nil)
require.False(t, res)
a.SFTPGoUserRequirements = 0
res = a.isUserToUpdate(u, nil)
require.True(t, res)
u.Password = password
res = a.isUserToUpdate(u, nil)
require.True(t, res)
u.Filters.WebClient = []string{sdk.WebClientPasswordChangeDisabled, sdk.WebClientPasswordResetDisabled,
sdk.WebClientShareNoPasswordDisabled}
groups := []sdk.GroupMapping{
{
Name: "g1",
Type: sdk.GroupTypePrimary,
},
}
res = a.isUserToUpdate(u, groups)
require.True(t, res)
u.Groups = []sdk.GroupMapping{
{
Name: "g1",
Type: sdk.GroupTypeSecondary,
},
}
res = a.isUserToUpdate(u, groups)
require.True(t, res)
u.Groups = groups
res = a.isUserToUpdate(u, groups)
require.False(t, res)
res = a.isUserToUpdate(u, nil)
require.True(t, res)
a.GroupAttributes = nil
res = a.isUserToUpdate(u, nil)
require.False(t, res)
a.GroupAttributes = []string{"attr"}
a.PrimaryGroupPrefix = ""
res = a.isUserToUpdate(u, nil)
require.False(t, res)
}
func TestGetCNFromDN(t *testing.T) {
res := getCNFromDN("")
assert.Empty(t, res)
res = getCNFromDN("cn=test")
assert.Equal(t, "test", res)
res = getCNFromDN("test")
assert.Equal(t, "test", res)
res = getCNFromDN("")
assert.Empty(t, res)
res = getCNFromDN("cn=admin ,ou=users,dc=mylab,dc=local")
assert.Equal(t, "admin", res)
res = getCNFromDN("ou=first,ou=users,dc=mylab,dc=local")
assert.Equal(t, "first", res)
}
func TestLoadCACerts(t *testing.T) {
caCrtPath := "testcacrt"
_, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, true, "", 0,
searchQuery, nil, []string{caCrtPath}, "", "", "", false, 0))
require.Error(t, err)
assert.Contains(t, err.Error(), "is not an absolute path")
caCrtPath = filepath.Join(os.TempDir(), caCrtPath)
_, err = NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, true, "", 0,
searchQuery, nil, []string{caCrtPath}, "", "", "", false, 0))
require.ErrorIs(t, err, fs.ErrNotExist)
err = os.WriteFile(caCrtPath, []byte(caCRT), 0600)
require.NoError(t, err)
auth, err := NewAuthenticator(getConfig(ldapURL, baseDN, username, password, 0, true, "", 0,
searchQuery, nil, []string{caCrtPath}, "", "", "", false, 0))
require.NoError(t, err)
require.NotNil(t, auth.tlsConfig.RootCAs)
err = os.Remove(caCrtPath)
require.NoError(t, err)
}
func TestLDAPMonitor(t *testing.T) {
auth, err := NewAuthenticator(getConfig(multipleLDAPURLs, baseDN, username, password, 0, false, "", 2, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
true, 0))
require.NoError(t, err)
defer auth.Cleanup()
assert.Len(t, auth.getDialURLs(), 2)
auth.removeActiveDialURL(multipleLDAPURLs[0], nil)
auth.removeActiveDialURL(multipleLDAPURLs[1], nil)
auth.startMonitorTicker(100 * time.Millisecond)
assert.Eventually(t, func() bool {
return len(auth.getDialURLs()) == 1
}, time.Second, 250*time.Millisecond)
auth.removeActiveDialURL(multipleLDAPURLs[0], nil)
auth.removeActiveDialURL(multipleLDAPURLs[1], nil)
// no active URL, all defined urls will be returned
assert.Len(t, auth.getDialURLs(), 2)
}
func TestRetryableErrors(t *testing.T) {
a := LDAPAuthenticator{}
require.False(t, a.isRetryableError(nil))
err := &ldap.Error{
Err: errNotImplemented,
ResultCode: ldap.ErrorNetwork,
}
require.True(t, a.isRetryableError(err))
err = &ldap.Error{
Err: errNotImplemented,
ResultCode: ldap.ErrorUnexpectedMessage,
}
require.False(t, a.isRetryableError(err))
require.False(t, a.isRetryableError(fs.ErrPermission))
}
func TestMultiAuthValidation(t *testing.T) {
tempDir := t.TempDir()
configFile := filepath.Join(tempDir, "config.json")
_, err := NewMultiAuthenticator(configFile)
require.ErrorIs(t, err, fs.ErrNotExist)
err = os.WriteFile(configFile, []byte("not a json"), 0666)
assert.NoError(t, err)
_, err = NewMultiAuthenticator(configFile)
require.ErrorContains(t, err, "invalid config file")
err = os.WriteFile(configFile, []byte(`{"configs":[]}`), 0666)
assert.NoError(t, err)
_, err = NewMultiAuthenticator(configFile)
require.ErrorContains(t, err, "no configurations defined")
err = os.WriteFile(configFile, []byte(`{"configs":[{}]}`), 0666)
assert.NoError(t, err)
_, err = NewMultiAuthenticator(configFile)
require.ErrorContains(t, err, "unable to create authenticator")
c := multiAuthConfig{
LRUCacheSize: 120,
}
c.Configs = append(c.Configs, *getConfig(ldapURL, baseDN, username, password, 0, false, "", 0, searchQuery,
nil, nil, "", "", "", false, 0))
data, err := json.Marshal(c)
require.NoError(t, err)
err = os.WriteFile(configFile, data, 0666)
assert.NoError(t, err)
_, err = NewMultiAuthenticator(configFile)
assert.NoError(t, err)
}
func TestMultiAuthenticators(t *testing.T) {
tempDir := t.TempDir()
configFile := filepath.Join(tempDir, "config.json")
c := multiAuthConfig{
LRUCacheSize: 120,
}
c.Configs = append(c.Configs, *getConfig([]string{"ldap://localhost:3898"}, baseDN, username, password, 0, false,
tempDir, 0, searchQuery, []string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix,
membershipGroupPrefix, true, 0))
c.Configs = append(c.Configs, *getConfig(ldapURL, baseDN, username, password, 0, false, tempDir, 0, searchQuery,
[]string{groupAttribute}, nil, primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix,
true, 0))
data, err := json.Marshal(c)
require.NoError(t, err)
err = os.WriteFile(configFile, data, 0666)
assert.NoError(t, err)
auth, err := NewMultiAuthenticator(configFile)
require.NoError(t, err)
userJSON, err := auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.NoError(t, err)
var user sdk.User
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(tempDir, user1), user.HomeDir)
require.Len(t, user.Groups, 2)
// authenticate again, the cached node will be used directly
_, err = auth.CheckUserAndPass(user1, password, "", "", []byte(`{"username":"user1"}`))
require.NoError(t, err)
// empty password
_, err = auth.CheckUserAndPass(user1, "", "", "", nil)
require.ErrorIs(t, err, errInvalidCredentials)
// wrong password
_, err = auth.CheckUserAndPass(user1, "wrong", "", "", nil)
require.Error(t, err)
e, ok := err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.ErrorNetwork), e.ResultCode)
// auth with a different user
userJSON, err = auth.CheckUserAndPass(user2, password, "", "", []byte(`{"username":"user2"}`))
require.NoError(t, err)
user = sdk.User{}
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Equal(t, filepath.Join(tempDir, user2), user.HomeDir)
require.Len(t, user.Groups, 1)
// test keyboard interactive authentication
_, err = auth.CheckUserAndKeyboardInteractive("missing user", "", "", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "does not exist")
userJSON, err = auth.CheckUserAndKeyboardInteractive(user1, "", "", userJSON)
require.NoError(t, err)
err = json.Unmarshal(userJSON, &user)
require.NoError(t, err)
require.Equal(t, 1, user.Status)
require.Len(t, user.Groups, 2)
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", nil, nil, 1)
require.NoError(t, err)
_, _, _, res, _, err := auth.SendKeyboardAuthRequest("", user1, "", "", []string{password}, nil, 2)
require.NoError(t, err)
require.Equal(t, 1, res)
// wrong password
_, _, _, _, _, err = auth.SendKeyboardAuthRequest("", user1, "", "", []string{"wrong"}, nil, 2)
require.Error(t, err)
e, ok = err.(*ldap.Error)
require.True(t, ok)
require.Equal(t, uint16(ldap.ErrorNetwork), e.ResultCode)
// no group
_, err = auth.CheckUserAndPass("serviceuser", password, "", "", []byte(`{"username":"serviceuser"}`))
require.Error(t, err)
assert.Contains(t, err.Error(), "users without group membership are not allowed")
// test unsupported methods
_, err = auth.CheckUserAndPublicKey(user2, "", "", "", nil)
require.ErrorIs(t, err, errNotImplemented)
_, err = auth.CheckUserAndTLSCert(user1, "", "", "", nil)
require.ErrorIs(t, err, errNotImplemented)
}
func getConfig(dialURLs []string, baseDN, username, password string, startTLS int, skipTLSVerify bool,
baseDir string, cacheTime int, searchQuery string, groupAttributes, caCertificates []string,
primaryGroupPrefix, secondaryGroupPrefix, membershipGroupPrefix string, requiresGroup bool,
sftpgoUserRequirements int,
) *Config {
return &Config{
DialURLs: dialURLs,
BaseDN: baseDN,
Username: username,
Password: password,
StartTLS: startTLS,
SkipTLSVerify: skipTLSVerify,
BaseDir: baseDir,
CacheTime: cacheTime,
SearchQuery: searchQuery,
GroupAttributes: groupAttributes,
CACertificates: caCertificates,
PrimaryGroupPrefix: primaryGroupPrefix,
SecondaryGroupPrefix: secondaryGroupPrefix,
MembershipGroupPrefix: membershipGroupPrefix,
RequireGroups: requiresGroup,
SFTPGoUserRequirements: sftpgoUserRequirements,
}
}