Skip to content

Commit 60ab599

Browse files
moved config store to operator service
1 parent f1969cc commit 60ab599

14 files changed

Lines changed: 178 additions & 327 deletions

conf/local-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"operator_type": "public",
4242
"encrypted_files": false,
4343
"disable_optout_token": true,
44-
"enable_remote_config": false,
45-
"uid_instance_id_prefix": "local-operator",
46-
"identity_environment": "test"
44+
"enable_remote_config": true,
45+
"uid_instance_id_prefix": "local-operator"
4746
}

conf/local-e2e-docker-private-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"cloud_refresh_interval": 30,
3232
"salts_expired_shutdown_hours": 12,
3333
"operator_type": "private",
34-
"enable_remote_config": false,
35-
"uid_instance_id_prefix": "local-private-operator",
36-
"identity_environment": "test"
34+
"enable_remote_config": true,
35+
"uid_instance_id_prefix": "local-private-operator"
3736
}

conf/local-e2e-docker-public-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"salts_expired_shutdown_hours": 12,
3939
"operator_type": "public",
4040
"disable_optout_token": true,
41-
"enable_remote_config": false,
42-
"uid_instance_id_prefix": "local-public-operator",
43-
"identity_environment": "test"
41+
"enable_remote_config": true,
42+
"uid_instance_id_prefix": "local-public-operator"
4443
}

conf/local-e2e-private-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"client_side_token_generate_log_invalid_http_origins": true,
4343
"salts_expired_shutdown_hours": 12,
4444
"operator_type": "private",
45-
"enable_remote_config": false,
46-
"uid_instance_id_prefix": "local-private-operator",
47-
"identity_environment": "test"
45+
"enable_remote_config": true,
46+
"uid_instance_id_prefix": "local-private-operator"
4847
}

conf/local-e2e-public-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"salts_expired_shutdown_hours": 12,
4545
"operator_type": "public",
4646
"disable_optout_token": true,
47-
"enable_remote_config": false,
48-
"uid_instance_id_prefix": "local-public-operator",
49-
"identity_environment": "test"
47+
"enable_remote_config": true,
48+
"uid_instance_id_prefix": "local-public-operator"
5049
}

src/main/java/com/uid2/operator/model/IdentityRequest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ public final class IdentityRequest {
44
public final PublisherIdentity publisherIdentity;
55
public final UserIdentity userIdentity;
66
public final OptoutCheckPolicy optoutCheckPolicy;
7-
public final IdentityEnvironment identityEnvironment;
87

98
public IdentityRequest(
109
PublisherIdentity publisherIdentity,
1110
UserIdentity userIdentity,
12-
OptoutCheckPolicy tokenGeneratePolicy,
13-
IdentityEnvironment identityEnvironment)
11+
OptoutCheckPolicy tokenGeneratePolicy)
1412
{
1513
this.publisherIdentity = publisherIdentity;
1614
this.userIdentity = userIdentity;
1715
this.optoutCheckPolicy = tokenGeneratePolicy;
18-
this.identityEnvironment = identityEnvironment;
1916
}
2017

