Skip to content

Commit fbfd5da

Browse files
Store previous snapshots in encrypted
1 parent f80094c commit fbfd5da

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/main/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
import java.nio.charset.StandardCharsets;
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
21+
import java.util.ArrayList;
2122
import java.util.Base64;
2223
import java.util.Collection;
24+
import java.util.List;
2325

2426
public class EncryptedSaltStoreWriter extends SaltStoreWriter implements StoreWriter {
2527
private StoreScope scope;
2628
private RotatingCloudEncryptionKeyProvider cloudEncryptionKeyProvider;
2729
private Integer siteId;
2830

31+
private final List<RotatingSaltProvider.SaltSnapshot> previousSeenSnapshots = new ArrayList<>();
32+
2933
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptedSaltStoreWriter.class);
3034
public EncryptedSaltStoreWriter(JsonObject config, RotatingSaltProvider provider, FileManager fileManager,
3135
TaggableCloudStorage cloudStorage, VersionGenerator versionGenerator, StoreScope scope,
@@ -91,6 +95,16 @@ protected void refreshProvider() {
9195
// we do not need to refresh the provider on encrypted writers
9296
}
9397

98+
@Override
99+
protected List<RotatingSaltProvider.SaltSnapshot> getSnapshots(RotatingSaltProvider.SaltSnapshot data){
100+
/*
101+
Since metadata.json is overwritten during the process, we maintain a history of all snapshots seen so far.
102+
On the final write, we append this history to metadata.json to ensure no snapshots are lost.
103+
*/
104+
this.previousSeenSnapshots.add(data);
105+
return this.previousSeenSnapshots;
106+
}
107+
94108
@Override
95109
public void upload(Object data, JsonObject extraMeta) throws Exception {
96110
for(RotatingSaltProvider.SaltSnapshot saltSnapshot: (Collection<RotatingSaltProvider.SaltSnapshot>) data) {

src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public SaltStoreWriter(JsonObject config, RotatingSaltProvider provider, FileMan
4343
this.versionGenerator = versionGenerator;
4444
}
4545

46+
protected List<RotatingSaltProvider.SaltSnapshot> getSnapshots(RotatingSaltProvider.SaltSnapshot data){
47+
return Stream.concat(provider.getSnapshots().stream(), Stream.of(data))
48+
.sorted(Comparator.comparing(RotatingSaltProvider.SaltSnapshot::getEffective))
49+
.collect(Collectors.toList());
50+
}
51+
4652
public void upload(RotatingSaltProvider.SaltSnapshot data) throws Exception {
4753
final Instant now = Instant.now();
4854
final long generated = now.getEpochSecond();
@@ -65,15 +71,8 @@ public void upload(RotatingSaltProvider.SaltSnapshot data) throws Exception {
6571
metadata.put("salts", snapshotsMetadata);
6672

6773
List<RotatingSaltProvider.SaltSnapshot> currentSnapshots = provider.getSnapshots();
68-
List<RotatingSaltProvider.SaltSnapshot> snapshots = null;
69-
70-
if (currentSnapshots != null) {
71-
snapshots = Stream.concat(currentSnapshots.stream(), Stream.of(data))
72-
.sorted(Comparator.comparing(RotatingSaltProvider.SaltSnapshot::getEffective))
73-
.collect(Collectors.toList());
74-
} else {
75-
snapshots = List.of(data);
76-
}
74+
List<RotatingSaltProvider.SaltSnapshot> snapshots = this.getSnapshots(data);
75+
7776
// of the currently effective snapshots keep only the most recent one
7877
RotatingSaltProvider.SaltSnapshot newestEffectiveSnapshot = snapshots.stream()
7978
.filter(snapshot -> snapshot.isEffective(now))

0 commit comments

Comments
 (0)