From 98b5b1dd1d4220b4951aceeb5871aab101912396 Mon Sep 17 00:00:00 2001 From: Alexander Smolyakov Date: Thu, 11 May 2023 20:23:49 +0400 Subject: [PATCH] [universal] Jekyll feature: Fix permissions for ruby version manager folder (#554) * [universal/jekyll] Fix permissions for ruby version manager folder - Change the setting up permission logic to be compatible with ruby feature * Empty-Commit * Revert "[universal/jekyll] Fix permissions for ruby version manager folder" This reverts commit 66486ef12d7a55107b9c0b34ef02b3f05963110d. * [universal] Jekyll feature: Rework the fix - Rework the fix to sync permissions for the ruby version manager gem folder only * Add directory ownership check - Add function to check directory ownership - Add test to ensure that `codespace` user has ownership over `extension` directory * Rework test to use IDs instead of names * Restart CI checks * Temp: Add diagnostics * Rework test --- .../local-features/jekyll/install.sh | 7 ++++ src/universal/test-project/test-utils.sh | 38 +++++++++++++++++++ src/universal/test-project/test.sh | 3 ++ 3 files changed, 48 insertions(+) diff --git a/src/universal/.devcontainer/local-features/jekyll/install.sh b/src/universal/.devcontainer/local-features/jekyll/install.sh index 2ff28c848..8f3ca139c 100644 --- a/src/universal/.devcontainer/local-features/jekyll/install.sh +++ b/src/universal/.devcontainer/local-features/jekyll/install.sh @@ -30,4 +30,11 @@ if ! jekyll --version > /dev/null ; then chown -R "${USERNAME}:rvm" "${GEMS_DIR}/" chmod -R g+r+w "${GEMS_DIR}/" find "${GEMS_DIR}" -type d | xargs -n 1 chmod g+s + + # Make sure the user has the necessary permissions to install the gems + RVM_GEMS_DIR=/usr/local/rvm/gems/default/extensions + + chown -R "${USERNAME}:rvm" "${RVM_GEMS_DIR}/" + chmod -R g+r+w "${RVM_GEMS_DIR}/" + find "${RVM_GEMS_DIR}" -type d | xargs -n 1 chmod g+s fi diff --git a/src/universal/test-project/test-utils.sh b/src/universal/test-project/test-utils.sh index 63996d31a..863526f70 100644 --- a/src/universal/test-project/test-utils.sh +++ b/src/universal/test-project/test-utils.sh @@ -179,3 +179,41 @@ checkVersionCount() { return 1 fi } + +checkDirectoryOwnership() { + LABEL=$1 + targetDirectory=$2 + expectedUser=$3 + expectedGroup=$4 + + echo -e "\n🧪 Testing $LABEL" + + # Get group metadata + groupMetadata=$(getent group ${expectedGroup}) + + # Extract group id and group members + targetGroupId=$(echo $groupMetadata | cut -d: -f3) + targetGroupMembers=$(echo $groupMetadata | cut -d: -f4) + + # Get directory ownership metadata + # Note: "stat" returns the string "UNKNOWN" for %U and %G if it's not defined in the system files. + # So it's better to work with UID (%u) and GID (%g) numbers from "stat". + directoryOwnershipGroupId=$(stat -c "%g" ${targetDirectory}) + + # Check that group has ownership over directory and user belong to the group + if [ "$targetGroupId" == "$directoryOwnershipGroupId" ] && [[ "$targetGroupMembers" == *"$expectedUser"* ]]; then + echo "✅ Passed!" + return 0 + else + expected="Expected: Group - $expectedGroup ($targetGroupId), User - $expectedUser" + got="Got: $(stat -c "Group - %G (%g), User - %U (%u)" ${targetDirectory})" + echoStderr "❌ $LABEL check failed. $expected $got" + + # Provide more context on test failure + stat ${targetDirectory} + + FAILED+=("$LABEL") + + return 1 + fi +} diff --git a/src/universal/test-project/test.sh b/src/universal/test-project/test.sh index 48136a475..3ca51a292 100644 --- a/src/universal/test-project/test.sh +++ b/src/universal/test-project/test.sh @@ -84,6 +84,9 @@ count=$(ls /usr/local/rvm/gems | wc -l) expectedCount=6 # 2 version folders + 2 global folders for each version + 1 default folder which links to either one of the version + 1 cache folder checkVersionCount "two versions of ruby are present" $count $expectedCount echo $(echo "ruby versions" && ls -a /usr/local/rvm/rubies) +rvmExtensions="/usr/local/rvm/gems/default/extensions" +rvmPlatform=$(rvm info default ruby | grep -w "platform" | cut -d'"' -f 2) +checkDirectoryOwnership "codespace user has ownership over extension directory" "$rvmExtensions/$rvmPlatform" "codespace" "rvm" # Node.js check "node" node --version