Skip to content

Commit

Permalink
Allow to serialize json structure received from Vault
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sol committed Jul 26, 2023
1 parent 0d7ea18 commit 20f7eb5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ext {
version_prefix = '0.1'
teamcity_version = '2023.05'
// compile
gson_version = '2.10.1'
spring_version = '5.3.18'
sprint_vault_version = '2.3.2'
httpclient_version = '4.3.6' // version is relevant to one used inside TeamCity
Expand Down Expand Up @@ -125,4 +126,4 @@ configurations.testImplementation {
exclude group: "org.jetbrains.teamcity", module: "server-api"
exclude group: "org.jetbrains.teamcity", module: "server-web-api"
exclude group: "org.jetbrains.teamcity.internal", module: "plugins"
}
}
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ teamcity {
}

dependencies {
implementation "com.google.code.gson:gson:$gson_version"
implementation("org.springframework.vault:spring-vault-core:$sprint_vault_version") {
exclude group: 'org.springframework'
}
Expand All @@ -38,5 +39,6 @@ dependencies {

testFixturesApi "org.jetbrains.teamcity:tests-support:${teamcity_version}"
testFixturesApi "org.springframework:spring-web:$spring_version"
testFixturesApi "com.google.code.gson:gson:$gson_version"
testFixturesApi "org.apache.httpcomponents:httpclient:$httpclient_version"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.teamcity.vault

import com.google.gson.Gson
import com.intellij.openapi.diagnostic.Logger
import com.jayway.jsonpath.JsonPath
import jetbrains.buildServer.util.ssl.SSLTrustStoreProvider
Expand Down Expand Up @@ -95,13 +96,7 @@ open class VaultResolver(private val trustStoreProvider: SSLTrustStoreProvider)
key = data.keys.first()
}
val value = data[key]
if (value == null) {
throw ResolvingError("'$key' is missing in HashiCorp Vault response for '${parameter.vaultPath}'")
}
if (value !is String) {
throw ResolvingError("Cannot extract data from non-string '$key'. Actual type is ${value.javaClass.simpleName} for '${parameter.vaultPath}'")
}
return value
return marshallValueIntoJSON(value, parameter)
}

val pattern: JsonPath?
Expand All @@ -114,19 +109,27 @@ open class VaultResolver(private val trustStoreProvider: SSLTrustStoreProvider)
}
try {
val value: Any? = pattern.read(data)
if (value == null) {
throw ResolvingError("'$jsonPath' found nothing for '${parameter.vaultPath}'")
}
if (value !is String) {
throw ResolvingError("Cannot extract data from non-string '$jsonPath'. Actual type is ${value.javaClass.simpleName} for '${parameter.vaultPath}'")
}
return value
return marshallValueIntoJSON(value, parameter)
} catch (e: Exception) {
LOG.warn("Cannot extract '$jsonPath' data from '${parameter.vaultPath}', full reference: ${parameter.full}", e)
throw ResolvingError("Cannot extract '$jsonPath' data from '${parameter.vaultPath}' for '${parameter.vaultPath}'")
}
}


private fun marshallValueIntoJSON(value: Any?, parameter: VaultQuery): String {
if (value == null) {
throw ResolvingError("'${parameter.jsonPath}' found nothing for '${parameter.vaultPath}'")
}
if (value is String) {
return value
}
LOG.info("Extract '${parameter.jsonPath}' data from '${parameter.vaultPath}' [${value.javaClass.simpleName}], full reference: ${parameter.full}")
val gson: Gson = Gson()
return gson.toJson(value)
}


private fun unwrapKV2IfNeeded(data: Map<String, Any>): Map<String, Any> {
if (isKV2Data(data)) {
@Suppress("UNCHECKED_CAST")
Expand All @@ -150,4 +153,4 @@ open class VaultResolver(private val trustStoreProvider: SSLTrustStoreProvider)
return true
}
}
}
}

0 comments on commit 20f7eb5

Please sign in to comment.