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

This reverts commit 9334f0ae92.
This commit is contained in:
az 2023-04-12 16:54:11 +02:00
parent 9334f0ae92
commit 2a5f0d95fb
3 changed files with 11 additions and 33 deletions

View file

@ -1,32 +1,21 @@
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.cli.createRemoteProvInstance import org.domaindrivenarchitecture.provs.framework.core.Secret
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 ensureSudoWithoutPassword(prov: Prov, targetCommand: TargetCliCommand): Prov { fun Prov.ensureSudoWithoutPassword(password: Secret?) {
return if (prov.currentUserCanSudoWithoutPassword()) { if (!currentUserCanSudoWithoutPassword()) {
prov val passwordNonNull = password ?: getPasswordToConfigureSudoWithoutPassword()
} else {
val password = targetCommand.remoteTarget()?.password ?: getPasswordToConfigureSudoWithoutPassword()
val result = prov.makeCurrentUserSudoerWithoutPasswordRequired(password) val result = makeCurrentUserSudoerWithoutPasswordRequired(passwordNonNull)
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)
val provWithSudo = ensureSudoWithoutPassword(prov, cmd.target) prov.ensureSudoWithoutPassword(cmd.target.remoteTarget()?.password)
try { try {
provisionDesktopCommand(provWithSudo, cmd) provisionDesktopCommand(prov, 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,7 +3,6 @@ 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
@ -43,10 +42,8 @@ class ProvWithSudoKtTest {
// when // when
val canSudo1 = prov.currentUserCanSudoWithoutPassword() val canSudo1 = prov.currentUserCanSudoWithoutPassword()
val provWithSudo = ensureSudoWithoutPassword( prov.ensureSudoWithoutPassword(null)
prov, TargetCliCommand("local") val canSudo2 = prov.currentUserCanSudoWithoutPassword()
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
// then // then
assertFalse(canSudo1) assertFalse(canSudo1)
@ -58,14 +55,10 @@ 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,
@ -88,15 +81,11 @@ class ProvWithSudoKtTest {
// when // when
val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword() val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword()
val provWithSudo = ensureSudoWithoutPassword( prov.ensureSudoWithoutPassword(password)
remoteProvBySsh, TargetCliCommand("testuser:${password.plain()}@$ip") val canSudo2 = prov.currentUserCanSudoWithoutPassword()
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
// then // then
assertFalse(canSudo1) assertFalse(canSudo1)
assertTrue(canSudo2) assertTrue(canSudo2)
// unmockkStatic(::getPasswordToConfigureSudoWithoutPassword)
} }
} }