Skip to content

fix: write Erlang cookie to disk for CLI tool authentication - #59

Merged
paudley merged 1 commit into
mainfrom
paudley/rabbitmq-validation
Mar 2, 2026
Merged

fix: write Erlang cookie to disk for CLI tool authentication#59
paudley merged 1 commit into
mainfrom
paudley/rabbitmq-validation

Conversation

@paudley

@paudley paudley commented Mar 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • Writes the Erlang cookie to $RABBITMQ_MNESIA_DIR/.erlang.cookie during container startup so that CLI tools (rabbitmqctl, rabbitmq-diagnostics) invoked via docker compose exec can authenticate with the broker
  • Handles pre-existing read-only (0400) cookie files from previous container runs by widening permissions before overwriting
  • Locks the cookie file back to 0400 after writing for security

Context

The RabbitMQ entrypoint exports RABBITMQ_ERLANG_COOKIE as an environment variable, but this is only visible to PID 1. When CLI tools are invoked via docker compose exec, they spawn a new shell that reads the cookie from ~/.erlang.cookie on disk. Without this fix, CLI tools fail with "Invalid challenge reply" errors.

Test plan

  • Container starts healthy with all 12 plugins enabled
  • rabbitmq-overview succeeds (CLI tool authentication works)
  • rabbitmq-plugins -e lists all enabled plugins
  • rabbitmq-export exports definitions via management API
  • Port connectivity check passes for all 5 listeners (AMQP 5672, Management 15672, Stream 5552, Prometheus 15692, Clustering 25672)
  • Consistent hash exchange routes messages correctly (3/3 distribution across 2 queues)
  • Stream queue creation and publishing works
  • Production config values applied (512MiB memory watermark, channel_max=128, etc.)
  • No local alarms reported

Copilot AI review requested due to automatic review settings March 2, 2026 22:50
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an authentication issue for RabbitMQ CLI tools when used within a Docker Compose environment. By ensuring the Erlang cookie is properly persisted to disk with appropriate permissions, it allows docker compose exec commands to successfully authenticate with the running RabbitMQ broker, improving the usability and debugging capabilities of the containerized setup.

Highlights

  • Erlang Cookie Persistence: The Erlang cookie is now explicitly written to $RABBITMQ_MNESIA_DIR/.erlang.cookie during container startup to enable CLI tools like rabbitmqctl and rabbitmq-diagnostics to authenticate with the broker when invoked via docker compose exec.
  • Robust File Handling: The script now gracefully handles pre-existing read-only cookie files by temporarily widening permissions to 0600 before overwriting, preventing errors from previous container runs.
  • Security Enhancement: After writing the cookie to disk, the file permissions are immediately reset to 0400 (read-only for the owner) to maintain security.
Changelog
  • rabbitmq/entrypoint.sh
    • Added logic to write the Erlang cookie to a file on disk.
    • Implemented permission handling for the cookie file, including widening permissions before writing and locking them down afterwards.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses an authentication issue with RabbitMQ CLI tools in a Docker environment by writing the Erlang cookie to disk. The overall logic is sound. I have one suggestion to improve the robustness of the entrypoint script by making its error handling more explicit, which will aid in debugging potential file permission issues.

Comment thread rabbitmq/entrypoint.sh
# widen permissions before overwriting, then lock back down.
cookie_file="${RABBITMQ_MNESIA_DIR:-/var/lib/rabbitmq}/.erlang.cookie"
if [ -f "${cookie_file}" ]; then
chmod 600 "${cookie_file}" 2>/dev/null || true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Suppressing errors from chmod with 2>/dev/null || true can hide underlying problems, such as incorrect file ownership. This makes debugging more difficult if the script fails later at the printf command. Since the if [ -f ... ] guard on the preceding line already prevents chmod from failing on a non-existent file, the error suppression is not necessary and it's safer to let the script fail immediately if chmod encounters an unexpected error.

Suggested change
chmod 600 "${cookie_file}" 2>/dev/null || true
chmod 600 "${cookie_file}"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Ensures RabbitMQ CLI tools executed via docker compose exec can authenticate by persisting the Erlang cookie to disk during container startup (instead of relying on an env var only visible to PID 1).

Changes:

  • Writes RABBITMQ_ERLANG_COOKIE to an on-disk .erlang.cookie file at startup.
  • Temporarily relaxes permissions on an existing cookie to allow overwrite, then locks it back down to 0400.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rabbitmq/entrypoint.sh
Comment on lines +56 to +61
if [ -f "${cookie_file}" ]; then
chmod 600 "${cookie_file}" 2>/dev/null || true
fi
printf '%s' "${cookie}" > "${cookie_file}"
chmod 400 "${cookie_file}"

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

If the cookie file already exists and you chmod it to 0600, then a subsequent failure while writing (disk full, permission issue, etc.) will cause the script to exit (set -e) and leave the cookie less restricted than intended. Consider writing via a temporary file + atomic rename and/or using a trap to always restore permissions to 0400 on exit.

Suggested change
if [ -f "${cookie_file}" ]; then
chmod 600 "${cookie_file}" 2>/dev/null || true
fi
printf '%s' "${cookie}" > "${cookie_file}"
chmod 400 "${cookie_file}"
tmp_cookie_file="${cookie_file}.tmp.$$"
printf '%s' "${cookie}" > "${tmp_cookie_file}"
chmod 400 "${tmp_cookie_file}"
mv -f "${tmp_cookie_file}" "${cookie_file}"

Copilot uses AI. Check for mistakes.
Comment thread rabbitmq/entrypoint.sh
# The env var is only visible to PID 1; exec'd shells read the file.
# The file may already exist read-only (0400) from a previous run, so
# widen permissions before overwriting, then lock back down.
cookie_file="${RABBITMQ_MNESIA_DIR:-/var/lib/rabbitmq}/.erlang.cookie"

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

Cookie location: Erlang CLI tools generally read the cookie from the invoking user’s home directory ("$HOME/.erlang.cookie"). Using RABBITMQ_MNESIA_DIR here can write the cookie somewhere else if that env var is set, which would still break docker compose exec rabbitmqctl .... Consider basing the path on $HOME (or explicitly /var/lib/rabbitmq) instead of the mnesia dir.

Suggested change
cookie_file="${RABBITMQ_MNESIA_DIR:-/var/lib/rabbitmq}/.erlang.cookie"
cookie_file="${HOME:-/var/lib/rabbitmq}/.erlang.cookie"

Copilot uses AI. Check for mistakes.
Comment thread rabbitmq/entrypoint.sh
Comment on lines +58 to +60
fi
printf '%s' "${cookie}" > "${cookie_file}"
chmod 400 "${cookie_file}"

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

The cookie file is created with whatever the current umask allows, and only tightened afterward. That can briefly create a world-readable cookie (e.g., umask 022 -> 0644) during startup. Consider setting a restrictive umask (as done in other entrypoints in this repo) before writing the cookie so it’s never created with broader perms.

Suggested change
fi
printf '%s' "${cookie}" > "${cookie_file}"
chmod 400 "${cookie_file}"
fi
old_umask=$(umask)
umask 077
printf '%s' "${cookie}" > "${cookie_file}"
chmod 400 "${cookie_file}"
umask "${old_umask}"

Copilot uses AI. Check for mistakes.
@paudley
paudley merged commit 93258e4 into main Mar 2, 2026
11 of 12 checks passed
@paudley
paudley deleted the paudley/rabbitmq-validation branch March 2, 2026 23:25
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.

2 participants