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

feat: add birdnet-user to docker runtime image #363

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

isZumpo
Copy link
Contributor

@isZumpo isZumpo commented Jan 2, 2025

Fixes #322

Changes:

  • Introduces a non-root user birdnet-user to the Docker image to enhance security and prevent potential vulnerabilities.
  • The entrypoint script utilizes gosu to downgrade to the less privileged birdnet-user (gid=10002, uid=10002), ensuring the container does not run as the root user.
  • Adjusts volume mount permissions for /data and /config to the new user during the downgrade process.
  • Supports running the container with an arbitrary user/group ID by passing --user into the docker run command. Safeguards are in place to ensure the specified user/group ID has the required permissions to the mounted volumes and belongs to the audio group.
  • Introduces the CONFIG_PATH environment variable to allow setting the configuration path. This is particularly useful when running inside a container, as the configuration might be mounted in a different path than expected when running on a real machine.

These changes should not leave any existing volumes in a bad state and should be easy for everyone to use. However, it would be good if someone else could try out these changes to be on the safe side before we merge anything.

Allows setting the config path with the CONFIG_PATH environment
variable. This is useful when running inside a container, since the
config might be mounted in a different path from what is normally
expected if running on a real machine.
By adding the "birdnet-user" for the Docker image, it ensures that the
image is not run as root user. This is considered a best practice
to enhance security and prevent potential vulnerabilities.

Downgrade to less privileged runtime user is done through the entrypoint
script by utilizing gosu: https://github.com/tianon/gosu. Meaning that
the container will be running as user gid=10002, uid=10002 which should
not collide with any existing users on the host system. During the
downgrade process, the volume mount permissions for /data and /config
will be adjusted to the new user.

If the entrypoint user is not privileged, by passing `--user` into the
docker run command, (arbitrary user/group id) the container will be run
as the specified user instead. Here some safe guards are in place to
ensure that provided user/group id has the required permissions to the
mounted volumes as well as belonging to the audio group.
Copy link
Contributor

coderabbitai bot commented Jan 2, 2025

Walkthrough

The pull request introduces enhanced Docker container security and configuration management for the BirdNET-Go application. The changes include creating a non-root user (birdnet-user), adding a custom entry point script, and implementing environment-based configuration path selection. The modifications aim to improve container runtime flexibility, user permissions, and directory access, allowing the application to run securely without root privileges.

Changes

File Change Summary
Dockerfile - Added gosu package
- Created birdnet-user with specific UID/GID
- Set CONFIG_PATH environment variable
- Updated entry point to custom script
- Changed default command to realtime
docker-entrypoint.sh - New script for container entry point
- Handles user switching and permissions
- Checks directory access
- Supports running as non-root user
internal/conf/utils.go - Enhanced GetDefaultConfigPaths to prioritize CONFIG_PATH environment variable

Sequence Diagram

sequenceDiagram
    participant Container as Docker Container
    participant Entrypoint as docker-entrypoint.sh
    participant App as BirdNET-Go App
    
    Container->>Entrypoint: Start container
    alt Running as root
        Entrypoint->>Entrypoint: Change directory ownership
        Entrypoint->>Entrypoint: Switch to birdnet-user
    end
    Entrypoint->>Entrypoint: Check directory permissions
    Entrypoint->>App: Launch application
Loading

Assessment against linked issues

Objective Addressed Explanation
Run without root privileges [#322]
Resolve permission issues for non-root users [#322]

Poem

🐰 A Docker tale of security's might,
Where birdnet-user dances light,
No more root's heavy-handed sway,
Permissions dance a nimble play!
Secure and swift, our container flies! 🚀


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5dd2d6e and 59680a3.

📒 Files selected for processing (3)
  • Dockerfile (3 hunks)
  • docker-entrypoint.sh (1 hunks)
  • internal/conf/utils.go (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
docker-entrypoint.sh

[warning] 12-1212: Expanding an array without an index only gives the first element.

(SC2128)


[warning] 19-1919: This flag is used as a command name. Bad line break or missing [ .. ]?

(SC2215)


[warning] 19-1919: This flag is used as a command name. Bad line break or missing [ .. ]?

(SC2215)


[warning] 24-2424: Expanding an array without an index only gives the first element.

(SC2128)


[error] 37-3737: Double quote array expansions to avoid re-splitting elements.

(SC2068)

🔇 Additional comments (10)
internal/conf/utils.go (1)

26-30: Leverage the CONFIG_PATH variable
Great approach to give priority to CONFIG_PATH when set. This provides users with explicit control over their configuration location. The fallback to system/user defaults ensures backward compatibility and a robust fail-safe.

docker-entrypoint.sh (4)

12-12: Shellcheck SC2128 is a false positive here
The shellcheck warning about expanding arrays without an index (SC2128) does not apply to $BASH_SOURCE or $@; these expansions are intended and properly quoted. No action needed.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 12-1212: Expanding an array without an index only gives the first element.

(SC2128)


17-20: Validate container audio group membership
It's good that you warn users if they aren't in the audio group. This ensures that audio-related functionality won't silently fail.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 19-1919: This flag is used as a command name. Bad line break or missing [ .. ]?

(SC2215)


[warning] 19-1919: This flag is used as a command name. Bad line break or missing [ .. ]?

(SC2215)


22-35: Graceful permission checks
Your checks for read/write access to /data and /config are clearly communicated and provide an immediate fail-fast mechanism, which is excellent for debugging file permission issues.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 24-2424: Expanding an array without an index only gives the first element.

(SC2128)


37-37: Secure command invocation
Double-quoting $@ helps avoid word splitting issues and is already correctly used. This addresses shellcheck SC2068.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 37-3737: Double quote array expansions to avoid re-splitting elements.

(SC2068)

Dockerfile (5)

61-61: Installing gosu
Installing gosu is essential for gracefully dropping privileges. Good for container security. Ensure that the version installed is kept updated to mitigate any security issues.


72-76: Non-root user creation
Defining a dedicated UID/GID and adding to the audio group is excellent. This keeps user privileges minimal while maintaining audio functionality.


77-79: Creating necessary directories
Preemptively creating /data and /config aligns with the new entrypoint script and ensures correct directory ownership.


81-81: Environment variable for config
Setting CONFIG_PATH to /config pairs well with GetDefaultConfigPaths—this ensures consistent referencing of configuration files.


91-93: Entrypoint reconfiguration
Switching to docker-entrypoint.sh centralizes user and volume setup logic while keeping the container’s CMD or arguments flexible. This is a maintainable design.


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.

@tphakala
Copy link
Owner

tphakala commented Jan 9, 2025

Sorry for delay, I will review this during weekend. This is needed but I am unsure how to handle this without breaking existing deployments on upgrade.

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.

Feature request - docker security
2 participants