Skip to content

Commit d657f5c

Browse files
committed
Address feedback
1 parent 693fbb2 commit d657f5c

File tree

6 files changed

+33
-55
lines changed

6 files changed

+33
-55
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class Main {
7676

7777
private final Vertx vertx;
7878
private final JsonObject config;
79-
8079
public Main(Vertx vertx, JsonObject config) {
8180
this.vertx = vertx;
8281
this.config = config;

src/main/java/com/uid2/admin/cloudEncryption/CloudKeyRotationStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ private Stream<CloudEncryptionKey> desiredKeysForSite(
4848
KeyIdGenerator idGenerator,
4949
Set<CloudEncryptionKey> existingKeys
5050
) {
51-
var withNewKey = Streams.concat(existingKeys.stream(), Stream.of(makeNewKey(siteId, idGenerator)));
52-
return keyRetentionStrategy.selectKeysToRetain(withNewKey.collect(Collectors.toSet())).stream();
51+
var existingKeysToRetain = keyRetentionStrategy.selectKeysToRetain(existingKeys);
52+
var newKey = makeNewKey(siteId, idGenerator);
53+
return Streams.concat(existingKeysToRetain.stream(), Stream.of(newKey));
5354
}
5455

5556
private CloudEncryptionKey makeNewKey(Integer siteId, KeyIdGenerator idGenerator) {

src/main/java/com/uid2/admin/cloudEncryption/ExpiredKeyCountRetentionStrategy.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import com.uid2.admin.store.Clock;
44
import com.uid2.shared.model.CloudEncryptionKey;
55

6-
import java.util.ArrayList;
76
import java.util.Comparator;
87
import java.util.Set;
98
import java.util.stream.Collectors;
10-
import java.util.stream.Stream;
119

12-
// Keep up to `expiredKeysToRetain` most recent expired keys
10+
// Only keep keys past activation time - no future keys allowed
11+
// Keep up to `expiredKeysToRetain` most recent keys
1312
// Considers all keys passed, doesn't do grouping by site (handled upstream)
1413
public class ExpiredKeyCountRetentionStrategy implements CloudKeyRetentionStrategy {
1514
private final Clock clock;
@@ -22,37 +21,10 @@ public ExpiredKeyCountRetentionStrategy(Clock clock, int expiredKeysToRetain) {
2221

2322
@Override
2423
public Set<CloudEncryptionKey> selectKeysToRetain(Set<CloudEncryptionKey> keysForSite) {
25-
var activeKey = findActiveKey(keysForSite);
26-
if (activeKey == null) {
27-
return keysForSite;
28-
}
29-
30-
var expiredKeys = new ArrayList<CloudEncryptionKey>();
31-
var nonExpiredKeys = new ArrayList<CloudEncryptionKey>();
32-
for (var key : keysForSite) {
33-
if (key.getActivates() < activeKey.getActivates()) {
34-
expiredKeys.add(key);
35-
} else {
36-
nonExpiredKeys.add(key);
37-
}
38-
}
39-
40-
var retainedExpiredKeys = pickRetainedExpiredKeys(expiredKeys);
41-
return Stream.concat(retainedExpiredKeys, nonExpiredKeys.stream()).collect(Collectors.toSet());
42-
43-
}
44-
45-
private Stream<CloudEncryptionKey> pickRetainedExpiredKeys(ArrayList<CloudEncryptionKey> expiredKeys) {
46-
return expiredKeys
47-
.stream()
24+
return keysForSite.stream()
25+
.filter(key -> key.getActivates() <= clock.getEpochSecond())
4826
.sorted(Comparator.comparingLong(CloudEncryptionKey::getActivates).reversed())
49-
.limit(expiredKeysToRetain);
50-
}
51-
52-
private CloudEncryptionKey findActiveKey(Set<CloudEncryptionKey> keys) {
53-
var keysPastActivation = keys.stream().filter(key -> key.getActivates() <= clock.getEpochSecond());
54-
return keysPastActivation
55-
.max(Comparator.comparingLong(CloudEncryptionKey::getActivates))
56-
.orElse(null);
27+
.limit(expiredKeysToRetain)
28+
.collect(Collectors.toSet());
5729
}
5830
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setupRoutes(Router router) {
5050
);
5151

5252
router.post(Endpoints.CLOUD_ENCRYPTION_KEY_ROTATE.toString()).handler(
53-
auth.handle(this::handleRotate, Role.MAINTAINER)
53+
auth.handle(this::handleRotate, Role.MAINTAINER, Role.SECRET_ROTATION)
5454
);
5555
}
5656

src/test/java/com/uid2/admin/cloudEncryption/ExpiredKeyCountRetentionStrategyTest.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
class ExpiredKeyCountRetentionStrategyTest {
1717
private final long past1 = 100L;
1818
private final long past2 = 200L;
19-
private final long past3 = 300L;
20-
private final long now = 400L;
21-
private final long future = 500L;
19+
private final long now = 300L;
20+
private final long future = 400L;
2221
private ExpiredKeyCountRetentionStrategy strategy;
23-
private Clock clock;
2422

2523
@BeforeEach
2624
void setUp() {
27-
clock = mock(Clock.class);
25+
Clock clock = mock(Clock.class);
2826
strategy = new ExpiredKeyCountRetentionStrategy(clock, 2);
2927
when(clock.getEpochSecond()).thenReturn(now);
3028
}
@@ -50,29 +48,27 @@ void selectKeysToRetain_withLessThanNKeys() {
5048
}
5149

5250
@Test
53-
void selectKeysToRetain_withMoreThanNNonExpiredKeys() {
51+
void selectKeysToRetain_dropsFutureKeys() {
5452
var activeKey = new CloudEncryptionKey(1, 1, now, now, "secret 1");
5553
var futureKey1 = new CloudEncryptionKey(2, 1, future, now, "secret 2");
56-
var futureKey2 = new CloudEncryptionKey(3, 1, future, now, "secret 3");
57-
var futureKey3 = new CloudEncryptionKey(4, 1, future, now, "secret 4");
58-
var expiredKey1 = new CloudEncryptionKey(5, 1, past1, now, "secret 5");
59-
var originalKeys = Set.of(activeKey, futureKey1, futureKey2, futureKey3, expiredKey1);
54+
var expiredKey1 = new CloudEncryptionKey(3, 1, past1, now, "secret 3");
55+
var originalKeys = Set.of(activeKey, futureKey1, expiredKey1);
56+
57+
var expected = Set.of(activeKey, expiredKey1);
6058

6159
var actual = strategy.selectKeysToRetain(originalKeys);
6260

63-
assertThat(actual).isEqualTo(originalKeys);
61+
assertThat(actual).isEqualTo(expected);
6462
}
6563

6664
@Test
67-
void selectKeysToRetain_withMoreThanNExpiredKeys() {
65+
void selectKeysToRetain_keepsNMostRecentNonFutureKeys() {
6866
var oldestExpiredKey = new CloudEncryptionKey(1, 1, past1, past1, "secret 1"); // Don't retain
6967
var expiredKey2 = new CloudEncryptionKey(2, 1, past2, past1, "secret 2");
70-
var expiredKey3 = new CloudEncryptionKey(3, 1, past3, past1, "secret 3");
71-
var activeKey = new CloudEncryptionKey(3, 1, now, past1, "secret 4");
72-
var futureKey = new CloudEncryptionKey(3, 1, now, past1, "secret 5");
73-
var originalKeys = Set.of(oldestExpiredKey, expiredKey2, expiredKey3, activeKey, futureKey);
68+
var activeKey = new CloudEncryptionKey(3, 1, now, past1, "secret 3");
69+
var originalKeys = Set.of(oldestExpiredKey, expiredKey2, activeKey);
7470

75-
var expected = Set.of(expiredKey2, expiredKey3, activeKey, futureKey);
71+
var expected = Set.of(expiredKey2, activeKey);
7672

7773
var actual = strategy.selectKeysToRetain(originalKeys);
7874

src/test/java/com/uid2/admin/vertx/CloudEncryptionKeyServiceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public void testRotate_noAccess(Vertx vertx, VertxTestContext testContext) {
111111
});
112112
}
113113

114+
@Test
115+
public void testRotate_canBeRotatedBySecretRotationJob(Vertx vertx, VertxTestContext testContext) {
116+
fakeAuth(Role.SECRET_ROTATION);
117+
post(vertx, testContext, Endpoints.CLOUD_ENCRYPTION_KEY_ROTATE, null, response -> {
118+
assertEquals(200, response.statusCode());
119+
120+
testContext.completeNow();
121+
});
122+
}
123+
114124
@Test
115125
public void testRotate_noSitesDoesNothing(Vertx vertx, VertxTestContext testContext) {
116126
fakeAuth(Role.MAINTAINER);

0 commit comments

Comments
 (0)