From 4860209ad87bb59b2401bc431ac1dcb97e38e9ae Mon Sep 17 00:00:00 2001 From: rlaisqls Date: Wed, 10 May 2023 14:24:35 +0900 Subject: [PATCH] chore: (#490) redisCache --- .../aliens/dms/common/model/SchoolSecret.kt | 3 +- .../aliens/dms/common/spi/SchoolSecretPort.kt | 2 +- dms-infrastructure/build.gradle.kts | 3 ++ .../aliens/dms/global/config/CacheConfig.kt | 8 ---- .../dms/global/config/RedisCacheConfig.kt | 46 +++++++++++++++++++ .../dms/thirdparty/encrypt/AwsKMSAdapter.kt | 11 ++--- .../encrypt/aop/EntityEncryptAspect.kt | 8 ++-- .../src/main/resources/application.yml | 3 ++ .../school/SchoolPersistenceAdapter.kt | 7 +++ .../SchoolSecretPersistenceAdapter.kt | 4 +- 10 files changed, 72 insertions(+), 23 deletions(-) delete mode 100644 dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/CacheConfig.kt create mode 100644 dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/RedisCacheConfig.kt diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt index 764faa25e..d8c66c231 100644 --- a/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt @@ -1,9 +1,8 @@ package team.aliens.dms.common.model -import java.io.Serializable import java.util.UUID data class SchoolSecret( val schoolId: UUID, val secretKey: String -) : Serializable +) \ No newline at end of file diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt index 4617a7956..5668e8646 100644 --- a/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt @@ -7,5 +7,5 @@ interface SchoolSecretPort { fun saveSchoolSecret(schoolSecret: SchoolSecret) - fun querySchoolSecretBySchoolId(schoolId: UUID): SchoolSecret? + fun querySchoolSecretBySchoolId(schoolId: UUID): String? } diff --git a/dms-infrastructure/build.gradle.kts b/dms-infrastructure/build.gradle.kts index 74601e61a..4940cdbec 100644 --- a/dms-infrastructure/build.gradle.kts +++ b/dms-infrastructure/build.gradle.kts @@ -49,6 +49,9 @@ dependencies { // logging implementation(Dependencies.SENTRY) + + // redis + implementation(Dependencies.REDIS) } kapt { diff --git a/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/CacheConfig.kt b/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/CacheConfig.kt deleted file mode 100644 index bc747b060..000000000 --- a/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/CacheConfig.kt +++ /dev/null @@ -1,8 +0,0 @@ -package team.aliens.dms.global.config - -import org.springframework.cache.annotation.EnableCaching -import org.springframework.context.annotation.Configuration - -@Configuration -@EnableCaching -class CacheConfig \ No newline at end of file diff --git a/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/RedisCacheConfig.kt b/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/RedisCacheConfig.kt new file mode 100644 index 000000000..be01b839a --- /dev/null +++ b/dms-infrastructure/src/main/kotlin/team/aliens/dms/global/config/RedisCacheConfig.kt @@ -0,0 +1,46 @@ +package team.aliens.dms.global.config + +import com.fasterxml.jackson.databind.ObjectMapper +import java.time.Duration +import org.springframework.cache.CacheManager +import org.springframework.cache.annotation.EnableCaching +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.data.redis.cache.RedisCacheConfiguration +import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder +import org.springframework.data.redis.connection.RedisConnectionFactory +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer +import org.springframework.data.redis.serializer.RedisSerializationContext +import org.springframework.data.redis.serializer.StringRedisSerializer + + +@Configuration +@EnableCaching +class RedisCacheConfig { + + @Bean + fun redisCacheManager( + connectionFactory: RedisConnectionFactory, + objectMapper: ObjectMapper, + ): CacheManager { + + val redisCacheConfiguration = RedisCacheConfiguration + .defaultCacheConfig() + .serializeKeysWith( + RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer()) + ) + .serializeValuesWith( + RedisSerializationContext.SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer()) + ) + .entryTtl(CACHE_DURATION) + + return RedisCacheManagerBuilder + .fromConnectionFactory(connectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build() + } + + companion object { + private val CACHE_DURATION = Duration.ofMinutes(30L) + } +} \ No newline at end of file diff --git a/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/AwsKMSAdapter.kt b/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/AwsKMSAdapter.kt index f73249d7a..ef8ef0c4d 100644 --- a/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/AwsKMSAdapter.kt +++ b/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/AwsKMSAdapter.kt @@ -4,14 +4,14 @@ import com.amazonaws.services.kms.AWSKMSAsync import com.amazonaws.services.kms.model.DecryptRequest import com.amazonaws.services.kms.model.EncryptRequest import com.amazonaws.services.kms.model.EncryptionAlgorithmSpec -import org.springframework.cache.annotation.Cacheable -import org.springframework.stereotype.Component -import team.aliens.dms.common.spi.EncryptPort -import team.aliens.dms.thirdparty.encrypt.exception.KMSException import java.nio.ByteBuffer import java.util.Base64 import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec +import org.springframework.cache.annotation.Cacheable +import org.springframework.stereotype.Component +import team.aliens.dms.common.spi.EncryptPort +import team.aliens.dms.thirdparty.encrypt.exception.KMSException @Component class AwsKMSAdapter( @@ -40,7 +40,7 @@ class AwsKMSAdapter( ) } - @Cacheable("cipher") + @Cacheable("cipher", cacheManager = "redisCacheManager") protected fun getCipher(opmode: Int, key: String): Cipher = Cipher.getInstance(encryptProperties.transformation).apply { init(opmode, SecretKeySpec(key.toByteArray(), encryptProperties.algorithm)) @@ -65,7 +65,6 @@ class AwsKMSAdapter( ) } - @Cacheable("decryptedText") override fun asymmetricDecrypt(cipherText: String): String { val request = DecryptRequest() diff --git a/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/aop/EntityEncryptAspect.kt b/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/aop/EntityEncryptAspect.kt index 801d95b8c..a59414006 100644 --- a/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/aop/EntityEncryptAspect.kt +++ b/dms-infrastructure/src/main/kotlin/team/aliens/dms/thirdparty/encrypt/aop/EntityEncryptAspect.kt @@ -1,5 +1,8 @@ package team.aliens.dms.thirdparty.encrypt.aop +import java.lang.reflect.Field +import java.util.function.Consumer +import java.util.function.Function import org.aspectj.lang.JoinPoint import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation.AfterReturning @@ -14,9 +17,6 @@ import team.aliens.dms.common.spi.EncryptPort import team.aliens.dms.common.spi.SchoolSecretPort import team.aliens.dms.common.spi.SecurityPort import team.aliens.dms.domain.school.exception.SchoolNotFoundException -import java.lang.reflect.Field -import java.util.function.Consumer -import java.util.function.Function @UseCase @Aspect @@ -117,6 +117,6 @@ class EntityEncryptAspect( private fun getSchoolKey(): String { val schoolId = securityPort.getCurrentUserSchoolId() val schoolSecret = schoolSecretPort.querySchoolSecretBySchoolId(schoolId) ?: throw SchoolNotFoundException - return encryptPort.asymmetricDecrypt(schoolSecret.secretKey) + return encryptPort.asymmetricDecrypt(schoolSecret) } } diff --git a/dms-infrastructure/src/main/resources/application.yml b/dms-infrastructure/src/main/resources/application.yml index 926d9ff31..0070b5c0f 100644 --- a/dms-infrastructure/src/main/resources/application.yml +++ b/dms-infrastructure/src/main/resources/application.yml @@ -39,6 +39,9 @@ spring: max-file-size: 20MB max-request-size: 20MB + cache: + type: redis + secret: secret-key: ${SECRET_KEY:asdfghgfds} access-exp: ${ACCESS_EXP:1800} diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/school/SchoolPersistenceAdapter.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/school/SchoolPersistenceAdapter.kt index a23f4cba9..72d2a7875 100644 --- a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/school/SchoolPersistenceAdapter.kt +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/school/SchoolPersistenceAdapter.kt @@ -2,6 +2,7 @@ package team.aliens.dms.persistence.school import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Component +import team.aliens.dms.domain.school.model.AvailableFeature import team.aliens.dms.domain.school.model.School import team.aliens.dms.domain.school.spi.SchoolPort import team.aliens.dms.persistence.school.mapper.AvailableFeatureMapper @@ -28,6 +29,12 @@ class SchoolPersistenceAdapter( ) )!! + override fun saveAvailableFeature(availableFeature: AvailableFeature) = availableFeatureMapper.toDomain( + availableFeatureRepository.save( + availableFeatureMapper.toEntity(availableFeature) + ) + )!! + override fun queryAvailableFeaturesBySchoolId(schoolId: UUID) = availableFeatureMapper.toDomain( availableFeatureRepository.findByIdOrNull(schoolId) ) diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt index 91b44bd78..501890d0c 100644 --- a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt @@ -15,10 +15,10 @@ class SchoolSecretPersistenceAdapter( private val schoolSecretMapper: SchoolSecretMapper ) : SchoolSecretPort { - @Cacheable("schoolSecret") + @Cacheable("schoolSecret", cacheManager = "redisCacheManager") override fun querySchoolSecretBySchoolId(schoolId: UUID) = schoolSecretRepository.findByIdOrNull(schoolId) - ?.let { schoolSecretMapper.toDomain(it) } + ?.let { schoolSecretMapper.toDomain(it)?.secretKey } override fun saveSchoolSecret(schoolSecret: SchoolSecret) { schoolSecretRepository.save(