Skip to content

Commit 62e7078

Browse files
committed
TLS config when connecting to Alertmanager
1 parent 0f7853e commit 62e7078

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/bastjan/alerts_exporter
33
go 1.21.3
44

55
require (
6+
github.com/go-openapi/runtime v0.26.0
67
github.com/prometheus/alertmanager v0.26.0
78
github.com/prometheus/client_golang v1.17.0
89
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
@@ -19,7 +20,6 @@ require (
1920
github.com/go-openapi/jsonpointer v0.19.5 // indirect
2021
github.com/go-openapi/jsonreference v0.20.0 // indirect
2122
github.com/go-openapi/loads v0.21.2 // indirect
22-
github.com/go-openapi/runtime v0.26.0 // indirect
2323
github.com/go-openapi/spec v0.20.8 // indirect
2424
github.com/go-openapi/strfmt v0.21.7 // indirect
2525
github.com/go-openapi/swag v0.22.3 // indirect

main.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"net/http"
88

99
alertscollector "github.com/bastjan/alerts_exporter/internal/alerts_collector"
10-
"github.com/prometheus/alertmanager/api/v2/client"
10+
openapiclient "github.com/go-openapi/runtime/client"
11+
alermanagerclient "github.com/prometheus/alertmanager/api/v2/client"
1112
"github.com/prometheus/client_golang/prometheus"
1213
"github.com/prometheus/client_golang/prometheus/promhttp"
1314
)
@@ -16,9 +17,20 @@ var host string
1617
var withInhibited, withSilenced, withUnprocessed, withActive bool
1718
var filters stringSliceFlag
1819

20+
var tlsCert, tlsCertKey, tlsCaCert, tlsServerName string
21+
var tlsInsecure bool
22+
var useTLS bool
23+
1924
func main() {
2025
flag.StringVar(&host, "host", "localhost:9093", "The host of the Alertmanager")
2126

27+
flag.BoolVar(&useTLS, "tls", false, "Use TLS when connecting to Alertmanager")
28+
flag.StringVar(&tlsCert, "tls-cert", "", "Path to client certificate for TLS authentication")
29+
flag.StringVar(&tlsCertKey, "tls-cert-key", "", "Path to client certificate key for TLS authentication")
30+
flag.StringVar(&tlsCaCert, "tls-ca-cert", "", "Path to CA certificate. System certificates are used if not provided.")
31+
flag.StringVar(&tlsServerName, "tls-server-name", "", "Server name to verify the hostname on the returned certificates. It must be a substring of either the Common Name or a Subject Alternative Name in the certificate. If empty, the hostname given in the address parameter is used.")
32+
flag.BoolVar(&tlsInsecure, "insecure", false, "Disable TLS host verification")
33+
2234
flag.BoolVar(&withActive, "with-active", true, "Query for active alerts")
2335
flag.BoolVar(&withInhibited, "with-inhibited", true, "Query for inhibited alerts")
2436
flag.BoolVar(&withSilenced, "with-silenced", true, "Query for silenced alerts")
@@ -27,7 +39,27 @@ func main() {
2739

2840
flag.Parse()
2941

30-
ac := client.NewHTTPClientWithConfig(nil, client.DefaultTransportConfig().WithHost(host))
42+
opts := openapiclient.TLSClientOptions{
43+
Certificate: tlsCert,
44+
Key: tlsCertKey,
45+
CA: tlsCaCert,
46+
ServerName: tlsServerName,
47+
}
48+
if tlsInsecure {
49+
opts.InsecureSkipVerify = true
50+
opts.ServerName = ""
51+
}
52+
var schemes []string
53+
if useTLS {
54+
schemes = []string{"https"}
55+
}
56+
57+
hc, err := openapiclient.TLSClient(opts)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
62+
ac := alermanagerclient.New(openapiclient.NewWithClient(host, alermanagerclient.DefaultBasePath, schemes, hc), nil)
3163

3264
reg := prometheus.NewRegistry()
3365

0 commit comments

Comments
 (0)