|
1 | 1 | package com.uid2.admin.secret; |
2 | 2 |
|
| 3 | +import com.uid2.admin.AdminConst; |
3 | 4 | import com.uid2.shared.model.SaltEntry; |
4 | 5 | import com.uid2.shared.secret.IKeyGenerator; |
5 | 6 | import com.uid2.shared.store.salt.RotatingSaltProvider; |
| 7 | +import io.vertx.core.json.JsonObject; |
6 | 8 | import org.junit.jupiter.api.BeforeEach; |
7 | 9 | import org.junit.jupiter.api.Test; |
8 | 10 | import org.junit.jupiter.params.ParameterizedTest; |
@@ -38,7 +40,9 @@ private Instant daysLater(int days) { |
38 | 40 | void setup() { |
39 | 41 | MockitoAnnotations.openMocks(this); |
40 | 42 |
|
41 | | - saltRotation = new SaltRotation(keyGenerator); |
| 43 | + JsonObject config = new JsonObject(); |
| 44 | + |
| 45 | + saltRotation = new SaltRotation(config, keyGenerator); |
42 | 46 | } |
43 | 47 |
|
44 | 48 | private static class SnapshotBuilder { |
@@ -318,4 +322,44 @@ void rotateSaltsRemovePreviousSaltsOver90DaysOld() throws Exception { |
318 | 322 | assertNull(salts[1].previousSalt()); |
319 | 323 | } |
320 | 324 |
|
| 325 | + |
| 326 | + @Test |
| 327 | + void rotateSaltsRotateWhenRefreshFromIsTargetDate() throws Exception { |
| 328 | + JsonObject config = new JsonObject(); |
| 329 | + config.put(AdminConst.ENABLE_SALT_ROTATION_REFRESH_FROM, Boolean.TRUE); |
| 330 | + saltRotation = new SaltRotation(config, keyGenerator); |
| 331 | + |
| 332 | + final Duration[] minAges = { |
| 333 | + Duration.ofDays(90), |
| 334 | + Duration.ofDays(60), |
| 335 | + }; |
| 336 | + |
| 337 | + var validForRotation1 = daysEarlier(120).toEpochMilli(); |
| 338 | + var validForRotation2 = daysEarlier(70).toEpochMilli(); |
| 339 | + var notValidForRotation = daysEarlier(30).toEpochMilli(); |
| 340 | + var refreshNow = targetDateAsInstant.toEpochMilli(); |
| 341 | + var refreshLater = daysLater(20).toEpochMilli(); |
| 342 | + |
| 343 | + var lastSnapshot = SnapshotBuilder.start() |
| 344 | + .withEntries( |
| 345 | + new SaltEntry(1, "1", validForRotation1, "salt", refreshNow, null, null, null), |
| 346 | + new SaltEntry(2, "2", notValidForRotation, "salt", refreshNow, null, null, null), |
| 347 | + new SaltEntry(3, "3", validForRotation2, "salt", refreshLater, null, null, null) |
| 348 | + ) |
| 349 | + .build(daysEarlier(1), daysLater(6)); |
| 350 | + |
| 351 | + var result = saltRotation.rotateSalts(lastSnapshot, minAges, 1, targetDate); |
| 352 | + assertTrue(result.hasSnapshot()); |
| 353 | + |
| 354 | + var salts = result.getSnapshot().getAllRotatingSalts(); |
| 355 | + |
| 356 | + assertEquals(targetDateAsInstant.toEpochMilli(), salts[0].lastUpdated()); |
| 357 | + assertEquals(daysLater(30).toEpochMilli(), salts[0].refreshFrom()); |
| 358 | + |
| 359 | + assertEquals(notValidForRotation, salts[1].lastUpdated()); |
| 360 | + assertEquals(daysLater(30).toEpochMilli(), salts[1].refreshFrom()); |
| 361 | + |
| 362 | + assertEquals(validForRotation2, salts[2].lastUpdated()); |
| 363 | + assertEquals(refreshLater, salts[2].refreshFrom()); |
| 364 | + } |
321 | 365 | } |
0 commit comments