diff --git a/src/main/java/com/uid2/admin/Main.java b/src/main/java/com/uid2/admin/Main.java index d9748eed0..f2562b296 100644 --- a/src/main/java/com/uid2/admin/Main.java +++ b/src/main/java/com/uid2/admin/Main.java @@ -16,7 +16,7 @@ import com.uid2.admin.managers.KeysetManager; import com.uid2.admin.cloudEncryption.CloudSecretGenerator; import com.uid2.admin.monitoring.DataStoreMetrics; -import com.uid2.admin.managers.CloudEncryptionKeyManager; +import com.uid2.admin.cloudEncryption.CloudEncryptionKeyManager; import com.uid2.admin.secret.*; import com.uid2.admin.store.*; import com.uid2.admin.store.reader.RotatingAdminKeysetStore; diff --git a/src/main/java/com/uid2/admin/managers/CloudEncryptionKeyManager.java b/src/main/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManager.java similarity index 71% rename from src/main/java/com/uid2/admin/managers/CloudEncryptionKeyManager.java rename to src/main/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManager.java index 76805b18c..f23e037fc 100644 --- a/src/main/java/com/uid2/admin/managers/CloudEncryptionKeyManager.java +++ b/src/main/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManager.java @@ -1,6 +1,5 @@ -package com.uid2.admin.managers; +package com.uid2.admin.cloudEncryption; -import com.uid2.admin.cloudEncryption.CloudSecretGenerator; import com.uid2.admin.store.writer.CloudEncryptionKeyStoreWriter; import com.uid2.shared.auth.OperatorKey; import com.uid2.shared.model.CloudEncryptionKey; @@ -8,10 +7,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - import java.time.Instant; import java.util.*; @@ -107,49 +102,6 @@ int getNextKeyId() { return cloudEncryptionKeys.keySet().stream().max(Integer::compareTo).orElse(0) + 1; } - // Used in test only - // Creates and uploads a CloudEncryptionKey that activates immediately for a specific sites, for emergency rotation - CloudEncryptionKey createAndAddImmediateCloudEncryptionKey(int siteId) throws Exception { - int newKeyId = getNextKeyId(); - long created = Instant.now().getEpochSecond(); - CloudEncryptionKey newKey = new CloudEncryptionKey(newKeyId, siteId, created, created, secretGenerator.generate()); - addCloudEncryptionKey(newKey); - return newKey; - } - - // Used in test only - CloudEncryptionKey getCloudEncryptionKeyByKeyIdentifier(int keyIdentifier) { - return RotatingCloudEncryptionKeyProvider.getAll().get(keyIdentifier); - } - - // Used in test only - Optional getCloudEncryptionKeyBySiteId(int siteId) { - return RotatingCloudEncryptionKeyProvider.getAll().values().stream() - .filter(key -> key.getSiteId() == siteId) - .findFirst(); - } - - // Used in test only - List getAllCloudEncryptionKeysBySiteId(int siteId) { - return RotatingCloudEncryptionKeyProvider.getAll().values().stream() - .filter(key -> key.getSiteId() == siteId) - .collect(Collectors.toList()); - } - - // Used in test only - Map getAllCloudEncryptionKeys() { - return RotatingCloudEncryptionKeyProvider.getAll(); - } - - // Used in test only - boolean doesSiteHaveKeys(int siteId) { - Map allKeys = RotatingCloudEncryptionKeyProvider.getAll(); - if (allKeys == null) { - return false; - } - return allKeys.values().stream().anyMatch(key -> key.getSiteId() == siteId); - } - int countKeysForSite(int siteId) { Map allKeys = RotatingCloudEncryptionKeyProvider.getAll(); return (int) allKeys.values().stream().filter(key -> key.getSiteId() == siteId).count(); diff --git a/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java b/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java index 4d7e6c752..8a360de1d 100644 --- a/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java +++ b/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.uid2.admin.auth.AdminAuthMiddleware; import com.uid2.admin.auth.RevealedKey; -import com.uid2.admin.managers.CloudEncryptionKeyManager; +import com.uid2.admin.cloudEncryption.CloudEncryptionKeyManager; import com.uid2.shared.model.Site; import com.uid2.shared.secret.IKeyGenerator; import com.uid2.admin.store.writer.OperatorKeyStoreWriter; diff --git a/src/test/java/com/uid2/admin/managers/CloudEncryptionKeyManagerTest.java b/src/test/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManagerTest.java similarity index 62% rename from src/test/java/com/uid2/admin/managers/CloudEncryptionKeyManagerTest.java rename to src/test/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManagerTest.java index 418378164..4a84fed37 100644 --- a/src/test/java/com/uid2/admin/managers/CloudEncryptionKeyManagerTest.java +++ b/src/test/java/com/uid2/admin/cloudEncryption/CloudEncryptionKeyManagerTest.java @@ -1,6 +1,5 @@ -package com.uid2.admin.managers; +package com.uid2.admin.cloudEncryption; -import com.uid2.admin.cloudEncryption.CloudSecretGenerator; import com.uid2.admin.store.writer.CloudEncryptionKeyStoreWriter; import com.uid2.shared.auth.OperatorKey; import com.uid2.shared.model.CloudEncryptionKey; @@ -99,33 +98,6 @@ void testGetNextKeyId() { assertEquals(2, nextKeyId); } - @Test - void testGetCloudEncryptionKey() { - CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(1, siteId, 500L, 1500L, "existingSecret1"); - Map existingKeys = new HashMap<>(); - existingKeys.put(1, cloudEncryptionKey); - when(cloudEncryptionKeyProvider.getAll()).thenReturn(existingKeys); - - CloudEncryptionKey result = cloudEncryptionKeyManager.getCloudEncryptionKeyByKeyIdentifier(1); - - assertEquals(cloudEncryptionKey, result); - } - - @Test - void testGetAllCloudEncryptionKeys() { - Map existingKeys = new HashMap<>(); - CloudEncryptionKey existingKey1 = new CloudEncryptionKey(1, siteId, 500L, 1500L, "existingSecret1"); - CloudEncryptionKey existingKey2 = new CloudEncryptionKey(2, siteId, 600L, 1600L, "existingSecret2"); - existingKeys.put(1, existingKey1); - existingKeys.put(2, existingKey2); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(existingKeys); - - Map result = cloudEncryptionKeyManager.getAllCloudEncryptionKeys(); - - assertEquals(existingKeys, result); - } - @Test void testAddCloudEncryptionKey() throws Exception { CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(1, siteId, 1000L, 2000L, "randomKeyString"); @@ -143,135 +115,6 @@ void testAddCloudEncryptionKey() throws Exception { assertEquals(cloudEncryptionKey, capturedKeys.get(1)); } - @Test - void testGetCloudEncryptionKeyBySiteId() { - CloudEncryptionKey key1 = new CloudEncryptionKey(1, 100, 0, 0, "secret1"); - CloudEncryptionKey key2 = new CloudEncryptionKey(2, 200, 0, 0, "secret2"); - Map keys = new HashMap<>(); - keys.put(1, key1); - keys.put(2, key2); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(keys); - - Optional result = cloudEncryptionKeyManager.getCloudEncryptionKeyBySiteId(100); - assertTrue(result.isPresent()); - assertEquals(key1, result.get()); - } - - @Test - void testGetAllCloudEncryptionKeysBySiteId() { - CloudEncryptionKey key1 = new CloudEncryptionKey(1, 100, 0, 0, "secret1"); - CloudEncryptionKey key2 = new CloudEncryptionKey(2, 100, 0, 0, "secret2"); - CloudEncryptionKey key3 = new CloudEncryptionKey(3, 200, 0, 0, "secret3"); - Map keys = new HashMap<>(); - keys.put(1, key1); - keys.put(2, key2); - keys.put(3, key3); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(keys); - - List result = cloudEncryptionKeyManager.getAllCloudEncryptionKeysBySiteId(100); - assertEquals(2, result.size()); - assertTrue(result.contains(key1)); - assertTrue(result.contains(key2)); - } - - @Test - void testCreateAndAddImmediateCloudEncryptionKey() throws Exception { - when(cloudEncryptionKeyProvider.getAll()).thenReturn(new HashMap<>()); - when(keyGenerator.generate()).thenReturn("generatedSecret"); - - CloudEncryptionKey newKey = cloudEncryptionKeyManager.createAndAddImmediateCloudEncryptionKey(100); - - assertNotNull(newKey); - assertEquals(100, newKey.getSiteId()); - assertEquals("generatedSecret", newKey.getSecret()); - - verify(cloudEncryptionKeyStoreWriter, times(1)).upload(any(Map.class), eq(null)); - } - - @Test - public void testDoesSiteHaveKeys_SiteHasKeys() { - CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(siteId, siteId, 0L, 0L, "key"); - Map allKeys = new HashMap<>(); - allKeys.put(1, cloudEncryptionKey); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(allKeys); - - boolean result = cloudEncryptionKeyManager.doesSiteHaveKeys(siteId); - assertTrue(result); - } - - @Test - public void testDoesSiteHaveKeys_SiteDoesNotHaveKeys() { - Map allKeys = new HashMap<>(); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(allKeys); - - boolean result = cloudEncryptionKeyManager.doesSiteHaveKeys(siteId); - assertFalse(result); - } - - @Test - public void testDoesSiteHaveKeys_AllKeysNull() { - when(cloudEncryptionKeyProvider.getAll()).thenReturn(null); - - boolean result = cloudEncryptionKeyManager.doesSiteHaveKeys(siteId); - assertFalse(result); - } - - @Test - public void testDoesSiteHaveKeys_MultipleKeysDifferentSiteIds() { - CloudEncryptionKey cloudEncryptionKey1 = new CloudEncryptionKey(1, 1, 0L, 0L, "key1"); - CloudEncryptionKey cloudEncryptionKey2 = new CloudEncryptionKey(2, 2, 0L, 0L, "key2"); - Map allKeys = new HashMap<>(); - allKeys.put(1, cloudEncryptionKey1); - allKeys.put(2, cloudEncryptionKey2); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(allKeys); - - assertTrue(cloudEncryptionKeyManager.doesSiteHaveKeys(1)); - assertTrue(cloudEncryptionKeyManager.doesSiteHaveKeys(2)); - assertFalse(cloudEncryptionKeyManager.doesSiteHaveKeys(3)); // Site ID 3 does not exist - } - - @Test - public void testDoesSiteHaveKeys_SameSiteIdMultipleKeys() { - CloudEncryptionKey cloudEncryptionKey1 = new CloudEncryptionKey(siteId, siteId, 0L, 0L, "key1"); - CloudEncryptionKey cloudEncryptionKey2 = new CloudEncryptionKey(siteId, siteId, 0L, 0L, "key2"); - Map allKeys = new HashMap<>(); - allKeys.put(1, cloudEncryptionKey1); - allKeys.put(2, cloudEncryptionKey2); - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(allKeys); - - boolean result = cloudEncryptionKeyManager.doesSiteHaveKeys(siteId); - assertTrue(result); - } - - @Test - public void testDoesSiteHaveKeys_LargeNumberOfKeys() { - Map allKeys = new HashMap<>(); - for (int i = 1; i <= 1000; i++) { - CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(i, i, 0L, 0L, "key" + i); - allKeys.put(i, cloudEncryptionKey); - } - - when(cloudEncryptionKeyProvider.getAll()).thenReturn(allKeys); - - for (int i = 1; i <= 1000; i++) { - assertTrue(cloudEncryptionKeyManager.doesSiteHaveKeys(i)); - } - assertFalse(cloudEncryptionKeyManager.doesSiteHaveKeys(1001)); // Site ID 1001 does not exist - } - - @Test - public void testDoesSiteHaveKeys_EmptyKeys() { - when(cloudEncryptionKeyProvider.getAll()).thenReturn(new HashMap<>()); - - assertFalse(cloudEncryptionKeyManager.doesSiteHaveKeys(1)); - } - @Test void testCountKeysForSite() { Map testKeys = new HashMap<>(); diff --git a/src/test/java/com/uid2/admin/vertx/OperatorKeyServiceTest.java b/src/test/java/com/uid2/admin/vertx/OperatorKeyServiceTest.java index 831f23118..e89f73f30 100644 --- a/src/test/java/com/uid2/admin/vertx/OperatorKeyServiceTest.java +++ b/src/test/java/com/uid2/admin/vertx/OperatorKeyServiceTest.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.uid2.admin.auth.RevealedKey; -import com.uid2.admin.managers.CloudEncryptionKeyManager; +import com.uid2.admin.cloudEncryption.CloudEncryptionKeyManager; import com.uid2.admin.vertx.service.IService; import com.uid2.admin.vertx.service.OperatorKeyService; import com.uid2.admin.vertx.test.ServiceTestBase;