Skip to content

Commit

Permalink
changed ssm version to 2+, fixed code to support new API
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinn Diallo committed Mar 21, 2024
1 parent cba1e9b commit a948ff1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions konfy-ssm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = rootProject.version
dependencies {
api(project(":konfy"))

api("com.amazonaws", "aws-java-sdk-ssm", "1.12.655")
implementation("org.slf4j:slf4j-api:1.7.36")
api("software.amazon.awssdk", "ssm", "2.25.13")
implementation("org.slf4j", "slf4j-api", "1.7.36")

testImplementation("org.testng", "testng", "7.9.0")
testImplementation("org.assertj", "assertj-core", "3.25.3")
Expand Down
7 changes: 0 additions & 7 deletions konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmClient.kt

This file was deleted.

8 changes: 8 additions & 0 deletions konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmClientFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tanvd.konfy.ssm

import software.amazon.awssdk.services.ssm.SsmClient


internal object SsmClientFactory {
val defaultClient by lazy { SsmClient.create()!! }
}
12 changes: 6 additions & 6 deletions konfy-ssm/src/main/kotlin/tanvd/konfy/ssm/SsmProvider.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 software.amazon.awssdk.services.ssm.SsmClient
import software.amazon.awssdk.services.ssm.model.GetParameterRequest
import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException
import tanvd.konfy.conversion.ConversionService
import tanvd.konfy.provider.ConfigProvider
import java.lang.reflect.Type
Expand All @@ -18,7 +18,7 @@ private const val KONFY_LOG_KEYS = "KONFY_LOG_KEYS"
* @param separator separator used by a client, provider will map it to `/`
*/
class SsmProvider(private val prefix: String?, private val separator: String,
private val ssm: AWSSimpleSystemsManagement = SsmClient.defaultClient,
private val ssm: SsmClient = SsmClientFactory.defaultClient,
private val convert: (String, Type) -> Any? = ConversionService::convert) : ConfigProvider() {

private val logger = LoggerFactory.getLogger(SsmProvider::class.java)
Expand All @@ -36,9 +36,9 @@ class SsmProvider(private val prefix: String?, private val separator: String,
log(key)

val fullKey = (prefix?.let { "$it/$key" } ?: key).replace(separator, "/")
val request = GetParameterRequest().withName(fullKey).withWithDecryption(true)
val request = GetParameterRequest.builder().name(fullKey).withDecryption(true).build()
return try {
ssm.getParameter(request)?.parameter?.value?.let { convert(it, type) as N }
ssm.getParameter(request)?.parameter()?.value()?.let { convert(it, type) as N }
} catch (e: ParameterNotFoundException) {
null
}
Expand Down
18 changes: 10 additions & 8 deletions konfy-ssm/src/test/kotlin/SsmProviderTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement
import io.mockk.mockk
import org.testng.Assert.assertTrue
import org.testng.annotations.Test
import software.amazon.awssdk.services.ssm.SsmClient
import tanvd.konfy.ssm.SsmProvider
import java.io.ByteArrayOutputStream
import java.io.PrintStream
Expand All @@ -13,23 +13,25 @@ class SsmProviderTest {
val oldOut = System.out
val konfyLogKeysParam = "KONFY_LOG_KEYS"
try {
//setup
val consoleOutput = ByteArrayOutputStream()
val ps = PrintStream(consoleOutput)
System.setOut(ps)

//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)
val ssmClient: SsmClient = mockk(relaxed = true)
val provider = SsmProvider(null, ".", ssmClient)

//when
provider.fetch<String>(key, String::class.java)
assertTrue(consoleOutput.toString().contains(logMessage))

//then
assertTrue(consoleOutput.toString().contains(logMessage))
} finally {
//cleanup
System.clearProperty(konfyLogKeysParam)
System.setOut(oldOut)
}
Expand Down

0 comments on commit a948ff1

Please sign in to comment.