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

Added TransmitPreciseGeo activity handling in modules #3348

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
activityControl = privacy.NewActivityControl(&account.Privacy)

hookExecutor.SetActivityControl(activityControl)
hookExecutor.SetAccount(account)

secGPC := r.Header.Get("Sec-GPC")

Expand Down
1 change: 1 addition & 0 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
activityControl = privacy.NewActivityControl(&account.Privacy)

hookExecutor.SetActivityControl(activityControl)
hookExecutor.SetAccount(account)

ctx := context.Background()

Expand Down
16 changes: 12 additions & 4 deletions hooks/hookexecution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hookexecution
import (
"context"
"fmt"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/ortb"
"strings"
"sync"
Expand Down Expand Up @@ -68,7 +69,7 @@ func executeGroup[H any, P any](

for _, hook := range group.Hooks {
mCtx := executionCtx.getModuleContext(hook.Module)
newPayload := handleModuleActivities(hook.Code, executionCtx.activityControl, payload)
newPayload := handleModuleActivities(hook.Code, executionCtx.activityControl, payload, executionCtx.account.Privacy)
wg.Add(1)
go func(hw hooks.HookWrapper[H], moduleCtx hookstage.ModuleInvocationContext) {
defer wg.Done()
Expand Down Expand Up @@ -315,22 +316,29 @@ func handleHookMutations[P any](
return payload
}

func handleModuleActivities[P any](hookCode string, activityControl privacy.ActivityControl, payload P) P {
func handleModuleActivities[P any](hookCode string, activityControl privacy.ActivityControl, payload P, ap config.AccountPrivacy) P {
payloadData, ok := any(&payload).(hookstage.RequestUpdater)
if !ok {
return payload
}

scopeGeneral := privacy.Component{Type: privacy.ComponentTypeGeneral, Name: hookCode}
transmitUserFPDActivityAllowed := activityControl.Allow(privacy.ActivityTransmitUserFPD, scopeGeneral, privacy.ActivityRequest{})
transmitPreciseGeoActivityAllowed := activityControl.Allow(privacy.ActivityTransmitPreciseGeo, scopeGeneral, privacy.ActivityRequest{})

if !transmitUserFPDActivityAllowed {
if !transmitUserFPDActivityAllowed || !transmitPreciseGeoActivityAllowed {
// changes need to be applied to new payload and leave original payload unchanged
bidderReq := payloadData.GetBidderRequestPayload()

bidderReqCopy := ortb.CloneBidderReq(bidderReq.BidRequest)

privacy.ScrubUserFPD(bidderReqCopy)
if !transmitUserFPDActivityAllowed {
privacy.ScrubUserFPD(bidderReqCopy)
}
if !transmitPreciseGeoActivityAllowed {
ipConf := privacy.IPConf{IPV6: ap.IPv6Config, IPV4: ap.IPv4Config}
privacy.ScrubGeoAndDeviceIP(bidderReqCopy, ipConf)
}

var newPayload = payload
var np = any(&newPayload).(hookstage.RequestUpdater)
Expand Down
90 changes: 87 additions & 3 deletions hooks/hookexecution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"testing"
)

const (
testIpv6 = "1111:2222:3333:4444:5555:6666:7777:8888"
testIPv6Scubbed = "1111:2222::"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nitpick: typo Scrubbed

testIPv6ScrubBytes = 32
)

func TestHandleModuleActivitiesBidderRequestPayload(t *testing.T) {

testCases := []struct {
Expand Down Expand Up @@ -49,13 +55,45 @@ func TestHandleModuleActivitiesBidderRequestPayload(t *testing.T) {
}},
},
},

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: can you remove this blank line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored func handleModuleActivities the way you proposed to refactor similar function in analytics activities to reduce nesting.

{
description: "payload should change when transmitPreciseGeo is blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIPv6Scubbed},
},
}},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
accountPrivacy := config.AccountPrivacy{IPv6Config: config.IPv6{testIPv6ScrubBytes}}
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, accountPrivacy)
assert.Equal(t, test.expectedPayloadData.Request.BidRequest, newPayload.Request.BidRequest)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down Expand Up @@ -101,13 +139,45 @@ func TestHandleModuleActivitiesProcessedAuctionRequestPayload(t *testing.T) {
}},
},
},

{
description: "payload should change when transmitPreciseGeo is blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIPv6Scubbed},
}},
},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
accountPrivacy := config.AccountPrivacy{IPv6Config: config.IPv6{testIPv6ScrubBytes}}
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, accountPrivacy)
assert.Equal(t, test.expectedPayloadData.Request.BidRequest, newPayload.Request.BidRequest)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down Expand Up @@ -137,13 +207,27 @@ func TestHandleModuleActivitiesNoBidderRequestPayload(t *testing.T) {
privacyConfig: getTransmitUFPDActivityConfig("foo", true),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
{
description: "payload should change when transmitPreciseGeo is blocked by activity",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the test cases in this test have descriptions that indicate the payload should change but it doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, description fixed. Payload should NOT change in any of these cases, because hookstage.RawAuctionRequestPayload doesn't implement hookstage.RequestUpdater

hookCode: "foo",
inPayloadData: hookstage.RawAuctionRequestPayload{},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.RawAuctionRequestPayload{},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, config.AccountPrivacy{})
assert.Equal(t, test.expectedPayloadData, newPayload)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down
1 change: 1 addition & 0 deletions hooks/hookexecution/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewHookExecutor(builder hooks.ExecutionPlanBuilder, endpoint string, me met
stageOutcomes: []StageOutcome{},
moduleContexts: &moduleContexts{ctxs: make(map[string]hookstage.ModuleContext)},
metricEngine: me,
account: &config.Account{Privacy: config.AccountPrivacy{}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for creating an empty account here instead of just keeping it as nil until an account is set on the hookExecutor using SetAccount? It seems like an attempt to avoid nil checks when executing each stage but I'm not sure that's a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. I changed handleModuleActivities function signature to accept account *config.Account instead of AccountPrivacy.

}
}

Expand Down
8 changes: 8 additions & 0 deletions hooks/hookexecution/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,14 @@ func getTransmitUFPDActivityConfig(componentName string, allow bool) *config.Acc
}
}

func getTransmitPreciseGeoActivityConfig(componentName string, allow bool) *config.AccountPrivacy {
return &config.AccountPrivacy{
AllowActivities: &config.AllowActivities{
TransmitPreciseGeo: buildDefaultActivityConfig(componentName, allow),
},
}
}

func buildDefaultActivityConfig(componentName string, allow bool) config.Activity {
return config.Activity{
Default: ptrutil.ToPtr(true),
Expand Down