From f820cccab632e87d8a13e98b0fb52845d193178d Mon Sep 17 00:00:00 2001 From: Cody Constine Date: Wed, 11 Dec 2024 14:09:33 -0700 Subject: [PATCH] Making change to multiscope writer to not copy all salts for each site --- .../job/EncryptionJob/SaltEncryptionJob.java | 9 +++++---- .../admin/store/MultiScopeStoreWriter.java | 14 ++++++++++++++ .../admin/store/factory/SaltStoreFactory.java | 7 +++++++ .../com/uid2/admin/util/PrivateSiteUtil.java | 13 ++----------- .../com/uid2/admin/util/PublicSiteUtil.java | 18 +++--------------- .../writer/EncryptedSaltStoreWriterTest.java | 6 +++++- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/uid2/admin/job/EncryptionJob/SaltEncryptionJob.java b/src/main/java/com/uid2/admin/job/EncryptionJob/SaltEncryptionJob.java index 6ab992f8..4ffb7b11 100644 --- a/src/main/java/com/uid2/admin/job/EncryptionJob/SaltEncryptionJob.java +++ b/src/main/java/com/uid2/admin/job/EncryptionJob/SaltEncryptionJob.java @@ -10,6 +10,7 @@ import com.uid2.shared.store.RotatingSaltProvider; import java.util.Collection; +import java.util.List; public class SaltEncryptionJob extends Job { private final Collection globalOperators; @@ -32,9 +33,9 @@ public String getId() { @Override public void execute() throws Exception { - PrivateSiteDataMap desiredPrivateState = PrivateSiteUtil.getPrivateSaltEntries(saltEntries, globalOperators); - multiScopeStoreWriter.uploadPrivateWithEncryption(desiredPrivateState, null); - PrivateSiteDataMap desiredPublicState = PublicSiteUtil.getPublicSaltEntries(saltEntries, globalOperators); - multiScopeStoreWriter.uploadPublicWithEncryption(desiredPublicState, null); + List desiredPrivateState = PrivateSiteUtil.getPrivateSaltSites(globalOperators); + multiScopeStoreWriter.uploadPrivateWithEncryption(desiredPrivateState, saltEntries, null); + List desiredPublicState = PublicSiteUtil.getPublicSaltSites(globalOperators); + multiScopeStoreWriter.uploadPublicWithEncryption(desiredPublicState, saltEntries, null); } } diff --git a/src/main/java/com/uid2/admin/store/MultiScopeStoreWriter.java b/src/main/java/com/uid2/admin/store/MultiScopeStoreWriter.java index 879b4e12..7c2205ca 100644 --- a/src/main/java/com/uid2/admin/store/MultiScopeStoreWriter.java +++ b/src/main/java/com/uid2/admin/store/MultiScopeStoreWriter.java @@ -69,6 +69,13 @@ public void uploadPrivateWithEncryption(Map desiredState, JsonObject } } + public void uploadPrivateWithEncryption(List siteIds, T desiredState, JsonObject extraMeta) throws Exception { + EncryptedStoreFactory encryptedFactory = (EncryptedStoreFactory) factory; + for (Integer siteId : siteIds) { + encryptedFactory.getEncryptedWriter(siteId,false).upload(desiredState, extraMeta); + } + } + public void uploadPublicWithEncryption(Map desiredPublicState, JsonObject extraMeta) throws Exception { EncryptedStoreFactory encryptedFactory = (EncryptedStoreFactory) factory; for (Map.Entry entry : desiredPublicState.entrySet()) { @@ -77,6 +84,13 @@ public void uploadPublicWithEncryption(Map desiredPublicState, JsonO } } + public void uploadPublicWithEncryption(List siteIds, T desiredState, JsonObject extraMeta) throws Exception { + EncryptedStoreFactory encryptedFactory = (EncryptedStoreFactory) factory; + for (Integer siteId : siteIds) { + encryptedFactory.getEncryptedWriter(siteId,true).upload(desiredState, extraMeta); + } + } + public static boolean areMapsEqual(Map a, Map b) { return a.size() == b.size() && a.entrySet().stream().allMatch(b.entrySet()::contains); } diff --git a/src/main/java/com/uid2/admin/store/factory/SaltStoreFactory.java b/src/main/java/com/uid2/admin/store/factory/SaltStoreFactory.java index afce4442..bd1fbcb9 100644 --- a/src/main/java/com/uid2/admin/store/factory/SaltStoreFactory.java +++ b/src/main/java/com/uid2/admin/store/factory/SaltStoreFactory.java @@ -13,10 +13,14 @@ import com.uid2.shared.store.reader.StoreReader; import com.uid2.shared.store.scope.EncryptedScope; import io.vertx.core.json.JsonObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Collection; public class SaltStoreFactory implements EncryptedStoreFactory> { + private static final Logger LOGGER = LoggerFactory.getLogger(SaltStoreFactory.class); + JsonObject config; CloudPath rootMetadatapath; FileManager fileManager; @@ -45,6 +49,7 @@ public StoreWriter> getEncryptedWr @Override public StoreReader> getEncryptedReader(Integer siteId, boolean isPublic) { + LOGGER.warn("getEncryptedReader called on SaltStoreFactory. This method is not implemented."); return null; } @@ -55,11 +60,13 @@ public RotatingCloudEncryptionKeyProvider getCloudEncryptionProvider() { @Override public StoreReader> getReader(Integer siteId) { + LOGGER.warn("getReader called on SaltStoreFactory. This method is not implemented."); return null; } @Override public StoreWriter> getWriter(Integer siteId) { + LOGGER.warn("getWriter called on SaltStoreFactory. This method is not implemented."); return null; } } diff --git a/src/main/java/com/uid2/admin/util/PrivateSiteUtil.java b/src/main/java/com/uid2/admin/util/PrivateSiteUtil.java index f065ac79..525b9494 100644 --- a/src/main/java/com/uid2/admin/util/PrivateSiteUtil.java +++ b/src/main/java/com/uid2/admin/util/PrivateSiteUtil.java @@ -313,17 +313,8 @@ public static PrivateSiteDataMap getKeysetKeys(Collection getPrivateSaltEntries( - Collection globalSaltEntries, - Collection operators) { + public static List getPrivateSaltSites(Collection operators) { final PrivateSiteDataMap result = getPrivateSites(operators); - - globalSaltEntries.forEach(saltEntry -> { - result.forEach((publicSiteId, publicSiteData) -> { - publicSiteData.add(saltEntry); - }); - }); - - return result; + return result.keySet().stream().toList(); } } diff --git a/src/main/java/com/uid2/admin/util/PublicSiteUtil.java b/src/main/java/com/uid2/admin/util/PublicSiteUtil.java index f3ec8fd9..815454b4 100644 --- a/src/main/java/com/uid2/admin/util/PublicSiteUtil.java +++ b/src/main/java/com/uid2/admin/util/PublicSiteUtil.java @@ -11,10 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; +import java.util.*; public class PublicSiteUtil { private static final Logger LOGGER = LoggerFactory.getLogger(PrivateSiteUtil.class); @@ -128,18 +125,9 @@ public static PrivateSiteDataMap getPublicKeysetKeys( return result; } - public static PrivateSiteDataMap getPublicSaltEntries( - Collection globalSaltEntries, - Collection operators) { + public static List getPublicSaltSites(Collection operators) { final PrivateSiteDataMap result = getPublicSitesMap(operators); - - globalSaltEntries.forEach(saltEntry -> { - result.forEach((publicSiteId, publicSiteData) -> { - publicSiteData.add(saltEntry); - }); - }); - - return result; + return result.keySet().stream().toList(); } public static PrivateSiteDataMap getPublicClientKeypairs( diff --git a/src/test/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriterTest.java b/src/test/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriterTest.java index e351ad5e..207dbf05 100644 --- a/src/test/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriterTest.java +++ b/src/test/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriterTest.java @@ -59,6 +59,8 @@ public class EncryptedSaltStoreWriterTest { @Captor private ArgumentCaptor pathCaptor; + @Captor + private ArgumentCaptor cloudPathCaptor; @BeforeEach public void setUp() throws Exception { @@ -118,7 +120,9 @@ public void testUploadNew() throws Exception { encryptedSaltStoreWriter.upload(snapshot); - verify(taggableCloudStorage).upload(pathCaptor.capture(), any(), any()); + verify(taggableCloudStorage).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); + assertEquals(cloudPathCaptor.getValue(), "test/path"); + verifyFile(pathCaptor.getValue(), snapshot); } }