Skip to content

Conversation

josibake
Copy link
Member

@josibake josibake commented Aug 22, 2022

Previously, everything in the ci docker image ran as the root user. This would lead to certain directories (ci/scratch, depends) being owned by root after running the ci locally which would lead to annoying behavior such as subsequent guix builds failing due to depends/ being owned by root.

This PR adds a non-root user in the container and chowns the mounted working directory. All the docker exec commands now run as the non-root user, except for the few that still need to run as root (mainly, installing packages).

To test this I checked out a fresh copy of the repo, applied my changes, ran the CI, and verified all the local file permissions were unchanged after the CI was finished running.

@fanquake fanquake added the Tests label Aug 22, 2022
@hebasto
Copy link
Member

hebasto commented Aug 22, 2022

Concept ACK.

Fix linter failures?

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 23, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK hebasto
Concept ACK w0xlt
Stale ACK satsie

Conflicts

No conflicts as of last run.

@satsie
Copy link
Contributor

satsie commented Sep 27, 2022

Concept ACK. I hope to circle back and do a code review.

I tested this PR, and things behave as expected. In addition to doing a spot check on the directories, I made an attempt to tally up all the files that had ownership changes after running the CI (./ci/test_run_all.sh).

Number of files that had some kind of change to their read/write/attribute/execute permissions:

  • master branch: 25,806
  • this PR: 41

For both scenarios I made a fresh directory and clone of the code base. I used auditctl to monitor the files and documented my steps here: https://gist.github.com/satsie/a73fe711954dc4bca8b43030bbd4fb2d

I'm completely new to auditctl so I wouldn't be surprised if there are improvements that can be made to my process, but my takeaway is this PR results in far fewer changes in file ownership (yay!).

For the 41 file permission changes that were present with this PR, I see that a lot of them are the result of git commands, and mkdir, so it's very likely this PR got them all. Happy to iterate on this if anyone has more insight, especially regarding how to use auditctl.

@josibake josibake force-pushed the josibake-fix-docker-ci-permissions branch 3 times, most recently from a852749 to 190b4f3 Compare October 6, 2022 20:26
@josibake
Copy link
Member Author

josibake commented Oct 6, 2022

I reworked this to incorporate feedback from @MarcoFalke: removed the Dockerfile and entrypoint.sh script and now create the non-root user directly in the running docker container. Also fixed the linting errors.

@satsie thanks for providing the testing script! I ran it and confirmed local file permissions were unchanged after running the ci script.

@josibake josibake force-pushed the josibake-fix-docker-ci-permissions branch from 190b4f3 to 05ce35e Compare October 7, 2022 15:20
@josibake
Copy link
Member Author

josibake commented Oct 7, 2022

removed to extraneous _ROOT calls; git range-diff master 190b4f3 05ce35e

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 05ce35e, I have reviewed the code and it looks OK. Also tested on Ubuntu 22.04: no root-owned files were detected after execution of:

$ make distclean
$ env MAKEJOBS="-j16" FILE_ENV="./ci/test/00_setup_env_win64.sh" ./ci/test_run_all.sh

@hebasto
Copy link
Member

hebasto commented Oct 10, 2022

After more tests done, suggesting:

--- a/ci/test/04_install.sh
+++ b/ci/test/04_install.sh
@@ -144,7 +144,7 @@ if [[ "${RUN_TIDY}" == "true" ]]; then
     CI_EXEC "mkdir -p ${DIR_IWYU}/build/"
     CI_EXEC "git clone --depth=1 https://github.com/include-what-you-use/include-what-you-use -b clang_14 ${DIR_IWYU}/include-what-you-use"
     CI_EXEC "cd ${DIR_IWYU}/build && cmake -G 'Unix Makefiles' -DCMAKE_PREFIX_PATH=/usr/lib/llvm-14 ../include-what-you-use"
-    CI_EXEC "cd ${DIR_IWYU}/build && make install $MAKEJOBS"
+    CI_EXEC_ROOT "cd ${DIR_IWYU}/build && make install $MAKEJOBS"
   fi
 fi
 

@satsie
Copy link
Contributor

satsie commented Oct 11, 2022

