This repository has been archived by the owner on Oct 6, 2019. It is now read-only.
/
ldap.go
140 lines (120 loc) · 3.12 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
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
package vault
import (
"errors"
"strings"
)
type LDAPGroup struct {
Name string
Policies []string
}
type LDAPUser struct {
Name string
Policies []string
Groups []string
}
func (auth AuthInfo) ListLDAPGroups() ([]LDAPGroup, error) {
client, err := auth.Client()
if err != nil {
return nil, err
}
logical := client.Logical()
resp, err := logical.List("auth/ldap/groups")
if err != nil {
return nil, err
}
// if there are no ldap groups, return an empty slice
if resp == nil || resp.Data == nil {
return []LDAPGroup{}, nil
}
raw, ok := resp.Data["keys"].([]interface{})
if !ok {
return nil, errors.New("Failed to fetch LDAP group names")
}
// ignore any group names that somehow can't be type asserted to string
var groups []string
for _, each := range raw {
if group, ok := each.(string); ok {
groups = append(groups, group)
}
}
results := make([]LDAPGroup, len(groups))
for i, group := range groups {
results[i] = LDAPGroup{
Name: group,
}
// fetch group's policies
resp, err := logical.Read("auth/ldap/groups/" + group)
if err == nil && resp != nil {
if raw, ok := resp.Data["policies"]; ok {
// vault v0.8.3 and higher returns an array of strings
if policies, ok := raw.([]interface{}); ok {
for _, p := range policies {
if s, ok := p.(string); ok {
results[i].Policies = append(results[i].Policies, s)
}
}
// vault v0.8.2 and lower has a different JSON response
} else if policies, ok := raw.(string); ok {
results[i].Policies = strings.Split(policies, ",")
}
}
}
}
return results, nil
}
func (auth AuthInfo) ListLDAPUsers() ([]LDAPUser, error) {
client, err := auth.Client()
if err != nil {
return nil, err
}
logical := client.Logical()
resp, err := logical.List("auth/ldap/users")
if err != nil {
return nil, err
}
// if there are no ldap users, return an empty slice
if resp == nil || resp.Data == nil {
return []LDAPUser{}, nil
}
raw, ok := resp.Data["keys"].([]interface{})
if !ok {
return nil, errors.New("Failed to fetch LDAP usernames")
}
// ignore any user names that somehow can't be type asserted to string
var users []string
for _, each := range raw {
if user, ok := each.(string); ok {
users = append(users, user)
}
}
results := make([]LDAPUser, len(users))
for i, user := range users {
results[i] = LDAPUser{
Name: user,
}
// fetch user's policies and groups
resp, err := logical.Read("auth/ldap/users/" + user)
if err != nil || resp == nil {
continue
}
if raw, ok := resp.Data["policies"]; ok {
// vault v0.8.3 and higher returns an array of strings
if policies, ok := raw.([]interface{}); ok {
for _, p := range policies {
if s, ok := p.(string); ok {
results[i].Policies = append(results[i].Policies, s)
}
}
// vault v0.8.2 and lower has a different JSON response
} else if policies, ok := raw.(string); ok {
results[i].Policies = strings.Split(policies, ",")
}
}
if raw, ok := resp.Data["groups"]; ok {
if groups, ok := raw.(string); ok {
results[i].Groups = strings.Split(groups, ",")
}
}
}
return results, nil
}