Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 38 additions & 20 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/tls"
"fmt"
"sync"

"gopkg.in/ldap.v2"
)
Expand All @@ -16,29 +17,46 @@ type Config struct {
BindPassword string
}

// The lazily initialized response from the first initiateAndBind attempt.
var initiateAndBindResponse *InitiateAndBindResponse
var once sync.Once

type InitiateAndBindResponse struct {
Connection *ldap.Conn
Err error
}

func (c *Config) initiateAndBind() (*ldap.Conn, error) {
// TODO: should we handle UDP ?
connection, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", c.LDAPHost, c.LDAPPort))
if err != nil {
return nil, err
}

// handle TLS
if c.UseTLS {
//TODO: Finish the TLS integration
err = connection.StartTLS(&tls.Config{InsecureSkipVerify: true})
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 {
return nil, err
initiateAndBindResponse.Err = err
return
}
}

// bind to current connection
err = connection.Bind(c.BindUser, c.BindPassword)
if err != nil {
connection.Close()
return nil, err
}
// 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
return connection, nil
// return the LDAP connection
initiateAndBindResponse.Connection = connection
})
return initiateAndBindResponse.Connection, initiateAndBindResponse.Err
}
11 changes: 2 additions & 9 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,11 @@ func Provider() terraform.ResourceProvider {
}

func configureProvider(d *schema.ResourceData) (interface{}, error) {
config := Config{
return &Config{
LDAPHost: d.Get("ldap_host").(string),
LDAPPort: d.Get("ldap_port").(int),
UseTLS: d.Get("use_tls").(bool),
BindUser: d.Get("bind_user").(string),
BindPassword: d.Get("bind_password").(string),
}

connection, err := config.initiateAndBind()
if err != nil {
return nil, err
}

return connection, nil
}, nil
}
39 changes: 30 additions & 9 deletions resource_ldap_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func resourceLDAPObjectImport(d *schema.ResourceData, meta interface{}) (importe
}

func resourceLDAPObjectExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
conn := meta.(*ldap.Conn)
config := meta.(*Config)
conn, err := config.initiateAndBind()
if err != nil {
return false, err
}
dn := d.Get("dn").(string)

log.Printf("[DEBUG] ldap_object::exists - checking if %q exists", dn)
Expand All @@ -135,7 +139,8 @@ func resourceLDAPObjectExists(d *schema.ResourceData, meta interface{}) (b bool,
nil,
)

_, err := conn.Search(request)
var _ *ldap.SearchResult
_, err = conn.Search(request)
if err != nil {
if err, ok := err.(*ldap.Error); ok {
if err.ResultCode == 32 { // no such object
Expand All @@ -152,7 +157,11 @@ func resourceLDAPObjectExists(d *schema.ResourceData, meta interface{}) (b bool,
}

func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
config := meta.(*Config)
client, err := config.initiateAndBind()
if err != nil {
return err
}
dn := d.Get("dn").(string)

log.Printf("[DEBUG] ldap_object::create - creating a new object under %q", dn)
Expand Down Expand Up @@ -193,7 +202,7 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
}
}

err := client.Add(request)
err = client.Add(request)
if err != nil {
return err
}
Expand All @@ -209,7 +218,11 @@ func resourceLDAPObjectRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceLDAPObjectUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
config := meta.(*Config)
client, err := config.initiateAndBind()
if err != nil {
return err
}

log.Printf("[DEBUG] ldap_object::update - performing update on %q", d.Id())

Expand Down Expand Up @@ -255,7 +268,7 @@ func resourceLDAPObjectUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

err := client.Modify(request)
err = client.Modify(request)
if err != nil {
log.Printf("[ERROR] ldap_object::update - error modifying LDAP object %q with values %v", d.Id(), err)
return err
Expand All @@ -264,14 +277,18 @@ func resourceLDAPObjectUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceLDAPObjectDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
config := meta.(*Config)
client, err := config.initiateAndBind()
if err != nil {
return err
}
dn := d.Get("dn").(string)

log.Printf("[DEBUG] ldap_object::delete - removing %q", dn)

request := ldap.NewDelRequest(dn, nil)

err := client.Del(request)
err = client.Del(request)
if err != nil {
log.Printf("[ERROR] ldap_object::delete - error removing %q: %v", dn, err)
return err
Expand All @@ -281,7 +298,11 @@ func resourceLDAPObjectDelete(d *schema.ResourceData, meta interface{}) error {
}

func readLDAPObjectImpl(d *schema.ResourceData, meta interface{}, updateState bool) error {
client := meta.(*ldap.Conn)
config := meta.(*Config)
client, err := config.initiateAndBind()
if err != nil {
return err
}
dn := d.Get("dn").(string)

log.Printf("[DEBUG] ldap_object::read - looking for object %q", dn)
Expand Down