Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to serialize json structure received from Vault #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 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 @@ -169,4 +170,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
}
}
}
}