Skip to content

Commit

Permalink
Improve error handling in TLS setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hikhvar committed Mar 12, 2021
1 parent 6c7f7f7 commit bad1940
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"encoding/json"
"flag"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io/ioutil"
"net/http"
"os"
"time"

"github.com/eclipse/paho.mqtt.golang"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/hikhvar/mqtt2prometheus/pkg/config"
"github.com/hikhvar/mqtt2prometheus/pkg/metrics"
"github.com/hikhvar/mqtt2prometheus/pkg/mqttclient"
Expand Down Expand Up @@ -75,14 +76,14 @@ func main() {
mqttClientOptions.SetUsername(cfg.MQTT.User)
mqttClientOptions.SetPassword(cfg.MQTT.Password)

if cfg.MQTT.ClientID != "" {
if cfg.MQTT.ClientID != "" {
mqttClientOptions.SetClientID(cfg.MQTT.ClientID)
} else {
mqttClientOptions.SetClientID(mustMQTTClientID())
}

if cfg.MQTT.ClientCert != "" || cfg.MQTT.ClientKey != "" {
tlsconfig, err := newTlsConfig(cfg)
tlsconfig, err := newTLSConfig(cfg)
if err != nil {
logger.Fatal("Invalid tls certificate settings", zap.Error(err))
}
Expand Down Expand Up @@ -195,29 +196,29 @@ func setupExtractor(cfg config.Config) (metrics.Extractor, error) {
return nil, fmt.Errorf("no extractor configured")
}

func newTlsConfig(cfg config.Config) (*tls.Config, error) {
func newTLSConfig(cfg config.Config) (*tls.Config, error) {
certpool := x509.NewCertPool()
if cfg.MQTT.CACert != "" {
pemCerts, err := ioutil.ReadFile(cfg.MQTT.CACert)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load ca_cert file: %w", err)
}
certpool.AppendCertsFromPEM(pemCerts)
}

cert, err := tls.LoadX509KeyPair(cfg.MQTT.ClientCert, cfg.MQTT.ClientKey)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load client certificate: %w", err)
}

cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
panic(fmt.Sprintf("could not parse certificate: %v", err))
return nil, fmt.Errorf("failed to parse client certificate: %w", err)
}

return &tls.Config{
RootCAs: certpool,
RootCAs: certpool,
InsecureSkipVerify: false,
Certificates: []tls.Certificate{cert},
Certificates: []tls.Certificate{cert},
}, nil
}

0 comments on commit bad1940

Please sign in to comment.