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

Add account.privacy.gdpr.eea-countries property #3212

Merged
merged 5 commits into from
Jun 21, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.prebid.server.json.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class CommaSeparatedStringAsListOfStringsDeserializer extends StdDeserializer<List<String>> {

public CommaSeparatedStringAsListOfStringsDeserializer() {
super(List.class);
}

@Override
public List<String> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if (parser.getCurrentToken() != JsonToken.VALUE_STRING) {
reportWrongTokenException(parser, context);
}

final String value = parser.getValueAsString();
if (value == null) {
reportWrongTokenException(parser, context);
}

try {
return parseList(value);
} catch (NumberFormatException e) {
reportPropertyInputMismatch(parser, context, e.getMessage());
throw new AssertionError();
}
}

private static void reportWrongTokenException(JsonParser parser, DeserializationContext context)
throws IOException {

context.reportWrongTokenException(
JsonToken.class,
JsonToken.VALUE_STRING,
"""
Failed to parse field %s to List<String> type with a reason: \
Expected comma-separated string.""".formatted(parser.getCurrentName()));
}

private static List<String> parseList(String value) throws NumberFormatException {
return Arrays.stream(value.split(","))
.map(StringUtils::strip)
.filter(StringUtils::isNotBlank)
.toList();
}

private static void reportPropertyInputMismatch(JsonParser parser, DeserializationContext context, String cause)
throws IOException {

context.reportPropertyInputMismatch(
JsonToken.class,
parser.getCurrentName(),
"""
Failed to parse field %s to List<String> type with a reason: \
NumberFormatException %s""".formatted(parser.getCurrentName(), cause));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Future<TcfContext> resolveTcfContext(Privacy privacy,

final Future<TcfContext> tcfContextFuture = !isGdprEnabled(accountGdprConfig, requestType)
? Future.succeededFuture(TcfContext.empty())
: prepareTcfContext(privacy, country, ipAddress, requestLogInfo, timeout, geoInfo);
: prepareTcfContext(privacy, country, ipAddress, accountGdprConfig, requestLogInfo, timeout, geoInfo);

return tcfContextFuture.map(this::updateTcfGeoMetrics);
}
Expand Down Expand Up @@ -185,6 +185,7 @@ private boolean isGdprEnabled(AccountGdprConfig accountGdprConfig, MetricName re
private Future<TcfContext> prepareTcfContext(Privacy privacy,
String country,
String ipAddress,
AccountGdprConfig accountGdprConfig,
RequestLogInfo requestLogInfo,
Timeout timeout,
GeoInfo geoInfo) {
Expand All @@ -195,7 +196,7 @@ private Future<TcfContext> prepareTcfContext(Privacy privacy,
final boolean consentValid = isConsentValid(consent);

final String effectiveIpAddress = maybeMaskIp(ipAddress, consent);
final Boolean inEea = isCountryInEea(country);
final Boolean inEea = isCountryInEea(country, accountGdprConfig);

final TcfContext defaultContext = TcfContext.builder()
.inGdprScope(inScopeOfGdpr(gdprDefaultValue))
Expand All @@ -218,7 +219,7 @@ private Future<TcfContext> prepareTcfContext(Privacy privacy,

return geoLocationServiceWrapper.doLookup(effectiveIpAddress, country, timeout)
.recover(ignored -> Future.succeededFuture(geoInfo))
.map(lookupResult -> enrichWithGeoInfo(defaultContext, lookupResult, country));
.map(lookupResult -> enrichWithGeoInfo(defaultContext, lookupResult, country, accountGdprConfig));
}

private String maybeMaskIp(String ipAddress, TCString consent) {
Expand All @@ -240,9 +241,13 @@ private static boolean shouldMaskIp(TCString consent) {
return isConsentValid(consent) && consent.getVersion() == 2 && !consent.getSpecialFeatureOptIns().contains(1);
}

private TcfContext enrichWithGeoInfo(TcfContext defaultTcfContext, GeoInfo geoInfo, String defaultCountry) {
private TcfContext enrichWithGeoInfo(TcfContext defaultTcfContext,
GeoInfo geoInfo,
String defaultCountry,
AccountGdprConfig accountGdprConfig) {

final String country = ObjectUtil.getIfNotNullOrDefault(geoInfo, GeoInfo::getCountry, () -> defaultCountry);
final Boolean inEea = isCountryInEea(country);
final Boolean inEea = isCountryInEea(country, accountGdprConfig);
final boolean inScope = inScopeOfGdpr(inEea);

return defaultTcfContext.toBuilder()
Expand All @@ -252,8 +257,11 @@ private TcfContext enrichWithGeoInfo(TcfContext defaultTcfContext, GeoInfo geoIn
.build();
}

private Boolean isCountryInEea(String country) {
return country != null ? eeaCountries.contains(country) : null;
private Boolean isCountryInEea(String country, AccountGdprConfig accountGdprConfig) {
final Set<String> publisherEeaCountries = ObjectUtils.defaultIfNull(
accountGdprConfig != null ? accountGdprConfig.getEeaCountries() : null,
eeaCountries);
return country != null ? publisherEeaCountries.contains(country) : null;
}

private TcfContext updateTcfGeoMetrics(TcfContext tcfContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.prebid.server.settings.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Builder;
import lombok.Value;
import org.prebid.server.json.deserializer.CommaSeparatedStringAsListOfStringsDeserializer;

import java.util.List;
import java.util.Set;

@Builder
@Value
Expand All @@ -13,6 +16,10 @@ public class AccountGdprConfig {
@JsonProperty("enabled")
Boolean enabled;

@JsonProperty("eea-countries")
@JsonDeserialize(using = CommaSeparatedStringAsListOfStringsDeserializer.class)
Set<String> eeaCountries;

@JsonProperty("channel-enabled")
EnabledForRequestType enabledForRequestType;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ host-cookie:
max-cookie-size-bytes: 4096
gdpr:
enabled: true
eea-countries: at,bg,be,cy,cz,dk,ee,fi,fr,de,gr,hu,ie,it,lv,lt,lu,mt,nl,pl,pt,ro,sk,si,es,se,gb,is,no,li,ai,aw,pt,bm,aq,io,vg,ic,ky,fk,re,mw,gp,gf,yt,pf,tf,gl,pt,ms,an,bq,cw,sx,nc,pn,sh,pm,gs,tc,uk,wf,ch
eea-countries: at,bg,be,cy,cz,dk,ee,fi,fr,de,gr,hu,ie,it,lv,lt,lu,mt,nl,pl,pt,ro,sk,si,es,se,gb,is,no,li,ai,aw,pt,bm,aq,io,vg,ic,ky,fk,re,mw,gp,gf,yt,pf,tf,gl,pt,ms,an,bq,cw,sx,nc,pn,sh,pm,gs,tc,uk,wf
vendorlist:
default-timeout-ms: 2000
v2:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.prebid.server.json.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.io.IOException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;

public class CommaSeparatedStringAsListOfStingsDeserializerTest {

@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();

private CommaSeparatedStringAsListOfStringsDeserializer target;

@Mock
private JsonParser parser;

@Mock
private DeserializationContext context;

@Before
public void setUp() {
target = new CommaSeparatedStringAsListOfStringsDeserializer();
}

@Test
public void deserializeShouldThrowExceptionOnWrongJsonToken() throws IOException {
// given
given(parser.getCurrentToken()).willReturn(JsonToken.VALUE_FALSE);
given(parser.getCurrentName()).willReturn("FIELD");
doThrow(RuntimeException.class)
.when(context)
.reportWrongTokenException(
eq(JsonToken.class),
eq(JsonToken.VALUE_STRING),
eq("""
Failed to parse field FIELD to List<String> type with a reason: \
Expected comma-separated string."""));

// when and then
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> target.deserialize(parser, context));
}

@Test
public void deserializeShouldThrowExceptionOnNullValue() throws IOException {
// given
given(parser.getCurrentToken()).willReturn(JsonToken.VALUE_STRING);
given(parser.getValueAsString()).willReturn(null);
given(parser.getCurrentName()).willReturn("FIELD");
doThrow(RuntimeException.class)
.when(context)
.reportWrongTokenException(
eq(JsonToken.class),
eq(JsonToken.VALUE_STRING),
eq("""
Failed to parse field FIELD to List<String> type with a reason: \
Expected comma-separated string."""));

// when and then
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> target.deserialize(parser, context));
}

@Test
public void deserializeShouldReturnExpectedValueOnEmptyString() throws IOException {
// given
given(parser.getCurrentToken()).willReturn(JsonToken.VALUE_STRING);
given(parser.getValueAsString()).willReturn("");

// when
final List<String> result = target.deserialize(parser, context);

// then
assertThat(result).isEmpty();
}

@Test
public void deserializeShouldReturnExpectedValue() throws IOException {
// given
given(parser.getCurrentToken()).willReturn(JsonToken.VALUE_STRING);
given(parser.getValueAsString()).willReturn("aa, ab,ac ,ad");

// when
final List<String> result = target.deserialize(parser, context);

// then
assertThat(result).containsExactly("aa", "ab", "ac", "ad");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonMap;
import static org.apache.commons.lang3.StringUtils.EMPTY;
Expand Down Expand Up @@ -311,6 +312,45 @@ public void resolveTcfContextShouldConsiderPresenceOfConsentStringAsInScope() {
verify(metrics).updatePrivacyTcfGeoMetric(2, null);
}

@Test
public void resolveTcfContextShouldUseEeaListFromAccountConfig() {
// given
final GdprConfig gdprConfig = GdprConfig.builder()
.enabled(true)
.consentStringMeansInScope(true)
.build();

target = new TcfDefinerService(
gdprConfig,
singleton(EEA_COUNTRY),
tcf2Service,
geoLocationServiceWrapper,
bidderCatalog,
ipAddressHelper,
metrics);

final String vendorConsent = "CPBCa-mPBCa-mAAAAAENA0CAAEAAAAAAACiQAaQAwAAgAgABoAAAAAA";

// when
final Future<TcfContext> result = target.resolveTcfContext(
Privacy.builder().consentString(vendorConsent).build(),
"country",
null,
AccountGdprConfig.builder().eeaCountries(emptySet()).build(),
null,
null,
null,
null);

// then
assertThat(result).isSucceeded();
assertThat(result.result())
.extracting(TcfContext::getInEea)
.isEqualTo(false);

verify(metrics).updatePrivacyTcfGeoMetric(2, false);
}

@Test
public void resolveTcfContextShouldReturnGdprFromCountryWhenGdprFromRequestIsNotValidAndGeoLookupSkipped() {
// given
Expand Down
Loading