Skip to content

Commit

Permalink
chore: (#490) redisCache
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed May 10, 2023
1 parent b05be2a commit 4860209
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface SchoolSecretPort {

fun saveSchoolSecret(schoolSecret: SchoolSecret)

fun querySchoolSecretBySchoolId(schoolId: UUID): SchoolSecret?
fun querySchoolSecretBySchoolId(schoolId: UUID): String?
}
3 changes: 3 additions & 0 deletions dms-infrastructure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {

// logging
implementation(Dependencies.SENTRY)

// redis
implementation(Dependencies.REDIS)
}

kapt {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand All @@ -65,7 +65,6 @@ class AwsKMSAdapter(
)
}

@Cacheable("decryptedText")
override fun asymmetricDecrypt(cipherText: String): String {

val request = DecryptRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions dms-infrastructure/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4860209

Please sign in to comment.