[skip ci] make makeUserSudoerWithoutPasswordRequired taskWithResult

This commit is contained in:
az 2023-02-26 19:32:44 +01:00
parent bf36a6283c
commit 082c0827e3
2 changed files with 5 additions and 6 deletions

View file

@ -7,7 +7,7 @@ import org.domaindrivenarchitecture.provs.framework.core.local
import org.domaindrivenarchitecture.provs.framework.core.remote import org.domaindrivenarchitecture.provs.framework.core.remote
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PromptSecretSource import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PromptSecretSource
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.makeUserSudoerWithoutPasswordRequired import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.makeCurrentUserSudoerWithoutPasswordRequired
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami
import kotlin.system.exitProcess import kotlin.system.exitProcess
@ -50,7 +50,7 @@ private fun createLocalProvInstance(): Prov {
"Please enter password to configure sudo without password in the future." + "Please enter password to configure sudo without password in the future." +
"\nWarning: This will permanently allow your user to use sudo privileges without a password." "\nWarning: This will permanently allow your user to use sudo privileges without a password."
).secret() ).secret()
prov.makeUserSudoerWithoutPasswordRequired(password) prov.makeCurrentUserSudoerWithoutPasswordRequired(password)
} }
return prov return prov
} }
@ -76,7 +76,7 @@ private fun createRemoteProvInstance(
require( require(
password != null, password != null,
{ "User ${prov.whoami()} not able to sudo on remote machine without password and no password available for the user." }) { "User ${prov.whoami()} not able to sudo on remote machine without password and no password available for the user." })
prov.makeUserSudoerWithoutPasswordRequired(password) prov.makeCurrentUserSudoerWithoutPasswordRequired(password)
// a new session is required after making the user a sudoer without password // a new session is required after making the user a sudoer without password
return remote(host, remoteUser, password) return remote(host, remoteUser, password)

View file

@ -89,7 +89,7 @@ fun Prov.makeUserSudoerWithoutPasswordRequired(
userName: String, userName: String,
password: Secret? = null, password: Secret? = null,
overwriteFile: Boolean = false overwriteFile: Boolean = false
): ProvResult = task { ): ProvResult = taskWithResult {
val userSudoFile = "/etc/sudoers.d/$userName" val userSudoFile = "/etc/sudoers.d/$userName"
if (!checkFile(userSudoFile) || overwriteFile) { if (!checkFile(userSudoFile) || overwriteFile) {
val sudoPrefix = if (password == null) "sudo" else "echo ${password.plain()} | sudo -S" val sudoPrefix = if (password == null) "sudo" else "echo ${password.plain()} | sudo -S"
@ -107,8 +107,7 @@ fun Prov.makeUserSudoerWithoutPasswordRequired(
* Makes the current (executing) user be able to sudo without password. * Makes the current (executing) user be able to sudo without password.
* IMPORTANT: Current user must already by sudoer when calling this function. * IMPORTANT: Current user must already by sudoer when calling this function.
*/ */
@Suppress("unused") // used externally fun Prov.makeCurrentUserSudoerWithoutPasswordRequired(password: Secret) = taskWithResult {
fun Prov.makeUserSudoerWithoutPasswordRequired(password: Secret) = task {
val currentUser = whoami() val currentUser = whoami()
if (currentUser != null) { if (currentUser != null) {
makeUserSudoerWithoutPasswordRequired(currentUser, password, overwriteFile = true) makeUserSudoerWithoutPasswordRequired(currentUser, password, overwriteFile = true)