-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.go
297 lines (248 loc) · 11.3 KB
/
config.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package ldap
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"strings"
"github.com/go-ldap/ldap/v3"
"github.com/hashicorp/go-secure-stdlib/tlsutil"
)
var derefAliasMap = map[string]int{
"never": ldap.NeverDerefAliases,
"finding": ldap.DerefFindingBaseObj,
"searching": ldap.DerefInSearching,
"always": ldap.DerefAlways,
}
func validateDerefAlias(deref string) (string, error) {
const op = "ldap.validateDerefAlias"
lowerDeref := strings.ToLower(deref)
_, found := derefAliasMap[lowerDeref]
switch {
case found:
return lowerDeref, nil
case deref == "":
return DefaultDerefAliases, nil
default:
return "", fmt.Errorf("%s: invalid dereference_aliases %q: %w", op, deref, ErrInvalidParameter)
}
}
const (
// DefaultTimeout is the timeout value used for both dialing and requests to
// the LDAP server
DefaultTimeout = 60
// DefaultURL for the ClientConfig.URLs
DefaultURL = "ldaps://127.0.0.1:686"
// DefaultUserAttr is the "username" attribute of the entry's DN and is
// typically either the cn in ActiveDirectory or uid in openLDAP (default:
// cn)
DefaultUserAttr = "cn"
// DefaultGroupFilter for the ClientConfig.GroupFilter
DefaultGroupFilter = `(|(memberUid={{.Username}})(member={{.UserDN}})(uniqueMember={{.UserDN}}))`
// DefaultGroupAttr for the ClientConfig.GroupAttr
DefaultGroupAttr = "cn"
// DefaultTLSMinVersion for the ClientConfig.TLSMinVersion
DefaultTLSMinVersion = "tls12"
// DefaultTLSMaxVersion for the ClientConfig.TLSMaxVersion
DefaultTLSMaxVersion = "tls13"
// DefaultOpenLDAPUserPasswordAttribute defines the attribute name for the
// openLDAP default password attribute which will always be excluded
DefaultOpenLDAPUserPasswordAttribute = "userPassword"
// DefaultADUserPasswordAttribute defines the attribute name for the
// AD default password attribute which will always be excluded
DefaultADUserPasswordAttribute = "unicodePwd"
// DefaultDerefAliases defines the default for dereferencing aliases
DefaultDerefAliases = "never"
)
type ClientConfig struct {
// URLs are the URLs to use when connecting to a directory (default:
// ldap://127.0.0.1). When multiple URLs are specified; they are tried
// in the order specified.
URLs []string `json:"urls"`
// UserDN is the base distinguished name to use when searching for users
// (eg: ou=People,dc=example,dc=org)
UserDN string `json:"userdn"`
// AnonymousGroupSearch specifies that an anonymous bind should be used when
// searching for groups (if true, the bind credentials will still be used
// for the initial connection test).
AnonymousGroupSearch bool `json:"anonymous_group_search"`
// AllowEmptyAnonymousGroupSearches: if true it removes the userDN from
// unauthenticated group searches (optional).
AllowEmptyAnonymousGroupSearch bool `json:"allow_empty_anonymous_group_search"`
// GroupDN is the distinguished name to use as base when searching for group
// membership (eg: ou=Groups,dc=example,dc=org)
GroupDN string `json:"groupdn"`
// GroupFilter is a Go template for querying the group membership of user
// (optional). The template can access the following context variables:
// UserDN, Username
//
// Example:
// (&(objectClass=group)(member:1.2.840.113556.1.4.1941:={{.UserDN}}))
// Default: (|(memberUid={{.Username}})(member={{.UserDN}})(uniqueMember={{.UserDN}}))`
GroupFilter string `json:"groupfilter"`
// GroupAttr is the attribute which identifies group members in entries
// returned from GroupFilter queries. Examples: for groupattr queries
// returning group objects, use: cn. For queries returning user objects,
// use: memberOf.
// Default: cn
GroupAttr string `json:"groupattr"`
// UPNDomain is the userPrincipalName domain, which enables a
// userPrincipalDomain login with [username]@UPNDomain (optional)
UPNDomain string `json:"upndomain"`
// UserFilter (optional) is a Go template used to construct a ldap user
// search filter. The template can access the following context variables:
// [UserAttr, Username]. The default userfilter is
// ({{.UserAttr}}={{.Username}}) or
// (userPrincipalName={{.Username}}@UPNDomain) if the upndomain parameter
// is set. The user search filter can be used to restrict what user can
// attempt to log in. For example, to limit login to users that are not
// contractors, you could write
// (&(objectClass=user)({{.UserAttr}}={{.Username}})(!(employeeType=Contractor)))
UserFilter string `json:"userfilter"`
// UserAttr is the "username" attribute of the entry's DN and is typically
// either the cn in ActiveDirectory or uid in openLDAP (default: cn)
UserAttr string `json:"userattr"`
// Certificates to use verify the identity of the directory service and is a
// set of PEM encoded x509 (optional)
Certificates []string `json:"certificates"`
// ClientTLSCert is the client certificate used with the ClientTLSKey to
// authenticate the client to the directory service. It must be PEM encoded
// x509 (optional)
ClientTLSCert string `json:"client_tls_cert"`
// ClientTLSKey is the client certificate key used with the ClientTLSCert to
// authenticate the client to the directory service. It must be a PEM
// encoded x509 (optional)
ClientTLSKey string `json:"client_tls_key"`
// InsecureTLS will skip the verification of the directory service's
// certificate when making a client connection (optional).
// Warning: this is insecure
InsecureTLS bool `json:"insecure_tls"`
// StartTLS will issue the StartTLS command after establishing an initial
// non-TLS connection (optional)
StartTLS bool `json:"starttls"`
// BindDN is the distinguished name used when the client binds
// (authenticates) to a directory service
BindDN string `json:"binddn"`
// BindPassword is the password used with the BindDN when the client binds
// (authenticates) to a directory service (optional)
BindPassword string `json:"bindpass"`
// AllowEmptyPasswordBinds: if true it allows binds even if the user's
// password is empty (zero length) (optional).
AllowEmptyPasswordBinds bool `json:"allow_empty_passwd_bind"`
// DiscoverDN: if true, it will use an anonymous bind with a search
// to discover the bind DN of a user (optional)
DiscoverDN bool `json:"discoverdn"`
// TLSMinVersion version to use. Accepted values are
// 'tls10', 'tls11', 'tls12' or 'tls13'. Defaults to 'tls12'
TLSMinVersion string `json:"tls_min_version"`
// TLSMaxVersion version to use. Accepted values are 'tls10', 'tls11',
// 'tls12' or 'tls13'. Defaults to 'tls12'
TLSMaxVersion string `json:"tls_max_version"`
// UseTokenGroups: if true, use the Active Directory tokenGroups constructed
// attribute of the user to find the group memberships. This will find all
// security groups including nested ones.",
UseTokenGroups bool `json:"use_token_groups"`
// RequestTimeout in seconds is used when dialing to establish the
// connection and when making requests against the server via a connection
// before returning back an error. If not set, then the DefaultTimeout is
// used.
RequestTimeout int `json:"request_timeout"`
// IncludeUserAttributes optionally specifies that the authenticating user's
// DN and attributes be included an authentication AuthResult.
//
// Note: the default password attribute for both openLDAP (userPassword) and
// AD (unicodePwd) will always be excluded.
IncludeUserAttributes bool
// ExcludedUserAttributes optionally defines a set of user attributes to be
// excluded when an authenticating user's attributes are included in an
// AuthResult (see: Config.IncludeUserAttributes or the WithUserAttributes()
// option).
//
// Note: the default password attribute for both openLDAP (userPassword) and
// AD (unicodePwd) will always be excluded.
ExcludedUserAttributes []string
// LowerUserAttributeKeys optionally specifies that the authenticating user's
// DN and attributes be included in AuthResult use lowercase key names rather
// than the default camel case.
LowerUserAttributeKeys bool
// IncludeUserGroups optionally specifies that the authenticating user's
// group membership be included an authentication AuthResult.
IncludeUserGroups bool
// MaximumPageSize optionally specifies a maximum ldap search result size to
// use when retrieving the authenticated user's group memberships. This can
// be used to avoid reaching the LDAP server's max result size.
MaximumPageSize int `json:"max_page_size"`
// DerefAliases will control how aliases are dereferenced when
// performing the search. Possible values are: never, finding, searching,
// and always. If unset, a default of "never" is used. When set to
// "finding", it will only dereference aliases during name resolution of the
// base. When set to "searching", it will dereference aliases after name
// resolution.
DerefAliases string `json:"dereference_aliases"`
// DeprecatedVaultPre111GroupCNBehavior: if true, group searching reverts to
// the pre 1.1.1 Vault behavior.
// see: https://www.vaultproject.io/docs/upgrading/upgrade-to-1.1.1
DeprecatedVaultPre111GroupCNBehavior *bool `json:"use_pre111_group_cn_behavior"`
// EnableSamaccountnameLogin enables login with sAMAccountName in addition to UserPrincipalName when upndomain is set.
EnableSamaccountnameLogin bool `json:"enable_samaccountname_login"`
}
func (c *ClientConfig) clone() (*ClientConfig, error) {
clone := *c
return &clone, nil
}
func (c *ClientConfig) validate() error {
const op = "ldap.(ClientConfig).validate"
if len(c.URLs) == 0 {
return fmt.Errorf("%s: at least one url must be provided: %w", op, ErrInvalidParameter)
}
tlsMinVersion, ok := tlsutil.TLSLookup[c.TLSMinVersion]
if !ok {
return fmt.Errorf("%s: invalid 'tls_min_version' in config: %w", op, ErrInvalidParameter)
}
tlsMaxVersion, ok := tlsutil.TLSLookup[c.TLSMaxVersion]
if !ok {
return fmt.Errorf("%s: invalid 'tls_max_version' in config: %w", op, ErrInvalidParameter)
}
if tlsMaxVersion < tlsMinVersion {
return fmt.Errorf("%s: 'tls_max_version' must be greater than or equal to 'tls_min_version': %w", op, ErrInvalidParameter)
}
if c.Certificates != nil {
for _, cert := range c.Certificates {
if err := validateCertificate([]byte(cert)); err != nil {
return fmt.Errorf("%s: failed to parse server tls cert: %w", op, err)
}
}
}
if (c.ClientTLSCert != "" && c.ClientTLSKey == "") ||
(c.ClientTLSCert == "" && c.ClientTLSKey != "") {
return fmt.Errorf("%s: both client_tls_cert and client_tls_key must be set in configuration: %w", op, ErrInvalidParameter)
}
if c.ClientTLSCert != "" && c.ClientTLSKey != "" {
if _, err := tls.X509KeyPair([]byte(c.ClientTLSCert), []byte(c.ClientTLSKey)); err != nil {
return fmt.Errorf("%s: failed to parse client X509 key pair: %w", op, err)
}
}
var err error
c.DerefAliases, err = validateDerefAlias(c.DerefAliases)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
func validateCertificate(pemBlock []byte) error {
const op = "ldap.validateCertificate"
if pemBlock == nil {
return fmt.Errorf("%s: missing certificate pem block: %w", op, ErrInvalidParameter)
}
block, _ := pem.Decode([]byte(pemBlock))
if block == nil || block.Type != "CERTIFICATE" {
return fmt.Errorf("%s: failed to decode PEM block in the certificate: %w", op, ErrInvalidParameter)
}
_, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("%s: failed to parse certificate %w", op, err)
}
return nil
}