2118
public boolean shouldCheckOptOut() {

src/main/java/com/uid2/operator/model/MapRequest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ public final class MapRequest {
66
public final UserIdentity userIdentity;
77
public final OptoutCheckPolicy optoutCheckPolicy;
88
public final Instant asOf;
9-
public final IdentityEnvironment identityEnvironment;
10-
119
public MapRequest(
1210
UserIdentity userIdentity,
1311
OptoutCheckPolicy optoutCheckPolicy,
14-
Instant asOf,
15-
IdentityEnvironment identityEnvironment)
12+
Instant asOf)
1613
{
1714
this.userIdentity = userIdentity;
1815
this.optoutCheckPolicy = optoutCheckPolicy;
1916
this.asOf = asOf;
20-
this.identityEnvironment = identityEnvironment;
2117
}
2218

2319
public boolean shouldCheckOptOut() {

src/main/java/com/uid2/operator/service/IUIDOperatorService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111

1212
public interface IUIDOperatorService {
1313

14-
IdentityTokens generateIdentity(IdentityRequest request, Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter);
14+
IdentityTokens generateIdentity(IdentityRequest request) throws Exception;
1515

16-
RefreshResponse refreshIdentity(RefreshToken token, Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter, IdentityEnvironment env);
16+
RefreshResponse refreshIdentity(RefreshToken token) throws Exception;
1717

18-
MappedIdentity mapIdentity(MapRequest request);
18+
MappedIdentity mapIdentity(MapRequest request) throws Exception;
1919

2020
@Deprecated
21-
MappedIdentity map(UserIdentity userIdentity, Instant asOf, IdentityEnvironment env);
21+
MappedIdentity map(UserIdentity userIdentity, Instant asOf) throws Exception;
2222

2323
List<SaltEntry> getModifiedBuckets(Instant sinceTimestamp);
2424

25-
void invalidateTokensAsync(UserIdentity userIdentity, Instant asOf, String uidTraceId, IdentityEnvironment env, Handler<AsyncResult<Instant>> handler);
25+
void invalidateTokensAsync(UserIdentity userIdentity, Instant asOf, String uidTraceId, Handler<AsyncResult<Instant>> handler);
2626

27-
boolean advertisingTokenMatches(String advertisingToken, UserIdentity userIdentity, Instant asOf, IdentityEnvironment env);
27+
boolean advertisingTokenMatches(String advertisingToken, UserIdentity userIdentity, Instant asOf);
2828

2929
Instant getLatestOptoutEntry(UserIdentity userIdentity, Instant asOf);
3030
}

src/main/java/com/uid2/operator/service/UIDOperatorService.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.uid2.operator.service;
22

33
import com.uid2.operator.model.*;
4+
import com.uid2.operator.store.IConfigStore;
5+
import com.uid2.operator.store.RuntimeConfig;
46
import com.uid2.operator.util.PrivacyBits;
57
import com.uid2.shared.audit.UidInstanceIdProvider;
68
import com.uid2.shared.model.SaltEntry;
@@ -52,16 +54,20 @@ public class UIDOperatorService implements IUIDOperatorService {
5254

5355
private final Handler<Boolean> saltRetrievalResponseHandler;
5456
private final UidInstanceIdProvider uidInstanceIdProvider;
57+
private final IConfigStore configStore;
58+
private RuntimeConfig runtimeConfig;
5559

5660
public UIDOperatorService(IOptOutStore optOutStore, ISaltProvider saltProvider, ITokenEncoder encoder, Clock clock,
57-
IdentityScope identityScope, Handler<Boolean> saltRetrievalResponseHandler, boolean identityV3Enabled, UidInstanceIdProvider uidInstanceIdProvider) {
61+
IdentityScope identityScope, Handler<Boolean> saltRetrievalResponseHandler, boolean identityV3Enabled, UidInstanceIdProvider uidInstanceIdProvider, IConfigStore configStore) {
5862
this.saltProvider = saltProvider;
5963
this.encoder = encoder;
6064
this.optOutStore = optOutStore;
6165
this.clock = clock;
6266
this.identityScope = identityScope;
6367
this.saltRetrievalResponseHandler = saltRetrievalResponseHandler;
6468
this.uidInstanceIdProvider = uidInstanceIdProvider;
69+
this.configStore = configStore;
70+
this.runtimeConfig = configStore.getConfig();
6571

6672
this.testOptOutIdentityForEmail = getFirstLevelHashIdentity(identityScope, IdentityType.Email,
6773
InputUtil.normalizeEmail(OptOutIdentityForEmail).getIdentityInput(), Instant.now());
@@ -82,6 +88,12 @@ public UIDOperatorService(IOptOutStore optOutStore, ISaltProvider saltProvider,
8288
this.rawUidV3Enabled = identityV3Enabled;
8389
}
8490

91+
private void loadAndValidateRuntimeConfig() throws Exception {
92+
this.configStore.loadContent();
93+
this.runtimeConfig = this.configStore.getConfig();
94+
validateTokenDurations(runtimeConfig.getRefreshIdentityTokenExpires(), runtimeConfig.getRefreshTokenExpires(), runtimeConfig.getIdentityTokenExpires());
95+
}
96+
8597
private void validateTokenDurations(Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter) {
8698
if (identityExpiresAfter.compareTo(refreshExpiresAfter) > 0) {
8799
throw new IllegalStateException(REFRESH_TOKEN_EXPIRES_AFTER_SECONDS + " (" + refreshExpiresAfter.toSeconds() + ") < " + IDENTITY_TOKEN_EXPIRES_AFTER_SECONDS + " (" + identityExpiresAfter.toSeconds() + ")");
@@ -95,8 +107,9 @@ private void validateTokenDurations(Duration refreshIdentityAfter, Duration refr
95107
}
96108

97109
@Override
98-
public IdentityTokens generateIdentity(IdentityRequest request, Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter) {
99-
this.validateTokenDurations(refreshIdentityAfter, refreshExpiresAfter, identityExpiresAfter);
110+
public IdentityTokens generateIdentity(IdentityRequest request) throws Exception {
111+
loadAndValidateRuntimeConfig();
112+
100113
final Instant now = EncodingUtils.NowUTCMillis(this.clock);
101114
final byte[] firstLevelHash = getFirstLevelHash(request.userIdentity.id, now);
102115
final UserIdentity firstLevelHashIdentity = new UserIdentity(
@@ -106,13 +119,14 @@ public IdentityTokens generateIdentity(IdentityRequest request, Duration refresh
106119
if (request.shouldCheckOptOut() && getGlobalOptOutResult(firstLevelHashIdentity, false).isOptedOut()) {
107120
return IdentityTokens.LogoutToken;
108121
} else {
109-
return this.generateIdentity(request.publisherIdentity, firstLevelHashIdentity, refreshIdentityAfter, refreshExpiresAfter, identityExpiresAfter, request.identityEnvironment);
122+
return this.generateIdentity(request.publisherIdentity, firstLevelHashIdentity);
110123
}
111124
}
112125

113126
@Override
114-
public RefreshResponse refreshIdentity(RefreshToken token, Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter, IdentityEnvironment env) {
115-
this.validateTokenDurations(refreshIdentityAfter, refreshExpiresAfter, identityExpiresAfter);
127+
public RefreshResponse refreshIdentity(RefreshToken token) throws Exception {
128+
loadAndValidateRuntimeConfig();
129+
116130
// should not be possible as different scopes should be using different keys, but just in case
117131
if (token.userIdentity.identityScope != this.identityScope) {
118132
return RefreshResponse.Invalid;
@@ -138,7 +152,7 @@ public RefreshResponse refreshIdentity(RefreshToken token, Duration refreshIdent
138152
final Duration durationSinceLastRefresh = Duration.between(token.createdAt, now);
139153

140154
if (!optedOut) {
141-
IdentityTokens identityTokens = this.generateIdentity(token.publisherIdentity, token.userIdentity, refreshIdentityAfter, refreshExpiresAfter, identityExpiresAfter, env);
155+
IdentityTokens identityTokens = this.generateIdentity(token.publisherIdentity, token.userIdentity);
142156

143157
return RefreshResponse.createRefreshedResponse(identityTokens, durationSinceLastRefresh, isCstg);
144158
} else {
@@ -152,19 +166,21 @@ public RefreshResponse refreshIdentity(RefreshToken token, Duration refreshIdent
152166
}
153167

154168
@Override
155-
public MappedIdentity mapIdentity(MapRequest request) {
169+
public MappedIdentity mapIdentity(MapRequest request) throws Exception {
170+
loadAndValidateRuntimeConfig();
171+
156172
final UserIdentity firstLevelHashIdentity = getFirstLevelHashIdentity(request.userIdentity, request.asOf);
157173
if (request.shouldCheckOptOut() && getGlobalOptOutResult(firstLevelHashIdentity, false).isOptedOut()) {
158174
return MappedIdentity.LogoutIdentity;
159175
} else {
160-
return getMappedIdentity(firstLevelHashIdentity, request.asOf, request.identityEnvironment);
176+
return getMappedIdentity(firstLevelHashIdentity, request.asOf);
161177
}
162178
}
163179

164180
@Override
165-
public MappedIdentity map(UserIdentity userIdentity, Instant asOf, IdentityEnvironment env) {
181+
public MappedIdentity map(UserIdentity userIdentity, Instant asOf) throws Exception {
166182
final UserIdentity firstLevelHashIdentity = getFirstLevelHashIdentity(userIdentity, asOf);
167-
return getMappedIdentity(firstLevelHashIdentity, asOf, env);
183+
return getMappedIdentity(firstLevelHashIdentity, asOf);
168184
}
169185

170186
@Override
@@ -183,9 +199,9 @@ private ISaltProvider.ISaltSnapshot getSaltProviderSnapshot(Instant asOf) {
183199
}
184200

185201
@Override
186-
public void invalidateTokensAsync(UserIdentity userIdentity, Instant asOf, String uidTraceId, IdentityEnvironment env, Handler<AsyncResult<Instant>> handler) {
202+
public void invalidateTokensAsync(UserIdentity userIdentity, Instant asOf, String uidTraceId, Handler<AsyncResult<Instant>> handler) {
187203
final UserIdentity firstLevelHashIdentity = getFirstLevelHashIdentity(userIdentity, asOf);
188-
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, asOf, env);
204+
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, asOf);
189205

190206
this.optOutStore.addEntry(firstLevelHashIdentity, mappedIdentity.advertisingId, uidTraceId, this.uidInstanceIdProvider.getInstanceId(), r -> {
191207
if (r.succeeded()) {
@@ -197,9 +213,9 @@ public void invalidateTokensAsync(UserIdentity userIdentity, Instant asOf, Strin
197213
}
198214

199215
@Override
200-
public boolean advertisingTokenMatches(String advertisingToken, UserIdentity userIdentity, Instant asOf, IdentityEnvironment env) {
216+
public boolean advertisingTokenMatches(String advertisingToken, UserIdentity userIdentity, Instant asOf) {
201217
final UserIdentity firstLevelHashIdentity = getFirstLevelHashIdentity(userIdentity, asOf);
202-
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, asOf, env);
218+
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, asOf);
203219

204220
final AdvertisingToken token = this.encoder.decodeAdvertisingToken(advertisingToken);
205221
return Arrays.equals(mappedIdentity.advertisingId, token.userIdentity.id);
@@ -224,7 +240,7 @@ private byte[] getFirstLevelHash(byte[] identityHash, Instant asOf) {
224240
return TokenUtils.getFirstLevelHash(identityHash, getSaltProviderSnapshot(asOf).getFirstLevelSalt());
225241
}
226242

227-
private MappedIdentity getMappedIdentity(UserIdentity firstLevelHashIdentity, Instant asOf, IdentityEnvironment env) {
243+
private MappedIdentity getMappedIdentity(UserIdentity firstLevelHashIdentity, Instant asOf) {
228244
final SaltEntry rotatingSalt = getSaltProviderSnapshot(asOf).getRotatingSalt(firstLevelHashIdentity.id);
229245
final byte[] advertisingId = getAdvertisingId(firstLevelHashIdentity, rotatingSalt.currentSalt());
230246
final byte[] previousAdvertisingId = getPreviousAdvertisingId(firstLevelHashIdentity, rotatingSalt, asOf);
@@ -262,17 +278,16 @@ private long getRefreshFrom(SaltEntry rotatingSalt, Instant asOf) {
262278
return refreshFrom;
263279
}
264280

265-
private IdentityTokens generateIdentity(PublisherIdentity publisherIdentity, UserIdentity firstLevelHashIdentity, Duration refreshIdentityAfter, Duration refreshExpiresAfter, Duration identityExpiresAfter, IdentityEnvironment env) {
281+
private IdentityTokens generateIdentity(PublisherIdentity publisherIdentity, UserIdentity firstLevelHashIdentity) {
266282
final Instant nowUtc = EncodingUtils.NowUTCMillis(this.clock);
267-
268-
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, nowUtc, env);
283+
final MappedIdentity mappedIdentity = getMappedIdentity(firstLevelHashIdentity, nowUtc);
269284
final UserIdentity advertisingIdentity = new UserIdentity(firstLevelHashIdentity.identityScope, firstLevelHashIdentity.identityType,
270285
mappedIdentity.advertisingId, firstLevelHashIdentity.privacyBits, firstLevelHashIdentity.establishedAt, nowUtc);
271286

272287
return this.encoder.encode(
273-
this.createAdvertisingToken(publisherIdentity, advertisingIdentity, nowUtc, identityExpiresAfter),
274-
this.createRefreshToken(publisherIdentity, firstLevelHashIdentity, nowUtc, refreshExpiresAfter),
275-
nowUtc.plusMillis(refreshIdentityAfter.toMillis()),
288+
this.createAdvertisingToken(publisherIdentity, advertisingIdentity, nowUtc, runtimeConfig.getIdentityTokenExpires()),
289+
this.createRefreshToken(publisherIdentity, firstLevelHashIdentity, nowUtc, runtimeConfig.getRefreshTokenExpires()),
290+
nowUtc.plusMillis(runtimeConfig.getRefreshIdentityTokenExpires().toMillis()),
276291
nowUtc
277292
);
278293
}

src/main/java/com/uid2/operator/store/RuntimeConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
99
import com.uid2.operator.model.IdentityEnvironment;
1010

11+
import java.time.Duration;
12+
1113
@JsonDeserialize(builder = RuntimeConfig.Builder.class)
1214
public class RuntimeConfig {
1315
private final Integer identityTokenExpiresAfterSeconds;
@@ -18,6 +20,18 @@ public class RuntimeConfig {
1820
private final Integer maxSharingLifetimeSeconds;
1921
private final IdentityEnvironment identityEnvironment;
2022

23+
public Duration getIdentityTokenExpires() {
24+
return Duration.ofSeconds(identityTokenExpiresAfterSeconds);
25+
}
26+
27+
public Duration getRefreshTokenExpires() {
28+
return Duration.ofSeconds(refreshTokenExpiresAfterSeconds);
29+
}
30+
31+
public Duration getRefreshIdentityTokenExpires() {
32+
return Duration.ofSeconds(refreshIdentityTokenAfterSeconds);
33+
}
34+
2135
public Integer getIdentityTokenExpiresAfterSeconds() {
2236
return identityTokenExpiresAfterSeconds;
2337
}

0 commit comments

Comments
 (0)