From 8cd62bf09afa20ead3f7aab3f3d7a463e0cae005 Mon Sep 17 00:00:00 2001 From: meonlol Date: Mon, 16 Sep 2019 12:06:54 +0200 Subject: [PATCH 1/2] add support for reading credentials from environment variables --- build.gradle.kts | 1 + src/main/kotlin/kscript/app/Script.kt | 21 ++++++++++++++++---- src/test/kotlin/Tests.kt | 28 ++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4514c100..adc8eb6f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,6 +24,7 @@ dependencies { compile("org.apache.maven:maven-core:3.0.3") compile("org.slf4j:slf4j-nop:1.7.25") + testCompile("com.github.stefanbirkner:system-rules:1.19.0") testCompile("junit:junit:4.12") testCompile( "io.kotlintest:kotlintest:2.0.7") testCompile(kotlin("script-runtime")) diff --git a/src/main/kotlin/kscript/app/Script.kt b/src/main/kotlin/kscript/app/Script.kt index 9cc284f6..01da1e31 100644 --- a/src/main/kotlin/kscript/app/Script.kt +++ b/src/main/kotlin/kscript/app/Script.kt @@ -205,15 +205,28 @@ fun Script.collectRepos(): List { .map { keyVal -> keyVal.split(keyValSep).map { it.trim(' ', '\"') }.let{ it.first() to it.last()}} .toMap() - MavenRepo(annotationParams[0], annotationParams[1], namedArgs.getOrDefault("user", ""), namedArgs.getOrDefault("password", "")) + MavenRepo(annotationParams[0], annotationParams[1], resolveOptionalVariableInString(namedArgs.getOrDefault("user", "")), resolveOptionalVariableInString(namedArgs.getOrDefault("password", ""))) } } + } +/** If the provided string contains {{something}}, the string will be treated as an environment variable. + * When the variable exists, the string will be replaced with the value of the environment variable. */ +private fun resolveOptionalVariableInString(stringWithOptionalVar: String): String { + val varRegex = "\\{\\{(.*)\\}\\}".toRegex() + val usrFind = varRegex.find(stringWithOptionalVar) + if (usrFind != null && usrFind.groupValues.size > 1) { + val varString = usrFind.groupValues[1] + if (System.getenv().containsKey(varString)) { + return System.getenv(varString) + } else { + throw RuntimeException("The specified environment variable '{{$varString}}' is not found.") + } + } + return stringWithOptionalVar // No match, so input string is returned +} -// -// Runtime Configuration -// /** diff --git a/src/test/kotlin/Tests.kt b/src/test/kotlin/Tests.kt index 75d2339c..d4731157 100644 --- a/src/test/kotlin/Tests.kt +++ b/src/test/kotlin/Tests.kt @@ -1,16 +1,21 @@ import io.kotlintest.matchers.shouldBe +import io.kotlintest.matchers.shouldThrow import kscript.app.* import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue +import org.junit.Rule import org.junit.Test import java.io.File +import org.junit.contrib.java.lang.system.EnvironmentVariables; /** * @author Holger Brandl */ - class Tests { + @JvmField @Rule + val environmentVariables = EnvironmentVariables() + // "comma separated dependencies should be parsed correctly" @Test fun directiveDependencyCollect() { @@ -124,6 +129,27 @@ class Tests { } + @Test + fun customRepoWithCredsFromEnvironment() { + environmentVariables.set("MY_USER_ENV", "my_user") + environmentVariables.set("MY_PASS_ENV", "my_pass") + + val validLines = listOf("""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="{{MY_USER_ENV}}", password="{{MY_PASS_ENV}}") """) + + with(Script(validLines)) { + collectRepos() shouldBe listOf(MavenRepo("imagej-releases", "http://maven.imagej.net/content/repositories/releases", "my_user", "my_pass")) + } + + val invalidLines = listOf("""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="{{MY_USER_ENV}}", password="{{MY_PASS_EN}}") """) + + with(Script(invalidLines)) { + shouldThrow { + collectRepos() + } + } + + } + // combine kotlin opts spread over multiple lines @Test From d593d97c0db19b0f18aef6dc7fb7bc030b63384f Mon Sep 17 00:00:00 2001 From: meonlol Date: Mon, 16 Sep 2019 13:38:43 +0200 Subject: [PATCH 2/2] fix: incorrect path in script tests --- test/test_suite.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_suite.sh b/test/test_suite.sh index 94106ff0..f064ca68 100755 --- a/test/test_suite.sh +++ b/test/test_suite.sh @@ -268,7 +268,7 @@ export -f kscript_nocall assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/include_variations.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0 ## Ensure relative includes with in shebang mode -assert_raises resources/includes/shebang_mode_includes 0 +assert_raises "${KSCRIPT_HOME}/test/resources/includes/shebang_mode_includes" 0 ## support diamond-shaped include schemes (see #133) assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/diamond.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0