[skip ci] Revert "[skip ci] refactor for ssh does not need to reconnect after user is sudoer without pw required"

This reverts commit cdb4281c72.
This commit is contained in:
az 2023-04-12 09:19:57 +02:00
parent cdb4281c72
commit 9334f0ae92
3 changed files with 33 additions and 11 deletions

View file

@ -1,21 +1,32 @@
package org.domaindrivenarchitecture.provs.configuration.application package org.domaindrivenarchitecture.provs.configuration.application
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.Secret import org.domaindrivenarchitecture.provs.framework.core.cli.createRemoteProvInstance
import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudoWithoutPassword
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.makeCurrentUserSudoerWithoutPasswordRequired import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.makeCurrentUserSudoerWithoutPasswordRequired
fun Prov.ensureSudoWithoutPassword(password: Secret?) { fun ensureSudoWithoutPassword(prov: Prov, targetCommand: TargetCliCommand): Prov {
if (!currentUserCanSudoWithoutPassword()) { return if (prov.currentUserCanSudoWithoutPassword()) {
val passwordNonNull = password ?: getPasswordToConfigureSudoWithoutPassword() prov
} else {
val password = targetCommand.remoteTarget()?.password ?: getPasswordToConfigureSudoWithoutPassword()
val result = makeCurrentUserSudoerWithoutPasswordRequired(passwordNonNull) val result = prov.makeCurrentUserSudoerWithoutPasswordRequired(password)
check(result.success) { check(result.success) {
"Could not make user a sudoer without password required. (E.g. the password provided may be incorrect.)" "Could not make user a sudoer without password required. (E.g. the password provided may be incorrect.)"
} }
return if (targetCommand.isValidRemote()) {
// return a new instance as for remote instances a new ssh client is required after user was made sudoer without password
createRemoteProvInstance(targetCommand.remoteTarget())
} else {
prov
}
} }
} }

View file

@ -19,10 +19,10 @@ fun main(args: Array<String>) {
} }
val prov = createProvInstance(cmd.target) val prov = createProvInstance(cmd.target)
prov.ensureSudoWithoutPassword(cmd.target.remoteTarget()?.password) val provWithSudo = ensureSudoWithoutPassword(prov, cmd.target)
try { try {
provisionDesktopCommand(prov, cmd) provisionDesktopCommand(provWithSudo, cmd)
} catch (e: SerializationException) { } catch (e: SerializationException) {
println( println(
"Error: File \"${cmd.configFile?.fileName}\" has an invalid format and or invalid data.\n" "Error: File \"${cmd.configFile?.fileName}\" has an invalid format and or invalid data.\n"

View file

@ -3,6 +3,7 @@ package org.domaindrivenarchitecture.provs.configuration.application
import io.mockk.every import io.mockk.every
import io.mockk.mockkStatic import io.mockk.mockkStatic
import io.mockk.unmockkStatic import io.mockk.unmockkStatic
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.framework.core.* import org.domaindrivenarchitecture.provs.framework.core.*
import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword
import org.domaindrivenarchitecture.provs.framework.core.docker.provideContainer import org.domaindrivenarchitecture.provs.framework.core.docker.provideContainer
@ -42,8 +43,10 @@ class ProvWithSudoKtTest {
// when // when
val canSudo1 = prov.currentUserCanSudoWithoutPassword() val canSudo1 = prov.currentUserCanSudoWithoutPassword()
prov.ensureSudoWithoutPassword(null) val provWithSudo = ensureSudoWithoutPassword(
val canSudo2 = prov.currentUserCanSudoWithoutPassword() prov, TargetCliCommand("local")
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
// then // then
assertFalse(canSudo1) assertFalse(canSudo1)
@ -55,10 +58,14 @@ class ProvWithSudoKtTest {
@ExtensiveContainerTest @ExtensiveContainerTest
fun test_ensureSudoWithoutPassword_remote_Prov() { fun test_ensureSudoWithoutPassword_remote_Prov() {
// mockkStatic(::getPasswordToConfigureSudoWithoutPassword)
// every { getPasswordToConfigureSudoWithoutPassword() } returns Secret("testuserpw")
// given // given
val containerName = "prov-test-sudo-no-pw-ssh" val containerName = "prov-test-sudo-no-pw-ssh"
val password = Secret("testuserpw") val password = Secret("testuserpw")
// local().provideContainer(containerName, "ubuntu_plus_user", options = "")
val prov = Prov.newInstance( val prov = Prov.newInstance(
ContainerUbuntuHostProcessor( ContainerUbuntuHostProcessor(
containerName, containerName,
@ -81,11 +88,15 @@ class ProvWithSudoKtTest {
// when // when
val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword() val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword()
prov.ensureSudoWithoutPassword(password) val provWithSudo = ensureSudoWithoutPassword(
val canSudo2 = prov.currentUserCanSudoWithoutPassword() remoteProvBySsh, TargetCliCommand("testuser:${password.plain()}@$ip")
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
// then // then
assertFalse(canSudo1) assertFalse(canSudo1)
assertTrue(canSudo2) assertTrue(canSudo2)
// unmockkStatic(::getPasswordToConfigureSudoWithoutPassword)
} }
} }