From fdbdbc112457214c0e1c2a8eec7425427dc3eb50 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 22 Aug 2023 09:06:08 +0800 Subject: [PATCH] feat: Accept Url escape in API path Use escaped for message bus topic - service name - profile name - device name Close #1495 Signed-off-by: bruce --- internal/common/utils.go | 2 +- internal/controller/messaging/callback.go | 4 ++-- internal/controller/messaging/command.go | 13 +++++++++---- internal/controller/messaging/validation.go | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/common/utils.go b/internal/common/utils.go index 0e817618..0b4754fd 100644 --- a/internal/common/utils.go +++ b/internal/common/utils.go @@ -71,7 +71,7 @@ func SendEvent(event *dtos.Event, correlationID string, dic *di.Container) { ctx = context.WithValue(ctx, common.ContentType, encoding) // nolint: staticcheck envelope := types.NewMessageEnvelope(bytes, ctx) serviceName := container.DeviceServiceFrom(dic.Get).Name - publishTopic := common.BuildTopic(configuration.MessageBus.GetBaseTopicPrefix(), common.EventsPublishTopic, DeviceServiceEventPrefix, serviceName, event.ProfileName, event.DeviceName, common.URLEncode(event.SourceName)) + publishTopic := common.BuildTopic(configuration.MessageBus.GetBaseTopicPrefix(), common.EventsPublishTopic, DeviceServiceEventPrefix, common.URLEncode(serviceName), common.URLEncode(event.ProfileName), common.URLEncode(event.DeviceName), common.URLEncode(event.SourceName)) err = mc.Publish(envelope, publishTopic) if err != nil { lc.Errorf("Failed to publish event to MessageBus: %s", err) diff --git a/internal/controller/messaging/callback.go b/internal/controller/messaging/callback.go index 7ad28de8..fd0b6722 100644 --- a/internal/controller/messaging/callback.go +++ b/internal/controller/messaging/callback.go @@ -28,7 +28,7 @@ func MetadataSystemEventsCallback(ctx context.Context, serviceBaseName string, d messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus deviceService := container.DeviceServiceFrom(dic.Get) metadataSystemEventTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), - common.MetadataSystemEventSubscribeTopic, deviceService.Name, "#") + common.MetadataSystemEventSubscribeTopic, common.URLEncode(deviceService.Name), "#") lc.Infof("Subscribing to System Events on topic: %s", metadataSystemEventTopic) @@ -49,7 +49,7 @@ func MetadataSystemEventsCallback(ctx context.Context, serviceBaseName string, d // Must replace the first wildcard with the type for Provision Watchers baseSubscribeTopic := strings.Replace(common.MetadataSystemEventSubscribeTopic, "+", common.ProvisionWatcherSystemEventType, 1) provisionWatcherSystemEventSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), - baseSubscribeTopic, serviceBaseName, "#") + baseSubscribeTopic, common.URLEncode(serviceBaseName), "#") topics = append(topics, types.TopicChannel{ Topic: provisionWatcherSystemEventSubscribeTopic, diff --git a/internal/controller/messaging/command.go b/internal/controller/messaging/command.go index 8795d3a7..f677b863 100644 --- a/internal/controller/messaging/command.go +++ b/internal/controller/messaging/command.go @@ -29,11 +29,12 @@ func SubscribeCommands(ctx context.Context, dic *di.Container) errors.EdgeX { lc := bootstrapContainer.LoggingClientFrom(dic.Get) messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus deviceService := container.DeviceServiceFrom(dic.Get) + escapedDeviceServiceName := common.URLEncode(deviceService.Name) - requestSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.CommandRequestSubscribeTopic, deviceService.Name, "#") + requestSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.CommandRequestSubscribeTopic, escapedDeviceServiceName, "#") lc.Infof("Subscribing to command requests on topic: %s", requestSubscribeTopic) - responsePublishTopicPrefix := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.ResponseTopic, deviceService.Name) + responsePublishTopicPrefix := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.ResponseTopic, escapedDeviceServiceName) lc.Infof("Responses to command requests will be published on topic: %s/", responsePublishTopicPrefix) messages := make(chan types.MessageEnvelope, 1) @@ -71,10 +72,14 @@ func SubscribeCommands(ctx context.Context, dic *di.Container) errors.EdgeX { } // expected command response topic scheme: #//// - deviceName := topicLevels[length-3] + deviceName, err := url.PathUnescape(topicLevels[length-3]) + if err != nil { + lc.Errorf("Failed to unescape device name: %s", err.Error()) + continue + } commandName, err := url.PathUnescape(topicLevels[length-2]) if err != nil { - lc.Errorf("Failed to unescape command name '%s'", commandName) + lc.Errorf("Failed to unescape command name: %s", err.Error()) continue } method := topicLevels[length-1] diff --git a/internal/controller/messaging/validation.go b/internal/controller/messaging/validation.go index fe5367eb..ff5a3a21 100644 --- a/internal/controller/messaging/validation.go +++ b/internal/controller/messaging/validation.go @@ -23,7 +23,7 @@ import ( func SubscribeDeviceValidation(ctx context.Context, dic *di.Container) errors.EdgeX { lc := bootstrapContainer.LoggingClientFrom(dic.Get) messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus - serviceName := container.DeviceServiceFrom(dic.Get).Name + serviceName := common.URLEncode(container.DeviceServiceFrom(dic.Get).Name) requestTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), serviceName, common.ValidateDeviceSubscribeTopic) lc.Infof("Subscribing to device validation requests on topic: %s", requestTopic)