-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEncryptionKeyService.java
More file actions
682 lines (570 loc) · 28.3 KB
/
EncryptionKeyService.java
File metadata and controls
682 lines (570 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package com.uid2.admin.vertx.service;
import com.uid2.admin.auth.AdminAuthMiddleware;
import com.uid2.admin.auth.AdminKeyset;
import com.uid2.admin.secret.IEncryptionKeyManager;
import com.uid2.admin.secret.IKeysetKeyManager;
import com.uid2.admin.store.Clock;
import com.uid2.admin.store.reader.RotatingAdminKeysetStore;
import com.uid2.admin.store.writer.AdminKeysetWriter;
import com.uid2.admin.store.writer.EncryptionKeyStoreWriter;
import com.uid2.admin.store.writer.KeysetKeyStoreWriter;
import com.uid2.admin.util.MaxKeyUtil;
import com.uid2.admin.vertx.RequestUtil;
import com.uid2.admin.vertx.ResponseUtil;
import com.uid2.admin.vertx.WriteLock;
import com.uid2.shared.Const;
import com.uid2.shared.audit.AuditParams;
import com.uid2.shared.auth.Role;
import com.uid2.shared.model.EncryptionKey;
import com.uid2.shared.model.KeysetKey;
import com.uid2.shared.model.SiteUtil;
import com.uid2.shared.secret.IKeyGenerator;
import com.uid2.shared.store.reader.RotatingKeyStore;
import com.uid2.shared.store.reader.RotatingKeysetKeyStore;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static com.uid2.admin.AdminConst.enableKeysetConfigProp;
import static com.uid2.admin.managers.KeysetManager.*;
import static com.uid2.admin.vertx.Endpoints.*;
import static java.util.stream.Collectors.*;
public class EncryptionKeyService implements IService, IEncryptionKeyManager, IKeysetKeyManager {
private static class RotationResult<T> {
public Set<Integer> rotatedIds = null;
public List<T> rotatedKeys = new ArrayList<>();
}
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptionKeyService.class);
private static final String MASTER_KEY_ACTIVATES_IN_SECONDS = "master_key_activates_in_seconds";
private static final String MASTER_KEY_EXPIRES_AFTER_SECONDS = "master_key_expires_after_seconds";
private static final String MASTER_KEY_ROTATION_CUT_OFF_DAYS = "master_key_rotation_cut_off_days";
private static final String SITE_KEY_ACTIVATES_IN_SECONDS = "site_key_activates_in_seconds";
private static final String SITE_KEY_EXPIRES_AFTER_SECONDS = "site_key_expires_after_seconds";
private static final String SITE_KEY_ROTATION_CUT_OFF_DAYS = "site_key_rotation_cut_off_days";
private static final String REFRESH_KEY_ROTATION_CUT_OFF_DAYS = "refresh_key_rotation_cut_off_days";
private static final String FILTER_KEY_OVER_CUT_OFF_DAYS = "filter_key_over_cut_off_days";
private final AdminAuthMiddleware auth;
private final Clock clock;
private final WriteLock writeLock;
private final EncryptionKeyStoreWriter storeWriter;
private final KeysetKeyStoreWriter keysetKeyStoreWriter;
private final RotatingKeyStore keyProvider;
private final RotatingKeysetKeyStore keysetKeyProvider;
private final RotatingAdminKeysetStore keysetProvider;
private final AdminKeysetWriter keysetStoreWriter;
private final IKeyGenerator keyGenerator;
private final Duration masterKeyActivatesIn;
private final Duration masterKeyExpiresAfter;
private final Duration masterKeyRotationCutoffTime;
private final Duration siteKeyActivatesIn;
private final Duration siteKeyExpiresAfter;
private final Duration siteKeyRotationCutOffTime;
private final Duration refreshKeyRotationCutOffTime;
private final boolean filterKeyOverCutOffTime;
private final boolean enableKeysets;
public EncryptionKeyService(JsonObject config,
AdminAuthMiddleware auth,
WriteLock writeLock,
EncryptionKeyStoreWriter storeWriter,
KeysetKeyStoreWriter keysetKeyStoreWriter,
RotatingKeyStore keyProvider,
RotatingKeysetKeyStore keysetKeyProvider,
RotatingAdminKeysetStore keysetProvider,
AdminKeysetWriter keysetStoreWriter,
IKeyGenerator keyGenerator,
Clock clock) {
this.auth = auth;
this.writeLock = writeLock;
this.storeWriter = storeWriter;
this.keysetKeyStoreWriter = keysetKeyStoreWriter;
this.keyProvider = keyProvider;
this.keysetKeyProvider = keysetKeyProvider;
this.keysetStoreWriter = keysetStoreWriter;
this.keysetProvider = keysetProvider;
this.keyGenerator = keyGenerator;
this.clock = clock;
masterKeyActivatesIn = Duration.ofSeconds(config.getInteger(MASTER_KEY_ACTIVATES_IN_SECONDS));
masterKeyExpiresAfter = Duration.ofSeconds(config.getInteger(MASTER_KEY_EXPIRES_AFTER_SECONDS));
masterKeyRotationCutoffTime = Duration.ofDays(config.getInteger(MASTER_KEY_ROTATION_CUT_OFF_DAYS, 37));
siteKeyActivatesIn = Duration.ofSeconds(config.getInteger(SITE_KEY_ACTIVATES_IN_SECONDS));
siteKeyExpiresAfter = Duration.ofSeconds(config.getInteger(SITE_KEY_EXPIRES_AFTER_SECONDS));
siteKeyRotationCutOffTime = Duration.ofDays(config.getInteger(SITE_KEY_ROTATION_CUT_OFF_DAYS, 37));
refreshKeyRotationCutOffTime = Duration.ofDays(config.getInteger(REFRESH_KEY_ROTATION_CUT_OFF_DAYS, 37));
filterKeyOverCutOffTime = config.getBoolean(FILTER_KEY_OVER_CUT_OFF_DAYS, false);
if (masterKeyActivatesIn.compareTo(masterKeyExpiresAfter) >= 0) {
throw new IllegalStateException(MASTER_KEY_ACTIVATES_IN_SECONDS + " must be greater than " + MASTER_KEY_EXPIRES_AFTER_SECONDS);
}
if (siteKeyActivatesIn.compareTo(siteKeyExpiresAfter) >= 0) {
throw new IllegalStateException(SITE_KEY_ACTIVATES_IN_SECONDS + " must be greater than " + SITE_KEY_EXPIRES_AFTER_SECONDS);
}
enableKeysets = config.getBoolean(enableKeysetConfigProp);
}
@Override
public void setupRoutes(Router router) {
router.get(API_KEY_LIST.toString()).handler(
auth.handle(this::handleKeyList, Role.MAINTAINER));
if(enableKeysets) {
router.get(API_KEY_LIST_KEYSET_KEYS.toString()).handler(
auth.handle(this::handleKeysetKeyList, Role.MAINTAINER));
}
router.post(API_KEY_REWRITE_METADATA.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRewriteMetadata(ctx);
}
}, Role.PRIVILEGED));
router.post(API_KEY_ROTATE_MASTER.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRotateMasterKey(ctx);
}
}, Role.MAINTAINER, Role.SECRET_ROTATION));
router.post(API_KEY_ADD.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleAddSiteKey(ctx);
}
}, new AuditParams(List.of("site_id", "activates_in_seconds"), Collections.emptyList()), Role.MAINTAINER));
router.post(API_KEY_ROTATE_SITE.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRotateSiteKey(ctx);
}
}, new AuditParams(List.of("site_id"), Collections.emptyList()), Role.MAINTAINER));
if(enableKeysets) {
router.post(API_KEY_ROTATE_KEYSET_KEY.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRotateKeysetKey(ctx);
}
}, new AuditParams(List.of("keyset_id"), Collections.emptyList()), Role.MAINTAINER));
}
router.post(API_KEY_ROTATE_ALL_SITES.toString()).blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRotateAllSiteKeys(ctx);
}
}, Role.MAINTAINER, Role.SECRET_ROTATION));
}
private void handleRewriteMetadata(RoutingContext rc) {
try {
storeWriter.rewriteMeta();
rc.response().end("OK");
} catch (Exception e) {
LOGGER.error("Could not rewrite metadata", e);
rc.fail(500, e);
}
}
@Override
public EncryptionKey addSiteKey(int siteId) throws Exception {
return addSiteKey(siteId, siteKeyActivatesIn);
}
private EncryptionKey addSiteKey(int siteId, Duration activatesIn) throws Exception {
// force refresh manually
this.keyProvider.loadContent();
return addSiteKeys(Arrays.asList(siteId), activatesIn, siteKeyExpiresAfter, false).get(0);
}
@Override
public KeysetKey addKeysetKey(int keysetId) throws Exception {
loadKeysetKeys();
return addKeysetKeys(Arrays.asList(keysetId), siteKeyActivatesIn, siteKeyExpiresAfter, false).get(0);
}
public void createKeysetKeys() throws Exception {
loadAllContent();
final List<EncryptionKey> encryptionKeys = this.keyProvider.getSnapshot().getActiveKeySet();
List<EncryptionKey> addKeys = new ArrayList<>();
for (EncryptionKey key: encryptionKeys) {
KeysetKey keysetKey = this.keysetKeyProvider.getSnapshot().getKey(key.getId());
if(keysetKey == null) {
addKeys.add(key);
}
}
catchUpKeysetKeys(addKeys, false, this.keyProvider.getMetadata().getInteger("max_key_id"));
}
private void handleKeyList(RoutingContext rc) {
try {
final JsonArray ja = new JsonArray();
this.keyProvider.getSnapshot().getActiveKeySet().stream()
.sorted(Comparator.comparingInt(EncryptionKey::getSiteId).thenComparing(EncryptionKey::getActivates))
.forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleKeysetKeyList(RoutingContext rc) {
try {
final JsonArray ja = new JsonArray();
this.keysetKeyProvider.getSnapshot().getAllKeysetKeys().stream()
.sorted(Comparator.comparingInt(KeysetKey::getKeysetId).thenComparing(KeysetKey::getActivates))
.forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleRotateMasterKey(RoutingContext rc) {
try {
final RotationResult<EncryptionKey> masterKeyResult = rotateKeys(rc, masterKeyActivatesIn, masterKeyExpiresAfter,
s -> s == Const.Data.MasterKeySiteId || s == Const.Data.RefreshKeySiteId);
final JsonArray ja = new JsonArray();
masterKeyResult.rotatedKeys.stream().forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleAddSiteKey(RoutingContext rc) {
final Optional<Integer> siteIdOpt = RequestUtil.getSiteId(rc, "site_id");
if (!siteIdOpt.isPresent()) {
return;
}
final int siteId = siteIdOpt.get();
if (!SiteUtil.isValidSiteId(siteId)) {
ResponseUtil.error(rc, 400, "must specify a valid site id");
return;
}
final boolean siteKeyExists = this.keyProvider.getSnapshot()
.getActiveKeySet()
.stream()
.anyMatch(key -> key.getSiteId() == siteId);
if (siteKeyExists) {
ResponseUtil.error(rc, 400, "Key already exists for specified site id: " + siteId);
return;
}
final Optional<String> activatesQueryParam = rc.queryParam("activates_in_seconds")
.stream()
.findFirst();
Optional<Duration> activatesInSeconds = Optional.empty();
if (activatesQueryParam.isPresent() && !activatesQueryParam.get().isEmpty()) {
final long seconds;
try {
seconds = Long.parseUnsignedLong(activatesQueryParam.get());
} catch (NumberFormatException e) {
ResponseUtil.error(rc, 400, "activates_in_seconds must be a non-negative number of seconds");
return;
}
activatesInSeconds = Optional.of(Duration.ofSeconds(seconds));
}
final EncryptionKey siteKey;
try {
siteKey = addSiteKey(siteId, activatesInSeconds.orElse(siteKeyActivatesIn));
} catch (Exception e) {
rc.fail(500, e);
return;
}
final JsonObject json = toJson(siteKey);
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(json.encode());
}
private void handleRotateSiteKey(RoutingContext rc) {
try {
final Optional<Integer> siteIdOpt = RequestUtil.getSiteId(rc, "site_id");
if (!siteIdOpt.isPresent()) return;
final int siteId = siteIdOpt.get();
if (siteId != Const.Data.AdvertisingTokenSiteId && !SiteUtil.isValidSiteId(siteId)) {
ResponseUtil.error(rc, 400, "must specify a valid site id");
return;
}
final RotationResult<EncryptionKey> result = rotateKeys(rc, siteKeyActivatesIn, siteKeyExpiresAfter, s -> s == siteId);
if (result == null) {
return;
} else if (!result.rotatedIds.contains(siteId)) {
ResponseUtil.error(rc, 404, "No keys found for the specified site id: " + siteId);
return;
}
final JsonArray ja = new JsonArray();
result.rotatedKeys.stream().forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleRotateKeysetKey(RoutingContext rc) {
try {
final Optional<Integer> keysetIdOpt = RequestUtil.getKeysetId(rc, "keyset_id");
if(!keysetIdOpt.isPresent()) return;
final int keysetId = keysetIdOpt.get();
//TODO how to check for valid keyset id
final RotationResult<KeysetKey> result = rotateKeysetKeys(rc, siteKeyActivatesIn, siteKeyExpiresAfter, s -> s == keysetId);
if (result == null) {
return;
} else if (!result.rotatedIds.contains(keysetId)) {
ResponseUtil.error(rc, 404, "No keys found for the keyset id: " + keysetId);
return;
}
final JsonArray ja = new JsonArray();
result.rotatedKeys.stream().forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleRotateAllSiteKeys(RoutingContext rc) {
try {
final RotationResult<EncryptionKey> result = rotateKeys(rc, siteKeyActivatesIn, siteKeyExpiresAfter, s -> SiteUtil.isValidSiteId(s) || s == Const.Data.AdvertisingTokenSiteId);
if (result == null) {
return;
}
final JsonArray ja = new JsonArray();
result.rotatedKeys.stream().forEachOrdered(k -> ja.add(toJson(k)));
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private RotationResult<EncryptionKey> rotateKeys(RoutingContext rc, Duration activatesIn, Duration expiresAfter, Predicate<Integer> siteSelector)
throws Exception {
final Duration minAge = RequestUtil.getDuration(rc, "min_age_seconds");
if (minAge == null) return null;
final Optional<Boolean> force = RequestUtil.getBoolean(rc, "force", false);
if (!force.isPresent()) return null;
return rotateKeys(siteSelector, minAge, activatesIn, expiresAfter, force.get());
}
private RotationResult<KeysetKey> rotateKeysetKeys(RoutingContext rc, Duration activatesIn, Duration expiresAfter, Predicate<Integer> siteSelector)
throws Exception {
final Duration minAge = RequestUtil.getDuration(rc, "min_age_seconds");
if (minAge == null) return null;
final Optional<Boolean> force = RequestUtil.getBoolean(rc, "force", false);
if (!force.isPresent()) return null;
return rotateKeysetKeys(siteSelector, minAge, activatesIn, expiresAfter, force.get());
}
private RotationResult<EncryptionKey> rotateKeys(Predicate<Integer> siteSelector, Duration minAge, Duration activatesIn, Duration expiresAfter, boolean force)
throws Exception {
RotationResult<EncryptionKey> result = new RotationResult();
// force refresh manually
loadAllContent();
final List<EncryptionKey> allKeys = this.keyProvider.getSnapshot().getActiveKeySet();
// report back which sites were considered
result.rotatedIds = allKeys.stream()
.map(k -> k.getSiteId())
.filter(s -> siteSelector.test(s))
.collect(Collectors.toSet());
final Instant now = clock.now();
final Instant activatesThreshold = now.minusSeconds(minAge.getSeconds());
// within the selected sites, find keys with max activation time
// and then select those which are old enough to be rotated
// from then on we only care about sites
List<Integer> siteIds = allKeys.stream()
.filter(k -> siteSelector.test(k.getSiteId()))
.collect(groupingBy(EncryptionKey::getSiteId, maxBy(Comparator.comparing(EncryptionKey::getActivates))))
.values().stream()
.filter(k -> k.isPresent())
.map(k -> k.get())
.filter(k -> force || k.getActivates().isBefore(activatesThreshold))
.map(k -> k.getSiteId())
.collect(Collectors.toList());
if (siteIds.isEmpty()) {
return result;
}
result.rotatedKeys = addSiteKeys(siteIds, activatesIn, expiresAfter, true);
return result;
}
private RotationResult<KeysetKey> rotateKeysetKeys(Predicate<Integer> siteSelector, Duration minAge, Duration activatesIn, Duration expiresAfter, boolean force)
throws Exception {
RotationResult<KeysetKey> result = new RotationResult();
loadAllContent();
final List<KeysetKey> keysetKeys = this.keysetKeyProvider.getSnapshot().getAllKeysetKeys();
result.rotatedIds = keysetKeys.stream()
.map(KeysetKey::getKeysetId)
.filter(siteSelector::test)
.collect(toSet());
final Instant now = clock.now();
final Instant activatesThreshold = now.minusSeconds(minAge.getSeconds());
List<Integer> keysetIds = keysetKeys.stream()
.filter(k -> siteSelector.test(k.getKeysetId()))
.collect(groupingBy(KeysetKey::getKeysetId, maxBy(Comparator.comparing(KeysetKey::getActivates))))
.values().stream()
.filter(Optional::isPresent)
.map(Optional::get)
.filter(k -> force || k.getActivates().isBefore(activatesThreshold))
.map(KeysetKey::getKeysetId)
.collect(toList());
if (keysetIds.isEmpty()) {
return result;
}
result.rotatedKeys = addKeysetKeys(keysetIds, activatesIn, expiresAfter, true);
return result;
}
private List<EncryptionKey> addSiteKeys(Iterable<Integer> siteIds, Duration activatesIn, Duration expiresAfter, boolean isDuringRotation)
throws Exception {
final Instant now = clock.now();
final List<EncryptionKey> keys = this.keyProvider.getSnapshot().getActiveKeySet().stream()
.sorted(Comparator.comparingInt(EncryptionKey::getId))
.filter(k -> isWithinCutOffTime(k, now, isDuringRotation))
.collect(Collectors.toList());
int maxKeyId = MaxKeyUtil.getMaxKeyId(this.keyProvider.getSnapshot().getActiveKeySet(),
this.keyProvider.getMetadata().getInteger("max_key_id"));
final List<EncryptionKey> addedKeys = new ArrayList<>();
for (Integer siteId : siteIds) {
++maxKeyId;
final byte[] secret = keyGenerator.generateRandomKey(32);
final Instant created = now;
final Instant activates = created.plusSeconds(activatesIn.getSeconds());
final Instant expires = activates.plusSeconds(expiresAfter.getSeconds());
final EncryptionKey key = new EncryptionKey(maxKeyId, secret, created, activates, expires, siteId);
keys.add(key);
addedKeys.add(key);
}
storeWriter.upload(keys, maxKeyId);
catchUpKeysetKeys(addedKeys, isDuringRotation, maxKeyId);
return addedKeys;
}
private void catchUpKeys(Iterable<KeysetKey> missingKeys, boolean isDuringRotation, int maxKeyId)
throws Exception {
final Instant now = clock.now();
final List<EncryptionKey> keys = this.keyProvider.getSnapshot().getActiveKeySet().stream()
.sorted(Comparator.comparingInt(EncryptionKey::getId))
.filter(k -> isWithinCutOffTime(k, now, isDuringRotation))
.collect(Collectors.toList());
final List<EncryptionKey> addedKeys = new ArrayList<>();
for (KeysetKey key : missingKeys) {
final int siteId = getSiteId(key.getKeysetId());
final EncryptionKey newKey = new EncryptionKey(key.getId(), key.getKeyBytes(), key.getCreated(), key.getActivates(), key.getExpires(), siteId);
keys.add(newKey);
addedKeys.add(newKey);
}
storeWriter.upload(keys, maxKeyId);
}
private int getOrCreateKeysetId(int siteId)
throws Exception {
Map<Integer, AdminKeyset> currentKeysets = keysetProvider.getSnapshot().getAllKeysets();
AdminKeyset keyset = lookUpKeyset(siteId, currentKeysets);
if(keyset == null) {
int newKeysetId = getMaxKeyset(currentKeysets)+1;
if(siteId == Const.Data.MasterKeySiteId) {
newKeysetId = Const.Data.MasterKeysetId;
}
else if(siteId == Const.Data.RefreshKeySiteId) {
newKeysetId = Const.Data.RefreshKeysetId;
}
else if(siteId == Const.Data.AdvertisingTokenSiteId) {
newKeysetId = Const.Data.FallbackPublisherKeysetId;
}
keyset = createKeysetSharedWithDsps(siteId, newKeysetId);
currentKeysets.put(newKeysetId, keyset);
keysetStoreWriter.upload(currentKeysets, null);
}
return keyset.getKeysetId();
}
private int getSiteId(int keysetId) {
Map<Integer, AdminKeyset> currentKeysets = keysetProvider.getSnapshot().getAllKeysets();
return currentKeysets.get(keysetId).getSiteId();
}
private List<KeysetKey> addKeysetKeys(Iterable<Integer> keysetIds, Duration activatesIn, Duration expiresAfter, boolean isDuringRotation)
throws Exception {
final Instant now = clock.now();
final List<KeysetKey> keysetKeys = this.keysetKeyProvider.getSnapshot().getAllKeysetKeys();
final List<KeysetKey> keys = keysetKeys.stream()
.sorted(Comparator.comparingInt(KeysetKey::getId))
.filter(k -> isWithinCutOffTime(k, now, isDuringRotation))
.collect(Collectors.toList());
int maxKeyId = MaxKeyUtil.getMaxKeysetKeyId(keysetKeys, this.keysetKeyProvider.getMetadata().getInteger("max_key_id"));
final List<KeysetKey> addedKeys = new ArrayList<>();
for (Integer keysetId : keysetIds) {
++maxKeyId;
final byte[] secret = keyGenerator.generateRandomKey(32);
final Instant created = now;
final boolean isAddingFirstKeyForKeyset = (!isDuringRotation && keysetKeys.stream().noneMatch(key -> key.getKeysetId() == keysetId));
final Instant activates = isAddingFirstKeyForKeyset ? created : created.plusSeconds(activatesIn.getSeconds());
final Instant expires = activates.plusSeconds(expiresAfter.getSeconds());
final KeysetKey key = new KeysetKey(maxKeyId, secret, created, activates, expires, keysetId);
keys.add(key);
addedKeys.add(key);
}
keysetKeyStoreWriter.upload(keys, maxKeyId);
catchUpKeys(addedKeys, isDuringRotation, maxKeyId);
return addedKeys;
}
private void catchUpKeysetKeys(Iterable<EncryptionKey> missingKeys, boolean isDuringRotation, int maxKeyId)
throws Exception {
if(!enableKeysets) return;
final Instant now = clock.now();
final List<KeysetKey> keys = this.keysetKeyProvider.getSnapshot().getAllKeysetKeys().stream()
.sorted(Comparator.comparingInt(KeysetKey::getId))
.filter(k -> isWithinCutOffTime(k, now, isDuringRotation))
.collect(Collectors.toList());
final List<KeysetKey> addedKeys = new ArrayList<>();
for (EncryptionKey key : missingKeys) {
final int keysetId = getOrCreateKeysetId(key.getSiteId());
final KeysetKey newKey = new KeysetKey(key.getId(), key.getKeyBytes(), key.getCreated(), key.getActivates(), key.getExpires(), keysetId);
keys.add(newKey);
addedKeys.add(newKey);
}
keysetKeyStoreWriter.upload(keys, maxKeyId);
}
private JsonObject toJson(EncryptionKey key) {
JsonObject jo = new JsonObject();
jo.put("id", key.getId());
jo.put("site_id", key.getSiteId());
jo.put("created", key.getCreated().toEpochMilli());
jo.put("activates", key.getActivates().toEpochMilli());
jo.put("expires", key.getExpires().toEpochMilli());
return jo;
}
private JsonObject toJson(KeysetKey key) {
JsonObject jo = new JsonObject();
jo.put("id", key.getId());
jo.put("keyset_id", key.getKeysetId());
jo.put("created", key.getCreated().toEpochMilli());
jo.put("activates", key.getActivates().toEpochMilli());
jo.put("expires", key.getExpires().toEpochMilli());
return jo;
}
private boolean isWithinCutOffTime(EncryptionKey key, Instant now, boolean duringRotation) {
if (!(filterKeyOverCutOffTime && duringRotation)) {
return true;
}
int siteId = key.getSiteId();
Duration cutoffTime;
switch (siteId) {
case Const.Data.MasterKeySiteId:
cutoffTime = masterKeyRotationCutoffTime;
break;
case Const.Data.RefreshKeySiteId:
cutoffTime = refreshKeyRotationCutOffTime;
break;
default:
cutoffTime = siteKeyRotationCutOffTime;
break;
}
return now.compareTo(key.getExpires().plus(cutoffTime.toDays(), ChronoUnit.DAYS)) < 0;
}
private boolean isWithinCutOffTime(KeysetKey key, Instant now, boolean duringRotation) {
if (!(filterKeyOverCutOffTime && duringRotation)) {
return true;
}
return now.compareTo(key.getExpires().plus(siteKeyRotationCutOffTime.toDays(), ChronoUnit.DAYS)) < 0;
}
private void loadAllContent() throws Exception {
this.keyProvider.loadContent();
loadKeysets();
loadKeysetKeys();
}
private void loadKeysetKeys() throws Exception {
if(enableKeysets){
this.keysetKeyProvider.loadContent();
}
}
private void loadKeysets() throws Exception {
if(enableKeysets) {
this.keysetProvider.loadContent();
}
}
}