Skip to content

Commit

Permalink
feature/JPF-15956-ssm-calls-logging
Browse files Browse the repository at this point in the history
added configurable logging to SSM provider


Merge-request: KONFY-MR-2
Merged-by: Dinn Diallo <[email protected]>
  • Loading branch information
Dinn Diallo authored and qodana-bot committed Mar 5, 2024
1 parent 15df57c commit e0e4b95
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ subprojects {
jvmTarget = "17"
apiVersion = "1.9"
languageVersion = "1.9"
freeCompilerArgs += "-Xuse-ir"
}
}

Expand Down
9 changes: 9 additions & 0 deletions konfy-ssm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

24 changes: 24 additions & 0 deletions konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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 <N : Any> 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 {
Expand All @@ -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")
}
}

}
38 changes: 38 additions & 0 deletions konfy-ssm/src/test/kotlin/SsmProviderTest.kt
Original file line number Diff line number Diff line change
@@ -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<String>(key, String::class.java)
assertTrue(consoleOutput.toString().contains(logMessage))

//then
} finally {
System.clearProperty(konfyLogKeysParam)
System.setOut(oldOut)
}
}

}
15 changes: 15 additions & 0 deletions konfy-ssm/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="tanvd.konfy.ssm.SsmProvider" level="trace" />

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit e0e4b95

Please sign in to comment.