Skip to content

Commit d3f1587

Browse files
Merge pull request #568 from IABTechLab/sch-UID2-5853-metrics-and-dashboard-for-key-rotation
sch-UID2-5853 Added logs for key bucket count in salt rotation
2 parents 7e80346 + adac568 commit d3f1587

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/main/java/com/uid2/admin/salt/SaltRotation.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public Result rotateSalts(
6262
logSaltAges("refreshable-salts", targetDate, refreshableSalts);
6363
logSaltAges("rotated-salts", targetDate, saltsToRotate);
6464
logSaltAges("total-salts", targetDate, Arrays.asList(postRotationSalts));
65+
logBucketFormatCount(targetDate, postRotationSalts);
6566

6667
var nextSnapshot = new SaltSnapshot(
6768
nextEffective,
@@ -252,6 +253,24 @@ private void logSaltAges(String saltCountType, TargetDate targetDate, Collection
252253
}
253254
}
254255

256+
257+
/** Logging to monitor migration of buckets from salts (old format - v2/v3) to encryption keys (new format - v4) **/
258+
private void logBucketFormatCount(TargetDate targetDate, SaltEntry[] postRotationBuckets) {
259+
int totalKeys = 0, totalSalts = 0, totalPreviousKeys = 0, totalPreviousSalts = 0;
260+
261+
for (SaltEntry bucket : postRotationBuckets) {
262+
if (bucket.currentKeySalt() != null) totalKeys++;
263+
if (bucket.currentSalt() != null) totalSalts++;
264+
if (bucket.previousKeySalt() != null) totalPreviousKeys++;
265+
if (bucket.previousSalt() != null) totalPreviousSalts++;
266+
}
267+
268+
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-current-key-buckets", totalKeys);
269+
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-current-salt-buckets", totalSalts);
270+
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-previous-key-buckets", totalPreviousKeys);
271+
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-previous-salt-buckets", totalPreviousSalts);
272+
}
273+
255274
@Getter
256275
public static final class Result {
257276
private final SaltSnapshot snapshot; // can be null if new snapshot is not needed

src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.uid2.admin.store.version.VersionGenerator;
55
import com.uid2.shared.cloud.CloudStorageException;
66
import com.uid2.shared.cloud.TaggableCloudStorage;
7-
import com.uid2.shared.model.SaltEntry;
87
import com.uid2.shared.store.CloudPath;
98
import com.uid2.shared.store.salt.RotatingSaltProvider;
109
import io.vertx.core.json.JsonArray;

src/test/java/com/uid2/admin/salt/SaltRotationTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void testLogFewSaltAgesOnRotation() throws Exception {
388388
var minAges = new Duration[]{Duration.ofDays(30), Duration.ofDays(60)};
389389
saltRotation.rotateSalts(lastSnapshot, minAges, 0.4, targetDate());
390390

391-
var actual = appender.list.stream().map(Object::toString).collect(Collectors.toSet());
391+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("salt_count_type") || s.contains("Salt rotation complete")).collect(Collectors.toSet());
392392
assertThat(actual).isEqualTo(expected);
393393
}
394394

@@ -428,7 +428,7 @@ void testLogManySaltAgesOnRotation() throws Exception {
428428
var minAges = new Duration[]{Duration.ofDays(30), Duration.ofDays(60)};
429429
saltRotation.rotateSalts(lastSnapshot, minAges, 0.2, targetDate());
430430

431-
var actual = appender.list.stream().map(Object::toString).collect(Collectors.toSet());
431+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("salt_count_type") || s.contains("Salt rotation complete")).collect(Collectors.toSet());
432432
assertThat(actual).isEqualTo(expected);
433433
}
434434

@@ -656,4 +656,38 @@ void testKeyRotationKeyToSaltRotation() throws Exception {
656656
assertThat(buckets[0].currentKeySalt()).isNull();
657657
assertThat(buckets[0].previousKeySalt()).isEqualTo(new SaltEntry.KeyMaterial(0, "keyKey1", "keySalt1"));
658658
}
659+
660+
@ParameterizedTest
661+
@CsvSource({
662+
"true, 3, 1",
663+
"false, 1, 3"
664+
})
665+
void testKeyRotationLogBucketFormat(boolean v4Enabled, int expectedTotalKeyBuckets, int expectedTotalSaltBuckets) throws Exception {
666+
saltRotation = new SaltRotation(keyGenerator, JsonObject.of(AdminConst.ENABLE_V4_RAW_UID, v4Enabled));
667+
when(keyGenerator.generateRandomKeyString(anyInt())).thenReturn("random-key-string");
668+
669+
final Duration[] minAges = {
670+
Duration.ofDays(30)
671+
};
672+
673+
var willRefresh = targetDate();
674+
var willNotRefresh = targetDate().plusDays(30);
675+
var lastSnapshot = SaltSnapshotBuilder.start()
676+
.entries(SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willRefresh).currentSalt(),
677+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willNotRefresh).currentSalt(),
678+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willRefresh).currentKeySalt(1),
679+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willNotRefresh).currentKeySalt(2))
680+
.build();
681+
682+
saltRotation.rotateSalts(lastSnapshot, minAges, 1, targetDate());
683+
684+
var expected = Set.of(
685+
String.format("[INFO] UID bucket format: target_date=2025-01-01 bucket_format=total-current-key-buckets bucket_count=%d", expectedTotalKeyBuckets),
686+
String.format("[INFO] UID bucket format: target_date=2025-01-01 bucket_format=total-current-salt-buckets bucket_count=%d", expectedTotalSaltBuckets),
687+
String.format("[INFO] UID bucket format: target_date=2025-01-01 bucket_format=total-previous-key-buckets bucket_count=%d", 1),
688+
String.format("[INFO] UID bucket format: target_date=2025-01-01 bucket_format=total-previous-salt-buckets bucket_count=%d", 1)
689+
);
690+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("UID bucket format")).collect(Collectors.toSet());
691+
assertThat(actual).isEqualTo(expected);
692+
}
659693
}

0 commit comments

Comments
 (0)