Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: common-utils configureZshAsDefaultShell option not working on alpine linux image #557

Merged
merged 12 commits into from
Aug 2, 2023

Conversation

krikchaip
Copy link
Contributor

@krikchaip krikchaip commented May 23, 2023

Hi, I was trying to set up a devcontainer for my side-project using the base alpine image with common-utils feature. I knew that the image already included the feature, but I also wanted to set the default shell to zsh. According to the documentation, I had to pass "configureZshAsDefaultShell": "true", but it didn't seem to be working for me.

While reading error logs, I found that the problem came from chsh command, which was always asking for a password.

https://github.com/devcontainers/features/blob/main/src/common-utils/main.sh#LL439C9-L441C7

if [ "${CONFIGURE_ZSH_AS_DEFAULT_SHELL}" == "true" ]; then
    chsh --shell /bin/zsh ${USERNAME}

    # PASSWORD:
    # error -> chsh: PAM: Authentication failure
fi

After doing some research, I found the solution. Basically, You need to modify the content in some file as shown on my PR.

/etc/pam.d/chsh

auth sufficient pam_shells.so

@krikchaip krikchaip requested a review from a team as a code owner May 23, 2023 10:40
@krikchaip
Copy link
Contributor Author

@microsoft-github-policy-service agree

@krikchaip
Copy link
Contributor Author

The license/cla status has been queued for over a day. How can I make it green? 🤔

Copy link
Member

@samruddhikhandale samruddhikhandale left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, left some thoughts!

The license/cla status has been queued for over a day. How can I make it green? 🤔

Mmm, I expect that it should pass now as the CLA is signed. I don't see an option to retry the check, should have been automatic.

Looking at microsoft/ContributorLicenseAgreement#123 , probably closing & reopening the PR or pushing a new commit helps.

src/common-utils/main.sh Outdated Show resolved Hide resolved
src/common-utils/main.sh Outdated Show resolved Hide resolved
@krikchaip
Copy link
Contributor Author

I found something interesting inside /etc/pam.d/chsh of the devcontainer of this repo.

image

Seems like we should add auth sufficient pam_rootok.so instead of pam_shells.so 🤔 .

src/common-utils/main.sh Outdated Show resolved Hide resolved
src/common-utils/main.sh Show resolved Hide resolved
@krikchaip
Copy link
Contributor Author

krikchaip commented Jun 29, 2023

@samruddhikhandale hmmm. strange. I've also tried the command with /etc/pam.d/chsh that has some content inside similar to you. It seemed to work pretty well AFAIK. 🤔

720p.mov

@samruddhikhandale
Copy link
Member

@krikchaip In your case, you are running the conditions from the terminal, however, I tested it with a Feature test scenario (which resembles an actual Feature installation). Also, the install script is run with user:root, and in your case, it's node. I believe these are the reasons why we both are seeing different results.

src/common-utils/main.sh Outdated Show resolved Hide resolved
@krikchaip
Copy link
Contributor Author

krikchaip commented Jun 30, 2023

in your case, you are running the conditions from the terminal, however, I tested it with a Feature test scenario (which resembles an actual Feature installation). Also, the install script is run with user:root, and in your case, it's node. I believe these are the reasons why we both are seeing different results.

@samruddhikhandale Did you mean the configure_zsh_as_default_shell scenario? If so, do you have any suggestion on how can I provide my own /etc/pam.d/chsh file? I basically want to replicate the same scenario as yours

@samruddhikhandale
Copy link
Member

@krikchaip Add echo statements to help debug the file changes to 👇

if [ "${CONFIGURE_ZSH_AS_DEFAULT_SHELL}" == "true" ]; then
# Fixing chsh always asking for a password on alpine linux
# ref: https://askubuntu.com/questions/812420/chsh-always-asking-a-password-and-get-pam-authentication-failure.
if [ -f "/etc/pam.d/chsh" ] && grep -vEq "^auth[[:blank:]]+sufficient[[:blank:]]+pam_rootok\.so$"; then
awk '/^auth(.*)pam_rootok\.so$/ { $2 = "sufficient" } { print }' /etc/pam.d/chsh > /tmp/chsh.tmp && mv /tmp/chsh.tmp /etc/pam.d/chsh
else
echo "auth sufficient pam_rootok.so" >> /etc/pam.d/chsh
fi
and run BUILDKIT_PROGRESS=plain devcontainer features test -f common-utils --filter configure_zsh_as_default_shell --skip-autogenerated

@krikchaip
Copy link
Contributor Author

@samruddhikhandale Opsie.. my bad XD. Accidentally forgot that grep requires 2 parameters...

image

Please check again. It should be working rn.

image

@samruddhikhandale
Copy link
Member

Can add another condition so that we could avoid running this command when /etc/pam.d/chsh already contains auth sufficient pam_rootok.so ?

Following up on #557 (comment)

@krikchaip Can we update the logic to NOT make any changes to the file if auth sufficient pam_rootok.so exists? I don't think we should touch/make changes to a file if that's not needed. Saw this with configure_zsh_as_default_shell scenario, the diff is easily visible with respect to spaces

@krikchaip
Copy link
Contributor Author

@samruddhikhandale I believe the logic here seems to prevent it from touching the /etc/pam.d/chsh file, but let me check again some time soon.

if [ -f "/etc/pam.d/chsh" ] &&
  grep -vEq "^auth[[:blank:]]+sufficient[[:blank:]]+pam_rootok\.so$" /etc/pam.d/chsh;

@krikchaip
Copy link
Contributor Author

@samruddhikhandale please recheck

Copy link
Member

@samruddhikhandale samruddhikhandale left a comment

Choose a reason for hiding this comment

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

Looks great, thank you so much for taking the time to contribute this PR and reiterating on feedbacks. We appreciate it! ❇️

@samruddhikhandale samruddhikhandale merged commit 685fa2e into devcontainers:main Aug 2, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants