This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ldap.go
213 lines (176 loc) · 8.28 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package validation
import (
"fmt"
"strings"
"gopkg.in/ldap.v2"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/openshift/origin/pkg/auth/ldaputil"
"github.com/openshift/origin/pkg/cmd/server/api"
)
func ValidateLDAPSyncConfig(config *api.LDAPSyncConfig) ValidationResults {
validationResults := ValidationResults{}
validationResults.Append(ValidateStringSource(config.BindPassword, field.NewPath("bindPassword")))
bindPassword, _ := api.ResolveStringValue(config.BindPassword)
validationResults.Append(ValidateLDAPClientConfig(config.URL, config.BindDN, bindPassword, config.CA, config.Insecure, nil))
for ldapGroupUID, openShiftGroupName := range config.LDAPGroupUIDToOpenShiftGroupNameMapping {
if len(ldapGroupUID) == 0 || len(openShiftGroupName) == 0 {
validationResults.AddErrors(field.Invalid(field.NewPath("groupUIDNameMapping").Key(ldapGroupUID), openShiftGroupName, "has empty key or value"))
}
}
schemaConfigsFound := []string{}
if config.RFC2307Config != nil {
configResults := ValidateRFC2307Config(config.RFC2307Config)
validationResults.AddErrors(configResults.Errors...)
validationResults.AddWarnings(configResults.Warnings...)
schemaConfigsFound = append(schemaConfigsFound, "rfc2307")
}
if config.ActiveDirectoryConfig != nil {
configResults := ValidateActiveDirectoryConfig(config.ActiveDirectoryConfig)
validationResults.AddErrors(configResults.Errors...)
validationResults.AddWarnings(configResults.Warnings...)
schemaConfigsFound = append(schemaConfigsFound, "activeDirectory")
}
if config.AugmentedActiveDirectoryConfig != nil {
configResults := ValidateAugmentedActiveDirectoryConfig(config.AugmentedActiveDirectoryConfig)
validationResults.AddErrors(configResults.Errors...)
validationResults.AddWarnings(configResults.Warnings...)
schemaConfigsFound = append(schemaConfigsFound, "augmentedActiveDirectory")
}
if len(schemaConfigsFound) > 1 {
validationResults.AddErrors(field.Invalid(field.NewPath("schema"), config, fmt.Sprintf("only one schema-specific config is allowed; found %v", schemaConfigsFound)))
}
if len(schemaConfigsFound) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("schema"), fmt.Sprintf("exactly one schema-specific config is required; one of %v", []string{"rfc2307", "activeDirectory", "augmentedActiveDirectory"})))
}
return validationResults
}
func ValidateLDAPClientConfig(url, bindDN, bindPassword, CA string, insecure bool, fldPath *field.Path) ValidationResults {
validationResults := ValidationResults{}
if len(url) == 0 {
validationResults.AddErrors(field.Required(fldPath.Child("url"), ""))
return validationResults
}
u, err := ldaputil.ParseURL(url)
if err != nil {
validationResults.AddErrors(field.Invalid(fldPath.Child("url"), url, err.Error()))
return validationResults
}
// Make sure bindDN and bindPassword are both set, or both unset
// Both unset means an anonymous bind is used for search (https://tools.ietf.org/html/rfc4513#section-5.1.1)
// Both set means the name/password simple bind is used for search (https://tools.ietf.org/html/rfc4513#section-5.1.3)
if (len(bindDN) == 0) != (len(bindPassword) == 0) {
validationResults.AddErrors(field.Invalid(fldPath.Child("bindDN"), bindDN,
"bindDN and bindPassword must both be specified, or both be empty"))
validationResults.AddErrors(field.Invalid(fldPath.Child("bindPassword"), "(masked)",
"bindDN and bindPassword must both be specified, or both be empty"))
}
if insecure {
if u.Scheme == ldaputil.SchemeLDAPS {
validationResults.AddErrors(field.Invalid(fldPath.Child("url"), url,
fmt.Sprintf("Cannot use %s scheme with insecure=true", u.Scheme)))
}
if len(CA) > 0 {
validationResults.AddErrors(field.Invalid(fldPath.Child("ca"), CA,
"Cannot specify a ca with insecure=true"))
}
} else {
if len(CA) > 0 {
validationResults.AddErrors(ValidateFile(CA, fldPath.Child("ca"))...)
}
}
// Warn if insecure
if insecure {
validationResults.AddWarnings(field.Invalid(fldPath.Child("insecure"), insecure,
"validating passwords over an insecure connection could allow them to be intercepted"))
}
return validationResults
}
func ValidateRFC2307Config(config *api.RFC2307Config) ValidationResults {
validationResults := ValidationResults{}
validationResults.Append(ValidateLDAPQuery(config.AllGroupsQuery, field.NewPath("groupsQuery")))
if len(config.GroupUIDAttribute) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupUIDAttribute"), ""))
}
if len(config.GroupNameAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupNameAttributes"), ""))
}
if len(config.GroupMembershipAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupMembershipAttributes"), ""))
}
isUserDNQuery := strings.TrimSpace(strings.ToLower(config.UserUIDAttribute)) == "dn"
validationResults.Append(validateLDAPQuery(config.AllUsersQuery, field.NewPath("usersQuery"), isUserDNQuery))
if len(config.UserUIDAttribute) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("userUIDAttribute"), ""))
}
if len(config.UserNameAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("userNameAttributes"), ""))
}
return validationResults
}
func ValidateActiveDirectoryConfig(config *api.ActiveDirectoryConfig) ValidationResults {
validationResults := ValidationResults{}
validationResults.Append(ValidateLDAPQuery(config.AllUsersQuery, field.NewPath("usersQuery")))
if len(config.UserNameAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("userNameAttributes"), ""))
}
if len(config.GroupMembershipAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupMembershipAttributes"), ""))
}
return validationResults
}
func ValidateAugmentedActiveDirectoryConfig(config *api.AugmentedActiveDirectoryConfig) ValidationResults {
validationResults := ValidationResults{}
validationResults.Append(ValidateLDAPQuery(config.AllUsersQuery, field.NewPath("usersQuery")))
if len(config.UserNameAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("userNameAttributes"), ""))
}
if len(config.GroupMembershipAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupMembershipAttributes"), ""))
}
isGroupDNQuery := strings.TrimSpace(strings.ToLower(config.GroupUIDAttribute)) == "dn"
validationResults.Append(validateLDAPQuery(config.AllGroupsQuery, field.NewPath("groupsQuery"), isGroupDNQuery))
if len(config.GroupUIDAttribute) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupUIDAttribute"), ""))
}
if len(config.GroupNameAttributes) == 0 {
validationResults.AddErrors(field.Required(field.NewPath("groupNameAttributes"), ""))
}
return validationResults
}
func ValidateLDAPQuery(query api.LDAPQuery, fldPath *field.Path) ValidationResults {
return validateLDAPQuery(query, fldPath, false)
}
func validateLDAPQuery(query api.LDAPQuery, fldPath *field.Path, isDNOnly bool) ValidationResults {
validationResults := ValidationResults{}
if _, err := ldap.ParseDN(query.BaseDN); err != nil {
validationResults.AddErrors(field.Invalid(fldPath.Child("baseDN"), query.BaseDN,
fmt.Sprintf("invalid base DN for search: %v", err)))
}
if len(query.Scope) > 0 {
if _, err := ldaputil.DetermineLDAPScope(query.Scope); err != nil {
validationResults.AddErrors(field.Invalid(fldPath.Child("scope"), query.Scope,
"invalid LDAP search scope"))
}
}
if len(query.DerefAliases) > 0 {
if _, err := ldaputil.DetermineDerefAliasesBehavior(query.DerefAliases); err != nil {
validationResults.AddErrors(field.Invalid(fldPath.Child("derefAliases"),
query.DerefAliases, "LDAP alias dereferencing instruction invalid"))
}
}
if query.TimeLimit < 0 {
validationResults.AddErrors(field.Invalid(fldPath.Child("timeout"), query.TimeLimit,
"timeout must be equal to or greater than zero"))
}
if isDNOnly {
if len(query.Filter) != 0 {
validationResults.AddErrors(field.Invalid(fldPath.Child("filter"), query.Filter, `cannot specify a filter when using "dn" as the UID attribute`))
}
return validationResults
}
if _, err := ldap.CompileFilter(query.Filter); err != nil {
validationResults.AddErrors(field.Invalid(fldPath.Child("filter"), query.Filter,
fmt.Sprintf("invalid query filter: %v", err)))
}
return validationResults
}