Skip to content

Commit

Permalink
Added AwsSecretsManagerPreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Jul 30, 2021
1 parent d15fdd5 commit 1895c04
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.gradle.kotlin.dsl.`kotlin-dsl`

repositories {
jcenter()
mavenCentral()
}

plugins {
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ object Libs {
}

object Aws {
private const val version = "1.11.1018"
private const val version = "1.12.36"
const val core = "com.amazonaws:aws-java-sdk-core:$version"
const val ssm = "com.amazonaws:aws-java-sdk-ssm:$version"
const val secrets = "com.amazonaws:aws-java-sdk-secretsmanager:$version"
}

object CronUtils {
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 1.4.5

* Added [Consul](https://www.consul.io/) preprocessor.

### 1.4.4

* Adds basic command line property source. (#217)
Expand Down
1 change: 1 addition & 0 deletions hoplite-aws/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
api(project(":hoplite-core"))
api(Libs.Aws.core)
api(Libs.Aws.ssm)
api(Libs.Aws.secrets)
}

apply("../publish.gradle.kts")
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sksamuel.hoplite.aws

import com.amazonaws.services.secretsmanager.AWSSecretsManager
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest
import com.sksamuel.hoplite.ConfigException
import com.sksamuel.hoplite.Node
import com.sksamuel.hoplite.PrimitiveNode
import com.sksamuel.hoplite.StringNode
import com.sksamuel.hoplite.fp.Try
import com.sksamuel.hoplite.fp.getOrElse
import com.sksamuel.hoplite.preprocessor.TraversingPrimitivePreprocessor

class AwsSecretsManagerPreprocessor(
private val configure: (AWSSecretsManagerClientBuilder) -> Unit = {}
) : TraversingPrimitivePreprocessor() {

private val client by lazy { createClient() }
private val regex = "\\$\\{awssecret:(.+?)}".toRegex()

private fun createClient(): AWSSecretsManager {
val builder = AWSSecretsManagerClientBuilder.standard()
configure.invoke(builder)
return builder.build()
}

private fun fetchValue(key: String): Try<String> = Try {
val req = GetSecretValueRequest().withSecretId(key)
client.getSecretValue(req).secretString
}

override fun handle(node: PrimitiveNode): Node = when (node) {
is StringNode -> {
when (val match = regex.matchEntire(node.value)) {
null -> node
else -> {
val key = match.groupValues[1]
val value = fetchValue(key)
.getOrElse { throw ConfigException("Failed loading secrets value from key '$key'", it) }
node.copy(value = value)
}
}
}
else -> node
}
}

0 comments on commit 1895c04

Please sign in to comment.