Skip to content

Commit fa609da

Browse files
feat: add custom CA file support for TLS cert verification
1 parent eecb8db commit fa609da

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

common/api.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package common
22

33
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"os"
7+
48
"github.com/coroot/coroot-node-agent/flags"
9+
"k8s.io/klog/v2"
510
)
611

712
func AuthHeaders() map[string]string {
@@ -11,3 +16,24 @@ func AuthHeaders() map[string]string {
1116
}
1217
return res
1318
}
19+
20+
func TlsConfig() *tls.Config {
21+
cfg := &tls.Config{InsecureSkipVerify: *flags.InsecureSkipVerify}
22+
if *flags.CAFile != "" {
23+
ca, err := os.ReadFile(*flags.CAFile)
24+
if err != nil {
25+
klog.Fatalln(err)
26+
return cfg
27+
}
28+
pool, err := x509.SystemCertPool()
29+
if err != nil {
30+
klog.Warningln("failed to load system cert pool, starting with empty pool:", err)
31+
pool = x509.NewCertPool()
32+
}
33+
if !pool.AppendCertsFromPEM(ca) {
34+
klog.Fatalf("failed to parse CA from %s", *flags.CAFile)
35+
}
36+
cfg.RootCAs = pool
37+
}
38+
return cfg
39+
}

flags/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var (
5151
LogsEndpoint = kingpin.Flag("logs-endpoint", "The URL of the endpoint to send logs to").Envar("LOGS_ENDPOINT").URL()
5252
ProfilesEndpoint = kingpin.Flag("profiles-endpoint", "The URL of the endpoint to send profiles to").Envar("PROFILES_ENDPOINT").URL()
5353
InsecureSkipVerify = kingpin.Flag("insecure-skip-verify", "whether to skip verifying the certificate or not").Envar("INSECURE_SKIP_VERIFY").Default("false").Bool()
54+
CAFile = kingpin.Flag("ca-file", "Path to the custom CA certificate file").Envar("CA_FILE").String()
5455

5556
ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
5657
WalDir = kingpin.Flag("wal-dir", "Path to where the agent stores data (e.g. the metrics Write-Ahead Log)").Default("/tmp/coroot-node-agent").Envar("WAL_DIR").String()

logs/otel.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package logs
22

33
import (
44
"context"
5-
"crypto/tls"
65
"time"
76

87
otel "github.com/agoda-com/opentelemetry-logs-go"
@@ -37,7 +36,7 @@ func Init(machineId, hostname, version string) {
3736
otlplogshttp.WithEndpoint(endpointUrl.Host),
3837
otlplogshttp.WithURLPath(path),
3938
otlplogshttp.WithHeaders(common.AuthHeaders()),
40-
otlplogshttp.WithTLSClientConfig(&tls.Config{InsecureSkipVerify: *flags.InsecureSkipVerify}),
39+
otlplogshttp.WithTLSClientConfig(common.TlsConfig()),
4140
}
4241
if endpointUrl.Scheme != "https" {
4342
opts = append(opts, otlplogshttp.WithInsecure())

profiling/profiling.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package profiling
22

33
import (
44
"bytes"
5-
"crypto/tls"
65
"fmt"
76
"io"
87
"net/http"
@@ -39,7 +38,7 @@ var (
3938
httpClient = http.Client{
4039
Timeout: UploadTimeout,
4140
Transport: &http.Transport{
42-
TLSClientConfig: &tls.Config{InsecureSkipVerify: *flags.InsecureSkipVerify},
41+
TLSClientConfig: common.TlsConfig(),
4342
},
4443
}
4544
endpointUrl *url.URL

prom/remote_writer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package prom
22

33
import (
44
"crypto/md5"
5-
"crypto/tls"
65
"encoding/hex"
76
"errors"
87
"fmt"
@@ -67,7 +66,7 @@ func StartAgent(reg *prometheus.Registry, machineId, systemUuid string) error {
6766
httpClient: http.Client{
6867
Timeout: RemoteWriteTimeout,
6968
Transport: &http.Transport{
70-
TLSClientConfig: &tls.Config{InsecureSkipVerify: *flags.InsecureSkipVerify},
69+
TLSClientConfig: common.TlsConfig(),
7170
},
7271
},
7372
spoolDir: path.Join(*flags.WalDir, "spool"),

tracing/tracing.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tracing
22

33
import (
44
"context"
5-
"crypto/tls"
65
"fmt"
76
"math/rand"
87
"time"
@@ -57,7 +56,7 @@ func Init(machineId, hostname, version string) {
5756
otlptracehttp.WithEndpoint(endpointUrl.Host),
5857
otlptracehttp.WithURLPath(path),
5958
otlptracehttp.WithHeaders(common.AuthHeaders()),
60-
otlptracehttp.WithTLSClientConfig(&tls.Config{InsecureSkipVerify: *flags.InsecureSkipVerify}),
59+
otlptracehttp.WithTLSClientConfig(common.TlsConfig()),
6160
}
6261
if endpointUrl.Scheme != "https" {
6362
opts = append(opts, otlptracehttp.WithInsecure())

0 commit comments

Comments
 (0)