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
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
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.ubuntu.user.base.currentUserCanSudoWithoutPassword
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()) {
prov
} else {
val password = targetCommand.remoteTarget()?.password ?: getPasswordToConfigureSudoWithoutPassword()
if (!currentUserCanSudoWithoutPassword()) {
val passwordNonNull = password ?: getPasswordToConfigureSudoWithoutPassword()
val result = prov.makeCurrentUserSudoerWithoutPasswordRequired(password)
val result = makeCurrentUserSudoerWithoutPasswordRequired(passwordNonNull)
check(result.success) {
"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 provWithSudo = ensureSudoWithoutPassword(prov, cmd.target)
prov.ensureSudoWithoutPassword(cmd.target.remoteTarget()?.password)
try {
provisionDesktopCommand(provWithSudo, cmd)
provisionDesktopCommand(prov, cmd)
} catch (e: SerializationException) {
println(
"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.mockkStatic
import io.mockk.unmockkStatic
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.framework.core.*
import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword
import org.domaindrivenarchitecture.provs.framework.core.docker.provideContainer
@ -43,10 +42,8 @@ class ProvWithSudoKtTest {
// when
val canSudo1 = prov.currentUserCanSudoWithoutPassword()
val provWithSudo = ensureSudoWithoutPassword(
prov, TargetCliCommand("local")
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
prov.ensureSudoWithoutPassword(null)
val canSudo2 = prov.currentUserCanSudoWithoutPassword()
// then
assertFalse(canSudo1)
@ -58,14 +55,10 @@ class ProvWithSudoKtTest {
@ExtensiveContainerTest
fun test_ensureSudoWithoutPassword_remote_Prov() {
// mockkStatic(::getPasswordToConfigureSudoWithoutPassword)
// every { getPasswordToConfigureSudoWithoutPassword() } returns Secret("testuserpw")
// given
val containerName = "prov-test-sudo-no-pw-ssh"
val password = Secret("testuserpw")
// local().provideContainer(containerName, "ubuntu_plus_user", options = "")
val prov = Prov.newInstance(
ContainerUbuntuHostProcessor(
containerName,
@ -88,15 +81,11 @@ class ProvWithSudoKtTest {
// when
val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword()
val provWithSudo = ensureSudoWithoutPassword(
remoteProvBySsh, TargetCliCommand("testuser:${password.plain()}@$ip")
)
val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword()
prov.ensureSudoWithoutPassword(password)
val canSudo2 = prov.currentUserCanSudoWithoutPassword()
// then
assertFalse(canSudo1)
assertTrue(canSudo2)
// unmockkStatic(::getPasswordToConfigureSudoWithoutPassword)
}
}