From 09a3995d1305c4fc67df8375b4d95852f1a26316 Mon Sep 17 00:00:00 2001 From: Alexandr Krylovskiy Date: Sat, 30 Jul 2022 14:16:46 +0200 Subject: [PATCH] Error out if broker CA file is invalid --- README.md | 10 +++++----- main.go | 36 ++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index e0d49dd..6fca343 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ $ ./mqtt-benchmark -h Usage of ./mqtt-benchmark: -broker string MQTT broker endpoint as scheme://host:port (default "tcp://localhost:1883") - -broker-cacert string + -broker-ca-cert string Path to broker CA certificate in PEM format -client-cert string Path to client certificate in PEM format @@ -32,6 +32,8 @@ Usage of ./mqtt-benchmark: Output format: text|json (default "text") -insecure Skip TLS certificate verification + -message-interval int + Time interval in seconds to publish message (default 1) -password string MQTT client password (empty if auth disabled) -payload string @@ -40,6 +42,8 @@ Usage of ./mqtt-benchmark: QoS for published messages (default 1) -quiet Suppress logs while running + -ramp-up-time int + Time in seconds to generate clients by default will not wait between load request -size int Size of the messages payload (bytes) (default 100) -topic string @@ -48,10 +52,6 @@ Usage of ./mqtt-benchmark: MQTT client username (empty if auth disabled) -wait int QoS 1 wait timeout in milliseconds (default 60000) - -ramp-up-time int - Time in seconds to generate clients, (default 0) i.e by default will not wait between load request - -message-interval - Time interval in seconds to publish message (default 1) ``` > NOTE: if `count=1` or `clients=1`, the sample standard deviation will be returned as `0` (convention due to the [lack of NaN support in JSON](https://tools.ietf.org/html/rfc4627#section-2.4)) diff --git a/main.go b/main.go index a549bbf..6722fcc 100644 --- a/main.go +++ b/main.go @@ -75,7 +75,7 @@ func main() { clientPrefix = flag.String("client-prefix", "mqtt-benchmark", "MQTT client id prefix (suffixed with '-'") clientCert = flag.String("client-cert", "", "Path to client certificate in PEM format") clientKey = flag.String("client-key", "", "Path to private clientKey in PEM format") - brokerCaCert = flag.String("broker-cacert", "", "Path to broker CA certificate in PEM format") + brokerCaCert = flag.String("broker-ca-cert", "", "Path to broker CA certificate in PEM format") insecure = flag.Bool("insecure", false, "Skip TLS certificate verification") rampUpTimeInSec = flag.Int("ramp-up-time", 0, "Time in seconds to generate clients by default will not wait between load request") messageIntervalInSec = flag.Int("message-interval", 1, "Time interval in seconds to publish message") @@ -111,23 +111,23 @@ func main() { log.Println("Starting client ", i) } c := &Client{ - ID: i, - ClientID: *clientPrefix, - BrokerURL: *broker, - BrokerUser: *username, - BrokerPass: *password, - MsgTopic: *topic, - MsgPayload: *payload, - MsgSize: *size, - MsgCount: *count, - MsgQoS: byte(*qos), - Quiet: *quiet, - WaitTimeout: time.Duration(*wait) * time.Millisecond, - TLSConfig: tlsConfig, + ID: i, + ClientID: *clientPrefix, + BrokerURL: *broker, + BrokerUser: *username, + BrokerPass: *password, + MsgTopic: *topic, + MsgPayload: *payload, + MsgSize: *size, + MsgCount: *count, + MsgQoS: byte(*qos), + Quiet: *quiet, + WaitTimeout: time.Duration(*wait) * time.Millisecond, + TLSConfig: tlsConfig, MessageInterval: *messageIntervalInSec, } go c.Run(resCh) - time.Sleep(time.Duration(sleepTime * 1000) * time.Millisecond) + time.Sleep(time.Duration(sleepTime*1000) * time.Millisecond) } // collect the results @@ -233,8 +233,12 @@ func generateTLSConfig(certFile string, keyFile string, caFile string, insecure if err != nil { log.Fatalf("Error reading CA certificate file: %v", err) } + caCertPool = x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) + ok := caCertPool.AppendCertsFromPEM(caCert) + if !ok { + log.Fatalf("Error parsing CA certificate %v", certFile) + } } cfg := tls.Config{