Skip to content

Commit

Permalink
Merge pull request #20 from coderigo/feature/tls
Browse files Browse the repository at this point in the history
Add support for TLS
  • Loading branch information
carlpett authored Jun 15, 2020
2 parents d6e9292 + 4abaaa6 commit 0e30f3a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
FROM alpine:3.10 AS certs
RUN apk update \
&& apk add ca-certificates

FROM golang:1.10 AS builder
WORKDIR /go/src/github.com/carlpett/zookeeper_exporter/
COPY . .
Expand All @@ -6,5 +10,6 @@ RUN make build
FROM scratch
EXPOSE 9141
USER 1000
ENTRYPOINT ["/zookeeper_exporter"]
COPY --from=builder /go/src/github.com/carlpett/zookeeper_exporter/zookeeper_exporter /zookeeper_exporter
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
ENTRYPOINT ["/zookeeper_exporter"]
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@ func init() {
}
logLevel = parsedLevel

if *enableTLS && (*certPath == "" || *certKeyPath == "") {
log.Fatal("-enable-tls requires -cert and -cert-key")
}

if *logJSON {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}

prometheus.MustRegister(version.NewCollector("zookeeper_exporter"))
}

var (
logLevel log.Level = log.InfoLevel
logJSON = flag.Bool("log-json", false, "Log output as JSON")
bindAddr = flag.String("bind-addr", ":9141", "bind address for the metrics server")
enableTLS = flag.Bool("enable-tls", false, "Connect to zookeeper using TLS. Requires -cert and -cert-key")
certPath = flag.String("cert", "", "path to certificate including any intermediaries")
certKeyPath = flag.String("cert-key", "", "path to certificate key")
metricsPath = flag.String("metrics-path", "/metrics", "path to metrics endpoint")
zookeeperAddr = flag.String("zookeeper", "localhost:2181", "host:port for zookeeper socket")
rawLevel = flag.String("log-level", "info", "log level")
Expand Down
19 changes: 18 additions & 1 deletion zookeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
"crypto/tls"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -190,7 +191,7 @@ const (
func sendZkCommand(fourLetterWord string) (string, bool) {
log.Debugf("Connecting to Zookeeper at %s", *zookeeperAddr)

conn, err := net.Dial("tcp", *zookeeperAddr)
conn, err := zkConnect()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Unable to open connection to Zookeeper")
return "", false
Expand Down Expand Up @@ -223,3 +224,19 @@ func sendZkCommand(fourLetterWord string) (string, bool) {

return buffer.String(), true
}

func zkConnect() (net.Conn, error) {
if *enableTLS {
log.Debugf("TLS certificate: %s key: %s", *certPath, *certKeyPath)
cert, err := tls.LoadX509KeyPair(*certPath, *certKeyPath)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Unable to read TLS cert or key")
return nil, err
}
return tls.Dial("tcp", *zookeeperAddr, &tls.Config{
Certificates: []tls.Certificate{cert},
})
}

return net.Dial("tcp", *zookeeperAddr)
}

0 comments on commit 0e30f3a

Please sign in to comment.