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

refactor(data): Use deepCopy of messageBusInfo to avoid external adds #4038

Merged
merged 1 commit into from
May 25, 2022
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
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