-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.go
133 lines (113 loc) · 3.17 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
package client
import (
"fmt"
"math"
"strings"
"github.com/go-errors/errors"
"github.com/go-ldap/ldap"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/helper/ldaputil"
"golang.org/x/text/encoding/unicode"
)
func NewClient(logger hclog.Logger) *Client {
return &Client{
ldap: &ldaputil.Client{
Logger: logger,
LDAP: ldaputil.NewLDAP(),
},
}
}
type Client struct {
ldap *ldaputil.Client
}
func (c *Client) Search(cfg *ldaputil.ConfigEntry, filters map[*Field][]string) ([]*Entry, error) {
req := &ldap.SearchRequest{
BaseDN: cfg.UserDN,
Scope: ldap.ScopeWholeSubtree,
Filter: toString(filters),
SizeLimit: math.MaxInt32,
}
conn, err := c.ldap.DialLDAP(cfg)
if err != nil {
return nil, err
}
defer conn.Close()
if err := bind(cfg, conn); err != nil {
return nil, err
}
result, err := conn.Search(req)
if err != nil {
return nil, err
}
entries := make([]*Entry, len(result.Entries))
for i, rawEntry := range result.Entries {
entries[i] = NewEntry(rawEntry)
}
return entries, nil
}
func (c *Client) UpdateEntry(cfg *ldaputil.ConfigEntry, filters map[*Field][]string, newValues map[*Field][]string) error {
entries, err := c.Search(cfg, filters)
if err != nil {
return err
}
if len(entries) != 1 {
return fmt.Errorf("filter of %s doesn't match just one entry: %s", filters, entries)
}
modifyReq := &ldap.ModifyRequest{
DN: entries[0].DN,
}
for field, vals := range newValues {
modifyReq.Replace(field.String(), vals)
}
conn, err := c.ldap.DialLDAP(cfg)
if err != nil {
return err
}
defer conn.Close()
if err := bind(cfg, conn); err != nil {
return err
}
return conn.Modify(modifyReq)
}
// UpdatePassword uses a Modify call under the hood because
// Active Directory doesn't recognize the passwordModify method.
// See https://github.com/go-ldap/ldap/issues/106
// for more.
func (c *Client) UpdatePassword(cfg *ldaputil.ConfigEntry, filters map[*Field][]string, newPassword string) error {
pwdEncoded, err := formatPassword(newPassword)
if err != nil {
return err
}
newValues := map[*Field][]string{
FieldRegistry.UnicodePassword: {pwdEncoded},
}
return c.UpdateEntry(cfg, filters, newValues)
}
// According to the MS docs, the password needs to be utf16 and enclosed in quotes.
func formatPassword(original string) (string, error) {
utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
return utf16.NewEncoder().String("\"" + original + "\"")
}
// Ex. "(cn=Ellen Jones)"
func toString(filters map[*Field][]string) string {
var fieldEquals []string
for f, values := range filters {
for _, v := range values {
fieldEquals = append(fieldEquals, fmt.Sprintf("%s=%s", f, v))
}
}
result := strings.Join(fieldEquals, ",")
return "(" + result + ")"
}
func bind(cfg *ldaputil.ConfigEntry, conn ldaputil.Connection) error {
if cfg.BindPassword == "" {
return errors.New("unable to bind due to lack of configured password")
}
if cfg.UPNDomain != "" {
return conn.Bind(fmt.Sprintf("%s@%s", ldaputil.EscapeLDAPValue(cfg.BindDN), cfg.UPNDomain), cfg.BindPassword)
}
if cfg.BindDN != "" {
return conn.Bind(cfg.BindDN, cfg.BindPassword)
}
return errors.New("must provide binddn or upndomain")
}