diff --git a/src/create-remote-user/devcontainer-feature.json b/src/create-remote-user/devcontainer-feature.json index a31ecafdb..b7da95269 100644 --- a/src/create-remote-user/devcontainer-feature.json +++ b/src/create-remote-user/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Create Remote User", "id": "create-remote-user", - "version": "0.0.3", + "version": "0.0.4", "description": "A to assert the configured remote user exists in the container", "licenseURL": "https://github.com/nils-geistmann/devcontainers-features/blob/main/LICENSE", "options": { @@ -19,6 +19,11 @@ "description": "Install sudo if it is not yet installed", "type": "boolean", "default": true + }, + "passwordLessSudo": { + "description": "Configures sudo to allow the remote user to elevate permissions without password", + "type": "boolean", + "default": false } } } diff --git a/src/create-remote-user/install.sh b/src/create-remote-user/install.sh index 699dd9a65..626f381c0 100755 --- a/src/create-remote-user/install.sh +++ b/src/create-remote-user/install.sh @@ -29,4 +29,9 @@ if [ "$INSTALLSUDO" = "true" ] && ! which sudo; then check_and_install sudo fi +if [ "$PASSWORDLESSSUDO" = "true" ]; then + echo "$_REMOTE_USER" ALL=\(root\) NOPASSWD:ALL > "/etc/sudoers.d/$_REMOTE_USER" +fi + + clean_package_cache diff --git a/test/create-remote-user/default_configuration.sh b/test/create-remote-user/default_configuration.sh index bebca0689..88019b96d 100644 --- a/test/create-remote-user/default_configuration.sh +++ b/test/create-remote-user/default_configuration.sh @@ -7,5 +7,6 @@ check "configured user should exist" assert_user_exists "remote" check "configured user should be in group sudo" assert_user_is_in_group "remote" "sudo" check "sudo should be available" assert_command_is_available "sudo" check "password for user should be set" assert_password_is_set "remote" "remote" +check "sudo should require a password" assert_sudo_requires_password "remote" reportResults diff --git a/test/create-remote-user/scenarios.json b/test/create-remote-user/scenarios.json index 08e961a68..deebf0839 100644 --- a/test/create-remote-user/scenarios.json +++ b/test/create-remote-user/scenarios.json @@ -15,5 +15,14 @@ } }, "remoteUser": "remote" + }, + "sudo_does_not_require_a_password": { + "image": "debian:bookworm-slim", + "features": { + "create-remote-user": { + "passwordLessSudo": true + } + }, + "remoteUser": "remote" } } diff --git a/test/create-remote-user/sudo_does_not_require_a_password.sh b/test/create-remote-user/sudo_does_not_require_a_password.sh new file mode 100644 index 000000000..8c7d9e246 --- /dev/null +++ b/test/create-remote-user/sudo_does_not_require_a_password.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +source dev-container-features-test-lib +source test_functions.sh + +check "sudo does not require a password" assert_sudo_requires_no_password "remote" + +reportResults diff --git a/test/create-remote-user/test_functions.sh b/test/create-remote-user/test_functions.sh index 99c19b92a..5a0c95909 100644 --- a/test/create-remote-user/test_functions.sh +++ b/test/create-remote-user/test_functions.sh @@ -36,3 +36,15 @@ assert_password_is_set() { exit 1 fi } + +assert_sudo_requires_password() { + if echo "$1" | sudo -S -l | grep NOPASSWD; then + exit 1 + fi +} + +assert_sudo_requires_no_password() { + if ! echo "$1" | sudo -S -l | grep NOPASSWD; then + exit 1 + fi +}