-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_util.go
183 lines (166 loc) · 5.35 KB
/
ldap_util.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
package ldap
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
ldapv2 "gopkg.in/ldap.v2"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
func NewLDAPConn(servers []string, TLS bool, port int64, connectionTimeout int64, caPool *x509.CertPool) (*ldapv2.Conn, error) {
logrus.Debug("Now creating Ldap connection")
var lConn *ldapv2.Conn
var err error
var tlsConfig *tls.Config
// TODO implment multi-server support
if len(servers) != 1 {
return nil, errors.New("invalid server config. only exactly 1 server is currently supported")
}
server := servers[0]
if TLS {
tlsConfig = &tls.Config{RootCAs: caPool, InsecureSkipVerify: false, ServerName: server}
lConn, err = ldapv2.DialTLS("tcp", fmt.Sprintf("%s:%d", server, port), tlsConfig)
if err != nil {
return nil, fmt.Errorf("Error creating ssl connection: %v", err)
}
} else {
lConn, err = ldapv2.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
if err != nil {
return nil, fmt.Errorf("Error creating connection: %v", err)
}
}
lConn.SetTimeout(time.Duration(connectionTimeout) * time.Second)
return lConn, nil
}
func GetUserExternalID(username string, loginDomain string) string {
if strings.Contains(username, "\\") {
return username
} else if loginDomain != "" {
return loginDomain + "\\" + username
}
return username
}
func HasPermission(attributes []*ldapv2.EntryAttribute, userObjectClass string, userEnabledAttribute string, userDisabledBitMask int64) bool {
var permission int64
if !IsType(attributes, userObjectClass) {
return true
}
if userEnabledAttribute != "" {
for _, attr := range attributes {
if attr.Name == userEnabledAttribute {
if len(attr.Values) > 0 && attr.Values[0] != "" {
intAttr, err := strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logrus.Errorf("Failed to get USER_ENABLED_ATTRIBUTE, error: %v", err)
return false
}
permission = intAttr
}
}
}
} else {
return true
}
permission = permission & userDisabledBitMask
return permission != userDisabledBitMask
}
func IsType(search []*ldapv2.EntryAttribute, varType string) bool {
for _, attrib := range search {
if attrib.Name == "objectClass" {
for _, val := range attrib.Values {
if val == varType {
return true
}
}
}
}
logrus.Debugf("Failed to determine if object is type: %s", varType)
return false
}
func EscapeLDAPSearchFilter(filter string) string {
buf := new(bytes.Buffer)
for i := 0; i < len(filter); i++ {
currChar := filter[i]
switch currChar {
case '\\':
buf.WriteString("\\5c")
break
case '*':
buf.WriteString("\\2a")
break
case '(':
buf.WriteString("\\28")
break
case ')':
buf.WriteString("\\29")
break
case '\u0000':
buf.WriteString("\\00")
break
default:
buf.WriteString(string(currChar))
}
}
return buf.String()
}
func GetUserSearchAttributes(memberOfAttribute, objectClassAttribute string, config *v3.ActiveDirectoryConfig) []string {
srchAttributes := strings.Split(config.UserSearchAttribute, "|")
userSearchAttributes := []string{memberOfAttribute,
objectClassAttribute,
config.UserObjectClass,
config.UserLoginAttribute,
config.UserNameAttribute,
config.UserEnabledAttribute}
userSearchAttributes = append(userSearchAttributes, srchAttributes...)
return userSearchAttributes
}
func GetGroupSearchAttributes(memberOfAttribute, objectClassAttribute string, config *v3.ActiveDirectoryConfig) []string {
groupSeachAttributes := []string{memberOfAttribute,
objectClassAttribute,
config.GroupObjectClass,
config.UserLoginAttribute,
config.GroupNameAttribute,
config.GroupSearchAttribute}
return groupSeachAttributes
}
func GetUserSearchAttributesForLDAP(config *v3.LdapConfig) []string {
userSearchAttributes := []string{"dn", config.UserMemberAttribute,
"objectClass",
config.UserObjectClass,
config.UserLoginAttribute,
config.UserNameAttribute,
config.UserEnabledAttribute}
return userSearchAttributes
}
func GetGroupSearchAttributesForLDAP(config *v3.LdapConfig) []string {
groupSeachAttributes := []string{config.GroupMemberUserAttribute,
"objectClass",
config.GroupObjectClass,
config.UserLoginAttribute,
config.GroupNameAttribute,
config.GroupSearchAttribute}
return groupSeachAttributes
}
func AuthenticateServiceAccountUser(enabled bool, serviceAccountPassword string, serviceAccountUsername string, lConn *ldapv2.Conn) (v3.Principal, []v3.Principal, map[string]string, error) {
if !enabled { // TODO testing for enabled here might not be correct. Might be better to pass in an explicit testSvcAccount bool
logrus.Debug("Bind service account username password")
if serviceAccountPassword == "" {
return v3.Principal{}, nil, nil, httperror.NewAPIError(httperror.MissingRequired, "service account password not provided")
}
sausername := GetUserExternalID(serviceAccountUsername, "")
err := lConn.Bind(sausername, serviceAccountPassword)
if err != nil {
if ldapv2.IsErrorWithCode(err, ldapv2.LDAPResultInvalidCredentials) {
return v3.Principal{}, nil, nil, httperror.WrapAPIError(err, httperror.Unauthorized, "authentication failed")
}
return v3.Principal{}, nil, nil, httperror.WrapAPIError(err, httperror.ServerError, "server error while authenticating")
}
}
return v3.Principal{}, nil, nil, nil
}