-
Notifications
You must be signed in to change notification settings - Fork 127
/
ldap.go
186 lines (161 loc) · 4.18 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package groups
import (
"fmt"
"strings"
"sync"
"time"
"github.com/go-ldap/ldap/v3"
log "github.com/sirupsen/logrus"
"github.com/armadaproject/armada/internal/common/auth/configuration"
)
type GroupLookup interface {
GetGroupNames(SIDs []string) ([]string, error)
}
type LDAPGroupLookup struct {
config configuration.LDAPConfig
cache map[string]cacheRecord
cacheExpiry time.Duration
mutex sync.Mutex
}
func NewLDAPGroupLookup(config configuration.LDAPConfig) *LDAPGroupLookup {
expiry := config.CacheExpiry
if expiry == 0 {
expiry = 5 * time.Minute
}
return &LDAPGroupLookup{
config: config,
cache: map[string]cacheRecord{},
cacheExpiry: expiry,
}
}
type cacheRecord struct {
name string
created time.Time
}
func (lookup *LDAPGroupLookup) GetGroupNames(SIDs []string) ([]string, error) {
err := lookup.updateCache(SIDs)
if err != nil {
return nil, err
}
result := lookup.readFromCache(SIDs)
return result, nil
}
func (lookup *LDAPGroupLookup) readFromCache(SIDs []string) []string {
lookup.mutex.Lock()
defer lookup.mutex.Unlock()
result := []string{}
for _, sid := range SIDs {
record, ok := lookup.cache[sid]
if ok && record.name != "" {
result = append(result, record.name)
}
}
return result
}
func (lookup *LDAPGroupLookup) updateCache(SIDs []string) error {
missing := lookup.getMissingGroups(SIDs)
if len(missing) == 0 {
return nil
}
groupNames, err := lookup.getGroupNames(missing)
if err != nil {
return err
}
lookup.saveInCache(SIDs, groupNames)
return nil
}
func (lookup *LDAPGroupLookup) saveInCache(SIDs []string, groupNames map[string]string) {
lookup.mutex.Lock()
defer lookup.mutex.Unlock()
for sid, name := range groupNames {
lookup.cache[sid] = cacheRecord{
name: name,
created: time.Now(),
}
}
for _, sid := range SIDs {
_, exists := lookup.cache[sid]
if !exists {
lookup.cache[sid] = cacheRecord{
name: "",
created: time.Now(),
}
log.Warnf("Could not find group name for %s", sid)
}
}
}
func (lookup *LDAPGroupLookup) getMissingGroups(SIDs []string) []string {
lookup.mutex.Lock()
defer lookup.mutex.Unlock()
deadline := time.Now().Add(-lookup.cacheExpiry)
missing := []string{}
for _, sid := range SIDs {
record, ok := lookup.cache[sid]
if !ok || record.created.Before(deadline) {
missing = append(missing, sid)
}
}
return missing
}
func (lookup *LDAPGroupLookup) getGroupNames(SIDs []string) (map[string]string, error) {
l, err := ldap.DialURL(lookup.config.URL)
if err != nil {
log.Errorf("LDAP dial error: %s", err)
return nil, err
}
defer l.Close()
err = l.Bind(lookup.config.Username, lookup.config.Password)
if err != nil {
log.Errorf("LDAP bind error %s", err)
return nil, err
}
searchRequest := lookup.createGroupSearch(SIDs)
sr, err := l.Search(searchRequest)
if err != nil {
log.Errorf("LDAP search error %s", err)
return nil, err
}
result := map[string]string{}
for _, entry := range sr.Entries {
bytes := entry.GetEqualFoldRawAttributeValue("objectSid")
sid := decodeSID(bytes)
result[sid] = entry.GetAttributeValue("cn")
}
return result, nil
}
func (lookup *LDAPGroupLookup) createGroupSearch(SIDs []string) *ldap.SearchRequest {
return ldap.NewSearchRequest(
lookup.config.GroupSearchBase,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=group)(|(objectSID="+
strings.Join(SIDs, ")(objectSID=")+
")))",
[]string{"cn", "objectSid"},
nil,
)
}
// Modified from https://github.com/bwmarrin/go-objectsid, newer go compiler requires uints for bit shifts
func decodeSID(b []byte) string {
revisionLevel := int(b[0])
subAuthorityCount := int(b[1]) & 0xFF
var authority uint = 0
for i := 2; i <= 7; i++ {
authority = authority | uint(b[i])<<uint(8*(5-(i-2)))
}
offset := 8
size := 4
var subAuthorities []uint
for i := 0; i < subAuthorityCount; i++ {
var subAuthority uint
for k := 0; k < size; k++ {
subAuthority = subAuthority | (uint(b[offset+k])&0xFF)<<uint(8*k)
}
subAuthorities = append(subAuthorities, subAuthority)
offset += size
}
s := fmt.Sprintf("S-%d-%d", revisionLevel, authority)
for _, v := range subAuthorities {
s += fmt.Sprintf("-%d", v)
}
return s
}