-
Notifications
You must be signed in to change notification settings - Fork 6
sch-UID2-5851 migration from salt to key rotation #567
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
Merged
sophia-chen-ttd
merged 24 commits into
main
from
sch-UID2-5851-migration-to-key-rotation
Aug 29, 2025
Merged
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 1a95fdf
simplified key generator implementation
sophia-chen-ttd 8101bf1
added logs for key bucket count
sophia-chen-ttd 9b6ad96
updated salt bucket format log to include salt count
sophia-chen-ttd 079eac4
updated variable names
sophia-chen-ttd 715f418
updated comment for logging bucket migration
sophia-chen-ttd dfcdd37
logged count for previous key and previous salt
sophia-chen-ttd 1e5b6bb
cleaned up code
sophia-chen-ttd 221aad8
updated currentkey variable names
sophia-chen-ttd 070104f
updated to store nextKeyId instead of lastKeyId
sophia-chen-ttd 82de10b
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd 6c94276
minor fix to keySalt naming
sophia-chen-ttd 534b8be
updated shared version
sophia-chen-ttd 7d812aa
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd 260a9ca
Merge branch 'main' into sch-UID2-5851-migration-to-key-rotation
sophia-chen-ttd 7e80346
[CI Pipeline] Released Snapshot version: 6.10.18-alpha-213-SNAPSHOT
c51f67b
Merge branch 'sch-UID2-5851-migration-to-key-rotation' into sch-UID2-…
sophia-chen-ttd c6d71c9
updated logs for easier dashboard visualisation
sophia-chen-ttd 62c5270
cleaned up file
sophia-chen-ttd adac568
added in salts sample file
sophia-chen-ttd d3f1587
Merge pull request #568 from IABTechLab/sch-UID2-5853-metrics-and-das…
sophia-chen-ttd dd15793
[CI Pipeline] Released Snapshot version: 6.10.19-alpha-214-SNAPSHOT
8d09585
restored admin version
sophia-chen-ttd 2454eca
restored admin version
sophia-chen-ttd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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 | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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