-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathget_group.go
148 lines (135 loc) · 3.24 KB
/
get_group.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
package pkg
import (
"fmt"
"strconv"
"github.com/go-ldap/ldap/v3"
pb "github.com/romnn/ldap-manager/pkg/grpc/gen"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// A ZeroOrMultipleGroupsError is returned when zero or multiple
// groups are found
type ZeroOrMultipleGroupsError struct {
Group string
GID int
Count int
}
func (e *ZeroOrMultipleGroupsError) groupName() string {
if e.Group != "" {
return fmt.Sprintf("name %q", e.Group)
}
return fmt.Sprintf("GID %d", e.GID)
}
func (e *ZeroOrMultipleGroupsError) Error() string {
if e.Count > 1 {
return fmt.Sprintf(
"multiple (%d) groups with %s",
e.Count, e.groupName(),
)
}
return fmt.Sprintf(
"no group with %s",
e.groupName(),
)
}
// StatusError returns the GRPC status error for this error
func (e *ZeroOrMultipleGroupsError) StatusError() error {
if e.Count > 1 {
return status.Errorf(codes.Internal, e.Error())
}
return status.Errorf(codes.NotFound, e.Error())
}
const (
groupGidNumber = "gidNumber"
groupCN = "cn"
)
// ParseGroup parses an ldap.Entry as a group
func (m *LDAPManager) parseGroup(entry *ldap.Entry) (*pb.Group, error) {
groupName := entry.GetAttributeValue(groupCN)
memberDNlist := entry.GetAttributeValues(m.GroupMembershipAttribute)
var members []*pb.GroupMember
for _, memberDN := range memberDNlist {
parts := ParseDN(memberDN)
usernames, ok := parts[m.AccountAttribute]
if !ok || ok && len(usernames) != 1 {
return nil, fmt.Errorf(
"failed to get username for group member %q",
memberDN,
)
}
members = append(members, &pb.GroupMember{
Username: usernames[0],
Dn: memberDN,
Group: groupName,
})
}
GID, err := strconv.Atoi(entry.GetAttributeValue(groupGidNumber))
if err != nil {
return nil, fmt.Errorf(
"failed to parse group GID as integer: %v",
err,
)
}
return &pb.Group{
Members: members,
Name: groupName,
GID: int64(GID),
}, nil
}
func (m *LDAPManager) groupFields() []string {
return []string{
m.GroupMembershipAttribute,
groupGidNumber,
groupCN,
}
}
// GetGroupByGID gets a group by its GID
func (m *LDAPManager) GetGroupByGID(GID int) (*pb.Group, error) {
conn, err := m.Pool.Get()
if err != nil {
return nil, err
}
defer conn.Close()
result, err := conn.Search(ldap.NewSearchRequest(
m.GroupsDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(gid=%d)", GID),
m.groupFields(),
[]ldap.Control{},
))
if err != nil {
return nil, err
}
if len(result.Entries) != 1 {
return nil, &ZeroOrMultipleGroupsError{
GID: GID,
Count: len(result.Entries),
}
}
return m.parseGroup(result.Entries[0])
}
// GetGroupByName gets a group by its name
func (m *LDAPManager) GetGroupByName(name string) (*pb.Group, error) {
conn, err := m.Pool.Get()
if err != nil {
return nil, err
}
defer conn.Close()
result, err := conn.Search(ldap.NewSearchRequest(
m.GroupsDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(cn=%s)", EscapeFilter(name)),
m.groupFields(),
[]ldap.Control{},
))
if err != nil {
return nil, err
}
if len(result.Entries) != 1 {
return nil, &ZeroOrMultipleGroupsError{
Group: name,
Count: len(result.Entries),
}
}
return m.parseGroup(result.Entries[0])
}