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

feat: allow accessing credentials from a namespace #9

Open
wants to merge 2 commits 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
22 changes: 21 additions & 1 deletion integration-token/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,27 @@ tasks {
if (with(Configs) { secretStuff() != "helloworld:1337" }) throw kotlin.IllegalStateException("config with secret couldn't be read")
}
}
val needsSecretsFromSimpleNamespace by creating(GetVaultSecretTask::class) {
secretPath.set("secret/example")
namespace.set("test")
doLast {
val secret = secret.get()
if (secret["examplestring"] != "helloworld") throw kotlin.IllegalStateException("examplestring couldn't be read from test namespace")
if (secret["exampleint"]?.toInt() != 1337) throw kotlin.IllegalStateException("exampleint couldn't be read from test namespace")
println("getting secret succeeded!")
}
}
val needsSecretsFromNestedNamespace by creating(GetVaultSecretTask::class) {
secretPath.set("secret/example")
namespace.set("test/child")
doLast {
val secret = secret.get()
if (secret["examplestring"] != "helloworld") throw kotlin.IllegalStateException("examplestring couldn't be read from test/child namespace")
if (secret["exampleint"]?.toInt() != 1337) throw kotlin.IllegalStateException("exampleint couldn't be read from test/child namespace")
println("getting secret succeeded!")
}
}
val build by existing {
dependsOn(needsSecretsConfigTime, needsSecrets, fromBuildSrc)
dependsOn(needsSecretsConfigTime, needsSecrets, fromBuildSrc, needsSecretsFromSimpleNamespace, needsSecretsFromNestedNamespace)
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/com/liftric/vault/GetVaultSecretTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ open class GetVaultSecretTask : DefaultTask() {
@Optional
val retryIntervalMilliseconds: Property<Int> = project.objects.property()

@Input
@Optional
val namespace: Property<String> = project.objects.property()

@Internal
// actually used as output...
val secret: MapProperty<String, String> = project.objects.mapProperty()
Expand All @@ -63,7 +67,8 @@ open class GetVaultSecretTask : DefaultTask() {
token = token,
vaultAddress = address,
maxRetries = maxRetries,
retryIntervalMilliseconds = retryIntervalMilliseconds
retryIntervalMilliseconds = retryIntervalMilliseconds,
namespace = namespace.orNull
).get(path)
)
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/com/liftric/vault/VaultClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class VaultClient(
token: String,
private val vaultAddress: String,
private val maxRetries: Int,
private val retryIntervalMilliseconds: Int
private val retryIntervalMilliseconds: Int,
private val namespace: String?
) {
private val config by lazy {
try {
Expand Down Expand Up @@ -40,11 +41,12 @@ class VaultClient(
verifyTokenValid()
return try {
vault.withRetries(maxRetries, retryIntervalMilliseconds)
.logical()
.read(secretPath)
.data.also {
if (it.isEmpty()) error("[vault] secret response contains no data - secret exists? token has correct rights to access it?")
}
.logical()
.withNameSpace(namespace)
.read(secretPath)
.data.also {
if (it.isEmpty()) error("[vault] secret response contains no data - secret exists? token has correct rights to access it?")
}
} catch (e: VaultException) {
println(
"[vault] exception while calling vault at $vaultAddress: ${e.message} - secret exists? token has correct rights to access it?"
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/liftric/vault/VaultClientExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ open class VaultClientExtension(project: Project) {
@Input
@Optional
val retryIntervalMilliseconds: Property<Int> = project.objects.property()

@Input
@Optional
val namespace: Property<String> = project.objects.property()
}
4 changes: 3 additions & 1 deletion src/main/kotlin/com/liftric/vault/VaultClientPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ fun Project.vault(secretPath: String): Map<String, String> {
val address = GetVaultSecretTask.determinAddress(vaultAddress = extension.vaultAddress.orNull)
val maxRetries = extension.maxRetries.getOrElse(Defaults.MAX_RETRIES)
val retryIntervalMilliseconds = extension.retryIntervalMilliseconds.getOrElse(Defaults.RETRY_INTERVAL_MILLI)
val namespace = extension.namespace.orNull
println("[vault] getting `$secretPath` from $address")

return VaultClient(
token = token,
vaultAddress = address,
maxRetries = maxRetries,
retryIntervalMilliseconds = retryIntervalMilliseconds
retryIntervalMilliseconds = retryIntervalMilliseconds,
namespace = namespace
).get(secretPath)
}
4 changes: 4 additions & 0 deletions vault.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export VAULT_TOKEN='myroottoken'
vault token lookup
vault kv put secret/example examplestring=helloworld exampleint=1337
vault kv get secret/example
vault namespace create test
vault namespace create -namespace=test child
vault kv put -namespace=test secret/example examplestring=hellochild exampleint=1338
vault kv put -namespace=test/child secret/example examplestring=hellochildchild exampleint=1339
wait $pid