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
310
311
312
313
314
|
package authentication
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/go-ldap/ldap/v3"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/utils"
)
// LDAPClientFactory an interface describing factories that produce LDAPConnection implementations.
type LDAPClientFactory interface {
Initialize() (err error)
GetClient(opts ...LDAPClientFactoryOption) (client ldap.Client, err error)
ReleaseClient(client ldap.Client) (err error)
Close() (err error)
}
// NewStandardLDAPClientFactory create a concrete ldap connection factory.
func NewStandardLDAPClientFactory(config *schema.AuthenticationBackendLDAP, certs *x509.CertPool, dialer LDAPClientDialer) LDAPClientFactory {
if dialer == nil {
dialer = &LDAPClientDialerStandard{}
}
tlsc := utils.NewTLSConfig(config.TLS, certs)
opts := []ldap.DialOpt{
ldap.DialWithDialer(&net.Dialer{Timeout: config.Timeout}),
ldap.DialWithTLSConfig(tlsc),
}
return &StandardLDAPClientFactory{
config: config,
tls: tlsc,
opts: opts,
dialer: dialer,
}
}
// StandardLDAPClientFactory the production implementation of an ldap connection factory.
type StandardLDAPClientFactory struct {
config *schema.AuthenticationBackendLDAP
tls *tls.Config
opts []ldap.DialOpt
dialer LDAPClientDialer
}
func (f *StandardLDAPClientFactory) Initialize() (err error) {
return nil
}
func (f *StandardLDAPClientFactory) GetClient(opts ...LDAPClientFactoryOption) (client ldap.Client, err error) {
return getLDAPClient(f.config.Address.String(), f.config.User, f.config.Password, f.dialer, f.tls, f.config.StartTLS, f.opts, opts...)
}
func (f *StandardLDAPClientFactory) ReleaseClient(client ldap.Client) (err error) {
if err = client.Close(); err != nil {
return fmt.Errorf("error occurred closing LDAP client: %w", err)
}
return nil
}
func (f *StandardLDAPClientFactory) Close() (err error) {
return nil
}
// NewPooledLDAPClientFactory is a decorator for a LDAPClientFactory that performs pooling.
func NewPooledLDAPClientFactory(config *schema.AuthenticationBackendLDAP, certs *x509.CertPool, dialer LDAPClientDialer) (factory LDAPClientFactory) {
if dialer == nil {
dialer = &LDAPClientDialerStandard{}
}
tlsc := utils.NewTLSConfig(config.TLS, certs)
opts := []ldap.DialOpt{
ldap.DialWithDialer(&net.Dialer{Timeout: config.Timeout}),
ldap.DialWithTLSConfig(tlsc),
}
if config.Pooling.Count <= 0 {
config.Pooling.Count = 3
}
if config.Pooling.Retries <= 0 {
config.Pooling.Retries = 3
}
if config.Pooling.Timeout <= 0 {
config.Pooling.Timeout = time.Second
}
sleep := config.Pooling.Timeout / time.Duration(config.Pooling.Retries)
return &PooledLDAPClientFactory{
config: config,
tls: tlsc,
opts: opts,
dialer: dialer,
sleep: sleep,
}
}
// PooledLDAPClientFactory is a LDAPClientFactory that takes another LDAPClientFactory and pools the
// factory generated connections using a channel for thread safety.
type PooledLDAPClientFactory struct {
config *schema.AuthenticationBackendLDAP
tls *tls.Config
opts []ldap.DialOpt
dialer LDAPClientDialer
pool chan *LDAPClientPooled
mu sync.Mutex
sleep time.Duration
closing bool
}
func (f *PooledLDAPClientFactory) Initialize() (err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.pool != nil {
return nil
}
f.pool = make(chan *LDAPClientPooled, f.config.Pooling.Count)
var (
errs []error
client *LDAPClientPooled
)
for i := 0; i < f.config.Pooling.Count; i++ {
if client, err = f.new(); err != nil {
errs = append(errs, err)
continue
}
f.pool <- client
}
if len(errs) == f.config.Pooling.Count {
return fmt.Errorf("errors occurred initializing the client pool: no connections could be established")
}
return nil
}
// GetClient opens new client using the pool.
func (f *PooledLDAPClientFactory) GetClient(opts ...LDAPClientFactoryOption) (conn ldap.Client, err error) {
if len(opts) != 0 {
return getLDAPClient(f.config.Address.String(), f.config.User, f.config.Password, f.dialer, f.tls, f.config.StartTLS, f.opts, opts...)
}
return f.acquire(context.Background())
}
// The new function creates a pool based client. This function is not thread safe.
func (f *PooledLDAPClientFactory) new() (pooled *LDAPClientPooled, err error) {
var client ldap.Client
if client, err = getLDAPClient(f.config.Address.String(), f.config.User, f.config.Password, f.dialer, f.tls, f.config.StartTLS, f.opts); err != nil {
return nil, fmt.Errorf("error occurred establishing new client for the pool: %w", err)
}
return &LDAPClientPooled{Client: client}, nil
}
// ReleaseClient returns a client using the pool or closes it.
func (f *PooledLDAPClientFactory) ReleaseClient(client ldap.Client) (err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.closing {
return client.Close()
}
if pool, ok := client.(*LDAPClientPooled); !ok || cap(f.pool) == len(f.pool) {
// Prevent extra or non-pool connections from being returned into the pool.
return client.Close()
} else {
f.pool <- pool
}
return nil
}
func (f *PooledLDAPClientFactory) acquire(ctx context.Context) (client *LDAPClientPooled, err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.closing {
return nil, fmt.Errorf("error acquiring client: the pool is closed")
}
if cap(f.pool) != f.config.Pooling.Count {
if err = f.Initialize(); err != nil {
return nil, err
}
}
ctx, cancel := context.WithTimeout(ctx, f.config.Pooling.Timeout)
defer cancel()
select {
case <-ctx.Done():
return nil, ctx.Err()
case client = <-f.pool:
if client.IsClosing() || client.Client == nil {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
if client, err = f.new(); err != nil {
time.Sleep(f.sleep)
continue
}
return client, nil
}
}
}
return client, nil
}
}
func (f *PooledLDAPClientFactory) Close() (err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.closing = true
close(f.pool)
var errs []error
for client := range f.pool {
if client.IsClosing() {
continue
}
if err = client.Close(); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("errors occurred closing the client pool: %w", errors.Join(errs...))
}
return nil
}
// LDAPClientPooled is a decorator for the ldap.Client which handles the pooling functionality. i.e. prevents the client
// from being closed and instead relinquishes the connection back to the pool.
type LDAPClientPooled struct {
ldap.Client
}
func getLDAPClient(address, username, password string, dialer LDAPClientDialer, tls *tls.Config, startTLS bool, dialerOpts []ldap.DialOpt, opts ...LDAPClientFactoryOption) (client ldap.Client, err error) {
config := &LDAPClientFactoryOptions{
Address: address,
Username: username,
Password: password,
}
for _, opt := range opts {
opt(config)
}
if client, err = dialer.DialURL(config.Address, dialerOpts...); err != nil {
return nil, fmt.Errorf("error occurred dialing address: %w", err)
}
if tls != nil && startTLS {
if err = client.StartTLS(tls); err != nil {
_ = client.Close()
return nil, fmt.Errorf("error occurred performing starttls: %w", err)
}
}
if config.Password == "" {
err = client.UnauthenticatedBind(config.Username)
} else {
err = client.Bind(config.Username, config.Password)
}
if err != nil {
_ = client.Close()
return nil, fmt.Errorf("error occurred performing bind: %w", err)
}
return client, nil
}
|