ACK 05ce35e - I tested the latest changes by performing the following steps on (1.) the master branch, and (2.) a fresh clone of the repo with this PR. I'm using Ubuntu 22.04, with the new 6.0 Linux kernel:

  1. ./autogen.sh
  2. ./configure.sh
  3. make
  4. ./ci/test_run_all.sh

Per hebasto's comment, realized there is a much, much easier way to check that this PR is working 😅 Instead of the auditctl commands I mentioned earlier, I simply checked the directory to make sure root didn't own any files.

Master branch:

find bitcoin/ -user root | wc -l
17315

This PR:

find bitcoin/ -user root | wc -l
0

Since you mentioned failing guix builds as a motivator for this PR, and I need the practice, I went ahead and ran a guix build:

guix SHA256 sums
guix.sigs/05ce35efaa77$ cat satsie/noncodesigned.SHA256SUMS
510ba7aa76869af85b586be1f9aa9110cf454abdc7047092f6e8765ad024df69  bitcoin-05ce35efaa77-aarch64-linux-gnu-debug.tar.gz
d0f07868e1cf7fc440c3ff551cf651f7ab8b3badbb28fc1f514896c74cf13384  bitcoin-05ce35efaa77-aarch64-linux-gnu.tar.gz
20bcc58ef04c2bde2573ebfe7265c910862f550517d5d2436e92c80da99090cd  bitcoin-05ce35efaa77-arm-linux-gnueabihf-debug.tar.gz
be95018d0be0a8578f31a72517725bbd3026f3c533a852eb367de7485bf0d3a6  bitcoin-05ce35efaa77-arm-linux-gnueabihf.tar.gz
5d3b941b1121355df626c58d752ebf995c5cb4894ca94aea01ac9c7da3460d6f  bitcoin-05ce35efaa77-arm64-apple-darwin-unsigned.dmg
a9364fddf9a0af1dec52fcad43d786a4b6b198e2ce3dcda3b7e48e31e1e96190  bitcoin-05ce35efaa77-arm64-apple-darwin-unsigned.tar.gz
a1545413dec498fe503da178312e10790f8ce6324be677fd13a00c7749a360d5  bitcoin-05ce35efaa77-arm64-apple-darwin.tar.gz
afa502db06c93bbfee44cd193f59be614b91181590225c5d4b4ec5a84dfdf0a8  bitcoin-05ce35efaa77.tar.gz
8268f616fb0a8bf04da556331e92ccbdbc6f627cd078f923ca9629184995586f  bitcoin-05ce35efaa77-powerpc64-linux-gnu-debug.tar.gz
a2fde1f7d89702dc950e830ef34664caa9790944b62cf8cc09bb3a93b2588f25  bitcoin-05ce35efaa77-powerpc64-linux-gnu.tar.gz
55b8a6415a99e33389120552e12dd98e0d293bbcb28cddcfcfd49cf9773425c1  bitcoin-05ce35efaa77-powerpc64le-linux-gnu-debug.tar.gz
cedfe14cec343f9895f090446b84075aaa9bacc824aa380b89e225382868cee0  bitcoin-05ce35efaa77-powerpc64le-linux-gnu.tar.gz
bf2961331c1161b9701d7d79462705d05dbdf1f5c29c1f2a1b1c6391de3aa46c  bitcoin-05ce35efaa77-riscv64-linux-gnu-debug.tar.gz
c3451bfb427ac6e346f4fecff1e76b9f08702d877ed19923e1dff706f65cfd32  bitcoin-05ce35efaa77-riscv64-linux-gnu.tar.gz
c0c0d8638b22a75c3e0efd7f31da1c5bed208ef6bf65544ef15ebce309896f28  bitcoin-05ce35efaa77-x86_64-apple-darwin-unsigned.dmg
bb7920fbfe3ac98b1cc17cf06771e5c5601d8c896caf95c3e5c63ccaa054bc52  bitcoin-05ce35efaa77-x86_64-apple-darwin-unsigned.tar.gz
e8b6bad3ca0252aa65e86e32b38f44cb7bff2cb82da7b7ecade8f727b3405c32  bitcoin-05ce35efaa77-x86_64-apple-darwin.tar.gz
af4bb718b88c143dc9873e9be16d2c1dda286e5bceae9607ba6dd6755ed4cd5c  bitcoin-05ce35efaa77-x86_64-linux-gnu-debug.tar.gz
3df38517b0162fe21f2fbfba3c3d7b6eac799285f409dd5bbe84deb78efbac84  bitcoin-05ce35efaa77-x86_64-linux-gnu.tar.gz
d75f46751f4562fa4316009dd5a7fed7418e686c11392064b86e0b229cac4fee  bitcoin-05ce35efaa77-win64-debug.zip
70acfd1c279a65a51992756801e0d9a138c8fa1f155d795fb19a8686bf5d3a16  bitcoin-05ce35efaa77-win64-setup-unsigned.exe
e6e27ff5b20d41659b5b58c6735c7e89fecbb947ad4d0640eb8aa1f7bddf0d62  bitcoin-05ce35efaa77-win64-unsigned.tar.gz
54f4dfbedd651b837e20f39b205670e55fe29ddae9cf867001ce078e69a46730  bitcoin-05ce35efaa77-win64.zip

