forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_util.go
140 lines (127 loc) · 3.54 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
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/types/apis/management.cattle.io/v3"
)
func NewLDAPConn(config *v3.ActiveDirectoryConfig, 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(config.Servers) != 1 {
return nil, errors.New("invalid server config. only exactly 1 server is currently supported")
}
server := config.Servers[0]
if config.TLS {
tlsConfig = &tls.Config{RootCAs: caPool, InsecureSkipVerify: false, ServerName: server}
lConn, err = ldapv2.DialTLS("tcp", fmt.Sprintf("%s:%d", server, config.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, config.Port))
if err != nil {
return nil, fmt.Errorf("Error creating connection: %v", err)
}
}
lConn.SetTimeout(time.Duration(config.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, config *v3.ActiveDirectoryConfig) bool {
var permission int64
if !IsType(attributes, config.UserObjectClass) {
return true
}
for _, attr := range attributes {
if attr.Name == config.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 & config.UserDisabledBitMask
return permission != config.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 {
userSearchAttributes := []string{memberOfAttribute,
objectClassAttribute,
config.UserObjectClass,
config.UserLoginAttribute,
config.UserNameAttribute,
config.UserSearchAttribute,
config.UserEnabledAttribute}
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
}