Skip to content

Commit

Permalink
should catch alias whose syncher key conflicts with bidder other than…
Browse files Browse the repository at this point in the history
… its parent

An alias can define its syncer key in YAML configuration, but this key must not be the same as that of a bidder other than its parent

For example,

parent1:
  syncer:
     key: k1

alias1:
   aliasOf: parent1
   syncer:
     Key: k1

parent2:
  syncer:
    key: k2

alias2:
  aliasOf: parent2
  syncer:
     key: k1 // syncer key k1 conflicts with parent1 key
  • Loading branch information
onkarvhanumante committed Sep 5, 2023
1 parent 2c1712e commit 7078ba5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 23 additions & 6 deletions usersync/syncersbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerCon
}

var (
bidderNames, bidderNamesWithEndpoints, nonAliasBidders []string
syncerConfig namedSyncerConfig
bidderNames, bidderNamesWithEndpoints []string
nonAliasBidderNames, parentBidderNames []string
nonAliasBidderNameMap = make(map[string]struct{}) // needed for O(1) lookup
syncerConfig namedSyncerConfig
)

for _, bidder := range biddersSyncerConfig {
Expand All @@ -110,7 +112,10 @@ func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerCon
syncerConfig = bidder

if len(bidder.bidderInfo.AliasOf) == 0 {
nonAliasBidders = append(nonAliasBidders, bidder.name)
nonAliasBidderNames = append(nonAliasBidderNames, bidder.name)
nonAliasBidderNameMap[bidder.name] = struct{}{}
} else {
parentBidderNames = append(parentBidderNames, bidder.bidderInfo.AliasOf)
}
}
}
Expand All @@ -121,12 +126,24 @@ func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerCon
return namedSyncerConfig{}, fmt.Errorf("bidders %s share the same syncer key, but none define endpoints (iframe and/or redirect)", bidders)
}

if len(nonAliasBidders) > 1 {
sort.Strings(nonAliasBidders)
bidders := strings.Join(nonAliasBidders, ", ")
if len(nonAliasBidderNames) > 1 {
sort.Strings(nonAliasBidderNames)
bidders := strings.Join(nonAliasBidderNames, ", ")
return namedSyncerConfig{}, fmt.Errorf("bidders %s define endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints", bidders)
}

// invalidAliases - stores alias whose syncer key conflicts with bidder other than their parent
invalidAliases := []string{}
for _, bidderName := range parentBidderNames {
if _, ok := nonAliasBidderNameMap[bidderName]; !ok {
invalidAliases = append(invalidAliases, bidderName)
}
}
if len(invalidAliases) > 0 {
sort.Strings(invalidAliases)
return namedSyncerConfig{}, fmt.Errorf("found aliases whose syncer key conflicts with a bidder other than their parent, aliases: %s", strings.Join(invalidAliases, ", "))
}

return syncerConfig, nil
}

Expand Down
18 changes: 14 additions & 4 deletions usersync/syncersbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,16 @@ func TestChooseSyncerConfig(t *testing.T) {
cfg: syncerCfg,
bidderInfo: config.BidderInfo{AliasOf: ""},
}
alias = namedSyncerConfig{
name: "alias",
alias1 = namedSyncerConfig{
name: "alias-1",
cfg: syncerCfg,
bidderInfo: config.BidderInfo{AliasOf: "parent"},
}
alias2 = namedSyncerConfig{
name: "alias-2",
cfg: syncerCfg,
bidderInfo: config.BidderInfo{AliasOf: "foo"},
}
)

testCases := []struct {
Expand Down Expand Up @@ -333,8 +338,13 @@ func TestChooseSyncerConfig(t *testing.T) {
},
{
description: "alias-can-have-same-key-as-parent",
given: []namedSyncerConfig{parent, alias},
expectedConfig: alias,
given: []namedSyncerConfig{parent, alias1},
expectedConfig: alias1,
},
{
description: "aliases-syncer-key-conflicts-with-non-parent-bidder",
given: []namedSyncerConfig{parent, alias1, alias2},
expectedError: "found aliases whose syncer key conflicts with a bidder other than their parent, aliases: foo",
},
}

Expand Down

0 comments on commit 7078ba5

Please sign in to comment.