As far as code review goes, I appreciate the effort you put in to get all the commands into the 04_install.sh script. I didn't get a chance to fully review the changes prior, but this new changeset is very straightforward and easy to read through. The addition of the new non root user that maps to the current one (in both ID and group ID), and the careful execution of only certain commands as root looks good to me.

@maflcko
Copy link
Member

maflcko commented Oct 11, 2022

This is still open: #25900 (comment)

@hebasto
Copy link
Member

hebasto commented Nov 11, 2022

@josibake

It seems the only changes, which are required to get this nice PR merged, are as follows:

diff --git a/ci/test/04_install.sh b/ci/test/04_install.sh
index e83d78d657..99da8e3b00 100755
--- a/ci/test/04_install.sh
+++ b/ci/test/04_install.sh
@@ -144,7 +144,7 @@ if [[ "${RUN_TIDY}" == "true" ]]; then
     CI_EXEC "mkdir -p ${DIR_IWYU}/build/"
     CI_EXEC "git clone --depth=1 https://github.com/include-what-you-use/include-what-you-use -b clang_14 ${DIR_IWYU}/include-what-you-use"
     CI_EXEC "cd ${DIR_IWYU}/build && cmake -G 'Unix Makefiles' -DCMAKE_PREFIX_PATH=/usr/lib/llvm-14 ../include-what-you-use"
-    CI_EXEC "cd ${DIR_IWYU}/build && make install $MAKEJOBS"
+    CI_EXEC_ROOT "cd ${DIR_IWYU}/build && make install $MAKEJOBS"
   fi
 fi
 
diff --git a/ci/test/05_before_script.sh b/ci/test/05_before_script.sh
index ef3dff86ca..dd2b43d38b 100755
--- a/ci/test/05_before_script.sh
+++ b/ci/test/05_before_script.sh
@@ -11,6 +11,7 @@ if [ "$CI_OS_NAME" == "macos" ]; then
   echo > "${HOME}/Library/Application Support/Bitcoin"
 else
   CI_EXEC echo \> \$HOME/.bitcoin
+  CI_EXEC_ROOT echo \> \$HOME/.bitcoin
 fi
 
 CI_EXEC mkdir -p "${DEPENDS_DIR}/SDKs" "${DEPENDS_DIR}/sdk-sources"

which in turn address both #25900 (comment) and #25900 (comment).

Running all commands as the root user in the docker image
will change local file permissions in the ci and depends directory.

Add a non-root user to the container and use this user whenever
possible when running docker exec commands.
@josibake josibake force-pushed the josibake-fix-docker-ci-permissions branch from 05ce35e to 849f20a Compare November 21, 2022 17:19
@josibake
Copy link
Member Author

hey @hebasto , thanks for the testing and suggested changes! I have added both. @MarcoFalke I verified that there is a .bitcoin dummy file created in the container for both users (root/nonroot), so this should be good to go.

changes: git range-diff master 05ce35e 849f20a

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 849f20a, tested on Ubuntu 22.04 by running commands as follows:

