Skip to content

Commit ac91023

Browse files
minggibrocaar
authored andcommitted
Add CA cert option for TLS connection (#42)
1 parent c99091a commit ac91023

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

backend/mqttpubsub/backend.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package mqttpubsub
22

33
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"io/ioutil"
47
"encoding/json"
58
"fmt"
69
"sync"
@@ -21,7 +24,7 @@ type Backend struct {
2124
}
2225

2326
// NewBackend creates a new Backend.
24-
func NewBackend(server, username, password string) (*Backend, error) {
27+
func NewBackend(server, username, password, cafile string) (*Backend, error) {
2528
b := Backend{
2629
txPacketChan: make(chan gw.TXPacketBytes),
2730
gateways: make(map[lorawan.EUI64]struct{}),
@@ -33,7 +36,14 @@ func NewBackend(server, username, password string) (*Backend, error) {
3336
opts.SetPassword(password)
3437
opts.SetOnConnectHandler(b.onConnected)
3538
opts.SetConnectionLostHandler(b.onConnectionLost)
36-
39+
40+
if cafile != "" {
41+
tlsconfig, err := NewTLSConfig(cafile)
42+
if err == nil {
43+
opts.SetTLSConfig(tlsconfig)
44+
}
45+
}
46+
3747
log.WithField("server", server).Info("backend: connecting to mqtt broker")
3848
b.conn = mqtt.NewClient(opts)
3949
if token := b.conn.Connect(); token.Wait() && token.Error() != nil {
@@ -43,6 +53,26 @@ func NewBackend(server, username, password string) (*Backend, error) {
4353
return &b, nil
4454
}
4555

56+
// NewTLSConfig returns the TLS configuration.
57+
func NewTLSConfig(cafile string) (*tls.Config, error) {
58+
// Import trusted certificates from CAfile.pem.
59+
60+
cert, err := ioutil.ReadFile(cafile)
61+
if err != nil {
62+
log.Errorf("backend: couldn't load cafile: %s", err)
63+
return nil, err
64+
}
65+
66+
certpool := x509.NewCertPool()
67+
certpool.AppendCertsFromPEM(cert)
68+
69+
// Create tls.Config with desired tls properties
70+
return &tls.Config{
71+
// RootCAs = certs used to verify server cert.
72+
RootCAs: certpool,
73+
}, nil
74+
}
75+
4676
// Close closes the backend.
4777
func (b *Backend) Close() {
4878
b.conn.Disconnect(250) // wait 250 milisec to complete pending actions

backend/mqttpubsub/backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestBackend(t *testing.T) {
2222
defer c.Disconnect(0)
2323

2424
Convey("Given a new Backend", func() {
25-
backend, err := NewBackend(conf.Server, conf.Username, conf.Password)
25+
backend, err := NewBackend(conf.Server, conf.Username, conf.Password, "")
2626
So(err, ShouldBeNil)
2727
defer backend.Close()
2828

cmd/lora-gateway-bridge/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GLOBAL OPTIONS:
1313
--mqtt-server "tcp://127.0.0.1:1883" MQTT server [$MQTT_SERVER]
1414
--mqtt-username MQTT username [$MQTT_USERNAME]
1515
--mqtt-password MQTT password [$MQTT_PASSWORD]
16+
--mqtt-ca-cert CA certificate file [$MQTT_CA_CERT]
1617
--log-level "4" debug=5, info=4, warning=3, error=2, fatal=1, panic=0 [$LOG_LEVEL]
1718
--help, -h show help
1819
--version, -v print the version

cmd/lora-gateway-bridge/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func run(c *cli.Context) error {
2828
var pubsub *mqttpubsub.Backend
2929
for {
3030
var err error
31-
pubsub, err = mqttpubsub.NewBackend(c.String("mqtt-server"), c.String("mqtt-username"), c.String("mqtt-password"))
31+
pubsub, err = mqttpubsub.NewBackend(c.String("mqtt-server"), c.String("mqtt-username"), c.String("mqtt-password"), c.String("mqtt-ca-cert"))
3232
if err == nil {
3333
break
3434
}
@@ -113,6 +113,11 @@ func main() {
113113
Usage: "mqtt server password (optional)",
114114
EnvVar: "MQTT_PASSWORD",
115115
},
116+
cli.StringFlag{
117+
Name: "mqtt-ca-cert",
118+
Usage: "mqtt CA certificate file (optional)",
119+
EnvVar: "MQTT_CA_CERT",
120+
},
116121
cli.BoolFlag{
117122
Name: "skip-crc-check",
118123
Usage: "skip the CRC status-check of received packets",

0 commit comments

Comments
 (0)