-
Notifications
You must be signed in to change notification settings - Fork 3
feat(sdk): cache ListKeyAccessServers response during decryption #391
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
base: main
Are you sure you want to change the base?
Changes from all commits
41e53e2
5b27531
2174dd7
b5254ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.opentdf.platform.sdk; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| // Mirrors the KASKeyCache pattern: null returns, LocalDateTime.now(), and 5-minute TTL. | ||
| class KASAllowlistCache { | ||
| private static final Logger log = LoggerFactory.getLogger(KASAllowlistCache.class); | ||
| final Map<String, TimeStampedAllowList> cache = new ConcurrentHashMap<>(); | ||
|
|
||
| public void clear() { | ||
| cache.clear(); | ||
| } | ||
|
|
||
| public Set<String> get(String platformURL) { | ||
| log.debug("retrieving allowlist for platformURL = [{}]", platformURL); | ||
| TimeStampedAllowList cachedValue = cache.get(platformURL); | ||
| if (cachedValue == null) { | ||
| log.debug("didn't find allowlist for platformURL = [{}]", platformURL); | ||
| return null; | ||
|
Check warning on line 28 in sdk/src/main/java/io/opentdf/platform/sdk/KASAllowlistCache.java
|
||
| } | ||
|
|
||
| LocalDateTime fiveMinAgo = LocalDateTime.now().minus(5, ChronoUnit.MINUTES); | ||
|
Check warning on line 31 in sdk/src/main/java/io/opentdf/platform/sdk/KASAllowlistCache.java
|
||
| if (fiveMinAgo.isAfter(cachedValue.timestamp)) { | ||
| log.debug("cached allowlist is too old timestamp = [{}] for platformURL = [{}]", | ||
| cachedValue.timestamp, platformURL); | ||
| cache.remove(platformURL); | ||
| return null; | ||
|
Check warning on line 36 in sdk/src/main/java/io/opentdf/platform/sdk/KASAllowlistCache.java
|
||
| } | ||
|
|
||
| log.debug("successfully returned allowlist for platformURL = [{}]", platformURL); | ||
| return new HashSet<>(cachedValue.allowlist); | ||
| } | ||
|
|
||
| public void store(String platformURL, Set<String> allowlist) { | ||
| log.debug("storing allowlist into the cache for platformURL = [{}]", platformURL); | ||
| cache.put(platformURL, new TimeStampedAllowList( | ||
| Collections.unmodifiableSet(new HashSet<>(allowlist)), LocalDateTime.now())); | ||
|
Check warning on line 46 in sdk/src/main/java/io/opentdf/platform/sdk/KASAllowlistCache.java
|
||
| } | ||
| } | ||
|
|
||
| class TimeStampedAllowList { | ||
| Set<String> allowlist; | ||
| LocalDateTime timestamp; | ||
|
|
||
| public TimeStampedAllowList(Set<String> allowlist, LocalDateTime timestamp) { | ||
| this.allowlist = allowlist; | ||
| this.timestamp = timestamp; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package io.opentdf.platform.sdk; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class KASAllowlistCacheTest { | ||
|
|
||
| private KASAllowlistCache cache; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| cache = new KASAllowlistCache(); | ||
| } | ||
|
|
||
| @Test | ||
| void testStoreAndGet_WithinTimeLimit() { | ||
| Set<String> allowlist = Set.of("https://kas1.example.org", "https://kas2.example.org"); | ||
| cache.store("https://platform.example.org", allowlist); | ||
|
|
||
| Set<String> result = cache.get("https://platform.example.org"); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(2, result.size()); | ||
| assertTrue(result.contains("https://kas1.example.org")); | ||
| assertTrue(result.contains("https://kas2.example.org")); | ||
| } | ||
|
|
||
| @Test | ||
| void testStoreAndGet_AfterTimeLimit() { | ||
| Set<String> allowlist = Set.of("https://kas.example.org"); | ||
| cache.store("https://platform.example.org", allowlist); | ||
|
|
||
| TimeStampedAllowList expired = new TimeStampedAllowList(allowlist, LocalDateTime.now().minus(6, ChronoUnit.MINUTES)); | ||
| cache.cache.put("https://platform.example.org", expired); | ||
|
|
||
| Set<String> result = cache.get("https://platform.example.org"); | ||
|
|
||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGet_EmptyCache() { | ||
| Set<String> result = cache.get("https://platform.example.org"); | ||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGet_DifferentKey() { | ||
| Set<String> allowlist = Set.of("https://kas.example.org"); | ||
| cache.store("https://platform.example.org", allowlist); | ||
|
|
||
| Set<String> result = cache.get("https://other.example.org"); | ||
|
|
||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testClearCache() { | ||
| Set<String> allowlist = Set.of("https://kas.example.org"); | ||
| cache.store("https://platform.example.org", allowlist); | ||
|
|
||
| cache.clear(); | ||
|
|
||
| Set<String> result = cache.get("https://platform.example.org"); | ||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testStoreMultipleAndGet() { | ||
| Set<String> allowlist1 = Set.of("https://kas1.example.org"); | ||
| Set<String> allowlist2 = Set.of("https://kas2.example.org"); | ||
| cache.store("https://platform1.example.org", allowlist1); | ||
| cache.store("https://platform2.example.org", allowlist2); | ||
|
|
||
| Set<String> result1 = cache.get("https://platform1.example.org"); | ||
| Set<String> result2 = cache.get("https://platform2.example.org"); | ||
|
|
||
| assertNotNull(result1); | ||
| assertTrue(result1.contains("https://kas1.example.org")); | ||
| assertNotNull(result2); | ||
| assertTrue(result2.contains("https://kas2.example.org")); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.