From 9334f0ae92a3d637bc5130e714694c61cffe7519 Mon Sep 17 00:00:00 2001 From: az Date: Wed, 12 Apr 2023 09:19:57 +0200 Subject: [PATCH] [skip ci] Revert "[skip ci] refactor for ssh does not need to reconnect after user is sudoer without pw required" This reverts commit cdb4281c722309495a2c529f94d2484c43fcb634. --- .../configuration/application/ProvWithSudo.kt | 21 ++++++++++++++----- .../provs/desktop/application/Application.kt | 4 ++-- .../application/ProvWithSudoKtTest.kt | 19 +++++++++++++---- 3 files changed, 33 insertions(+), 11 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 09be29c..15a171e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudo.kt @@ -1,21 +1,32 @@ 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.Secret +import org.domaindrivenarchitecture.provs.framework.core.cli.createRemoteProvInstance 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 Prov.ensureSudoWithoutPassword(password: Secret?) { +fun ensureSudoWithoutPassword(prov: Prov, targetCommand: TargetCliCommand): Prov { - if (!currentUserCanSudoWithoutPassword()) { - val passwordNonNull = password ?: getPasswordToConfigureSudoWithoutPassword() + return if (prov.currentUserCanSudoWithoutPassword()) { + prov + } else { + val password = targetCommand.remoteTarget()?.password ?: getPasswordToConfigureSudoWithoutPassword() - val result = makeCurrentUserSudoerWithoutPasswordRequired(passwordNonNull) + val result = prov.makeCurrentUserSudoerWithoutPasswordRequired(password) 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 bd85441..b9d37ee 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) - prov.ensureSudoWithoutPassword(cmd.target.remoteTarget()?.password) + val provWithSudo = ensureSudoWithoutPassword(prov, cmd.target) try { - provisionDesktopCommand(prov, cmd) + provisionDesktopCommand(provWithSudo, 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 a895618..29c3670 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt @@ -3,6 +3,7 @@ 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 @@ -42,8 +43,10 @@ class ProvWithSudoKtTest { // when val canSudo1 = prov.currentUserCanSudoWithoutPassword() - prov.ensureSudoWithoutPassword(null) - val canSudo2 = prov.currentUserCanSudoWithoutPassword() + val provWithSudo = ensureSudoWithoutPassword( + prov, TargetCliCommand("local") + ) + val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword() // then assertFalse(canSudo1) @@ -55,10 +58,14 @@ 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, @@ -81,11 +88,15 @@ class ProvWithSudoKtTest { // when val canSudo1 = remoteProvBySsh.currentUserCanSudoWithoutPassword() - prov.ensureSudoWithoutPassword(password) - val canSudo2 = prov.currentUserCanSudoWithoutPassword() + val provWithSudo = ensureSudoWithoutPassword( + remoteProvBySsh, TargetCliCommand("testuser:${password.plain()}@$ip") + ) + val canSudo2 = provWithSudo.currentUserCanSudoWithoutPassword() // then assertFalse(canSudo1) assertTrue(canSudo2) + +// unmockkStatic(::getPasswordToConfigureSudoWithoutPassword) } } \ No newline at end of file