forked from hashicorp/vault-plugin-secrets-openldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
68 lines (53 loc) · 1.95 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
package openldap
import (
"fmt"
"github.com/go-ldap/ldap/v3"
"github.com/go-ldap/ldif"
"github.com/hashicorp/go-hclog"
"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 {
filters := map[*client.Field][]string{client.FieldRegistry.ObjectClass: {"*"}}
newValues, err := client.GetSchemaFieldRegistry(conf.Schema, newPassword)
if err != nil {
return fmt.Errorf("error updating password: %s", err)
}
return c.ldap.UpdatePassword(conf, dn, ldap.ScopeBaseObject, 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)
}