refactorings & mark tests as ExtensiveContainerTests & add EnvSecretSource.kt
This commit is contained in:
parent
9e1023b4b8
commit
445d12c849
13 changed files with 65 additions and 29 deletions
|
@ -329,11 +329,15 @@ fun Prov.deleteDir(dir: String, path: String, sudo: Boolean = false): ProvResult
|
||||||
if ("" == path) {
|
if ("" == path) {
|
||||||
throw RuntimeException("In deleteDir: path must not be empty.")
|
throw RuntimeException("In deleteDir: path must not be empty.")
|
||||||
}
|
}
|
||||||
val cmd = "cd $path && rmdir $dir"
|
return if (checkDir(dir, path, sudo)) {
|
||||||
return if (!sudo) {
|
val cmd = "cd $path && rmdir $dir"
|
||||||
cmd(cmd)
|
if (!sudo) {
|
||||||
|
cmd(cmd)
|
||||||
|
} else {
|
||||||
|
cmd(cmd.sudoizeCommand())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cmd(cmd.sudoizeCommand())
|
ProvResult(true, out = "Dir to delete did not exist: $dir")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.
|
||||||
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
abstract class SecretSource(protected val input: String) {
|
abstract class SecretSource(protected val parameter: String) {
|
||||||
abstract fun secret() : Secret
|
abstract fun secret() : Secret
|
||||||
abstract fun secretNullable() : Secret?
|
abstract fun secretNullable() : Secret?
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,16 @@ abstract class SecretSource(protected val input: String) {
|
||||||
@Serializable
|
@Serializable
|
||||||
enum class SecretSourceType {
|
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) {
|
return when (this) {
|
||||||
PLAIN -> PlainSecretSource(input).secret()
|
PLAIN -> PlainSecretSource(parameter).secret()
|
||||||
FILE -> FileSecretSource(input).secret()
|
FILE -> FileSecretSource(parameter).secret()
|
||||||
PROMPT -> PromptSecretSource().secret()
|
PROMPT -> PromptSecretSource().secret()
|
||||||
PASS -> PassSecretSource(input).secret()
|
PASS -> PassSecretSource(parameter).secret()
|
||||||
GOPASS -> GopassSecretSource(input).secret()
|
GOPASS -> GopassSecretSource(parameter).secret()
|
||||||
|
ENV -> EnvSecretSource(parameter).secret()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,11 +13,11 @@ class FileSecretSource(fqFileName: String) : SecretSource(fqFileName) {
|
||||||
|
|
||||||
override fun secret(): Secret {
|
override fun secret(): Secret {
|
||||||
val p = Prov.newInstance(name = "FileSecretSource", progressType = ProgressType.NONE)
|
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? {
|
override fun secretNullable(): Secret? {
|
||||||
val p = Prov.newInstance(name = "FileSecretSource", progressType = ProgressType.NONE)
|
val p = Prov.newInstance(name = "FileSecretSource", progressType = ProgressType.NONE)
|
||||||
return p.getSecret("cat " + input)
|
return p.getSecret("cat " + parameter)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,10 +11,10 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource
|
||||||
*/
|
*/
|
||||||
class GopassSecretSource(path: String) : SecretSource(path) {
|
class GopassSecretSource(path: String) : SecretSource(path) {
|
||||||
override fun secret(): Secret {
|
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? {
|
override fun secretNullable(): Secret? {
|
||||||
val p = Prov.newInstance(name = "GopassSecretSource for $input", progressType = ProgressType.NONE)
|
val p = Prov.newInstance(name = "GopassSecretSource for $parameter", progressType = ProgressType.NONE)
|
||||||
return p.getSecret("gopass show -f $input", true)
|
return p.getSecret("gopass show -f $parameter", true)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,10 +12,10 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource
|
||||||
class PassSecretSource(path: String) : SecretSource(path) {
|
class PassSecretSource(path: String) : SecretSource(path) {
|
||||||
override fun secret(): Secret {
|
override fun secret(): Secret {
|
||||||
val p = Prov.newInstance(name = "PassSecretSource", progressType = ProgressType.NONE)
|
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? {
|
override fun secretNullable(): Secret? {
|
||||||
val p = Prov.newInstance(name = "PassSecretSource", progressType = ProgressType.NONE)
|
val p = Prov.newInstance(name = "PassSecretSource", progressType = ProgressType.NONE)
|
||||||
return p.getSecret("pass " + input)
|
return p.getSecret("pass " + parameter)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,9 +6,9 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSource
|
||||||
|
|
||||||
class PlainSecretSource(plainSecret: String) : SecretSource(plainSecret) {
|
class PlainSecretSource(plainSecret: String) : SecretSource(plainSecret) {
|
||||||
override fun secret(): Secret {
|
override fun secret(): Secret {
|
||||||
return Secret(input)
|
return Secret(parameter)
|
||||||
}
|
}
|
||||||
override fun secretNullable(): Secret {
|
override fun secretNullable(): Secret {
|
||||||
return Secret(input)
|
return Secret(parameter)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -47,7 +47,7 @@ class PasswordPanel : JPanel(FlowLayout()) {
|
||||||
class PromptSecretSource(text: String = "Secret/Password") : SecretSource(text) {
|
class PromptSecretSource(text: String = "Secret/Password") : SecretSource(text) {
|
||||||
|
|
||||||
override fun secret(): Secret {
|
override fun secret(): Secret {
|
||||||
val password = PasswordPanel.requestPassword(input)
|
val password = PasswordPanel.requestPassword(parameter)
|
||||||
if (password == null) {
|
if (password == null) {
|
||||||
throw IllegalArgumentException("Failed to retrieve secret from prompting.")
|
throw IllegalArgumentException("Failed to retrieve secret from prompting.")
|
||||||
} else {
|
} else {
|
||||||
|
@ -56,7 +56,7 @@ class PromptSecretSource(text: String = "Secret/Password") : SecretSource(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun secretNullable(): Secret? {
|
override fun secretNullable(): Secret? {
|
||||||
val password = PasswordPanel.requestPassword(input)
|
val password = PasswordPanel.requestPassword(parameter)
|
||||||
|
|
||||||
return if(password == null) {
|
return if(password == null) {
|
||||||
null
|
null
|
||||||
|
|
|
@ -3,7 +3,6 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText
|
import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.*
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.*
|
||||||
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
||||||
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
|
|
||||||
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
|
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.Disabled
|
import org.junit.jupiter.api.Disabled
|
||||||
|
@ -47,7 +46,7 @@ internal class DevOpsKtTest {
|
||||||
assertTrue(res.success)
|
assertTrue(res.success)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContainerTest
|
@ExtensiveContainerTest
|
||||||
fun installKubeconform() {
|
fun installKubeconform() {
|
||||||
// given
|
// given
|
||||||
val prov = defaultTestContainer()
|
val prov = defaultTestContainer()
|
||||||
|
|
|
@ -2,13 +2,13 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
||||||
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
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.*
|
import org.junit.jupiter.api.Assertions.*
|
||||||
|
|
||||||
class GraalVMKtTest {
|
class GraalVMKtTest {
|
||||||
|
|
||||||
@ContainerTest
|
@ExtensiveContainerTest
|
||||||
fun installGraalVM() {
|
fun installGraalVM() {
|
||||||
// given
|
// given
|
||||||
val prov = defaultTestContainer()
|
val prov = defaultTestContainer()
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package org.domaindrivenarchitecture.provs.desktop.infrastructure
|
package org.domaindrivenarchitecture.provs.desktop.infrastructure
|
||||||
|
|
||||||
import com.charleskorn.kaml.InvalidPropertyValueException
|
import com.charleskorn.kaml.InvalidPropertyValueException
|
||||||
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSourceType
|
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.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
@ -34,7 +32,7 @@ internal class K3SDesktopConfigRepositoryKtTest {
|
||||||
val exception = assertThrows<InvalidPropertyValueException> {
|
val exception = assertThrows<InvalidPropertyValueException> {
|
||||||
getConfig("src/test/resources/invalid-desktop-config.yaml")
|
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
|
@Test
|
||||||
|
|
|
@ -184,6 +184,7 @@ internal class FilesystemKtTest {
|
||||||
val res7 = prov.createDirs("test/testdir")
|
val res7 = prov.createDirs("test/testdir")
|
||||||
val res8 = prov.checkDir("testdir", "~/test")
|
val res8 = prov.checkDir("testdir", "~/test")
|
||||||
prov.deleteDir("testdir", "~/test/")
|
prov.deleteDir("testdir", "~/test/")
|
||||||
|
val res9 = prov.deleteDir("notexistingdirdir", "~/")
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertFalse(res1)
|
assertFalse(res1)
|
||||||
|
@ -194,6 +195,7 @@ internal class FilesystemKtTest {
|
||||||
assertFalse(res6)
|
assertFalse(res6)
|
||||||
assertTrue(res7.success)
|
assertTrue(res7.success)
|
||||||
assertTrue(res8)
|
assertTrue(res8)
|
||||||
|
assertTrue(res9.success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue