From ed0060ebc48e948f8ea11270a0c06fa840926a1b Mon Sep 17 00:00:00 2001 From: Pablo Ulloa Date: Tue, 26 Sep 2023 01:34:28 +0000 Subject: [PATCH 1/3] [common-utils] fix bug when user home is custom by base docker --- src/common-utils/main.sh | 13 +++++++++---- test/common-utils/devcontainer-custom-home.sh | 13 +++++++++++++ .../devcontainer-custom-home/Dockerfile | 4 ++++ .../devcontainer-custom-user-default-home.sh | 13 +++++++++++++ test/common-utils/scenarios.json | 16 ++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/common-utils/devcontainer-custom-home.sh create mode 100644 test/common-utils/devcontainer-custom-home/Dockerfile create mode 100644 test/common-utils/devcontainer-custom-user-default-home.sh diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index 4a048db71..d198179a0 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -408,10 +408,15 @@ fi if [ "${USERNAME}" = "root" ]; then user_home="/root" else - user_home="/home/${USERNAME}" - if [ ! -d "${user_home}" ]; then - mkdir -p "${user_home}" - chown ${USERNAME}:${group_name} "${user_home}" + # Check if user already has a home directory other than /home/${USERNAME} + if [ "/home/${USERNAME}" != $( getent passwd $USERNAME | cut -d: -f6 ) ]; then + user_home=$( getent passwd $USERNAME | cut -d: -f6 ) + else + user_home="/home/${USERNAME}" + if [ ! -d "${user_home}" ]; then + mkdir -p "${user_home}" + chown ${USERNAME}:${group_name} "${user_home}" + fi fi fi diff --git a/test/common-utils/devcontainer-custom-home.sh b/test/common-utils/devcontainer-custom-home.sh new file mode 100644 index 000000000..9b64cc0bf --- /dev/null +++ b/test/common-utils/devcontainer-custom-home.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +check "user is customUser" grep customUser <(whoami) +check "home is /customHome" grep "/customHome" <(getent passwd customUser | cut -d: -f6) + +# Report result +reportResults diff --git a/test/common-utils/devcontainer-custom-home/Dockerfile b/test/common-utils/devcontainer-custom-home/Dockerfile new file mode 100644 index 000000000..a94af4bc1 --- /dev/null +++ b/test/common-utils/devcontainer-custom-home/Dockerfile @@ -0,0 +1,4 @@ +FROM ubuntu:focal + +RUN groupadd customUser -g 30000 && \ + useradd customUser -u 30000 -g 30000 --create-home --home-dir /customHome diff --git a/test/common-utils/devcontainer-custom-user-default-home.sh b/test/common-utils/devcontainer-custom-user-default-home.sh new file mode 100644 index 000000000..f29bd7c74 --- /dev/null +++ b/test/common-utils/devcontainer-custom-user-default-home.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +check "user is customUser" grep customUser <(whoami) +check "home is /home/customUser" grep "/home/customUser" <(getent passwd customUser | cut -d: -f6) + +# Report result +reportResults diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index d7b296d41..bb65874e1 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -183,5 +183,21 @@ "configureZshAsDefaultShell": true } } + }, + "devcontainer-custom-home": { + "build": { + "dockerfile": "Dockerfile" + }, + "remoteUser": "customUser", + "features": { + "common-utils": {} + } + }, + "devcontainer-custom-user-default-home": { + "image": "mcr.microsoft.com/devcontainers/base:alpine", + "remoteUser": "customUser", + "features": { + "common-utils": {} + } } } From 8a4bd2068421fdd8391c8c2bd6dff5eb55d79d66 Mon Sep 17 00:00:00 2001 From: Pablo Ulloa Date: Wed, 27 Sep 2023 01:49:06 +0000 Subject: [PATCH 2/3] bumped minor version --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 869812be6..181e54594 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "common-utils", - "version": "2.1.3", + "version": "2.2.0", "name": "Common Utilities", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils", "description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.", From cbb6ae5dcb3f164a7f9042ac24157f30707a53fa Mon Sep 17 00:00:00 2001 From: Pablo Ulloa Date: Wed, 27 Sep 2023 02:03:03 +0000 Subject: [PATCH 3/3] reduced conditions to improve readability --- src/common-utils/main.sh | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index d198179a0..71422465c 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -407,16 +407,14 @@ fi if [ "${USERNAME}" = "root" ]; then user_home="/root" +# Check if user already has a home directory other than /home/${USERNAME} +elif [ "/home/${USERNAME}" != $( getent passwd $USERNAME | cut -d: -f6 ) ]; then + user_home=$( getent passwd $USERNAME | cut -d: -f6 ) else - # Check if user already has a home directory other than /home/${USERNAME} - if [ "/home/${USERNAME}" != $( getent passwd $USERNAME | cut -d: -f6 ) ]; then - user_home=$( getent passwd $USERNAME | cut -d: -f6 ) - else - user_home="/home/${USERNAME}" - if [ ! -d "${user_home}" ]; then - mkdir -p "${user_home}" - chown ${USERNAME}:${group_name} "${user_home}" - fi + user_home="/home/${USERNAME}" + if [ ! -d "${user_home}" ]; then + mkdir -p "${user_home}" + chown ${USERNAME}:${group_name} "${user_home}" fi fi