Skip to content

Commit

Permalink
added ca file and option skipverify
Browse files Browse the repository at this point in the history
will close #14
  • Loading branch information
kbudde committed Sep 12, 2016
1 parent f5e1154 commit eab7662
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Settings:
* PUBLISH_PORT: "9090",
* OUTPUT_FORMAT: "TTY", //change to JSON if needed
* LOG_LEVEL: "info", // can be "debug", "info", "warning", "error", "fatal", or "panic"
* CAFILE: "ca.pem", // default is ca.pem. If file does not exist it will be ignored.
* SKIPVERIFY: false or 0, true or 1 // will skip hostname/certificate check at all

Example

Expand Down
31 changes: 21 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import (
var (
config rabbitExporterConfig
defaultConfig = rabbitExporterConfig{
RabbitURL: "http://localhost:15672",
RabbitUsername: "guest",
RabbitPassword: "guest",
PublishPort: "9090",
OutputFormat: "TTY", //JSON
RabbitURL: "http://localhost:15672",
RabbitUsername: "guest",
RabbitPassword: "guest",
PublishPort: "9090",
OutputFormat: "TTY", //JSON
CAFile: "ca.pem",
InsecureSkipVerify: false,
}
)

type rabbitExporterConfig struct {
RabbitURL string
RabbitUsername string
RabbitPassword string
PublishPort string
OutputFormat string
RabbitURL string
RabbitUsername string
RabbitPassword string
PublishPort string
OutputFormat string
CAFile string
InsecureSkipVerify bool
}

func initConfig() {
Expand Down Expand Up @@ -52,4 +56,11 @@ func initConfig() {
if output := os.Getenv("OUTPUT_FORMAT"); output != "" {
config.OutputFormat = output
}

if cafile := os.Getenv("CAFILE"); cafile != "" {
config.CAFile = cafile
}
if insecureSkipVerify := os.Getenv("SKIPVERIFY"); insecureSkipVerify == "true" || insecureSkipVerify == "1" {
config.InsecureSkipVerify = true
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func initLogger() {
func main() {
initConfig()
initLogger()
InitClient()
exporter := newExporter()
prometheus.MustRegister(exporter)

Expand Down
27 changes: 27 additions & 0 deletions rabbitClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"io/ioutil"
Expand All @@ -13,6 +15,31 @@ import (

var client = &http.Client{Timeout: 10 * time.Second}

func InitClient() {
roots := x509.NewCertPool()

if data, err := ioutil.ReadFile(config.CAFile); err == nil {
if !roots.AppendCertsFromPEM(data) {
log.WithField("filename", config.CAFile).Error("Adding certificate to rootCAs failed")
}
} else {
log.Info("Using default certificate pool")
}

tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.InsecureSkipVerify,
RootCAs: roots,
},
}

client = &http.Client{
Transport: tr,
Timeout: 10 * time.Second,
}

}

func loadMetrics(config rabbitExporterConfig, endpoint string) (*json.Decoder, error) {
req, err := http.NewRequest("GET", config.RabbitURL+"/api/"+endpoint, nil)
req.SetBasicAuth(config.RabbitUsername, config.RabbitPassword)
Expand Down

0 comments on commit eab7662

Please sign in to comment.