Skip to content

Commit

Permalink
refactor(data): Use deepCopy of messageBusInfo to avoid external adds (
Browse files Browse the repository at this point in the history
…#4038)

close #4021

Signed-off-by: Ginny Guan <[email protected]>
  • Loading branch information
jinlinGuan authored May 25, 2022
1 parent 4c16504 commit 9735311
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 24 additions & 4 deletions internal/core/data/messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
bootstrapMessaging "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/messaging"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"

"github.com/edgexfoundry/edgex-go/internal/core/data/container"
Expand All @@ -34,7 +35,9 @@ import (
// and adds it to the DIC
func BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer startup.Timer, dic *di.Container) bool {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
messageBusInfo := container.ConfigurationFrom(dic.Get).MessageQueue

// Make sure the MessageBus password is not leaked into the Service Config that can be retrieved via the /config endpoint
messageBusInfo := deepCopy(container.ConfigurationFrom(dic.Get).MessageQueue)

messageBusInfo.AuthMode = strings.ToLower(strings.TrimSpace(messageBusInfo.AuthMode))
if len(messageBusInfo.AuthMode) > 0 && messageBusInfo.AuthMode != bootstrapMessaging.AuthModeNone {
Expand Down Expand Up @@ -102,13 +105,30 @@ func BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer star
messageBusInfo.PublishTopicPrefix,
messageBusInfo.AuthMode)

// Make sure the MessageBus password is not leaked into the Service Config that can be retrieved via the /config endpoint
delete(messageBusInfo.Optional, bootstrapMessaging.OptionsPasswordKey)

return true
}
}

lc.Error("Connecting to MessageBus time out")
return false
}

func deepCopy(target bootstrapConfig.MessageBusInfo) bootstrapConfig.MessageBusInfo {
result := bootstrapConfig.MessageBusInfo{
Type: target.Type,
Protocol: target.Protocol,
Host: target.Host,
Port: target.Port,
PublishTopicPrefix: target.PublishTopicPrefix,
SubscribeTopic: target.SubscribeTopic,
AuthMode: target.AuthMode,
SecretName: target.SecretName,
SubscribeEnabled: target.SubscribeEnabled,
}
result.Optional = make(map[string]string)
for key, value := range target.Optional {
result.Optional[key] = value
}

return result
}
9 changes: 5 additions & 4 deletions internal/core/data/messaging/messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestMain(m *testing.M) {
func TestBootstrapHandler(t *testing.T) {
validCreateClient := config.ConfigurationStruct{
MessageQueue: bootstrapConfig.MessageBusInfo{
Type: messaging.ZeroMQ, // Use ZMQ so no issue connecting.
Protocol: "http",
Host: "*",
Port: 8765,
Type: messaging.Redis,
Protocol: "redis",
Host: "localhost",
Port: 6379,
PublishTopicPrefix: "edgex/events/#",
AuthMode: messaging2.AuthModeUsernamePassword,
SecretName: "redisdb",
Expand Down Expand Up @@ -99,6 +99,7 @@ func TestBootstrapHandler(t *testing.T) {

actual := BootstrapHandler(context.Background(), &sync.WaitGroup{}, startup.NewTimer(1, 1), dic)
assert.Equal(t, test.ExpectedResult, actual)
assert.Empty(t, test.Config.MessageQueue.Optional)
if test.ExpectClient {
assert.NotNil(t, bootstrapContainer.MessagingClientFrom(dic.Get))
} else {
Expand Down

0 comments on commit 9735311

Please sign in to comment.