change cli to new format
parent
4c6d3ba3c3
commit
478f058fd7
@ -1,29 +1,64 @@
|
||||
package org.domaindrivenarchitecture.provs.configuration.domain
|
||||
|
||||
import org.domaindrivenarchitecture.provs.framework.core.Secret
|
||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PlainSecretSource
|
||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PromptSecretSource
|
||||
|
||||
|
||||
private const val USER_HOST_DELIMITER = "@"
|
||||
private const val USER_PW_DELIMITER = ":"
|
||||
|
||||
|
||||
class TargetCliCommand(
|
||||
val localHost: Boolean?,
|
||||
val remoteHost: String?,
|
||||
val userName: String?,
|
||||
val sshWithPasswordPrompt: Boolean,
|
||||
val sshWithGopassPath: String?,
|
||||
val sshWithKey: Boolean
|
||||
val target: String,
|
||||
val passwordInteractive: Boolean = false
|
||||
) {
|
||||
fun isValidLocalhost(): Boolean {
|
||||
return (localHost ?: false) && remoteHost == null && userName == null && sshWithGopassPath == null &&
|
||||
!sshWithPasswordPrompt && !sshWithKey
|
||||
private var remoteTarget: RemoteTarget? = null
|
||||
|
||||
init {
|
||||
remoteTarget = parseRemoteTarget()
|
||||
}
|
||||
|
||||
fun hasValidPasswordOption(): Boolean {
|
||||
return (sshWithGopassPath != null) xor sshWithPasswordPrompt xor sshWithKey
|
||||
fun isValidLocalhost(): Boolean {
|
||||
return target == "local"
|
||||
}
|
||||
|
||||
fun isValidRemote(): Boolean {
|
||||
return remoteHost != null && userName != null && hasValidPasswordOption()
|
||||
return (remoteTarget != null)
|
||||
}
|
||||
|
||||
fun isValid(): Boolean {
|
||||
return (isValidLocalhost() || isValidRemote())
|
||||
}
|
||||
|
||||
private fun parseRemoteTarget(): RemoteTarget? {
|
||||
val user: String?
|
||||
val host: String?
|
||||
var password: Secret? = null
|
||||
|
||||
if (!target.contains(USER_HOST_DELIMITER)) {
|
||||
return null
|
||||
}
|
||||
|
||||
host = target.substringAfter(USER_HOST_DELIMITER)
|
||||
|
||||
val userPw = target.substringBefore(USER_HOST_DELIMITER)
|
||||
if (!userPw.contains(USER_PW_DELIMITER)) {
|
||||
user = userPw
|
||||
} else {
|
||||
user = userPw.substringBefore(USER_PW_DELIMITER)
|
||||
password = PlainSecretSource(userPw.substringAfter(USER_PW_DELIMITER)).secret()
|
||||
}
|
||||
if (passwordInteractive) {
|
||||
password = PromptSecretSource("Password for $user on $host").secretNullable()
|
||||
}
|
||||
return RemoteTarget(user, host, password)
|
||||
}
|
||||
|
||||
fun remoteTarget(): RemoteTarget? {
|
||||
return remoteTarget
|
||||
}
|
||||
|
||||
class RemoteTarget(val user: String, val host: String, val password: Secret?)
|
||||
}
|
||||
|
||||
|
@ -1,64 +1,47 @@
|
||||
package org.domaindrivenarchitecture.provs.configuration.application
|
||||
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class CliTargetParserTest {
|
||||
|
||||
@Test
|
||||
fun parse_localhost_with_default() {
|
||||
val parseCli = parseTarget(args = emptyArray())
|
||||
|
||||
assertFalse(parseCli.isValidLocalhost())
|
||||
assertFalse(parseCli.isValidRemote())
|
||||
assertFalse(parseCli.isValid())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parse_localhost() {
|
||||
val parseCli = parseTarget(args = arrayOf("-l"))
|
||||
assertTrue(parseCli.isValidLocalhost())
|
||||
assertFalse(parseCli.isValidRemote())
|
||||
assertTrue(parseCli.isValid())
|
||||
val cliCommand = parseTarget(args = arrayOf("local"))
|
||||
assertTrue(cliCommand.isValidLocalhost())
|
||||
assertFalse(cliCommand.isValidRemote())
|
||||
assertTrue(cliCommand.isValid())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parse_remote_with_missing_passwordoption() {
|
||||
val parseCli = parseTarget(args = arrayOf("-r", "1.2.3.4", "-u", "user"))
|
||||
|
||||
assertFalse(parseCli.isValidLocalhost())
|
||||
assertEquals("1.2.3.4", parseCli.remoteHost)
|
||||
assertEquals("user", parseCli.userName)
|
||||
assertFalse(parseCli.isValidRemote())
|
||||
assertFalse(parseCli.isValid())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parse_remote_with_remote_key() {
|
||||
val parseCli = parseTarget(args = arrayOf("-r", "1.2.3.4", "-u", "user", "-k"))
|
||||
|
||||
assertFalse(parseCli.isValidLocalhost())
|
||||
assertEquals("1.2.3.4", parseCli.remoteHost)
|
||||
assertEquals("user", parseCli.userName)
|
||||
assertTrue(parseCli.isValid())
|
||||
fun parse_remote_with_given_pasword() {
|
||||
val cliCommand = parseTarget(args = arrayOf("user:mypassword@1.2.3.4"))
|
||||
|
||||
assertFalse(cliCommand.isValidLocalhost())
|
||||
assertEquals("1.2.3.4", cliCommand.remoteTarget()?.host)
|
||||
assertEquals("user", cliCommand.remoteTarget()?.user)
|
||||
assertEquals("mypassword", cliCommand.remoteTarget()?.password?.plain())
|
||||
assertTrue(cliCommand.isValid())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parse_remote_with_remote_password_prompt() {
|
||||
val parseCli = parseTarget(args = arrayOf("-r", "1.2.3.4", "-u", "user", "-i"))
|
||||
fun parse_remote_with_ssh_key() {
|
||||
val cliCommand = parseTarget(args = arrayOf("user@1.2.3.4"))
|
||||
|
||||
assertEquals("1.2.3.4", parseCli.remoteHost)
|
||||
assertEquals("user", parseCli.userName)
|
||||
assertTrue(parseCli.isValid())
|
||||
assertFalse(cliCommand.isValidLocalhost())
|
||||
assertEquals("1.2.3.4", cliCommand.remoteTarget()?.host)
|
||||
assertEquals("user", cliCommand.remoteTarget()?.user)
|
||||
assertTrue(cliCommand.isValid())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parse_remote_with_remote_password_gopass_path() {
|
||||
val parseCli = parseTarget(args = arrayOf("-r", "1.2.3.4", "-u", "user", "-p", "gopass/path"))
|
||||
@Disabled // enable to enter manually the password when prompted
|
||||
fun parse_remote_with_password_prompt() {
|
||||
val cliCommand = parseTarget(args = arrayOf("user@1.2.3.4", "-p"))
|
||||
|
||||
assertEquals("1.2.3.4", parseCli.remoteHost)
|
||||
assertEquals("user", parseCli.userName)
|
||||
assertEquals("gopass/path", parseCli.sshWithGopassPath)
|
||||
assertTrue(parseCli.isValid())
|
||||
assertEquals("1.2.3.4", cliCommand.remoteTarget()?.host)
|
||||
assertEquals("user", cliCommand.remoteTarget()?.user)
|
||||
assertTrue(cliCommand.isValid())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue