diff --git a/arex-storage-web-api/pom.xml b/arex-storage-web-api/pom.xml index 09aa799f..feac3471 100644 --- a/arex-storage-web-api/pom.xml +++ b/arex-storage-web-api/pom.xml @@ -134,6 +134,22 @@ storage + + + src/main/resources + + **/*.jar + + true + + + src/main/resources + + **/*.yaml + **/*.jar + + + maven-compiler-plugin diff --git a/arex-storage-web-api/src/main/java/com/arextest/storage/beans/StorageAutoConfiguration.java b/arex-storage-web-api/src/main/java/com/arextest/storage/beans/StorageAutoConfiguration.java index eddf89c3..51cb06b2 100644 --- a/arex-storage-web-api/src/main/java/com/arextest/storage/beans/StorageAutoConfiguration.java +++ b/arex-storage-web-api/src/main/java/com/arextest/storage/beans/StorageAutoConfiguration.java @@ -4,6 +4,7 @@ import com.arextest.common.config.ConfigProvider; import com.arextest.common.config.DefaultApplicationConfig; import com.arextest.common.config.DefaultConfigProvider; +import com.arextest.common.desensitization.DesensitizationProvider; import com.arextest.common.jwt.JWTService; import com.arextest.common.jwt.JWTServiceImpl; import com.arextest.config.model.dao.config.SystemConfigurationCollection; @@ -13,6 +14,7 @@ import com.arextest.config.repository.impl.ApplicationServiceConfigurationRepositoryImpl; import com.arextest.config.repository.impl.SystemConfigurationRepositoryImpl; import com.arextest.config.utils.MongoHelper; +import com.arextest.extension.desensitization.DataDesensitization; import com.arextest.model.mock.AREXMocker; import com.arextest.model.mock.AREXQueryMocker; import com.arextest.model.mock.MockCategoryType; @@ -29,7 +31,6 @@ import com.arextest.storage.repository.RepositoryProviderFactory; import com.arextest.storage.repository.impl.mongo.AREXMockerMongoRepositoryProvider; import com.arextest.storage.repository.impl.mongo.AREXQueryMockerMongoRepositoryProvider; -import com.arextest.storage.repository.impl.mongo.DesensitizationLoader; import com.arextest.storage.repository.impl.mongo.converters.ArexEigenCompressionConverter; import com.arextest.storage.repository.impl.mongo.converters.ArexMockerCompressionConverter; import com.arextest.storage.serialization.ZstdJacksonSerializer; @@ -42,6 +43,8 @@ import com.arextest.storage.service.ScheduleReplayingService; import com.arextest.storage.service.config.ApplicationService; import com.arextest.storage.service.handler.mocker.HandleReplayResultService; +import com.arextest.storage.service.handler.mocker.coverage.CoverageEventListener; +import com.arextest.storage.service.handler.mocker.coverage.DefaultCoverageEventListener; import com.arextest.storage.service.listener.AgentWorkingListener; import com.arextest.storage.service.listener.AutoDiscoveryEntryPointListener; import com.arextest.storage.web.controller.MockSourceEditionController; @@ -58,6 +61,7 @@ import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.bson.Document; import org.bson.conversions.Bson; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -115,9 +119,6 @@ public MongoDatabaseFactory mongoDbFactory() { // todo make this optional indexesSettingConfiguration.setIndexes(database); - - // load singleton desensitization service for every thread - DesensitizationLoader.DESENSITIZATION_SERVICE = DesensitizationLoader.load(database); syncAuthSwitch(database); return factory; } catch (Exception e) { @@ -132,13 +133,26 @@ MongoTemplate mongoTemplate(MongoDatabaseFactory factory, MongoConverter convert return new MongoTemplate(factory, converter); } + @Bean + @ConditionalOnMissingBean(DesensitizationProvider.class) + DesensitizationProvider desensitizationProvider(MongoDatabaseFactory factory) { + String desensitizationJarUrl = DataDesensitizationUtils.getDesensitizationJarUrl( + factory.getMongoDatabase()); + return new DesensitizationProvider(desensitizationJarUrl); + } + + @Bean + DataDesensitization dataDesensitization(DesensitizationProvider desensitizationProvider) { + return desensitizationProvider.get(); + } + @Bean @ConditionalOnMissingBean(MongoCustomConversions.class) - public MongoCustomConversions customConversions() { + public MongoCustomConversions customConversions(DataDesensitization dataDesensitization) { return MongoCustomConversions.create((adapter) -> { // Type based converter - adapter.registerConverter(new ArexMockerCompressionConverter.Read()); - adapter.registerConverter(new ArexMockerCompressionConverter.Write()); + adapter.registerConverter(new ArexMockerCompressionConverter.Read(dataDesensitization)); + adapter.registerConverter(new ArexMockerCompressionConverter.Write(dataDesensitization)); // Property based converter adapter.configurePropertyConversions((register) -> { @@ -257,7 +271,8 @@ public ScheduleReplayQueryController scheduleReplayQueryController( CacheProvider redisCacheProvider, DefaultApplicationConfig applicationDefaultConfig) { return new ScheduleReplayQueryController(scheduleReplayingService, prepareMockResultService, - invalidRecordService, handleReplayResultService, redisCacheProvider, applicationDefaultConfig); + invalidRecordService, handleReplayResultService, redisCacheProvider, + applicationDefaultConfig); } @Bean @@ -311,6 +326,12 @@ public DefaultApplicationConfig defaultApplicationConfig(ConfigProvider configPr return new DefaultApplicationConfig(configProvider); } + @Bean + @ConditionalOnMissingBean(CoverageEventListener.class) + public CoverageEventListener defaultCoverageEventListener() { + return new DefaultCoverageEventListener(); + } + @Bean @Order(3) public RepositoryProvider autoPinnedMockerProvider(MongoTemplate mongoTemplate, @@ -348,7 +369,8 @@ public RepositoryProvider defaultQueryMockerProvider(MongoTempl @Order(5) public RepositoryProvider pinnedQueryMockerProvider(MongoTemplate mongoTemplate, Set entryPointTypes, DefaultApplicationConfig defaultApplicationConfig) { - return new AREXQueryMockerMongoRepositoryProvider(ProviderNames.PINNED, mongoTemplate, properties, + return new AREXQueryMockerMongoRepositoryProvider(ProviderNames.PINNED, mongoTemplate, + properties, entryPointTypes, defaultApplicationConfig); } @@ -370,4 +392,22 @@ private void syncAuthSwitch(MongoDatabase database) { LOGGER.error("sync auth switch failed", e); } } + + private static class DataDesensitizationUtils { + + private static final String SYSTEM_CONFIGURATION = "SystemConfiguration"; + private static final String DESENSITIZATION_JAR = "desensitizationJar"; + private static final String JAR_URL = "jarUrl"; + + private static String getDesensitizationJarUrl(MongoDatabase database) { + MongoCollection collection = database.getCollection(SYSTEM_CONFIGURATION); + Bson filter = Filters.eq(SystemConfigurationCollection.Fields.key, + KeySummary.DESERIALIZATION_JAR); + Document document = collection.find(filter).first(); + if (document != null && document.get(DESENSITIZATION_JAR) != null) { + return document.get(DESENSITIZATION_JAR, Document.class).getString(JAR_URL); + } + return null; + } + } } diff --git a/arex-storage-web-api/src/main/java/com/arextest/storage/mock/impl/DefaultMockerResultConverterImpl.java b/arex-storage-web-api/src/main/java/com/arextest/storage/mock/impl/DefaultMockerResultConverterImpl.java index f79d94b1..6a1fc6d7 100644 --- a/arex-storage-web-api/src/main/java/com/arextest/storage/mock/impl/DefaultMockerResultConverterImpl.java +++ b/arex-storage-web-api/src/main/java/com/arextest/storage/mock/impl/DefaultMockerResultConverterImpl.java @@ -4,8 +4,8 @@ import static com.arextest.storage.model.Constants.AREX_CONFIG_MOCKERCONVERT_ENABLED_DEFAULT; import com.arextest.common.config.DefaultApplicationConfig; -import com.arextest.diff.model.classloader.RemoteJarClassLoader; -import com.arextest.diff.utils.RemoteJarLoaderUtils; +import com.arextest.common.model.classloader.RemoteJarClassLoader; +import com.arextest.common.utils.RemoteJarLoaderUtils; import com.arextest.extension.mockconvert.MockerConverter; import com.arextest.model.mock.AREXMocker; import com.arextest.model.mock.MockCategoryType; diff --git a/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/DesensitizationLoader.java b/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/DesensitizationLoader.java deleted file mode 100644 index adc6ddb5..00000000 --- a/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/DesensitizationLoader.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.arextest.storage.repository.impl.mongo; - -import com.arextest.common.model.classloader.RemoteJarClassLoader; -import com.arextest.common.utils.RemoteJarLoaderUtils; -import com.arextest.config.model.dao.config.SystemConfigurationCollection; -import com.arextest.config.model.dao.config.SystemConfigurationCollection.KeySummary; -import com.arextest.extension.desensitization.DataDesensitization; -import com.arextest.extension.desensitization.DefaultDataDesensitization; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Filters; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.bson.Document; -import org.bson.conversions.Bson; - -@Slf4j -public class DesensitizationLoader { - private static final String SYSTEM_CONFIGURATION = "SystemConfiguration"; - private static final String DESENSITIZATION_JAR = "desensitizationJar"; - private static final String JAR_URL = "jarUrl"; - - public static final DataDesensitization DEFAULT_DESENSITIZATION_SERVICE = new DefaultDataDesensitization(); - public static DataDesensitization DESENSITIZATION_SERVICE = DEFAULT_DESENSITIZATION_SERVICE; - - public static DataDesensitization load(MongoDatabase mongoDatabase) { - String jarUrl = getJarUrl(mongoDatabase); - return loadDesensitization(jarUrl); - } - - public static DataDesensitization get() { - return DESENSITIZATION_SERVICE; - } - - private static String getJarUrl(MongoDatabase mongoDatabase) { - MongoCollection collection = mongoDatabase.getCollection(SYSTEM_CONFIGURATION); - if (collection.countDocuments() <= 0) { - return null; - } - Bson filter = Filters.eq(SystemConfigurationCollection.Fields.key, - KeySummary.DESERIALIZATION_JAR); - Document document = collection.find(filter).first(); - if (document != null && document.get(DESENSITIZATION_JAR) != null) { - return document.get(DESENSITIZATION_JAR, Document.class).getString(JAR_URL); - } - return null; - } - - private static DataDesensitization loadDesensitization(String remoteJarUrl) { - DataDesensitization dataDesensitization = new DefaultDataDesensitization(); - if (StringUtils.isEmpty(remoteJarUrl)) { - return dataDesensitization; - } - try { - RemoteJarClassLoader remoteJarClassLoader = RemoteJarLoaderUtils.loadJar(remoteJarUrl); - dataDesensitization = RemoteJarLoaderUtils - .loadService(DataDesensitization.class, remoteJarClassLoader) - .get(0); - } catch (Exception e) { - LOGGER.error("load desensitization error", e); - throw new RuntimeException(e); - } - return dataDesensitization; - } -} diff --git a/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/converters/ArexMockerCompressionConverter.java b/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/converters/ArexMockerCompressionConverter.java index 18e9f410..fc1a40ec 100644 --- a/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/converters/ArexMockerCompressionConverter.java +++ b/arex-storage-web-api/src/main/java/com/arextest/storage/repository/impl/mongo/converters/ArexMockerCompressionConverter.java @@ -1,9 +1,10 @@ package com.arextest.storage.repository.impl.mongo.converters; import com.arextest.common.utils.SerializationUtils; +import com.arextest.extension.desensitization.DataDesensitization; import com.arextest.model.mock.Mocker.Target; -import com.arextest.storage.repository.impl.mongo.DesensitizationLoader; import com.mongodb.lang.NonNull; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.convert.converter.Converter; import org.springframework.data.convert.ReadingConverter; @@ -11,41 +12,51 @@ @Slf4j public class ArexMockerCompressionConverter { - private static Target read(String source) { - String encodeWithDecryptString = null; - try { - encodeWithDecryptString = DesensitizationLoader.DESENSITIZATION_SERVICE.decrypt(source); - } catch (Exception e) { - LOGGER.error("Data decrypt failed", e); - } - return SerializationUtils.useZstdDeserialize(encodeWithDecryptString, Target.class); - } - - private static String write(Target source) { - String base64Result = SerializationUtils.useZstdSerializeToBase64(source); - try { - base64Result = DesensitizationLoader.DESENSITIZATION_SERVICE.encrypt(base64Result); - return base64Result; - } catch (Exception e) { - LOGGER.error("Data encrypt failed", e); - } - return base64Result; - } @ReadingConverter + @RequiredArgsConstructor public static class Read implements Converter { + private final DataDesensitization dataDesensitization; + @Override public Target convert(@NonNull String source) { return read(source); } + + private Target read(String source) { + String encodeWithDecryptString = null; + try { + encodeWithDecryptString = dataDesensitization.decrypt(source); + } catch (Exception e) { + LOGGER.error("Data decrypt failed", e); + } + return SerializationUtils.useZstdDeserialize(encodeWithDecryptString, Target.class); + } } + @WritingConverter + @RequiredArgsConstructor public static class Write implements Converter { + private final DataDesensitization dataDesensitization; + @Override public String convert(@NonNull Target source) { return write(source); } + + private String write(Target source) { + String base64Result = SerializationUtils.useZstdSerializeToBase64(source); + try { + base64Result = dataDesensitization.encrypt(base64Result); + return base64Result; + } catch (Exception e) { + LOGGER.error("Data encrypt failed", e); + } + return base64Result; + } + } + } \ No newline at end of file diff --git a/arex-storage-web-api/src/main/java/com/arextest/storage/service/handler/mocker/coverage/DefaultCoverageEventListener.java b/arex-storage-web-api/src/main/java/com/arextest/storage/service/handler/mocker/coverage/DefaultCoverageEventListener.java index 19726ec7..9d9e68bf 100644 --- a/arex-storage-web-api/src/main/java/com/arextest/storage/service/handler/mocker/coverage/DefaultCoverageEventListener.java +++ b/arex-storage-web-api/src/main/java/com/arextest/storage/service/handler/mocker/coverage/DefaultCoverageEventListener.java @@ -1,30 +1,26 @@ package com.arextest.storage.service.handler.mocker.coverage; import com.arextest.model.mock.Mocker; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.stereotype.Service; /** * @author: QizhengMo * @date: 2024/9/18 19:00 */ -@Service -@ConditionalOnMissingBean(CoverageEventListener.class) public class DefaultCoverageEventListener implements CoverageEventListener { - @Override - public void onBeforeNewCaseRecord(Mocker coverageMocker) { - } + @Override + public void onBeforeNewCaseRecord(Mocker coverageMocker) { + } - @Override - public void onNewCaseRecorded(Mocker coverageMocker) { - } + @Override + public void onNewCaseRecorded(Mocker coverageMocker) { + } - @Override - public void onBeforeExistingCaseRecord(Mocker coverageMocker) { - } + @Override + public void onBeforeExistingCaseRecord(Mocker coverageMocker) { + } - @Override - public void onExistingCaseRecorded(Mocker coverageMocker) { - } + @Override + public void onExistingCaseRecorded(Mocker coverageMocker) { + } } diff --git a/arex-storage-web-api/src/main/resources/lib/arex-extension-desensitization.jar b/arex-storage-web-api/src/main/resources/lib/arex-extension-desensitization.jar new file mode 100644 index 00000000..19d5adcc Binary files /dev/null and b/arex-storage-web-api/src/main/resources/lib/arex-extension-desensitization.jar differ diff --git a/pom.xml b/pom.xml index 217973c1..63b93bee 100644 --- a/pom.xml +++ b/pom.xml @@ -119,6 +119,7 @@ + lombok @@ -278,8 +279,18 @@ src/main/resources + + **/*.jar + true + + src/main/resources + + **/*.yaml + **/*.jar + + @@ -433,7 +444,7 @@ - 1.3.5 + 1.3.6 3.3.2 1.8 @@ -456,7 +467,7 @@ 2.7.18 - 0.2.5 + 0.2.6 3.20.1