Skip to content

Commit 69b5892

Browse files
made default threshold ages static
1 parent adbd718 commit 69b5892

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,28 @@
2727

2828
public class SaltService implements IService {
2929
private static final Logger LOGGER = LoggerFactory.getLogger(SaltService.class);
30+
private static final Duration[] SALT_ROTATION_AGE_THRESHOLDS = new Duration[]{
31+
Duration.ofDays(30),
32+
Duration.ofDays(60),
33+
Duration.ofDays(90),
34+
Duration.ofDays(120),
35+
Duration.ofDays(150),
36+
Duration.ofDays(180),
37+
Duration.ofDays(210),
38+
Duration.ofDays(240),
39+
Duration.ofDays(270),
40+
Duration.ofDays(300),
41+
Duration.ofDays(330),
42+
Duration.ofDays(360),
43+
Duration.ofDays(390)
44+
};
3045

3146
private final AdminAuthMiddleware auth;
3247
private final WriteLock writeLock;
3348
private final SaltStoreWriter storageManager;
3449
private final RotatingSaltProvider saltProvider;
3550
private final SaltRotation saltRotation;
36-
private final Duration[] defaultSaltRotationAgeThresholds;
51+
3752

3853
public SaltService(AdminAuthMiddleware auth,
3954
WriteLock writeLock,
@@ -45,7 +60,6 @@ public SaltService(AdminAuthMiddleware auth,
4560
this.storageManager = storageManager;
4661
this.saltProvider = saltProvider;
4762
this.saltRotation = saltRotation;
48-
this.defaultSaltRotationAgeThresholds = generateThresholds(30, 390, 30);
4963
}
5064

5165
@Override
@@ -122,9 +136,9 @@ private void handleSaltRotate(RoutingContext rc) {
122136
ageThresholds = RequestUtil.getDurations(rc, "min_ages_in_seconds");
123137
if (ageThresholds == null) return;
124138
} else {
125-
ageThresholds = defaultSaltRotationAgeThresholds;
139+
ageThresholds = SALT_ROTATION_AGE_THRESHOLDS;
126140
}
127-
141+
LOGGER.info("Salt rotation age thresholds: {}", Arrays.toString(ageThresholds));
128142

129143
final TargetDate targetDate =
130144
RequestUtil.getDate(rc, "target_date", DateTimeFormatter.ISO_LOCAL_DATE)
@@ -157,14 +171,6 @@ private void handleSaltRotate(RoutingContext rc) {
157171
}
158172
}
159173

160-
private Duration[] generateThresholds(int minAge, int maxAge, int interval) {
161-
List<Duration> thresholds = new ArrayList<>();
162-
for (int i = minAge; i <= maxAge; i += interval) {
163-
thresholds.add(Duration.ofDays(i));
164-
}
165-
return thresholds.toArray(new Duration[0]);
166-
}
167-
168174
private JsonObject toJson(RotatingSaltProvider.SaltSnapshot snapshot) {
169175
JsonObject jo = new JsonObject();
170176
jo.put("effective", snapshot.getEffective().toEpochMilli());

src/test/java/com/uid2/admin/salt/SaltServiceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ void rotateSaltsWithCustomAgeThresholdsEnabled(Vertx vertx, VertxTestContext tes
153153
when(saltRotation.rotateSalts(any(), eq(expectedCustomAgeThresholds), eq(0.2), eq(utcTomorrow))).thenReturn(result);
154154

155155
post(vertx, testContext, "api/salt/rotate?min_ages_in_seconds=50,60,70&fraction=0.2", "", response -> {
156+
verify(saltRotation).rotateSalts(any(), eq(expectedCustomAgeThresholds), eq(0.2), eq(utcTomorrow));
156157
assertEquals(200, response.statusCode());
157158
testContext.completeNow();
158159
});
@@ -179,6 +180,7 @@ void rotateSaltsWithDefaultAgeThresholds(Vertx vertx, VertxTestContext testConte
179180
when(saltRotation.rotateSalts(any(), eq(expectedDefaultAgeThresholds), eq(0.2), eq(utcTomorrow))).thenReturn(result);
180181

181182
post(vertx, testContext, "api/salt/rotate?min_ages_in_seconds=50,60,70&fraction=0.2", "", response -> {
183+
verify(saltRotation).rotateSalts(any(), eq(expectedDefaultAgeThresholds), eq(0.2), eq(utcTomorrow));
182184
assertEquals(200, response.statusCode());
183185
testContext.completeNow();
184186
});
@@ -194,6 +196,7 @@ void rotateSaltsWithCustomAgeThresholdsEnabledButMissingParameter(Vertx vertx, V
194196
setSnapshots(lastSnapshot);
195197

196198
post(vertx, testContext, "api/salt/rotate?fraction=0.2", "", response -> {
199+
verify(saltRotation, never()).rotateSalts(any(), any(), anyDouble(), any());
197200
assertEquals(400, response.statusCode());
198201
testContext.completeNow();
199202
});

0 commit comments

Comments
 (0)