Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: rename ssl_config to tls_config #69

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions config/example/just_probe.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions prober/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down