/
ldap.go
57 lines (44 loc) · 1.14 KB
/
ldap.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
package vault
import (
"errors"
"fmt"
"strings"
hashivault "github.com/hashicorp/vault/api"
)
type LDAP struct {
client *hashivault.Client
}
type LDAPLoginOptions struct {
Username string
Password string
MountPath string
}
func (l *LDAP) Login(options LDAPLoginOptions) (*hashivault.Secret, error) {
authSecret, err := l.ldapLogin(options)
if err != nil {
return nil, err
}
if authSecret.Auth == nil {
return nil, errors.New("Vault LDAP Auth returned nil")
}
l.client.SetToken(authSecret.Auth.ClientToken)
return authSecret, nil
}
func (l *LDAP) ldapLogin(options LDAPLoginOptions) (*hashivault.Secret, error) {
ldapCreds := map[string]interface{}{
"password": options.Password,
}
pathFormatString := "auth/ldap/login/%s"
if options.MountPath != "" {
pathFormatString = "auth/" + strings.Trim(options.MountPath, "/") + "/login/%s"
}
normalizedPath := fmt.Sprintf(pathFormatString, options.Username)
authSecret, err := l.client.Logical().Write(normalizedPath, ldapCreds)
if err != nil {
return nil, err
}
if authSecret == nil {
return nil, errors.New("empty response from vault ldap")
}
return authSecret, nil
}