git clean -xdff
MAKEJOBS="-j8" FILE_ENV="./ci/test/00_setup_env_arm.sh" ./ci/test_run_all.sh
MAKEJOBS="-j8" FILE_ENV="./ci/test/00_setup_env_native_tidy.sh" ./ci/test_run_all.sh
git clean -xdff

@maflcko maflcko merged commit 85892f7 into bitcoin:master Nov 22, 2022
LOCAL_GID=$(id -g)

# the name isn't important, so long as we use the same UID
LOCAL_USER=nonroot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true. If a different name is used, it will require root permission to create non-default folders.

DIR_QA_ASSETS=/home/new_user/qa_assets_clean CCACHE_DIR=/home/new_user/ccache_dir_clean CCACHE_SIZE=500M MAKEJOBS="-j4" FILE_ENV="./ci/test/00_setup_env_arm.sh" ./ci/test_run_all.sh

fails with:

mkdir: cannot create directory ‘/home/new_user/qa_assets_clean’: Permission denied

Presumably because new_user does not exist in the docker (only outside) and is thus root-owned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the username from the host is used, this will print a warning:

useradd: warning: the home directory /home/new_user already exists.

But I guess this is fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, wrong alarm. I think your code is all fine. It is not possible to set DIR_QA_ASSETS, because it is not mounted.

Any folder that is mounted (like CCACHE_DIR), will be created before mounting, see the comment # Create folders that are mounted into the docker. Also, the BASE_ROOT_DIR, which may live outside of /home/nonroot in the docker, will be chown'd to nonroot.

So the code should be good as-is. (I'll continue testing a bit)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the thorough testing! much appreciated.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 22, 2022
@maflcko
Copy link
Member

maflcko commented Nov 25, 2022

Did some more testing and FILE_ENV="./ci/test/00_setup_env_native_msan.sh" fails with:

update-alternatives: error: error creating symbolic link '/etc/alternatives/clang++.dpkg-tmp': Permission denied

(not sure if related to this pull)

@josibake
Copy link
Member Author

Did some more testing and FILE_ENV="./ci/test/00_setup_env_native_msan.sh" fails with:

update-alternatives: error: error creating symbolic link '/etc/alternatives/clang++.dpkg-tmp': Permission denied

(not sure if related to this pull)

good catch! this is related to this pull. I reverted the commit, ran ci without errors, and then reproduced the error running with the commit. fixed in #26574

maflcko pushed a commit that referenced this pull request Nov 28, 2022
54dd8f5 ci: use ci_exec_root for clang install (josibake)

Pull request description:

  fixes a bug introduced in #25900 ; see #25900 (comment)

  the general idea of #25900 was to use a non-root user as much as possible to avoid modifying the user's local filesystem. however, it appears the root user is needed to correctly install clang.

ACKs for top commit:
  hebasto:
    ACK 54dd8f5, tested on Ubuntu 22.04.

Tree-SHA512: beb01d4b6127fbba3c8d18e85cf7ec7d1b2ec93ea05c475ab51bcaa04ef1b0591d886f1a7e0732c5ae86806013f022c0b44027380d2b0cfb1bfdc843e40f99b4
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 1, 2022
54dd8f5 ci: use ci_exec_root for clang install (josibake)

Pull request description:

  fixes a bug introduced in bitcoin#25900 ; see bitcoin#25900 (comment)

  the general idea of bitcoin#25900 was to use a non-root user as much as possible to avoid modifying the user's local filesystem. however, it appears the root user is needed to correctly install clang.

ACKs for top commit:
  hebasto:
    ACK 54dd8f5, tested on Ubuntu 22.04.

Tree-SHA512: beb01d4b6127fbba3c8d18e85cf7ec7d1b2ec93ea05c475ab51bcaa04ef1b0591d886f1a7e0732c5ae86806013f022c0b44027380d2b0cfb1bfdc843e40f99b4
@bitcoin bitcoin locked and limited conversation to collaborators Nov 25, 2023
@josibake josibake deleted the josibake-fix-docker-ci-permissions branch January 26, 2024 10:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants