-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
92 lines (73 loc) · 2.55 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package openldap
import (
"fmt"
"github.com/go-ldap/ldap/v3"
"github.com/go-ldap/ldif"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/ldaputil"
"github.com/hashicorp/vault-plugin-secrets-openldap/client"
)
type ldapClient interface {
UpdateDNPassword(conf *client.Config, dn string, newPassword string) error
UpdateUserPassword(conf *client.Config, user, newPassword string) error
Execute(conf *client.Config, entries []*ldif.Entry, continueOnError bool) error
}
func NewClient(logger hclog.Logger) *Client {
return &Client{
ldap: client.New(logger),
}
}
var _ ldapClient = (*Client)(nil)
type Client struct {
ldap client.Client
}
// UpdateDNPassword updates the password for the object with the given DN.
func (c *Client) UpdateDNPassword(conf *client.Config, dn string, newPassword string) error {
scope := ldap.ScopeBaseObject
filters := map[*client.Field][]string{
client.FieldRegistry.ObjectClass: {"*"},
}
userAttr := conf.UserAttr
if userAttr == "" {
userAttr = defaultUserAttr(conf.Schema)
}
field := client.FieldRegistry.Parse(userAttr)
if field == nil {
return fmt.Errorf("unsupported userattr %q", userAttr)
}
if field == client.FieldRegistry.UserPrincipalName && conf.UPNDomain != "" {
scope = ldap.ScopeWholeSubtree
bindUser := fmt.Sprintf("%s@%s", ldaputil.EscapeLDAPValue(dn), conf.UPNDomain)
filters[field] = []string{bindUser}
dn = conf.UserDN
}
newValues, err := client.GetSchemaFieldRegistry(conf.Schema, newPassword)
if err != nil {
return fmt.Errorf("error updating password: %s", err)
}
return c.ldap.UpdatePassword(conf, dn, scope, newValues, filters)
}
// UpdateUserPassword updates the password for the object with the given username.
func (c *Client) UpdateUserPassword(conf *client.Config, username string, newPassword string) error {
userAttr := conf.UserAttr
if userAttr == "" {
userAttr = defaultUserAttr(conf.Schema)
}
field := client.FieldRegistry.Parse(userAttr)
if field == nil {
return fmt.Errorf("unsupported userattr %q", userAttr)
}
filters := map[*client.Field][]string{
field: {username},
}
newValues, err := client.GetSchemaFieldRegistry(conf.Schema, newPassword)
if err != nil {
return fmt.Errorf("error updating password: %s", err)
}
return c.ldap.UpdatePassword(conf, conf.UserDN, ldap.ScopeWholeSubtree, newValues, filters)
}
func (c *Client) Execute(conf *client.Config, entries []*ldif.Entry, continueOnError bool) (err error) {
return c.ldap.Execute(conf, entries, continueOnError)
}