Skip to content

Commit

Permalink
Allow to add trusted certificates for LDAP
Browse files Browse the repository at this point in the history
This add a new configparameter "cacert" to allow to add trusted CAs
and Server Certificates for the LDAP connections. This allows us to
avoid using "insecure" when running against self-signed certificates.
(As e.g. issued for glauth by default)
  • Loading branch information
rhafer committed Sep 13, 2021
1 parent 7574c62 commit b07fb10
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/utils/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@ package utils

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"

"github.com/go-ldap/ldap/v3"
"github.com/pkg/errors"
)

type LDAPConn struct {
Hostname string `mapstructure:"hostname"`
Port int `mapstructure:"port"`
Insecure bool `mapstructure:"insecure"`
CACert string `mapstructure:"cacert"`
}

func GetLDAPConnection(c *LDAPConn) (*ldap.Conn, error) {
return ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", c.Hostname, c.Port), &tls.Config{InsecureSkipVerify: c.Insecure})
tlsconfig := &tls.Config{InsecureSkipVerify: c.Insecure}

if !c.Insecure && c.CACert != "" {
if pemBytes, err := ioutil.ReadFile(c.CACert); err == nil {
rpool, _ := x509.SystemCertPool()
rpool.AppendCertsFromPEM(pemBytes)
tlsconfig.RootCAs = rpool
} else {
return nil, errors.Wrapf(err, "Error reading LDAP CA Cert '%s.'", c.CACert)
}
}
return ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", c.Hostname, c.Port), tlsconfig)
}

0 comments on commit b07fb10

Please sign in to comment.