diff --git a/build.gradle.kts b/build.gradle.kts index c315e51..4392d4c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,6 +51,7 @@ subprojects { jvmTarget = "17" apiVersion = "1.9" languageVersion = "1.9" + freeCompilerArgs += "-Xuse-ir" } } diff --git a/konfy-ssm/build.gradle.kts b/konfy-ssm/build.gradle.kts index 0f0a8e0..0ae1fe6 100644 --- a/konfy-ssm/build.gradle.kts +++ b/konfy-ssm/build.gradle.kts @@ -5,6 +5,15 @@ dependencies { api(project(":konfy")) api("com.amazonaws", "aws-java-sdk-ssm", "1.12.655") + implementation("org.slf4j:slf4j-api:1.7.36") + + testImplementation("org.testng", "testng", "7.9.0") + testImplementation("org.assertj", "assertj-core", "3.25.3") + testImplementation("io.mockk", "mockk", "1.13.9") + testImplementation("ch.qos.logback:logback-classic:1.5.0") } +tasks.test { + useTestNG() +} diff --git a/konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt b/konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt index 2f99840..3e38fc3 100644 --- a/konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt +++ b/konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt @@ -3,10 +3,13 @@ package tanvd.konfy.ssm import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest import com.amazonaws.services.simplesystemsmanagement.model.ParameterNotFoundException +import org.slf4j.LoggerFactory import tanvd.konfy.conversion.ConversionService import tanvd.konfy.provider.ConfigProvider import java.lang.reflect.Type +private const val KONFY_LOG_KEYS = "KONFY_LOG_KEYS" + /** * Provider takes values from AWS SSM. * Each key will be prepended with a prefix (with a slash after it) -- "${prefix}/${key}" @@ -17,8 +20,21 @@ import java.lang.reflect.Type class SsmProvider(private val prefix: String?, private val separator: String, private val ssm: AWSSimpleSystemsManagement = SsmClient.defaultClient, private val convert: (String, Type) -> Any? = ConversionService::convert) : ConfigProvider() { + + private val logger = LoggerFactory.getLogger(SsmProvider::class.java) + private val konfyLogKeys = (System.getenv(KONFY_LOG_KEYS) ?: System.getProperty(KONFY_LOG_KEYS)) + ?.split(",") + ?.map { it.trim() }.orEmpty() + + init { + logger.info("SsmProvider initialized.") + logger.info("Konfy log keys: $konfyLogKeys") + } + @Suppress("UNCHECKED_CAST") override fun fetch(key: String, type: Type): N? { + log(key) + val fullKey = (prefix?.let { "$it/$key" } ?: key).replace(separator, "/") val request = GetParameterRequest().withName(fullKey).withWithDecryption(true) return try { @@ -27,4 +43,12 @@ class SsmProvider(private val prefix: String?, private val separator: String, null } } + + private fun log(key: String) { + if (konfyLogKeys.contains(key)) { + val stackTrace = Thread.currentThread().stackTrace.joinToString("\n") + logger.info("Fetching parameter with key: $key \n Stack trace: \n $stackTrace") + } + } + } diff --git a/konfy-ssm/src/test/kotlin/SsmProviderTest.kt b/konfy-ssm/src/test/kotlin/SsmProviderTest.kt new file mode 100644 index 0000000..78f5d09 --- /dev/null +++ b/konfy-ssm/src/test/kotlin/SsmProviderTest.kt @@ -0,0 +1,38 @@ +import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement +import io.mockk.mockk +import org.testng.Assert.assertTrue +import org.testng.annotations.Test +import tanvd.konfy.ssm.SsmProvider +import java.io.ByteArrayOutputStream +import java.io.PrintStream + +class SsmProviderTest { + + @Test + fun shouldPrintCorrectLogMessage() { + val oldOut = System.out + val konfyLogKeysParam = "KONFY_LOG_KEYS" + try { + //given + val key = "testKey" + System.setProperty(konfyLogKeysParam, key) + val logMessage = "Fetching parameter with key: $key \n Stack trace:" + val management: AWSSimpleSystemsManagement = mockk(relaxed = true) + val provider = SsmProvider(null, ".", management) + + val consoleOutput = ByteArrayOutputStream() + val ps = PrintStream(consoleOutput) + System.setOut(ps) + + //when + provider.fetch(key, String::class.java) + assertTrue(consoleOutput.toString().contains(logMessage)) + + //then + } finally { + System.clearProperty(konfyLogKeysParam) + System.setOut(oldOut) + } + } + +} diff --git a/konfy-ssm/src/test/resources/logback.xml b/konfy-ssm/src/test/resources/logback.xml new file mode 100644 index 0000000..114f332 --- /dev/null +++ b/konfy-ssm/src/test/resources/logback.xml @@ -0,0 +1,15 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + +