Skip to content

Commit d5943a5

Browse files
authored
Merge pull request #461 from IABTechLab/aul-UID2-5351-setting-refresh-from-field
Recording refreshFrom, remove unnecessary interface
2 parents 83a89f1 + 59e3188 commit d5943a5

File tree

6 files changed

+141
-137
lines changed

6 files changed

+141
-137
lines changed

src/main/java/com/uid2/admin/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void run() {
232232
WriteLock writeLock = new WriteLock();
233233
KeyHasher keyHasher = new KeyHasher();
234234
IKeypairGenerator keypairGenerator = new SecureKeypairGenerator();
235-
ISaltRotation saltRotation = new SaltRotation(keyGenerator);
235+
SaltRotation saltRotation = new SaltRotation(keyGenerator);
236236
EncryptionKeyService encryptionKeyService = new EncryptionKeyService(
237237
config, auth, writeLock, encryptionKeyStoreWriter, keysetKeyStoreWriter, keyProvider, keysetKeysProvider, adminKeysetProvider, adminKeysetStoreWriter, keyGenerator, clock);
238238
KeysetManager keysetManager = new KeysetManager(

src/main/java/com/uid2/admin/secret/ISaltRotation.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616

1717
import static java.util.stream.Collectors.toList;
1818

19-
public class SaltRotation implements ISaltRotation {
19+
public class SaltRotation {
2020
private final IKeyGenerator keyGenerator;
21+
private final long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis();
2122

2223
public SaltRotation(IKeyGenerator keyGenerator) {
2324
this.keyGenerator = keyGenerator;
2425
}
2526

26-
@Override
2727
public Result rotateSalts(RotatingSaltProvider.SaltSnapshot lastSnapshot,
2828
Duration[] minAges,
2929
double fraction,
3030
LocalDate targetDate) throws Exception {
31-
3231
final Instant nextEffective = targetDate.atStartOfDay().toInstant(ZoneOffset.UTC);
3332
final Instant nextExpires = nextEffective.plus(7, ChronoUnit.DAYS);
3433
if (nextEffective.equals(lastSnapshot.getEffective()) || nextEffective.isBefore(lastSnapshot.getEffective())) {
@@ -63,19 +62,26 @@ private SaltEntry[] updateSalts(SaltEntry[] oldSalts, List<Integer> saltIndexesT
6362
private SaltEntry updateSalt(SaltEntry oldSalt, boolean shouldRotate, long nextEffective) throws Exception {
6463
var currentSalt = shouldRotate ? this.keyGenerator.generateRandomKeyString(32) : oldSalt.currentSalt();
6564
var lastUpdated = shouldRotate ? nextEffective : oldSalt.lastUpdated();
65+
var refreshFrom = calculateRefreshFrom(oldSalt.lastUpdated(), nextEffective);
6666

6767
return new SaltEntry(
6868
oldSalt.id(),
6969
oldSalt.hashedId(),
7070
lastUpdated,
7171
currentSalt,
72-
null,
72+
refreshFrom,
7373
null,
7474
null,
7575
null
7676
);
7777
}
7878

79+
private long calculateRefreshFrom(long lastUpdated, long nextEffective) {
80+
long age = nextEffective - lastUpdated;
81+
long multiplier = age / THIRTY_DAYS_IN_MS + 1;
82+
return lastUpdated + (multiplier * THIRTY_DAYS_IN_MS);
83+
}
84+
7985
private List<Integer> pickSaltIndexesToRotate(
8086
SaltSnapshot lastSnapshot,
8187
Instant nextEffective,
@@ -125,4 +131,24 @@ private static boolean isBetween(long t, long minInclusive, long maxExclusive) {
125131
return minInclusive <= t && t < maxExclusive;
126132
}
127133

134+
public static class Result {
135+
private final RotatingSaltProvider.SaltSnapshot snapshot; // can be null if new snapshot is not needed
136+
private final String reason; // why you are not getting a new snapshot
137+
138+
private Result(RotatingSaltProvider.SaltSnapshot snapshot, String reason) {
139+
this.snapshot = snapshot;
140+
this.reason = reason;
141+
}
142+
143+
public boolean hasSnapshot() { return snapshot != null; }
144+
public RotatingSaltProvider.SaltSnapshot getSnapshot() { return snapshot; }
145+
public String getReason() { return reason; }
146+
147+
public static Result fromSnapshot(RotatingSaltProvider.SaltSnapshot snapshot) {
148+
return new Result(snapshot, null);
149+
}
150+
public static Result noSnapshot(String reason) {
151+
return new Result(null, reason);
152+
}
153+
}
128154
}

src/main/java/com/uid2/admin/vertx/service/SaltService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.uid2.admin.vertx.service;
22

33
import com.uid2.admin.auth.AdminAuthMiddleware;
4-
import com.uid2.admin.secret.ISaltRotation;
4+
import com.uid2.admin.secret.SaltRotation;
55
import com.uid2.admin.store.writer.SaltStoreWriter;
66
import com.uid2.admin.vertx.RequestUtil;
77
import com.uid2.admin.vertx.ResponseUtil;
@@ -30,13 +30,13 @@ public class SaltService implements IService {
3030
private final WriteLock writeLock;
3131
private final SaltStoreWriter storageManager;
3232
private final RotatingSaltProvider saltProvider;
33-
private final ISaltRotation saltRotation;
33+
private final SaltRotation saltRotation;
3434

3535
public SaltService(AdminAuthMiddleware auth,
3636
WriteLock writeLock,
3737
SaltStoreWriter storageManager,
3838
RotatingSaltProvider saltProvider,
39-
ISaltRotation saltRotation) {
39+
SaltRotation saltRotation) {
4040
this.auth = auth;
4141
this.writeLock = writeLock;
4242
this.storageManager = storageManager;
@@ -89,7 +89,7 @@ private void handleSaltRotate(RoutingContext rc) {
8989
final List<RotatingSaltProvider.SaltSnapshot> snapshots = this.saltProvider.getSnapshots();
9090
final RotatingSaltProvider.SaltSnapshot lastSnapshot = snapshots.get(snapshots.size() - 1);
9191

92-
final ISaltRotation.Result result = saltRotation.rotateSalts(
92+
final SaltRotation.Result result = saltRotation.rotateSalts(
9393
lastSnapshot, minAges, fraction.get(), targetDate);
9494
if (!result.hasSnapshot()) {
9595
ResponseUtil.error(rc, 200, result.getReason());

0 commit comments

Comments
 (0)