-
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.
feat: adapt plugin and test projects to a gradle lazy configuration a…
…pproach
- Loading branch information
1 parent
1121e8f
commit d81ec74
Showing
11 changed files
with
241 additions
and
108 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
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
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,11 +1,44 @@ | ||
package com.liftric.vault | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.kotlin.dsl.property | ||
|
||
open class VaultClientExtension(val project: Project) { | ||
var vaultAddress: String? = System.getenv()["VAULT_ADDR"] | ||
var vaultToken: String? = System.getenv()["VAULT_TOKEN"] | ||
var vaultTokenFilePath: String? = System.getenv()["VAULT_TOKEN_FILE_PATH"] | ||
var maxRetries: Int = 5 | ||
var retryIntervalMilliseconds = 1000 | ||
open class VaultClientExtension(project: Project) { | ||
/** | ||
* vault address to be used. will check env var `VAULT_ADDR` if unset | ||
*/ | ||
@Input | ||
@Optional | ||
val vaultAddress: Property<String> = project.objects.property() | ||
|
||
/** | ||
* vault token to be used (it's recommend you don't set this in your build file). will check env var `VAULT_TOKEN` if unset | ||
*/ | ||
@Input | ||
@Optional | ||
val vaultToken: Property<String> = project.objects.property() | ||
|
||
/** | ||
* vault token file path (if set has precedence over vaultToken). will check env var `VAULT_TOKEN_FILE_PATH` if unset | ||
*/ | ||
@Input | ||
@Optional | ||
val vaultTokenFilePath: Property<String> = project.objects.property() | ||
|
||
/** | ||
* vault client max retry count | ||
*/ | ||
@Input | ||
@Optional | ||
val maxRetries: Property<Int> = project.objects.property() | ||
|
||
/** | ||
* time between vault request retries | ||
*/ | ||
@Input | ||
@Optional | ||
val retryIntervalMilliseconds: Property<Int> = project.objects.property() | ||
} |
Oops, something went wrong.