Skip to content

cli/registry/login: Add the --password-env flag #5972

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

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

Conversation

octo
Copy link

@octo octo commented Mar 31, 2025

- What I did

Adds support for providing registry passwords via environment variables, which is particularly useful in CI pipelines. For example, GitLab stores registry passwords in the CI_REGISTRY_PASSWORD environment variable.

With this change, authenticating to a registry is as simple as:

docker login --username "${CI_REGISTRY_USER}" --password-env "CI_REGISTRY_PASSWORD" "${CI_REGISTRY}"

Alternatives Considered

  • Using docker login -p "${VAR}" with warning suppression

    • Shell history concerns don't apply with variable substitution
    • Tokens are often short-lived, reducing security concerns
    • Could introduce a way to suppress warnings via environment variables
  • Passing passwords via STDIN (current recommended method):

    sh echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"

    Avoids warnings but adds complexity to command chains

- How I did it

  • Expanded the loginOptions struct, adding passwordEnv string.
  • Refactored verifyLoginOptions to use a switch statement instead of if blocks to ensure only one block is evaluated.

- How to verify it

  1. Verify the following command fails with a "variable DOCKER_TOKEN not defined" error:

    docker login -u "${DOCKER_USERNAME:?}" --password-env DOCKER_TOKEN "${DOCKER_HOST:?}"
  2. Store login credentials in the DOCKER_TOKEN environment variable:

    DOCKER_TOKEN="f00bar"
  3. Verify the command from step 1 now succeeds.

- Human readable description for the release notes

`docker login`: The new `--password-env` flag allows reading login credentials from an environment variable. 

- A picture of a cute animal (not mandatory but encouraged)

grafik

AI generated picutre of a Tanuki

Closes: #5971

Adds support for providing registry passwords via environment variables, which
is particularly useful in CI pipelines. For example, GitLab stores registry
passwords in the `CI_REGISTRY_PASSWORD` environment variable.

With this change, authenticating to a registry is as simple as:

```sh
docker login --username "${CI_REGISTRY_USER}" --password-env "CI_REGISTRY_PASSWORD" "${CI_REGISTRY}"
```

## Alternatives Considered

* Using `docker login -p "${VAR}"` with warning suppression
  * Shell history concerns don't apply with variable substitution
  * Tokens are often short-lived, reducing security concerns
  * Could introduce a way to suppress warnings via environment variables
* Passing passwords via STDIN (current recommended method):

  ```sh
  echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"
  ```

  Avoids warnings but adds complexity to command chains

Signed-off-by: Florian Forster <fforster@gitlab.com>
@octo octo requested review from thaJeztah and a team as code owners March 31, 2025 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant