Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/uid2/admin/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cloudencryption

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename in future commit. Don't really find that super readable but better stick to conventions.

import com.uid2.admin.secret.*;
import com.uid2.admin.store.*;
import com.uid2.admin.store.reader.RotatingAdminKeysetStore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
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;
import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider;
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.*;

Expand Down Expand Up @@ -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<CloudEncryptionKey> getCloudEncryptionKeyBySiteId(int siteId) {
return RotatingCloudEncryptionKeyProvider.getAll().values().stream()
.filter(key -> key.getSiteId() == siteId)
.findFirst();
}

// Used in test only
List<CloudEncryptionKey> getAllCloudEncryptionKeysBySiteId(int siteId) {
return RotatingCloudEncryptionKeyProvider.getAll().values().stream()
.filter(key -> key.getSiteId() == siteId)
.collect(Collectors.toList());
}

// Used in test only
Map<Integer, CloudEncryptionKey> getAllCloudEncryptionKeys() {
return RotatingCloudEncryptionKeyProvider.getAll();
}

// Used in test only
boolean doesSiteHaveKeys(int siteId) {
Map<Integer, CloudEncryptionKey> allKeys = RotatingCloudEncryptionKeyProvider.getAll();
if (allKeys == null) {
return false;
}
return allKeys.values().stream().anyMatch(key -> key.getSiteId() == siteId);
}

int countKeysForSite(int siteId) {
Map<Integer, CloudEncryptionKey> allKeys = RotatingCloudEncryptionKeyProvider.getAll();
return (int) allKeys.values().stream().filter(key -> key.getSiteId() == siteId).count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -99,33 +98,6 @@ void testGetNextKeyId() {
assertEquals(2, nextKeyId);
}

@Test
void testGetCloudEncryptionKey() {
CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(1, siteId, 500L, 1500L, "existingSecret1");
Map<Integer, CloudEncryptionKey> existingKeys = new HashMap<>();
existingKeys.put(1, cloudEncryptionKey);
when(cloudEncryptionKeyProvider.getAll()).thenReturn(existingKeys);

CloudEncryptionKey result = cloudEncryptionKeyManager.getCloudEncryptionKeyByKeyIdentifier(1);

assertEquals(cloudEncryptionKey, result);
}

@Test
void testGetAllCloudEncryptionKeys() {
Map<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> result = cloudEncryptionKeyManager.getAllCloudEncryptionKeys();

assertEquals(existingKeys, result);
}

@Test
void testAddCloudEncryptionKey() throws Exception {
CloudEncryptionKey cloudEncryptionKey = new CloudEncryptionKey(1, siteId, 1000L, 2000L, "randomKeyString");
Expand All @@ -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<Integer, CloudEncryptionKey> keys = new HashMap<>();
keys.put(1, key1);
keys.put(2, key2);

when(cloudEncryptionKeyProvider.getAll()).thenReturn(keys);

Optional<CloudEncryptionKey> 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<Integer, CloudEncryptionKey> keys = new HashMap<>();
keys.put(1, key1);
keys.put(2, key2);
keys.put(3, key3);

when(cloudEncryptionKeyProvider.getAll()).thenReturn(keys);

List<CloudEncryptionKey> 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<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> 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<Integer, CloudEncryptionKey> testKeys = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down