Skip to content

Commit 1bb3010

Browse files
authored
fix: Username may be required when MQTT cert authentication (#1215)
* fix: Username may be required when MQTT cert authentication Signed-off-by: ancientxu <ancientxu@gmail.com>
1 parent 69f97d0 commit 1bb3010

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pkg/secure/mqttfactory.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ func (factory MqttFactory) configureMQTTClientForAuth(secretData *messaging.Secr
8282
InsecureSkipVerify: factory.skipCertVerify,
8383
MinVersion: tls.VersionTLS12,
8484
}
85+
// Username may be required when cert authentication
86+
if secretData.Username != "" {
87+
factory.opts.SetUsername(secretData.Username)
88+
}
8589
switch factory.authMode {
8690
case messaging.AuthModeUsernamePassword:
87-
factory.opts.SetUsername(secretData.Username)
8891
factory.opts.SetPassword(secretData.Password)
8992
case messaging.AuthModeCert:
9093
cert, err = tls.X509KeyPair(secretData.CertPemBlock, secretData.KeyPemBlock)

pkg/secure/mqttfactory_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
package secure
2121

2222
import (
23+
mqtt "github.com/eclipse/paho.mqtt.golang"
2324
"os"
2425
"testing"
2526

26-
"github.com/eclipse/paho.mqtt.golang"
2727
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
2828
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/messaging"
2929
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
@@ -143,7 +143,7 @@ func TestConfigureMQTTClientForAuthWithCACert(t *testing.T) {
143143

144144
require.NoError(t, err)
145145
assert.NotNil(t, target.opts.TLSConfig.RootCAs)
146-
assert.Empty(t, target.opts.Username)
146+
assert.Equal(t, target.opts.Username, "Username")
147147
assert.Empty(t, target.opts.Password)
148148
assert.Nil(t, target.opts.TLSConfig.Certificates)
149149
}
@@ -159,7 +159,7 @@ func TestConfigureMQTTClientForAuthWithClientCert(t *testing.T) {
159159
CaPemBlock: []byte(testCACert),
160160
})
161161
require.NoError(t, err)
162-
assert.Empty(t, target.opts.Username)
162+
assert.Equal(t, target.opts.Username, "Username")
163163
assert.Empty(t, target.opts.Password)
164164
assert.NotNil(t, target.opts.TLSConfig.Certificates)
165165
assert.NotNil(t, target.opts.TLSConfig.RootCAs)
@@ -177,7 +177,7 @@ func TestConfigureMQTTClientForAuthWithClientCertNoCA(t *testing.T) {
177177
})
178178

179179
require.NoError(t, err)
180-
assert.Empty(t, target.opts.Username)
180+
assert.Equal(t, target.opts.Username, messaging.SecretUsernameKey)
181181
assert.Empty(t, target.opts.Password)
182182
assert.NotNil(t, target.opts.TLSConfig.Certificates)
183183
assert.Nil(t, target.opts.TLSConfig.RootCAs)

0 commit comments

Comments
 (0)