Skip to content

Commit

Permalink
Add support for allowlisting of sensors by MAC
Browse files Browse the repository at this point in the history
Currently the exporter will publish data about every single
encountered sensor. This change adds support for MAC address based
allowlist so that only selected sensors can be listened for. When
the list is empty, all sensors are exported just like without this
feature.
  • Loading branch information
p2004a committed Dec 6, 2023
1 parent f8e2d22 commit a0eb39f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/gbcsdpd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,21 @@ func main() {
log.Fatalf("Failed to listen for BLE advertisements: %v", err)
}

sensorsAllowlist := make(map[string]bool)
for _, addr := range conf.SensorAllowlist {
sensorsAllowlist[addr.String()] = true
}

for adv := range advListener.Advertisements() {
data, ok := adv.ManufacturerData[ruuviManufacturerID]
if !ok {
continue
}

if len(sensorsAllowlist) > 0 && !sensorsAllowlist[adv.Address.String()] {
continue
}

ruuviData, err := ruuviparse.Parse(data)
if err != nil {
log.Printf("Failed to parse ruuvi data: %v", err)
Expand Down
13 changes: 11 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"path"
"regexp"
"strings"
Expand All @@ -35,8 +36,9 @@ type Sink interface{}

// Config contains the full parsed configuration for the application.
type Config struct {
Adapter string
Sinks []Sink
Adapter string
Sinks []Sink
SensorAllowlist []net.HardwareAddr
}

// RateLimit is configruation for the rate limiting of sinks.
Expand Down Expand Up @@ -294,6 +296,13 @@ func Read(configPath string) (*Config, error) {
} else {
config.Adapter = *fconfig.Adapter
}
for _, address := range fconfig.SensorAllowlist {
hwAddr, err := net.ParseMAC(address)
if err != nil {
return nil, fmt.Errorf("failed to parse entry in sensor allow list: %v", err)
}
config.SensorAllowlist = append(config.SensorAllowlist, hwAddr)
}
for i, sink := range fconfig.Sinks.MQTT {
mqttSink, err := parseMQTTSink(path.Dir(configPath), i, sink)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type fConfig struct {

// If none sinks are defined, a single default Stdout sink is created
Sinks fSinks `toml:"sinks"`

// Sensors MAC adresses allowlist. If emtpy, all sensors are allowed.
SensorAllowlist []string `toml:"sensor_allowlist"`
}

// Struct holds list of sinks for publications
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"testing"
"time"

Expand Down Expand Up @@ -111,6 +112,10 @@ func TestParsingCorrect(t *testing.T) {
RateLimit: &RateLimit{Max1In: 10 * time.Second},
},
},
SensorAllowlist: []net.HardwareAddr{
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xf1},
},
}
if diff := cmpConfig(config, expectedConfig); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
Expand All @@ -129,6 +134,7 @@ func TestParsingEmpty(t *testing.T) {
Name: "default-sink",
},
},
SensorAllowlist: nil,
}
if diff := cmpConfig(config, expectedConfig); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/testdata/test1/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
adapter = "hci1"

sensor_allowlist = [
"FF:FF:FF:FF:FF:FF",
"ff:ff:ff:ff:ff:f1",
]

[[sinks.stdout]]
name = "stdout sink 1"
rate_limit.max_1_in = "90s"
Expand Down

0 comments on commit a0eb39f

Please sign in to comment.