|
3 | 3 | import com.uid2.shared.model.SaltEntry; |
4 | 4 | import com.uid2.shared.secret.IKeyGenerator; |
5 | 5 | import com.uid2.shared.store.salt.RotatingSaltProvider; |
6 | | -import io.vertx.core.json.JsonObject; |
| 6 | + |
| 7 | +import com.uid2.shared.store.salt.RotatingSaltProvider.SaltSnapshot; |
7 | 8 |
|
8 | 9 | import java.time.Duration; |
9 | 10 | import java.time.Instant; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.time.ZoneOffset; |
| 13 | +import java.time.temporal.ChronoUnit; |
10 | 14 | import java.util.*; |
11 | 15 | import java.util.stream.IntStream; |
12 | 16 |
|
13 | 17 | import static java.util.stream.Collectors.toList; |
14 | 18 |
|
15 | 19 | public class SaltRotation implements ISaltRotation { |
16 | | - private static final String SNAPSHOT_ACTIVATES_IN_SECONDS = "salt_snapshot_activates_in_seconds"; |
17 | | - private static final String SNAPSHOT_EXPIRES_AFTER_SECONDS = "salt_snapshot_expires_after_seconds"; |
18 | | - |
19 | 20 | private final IKeyGenerator keyGenerator; |
20 | | - private final Duration snapshotActivatesIn; |
21 | | - private final Duration snapshotExpiresAfter; |
22 | | - |
23 | | - public static Duration getSnapshotActivatesIn(JsonObject config) { |
24 | | - return Duration.ofSeconds(config.getInteger(SNAPSHOT_ACTIVATES_IN_SECONDS)); |
25 | | - } |
26 | | - public static Duration getSnapshotExpiresAfter(JsonObject config) { |
27 | | - return Duration.ofSeconds(config.getInteger(SNAPSHOT_EXPIRES_AFTER_SECONDS)); |
28 | | - } |
29 | 21 |
|
30 | | - public SaltRotation(JsonObject config, IKeyGenerator keyGenerator) { |
| 22 | + public SaltRotation(IKeyGenerator keyGenerator) { |
31 | 23 | this.keyGenerator = keyGenerator; |
| 24 | + } |
32 | 25 |
|
33 | | - snapshotActivatesIn = getSnapshotActivatesIn(config); |
34 | | - snapshotExpiresAfter = getSnapshotExpiresAfter(config); |
| 26 | + @Override |
| 27 | + public Result rotateSalts(RotatingSaltProvider.SaltSnapshot lastSnapshot, |
| 28 | + Duration[] minAges, |
| 29 | + double fraction, |
| 30 | + LocalDate targetDate) throws Exception { |
| 31 | + |
| 32 | + final Instant nextEffective = targetDate.atStartOfDay().toInstant(ZoneOffset.UTC); |
| 33 | + final Instant nextExpires = nextEffective.plus(7, ChronoUnit.DAYS); |
| 34 | + if (nextEffective.equals(lastSnapshot.getEffective()) || nextEffective.isBefore(lastSnapshot.getEffective())) { |
| 35 | + return Result.noSnapshot("cannot create a new salt snapshot with effective timestamp equal or prior to that of an existing snapshot"); |
| 36 | + } |
35 | 37 |
|
36 | | - if (snapshotActivatesIn.compareTo(snapshotExpiresAfter) >= 0) { |
37 | | - throw new IllegalStateException(SNAPSHOT_EXPIRES_AFTER_SECONDS + " must be greater than " + SNAPSHOT_ACTIVATES_IN_SECONDS); |
| 38 | + List<Integer> saltIndexesToRotate = pickSaltIndexesToRotate(lastSnapshot, nextEffective, minAges, fraction); |
| 39 | + if (saltIndexesToRotate.isEmpty()) { |
| 40 | + return Result.noSnapshot("all salts are below min rotation age"); |
38 | 41 | } |
| 42 | + |
| 43 | + var updatedSalts = updateSalts(lastSnapshot.getAllRotatingSalts(), saltIndexesToRotate, nextEffective.toEpochMilli()); |
| 44 | + |
| 45 | + SaltSnapshot nextSnapshot = new SaltSnapshot( |
| 46 | + nextEffective, |
| 47 | + nextExpires, |
| 48 | + updatedSalts, |
| 49 | + lastSnapshot.getFirstLevelSalt()); |
| 50 | + return Result.fromSnapshot(nextSnapshot); |
39 | 51 | } |
40 | 52 |
|
41 | | - @Override |
42 | | - public Result rotateSalts(RotatingSaltProvider.SaltSnapshot lastSnapshot, |
43 | | - Duration[] minAges, |
44 | | - double fraction) throws Exception { |
45 | | - final Instant now = Instant.now(); |
46 | | - final Instant nextEffective = now.plusSeconds(snapshotActivatesIn.getSeconds()); |
47 | | - final Instant nextExpires = nextEffective.plusSeconds(snapshotExpiresAfter.getSeconds()); |
48 | | - if (!nextEffective.isAfter(lastSnapshot.getEffective())) { |
49 | | - return Result.noSnapshot("cannot create a new salt snapshot with effective timestamp prior to that of an existing snapshot"); |
| 53 | + private SaltEntry[] updateSalts(SaltEntry[] oldSalts, List<Integer> saltIndexesToRotate, long nextEffective) throws Exception { |
| 54 | + var updatedSalts = new SaltEntry[oldSalts.length]; |
| 55 | + |
| 56 | + for (int i = 0; i < oldSalts.length; i++) { |
| 57 | + var shouldRotate = saltIndexesToRotate.contains(i); |
| 58 | + updatedSalts[i] = updateSalt(oldSalts[i], shouldRotate, nextEffective); |
50 | 59 | } |
| 60 | + return updatedSalts; |
| 61 | + } |
| 62 | + |
| 63 | + private SaltEntry updateSalt(SaltEntry oldSalt, boolean shouldRotate, long nextEffective) throws Exception { |
| 64 | + var currentSalt = shouldRotate ? this.keyGenerator.generateRandomKeyString(32) : oldSalt.currentSalt(); |
| 65 | + var lastUpdated = shouldRotate ? nextEffective : oldSalt.lastUpdated(); |
| 66 | + |
| 67 | + return new SaltEntry( |
| 68 | + oldSalt.id(), |
| 69 | + oldSalt.hashedId(), |
| 70 | + lastUpdated, |
| 71 | + currentSalt, |
| 72 | + null, |
| 73 | + null, |
| 74 | + null, |
| 75 | + null |
| 76 | + ); |
| 77 | + } |
51 | 78 |
|
| 79 | + private List<Integer> pickSaltIndexesToRotate( |
| 80 | + SaltSnapshot lastSnapshot, |
| 81 | + Instant nextEffective, |
| 82 | + Duration[] minAges, |
| 83 | + double fraction) { |
52 | 84 | final Instant[] thresholds = Arrays.stream(minAges) |
53 | | - .map(a -> now.minusSeconds(a.getSeconds())) |
| 85 | + .map(age -> nextEffective.minusSeconds(age.getSeconds())) |
54 | 86 | .sorted() |
55 | 87 | .toArray(Instant[]::new); |
56 | | - final int maxSalts = (int)Math.ceil(lastSnapshot.getAllRotatingSalts().length * fraction); |
57 | | - final List<Integer> entryIndexes = new ArrayList<>(); |
| 88 | + final int maxSalts = (int) Math.ceil(lastSnapshot.getAllRotatingSalts().length * fraction); |
| 89 | + final List<Integer> indexesToRotate = new ArrayList<>(); |
58 | 90 |
|
59 | 91 | Instant minLastUpdated = Instant.ofEpochMilli(0); |
60 | 92 | for (Instant threshold : thresholds) { |
61 | | - if (entryIndexes.size() >= maxSalts) break; |
62 | | - addIndexesToRotate(entryIndexes, lastSnapshot, |
63 | | - minLastUpdated.toEpochMilli(), threshold.toEpochMilli(), |
64 | | - maxSalts - entryIndexes.size()); |
| 93 | + if (indexesToRotate.size() >= maxSalts) break; |
| 94 | + addIndexesToRotate( |
| 95 | + indexesToRotate, |
| 96 | + lastSnapshot, |
| 97 | + minLastUpdated.toEpochMilli(), |
| 98 | + threshold.toEpochMilli(), |
| 99 | + maxSalts - indexesToRotate.size() |
| 100 | + ); |
65 | 101 | minLastUpdated = threshold; |
66 | 102 | } |
67 | | - |
68 | | - if (entryIndexes.isEmpty()) return Result.noSnapshot("all salts are below min rotation age"); |
69 | | - |
70 | | - return Result.fromSnapshot(createRotatedSnapshot(lastSnapshot, nextEffective, nextExpires, entryIndexes)); |
| 103 | + return indexesToRotate; |
71 | 104 | } |
72 | 105 |
|
73 | 106 | private void addIndexesToRotate(List<Integer> entryIndexes, |
74 | | - RotatingSaltProvider.SaltSnapshot lastSnapshot, |
| 107 | + SaltSnapshot lastSnapshot, |
75 | 108 | long minLastUpdated, |
76 | 109 | long maxLastUpdated, |
77 | 110 | int maxIndexes) { |
78 | 111 | final SaltEntry[] entries = lastSnapshot.getAllRotatingSalts(); |
79 | 112 | final List<Integer> candidateIndexes = IntStream.range(0, entries.length) |
80 | 113 | .filter(i -> isBetween(entries[i].lastUpdated(), minLastUpdated, maxLastUpdated)) |
81 | | - .boxed().collect(toList()); |
| 114 | + .boxed() |
| 115 | + .collect(toList()); |
82 | 116 | if (candidateIndexes.size() <= maxIndexes) { |
83 | 117 | entryIndexes.addAll(candidateIndexes); |
84 | 118 | return; |
85 | 119 | } |
86 | 120 | Collections.shuffle(candidateIndexes); |
87 | | - candidateIndexes.stream().limit(maxIndexes).forEachOrdered(i -> entryIndexes.add(i)); |
| 121 | + candidateIndexes.stream().limit(maxIndexes).forEachOrdered(entryIndexes::add); |
88 | 122 | } |
89 | 123 |
|
90 | 124 | private static boolean isBetween(long t, long minInclusive, long maxExclusive) { |
91 | 125 | return minInclusive <= t && t < maxExclusive; |
92 | 126 | } |
93 | 127 |
|
94 | | - private RotatingSaltProvider.SaltSnapshot createRotatedSnapshot(RotatingSaltProvider.SaltSnapshot lastSnapshot, |
95 | | - Instant nextEffective, |
96 | | - Instant nextExpires, |
97 | | - List<Integer> entryIndexes) throws Exception { |
98 | | - final long lastUpdated = nextEffective.toEpochMilli(); |
99 | | - final RotatingSaltProvider.SaltSnapshot nextSnapshot = new RotatingSaltProvider.SaltSnapshot( |
100 | | - nextEffective, nextExpires, |
101 | | - Arrays.copyOf(lastSnapshot.getAllRotatingSalts(), lastSnapshot.getAllRotatingSalts().length), |
102 | | - lastSnapshot.getFirstLevelSalt()); |
103 | | - for (Integer i : entryIndexes) { |
104 | | - final SaltEntry oldSalt = nextSnapshot.getAllRotatingSalts()[i]; |
105 | | - final String secret = this.keyGenerator.generateRandomKeyString(32); |
106 | | - nextSnapshot.getAllRotatingSalts()[i] = new SaltEntry(oldSalt.id(), oldSalt.hashedId(), lastUpdated, secret, null, null, null, null); |
107 | | - } |
108 | | - return nextSnapshot; |
109 | | - } |
110 | 128 | } |
0 commit comments