Skip to content

Commit 594880b

Browse files
committed
Cleaned up refresh from and salt age threshold flags
1 parent 0d1d436 commit 594880b

File tree

5 files changed

+7
-74
lines changed

5 files changed

+7
-74
lines changed

conf/default-config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"enable_keysets": false,
3-
"enable_salt_rotation_refresh_from": false
4-
}
2+
"enable_keysets": false
3+
}

conf/local-config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"keys_acl_metadata_path": "keys_acl/metadata.json",
1313
"salts_metadata_path": "salts/metadata.json",
1414
"salt_snapshot_location_prefix": "salts/salts.txt.",
15-
"enable_salt_rotation_refresh_from": false,
1615
"operators_metadata_path": "operators/metadata.json",
1716
"enclaves_metadata_path": "enclaves/metadata.json",
1817
"partners_metadata_path": "partners/metadata.json",

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.uid2.admin.salt;
22

3-
import com.uid2.admin.AdminConst;
43
import com.uid2.shared.model.SaltEntry;
54
import com.uid2.shared.secret.IKeyGenerator;
65

7-
import com.uid2.shared.store.salt.ISaltProvider;
86
import com.uid2.shared.store.salt.ISaltProvider.ISaltSnapshot;
97
import com.uid2.shared.store.salt.RotatingSaltProvider.SaltSnapshot;
108
import io.vertx.core.json.JsonObject;
@@ -22,18 +20,10 @@ public class SaltRotation {
2220
private static final double MAX_SALT_PERCENTAGE = 0.8;
2321

2422
private final IKeyGenerator keyGenerator;
25-
private final boolean isRefreshFromEnabled;
26-
private final boolean isCustomAgeThresholdEnabled;
2723
private static final Logger LOGGER = LoggerFactory.getLogger(SaltRotation.class);
2824

2925
public SaltRotation(JsonObject config, IKeyGenerator keyGenerator) {
3026
this.keyGenerator = keyGenerator;
31-
this.isRefreshFromEnabled = config.getBoolean(AdminConst.ENABLE_SALT_ROTATION_REFRESH_FROM, false);
32-
this.isCustomAgeThresholdEnabled = config.getBoolean(AdminConst.ENABLE_SALT_ROTATION_CUSTOM_AGE_THRESHOLDS, false);
33-
}
34-
35-
public boolean isCustomAgeThresholdEnabled() {
36-
return this.isCustomAgeThresholdEnabled;
3727
}
3828

3929
public Result rotateSalts(
@@ -107,11 +97,7 @@ private Set<SaltEntry> findRefreshableSalts(SaltEntry[] preRotationSalts, Target
10797
}
10898

10999
private boolean isRefreshable(TargetDate targetDate, SaltEntry salt) {
110-
if (this.isRefreshFromEnabled) {
111-
return Instant.ofEpochMilli(salt.refreshFrom()).truncatedTo(ChronoUnit.DAYS).equals(targetDate.asInstant());
112-
}
113-
114-
return true;
100+
return Instant.ofEpochMilli(salt.refreshFrom()).truncatedTo(ChronoUnit.DAYS).equals(targetDate.asInstant());
115101
}
116102

117103
private SaltEntry[] rotateSalts(SaltEntry[] oldSalts, List<SaltEntry> saltsToRotate, TargetDate targetDate) throws Exception {
@@ -163,7 +149,7 @@ private List<SaltEntry> pickSaltsToRotate(
163149
TargetDate targetDate,
164150
Duration[] minAges,
165151
int numSaltsToRotate) {
166-
var maxSaltsPerAge = this.isRefreshFromEnabled ? (int) (numSaltsToRotate * MAX_SALT_PERCENTAGE) : numSaltsToRotate;
152+
var maxSaltsPerAge = (int) (numSaltsToRotate * MAX_SALT_PERCENTAGE);
167153

168154
var thresholds = Arrays.stream(minAges)
169155
.map(minAge -> targetDate.asInstant().minusSeconds(minAge.getSeconds()))

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,7 @@ private void handleSaltRotate(RoutingContext rc) {
132132
final Optional<Double> fraction = RequestUtil.getDouble(rc, "fraction");
133133
if (fraction.isEmpty()) return;
134134

135-
final Duration[] ageThresholds;
136-
if (saltRotation.isCustomAgeThresholdEnabled()) {
137-
ageThresholds = RequestUtil.getDurations(rc, "min_ages_in_seconds");
138-
if (ageThresholds == null) return;
139-
} else {
140-
ageThresholds = SALT_ROTATION_AGE_THRESHOLDS;
141-
}
142-
LOGGER.info("Salt rotation age thresholds in seconds: {}", Arrays.stream(ageThresholds).map(Duration::toSeconds).collect(Collectors.toList()));
135+
LOGGER.info("Salt rotation age thresholds in seconds: {}", Arrays.stream(SALT_ROTATION_AGE_THRESHOLDS).map(Duration::toSeconds).collect(Collectors.toList()));
143136

144137
final TargetDate targetDate =
145138
RequestUtil.getDate(rc, "target_date", DateTimeFormatter.ISO_LOCAL_DATE)
@@ -155,7 +148,7 @@ private void handleSaltRotate(RoutingContext rc) {
155148
final List<RotatingSaltProvider.SaltSnapshot> snapshots = saltProvider.getSnapshots();
156149
final RotatingSaltProvider.SaltSnapshot lastSnapshot = snapshots.getLast();
157150

158-
final SaltRotation.Result result = saltRotation.rotateSalts(lastSnapshot, ageThresholds, fraction.get(), targetDate);
151+
final SaltRotation.Result result = saltRotation.rotateSalts(lastSnapshot, SALT_ROTATION_AGE_THRESHOLDS, fraction.get(), targetDate);
159152
if (!result.hasSnapshot()) {
160153
ResponseUtil.error(rc, 200, result.getReason());
161154
return;

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

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -133,37 +133,9 @@ void rotateSaltsWithSpecificTargetDate(Vertx vertx, VertxTestContext testContext
133133
});
134134
}
135135

136-
@Test
137-
void rotateSaltsWithCustomAgeThresholdsEnabled(Vertx vertx, VertxTestContext testContext) throws Exception {
138-
fakeAuth(Role.SUPER_USER);
139-
140-
when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(true);
141-
142-
final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1));
143-
setSnapshots(lastSnapshot);
144-
145-
var result = SaltRotation.Result.fromSnapshot(SaltSnapshotBuilder.start().effective(targetDate()).expires(daysEarlier(7)).entries(1, targetDate()).build());
146-
147-
Duration[] expectedCustomAgeThresholds = new Duration[]{
148-
Duration.ofSeconds(50),
149-
Duration.ofSeconds(60),
150-
Duration.ofSeconds(70)
151-
};
152-
153-
when(saltRotation.rotateSalts(any(), eq(expectedCustomAgeThresholds), eq(0.2), eq(utcTomorrow))).thenReturn(result);
154-
155-
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));
157-
assertEquals(200, response.statusCode());
158-
testContext.completeNow();
159-
});
160-
}
161-
162136
@Test
163137
void rotateSaltsWithDefaultAgeThresholds(Vertx vertx, VertxTestContext testContext) throws Exception {
164-
fakeAuth(Role.SUPER_USER);
165-
166-
when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(false);
138+
fakeAuth(Role.SUPER_USER);
167139

168140
final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1));
169141
setSnapshots(lastSnapshot);
@@ -186,22 +158,6 @@ void rotateSaltsWithDefaultAgeThresholds(Vertx vertx, VertxTestContext testConte
186158
});
187159
}
188160

189-
@Test
190-
void rotateSaltsWithCustomAgeThresholdsEnabledButMissingParameter(Vertx vertx, VertxTestContext testContext) {
191-
fakeAuth(Role.SUPER_USER);
192-
193-
when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(true);
194-
195-
final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1));
196-
setSnapshots(lastSnapshot);
197-
198-
post(vertx, testContext, "api/salt/rotate?fraction=0.2", "", response -> {
199-
verify(saltRotation, never()).rotateSalts(any(), any(), anyDouble(), any());
200-
assertEquals(400, response.statusCode());
201-
testContext.completeNow();
202-
});
203-
}
204-
205161
private void checkSnapshotsResponse(SaltSnapshotBuilder[] expectedSnapshots, Object[] actualSnapshots) {
206162
assertEquals(expectedSnapshots.length, actualSnapshots.length);
207163
for (int i = 0; i < expectedSnapshots.length; ++i) {

0 commit comments

Comments
 (0)