Skip to content

Commit

Permalink
Add support of StartTLS and bind, and converted LDAP params to config…
Browse files Browse the repository at this point in the history
… file for security.
  • Loading branch information
kyle-williams-1 committed Apr 28, 2018
1 parent 65cc724 commit 0b296ad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.dll
*.so
*.dylib
config.yaml

# Test binary, build with `go test -c`
*.test
Expand Down
10 changes: 10 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

ldap:
host: 'localhost'
port: '636'
basedn: 'dc=example,dc=org'
starttls: false
bind: false
bindcn: ''
bindpass: ''
67 changes: 53 additions & 14 deletions syncrepl_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"crypto/tls"
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/ldap.v2"
"log"
"net/http"
"strings"
"time"

"github.com/jinzhu/configor"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/ldap.v2"
)

var (
Expand All @@ -34,6 +36,18 @@ var (
})
)

var Config = struct {
Ldap struct {
Host string `default:"localhost"`
Port string `default:"636"`
Basedn string `default:"dc=example,dc=org"`
StartTLS bool `default:"false"`
Bind bool `default:"false"`
Bindcn string `default:""`
Bindpass string `default:""`
}
}{}

func ymdToUnix(contextCSN string) (timestamp int64, label string) {
// This is a totally crude approach to set a well known base time to parse another date later
format := "20060102150405"
Expand All @@ -44,21 +58,41 @@ func ymdToUnix(contextCSN string) (timestamp int64, label string) {
}

// Actually collect values from ldap
func csnWorker(ldapHost, baseDN string) {
func csnWorker() {
conf := &tls.Config{
InsecureSkipVerify: true,
}

l, err := ldap.DialTLS("tcp", ldapHost, conf)
var l *ldap.Conn
var err error

if Config.Ldap.StartTLS {
// Connect to host
l, err = ldap.Dial("tcp", Config.Ldap.Host + ":" + Config.Ldap.Port)
if err != nil {
log.Fatal(err)
}
defer l.Close()

// Reconnect with TLS
err = l.StartTLS(conf)
} else {
l, err = ldap.DialTLS("tcp", Config.Ldap.Host + ":" + Config.Ldap.Port, conf)
}

// Bind
if Config.Ldap.Bind {
err = l.Bind(Config.Ldap.Bindcn, Config.Ldap.Bindpass)
}

if err != nil {
openldapUp.Set(0)
log.Println(err)
log.Fatal(err)
} else {
defer l.Close()

searchRequest := ldap.NewSearchRequest(
baseDN, // The base dn to search
Config.Ldap.Basedn, // The base dn to search
ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false,
"(objectClass=*)", // The filter to apply
[]string{"contextCSN"}, // A list attributes to retrieve
Expand All @@ -79,7 +113,7 @@ func csnWorker(ldapHost, baseDN string) {
}
}
searchRequest = ldap.NewSearchRequest(
baseDN, // The base dn to search
Config.Ldap.Basedn, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(objectClass=*)", // The filter to apply
[]string{"dn"}, // A list attributes to retrieve
Expand All @@ -98,9 +132,9 @@ func csnWorker(ldapHost, baseDN string) {
}
}

func ldapWorker(ldapHost, baseDN string) {
func ldapWorker() {
for {
csnWorker(ldapHost, baseDN)
csnWorker()
time.Sleep(60 * time.Second)
}
}
Expand All @@ -116,12 +150,17 @@ func main() {
var (
addr = flag.String("telemetry.addr", ":9328", "host:port for syncrepl exporter")
metricsPath = flag.String("telemetry.path", "/metrics", "URL path for surfacing collected metrics")
ldapHost = flag.String("ldap.host", "localhost:636", "hostname:port of the ldap server")
baseDN = flag.String("base.dn", "", "'dc=example,dc=org' the base DN of the directory")
configFile = flag.String("config.file", "config.yaml", "bind cn and password")
)

flag.Parse()
log.Printf(*addr, *metricsPath, *baseDN, *ldapHost)
go ldapWorker(*ldapHost, *baseDN)
log.Printf(*addr, *metricsPath, *configFile)

configor.Load(&Config, *configFile)

log.Printf("config: %#v", Config)

go ldapWorker()

http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 0b296ad

Please sign in to comment.