-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.go
870 lines (794 loc) · 26.9 KB
/
client.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package ldap
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"fmt"
"math"
"net"
"net/url"
"strings"
"text/template"
"time"
"github.com/go-ldap/ldap/v3"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/go-secure-stdlib/tlsutil"
)
const (
schemeLDAP = "ldap"
schemeLDAPTLS = "ldaps"
)
// Client provides a client for making requests to a directory service.
type Client struct {
conf *ClientConfig
conn *ldap.Conn
}
// Warning is a warning message
type Warning string
func fmtWarning(format string, a ...interface{}) Warning {
return Warning(fmt.Sprintf(format, a...))
}
// NewClient will create a new client from the configuration. The following
// defaults will be used if no config value is provided for them:
// - URLs: see constant DefaultURL
// - UserAttr: see constant DefaultUserAttr
// - GroupAttr: see constant DefaultGroupAttr
// - GroupFilter: see constant DefaultGroupFilter
// - TLSMinVersion: see constant DefaultTLSMinVersion
// - TLSMaxVersion: see constant DefaultTLSMaxVersion
func NewClient(ctx context.Context, conf *ClientConfig) (*Client, error) {
const op = "ldap.NewClient"
if conf == nil {
return nil, fmt.Errorf("%s: missing config: %w", op, ErrInvalidParameter)
}
clientConf, err := conf.clone()
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
if len(clientConf.URLs) == 0 {
clientConf.URLs = []string{DefaultURL}
}
if clientConf.GroupAttr == "" {
clientConf.GroupAttr = DefaultGroupAttr
}
if clientConf.GroupFilter == "" {
clientConf.GroupFilter = DefaultGroupFilter
}
if clientConf.TLSMinVersion == "" {
clientConf.TLSMinVersion = DefaultTLSMinVersion
}
if clientConf.TLSMaxVersion == "" {
clientConf.TLSMaxVersion = DefaultTLSMaxVersion
}
if clientConf.UserAttr == "" {
clientConf.UserAttr = DefaultUserAttr
}
if err := clientConf.validate(); err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
return &Client{
conf: clientConf,
}, nil
}
// connect will connect to a directory server using the URLs from the config or
// the WithURLs option. It will attempt Supports options: WithDialer, WithURLs
func (c *Client) connect(ctx context.Context, opt ...Option) error {
const op = "ldap.(Client).connect"
if c.conf == nil {
return fmt.Errorf("%s: missing configuration: %w", op, ErrInternal)
}
opts := getConfigOpts(opt...)
if len(c.conf.URLs) == 0 && len(opts.withURLs) == 0 {
return fmt.Errorf("%s: missing both configuration and optional LDAP URLs: %w", op, ErrInvalidParameter)
}
if len(opts.withURLs) == 0 {
opts.withURLs = c.conf.URLs
}
var retErr *multierror.Error
var conn *ldap.Conn
for _, uut := range opts.withURLs {
u, err := url.Parse(uut)
if err != nil {
retErr = multierror.Append(retErr, fmt.Errorf("%s: error parsing url %q: %w", op, uut, err))
continue
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}
var tlsConfig *tls.Config
switch u.Scheme {
case schemeLDAP:
conn, err = ldap.DialURL(uut, ldap.DialWithDialer(&net.Dialer{Timeout: c.getTimeout()}))
if err != nil {
break
}
if conn == nil {
err = fmt.Errorf("%s: empty connection after dialing: %w", op, ErrUnknown)
break
}
if c.conf.StartTLS {
tlsConfig, err = getTLSConfig(
host,
withInsecureTLS(c.conf.InsecureTLS),
withTLSMinVersion(c.conf.TLSMinVersion),
withTLSMaxVersion(c.conf.TLSMaxVersion),
withCertificates(c.conf.Certificates...),
withClientTLSCert(c.conf.ClientTLSCert),
withClientTLSKey(c.conf.ClientTLSKey),
)
if err != nil {
break
}
err = conn.StartTLS(tlsConfig)
}
case schemeLDAPTLS:
tlsConfig, err = getTLSConfig(
host,
withInsecureTLS(c.conf.InsecureTLS),
withTLSMinVersion(c.conf.TLSMinVersion),
withTLSMaxVersion(c.conf.TLSMaxVersion),
withCertificates(c.conf.Certificates...),
withClientTLSCert(c.conf.ClientTLSCert),
withClientTLSKey(c.conf.ClientTLSKey),
)
if err != nil {
break
}
conn, err = ldap.DialURL(uut, ldap.DialWithTLSDialer(tlsConfig, &net.Dialer{Timeout: c.getTimeout()}))
default:
retErr = multierror.Append(retErr, fmt.Errorf("%s: invalid LDAP scheme in url %q: %w", op, uut, ErrInvalidParameter))
continue
}
if err == nil {
retErr = nil
break
}
retErr = multierror.Append(retErr, fmt.Errorf("%s: error connecting to host %q: %w", op, uut, err))
}
if retErr != nil {
return retErr
}
conn.SetTimeout(c.getTimeout())
c.conn = conn
return nil
}
func (c *Client) getTimeout() time.Duration {
switch c.conf.RequestTimeout {
case 0:
return DefaultTimeout * time.Second
default:
return time.Duration(c.conf.RequestTimeout) * time.Second
}
}
// AuthResult is the result from a user authentication request via Client.Authenticate(...)
type AuthResult struct {
// Success represents whether or not the attempt was successful
Success bool
// Groups are the groups that were associated with the authenticated
// user (optional, see WithGroups() option)
Groups []string
// UserDN of the authenticated user (optional see WithUserAttributes()
// option along with IncludeUserAttributes and ExcludedUserAttributes config
// fields).
UserDN string
// UserAttributes that are associated with the authenticated user (optional
// see WithUserAttributes() option along with IncludeUserAttributes and
// ExcludedUserAttributes config fields)
UserAttributes map[string][]string
// Warnings are warnings that happen during either authentication or when
// attempting to find the groups associated with the authenticated user (see
// the WithGroups option)
Warnings []Warning
}
type Attribute struct {
// Name is the name of the LDAP attribute
Name string
// Vals are the LDAP attribute values
Vals []string
}
// Authenticate the user using the client's configured directory service. If
// the WithGroups option is specified, it will also return the user's groups
// from the directory.
//
// Supported options: WithUserAttributes, WithGroups, WithDialer, WithURLs
func (c *Client) Authenticate(ctx context.Context, username, password string, opt ...Option) (*AuthResult, error) {
const op = "ldap.(Client).Authenticate"
if username == "" {
return nil, fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
if !c.conf.AllowEmptyPasswordBinds && password == "" {
return nil, fmt.Errorf("%s: password cannot be of zero length if allow_empty_passwd_bind is not enabled: %w", op, ErrInvalidParameter)
}
if err := c.connect(ctx, opt...); err != nil {
return nil, fmt.Errorf("%s: failed to connect: %w", op, err)
}
userBindDN, err := c.getUserBindDN(username)
if err != nil {
return nil, fmt.Errorf("%s: discovery of user bind DN failed: %w", op, err)
}
// Try to bind as the login user. This is where the actual authentication takes place.
if len(password) > 0 {
err = c.conn.Bind(userBindDN, password)
} else {
err = c.conn.UnauthenticatedBind(userBindDN)
}
if err != nil {
return nil, fmt.Errorf("%s: unable to bind user: %w", op, err)
}
opts := getConfigOpts(opt...)
if !opts.withGroups && !c.conf.IncludeUserGroups &&
!opts.withUserAttributes && !c.conf.IncludeUserAttributes {
return &AuthResult{
Success: true,
}, nil
}
// We re-bind to the BindDN if it's defined because we assume the BindDN
// should be the one to search, not the user authenticating.
if c.conf.BindDN != "" && c.conf.BindPassword != "" {
if err := c.conn.Bind(c.conf.BindDN, c.conf.BindPassword); err != nil {
// unless the binddn was changed during this inflight authentication
// flow, it should be very difficult to encounter this error
return nil, fmt.Errorf("%s: unable to re-bind with the configuration BindDN user: %w", op, err)
}
}
userDN, err := c.getUserDN(userBindDN, username)
if err != nil {
return nil, fmt.Errorf("%s: failed to get the DN for the authenticated user: %w", op, err)
}
userAttrs := map[string][]string{}
if c.conf.IncludeUserAttributes || opts.withUserAttributes {
attrs, err := c.getUserAttributes(userDN)
if err != nil {
return nil, fmt.Errorf("%s: failed to get user attributes: %w", op, err)
}
for _, a := range attrs {
userAttrs[a.Name] = a.Vals
}
}
if !opts.withGroups && !c.conf.IncludeUserGroups {
return &AuthResult{
Success: true,
UserDN: userDN,
UserAttributes: userAttrs,
}, nil
}
if c.conf.AnonymousGroupSearch {
if err := c.conn.UnauthenticatedBind(userDN); err != nil {
return nil, fmt.Errorf("%s: group search anonymous bind failed: %w", op, err)
}
}
ldapGroups, warnings, err := c.getGroups(userDN, username)
if err != nil {
return nil, fmt.Errorf("%s: unable to get user groups: %w", op, err)
}
switch {
case c.conf.IncludeUserAttributes || opts.withUserAttributes:
return &AuthResult{
Success: true,
UserDN: userDN,
UserAttributes: userAttrs,
Groups: ldapGroups,
Warnings: warnings,
}, nil
default:
return &AuthResult{
Success: true,
Groups: ldapGroups,
Warnings: warnings,
}, nil
}
}
// Close will close the client's connection to the directory service.
func (c *Client) Close(ctx context.Context) {
if c.conn != nil {
c.conn.Close()
}
}
func (c *Client) getUserAttributes(userDN string) ([]Attribute, error) {
const op = "ldap.(Client).getUserAttributes"
switch {
case userDN == "":
return nil, fmt.Errorf("%s: missing user dn: %w", op, ErrInvalidParameter)
}
result, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: userDN,
Scope: ldap.ScopeBaseObject,
Filter: "(objectClass=*)",
})
switch {
case err != nil:
return nil, fmt.Errorf("%s: LDAP search for user attributes failed (baseDN: %q / filter: %q): %w", op, userDN, "(objectClass=*)", err)
case len(result.Entries) != 1:
return nil, fmt.Errorf("%s: LDAP search for user attributes was 0 or not unique", op)
}
userEntry := result.Entries[0]
attributes := make([]Attribute, 0, len(userEntry.Attributes))
for _, a := range userEntry.Attributes {
switch {
// exclude the default openLDAP password attribute
case strings.EqualFold(a.Name, DefaultOpenLDAPUserPasswordAttribute):
// exclude the default AD password attribute
case strings.EqualFold(a.Name, DefaultADUserPasswordAttribute):
// filter out excluded attributes
case strutil.StrListContainsCaseInsensitive(c.conf.ExcludedUserAttributes, a.Name):
default:
attributes = append(attributes, Attribute{
Name: a.Name,
Vals: a.Values,
})
}
}
return attributes, nil
}
// getGroups queries LDAP and returns a slice describing the set of groups the
// authenticated user is a member of.
//
// If c.conf.UseTokenGroups is true then the search is performed directly on the
// userDN. The values of those attributes are converted to string SIDs, and then
// looked up to get Entry objects. Otherwise, the search query is constructed
// according to c.conf..GroupFilter, and run in context of c.conf.GroupDN.
// Groups will be resolved from the query results by following the attribute
// defined in c.conf.GroupAttr.
//
// c.conf.GroupFilter is a go template and is compiled with the following context: [UserDN, Username]
//
// UserDN - The DN of the authenticated user
// Username - The Username of the authenticated user
//
// Example:
//
// c.conf.GroupFilter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={{.UserDN}}))"
// c.conf.GroupDN = "OU=Groups,DC=myorg,DC=com"
// c.conf.GroupAttr = "cn"
//
// NOTE - If the config GroupFilter is empty, no query is performed and an
// empty result slice is returned.
func (c *Client) getGroups(userDN string, username string) ([]string, []Warning, error) {
const op = "ldap.(Client).getGroups"
var warnings []Warning
if userDN == "" {
return nil, warnings, fmt.Errorf("%s: missing user dn: %w", op, ErrInvalidParameter)
}
if username == "" {
return nil, warnings, fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
var entries []*ldap.Entry
var err error
if c.conf.UseTokenGroups {
entries, warnings, err = c.tokenGroupsSearch(userDN)
} else {
entries, warnings, err = c.filterGroupsSearch(userDN, username)
}
if err != nil {
return nil, warnings, fmt.Errorf("%s: %w", op, err)
}
// retrieve the groups in a string/bool map as a structure to avoid duplicates
ldapMap := make(map[string]bool)
for _, e := range entries {
dn, err := ldap.ParseDN(e.DN)
if err != nil || len(dn.RDNs) == 0 {
continue
}
// Enumerate attributes of each result, parse out CN and add as group
values := e.GetAttributeValues(c.conf.GroupAttr)
if len(values) > 0 {
for _, val := range values {
groupCN := c.getCN(val)
ldapMap[groupCN] = true
}
} else {
// If groupattr didn't resolve, use self (enumerating group objects)
groupCN := c.getCN(e.DN)
ldapMap[groupCN] = true
}
}
ldapGroups := make([]string, 0, len(ldapMap))
for key := range ldapMap {
ldapGroups = append(ldapGroups, key)
}
return ldapGroups, warnings, nil
}
func (c *Client) tokenGroupsSearch(userDN string) ([]*ldap.Entry, []Warning, error) {
const op = "ldap.(Client).tokenGroupsSearch"
var warnings []Warning
if userDN == "" {
return nil, warnings, fmt.Errorf("%s: missing user dn: %w", op, ErrInvalidParameter)
}
result, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: userDN,
Scope: ldap.ScopeBaseObject,
Filter: "(objectClass=*)",
Attributes: []string{
"tokenGroups",
},
SizeLimit: 1,
})
if err != nil {
return nil, warnings, fmt.Errorf("%s: search failed (baseDN: %q / filter: %q): %w", op, userDN, "(objectClass=*)", err)
}
if len(result.Entries) == 0 {
warnings = append(warnings, fmtWarning("%s: unable to read object for group attributes: userdn %s and groupattr %s", op, userDN, c.conf.GroupAttr))
return make([]*ldap.Entry, 0), warnings, nil
}
userEntry := result.Entries[0]
groupAttrValues := userEntry.GetRawAttributeValues("tokenGroups")
groupEntries := make([]*ldap.Entry, 0, len(groupAttrValues))
for _, sidBytes := range groupAttrValues {
sidString, err := sidBytesToString(sidBytes)
if err != nil {
warnings = append(warnings, fmtWarning("%s: unable to read sid: %s", op, err.Error()))
continue
}
groupResult, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: fmt.Sprintf("<SID=%s>", sidString),
Scope: ldap.ScopeBaseObject,
Filter: "(objectClass=*)",
Attributes: []string{
"1.1", // RFC no attributes
},
SizeLimit: 1,
})
if err != nil {
warnings = append(warnings, fmtWarning("%s: unable to read the group sid (baseDN: %q / filter: %q): %s", op, fmt.Sprintf("<SID=%s>", sidString), "(objectClass=*)", sidString))
continue
}
if len(groupResult.Entries) == 0 {
warnings = append(warnings, fmtWarning("%s: unable to find the group sid (baseDN: %q / filter: %q): %s", op, fmt.Sprintf("<SID=%s>", sidString), "(objectClass=*)", sidString))
continue
}
groupEntries = append(groupEntries, groupResult.Entries[0])
}
return groupEntries, warnings, nil
}
func (c *Client) filterGroupsSearch(userDN string, username string) ([]*ldap.Entry, []Warning, error) {
const op = "ldap.(Client).filterGroupsSearch"
var warnings []Warning
if userDN == "" {
return nil, warnings, fmt.Errorf("%s: missing user dn: %w", op, ErrInvalidParameter)
}
if username == "" {
return nil, warnings, fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
if c.conf.GroupFilter == "" {
return make([]*ldap.Entry, 0), warnings, nil
}
if c.conf.GroupDN == "" {
return make([]*ldap.Entry, 0), warnings, nil
}
// Parse the configuration as a template.
// Example template "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={{.UserDN}}))"
t, err := template.New("queryTemplate").Parse(c.conf.GroupFilter)
if err != nil {
return nil, warnings, fmt.Errorf("%s: LDAP search failed due to template compilation error: %w", op, err)
}
// Build context to pass to template - we will be exposing UserDn and Username.
context := struct {
UserDN string
Username string
}{
ldap.EscapeFilter(userDN),
ldap.EscapeFilter(username),
}
var renderedQuery bytes.Buffer
if err := t.Execute(&renderedQuery, context); err != nil {
return nil, warnings, fmt.Errorf("%s: LDAP search failed due to template parsing error: %w", op, err)
}
result, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: c.conf.GroupDN,
Scope: ldap.ScopeWholeSubtree,
Filter: renderedQuery.String(),
Attributes: []string{
c.conf.GroupAttr,
},
SizeLimit: math.MaxInt32,
})
if err != nil {
switch {
case ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject):
warnings = append(warnings, Warning(err.Error()))
return []*ldap.Entry{}, warnings, nil
default:
return nil, warnings, fmt.Errorf("%s: LDAP search failed (baseDN: %q / filter: %q): %w", op, c.conf.GroupDN, renderedQuery.String(), err)
}
}
return result.Entries, warnings, nil
}
func sidBytesToString(b []byte) (string, error) {
const op = "ldap.sidBytesToString"
if b == nil {
return "", fmt.Errorf("%s: missing bytes: %w", op, ErrInvalidParameter)
}
reader := bytes.NewReader(b)
var revision, subAuthorityCount uint8
var identifierAuthorityParts [3]uint16
if err := binary.Read(reader, binary.LittleEndian, &revision); err != nil {
return "", fmt.Errorf("%s: SID %#v convert failed reading Revision: %w", op, b, err)
}
if err := binary.Read(reader, binary.LittleEndian, &subAuthorityCount); err != nil {
return "", fmt.Errorf("%s: SID %#v convert failed reading SubAuthorityCount: %w", op, b, err)
}
if err := binary.Read(reader, binary.BigEndian, &identifierAuthorityParts); err != nil {
return "", fmt.Errorf("%s: SID %#v convert failed reading IdentifierAuthority: %w", op, b, err)
}
identifierAuthority := (uint64(identifierAuthorityParts[0]) << 32) + (uint64(identifierAuthorityParts[1]) << 16) + uint64(identifierAuthorityParts[2])
subAuthority := make([]uint32, subAuthorityCount)
if err := binary.Read(reader, binary.LittleEndian, &subAuthority); err != nil {
return "", fmt.Errorf("%s: SID %#v convert failed reading SubAuthority: %w", op, b, err)
}
result := fmt.Sprintf("S-%d-%d", revision, identifierAuthority)
for _, subAuthorityPart := range subAuthority {
result += fmt.Sprintf("-%d", subAuthorityPart)
}
return result, nil
}
// SIDBytes creates a SID from the provided revision and identifierAuthority
func SIDBytes(revision uint8, identifierAuthority uint16) ([]byte, error) {
const op = "ldap.SidBytes"
var identifierAuthorityParts [3]uint16
identifierAuthorityParts[2] = identifierAuthority
subAuthorityCount := uint8(0)
var writer bytes.Buffer
if err := binary.Write(&writer, binary.LittleEndian, uint8(revision)); err != nil {
return nil, fmt.Errorf("%s: unable to write revision: %w", op, err)
}
if err := binary.Write(&writer, binary.LittleEndian, subAuthorityCount); err != nil {
return nil, fmt.Errorf("%s: unable to write subauthority count: %w", op, err)
}
if err := binary.Write(&writer, binary.BigEndian, identifierAuthorityParts); err != nil {
return nil, fmt.Errorf("%s: unable to write authority parts: %w", op, err)
}
return writer.Bytes(), nil
}
func (c *Client) getUserBindDN(username string) (string, error) {
const op = "ldap.(Client).getUserBindDN"
if username == "" {
return "", fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
// this validation check and the logic right below it are dependent, so if
// you update one of them, be sure to update the other one as well.
if !c.conf.DiscoverDN && (c.conf.BindDN == "" || c.conf.BindPassword == "") && c.conf.UPNDomain == "" && c.conf.UserDN == "" {
return "", fmt.Errorf("%s: cannot derive UserBindDN based on config (see combination of: discoverdn, binddn, bindpass, upndomain, userdn): %w", op, ErrInvalidParameter)
}
var bindDN string
if c.conf.DiscoverDN || (c.conf.BindDN != "" && c.conf.BindPassword != "") {
var err error
if c.conf.BindPassword != "" {
err = c.conn.Bind(c.conf.BindDN, c.conf.BindPassword)
} else {
err = c.conn.UnauthenticatedBind(c.conf.BindDN)
}
if err != nil {
return "", fmt.Errorf("%s: bind (service) failed: %w", op, err)
}
filter, err := c.renderUserSearchFilter(username)
if err != nil {
return "", fmt.Errorf("%s: %w", op, err)
}
result, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: c.conf.UserDN,
Scope: ldap.ScopeWholeSubtree,
Filter: filter,
SizeLimit: math.MaxInt32,
})
if err != nil {
return "", fmt.Errorf("%s: LDAP search for binddn failed using (baseDN: %q / filter: %q): %w", op, c.conf.UserDN, filter, err)
}
if len(result.Entries) != 1 {
return "", fmt.Errorf("LDAP search for binddn 0 or not unique")
}
bindDN = result.Entries[0].DN
} else {
if c.conf.UPNDomain != "" {
bindDN = fmt.Sprintf("%s@%s", EscapeValue(username), c.conf.UPNDomain)
} else {
bindDN = fmt.Sprintf("%s=%s,%s", c.conf.UserAttr, EscapeValue(username), c.conf.UserDN)
}
}
return bindDN, nil
}
/*
* Returns the DN of the object representing the authenticated user.
*/
func (c *Client) getUserDN(bindDN, username string) (string, error) {
const op = "ldap.(Client).getUserDN"
if bindDN == "" {
return "", fmt.Errorf("%s: missing bind dn: %w", op, ErrInvalidParameter)
}
if username == "" {
return "", fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
var userDN string
if c.conf.UPNDomain != "" {
// Find the distinguished name for the user if userPrincipalName used for login
filter := fmt.Sprintf("(userPrincipalName=%s@%s)", EscapeValue(username), c.conf.UPNDomain)
result, err := c.conn.Search(&ldap.SearchRequest{
BaseDN: c.conf.UserDN,
Scope: ldap.ScopeWholeSubtree,
Filter: filter,
SizeLimit: math.MaxInt32,
})
if err != nil {
return userDN, fmt.Errorf("%s: LDAP search failed for detecting user (baseDN: %q / filter: %q): %w", op, c.conf.UserDN, filter, err)
}
for _, e := range result.Entries {
userDN = e.DN
}
} else {
userDN = bindDN
}
return userDN, nil
}
/*
* Parses a distinguished name and returns the CN portion.
* Given a non-conforming string (such as an already-extracted CN),
* it will be returned as-is.
*/
func (c *Client) getCN(dn string) string {
parsedDN, err := ldap.ParseDN(dn)
if err != nil || len(parsedDN.RDNs) == 0 {
// It was already a CN, return as-is
return dn
}
for _, rdn := range parsedDN.RDNs {
for _, rdnAttr := range rdn.Attributes {
if c.conf.DeprecatedVaultPre111GroupCNBehavior == nil || *c.conf.DeprecatedVaultPre111GroupCNBehavior {
if rdnAttr.Type == "CN" {
return rdnAttr.Value
}
} else {
if strings.EqualFold(rdnAttr.Type, "CN") {
return rdnAttr.Value
}
}
}
}
// Default, return self
return dn
}
func (c *Client) renderUserSearchFilter(username string) (string, error) {
const (
op = "ldap.(Client).renderUserSearchFilter"
emptyUserFilter = ""
defaultUserFilter = "({{.UserAttr}}={{.Username}})"
queryTemplate = "queryTemplate"
)
if username == "" {
return "", fmt.Errorf("%s: missing username: %w", op, ErrInvalidParameter)
}
var userFilter string
// The UserFilter can be blank if not set, or running this version of the code
// on an existing ldap configuration
switch {
case c.conf.UserFilter != emptyUserFilter:
userFilter = c.conf.UserFilter
default:
userFilter = defaultUserFilter
}
// Parse the configuration as a template.
// Example template "({{.UserAttr}}={{.Username}})"
t, err := template.New(queryTemplate).Parse(userFilter)
if err != nil {
return "", fmt.Errorf("%s: search failed due to template compilation error: %w", op, err)
}
// Build context to pass to template - we will be exposing UserDn and Username.
context := struct {
UserAttr string
Username string
}{
EscapeFilter(c.conf.UserAttr),
EscapeFilter(username),
}
if c.conf.UPNDomain != "" {
context.UserAttr = "userPrincipalName"
// Intentionally, calling EscapeFilter(...) (vs EscapeValue) since the
// username is being injected into a search filter.
// As an untrusted string, the username must be escaped according to RFC
// 4515, in order to prevent attackers from injecting characters that could modify the filter
context.Username = fmt.Sprintf("%s@%s", EscapeFilter(username), c.conf.UPNDomain)
}
var renderedFilter bytes.Buffer
if err := t.Execute(&renderedFilter, context); err != nil {
return "", fmt.Errorf("%s: search failed due to template parsing error: %w", op, err)
}
return renderedFilter.String(), nil
}
func getTLSConfig(host string, opt ...Option) (*tls.Config, error) {
const op = "ldap.getTLSConfig"
if host == "" {
return nil, fmt.Errorf("%s: missing host: %w", op, ErrInvalidParameter)
}
opts := getConfigOpts(opt...)
tlsConfig := &tls.Config{
ServerName: host,
}
if opts.withTLSMinVersion != "" {
tlsMinVersion, ok := tlsutil.TLSLookup[opts.withTLSMinVersion]
if !ok {
return nil, fmt.Errorf("%s: invalid 'tls_min_version' in config: %w", op, ErrInvalidParameter)
}
tlsConfig.MinVersion = tlsMinVersion
}
if opts.withTLSMaxVersion != "" {
tlsMaxVersion, ok := tlsutil.TLSLookup[opts.withTLSMaxVersion]
if !ok {
return nil, fmt.Errorf("%s: invalid 'tls_max_version' in config: %w", op, ErrInvalidParameter)
}
tlsConfig.MaxVersion = tlsMaxVersion
}
if opts.withInsecureTLS {
tlsConfig.InsecureSkipVerify = true
}
if opts.withCertificates != nil {
caPool := x509.NewCertPool()
for _, c := range opts.withCertificates {
ok := caPool.AppendCertsFromPEM([]byte(c))
if !ok {
return nil, fmt.Errorf("%s: could not append CA certificate: %w", op, ErrUnknown)
}
}
tlsConfig.RootCAs = caPool
}
if opts.withClientTLSCert != "" && opts.withClientTLSKey != "" {
certificate, err := tls.X509KeyPair([]byte(opts.withClientTLSCert), []byte(opts.withClientTLSKey))
if err != nil {
return nil, fmt.Errorf("%s: failed to parse client X509 key pair: %w", op, err)
}
tlsConfig.Certificates = append(tlsConfig.Certificates, certificate)
} else if opts.withClientTLSCert != "" || opts.withClientTLSKey != "" {
return nil, fmt.Errorf("%s: both client_tls_cert and client_tls_key must be set in configuration: %w", op, ErrInvalidParameter)
}
return tlsConfig, nil
}
// EscapeValue will properly escape the input string as an ldap value
func EscapeValue(input string) string {
if input == "" {
return ""
}
// RFC4514 forbids un-escaped:
// - leading space or hash
// - trailing space
// - special characters '"', '+', ',', ';', '<', '>', '\\'
// - null
inputLen := len(input)
for i := 0; i < inputLen; i++ {
escaped := false
if input[i] == '\\' {
i++
if i > inputLen-1 {
break
}
escaped = true
}
switch input[i] {
case '"', '+', ',', ';', '<', '>', '\\':
if !escaped {
input = input[0:i] + "\\" + input[i:]
i++
}
continue
case '\000':
input = input[0:i] + `\00` + input[i+1:]
continue
}
if escaped {
input = input[0:i] + "\\" + input[i:]
i++
}
}
if input[0] == ' ' || input[0] == '#' {
input = "\\" + input
}
if input[len(input)-1] == ' ' {
input = input[0:len(input)-1] + "\\ "
}
return input
}
func EscapeFilter(filter string) string {
return ldap.EscapeFilter(filter)
}