From 59268d300580d48916bb78d89e9c484fd80457c9 Mon Sep 17 00:00:00 2001 From: Hristo Bozhilov Date: Mon, 13 Feb 2023 09:38:39 +0100 Subject: [PATCH 1/3] [#69] Add possibility to specify MQTT username directly Signed-off-by: Hristo Bozhilov --- config/connections.go | 18 ++++++++++++++---- config/connections_test.go | 23 +++++++++++++++++++++++ flags/flags.go | 1 + flags/flags_test.go | 2 ++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/config/connections.go b/config/connections.go index 03e1607..c0d2598 100644 --- a/config/connections.go +++ b/config/connections.go @@ -24,11 +24,12 @@ import ( "syscall" "time" - "github.com/ThreeDotsLabs/watermill" - "github.com/ThreeDotsLabs/watermill/message" "github.com/cenkalti/backoff/v3" "github.com/pkg/errors" + "github.com/ThreeDotsLabs/watermill" + "github.com/ThreeDotsLabs/watermill/message" + "github.com/eclipse-kanto/suite-connector/logger" "github.com/eclipse-kanto/suite-connector/routing" "github.com/eclipse-kanto/suite-connector/util" @@ -61,6 +62,7 @@ type HubConnectionSettings struct { PolicyID string `json:"policyId"` Address string `json:"address"` Password string `json:"password"` + Username string `json:"username"` ClientID string `json:"clientId"` AuthID string `json:"authId"` @@ -163,7 +165,7 @@ func CreateHubConnection( if len(tlsConfig.Certificates) == 0 { settings.UseCertificate = false - honoConfig.Credentials.UserName = util.NewHonoUserName(settings.AuthID, settings.TenantID) + honoConfig.Credentials.UserName = NewHubUsername(settings) honoConfig.Credentials.Password = settings.Password } else { if len(settings.DeviceIDPattern) > 0 { @@ -186,7 +188,7 @@ func CreateHubConnection( } else { logger.Warnf("Insecure connection is used with address=%s", settings.Address) settings.UseCertificate = false - honoConfig.Credentials.UserName = util.NewHonoUserName(settings.AuthID, settings.TenantID) + honoConfig.Credentials.UserName = NewHubUsername(settings) honoConfig.Credentials.Password = settings.Password } @@ -443,6 +445,14 @@ func HonoConnect(sigs chan os.Signal, } } +func NewHubUsername(settings *HubConnectionSettings) string { + if len(settings.Username) > 0 { + return settings.Username + } else { + return util.NewHonoUserName(settings.AuthID, settings.TenantID) + } +} + func isConnectionSecure(schema string) bool { switch schema { case "wss", "ssl", "tls", "mqtts", "mqtt+ssl", "tcps": diff --git a/config/connections_test.go b/config/connections_test.go index 476e631..851dfac 100644 --- a/config/connections_test.go +++ b/config/connections_test.go @@ -26,6 +26,7 @@ import ( "github.com/eclipse-kanto/suite-connector/config" "github.com/eclipse-kanto/suite-connector/logger" "github.com/eclipse-kanto/suite-connector/testutil" + "github.com/eclipse-kanto/suite-connector/util" conn "github.com/eclipse-kanto/suite-connector/connector" ) @@ -249,3 +250,25 @@ func TestHonoSub(t *testing.T) { require.NotNil(t, sub) assert.NoError(t, sub.Close()) } + +func TestNewHubUsername(t *testing.T) { + hubSettings := config.HubConnectionSettings{ + TenantID: "t6ea0f08c_hub", + AuthID: "org.eclipse.kanto_test", + DeviceID: "org.eclipse.kanto:test", + Address: "mqtts://mqtt.example.com:8883", + Username: "testuser", + } + settings := &config.Settings{ + HubConnectionSettings: hubSettings, + } + require.NoError(t, settings.ValidateDynamic()) + assert.Equal(t, "testuser", config.NewHubUsername(&hubSettings)) + + hubSettings.Username = "" + settings = &config.Settings{ + HubConnectionSettings: hubSettings, + } + require.NoError(t, settings.ValidateDynamic()) + assert.Equal(t, util.NewHonoUserName(hubSettings.AuthID, hubSettings.TenantID), config.NewHubUsername(&hubSettings)) +} diff --git a/flags/flags.go b/flags/flags.go index 5017246..d62d814 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -100,6 +100,7 @@ func AddHub(f *flag.FlagSet, settings, def *config.HubConnectionSettings) { f.StringVar(&settings.PolicyID, "policyId", def.PolicyID, "Policy `ID`") f.StringVar(&settings.AuthID, "authId", def.AuthID, "Authorization `ID`") f.StringVar(&settings.Password, "password", def.Password, "Hub endpoint password") + f.StringVar(&settings.Username, "username", def.Username, "Hub endpoint username") f.StringVar(&settings.ClientID, "clientId", def.ClientID, "Hub client `ID`") AddTLS(f, &settings.TLSSettings, &def.TLSSettings) diff --git a/flags/flags_test.go b/flags/flags_test.go index 0fb3729..0c39188 100644 --- a/flags/flags_test.go +++ b/flags/flags_test.go @@ -50,6 +50,7 @@ func TestFlagsMappings(t *testing.T) { "-policyId=D", "-address=mqtts://mqtt.bosch-iot-hub.com:8883", "-password=E", + "-username=T", "-clientId=F", "-localAddress=tcp://localhost:1883", "-localUsername=G", @@ -81,6 +82,7 @@ func TestFlagsMappings(t *testing.T) { assert.Equal(t, "D", cmd.PolicyID) assert.Equal(t, "mqtts://mqtt.bosch-iot-hub.com:8883", cmd.Address) assert.Equal(t, "E", cmd.Password) + assert.Equal(t, "T", cmd.Username) assert.Equal(t, "F", cmd.ClientID) assert.Equal(t, "tcp://localhost:1883", cmd.LocalAddress) assert.Equal(t, "G", cmd.LocalUsername) From 0d560bd4ac9ffa5edbf56a12f4ce540f08a035b9 Mon Sep 17 00:00:00 2001 From: Hristo Bozhilov Date: Mon, 13 Feb 2023 12:21:32 +0100 Subject: [PATCH 2/3] [#69] Add possibility to specify MQTT username directly Signed-off-by: Hristo Bozhilov --- config/connections.go | 1 + 1 file changed, 1 insertion(+) diff --git a/config/connections.go b/config/connections.go index c0d2598..f81d00a 100644 --- a/config/connections.go +++ b/config/connections.go @@ -445,6 +445,7 @@ func HonoConnect(sigs chan os.Signal, } } +// NewHubUsername returns username from hub connection settings func NewHubUsername(settings *HubConnectionSettings) string { if len(settings.Username) > 0 { return settings.Username From 2d192d02c671af03506006a4a7d84c4df367d2df Mon Sep 17 00:00:00 2001 From: Hristo Bozhilov Date: Mon, 13 Feb 2023 12:26:22 +0100 Subject: [PATCH 3/3] [#69] Add possibility to specify MQTT username directly Signed-off-by: Hristo Bozhilov --- config/connections.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/connections.go b/config/connections.go index f81d00a..fae5e13 100644 --- a/config/connections.go +++ b/config/connections.go @@ -449,9 +449,8 @@ func HonoConnect(sigs chan os.Signal, func NewHubUsername(settings *HubConnectionSettings) string { if len(settings.Username) > 0 { return settings.Username - } else { - return util.NewHonoUserName(settings.AuthID, settings.TenantID) } + return util.NewHonoUserName(settings.AuthID, settings.TenantID) } func isConnectionSecure(schema string) bool {