From 2a5f0d95fb16e3e7a3289735d1d9ccfe01db860f Mon Sep 17 00:00:00 2001 From: az Date: Wed, 12 Apr 2023 16:54:11 +0200 Subject: [PATCH] Revert "[skip ci] Revert "[skip ci] refactor for ssh does not need to reconnect after user is sudoer without pw required"" This reverts commit 9334f0ae92a3d637bc5130e714694c61cffe7519. --- .../configuration/application/ProvWithSudo.kt | 21 +++++-------------- .../provs/desktop/application/Application.kt | 4 ++-- .../application/ProvWithSudoKtTest.kt | 19 ++++------------- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt index 15a171e..09be29c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt @@ -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 - } - } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt index b9d37ee..bd85441 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt @@ -19,10 +19,10 @@ fun main(args: Array) { } 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" diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt index 29c3670..a895618 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt @@ -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) } } \ No newline at end of file