From 4de0afebe7261dfe2772d4c8f3be3491b39d3ac0 Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Tue, 31 Oct 2023 16:58:30 +0800 Subject: [PATCH] style: rename ssl_config to tls_config Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- config/config.go | 34 +++++++++++++++++----------------- config/example/just_probe.yaml | 14 ++++++++++++-- prober/mqtt.go | 4 ++-- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/config/config.go b/config/config.go index e3a54d1..005e2dd 100644 --- a/config/config.go +++ b/config/config.go @@ -25,17 +25,17 @@ type Metrics struct { } type Probe struct { - Target string `yaml:"target"` - Scheme string `yaml:"scheme,omitempty"` - ClientID string `yaml:"client_id,omitempty"` - Username string `yaml:"username,omitempty"` - Password string `yaml:"password,omitempty"` - Topic string `yaml:"topic,omitempty"` - QoS byte `yaml:"qos,omitempty"` - SSLConfig *SSLConfig `yaml:"ssl_config,omitempty"` + Target string `yaml:"target"` + Scheme string `yaml:"scheme,omitempty"` + ClientID string `yaml:"client_id,omitempty"` + Username string `yaml:"username,omitempty"` + Password string `yaml:"password,omitempty"` + Topic string `yaml:"topic,omitempty"` + QoS byte `yaml:"qos,omitempty"` + TLSClientConfig *TLSClientConfig `yaml:"tls_config,omitempty"` } -type SSLConfig struct { +type TLSClientConfig struct { // Server should be accessed without verifying the TLS certificate. For testing only. InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"` @@ -119,17 +119,17 @@ func (sc *SafeConfig) ReloadConfig(confFile string) (err error) { if probe.Target == "" { return fmt.Errorf("probes[%d].target is required", index) } - if probe.SSLConfig != nil { + if probe.TLSClientConfig != nil { if probe.Scheme == "" { probe.Scheme = "ssl" } - if probe.SSLConfig.CAData, err = dataFromSliceOrFile(probe.SSLConfig.CAData, probe.SSLConfig.CAFile); err != nil { + if probe.TLSClientConfig.CAData, err = dataFromSliceOrFile(probe.TLSClientConfig.CAData, probe.TLSClientConfig.CAFile); err != nil { return fmt.Errorf("probes[%d].ssl_config.ca_data: %s", index, err) } - if probe.SSLConfig.CertData, err = dataFromSliceOrFile(probe.SSLConfig.CertData, probe.SSLConfig.CertFile); err != nil { + if probe.TLSClientConfig.CertData, err = dataFromSliceOrFile(probe.TLSClientConfig.CertData, probe.TLSClientConfig.CertFile); err != nil { return fmt.Errorf("probes[%d].ssl_config.cert_data: %s", index, err) } - if probe.SSLConfig.KeyData, err = dataFromSliceOrFile(probe.SSLConfig.KeyData, probe.SSLConfig.KeyFile); err != nil { + if probe.TLSClientConfig.KeyData, err = dataFromSliceOrFile(probe.TLSClientConfig.KeyData, probe.TLSClientConfig.KeyFile); err != nil { return fmt.Errorf("probes[%d].ssl_config.key_data: %s", index, err) } } @@ -152,12 +152,12 @@ func (sc *SafeConfig) ReloadConfig(confFile string) (err error) { return nil } -func (sslConfig *SSLConfig) ToTLSConfig() *tls.Config { +func (conf *TLSClientConfig) ToTLSConfig() *tls.Config { certpool := x509.NewCertPool() - certpool.AppendCertsFromPEM(sslConfig.CAData) - clientKeyPair, _ := tls.X509KeyPair(sslConfig.CertData, sslConfig.KeyData) + certpool.AppendCertsFromPEM(conf.CAData) + clientKeyPair, _ := tls.X509KeyPair(conf.CertData, conf.KeyData) return &tls.Config{ - InsecureSkipVerify: sslConfig.InsecureSkipVerify, + InsecureSkipVerify: conf.InsecureSkipVerify, RootCAs: certpool, Certificates: []tls.Certificate{clientKeyPair}, ClientAuth: tls.NoClientCert, diff --git a/config/example/just_probe.yaml b/config/example/just_probe.yaml index 9cdf06d..c2ed6d2 100644 --- a/config/example/just_probe.yaml +++ b/config/example/just_probe.yaml @@ -1,8 +1,18 @@ probes: - target: 127.0.0.1:1883 + scheme: tcp ## mqtt, tcp - target: 127.0.0.1:8883 - scheme: ssl - ssl_config: + scheme: tls ## ssl, tls, mqtts + tls_config: + insecure_skip_verify: true + ca_file: config/example/certs/cacert.pem + cert_file: config/example/certs/client-cert.pem + key_file: config/example/certs/client-key.pem + - target: 127.0.0.1:8083/mqtt + scheme: ws + - target: 127.0.0.1:8084/mqtt + scheme: wss + tls_config: insecure_skip_verify: true ca_file: config/example/certs/cacert.pem cert_file: config/example/certs/client-cert.pem diff --git a/prober/mqtt.go b/prober/mqtt.go index 2ac3102..23a2fc6 100644 --- a/prober/mqtt.go +++ b/prober/mqtt.go @@ -43,8 +43,8 @@ func init() { func initMQTTProbe(probe config.Probe, logger log.Logger) (*MQTTProbe, error) { opt := mqtt.NewClientOptions().AddBroker(probe.Scheme + "://" + probe.Target).SetClientID(probe.ClientID).SetUsername(probe.Username).SetPassword(probe.Password) - if probe.SSLConfig != nil { - opt.SetTLSConfig(probe.SSLConfig.ToTLSConfig()) + if probe.TLSClientConfig != nil { + opt.SetTLSConfig(probe.TLSClientConfig.ToTLSConfig()) } opt.SetOnConnectHandler(func(c mqtt.Client) { level.Info(logger).Log("msg", "Connected to MQTT broker")