-
Notifications
You must be signed in to change notification settings - Fork 6
Key rotation API with retention of 5 last expired keys #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/uid2/admin/cloudEncryption/CloudKeyRetentionStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.uid2.admin.cloudEncryption; | ||
|
|
||
| import com.uid2.shared.model.CloudEncryptionKey; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| public interface CloudKeyRetentionStrategy { | ||
| Set<CloudEncryptionKey> selectKeysToRetain(Set<CloudEncryptionKey> keysForSite); | ||
| } |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/uid2/admin/cloudEncryption/CloudKeyRotationStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.uid2.admin.cloudEncryption; | ||
|
|
||
| import com.google.common.collect.Streams; | ||
| import com.uid2.admin.store.Clock; | ||
| import com.uid2.shared.model.CloudEncryptionKey; | ||
| import com.uid2.shared.model.Site; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class CloudKeyRotationStrategy { | ||
| private final CloudSecretGenerator secretGenerator; | ||
| private final Clock clock; | ||
| private final CloudKeyRetentionStrategy keyRetentionStrategy; | ||
|
|
||
| public CloudKeyRotationStrategy( | ||
| CloudSecretGenerator secretGenerator, | ||
| Clock clock, | ||
| CloudKeyRetentionStrategy keyRetentionStrategy | ||
| ) { | ||
| this.secretGenerator = secretGenerator; | ||
| this.clock = clock; | ||
| this.keyRetentionStrategy = keyRetentionStrategy; | ||
| } | ||
|
|
||
| public Set<CloudEncryptionKey> computeDesiredKeys( | ||
| Collection<CloudEncryptionKey> existingKeys, | ||
| Collection<Site> sites | ||
| ) { | ||
| var idGenerator = new KeyIdGenerator(existingKeys); | ||
| Map<Integer, Set<CloudEncryptionKey>> existingKeysBySite = existingKeys | ||
| .stream() | ||
| .collect(Collectors.groupingBy(CloudEncryptionKey::getSiteId, Collectors.toSet())); | ||
|
|
||
| return sites | ||
| .stream() | ||
| .map(Site::getId) | ||
| .distinct() | ||
| .flatMap(siteId -> desiredKeysForSite(siteId, idGenerator, existingKeysBySite.getOrDefault(siteId, Set.of()))) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private Stream<CloudEncryptionKey> desiredKeysForSite( | ||
| Integer siteId, | ||
| KeyIdGenerator idGenerator, | ||
| Set<CloudEncryptionKey> existingKeys | ||
| ) { | ||
| var existingKeysToRetain = keyRetentionStrategy.selectKeysToRetain(existingKeys); | ||
| var newKey = makeNewKey(siteId, idGenerator); | ||
| return Streams.concat(existingKeysToRetain.stream(), Stream.of(newKey)); | ||
| } | ||
|
|
||
| private CloudEncryptionKey makeNewKey(Integer siteId, KeyIdGenerator idGenerator) { | ||
| var nowSeconds = clock.getEpochSecond(); | ||
| var keyId = idGenerator.nextId(); | ||
| var secret = secretGenerator.generate(); | ||
| return new CloudEncryptionKey(keyId, siteId, nowSeconds, nowSeconds, secret); | ||
| } | ||
|
|
||
| private static class KeyIdGenerator { | ||
| private int lastId; | ||
|
|
||
| public KeyIdGenerator(Collection<CloudEncryptionKey> existingKeys) { | ||
| this.lastId = existingKeys.stream().map(CloudEncryptionKey::getId).max(Integer::compareTo).orElse(0); | ||
| } | ||
|
|
||
| public int nextId() { | ||
| return ++lastId; | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/uid2/admin/cloudEncryption/CloudSecretGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.uid2.admin.cloudEncryption; | ||
|
|
||
| import com.uid2.shared.secret.SecureKeyGenerator; | ||
|
|
||
| public class CloudSecretGenerator { | ||
| private final SecureKeyGenerator keyGenerator; | ||
|
|
||
| // The SecureKeyGenerator is preferable to the IKeyGenerator interface as it doesn't throw Exception | ||
| public CloudSecretGenerator(SecureKeyGenerator keyGenerator) { | ||
| this.keyGenerator = keyGenerator; | ||
| } | ||
|
|
||
| public String generate() { | ||
| //Generate a 32-byte key for AesGcm | ||
| return keyGenerator.generateRandomKeyString(32); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/uid2/admin/cloudEncryption/ExpiredKeyCountRetentionStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.uid2.admin.cloudEncryption; | ||
|
|
||
| import com.uid2.admin.store.Clock; | ||
| import com.uid2.shared.model.CloudEncryptionKey; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| // Only keep keys past activation time - no future keys allowed | ||
| // Keep up to `expiredKeysToRetain` most recent keys | ||
| // Considers all keys passed, doesn't do grouping by site (handled upstream) | ||
| public class ExpiredKeyCountRetentionStrategy implements CloudKeyRetentionStrategy { | ||
| private final Clock clock; | ||
| private final int expiredKeysToRetain; | ||
|
|
||
| public ExpiredKeyCountRetentionStrategy(Clock clock, int expiredKeysToRetain) { | ||
| this.clock = clock; | ||
| this.expiredKeysToRetain = expiredKeysToRetain; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<CloudEncryptionKey> selectKeysToRetain(Set<CloudEncryptionKey> keysForSite) { | ||
| return keysForSite.stream() | ||
| .filter(key -> key.getActivates() <= clock.getEpochSecond()) | ||
| .sorted(Comparator.comparingLong(CloudEncryptionKey::getActivates).reversed()) | ||
| .limit(expiredKeysToRetain) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.