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

Set kid field in JWS #16

Merged
merged 1 commit into from
Jun 14, 2024
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
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func init() {
runCmd.Flags().Int("minimiser-workers", 1, "how many minimiser workers to start (0 means same as GOMAXPROCS)")
runCmd.Flags().Bool("disable-mqtt", false, "disable MQTT message sending")
runCmd.Flags().String("mqtt-signing-key-file", "dtm-mqtt-signer-key.pem", "ECSDSA key used for signing MQTT messages")
runCmd.Flags().String("mqtt-signing-key-id", "key1", "ID (used as `kid` in JWS) when signing MQTT messages")
runCmd.Flags().String("mqtt-client-key-file", "dtm-mqtt-client-key.pem", "ECSDSA client key used for authenticating to MQTT bus")
runCmd.Flags().String("mqtt-client-cert-file", "dtm-mqtt-client.pem", "ECSDSA client cert used for authenticating to MQTT bus")
runCmd.Flags().String("mqtt-server", "127.0.0.1:8883", "MQTT server we will publish events to")
Expand Down
6 changes: 3 additions & 3 deletions pkg/runner/mqtt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runner

import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"fmt"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/eclipse/paho.golang/paho"
"github.com/eclipse/paho.golang/paho/log"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func (dtm *dnstapMinimiser) newAutoPahoClientConfig(caCertPool *x509.CertPool, s

}

func (dtm *dnstapMinimiser) runAutoPaho(cm *autopaho.ConnectionManager, topic string, mqttSigningKey *ecdsa.PrivateKey) {
func (dtm *dnstapMinimiser) runAutoPaho(cm *autopaho.ConnectionManager, topic string, mqttJWK jwk.Key) {
dtm.autopahoWg.Add(1)
defer dtm.autopahoWg.Done()
for {
Expand All @@ -69,7 +69,7 @@ func (dtm *dnstapMinimiser) runAutoPaho(cm *autopaho.ConnectionManager, topic st
return
}

signedMsg, err := jws.Sign(unsignedMsg, jws.WithJSON(), jws.WithKey(jwa.ES256, mqttSigningKey))
signedMsg, err := jws.Sign(unsignedMsg, jws.WithJSON(), jws.WithKey(jwa.ES256, mqttJWK))
if err != nil {
dtm.log.Error("runAutoPaho: failed to created JWS message", "error", err)
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/fsnotify/fsnotify"
_ "github.com/grafana/pyroscope-go/godeltaprof/http/pprof"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
Expand Down Expand Up @@ -500,8 +501,20 @@ func (dtm *dnstapMinimiser) setupMQTT() {
// Setup channel for reading messages to publish
dtm.mqttPubCh = make(chan []byte, 100)

mqttJWK, err := jwk.FromRaw(mqttSigningKey)
if err != nil {
dtm.log.Error("unable to create MQTT JWK key", "error", err)
os.Exit(1)
}

err = mqttJWK.Set(jwk.KeyIDKey, viper.GetString("mqtt-signing-key-id"))
if err != nil {
dtm.log.Error("unable to set MQTT JWK `kid`", "error", err)
os.Exit(1)
}

// Connect to the broker - this will return immediately after initiating the connection process
go dtm.runAutoPaho(autopahoCm, viper.GetString("mqtt-topic"), mqttSigningKey)
go dtm.runAutoPaho(autopahoCm, viper.GetString("mqtt-topic"), mqttJWK)
}

func Run() {
Expand Down