Skip to content

Commit

Permalink
Error out if broker CA file is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
krylovsk committed Jul 30, 2022
1 parent fbedea9 commit 09a3995
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down
36 changes: 20 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
clientPrefix = flag.String("client-prefix", "mqtt-benchmark", "MQTT client id prefix (suffixed with '-<client-num>'")
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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 09a3995

Please sign in to comment.