Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a7a2c04
implemented v4 key rotation behind feature flag
sophia-chen-ttd Aug 22, 2025
1a95fdf
simplified key generator implementation
sophia-chen-ttd Aug 22, 2025
8101bf1
added logs for key bucket count
sophia-chen-ttd Aug 25, 2025
9b6ad96
updated salt bucket format log to include salt count
sophia-chen-ttd Aug 25, 2025
079eac4
updated variable names
sophia-chen-ttd Aug 25, 2025
715f418
updated comment for logging bucket migration
sophia-chen-ttd Aug 25, 2025
dfcdd37
logged count for previous key and previous salt
sophia-chen-ttd Aug 25, 2025
1e5b6bb
cleaned up code
sophia-chen-ttd Aug 26, 2025
221aad8
updated currentkey variable names
sophia-chen-ttd Aug 26, 2025
070104f
updated to store nextKeyId instead of lastKeyId
sophia-chen-ttd Aug 26, 2025
82de10b
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd Aug 26, 2025
6c94276
minor fix to keySalt naming
sophia-chen-ttd Aug 27, 2025
534b8be
updated shared version
sophia-chen-ttd Aug 27, 2025
7d812aa
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd Aug 27, 2025
260a9ca
Merge branch 'main' into sch-UID2-5851-migration-to-key-rotation
sophia-chen-ttd Aug 27, 2025
7e80346
[CI Pipeline] Released Snapshot version: 6.10.18-alpha-213-SNAPSHOT
Aug 27, 2025
c51f67b
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd Aug 28, 2025
c6d71c9
updated logs for easier dashboard visualisation
sophia-chen-ttd Aug 28, 2025
62c5270
cleaned up file
sophia-chen-ttd Aug 28, 2025
adac568
added in salts sample file
sophia-chen-ttd Aug 28, 2025
d3f1587
Merge pull request #568 from IABTechLab/sch-UID2-5853-metrics-and-das…
sophia-chen-ttd Aug 28, 2025
dd15793
[CI Pipeline] Released Snapshot version: 6.10.19-alpha-214-SNAPSHOT
Aug 28, 2025
8d09585
restored admin version
sophia-chen-ttd Aug 29, 2025
2454eca
restored admin version
sophia-chen-ttd Aug 29, 2025
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- check micrometer.version vertx-micrometer-metrics consumes before bumping up -->
<micrometer.version>1.12.2</micrometer.version>
<junit-jupiter.version>5.11.2</junit-jupiter.version>
<uid2-shared.version>10.9.4</uid2-shared.version>
<uid2-shared.version>11.0.0</uid2-shared.version>
<okta-jwt.version>0.5.10</okta-jwt.version>
<image.version>${project.version}</image.version>
</properties>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/uid2/admin/AdminConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ private AdminConst() {
public static final String ROLE_OKTA_GROUP_MAP_MAINTAINER = "role_okta_group_map_maintainer";
public static final String ROLE_OKTA_GROUP_MAP_PRIVILEGED = "role_okta_group_map_privileged";
public static final String ROLE_OKTA_GROUP_MAP_SUPER_USER = "role_okta_group_map_super_user";
public static final String ENABLE_V4_RAW_UID = "enable_v4_raw_uid";
}
2 changes: 1 addition & 1 deletion src/main/java/com/uid2/admin/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void run() {
WriteLock writeLock = new WriteLock();
KeyHasher keyHasher = new KeyHasher();
IKeypairGenerator keypairGenerator = new SecureKeypairGenerator();
SaltRotation saltRotation = new SaltRotation(keyGenerator);
SaltRotation saltRotation = new SaltRotation(keyGenerator, config);
EncryptionKeyService encryptionKeyService = new EncryptionKeyService(
config, auth, writeLock, encryptionKeyStoreWriter, keysetKeyStoreWriter, keyProvider, keysetKeysProvider, adminKeysetProvider, adminKeysetStoreWriter, keyGenerator, clock);
KeysetManager keysetManager = new KeysetManager(
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/uid2/admin/salt/KeyIdGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.uid2.admin.salt;

import com.uid2.shared.model.SaltEntry;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Assumptions:
* - The latest assigned key ids are from the latest updated buckets
* - Key ids from these buckets will always be monotonically increasing (apart from wraparound) as they have not rotated again after last assignment
*
* Intended outcomes of KeyIdGenerator:
* - Key ids are always monotonically increasing, starting from 0
* - When the last allocated key id reaches 16777215, the next key id will wrap around to 0
* - Continuing to increment from the highest key id will result in monotonic incrementation of key ids for all newly rotated buckets
**/
public class KeyIdGenerator {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worthwhile to add a comment somewhere in this class about the assumptions of key IDs in the latest rotated buckets and the intended outcomes of this logic:

  • Key IDs from the latest buckets were the last allocated key IDs
  • These key IDs should always be consecutive
  • If the last allocated key ID contains 16777215, we should wrap around
  • Continuing to increment from the last "highest" key ID will result in monotonic incrementation of key IDs for all newly rotated buckets

Hopefully my understanding is correct, it took me a while to grasp all of these assumptions and outcomes so a comment would help future readers of this code

private static final int MAX_KEY_ID = 16777215; // 3 bytes
private final AtomicInteger nextActiveKeyId;

public KeyIdGenerator(SaltEntry[] buckets) {
this.nextActiveKeyId = new AtomicInteger(getNextActiveKeyId(buckets));
}

private static int getNextActiveKeyId(SaltEntry[] buckets) {
long lastUpdatedTimestampWithKey = Arrays.stream(buckets).filter(s -> s.currentKeySalt() != null).mapToLong(SaltEntry::lastUpdated).max().orElse(0);
if (lastUpdatedTimestampWithKey == 0) return 0;

int[] lastActiveKeyIdsSorted = Arrays.stream(buckets)
.filter(s -> s.lastUpdated() == lastUpdatedTimestampWithKey && s.currentKeySalt() != null)
.mapToInt(s -> s.currentKeySalt().id())
.sorted()
.toArray();

int highestId = lastActiveKeyIdsSorted[lastActiveKeyIdsSorted.length - 1];

int nextKeyId = highestId + 1;
if (nextKeyId <= MAX_KEY_ID) return nextKeyId;

// Wrapped case - find the last consecutive ID from 0
for (int i = 0; i < lastActiveKeyIdsSorted.length - 1; i++) {
if (lastActiveKeyIdsSorted[i + 1] - lastActiveKeyIdsSorted[i] > 1) {
return lastActiveKeyIdsSorted[i] + 1;
}
}

return 0;
}

public int getNextKeyId() {
return nextActiveKeyId.getAndUpdate(id ->
id + 1 > MAX_KEY_ID ? 0 : id + 1
);
}
}
100 changes: 82 additions & 18 deletions src/main/java/com/uid2/admin/salt/SaltRotation.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.uid2.admin.salt;

import com.uid2.admin.AdminConst;
import com.uid2.shared.model.SaltEntry;
import com.uid2.shared.secret.IKeyGenerator;

import com.uid2.shared.store.salt.ISaltProvider.ISaltSnapshot;
import com.uid2.shared.store.salt.RotatingSaltProvider.SaltSnapshot;
import io.vertx.core.json.JsonObject;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,12 +19,15 @@
public class SaltRotation {
private static final long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis();
private static final double MAX_SALT_PERCENTAGE = 0.8;
private final boolean ENABLE_V4_RAW_UID;

private final IKeyGenerator keyGenerator;

private static final Logger LOGGER = LoggerFactory.getLogger(SaltRotation.class);

public SaltRotation(IKeyGenerator keyGenerator) {
public SaltRotation(IKeyGenerator keyGenerator, JsonObject config) {
this.keyGenerator = keyGenerator;
this.ENABLE_V4_RAW_UID = config.getBoolean(AdminConst.ENABLE_V4_RAW_UID, false);
}

public Result rotateSalts(
Expand Down Expand Up @@ -57,6 +62,7 @@ public Result rotateSalts(
logSaltAges("refreshable-salts", targetDate, refreshableSalts);
logSaltAges("rotated-salts", targetDate, saltsToRotate);
logSaltAges("total-salts", targetDate, Arrays.asList(postRotationSalts));
logBucketFormatCount(targetDate, postRotationSalts);

var nextSnapshot = new SaltSnapshot(
nextEffective,
Expand Down Expand Up @@ -99,45 +105,85 @@ private boolean isRefreshable(TargetDate targetDate, SaltEntry salt) {
}

private SaltEntry[] rotateSalts(SaltEntry[] oldSalts, List<SaltEntry> saltsToRotate, TargetDate targetDate) throws Exception {
var keyIdGenerator = new KeyIdGenerator(oldSalts);
var saltIdsToRotate = saltsToRotate.stream().map(SaltEntry::id).collect(Collectors.toSet());

var updatedSalts = new SaltEntry[oldSalts.length];
for (int i = 0; i < oldSalts.length; i++) {
var shouldRotate = saltIdsToRotate.contains(oldSalts[i].id());
updatedSalts[i] = updateSalt(oldSalts[i], targetDate, shouldRotate);
updatedSalts[i] = updateSalt(oldSalts[i], targetDate, shouldRotate, keyIdGenerator);
}
return updatedSalts;
}

private SaltEntry updateSalt(SaltEntry oldSalt, TargetDate targetDate, boolean shouldRotate) throws Exception {
var currentSalt = shouldRotate ? this.keyGenerator.generateRandomKeyString(32) : oldSalt.currentSalt();
var lastUpdated = shouldRotate ? targetDate.asEpochMs() : oldSalt.lastUpdated();
var refreshFrom = calculateRefreshFrom(oldSalt, targetDate);
var previousSalt = calculatePreviousSalt(oldSalt, shouldRotate, targetDate);
private SaltEntry updateSalt(SaltEntry oldBucket, TargetDate targetDate, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
var lastUpdated = shouldRotate ? targetDate.asEpochMs() : oldBucket.lastUpdated();
var refreshFrom = calculateRefreshFrom(oldBucket, targetDate);
var currentSalt = calculateCurrentSalt(oldBucket, shouldRotate);
var previousSalt = calculatePreviousSalt(oldBucket, shouldRotate, targetDate);
var currentKeySalt = calculateCurrentKeySalt(oldBucket, shouldRotate, keyIdGenerator);
var previousKeySalt = calculatePreviousKeySalt(oldBucket,shouldRotate, targetDate);

return new SaltEntry(
oldSalt.id(),
oldSalt.hashedId(),
oldBucket.id(),
oldBucket.hashedId(),
lastUpdated,
currentSalt,
refreshFrom,
previousSalt,
null,
null
currentKeySalt,
previousKeySalt
);
}

private long calculateRefreshFrom(SaltEntry salt, TargetDate targetDate) {
long multiplier = targetDate.saltAgeInDays(salt) / 30 + 1;
return Instant.ofEpochMilli(salt.lastUpdated()).truncatedTo(ChronoUnit.DAYS).toEpochMilli() + (multiplier * THIRTY_DAYS_IN_MS);
private long calculateRefreshFrom(SaltEntry bucket, TargetDate targetDate) {
long multiplier = targetDate.saltAgeInDays(bucket) / 30 + 1;
return Instant.ofEpochMilli(bucket.lastUpdated()).truncatedTo(ChronoUnit.DAYS).toEpochMilli() + (multiplier * THIRTY_DAYS_IN_MS);
}

private String calculateCurrentSalt(SaltEntry bucket, boolean shouldRotate) throws Exception {
if (shouldRotate) {
if (ENABLE_V4_RAW_UID) {
return null;
}
else {
return this.keyGenerator.generateRandomKeyString(32);
}
}
return bucket.currentSalt();
}

private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, TargetDate targetDate) {
private String calculatePreviousSalt(SaltEntry bucket, boolean shouldRotate, TargetDate targetDate) {
if (shouldRotate) {
return salt.currentSalt();
return bucket.currentSalt();
}
if (targetDate.saltAgeInDays(salt) < 90) {
return salt.previousSalt();
if (targetDate.saltAgeInDays(bucket) < 90) {
return bucket.previousSalt();
}
return null;
}

private SaltEntry.KeyMaterial calculateCurrentKeySalt(SaltEntry bucket, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
if (shouldRotate) {
if (ENABLE_V4_RAW_UID) {
return new SaltEntry.KeyMaterial(
keyIdGenerator.getNextKeyId(),
this.keyGenerator.generateRandomKeyString(32),
this.keyGenerator.generateRandomKeyString(32)
);
} else {
return null;
}
}
return bucket.currentKeySalt();
}

private SaltEntry.KeyMaterial calculatePreviousKeySalt(SaltEntry bucket, boolean shouldRotate, TargetDate targetDate) {
if (shouldRotate) {
return bucket.currentKeySalt();
}
if (targetDate.saltAgeInDays(bucket) < 90) {
return bucket.previousKeySalt();
}
return null;
}
Expand Down Expand Up @@ -207,6 +253,24 @@ private void logSaltAges(String saltCountType, TargetDate targetDate, Collection
}
}


/** Logging to monitor migration of buckets from salts (old format - v2/v3) to encryption keys (new format - v4) **/
private void logBucketFormatCount(TargetDate targetDate, SaltEntry[] postRotationBuckets) {
int totalKeys = 0, totalSalts = 0, totalPreviousKeys = 0, totalPreviousSalts = 0;

for (SaltEntry bucket : postRotationBuckets) {
if (bucket.currentKeySalt() != null) totalKeys++;
if (bucket.currentSalt() != null) totalSalts++;
if (bucket.previousKeySalt() != null) totalPreviousKeys++;
if (bucket.previousSalt() != null) totalPreviousSalts++;
}

LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-current-key-buckets", totalKeys);
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-current-salt-buckets", totalSalts);
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-previous-key-buckets", totalPreviousKeys);
LOGGER.info("UID bucket format: target_date={} bucket_format={} bucket_count={}", targetDate, "total-previous-salt-buckets", totalPreviousSalts);
}

@Getter
public static final class Result {
private final SaltSnapshot snapshot; // can be null if new snapshot is not needed
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/uid2/admin/store/writer/SaltSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ private static void addLine(SaltEntry entry, StringBuilder stringBuilder) {
.append(",")
.append(lastUpdated % 1000 == 0 ? lastUpdated + 1 : lastUpdated)
.append(",")
.append(entry.currentSalt());
.append(serializeNullable(entry.currentSalt()));

stringBuilder.append(",");
stringBuilder.append(serializeNullable(entry.refreshFrom()));

stringBuilder.append(",");
stringBuilder.append(serializeNullable(entry.previousSalt()));

appendKey(stringBuilder, entry.currentKey());
appendKey(stringBuilder, entry.previousKey());
appendKey(stringBuilder, entry.currentKeySalt());
appendKey(stringBuilder, entry.previousKeySalt());

stringBuilder.append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.uid2.admin.store.version.VersionGenerator;
import com.uid2.shared.cloud.CloudStorageException;
import com.uid2.shared.cloud.TaggableCloudStorage;
import com.uid2.shared.model.SaltEntry;
import com.uid2.shared.store.CloudPath;
import com.uid2.shared.store.salt.RotatingSaltProvider;
import io.vertx.core.json.JsonArray;
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/localstack/s3/core/salts/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"location" : "salts/salts.txt.1670796729291",
"size" : 2
},{
"effective" : 1745907348982,
"expires" : 1766720293000,
"location" : "salts/salts.txt.1745907348982",
"size" : 2
"effective" : 1755648000000,
"expires" : 1756252800000,
"location" : "salts/salts.txt.1755648000000",
"size" : 1001
}
]
}
Loading