-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Liftric/feature/lazy
lazy configuration and enhanced error messages
- Loading branch information
Showing
12 changed files
with
260 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import com.liftric.vault.vault | ||
import com.liftric.vault.GetVaultSecretTask | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.getByName | ||
|
||
object Configs { | ||
fun Project.secretStuff(): String { | ||
val secrets = project.vault("secret/example") | ||
return "${secrets["examplestring"]}:${secrets["exampleint"]}" | ||
val needsSecrets: GetVaultSecretTask = tasks.getByName<GetVaultSecretTask>("needsSecrets").apply { | ||
execute() | ||
} | ||
val secret = needsSecrets.secret.get() | ||
return "${secret["examplestring"]}:${secret["exampleint"]}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.liftric.vault | ||
|
||
object Defaults { | ||
// values | ||
const val MAX_RETRIES = 5 | ||
const val RETRY_INTERVAL_MILLI = 1000 | ||
|
||
// env vars | ||
const val VAULT_TOKEN_ENV = "VAULT_TOKEN" | ||
const val VAULT_TOKEN_FILE_PATH_ENV = "VAULT_TOKEN_FILE_PATH" | ||
const val VAULT_ADDR_ENV = "VAULT_ADDR" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.liftric.vault | ||
|
||
import com.liftric.vault.Defaults.MAX_RETRIES | ||
import com.liftric.vault.Defaults.RETRY_INTERVAL_MILLI | ||
import com.liftric.vault.Defaults.VAULT_ADDR_ENV | ||
import com.liftric.vault.Defaults.VAULT_TOKEN_ENV | ||
import com.liftric.vault.Defaults.VAULT_TOKEN_FILE_PATH_ENV | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.MapProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.* | ||
import org.gradle.kotlin.dsl.mapProperty | ||
import org.gradle.kotlin.dsl.property | ||
import java.io.File | ||
|
||
/** | ||
* See extension for property documentation | ||
*/ | ||
open class GetVaultSecretTask : DefaultTask() { | ||
init { | ||
group = "com.liftric.vault" | ||
description = "Fetches a secret from vault." | ||
outputs.upToDateWhen { false } | ||
} | ||
|
||
@Input | ||
val secretPath: Property<String> = project.objects.property() | ||
|
||
@Input | ||
@Optional | ||
val vaultAddress: Property<String> = project.objects.property() | ||
|
||
@Input | ||
@Optional | ||
val vaultToken: Property<String> = project.objects.property() | ||
|
||
@Input | ||
@Optional | ||
val vaultTokenFilePath: Property<String> = project.objects.property() | ||
|
||
@Input | ||
@Optional | ||
val maxRetries: Property<Int> = project.objects.property() | ||
|
||
@Input | ||
@Optional | ||
val retryIntervalMilliseconds: Property<Int> = project.objects.property() | ||
|
||
@Internal | ||
// actually used as output... | ||
val secret: MapProperty<String, String> = project.objects.mapProperty() | ||
|
||
@TaskAction | ||
fun execute() { | ||
val token = determineToken(vaultToken = vaultToken.orNull, vaultTokenFilePath = vaultTokenFilePath.orNull) | ||
val address = determinAddress(vaultAddress = vaultAddress.orNull) | ||
val maxRetries = maxRetries.getOrElse(MAX_RETRIES) | ||
val retryIntervalMilliseconds = retryIntervalMilliseconds.getOrElse(RETRY_INTERVAL_MILLI) | ||
val path = secretPath.get() | ||
println("[vault] getting `$path` from $address") | ||
secret.set( | ||
VaultClient( | ||
token = token, | ||
vaultAddress = address, | ||
maxRetries = maxRetries, | ||
retryIntervalMilliseconds = retryIntervalMilliseconds | ||
).get(path) | ||
) | ||
} | ||
|
||
companion object { | ||
fun determineToken(vaultToken: String?, vaultTokenFilePath: String?): String { | ||
val finalVaultToken = vaultToken ?: System.getenv()[VAULT_TOKEN_ENV]?.trim() | ||
val finalVaultTokenFilePath = vaultTokenFilePath ?: System.getenv()[VAULT_TOKEN_FILE_PATH_ENV]?.trim() | ||
return when { | ||
finalVaultToken != null -> finalVaultToken.trim() | ||
finalVaultTokenFilePath != null -> File(finalVaultTokenFilePath).apply { | ||
if (exists().not()) error("vault token file doesn't exist!") | ||
}.let { | ||
return@let it.readText().trim() | ||
} | ||
else -> error("neither `vaultToken` nor `vaultTokenFilePath` nor `$VAULT_TOKEN_FILE_PATH_ENV` env var nor `$VAULT_TOKEN_ENV` env var provided!") | ||
} | ||
} | ||
|
||
fun determinAddress(vaultAddress: String?): String { | ||
val finalVaultAddress = vaultAddress ?: System.getenv()[VAULT_ADDR_ENV] | ||
return finalVaultAddress?.trim() ?: error("neither `vaultAddress` nor `$VAULT_ADDR_ENV` env var provided!") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.