Skip to content

Commit

Permalink
Fix IPv6 processing according to privacy ip masking (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanchyk authored May 20, 2021
1 parent 5f50c46 commit e8e7bad
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ private static boolean isEmptyBidderResponses(List<BidderResponseInfo> bidderRes
.allMatch(CollectionUtils::isEmpty);
}

private static List<BidderResponseInfo> toBidderResponseInfos(List<BidderResponse> bidderResponses,
List<Imp> imps) {
private List<BidderResponseInfo> toBidderResponseInfos(List<BidderResponse> bidderResponses, List<Imp> imps) {
final List<BidderResponseInfo> result = new ArrayList<>();
for (BidderResponse bidderResponse : bidderResponses) {
final String bidder = bidderResponse.getBidder();
Expand All @@ -317,7 +316,7 @@ private static List<BidderResponseInfo> toBidderResponseInfos(List<BidderRespons
return result;
}

private static BidInfo toBidInfo(Bid bid, BidType type, List<Imp> imps, String bidder) {
private BidInfo toBidInfo(Bid bid, BidType type, List<Imp> imps, String bidder) {
return BidInfo.builder()
.bid(bid)
.bidType(type)
Expand Down Expand Up @@ -358,8 +357,8 @@ private Future<BidResponse> cacheBidsAndCreateResponse(List<BidderResponseInfo>
final Set<BidInfo> winningBidInfos = targeting == null
? null
: bidInfos.stream()
.filter(bidInfo -> bidInfo.getTargetingInfo().isWinningBid())
.collect(Collectors.toSet());
.filter(bidInfo -> bidInfo.getTargetingInfo().isWinningBid())
.collect(Collectors.toSet());

final Set<BidInfo> bidsToCache = cacheInfo.isShouldCacheWinningBidsOnly() ? winningBidInfos : bidInfos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.auction.IpAddressHelper;
import org.prebid.server.auction.StoredRequestProcessor;
import org.prebid.server.auction.TimeoutResolver;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.cookie.UidsCookieService;
import org.prebid.server.exception.BlacklistedAccountException;
import org.prebid.server.exception.InvalidRequestException;
Expand Down Expand Up @@ -55,10 +57,11 @@ public class Ortb2RequestFactory {
private final List<String> blacklistedAccounts;
private final UidsCookieService uidsCookieService;
private final RequestValidator requestValidator;
private final TimeoutResolver timeoutResolver;
private final TimeoutFactory timeoutFactory;
private final StoredRequestProcessor storedRequestProcessor;
private final ApplicationSettings applicationSettings;
private final TimeoutResolver timeoutResolver;
private final IpAddressHelper ipAddressHelper;

public Ortb2RequestFactory(boolean enforceValidAccount,
List<String> blacklistedAccounts,
Expand All @@ -67,16 +70,18 @@ public Ortb2RequestFactory(boolean enforceValidAccount,
TimeoutResolver timeoutResolver,
TimeoutFactory timeoutFactory,
StoredRequestProcessor storedRequestProcessor,
ApplicationSettings applicationSettings) {
ApplicationSettings applicationSettings,
IpAddressHelper ipAddressHelper) {

this.enforceValidAccount = enforceValidAccount;
this.blacklistedAccounts = Objects.requireNonNull(blacklistedAccounts);
this.uidsCookieService = Objects.requireNonNull(uidsCookieService);
this.requestValidator = Objects.requireNonNull(requestValidator);
this.timeoutResolver = Objects.requireNonNull(timeoutResolver);
this.timeoutFactory = Objects.requireNonNull(timeoutFactory);
this.applicationSettings = Objects.requireNonNull(applicationSettings);
this.storedRequestProcessor = Objects.requireNonNull(storedRequestProcessor);
this.applicationSettings = Objects.requireNonNull(applicationSettings);
this.ipAddressHelper = Objects.requireNonNull(ipAddressHelper);
}

public Future<AuctionContext> fetchAccountAndCreateAuctionContext(RoutingContext routingContext,
Expand Down Expand Up @@ -280,21 +285,30 @@ private ExtRequest enrichExtRequest(ExtRequest ext, Account account) {

private Device enrichDevice(Device device, PrivacyContext privacyContext) {
final String ipAddress = privacyContext.getIpAddress();
final String country = getIfNotNull(privacyContext.getTcfContext().getGeoInfo(), GeoInfo::getCountry);
final IpAddress ip = ipAddressHelper.toIpAddress(ipAddress);

final String ipAddressInRequest = getIfNotNull(device, Device::getIp);
final String ipV4InRequest = getIfNotNull(device, Device::getIp);
final String ipV4 = ip != null && ip.getVersion() == IpAddress.IP.v4 ? ipAddress : null;
final boolean shouldUpdateIpV4 = ipV4 != null && !Objects.equals(ipV4InRequest, ipV4);

final Geo geo = getIfNotNull(device, Device::getGeo);
final String countryFromRequest = getIfNotNull(geo, Geo::getCountry);
final String ipV6InRequest = getIfNotNull(device, Device::getIpv6);
final String ipV6 = ip != null && ip.getVersion() == IpAddress.IP.v6 ? ipAddress : null;
final boolean shouldUpdateIpV6 = ipV6 != null && !Objects.equals(ipV6InRequest, ipV6);

final boolean shouldUpdateIp = ipAddress != null && !Objects.equals(ipAddressInRequest, ipAddress);
final boolean shouldUpdateCountry = country != null && !Objects.equals(countryFromRequest, country);
final Geo geo = getIfNotNull(device, Device::getGeo);
final String countryInRequest = getIfNotNull(geo, Geo::getCountry);
final String country = getIfNotNull(privacyContext.getTcfContext().getGeoInfo(), GeoInfo::getCountry);
final boolean shouldUpdateCountry = country != null && !Objects.equals(countryInRequest, country);

if (shouldUpdateIp || shouldUpdateCountry) {
if (shouldUpdateIpV4 || shouldUpdateIpV6 || shouldUpdateCountry) {
final Device.DeviceBuilder deviceBuilder = device != null ? device.toBuilder() : Device.builder();

if (shouldUpdateIp) {
deviceBuilder.ip(ipAddress);
if (shouldUpdateIpV4) {
deviceBuilder.ip(ipV4);
}

if (shouldUpdateIpV6) {
deviceBuilder.ipv6(ipV6);
}

if (shouldUpdateCountry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ Ortb2RequestFactory openRtb2RequestFactory(
TimeoutResolver timeoutResolver,
TimeoutFactory timeoutFactory,
StoredRequestProcessor storedRequestProcessor,
ApplicationSettings applicationSettings) {
ApplicationSettings applicationSettings,
IpAddressHelper ipAddressHelper) {

final List<String> blacklistedAccounts = splitToList(blacklistedAccountsString);

Expand All @@ -222,7 +223,8 @@ Ortb2RequestFactory openRtb2RequestFactory(
timeoutResolver,
timeoutFactory,
storedRequestProcessor,
applicationSettings);
applicationSettings,
ipAddressHelper);
}

@Bean
Expand Down
Loading

0 comments on commit e8e7bad

Please sign in to comment.