diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt index 8cf4415..097f7cf 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt @@ -329,11 +329,15 @@ fun Prov.deleteDir(dir: String, path: String, sudo: Boolean = false): ProvResult if ("" == path) { throw RuntimeException("In deleteDir: path must not be empty.") } - val cmd = "cd $path && rmdir $dir" - return if (!sudo) { - cmd(cmd) + return if (checkDir(dir, path, sudo)) { + val cmd = "cd $path && rmdir $dir" + if (!sudo) { + cmd(cmd) + } else { + cmd(cmd.sudoizeCommand()) + } } else { - cmd(cmd.sudoizeCommand()) + ProvResult(true, out = "Dir to delete did not exist: $dir") } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/SecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/SecretSource.kt index ae979ef..4d8efac 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/SecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/SecretSource.kt @@ -6,7 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources. @Serializable -abstract class SecretSource(protected val input: String) { +abstract class SecretSource(protected val parameter: String) { abstract fun secret() : Secret abstract fun secretNullable() : Secret? } @@ -15,15 +15,16 @@ abstract class SecretSource(protected val input: String) { @Serializable enum class SecretSourceType { - PLAIN, FILE, PROMPT, PASS, GOPASS; + PLAIN, FILE, PROMPT, PASS, GOPASS, ENV; - fun secret(input: String) : Secret { + fun secret(parameter: String) : Secret { return when (this) { - PLAIN -> PlainSecretSource(input).secret() - FILE -> FileSecretSource(input).secret() + PLAIN -> PlainSecretSource(parameter).secret() + FILE -> FileSecretSource(parameter).secret() PROMPT -> PromptSecretSource().secret() - PASS -> PassSecretSource(input).secret() - GOPASS -> GopassSecretSource(input).secret() + PASS -> PassSecretSource(parameter).secret() + GOPASS -> GopassSecretSource(parameter).secret() + ENV -> EnvSecretSource(parameter).secret() } } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSource.kt new file mode 100644 index 0000000..afb3984 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSource.kt @@ -0,0 +1,18 @@ +package org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources + +import org.domaindrivenarchitecture.provs.framework.core.Secret +import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource + + +/** + * Reads secret from a local environment variable + */ +class EnvSecretSource(varName: String) : SecretSource(varName) { + override fun secret(): Secret { + return secretNullable() ?: throw Exception("Failed to get secret from environment variable: $parameter") + } + override fun secretNullable(): Secret? { + val secret = System.getenv(parameter) + return if (secret == null) null else Secret(secret) + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/FileSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/FileSecretSource.kt index 7595b50..14d6b0e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/FileSecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/FileSecretSource.kt @@ -13,11 +13,11 @@ class FileSecretSource(fqFileName: String) : SecretSource(fqFileName) { override fun secret(): Secret { val p = Prov.newInstance(name = "FileSecretSource", progressType = ProgressType.NONE) - return p.getSecret("cat " + input) ?: throw Exception("Failed to get secret.") + return p.getSecret("cat " + parameter) ?: throw Exception("Failed to get secret.") } override fun secretNullable(): Secret? { val p = Prov.newInstance(name = "FileSecretSource", progressType = ProgressType.NONE) - return p.getSecret("cat " + input) + return p.getSecret("cat " + parameter) } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/GopassSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/GopassSecretSource.kt index 03d2998..a078fbd 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/GopassSecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/GopassSecretSource.kt @@ -11,10 +11,10 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource */ class GopassSecretSource(path: String) : SecretSource(path) { override fun secret(): Secret { - return secretNullable() ?: throw Exception("Failed to get \"$input\" secret from gopass.") + return secretNullable() ?: throw Exception("Failed to get \"$parameter\" secret from gopass.") } override fun secretNullable(): Secret? { - val p = Prov.newInstance(name = "GopassSecretSource for $input", progressType = ProgressType.NONE) - return p.getSecret("gopass show -f $input", true) + val p = Prov.newInstance(name = "GopassSecretSource for $parameter", progressType = ProgressType.NONE) + return p.getSecret("gopass show -f $parameter", true) } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PassSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PassSecretSource.kt index 5ec30bb..f54e956 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PassSecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PassSecretSource.kt @@ -12,10 +12,10 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource class PassSecretSource(path: String) : SecretSource(path) { override fun secret(): Secret { val p = Prov.newInstance(name = "PassSecretSource", progressType = ProgressType.NONE) - return p.getSecret("pass " + input) ?: throw Exception("Failed to get secret.") + return p.getSecret("pass " + parameter) ?: throw Exception("Failed to get secret.") } override fun secretNullable(): Secret? { val p = Prov.newInstance(name = "PassSecretSource", progressType = ProgressType.NONE) - return p.getSecret("pass " + input) + return p.getSecret("pass " + parameter) } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PlainSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PlainSecretSource.kt index 0e4bffd..8f8cd9e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PlainSecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PlainSecretSource.kt @@ -6,9 +6,9 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource class PlainSecretSource(plainSecret: String) : SecretSource(plainSecret) { override fun secret(): Secret { - return Secret(input) + return Secret(parameter) } override fun secretNullable(): Secret { - return Secret(input) + return Secret(parameter) } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PromptSecretSource.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PromptSecretSource.kt index bf69f2f..540a7c3 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PromptSecretSource.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/PromptSecretSource.kt @@ -47,7 +47,7 @@ class PasswordPanel : JPanel(FlowLayout()) { class PromptSecretSource(text: String = "Secret/Password") : SecretSource(text) { override fun secret(): Secret { - val password = PasswordPanel.requestPassword(input) + val password = PasswordPanel.requestPassword(parameter) if (password == null) { throw IllegalArgumentException("Failed to retrieve secret from prompting.") } else { @@ -56,7 +56,7 @@ class PromptSecretSource(text: String = "Secret/Password") : SecretSource(text) } override fun secretNullable(): Secret? { - val password = PasswordPanel.requestPassword(input) + val password = PasswordPanel.requestPassword(parameter) return if(password == null) { null diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt index d02c088..a539962 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt @@ -3,7 +3,6 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.* import org.domaindrivenarchitecture.provs.test.defaultTestContainer -import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Disabled @@ -47,7 +46,7 @@ internal class DevOpsKtTest { assertTrue(res.success) } - @ContainerTest + @ExtensiveContainerTest fun installKubeconform() { // given val prov = defaultTestContainer() diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt index 512eefb..524723d 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt @@ -2,13 +2,13 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile import org.domaindrivenarchitecture.provs.test.defaultTestContainer -import org.domaindrivenarchitecture.provs.test.tags.ContainerTest +import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest import org.junit.jupiter.api.Assertions.* class GraalVMKtTest { - @ContainerTest + @ExtensiveContainerTest fun installGraalVM() { // given val prov = defaultTestContainer() diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3SDesktopConfigRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3SDesktopConfigRepositoryKtTest.kt index 83d8618..2b954c0 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3SDesktopConfigRepositoryKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3SDesktopConfigRepositoryKtTest.kt @@ -1,9 +1,7 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import com.charleskorn.kaml.InvalidPropertyValueException -import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSourceType -import org.domaindrivenarchitecture.provs.server.infrastructure.getK3sConfig import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -34,7 +32,7 @@ internal class K3SDesktopConfigRepositoryKtTest { val exception = assertThrows { getConfig("src/test/resources/invalid-desktop-config.yaml") } - assertEquals("Value for 'sourceType' is invalid: Value 'xxx' is not a valid option, permitted choices are: FILE, GOPASS, PASS, PLAIN, PROMPT", exception.message) + assertEquals("Value for 'sourceType' is invalid: Value 'xxx' is not a valid option, permitted choices are: ENV, FILE, GOPASS, PASS, PLAIN, PROMPT", exception.message) } @Test diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt index cc3ea20..925340a 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt @@ -184,6 +184,7 @@ internal class FilesystemKtTest { val res7 = prov.createDirs("test/testdir") val res8 = prov.checkDir("testdir", "~/test") prov.deleteDir("testdir", "~/test/") + val res9 = prov.deleteDir("notexistingdirdir", "~/") // then assertFalse(res1) @@ -194,6 +195,7 @@ internal class FilesystemKtTest { assertFalse(res6) assertTrue(res7.success) assertTrue(res8) + assertTrue(res9.success) } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSourceTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSourceTest.kt new file mode 100644 index 0000000..19c9e15 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/secret/secretSources/EnvSecretSourceTest.kt @@ -0,0 +1,14 @@ +package org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test + +internal class EnvSecretSourceTest { + + @Test + @Disabled // set env variable "envtest=envtestval" externally e.g. in IDE and run manually + fun secret() { + assertEquals("envtestval", EnvSecretSource("envtest").secret().plain()) + } +} \ No newline at end of file