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 Sep 29, 2024
1 parent 0058979 commit f649abc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ext {
version_prefix = '0.1'
teamcity_version = anyParam('teamcityVersion') ?: '2024.07-SNAPSHOT'
// compile
gson_version = '2.10.1'
spring_version = '5.3.34'
sprint_vault_version = '2.3.3'
httpclient_version = '4.5.14' // version is relevant to one used inside TeamCity
Expand Down Expand Up @@ -133,6 +134,7 @@ configurations.testImplementation {
exclude group: "org.jetbrains.teamcity", module: "server-web-api"
exclude group: "org.jetbrains.teamcity.internal", module: "plugins"
}
<<<<<<< HEAD

def anyParamPath(String... names) {
def param = anyParam(names);
Expand Down Expand Up @@ -169,4 +171,4 @@ def initializeWorkspace() {
}
}
}
}
}
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 @@ -26,5 +27,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.serverSide.TeamCityProperties
Expand Down Expand Up @@ -115,13 +116,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 @@ -134,19 +129,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 @@ -170,4 +173,4 @@ open class VaultResolver(private val trustStoreProvider: SSLTrustStoreProvider)
return true
}
}
}
}

0 comments on commit f649abc

Please sign in to comment.