-
Notifications
You must be signed in to change notification settings - Fork 6
Logging salt age stats on rotation #475
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
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ebc73b
Logging salt ages on rotation
aulme d43b079
Refactoring and adding target-date
aulme 6229778
Refactoring
aulme b7656ba
addressing feedback
aulme 27e005b
More refactoring and fixing tests
aulme 78f7740
Rename rotatable to refreshable salts
aulme bdcb165
[CI Pipeline] Released Snapshot version: 5.22.9-alpha-165-SNAPSHOT
b356b89
Merge branch 'main' into aul-UID2-5349-logging-salt-stats
aulme 8dbf326
feedback
aulme 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| 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.RotatingSaltProvider.SaltSnapshot; | ||
| import io.vertx.core.json.JsonObject; | ||
| import lombok.Getter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.*; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class SaltRotation { | ||
| private final static long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis(); | ||
|
|
||
| private final IKeyGenerator keyGenerator; | ||
| private final boolean isRefreshFromEnabled; | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(SaltRotation.class); | ||
|
|
||
| public SaltRotation(JsonObject config, IKeyGenerator keyGenerator) { | ||
| this.keyGenerator = keyGenerator; | ||
| this.isRefreshFromEnabled = config.getBoolean(AdminConst.ENABLE_SALT_ROTATION_REFRESH_FROM, false); | ||
| } | ||
|
|
||
| public Result rotateSalts( | ||
| SaltSnapshot lastSnapshot, | ||
| Duration[] minAges, | ||
| double fraction, | ||
| TargetDate targetDate | ||
| ) throws Exception { | ||
| var preRotationSalts = lastSnapshot.getAllRotatingSalts(); | ||
| var nextEffective = targetDate.asInstant(); | ||
| var nextExpires = nextEffective.plus(7, ChronoUnit.DAYS); | ||
| if (nextEffective.equals(lastSnapshot.getEffective()) || nextEffective.isBefore(lastSnapshot.getEffective())) { | ||
| return Result.noSnapshot("cannot create a new salt snapshot with effective timestamp equal or prior to that of an existing snapshot"); | ||
| } | ||
|
|
||
| // Salts that can be rotated based on their refreshFrom being at target date | ||
| var refreshableSalts = findRefreshableSalts(preRotationSalts, targetDate); | ||
|
|
||
| var saltsToRotate = pickSaltsToRotate( | ||
| refreshableSalts, | ||
| targetDate, | ||
| minAges, | ||
| getNumSaltsToRotate(preRotationSalts, fraction) | ||
| ); | ||
|
|
||
| if (saltsToRotate.isEmpty()) { | ||
| return Result.noSnapshot("all refreshable salts are below min rotation age"); | ||
| } | ||
|
|
||
| var postRotationSalts = rotateSalts(preRotationSalts, saltsToRotate, targetDate); | ||
|
|
||
| logSaltAges("refreshable-salts", targetDate, refreshableSalts); | ||
| logSaltAges("rotated-salts", targetDate, saltsToRotate); | ||
| logSaltAges("total-salts", targetDate, Arrays.asList(postRotationSalts)); | ||
|
|
||
| var nextSnapshot = new SaltSnapshot( | ||
| nextEffective, | ||
| nextExpires, | ||
| postRotationSalts, | ||
| lastSnapshot.getFirstLevelSalt()); | ||
| return Result.fromSnapshot(nextSnapshot); | ||
| } | ||
|
|
||
| private static int getNumSaltsToRotate(SaltEntry[] preRotationSalts, double fraction) { | ||
| return (int) Math.ceil(preRotationSalts.length * fraction); | ||
| } | ||
|
|
||
| private Set<SaltEntry> findRefreshableSalts(SaltEntry[] preRotationSalts, TargetDate targetDate) { | ||
| return Arrays.stream(preRotationSalts).filter(s -> isRefreshable(targetDate, s)).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private boolean isRefreshable(TargetDate targetDate, SaltEntry salt) { | ||
| if (this.isRefreshFromEnabled) { | ||
| return salt.refreshFrom().equals(targetDate.asEpochMs()); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private SaltEntry[] rotateSalts(SaltEntry[] oldSalts, List<SaltEntry> saltsToRotate, TargetDate targetDate) throws Exception { | ||
| 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); | ||
| } | ||
| 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); | ||
|
|
||
| return new SaltEntry( | ||
| oldSalt.id(), | ||
| oldSalt.hashedId(), | ||
| lastUpdated, | ||
| currentSalt, | ||
| refreshFrom, | ||
| previousSalt, | ||
| null, | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| private long calculateRefreshFrom(SaltEntry salt, TargetDate targetDate) { | ||
| long multiplier = targetDate.saltAgeInDays(salt) / 30 + 1; | ||
| return salt.lastUpdated() + (multiplier * THIRTY_DAYS_IN_MS); | ||
| } | ||
|
|
||
| private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, TargetDate targetDate) { | ||
| if (shouldRotate) { | ||
| return salt.currentSalt(); | ||
| } | ||
| if (targetDate.saltAgeInDays(salt) < 90) { | ||
| return salt.previousSalt(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private List<SaltEntry> pickSaltsToRotate( | ||
| Set<SaltEntry> refreshableSalts, | ||
| TargetDate targetDate, | ||
| Duration[] minAges, | ||
| int numSaltsToRotate | ||
| ) { | ||
| var thresholds = Arrays.stream(minAges) | ||
| .map(minAge -> targetDate.asInstant().minusSeconds(minAge.getSeconds())) | ||
| .sorted() | ||
| .toArray(Instant[]::new); | ||
| var indexesToRotate = new ArrayList<SaltEntry>(); | ||
|
|
||
| var minLastUpdated = Instant.ofEpochMilli(0); | ||
| for (var maxLastUpdated : thresholds) { | ||
| if (indexesToRotate.size() >= numSaltsToRotate) break; | ||
|
|
||
| var maxIndexes = numSaltsToRotate - indexesToRotate.size(); | ||
| var saltsToRotate = pickSaltsToRotateInTimeWindow( | ||
| refreshableSalts, | ||
| maxIndexes, | ||
| minLastUpdated.toEpochMilli(), | ||
| maxLastUpdated.toEpochMilli() | ||
| ); | ||
| indexesToRotate.addAll(saltsToRotate); | ||
| minLastUpdated = maxLastUpdated; | ||
| } | ||
| return indexesToRotate; | ||
| } | ||
|
|
||
| private List<SaltEntry> pickSaltsToRotateInTimeWindow( | ||
| Set<SaltEntry> refreshableSalts, | ||
| int maxIndexes, | ||
| long minLastUpdated, | ||
| long maxLastUpdated | ||
| ) { | ||
| ArrayList<SaltEntry> candidateSalts = refreshableSalts.stream() | ||
| .filter(salt -> minLastUpdated <= salt.lastUpdated() && salt.lastUpdated() < maxLastUpdated) | ||
| .collect(Collectors.toCollection(ArrayList::new)); | ||
|
|
||
| if (candidateSalts.size() <= maxIndexes) { | ||
| return candidateSalts; | ||
| } | ||
|
|
||
| Collections.shuffle(candidateSalts); | ||
|
|
||
| return candidateSalts.stream().limit(maxIndexes).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private void logSaltAges(String saltCountType, TargetDate targetDate, Collection<SaltEntry> salts) { | ||
| var ages = new HashMap<Long, Long>(); // salt age to count | ||
| for (var salt : salts) { | ||
| long ageInDays = targetDate.saltAgeInDays(salt); | ||
| ages.put(ageInDays, ages.getOrDefault(ageInDays, 0L) + 1); | ||
| } | ||
|
|
||
| for (var entry : ages.entrySet()) { | ||
| LOGGER.info("salt-count-type={} target-date={} age={} salt-count={}", | ||
| saltCountType, | ||
| targetDate, | ||
| entry.getKey(), | ||
| entry.getValue() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| public static class Result { | ||
| private final SaltSnapshot snapshot; // can be null if new snapshot is not needed | ||
| private final String reason; // why you are not getting a new snapshot | ||
|
|
||
| private Result(SaltSnapshot snapshot, String reason) { | ||
| this.snapshot = snapshot; | ||
| this.reason = reason; | ||
| } | ||
|
|
||
| public boolean hasSnapshot() { | ||
| return snapshot != null; | ||
| } | ||
|
|
||
| public static Result fromSnapshot(SaltSnapshot snapshot) { | ||
| return new Result(snapshot, null); | ||
| } | ||
|
|
||
| public static Result noSnapshot(String reason) { | ||
| return new Result(null, reason); | ||
| } | ||
| } | ||
| } | ||
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,69 @@ | ||
| package com.uid2.admin.salt; | ||
|
|
||
| import com.uid2.shared.model.SaltEntry; | ||
|
|
||
| import java.time.*; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Objects; | ||
|
|
||
| public class TargetDate { | ||
| private final static long DAY_IN_MS = Duration.ofDays(1).toMillis(); | ||
|
|
||
| private final LocalDate date; | ||
| private final long epochMs; | ||
| private final Instant instant; | ||
| private final String formatted; | ||
|
|
||
| public TargetDate(LocalDate date) { | ||
| this.instant = date.atStartOfDay().toInstant(ZoneOffset.UTC); | ||
| this.date = date; | ||
| this.epochMs = instant.toEpochMilli(); | ||
| this.formatted = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); | ||
| } | ||
|
|
||
| public static TargetDate now() { | ||
| return new TargetDate(LocalDate.now(Clock.systemUTC())); | ||
| } | ||
|
|
||
| public static TargetDate of(int year, int month, int day) { | ||
| return new TargetDate(LocalDate.of(year, month, day)); | ||
| } | ||
|
|
||
| public long asEpochMs() { | ||
| return epochMs; | ||
| } | ||
|
|
||
| public Instant asInstant() { | ||
| return instant; | ||
| } | ||
|
|
||
| // relative to this date | ||
| public long saltAgeInDays(SaltEntry salt) { | ||
| return (this.asEpochMs() - salt.lastUpdated()) / DAY_IN_MS; | ||
| } | ||
|
|
||
| public TargetDate plusDays(int days) { | ||
| return new TargetDate(date.plusDays(days)); | ||
| } | ||
|
|
||
| public TargetDate minusDays(int days) { | ||
| return new TargetDate(date.minusDays(days)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return formatted; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| TargetDate that = (TargetDate) o; | ||
| return epochMs == that.epochMs; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(epochMs); | ||
| } | ||
| } |
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.
should this be less than or equal to? or less than?
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 this should be <, not <= since this includes the day on which we actually rotated the salt when the age would be 0. This results in people seeing previousSalt for 90 days exactly.
The current behaviour is also less than, we're even testing for it in L295.