From 7e2bc23e53823bfe42aab21131f1a3f069ab37c1 Mon Sep 17 00:00:00 2001 From: "Charel Baum (external expert on behalf of DB InfraGO AG)" Date: Thu, 2 May 2024 13:21:29 +0200 Subject: [PATCH] fix(bucket): also sanitize observed FilterRules.Name of lambda and topic configs Signed-off-by: Charel Baum (external expert on behalf of DB InfraGO AG) (cherry picked from commit f0a48f992fb9cc3d792b194fb37474d30cdd3145) --- .../s3/bucket/convert/converter_types.go | 2 + .../s3/bucket/convert/zz_converter.go | 70 +++++++++++++++++++ .../s3/bucket/notificationConfig.go | 60 +++++++++++++++- 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/pkg/controller/s3/bucket/convert/converter_types.go b/pkg/controller/s3/bucket/convert/converter_types.go index 2505b396b4..c6e91d3a4d 100644 --- a/pkg/controller/s3/bucket/convert/converter_types.go +++ b/pkg/controller/s3/bucket/convert/converter_types.go @@ -23,5 +23,7 @@ import ( // goverter:converter // goverter:ignoreUnexported type Converter interface { + DeepCopyAWSLambdaFunctionConfiguration([]types.LambdaFunctionConfiguration) []types.LambdaFunctionConfiguration DeepCopyAWSQueueConfiguration([]types.QueueConfiguration) []types.QueueConfiguration + DeepCopyAWSTopicConfiguration([]types.TopicConfiguration) []types.TopicConfiguration } diff --git a/pkg/controller/s3/bucket/convert/zz_converter.go b/pkg/controller/s3/bucket/convert/zz_converter.go index 866530308d..98783ca594 100755 --- a/pkg/controller/s3/bucket/convert/zz_converter.go +++ b/pkg/controller/s3/bucket/convert/zz_converter.go @@ -6,6 +6,16 @@ import types "github.com/aws/aws-sdk-go-v2/service/s3/types" type ConverterImpl struct{} +func (c *ConverterImpl) DeepCopyAWSLambdaFunctionConfiguration(source []types.LambdaFunctionConfiguration) []types.LambdaFunctionConfiguration { + var typesLambdaFunctionConfigurationList []types.LambdaFunctionConfiguration + if source != nil { + typesLambdaFunctionConfigurationList = make([]types.LambdaFunctionConfiguration, len(source)) + for i := 0; i < len(source); i++ { + typesLambdaFunctionConfigurationList[i] = c.typesLambdaFunctionConfigurationToTypesLambdaFunctionConfiguration(source[i]) + } + } + return typesLambdaFunctionConfigurationList +} func (c *ConverterImpl) DeepCopyAWSQueueConfiguration(source []types.QueueConfiguration) []types.QueueConfiguration { var typesQueueConfigurationList []types.QueueConfiguration if source != nil { @@ -16,6 +26,16 @@ func (c *ConverterImpl) DeepCopyAWSQueueConfiguration(source []types.QueueConfig } return typesQueueConfigurationList } +func (c *ConverterImpl) DeepCopyAWSTopicConfiguration(source []types.TopicConfiguration) []types.TopicConfiguration { + var typesTopicConfigurationList []types.TopicConfiguration + if source != nil { + typesTopicConfigurationList = make([]types.TopicConfiguration, len(source)) + for i := 0; i < len(source); i++ { + typesTopicConfigurationList[i] = c.typesTopicConfigurationToTypesTopicConfiguration(source[i]) + } + } + return typesTopicConfigurationList +} func (c *ConverterImpl) pTypesNotificationConfigurationFilterToPTypesNotificationConfigurationFilter(source *types.NotificationConfigurationFilter) *types.NotificationConfigurationFilter { var pTypesNotificationConfigurationFilter *types.NotificationConfigurationFilter if source != nil { @@ -52,6 +72,31 @@ func (c *ConverterImpl) typesFilterRuleToTypesFilterRule(source types.FilterRule typesFilterRule.Value = pString return typesFilterRule } +func (c *ConverterImpl) typesLambdaFunctionConfigurationToTypesLambdaFunctionConfiguration(source types.LambdaFunctionConfiguration) types.LambdaFunctionConfiguration { + var typesLambdaFunctionConfiguration types.LambdaFunctionConfiguration + var typesEventList []types.Event + if source.Events != nil { + typesEventList = make([]types.Event, len(source.Events)) + for i := 0; i < len(source.Events); i++ { + typesEventList[i] = types.Event(source.Events[i]) + } + } + typesLambdaFunctionConfiguration.Events = typesEventList + var pString *string + if source.LambdaFunctionArn != nil { + xstring := *source.LambdaFunctionArn + pString = &xstring + } + typesLambdaFunctionConfiguration.LambdaFunctionArn = pString + typesLambdaFunctionConfiguration.Filter = c.pTypesNotificationConfigurationFilterToPTypesNotificationConfigurationFilter(source.Filter) + var pString2 *string + if source.Id != nil { + xstring2 := *source.Id + pString2 = &xstring2 + } + typesLambdaFunctionConfiguration.Id = pString2 + return typesLambdaFunctionConfiguration +} func (c *ConverterImpl) typesQueueConfigurationToTypesQueueConfiguration(source types.QueueConfiguration) types.QueueConfiguration { var typesQueueConfiguration types.QueueConfiguration var typesEventList []types.Event @@ -77,3 +122,28 @@ func (c *ConverterImpl) typesQueueConfigurationToTypesQueueConfiguration(source typesQueueConfiguration.Id = pString2 return typesQueueConfiguration } +func (c *ConverterImpl) typesTopicConfigurationToTypesTopicConfiguration(source types.TopicConfiguration) types.TopicConfiguration { + var typesTopicConfiguration types.TopicConfiguration + var typesEventList []types.Event + if source.Events != nil { + typesEventList = make([]types.Event, len(source.Events)) + for i := 0; i < len(source.Events); i++ { + typesEventList[i] = types.Event(source.Events[i]) + } + } + typesTopicConfiguration.Events = typesEventList + var pString *string + if source.TopicArn != nil { + xstring := *source.TopicArn + pString = &xstring + } + typesTopicConfiguration.TopicArn = pString + typesTopicConfiguration.Filter = c.pTypesNotificationConfigurationFilterToPTypesNotificationConfigurationFilter(source.Filter) + var pString2 *string + if source.Id != nil { + xstring2 := *source.Id + pString2 = &xstring2 + } + typesTopicConfiguration.Id = pString2 + return typesTopicConfiguration +} diff --git a/pkg/controller/s3/bucket/notificationConfig.go b/pkg/controller/s3/bucket/notificationConfig.go index 5629fc1b7a..06f8073469 100644 --- a/pkg/controller/s3/bucket/notificationConfig.go +++ b/pkg/controller/s3/bucket/notificationConfig.go @@ -96,10 +96,12 @@ func IsNotificationConfigurationUpToDate(cr *v1beta1.NotificationConfiguration, sortTopic(generated.TopicConfigurations) sortTopic(external.TopicConfigurations) - // The AWS API returns QueueConfiguration.Filter.Key.FilterRules.Name as "Prefix"/"Suffix" but expects + // The AWS API returns *Configuration.Filter.Key.FilterRules.Name as "Prefix"/"Suffix" but expects // "prefix"/"suffix" this leads to inconsistency and a constant diff. Fixes // https://github.com/crossplane-contrib/provider-aws/issues/1165 + external.LambdaFunctionConfigurations = sanitizedLambdaFunctionConfigurations(external.LambdaFunctionConfigurations) external.QueueConfigurations = sanitizedQueueConfigurations(external.QueueConfigurations) + external.TopicConfigurations = sanitizedTopicConfigurations(external.TopicConfigurations) if cmp.Equal(external.LambdaFunctionConfigurations, generated.LambdaFunctionConfigurations, cmpopts.IgnoreTypes(document.NoSerde{}, types.LambdaFunctionConfiguration{}.Id), cmpopts.EquateEmpty()) && cmp.Equal(external.QueueConfigurations, generated.QueueConfigurations, cmpopts.IgnoreTypes(document.NoSerde{}, types.QueueConfiguration{}.Id), cmpopts.EquateEmpty()) && @@ -137,6 +139,34 @@ func sortTopic(configs []types.TopicConfiguration) { }) } +func sanitizedLambdaFunctionConfigurations(configs []types.LambdaFunctionConfiguration) []types.LambdaFunctionConfiguration { + if configs == nil { + return []types.LambdaFunctionConfiguration{} + } + + sConfig := (&convert.ConverterImpl{}).DeepCopyAWSLambdaFunctionConfiguration(configs) + for c := range sConfig { + if sConfig[c].Events == nil { + sConfig[c].Events = []types.Event{} + } + if sConfig[c].Filter == nil { + continue + } + if sConfig[c].Filter.Key == nil { + continue + } + if sConfig[c].Filter.Key.FilterRules == nil { + sConfig[c].Filter.Key.FilterRules = []types.FilterRule{} + } + for r := range sConfig[c].Filter.Key.FilterRules { + name := string(sConfig[c].Filter.Key.FilterRules[r].Name) + sConfig[c].Filter.Key.FilterRules[r].Name = types.FilterRuleName(strings.ToLower(name)) + } + } + + return sConfig +} + func sanitizedQueueConfigurations(configs []types.QueueConfiguration) []types.QueueConfiguration { if configs == nil { return []types.QueueConfiguration{} @@ -165,6 +195,34 @@ func sanitizedQueueConfigurations(configs []types.QueueConfiguration) []types.Qu return sConfig } +func sanitizedTopicConfigurations(configs []types.TopicConfiguration) []types.TopicConfiguration { + if configs == nil { + return []types.TopicConfiguration{} + } + + sConfig := (&convert.ConverterImpl{}).DeepCopyAWSTopicConfiguration(configs) + for c := range sConfig { + if sConfig[c].Events == nil { + sConfig[c].Events = []types.Event{} + } + if sConfig[c].Filter == nil { + continue + } + if sConfig[c].Filter.Key == nil { + continue + } + if sConfig[c].Filter.Key.FilterRules == nil { + sConfig[c].Filter.Key.FilterRules = []types.FilterRule{} + } + for r := range sConfig[c].Filter.Key.FilterRules { + name := string(sConfig[c].Filter.Key.FilterRules[r].Name) + sConfig[c].Filter.Key.FilterRules[r].Name = types.FilterRuleName(strings.ToLower(name)) + } + } + + return sConfig +} + // GenerateLambdaConfiguration creates []awss3.LambdaFunctionConfiguration from the local NotificationConfiguration func GenerateLambdaConfiguration(config *v1beta1.NotificationConfiguration) []types.LambdaFunctionConfiguration { // NOTE(muvaf): We skip prealloc because the behavior of AWS SDK differs when