Skip to content

Commit

Permalink
fix: drop duplicate aws auth field in postgresql (dapr#3525)
Browse files Browse the repository at this point in the history
Signed-off-by: Eileen Yu <[email protected]>
Co-authored-by: Yaron Schneider <[email protected]>
  • Loading branch information
2 people authored and elena-kolevska committed Sep 12, 2024
1 parent 3be44e1 commit 71de061
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ package metadataschema

import (
"fmt"
"strings"
)

// Built-in authentication profiles
var BuiltinAuthenticationProfiles map[string][]AuthenticationProfile

// ParseBuiltinAuthenticationProfile returns an AuthenticationProfile(s) from a given BuiltinAuthenticationProfile.
func ParseBuiltinAuthenticationProfile(bi BuiltinAuthenticationProfile) ([]AuthenticationProfile, error) {
func ParseBuiltinAuthenticationProfile(bi BuiltinAuthenticationProfile, componentTitle string) ([]AuthenticationProfile, error) {
profiles, ok := BuiltinAuthenticationProfiles[bi.Name]
if !ok {
return nil, fmt.Errorf("built-in authentication profile %s does not exist", bi.Name)
Expand All @@ -30,7 +31,14 @@ func ParseBuiltinAuthenticationProfile(bi BuiltinAuthenticationProfile) ([]Authe
res := make([]AuthenticationProfile, len(profiles))
for i, profile := range profiles {
res[i] = profile

res[i].Metadata = mergedMetadata(bi.Metadata, res[i].Metadata...)

// If component is PostgreSQL, filter out duplicated aws profile fields
if strings.ToLower(componentTitle) == "postgresql" && bi.Name == "aws" {
res[i].Metadata = filterOutDuplicateFields(res[i].Metadata)
}

}
return res, nil
}
Expand All @@ -45,3 +53,29 @@ func mergedMetadata(base []Metadata, add ...Metadata) []Metadata {
res = append(res, add...)
return res
}

// filterOutDuplicateFields removes specific duplicated fields from the metadata
func filterOutDuplicateFields(metadata []Metadata) []Metadata {
duplicateFields := map[string]int{
"awsRegion": 0,
"accessKey": 0,
"secretKey": 0,
}

filteredMetadata := []Metadata{}

for _, field := range metadata {
if _, exists := duplicateFields[field.Name]; !exists {
filteredMetadata = append(filteredMetadata, field)
} else {
if field.Name == "awsRegion" && duplicateFields["awsRegion"] == 0 {
filteredMetadata = append(filteredMetadata, field)
duplicateFields["awsRegion"]++
} else if field.Name != "awsRegion" {
continue
}
}
}

return filteredMetadata
}
2 changes: 1 addition & 1 deletion .build-tools/pkg/metadataschema/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *ComponentMetadata) IsValid() error {

// Append built-in authentication profiles
for _, profile := range c.BuiltInAuthenticationProfiles {
appendProfiles, err := ParseBuiltinAuthenticationProfile(profile)
appendProfiles, err := ParseBuiltinAuthenticationProfile(profile, c.Title)
if err != nil {
return err
}
Expand Down

0 comments on commit 71de061

Please sign in to comment.