forked from Pryz/terraform-provider-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (53 loc) · 1.39 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
package main
import (
"crypto/tls"
"fmt"
"sync"
"gopkg.in/ldap.v2"
)
// Config is the set of parameters needed to configure the LDAP provider.
type Config struct {
LDAPHost string
LDAPPort int
UseTLS bool
BindUser string
BindPassword string
}
// The lazily initialized response from the first initiateAndBind attempt.
var initiateAndBindResponse *InitiateAndBindResponse
var once sync.Once
// InitiateAndBindResponse struct
type InitiateAndBindResponse struct {
Connection *ldap.Conn
Err error
}
func (c *Config) initiateAndBind() (*ldap.Conn, error) {
once.Do(func() {
initiateAndBindResponse = &InitiateAndBindResponse{}
// TODO: should we handle UDP ?
connection, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", c.LDAPHost, c.LDAPPort))
if err != nil {
initiateAndBindResponse.Err = err
return
}
// handle TLS
if c.UseTLS {
//TODO: Finish the TLS integration
err = connection.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
connection.Close()
initiateAndBindResponse.Err = err
return
}
}
// bind to current connection
err = connection.Bind(c.BindUser, c.BindPassword)
if err != nil {
connection.Close()
initiateAndBindResponse.Err = err
}
// return the LDAP connection
initiateAndBindResponse.Connection = connection
})
return initiateAndBindResponse.Connection, initiateAndBindResponse.Err
}