forked from openshift/oc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.go
309 lines (260 loc) · 11.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package ldap
import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"strings"
"unicode"
"unicode/utf8"
"github.com/openshift/library-go/pkg/certs"
"gopkg.in/ldap.v2"
"k8s.io/apimachinery/pkg/util/validation/field"
legacyconfigv1 "github.com/openshift/api/legacyconfig/v1"
"github.com/openshift/library-go/pkg/config/validation"
"github.com/openshift/library-go/pkg/security/ldaputil"
)
func GetStringSourceFileReferences(s *legacyconfigv1.StringSource) []*string {
if s == nil {
return nil
}
return []*string{
&s.File,
&s.KeyFile,
}
}
func ResolveStringValue(s legacyconfigv1.StringSource) (string, error) {
var value string
switch {
case len(s.Value) > 0:
value = s.Value
case len(s.Env) > 0:
value = os.Getenv(s.Env)
case len(s.File) > 0:
data, err := ioutil.ReadFile(s.File)
if err != nil {
return "", err
}
value = string(data)
default:
value = ""
}
if len(s.KeyFile) == 0 {
// value is cleartext, return
return value, nil
}
keyData, err := ioutil.ReadFile(s.KeyFile)
if err != nil {
return "", err
}
secretBlock, ok := certs.BlockFromBytes([]byte(value), certs.StringSourceEncryptedBlockType)
if !ok {
return "", fmt.Errorf("no valid PEM block of type %q found in data", certs.StringSourceEncryptedBlockType)
}
keyBlock, ok := certs.BlockFromBytes(keyData, certs.StringSourceKeyBlockType)
if !ok {
return "", fmt.Errorf("no valid PEM block of type %q found in key", certs.StringSourceKeyBlockType)
}
data, err := x509.DecryptPEMBlock(secretBlock, keyBlock.Bytes)
return string(data), err
}
func ValidateLDAPSyncConfig(config *legacyconfigv1.LDAPSyncConfig) validation.ValidationResults {
validationResults := validation.ValidationResults{}
validationResults.Append(ValidateStringSource(config.BindPassword, field.NewPath("bindPassword")))
bindPassword, _ := 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) validation.ValidationResults {
validationResults := validation.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(validation.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 *legacyconfigv1.RFC2307Config) validation.ValidationResults {
validationResults := validation.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 *legacyconfigv1.ActiveDirectoryConfig) validation.ValidationResults {
validationResults := validation.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 *legacyconfigv1.AugmentedActiveDirectoryConfig) validation.ValidationResults {
validationResults := validation.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 legacyconfigv1.LDAPQuery, fldPath *field.Path) validation.ValidationResults {
return validateLDAPQuery(query, fldPath, false)
}
func validateLDAPQuery(query legacyconfigv1.LDAPQuery, fldPath *field.Path, isDNOnly bool) validation.ValidationResults {
validationResults := validation.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
}
func ValidateStringSource(s legacyconfigv1.StringSource, fieldPath *field.Path) validation.ValidationResults {
validationResults := validation.ValidationResults{}
methods := 0
if len(s.Value) > 0 {
methods++
}
if len(s.File) > 0 {
methods++
fileErrors := validation.ValidateFile(s.File, fieldPath.Child("file"))
validationResults.AddErrors(fileErrors...)
// If the file was otherwise ok, and its value will be used verbatim, warn about trailing whitespace
if len(fileErrors) == 0 && len(s.KeyFile) == 0 {
if data, err := ioutil.ReadFile(s.File); err != nil {
validationResults.AddErrors(field.Invalid(fieldPath.Child("file"), s.File, fmt.Sprintf("could not read file: %v", err)))
} else if len(data) > 0 {
r, _ := utf8.DecodeLastRune(data)
if unicode.IsSpace(r) {
validationResults.AddWarnings(field.Invalid(fieldPath.Child("file"), s.File, "contains trailing whitespace which will be included in the value"))
}
}
}
}
if len(s.Env) > 0 {
methods++
}
if methods > 1 {
validationResults.AddErrors(field.Invalid(fieldPath, "", "only one of value, file, and env can be specified"))
}
if len(s.KeyFile) > 0 {
validationResults.AddErrors(validation.ValidateFile(s.KeyFile, fieldPath.Child("keyFile"))...)
}
return validationResults
}