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

init-user: Safeguarded linking directories and files #922

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

systemofapwne
Copy link

@systemofapwne systemofapwne commented Feb 3, 2025

Proposed Changes

This PR adds the --no-dereference (-n) flag for linking folders in order to avoid linking too deep as a good practice.
It also convers linking errors of .ssh and .gitconfig to warnings via bashio to avoid s6 to crash the container for certain container deployment setups [1].

I observed that restarting the container (when deployed standalone [1]), the init-user script succeeds on the first container start without any problems. Another container restart however re-ran the init-user script, which caused two issues:

  • Creation of links inside linked directories. E.g. ln -s /config ${HOME}/config created /config/config if ${HOME}/config already existed.
  • The container crashed because init-user tried to recreated existing links, that were not guarded via || like ln -s src target || bashio::log.warning "The warning message" that absorbed the non-zero return code of ln on failues (.gitignore and .ssh).

However (I hope this does not get too quickly discared as a wont fix):
For container-addons spawned by the HA supervisor, these issues might not appear when the container is deployed as it is intended via HA and the supervisor.
However, standalone deployments [1] will cause this issue. Since my proposed changes are just good practice, I hope you consider to add my minute changes.

[1] Standalone deployment for either testing, debugging etc or in my case: manually integrating the container in an already existing big docker stack without using the supervisor

Summary by CodeRabbit

  • Chores
    • Improved the setup process to preserve your existing configuration files and directories without unintended overwrites, while maintaining robust error notifications.

…lag to avoid link-loops. Catching linking of .gitignore and .ssh via a warning, so that s6 does does not crash (and with it, the container)
Copy link

coderabbitai bot commented Feb 3, 2025

Walkthrough

The script has been updated to modify symbolic link creation. The commands now use ln -sn instead of ln -s to avoid overwriting existing files when linking common directories, the .ssh directory, and the .gitconfig file. The error handling remains unchanged, logging warnings if a linking attempt fails.

Changes

File(s) Changes
vscode/rootfs/.../init-user/run Updated symbolic link commands from ln -s to ln -sn for safe creation of symbolic links.

Possibly related PRs

Suggested labels

bugfix

Poem

Hopping through the code with glee,
I tweak the links so safely, you see.
No overwrites, just a gentle command,
Keeping files intact, oh so grand!
A bunny’s delight in every line—hip hop, fine! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
vscode/rootfs/etc/s6-overlay/s6-rc.d/init-user/run (1)

34-34: Consider additional security checks for .ssh directory.

While converting the error to a warning helps prevent container crashes, the .ssh directory requires strict permissions for security. Consider adding permission verification after linking.

-ln -sn "${SSH_USER_PATH}" ~/.ssh || bashio::log.warning "Failed linking .ssh"
+ln -sn "${SSH_USER_PATH}" ~/.ssh || bashio::log.warning "Failed linking .ssh"
+if [[ -L ~/.ssh ]] && [[ -d ~/.ssh ]]; then
+    [[ "$(stat -c %a ~/.ssh)" == "700" ]] || {
+        chmod 700 ~/.ssh
+        bashio::log.warning "Fixed .ssh directory permissions"
+    }
+fi
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3faac46 and 70ade45.

📒 Files selected for processing (1)
  • vscode/rootfs/etc/s6-overlay/s6-rc.d/init-user/run (3 hunks)
🔇 Additional comments (3)
vscode/rootfs/etc/s6-overlay/s6-rc.d/init-user/run (3)

14-17: LGTM! Good use of --no-dereference flag.

The addition of -n flag prevents the creation of deep links when the target is already a symbolic link, which is a good practice for container deployments.


20-23: LGTM! Consistent use of --no-dereference flag.

The changes maintain consistency with the new linking approach while preserving backward compatibility.


44-45: LGTM! Appropriate use of force flag for ZSH history.

The use of -f flag and strict error handling is appropriate for the ZSH history file as it's critical for shell functionality.

@@ -58,7 +58,7 @@ if ! bashio::fs.file_exists "${GIT_USER_PATH}/.gitconfig"; then
touch "${GIT_USER_PATH}/.gitconfig" \
|| bashio::exit.nok 'Failed to create .gitconfig'
fi
ln -s "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig
ln -s "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig || bashio::log.warning "Failed linking .gitconfig"
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add missing --no-dereference flag for consistency.

The -n flag is missing in this ln command, making it inconsistent with other link commands in the script.

-ln -s "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig || bashio::log.warning "Failed linking .gitconfig"
+ln -sn "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig || bashio::log.warning "Failed linking .gitconfig"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ln -s "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig || bashio::log.warning "Failed linking .gitconfig"
ln -sn "${GIT_USER_PATH}/.gitconfig" ~/.gitconfig || bashio::log.warning "Failed linking .gitconfig"

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.

1 participant