Authentication
This document describes the authentication system in pycontainer-build.
Overview
Section titled “Overview”pycontainer-build supports multiple authentication methods for pushing images to container registries. The authentication system is designed to be:
- Flexible: Multiple auth providers with automatic fallback
- Secure: No credentials stored in code, supports environment variables and external tools
- Convenient: Auto-discovers credentials from Docker, Azure CLI, environment
Authentication Flow
Section titled “Authentication Flow”Push Request │ ├─> Check CLI flags (--username, --password) │ ├─> Username + password: Use Basic Auth │ └─> Password only: Use it as a bearer token │ ├─> Check Environment Variables │ ├─> GITHUB_TOKEN (only for ghcr.io) │ ├─> REGISTRY_TOKEN (generic bearer token) │ └─> REGISTRY_USERNAME + REGISTRY_PASSWORD │ ├─> Check Docker Config (~/.docker/config.json) │ ├─> Look for registry key │ ├─> Decode base64 auth │ └─> Extract username/password │ ├─> Check Azure CLI (only for an ACR login server) │ └─> Run: az acr login --expose-token │ └─> Make Request ├─> If 401 + Www-Authenticate header │ ├─> Parse OAuth2 challenge │ │ ├─> Exchange Basic credentials or bearer token for a registry token │ └─> Retry with bearer token └─> SuccessComponents
Section titled “Components”1. Auth Providers (src/pycontainer/auth.py)
Section titled “1. Auth Providers (src/pycontainer/auth.py)”AuthProvider (ABC)
Section titled “AuthProvider (ABC)”Base interface for authentication providers.
class AuthProvider(ABC): def get_credentials(self, registry: str) -> Optional[Tuple[str, str]]: """Return (username, password/token) or None"""
def get_token(self, registry: str) -> Optional[str]: """Return bearer token or None"""EnvironmentAuthProvider
Section titled “EnvironmentAuthProvider”Reads credentials from environment variables:
REGISTRY_USERNAME+REGISTRY_PASSWORD(Basic Auth)GITHUB_TOKEN(only for theghcr.ioregistry)REGISTRY_TOKEN(generic)
The generic REGISTRY_* variables are not scoped to a registry. Pass
include_generic_environment=False to EnvironmentAuthProvider,
get_default_auth_provider or get_registry_auth to ignore them while keeping
GITHUB_TOKEN for ghcr.io; base-image pulls do this.
Registry host scoping
Section titled “Registry host scoping”Provider-specific credentials are selected only when the registry is a bare
host authority, optionally with a numeric HTTPS port of 443 (including
zero-padded forms such as :0443). Hostnames are compared case-insensitively
and must consist of valid DNS labels; schemes, paths, user information, trailing
dots, other ports and other malformed authorities never match.
GITHUB_TOKENis used only for exactlyghcr.io. Hosts such asghcr.io.example.comorevilghcr.iodo not receive it.- Azure CLI runs only for an ACR login server named
<name>.azurecr.ioor<name>-<suffix>.azurecr.io. The registry resource name is 5-50 ASCII letters or digits, and the optional suffix accommodates domain-name-label protected registries. The provider passes the resource name toaz acr login --nameand accepts the token only if the returnedloginServermatches the requested host.
Explicit CLI credentials and REGISTRY_USERNAME + REGISTRY_PASSWORD remain
host-agnostic, subject to provider precedence. REGISTRY_TOKEN is available
for non-GHCR registries; for GHCR, the environment provider uses GITHUB_TOKEN
instead, even when it is unset. Docker config credentials use registry keys.
DockerConfigAuthProvider
Section titled “DockerConfigAuthProvider”Reads ~/.docker/config.json:
- Parses
authssection - Decodes base64 auth strings
- Handles both encoded and plain credentials
- Tries multiple registry key formats
- Returns decoded username/password as Basic credentials, never as a bearer token
AzureCLIAuthProvider
Section titled “AzureCLIAuthProvider”For Azure Container Registry login servers (optionally with HTTPS port 443):
- Runs
az acr login --expose-token - Returns the exposed token only when the login server matches
- Only activates for ACR registries
ChainAuthProvider
Section titled “ChainAuthProvider”Chains multiple providers, tries each in sequence.
2. Registry Client (src/pycontainer/registry_client.py)
Section titled “2. Registry Client (src/pycontainer/registry_client.py)”OAuth2 Token Exchange
Section titled “OAuth2 Token Exchange”When the registry returns 401 Unauthorized with Www-Authenticate header:
Www-Authenticate: Bearer realm="https://auth.registry.io/token", service="registry.io", scope="repository:user/app:pull,push"The client:
- Parses the challenge (realm, service, scope)
- Makes token request to
realmwith query params - Includes credentials via
Authorizationheader (Basic or Bearer) - Extracts token from response
- Retries original request with bearer token
Bearer-token exchanges, authenticated request replays and blob redirects run
after leaving the original HTTPError handler, so subsequent failures do not
retain its potentially credential-bearing reason in their exception chain.
Token responses that are not valid UTF-8 JSON raise an authentication error.
Upload replays restore the body’s original stream offset before retrying;
rewind failures propagate outside that handler too.
Registry control requests retain their credentials on same-origin redirects, but not when the scheme, host, or effective port changes. Token-exchange and blob-download redirects are fetched without credentials, even on the same origin.
Auth Methods
Section titled “Auth Methods”- Bearer Token:
Authorization: Bearer <token> - Basic Auth:
Authorization: Basic <base64(username:password)>
Both methods supported, auto-selected based on available credentials.
3. Builder Integration (src/pycontainer/builder.py)
Section titled “3. Builder Integration (src/pycontainer/builder.py)”The push() method:
- Resolves explicit CLI arguments or the environment, Docker config and Azure CLI providers
- Preserves bearer tokens and username/password as distinct credential types
- Passes credentials to
RegistryClient, which handles the OAuth2 challenge flow
Registry-Specific Behavior
Section titled “Registry-Specific Behavior”GitHub Container Registry (ghcr.io)
Section titled “GitHub Container Registry (ghcr.io)”- Prefers
GITHUB_TOKENenvironment variable - Username can be anything (often just “USERNAME”)
- Token must have
write:packagesscope - Supports OAuth2 token exchange
Docker Hub (docker.io)
Section titled “Docker Hub (docker.io)”- Uses Docker config by default
- Supports username/password or token
- Token created at https://hub.docker.com/settings/security
Azure Container Registry (<name>.azurecr.io)
Section titled “Azure Container Registry (<name>.azurecr.io)”- Auto-detects via
az acr login - Supports service principal credentials
- Supports admin username/password
- Token obtained via
--expose-tokenflag
Private/Generic Registries
Section titled “Private/Generic Registries”- Falls back to Docker config
- Uses environment variables
- Supports Basic Auth and OAuth2
Usage Examples
Section titled “Usage Examples”GitHub PAT
Section titled “GitHub PAT”export GITHUB_TOKEN="ghp_xxxxx"pycontainer build --tag ghcr.io/user/app:v1 --pushDocker Login
Section titled “Docker Login”docker login ghcr.iopycontainer build --tag ghcr.io/user/app:v1 --pushAzure CLI
Section titled “Azure CLI”az loginpycontainer build --tag myregistry.azurecr.io/app:v1 --pushExplicit Credentials
Section titled “Explicit Credentials”pycontainer build \ --tag registry.example.com/app:v1 \ --username myuser \ --password mypassword \ --pushThe username and password are sent using Basic authentication. A password/token
without --username is treated as a bearer token. Supplying --username
without either --password or REGISTRY_PASSWORD is an error.
Environment Variables
Section titled “Environment Variables”export REGISTRY_USERNAME="myuser"export REGISTRY_PASSWORD="mypass"pycontainer build --tag registry.example.com/app:v1 --pushPrivate Base Images
Section titled “Private Base Images”Base-image pulls use only registry-scoped credentials. Generic
REGISTRY_TOKEN, REGISTRY_USERNAME and REGISTRY_PASSWORD values, and the
--username/--password flags, are reserved for the destination push and are
never sent to the base registry or its token endpoint. This applies to every
target in a multi-platform image-index build. Authenticate a private
base independently of the destination with an inline auths entry for the base
registry host in ~/.docker/config.json (credential helpers and credsStore
are not read). The host-scoped GITHUB_TOKEN (ghcr.io) and Azure CLI
(*.azurecr.io) providers remain enabled for base pulls, with the same behavior
as for pushes.
# ~/.docker/config.json contains an inline auths entry for base.example.comexport REGISTRY_USERNAME="push-user"export REGISTRY_PASSWORD="push-password"pycontainer build \ --base-image base.example.com/team/python:3.11 \ --tag registry.example.com/app:v1 \ --pushPrivate Package Indexes
Section titled “Private Package Indexes”With --include-deps and a requirements file, the builder runs the host
interpreter’s pip to download target wheels. Index credentials are read by
pip on the build host only; they are not passed through pycontainer
configuration. This section covers that path; registry authentication above is
separate.
Supported credential sources
Section titled “Supported credential sources”| Source | How to use it |
|---|---|
| Environment | PIP_INDEX_URL / PIP_EXTRA_INDEX_URL containing https://user:token@host/simple |
| pip config file | index-url / extra-index-url in a pip config file outside the build context, or selected with PIP_CONFIG_FILE |
| netrc | ~/.netrc (or the file named by NETRC) with a machine <host> entry; the index URL carries no credentials |
| keyring | PIP_KEYRING_PROVIDER=import (keyring importable by the builder’s Python) or subprocess (a keyring executable on PATH; put the username in the URL, e.g. https://user@host/simple). pip runs with --no-input, so the default auto provider does not query keyring |
| Placeholders | --index-url https://${INDEX_USER}:${INDEX_TOKEN}@host/simple in the requirements file; pip expands ${NAME} (uppercase letters, digits, _) from the builder’s environment |
What the builder enforces
Section titled “What the builder enforces”- Literal credentials are rejected. Every file in the requirements graph
(
-rand-cincludes, comments included) is scanned before pip runs. URLs with a password, a bare username or token (other thangit@), or a query parameter named liketoken,key,secret,password,auth,sigorcredentialfail the build unless the value is only${VAR}placeholders. URL path segments are also checked: known token formats (GitHubghp_/github_pat_, GitLabglpat-, PyPIpypi-AgE…, Hugging Facehf_), the first path segment ondl.cloudsmith.ioother thanpublicorbasic, and, except on public source hosts such asgithub.comandpypi.org, a whole path segment of 20+ alphanumerics that mixes upper case, lower case and digits with frequent changes between them. Userinfo or path segments made only of${VAR}placeholders, or a literal user with a${VAR}password, are allowed; pip expands placeholders anywhere on a requirements line, sohttps://dl.cloudsmith.io/${CLOUDSMITH_TOKEN}/org/repo/python/simple/works. The error names the file, line and URL scheme and host only. - Shipped pip inputs are checked even without
--include-deps. Before the application layer is created (and before any cache lookup), includedrequirements*.txt|.in,constraints*.txt|.in,pip.conf,pip.iniand requirements-graph files are scanned the same way, and.netrc/_netrcfiles are rejected. A hostPIP_CONFIG_FILEorNETRCthat points inside the context is treated the same way, whatever its file name. Files over 1 MiB are rejected rather than partially scanned. Exclude them from the context or use placeholders. - Host pip settings cannot change target selection. Only these
PIP_*variables, and the matching pip config-file keys, reach pip:PIP_CERT,PIP_CLIENT_CERT,PIP_CONFIG_FILE,PIP_DEFAULT_TIMEOUT,PIP_EXTRA_INDEX_URL,PIP_FIND_LINKS,PIP_INDEX_URL,PIP_KEYRING_PROVIDER,PIP_NO_INDEX,PIP_PROXY,PIP_REQUIRE_HASHES,PIP_RETRIES,PIP_TIMEOUTandPIP_TRUSTED_HOST. Others, such asPIP_PLATFORM,PIP_ONLY_BINARY,PIP_NO_BINARY,PIP_TARGET,PIP_PREFIXorPIP_USER, are ignored; their names (never values) are printed. pipappendoptions combine config and command-line values, so command-line flags alone would not pin the target. A self-test refuses to install if pip’s configuration hook changes. StandardHTTP(S)_PROXY,NO_PROXY,REQUESTS_CA_BUNDLEandSSL_CERT_FILEvariables still apply. pip runs withpython -E, soPYTHONPATHandPYTHONHOMEcannot replace the pinned pip. - Only installed packages ship. The dependency layer contains the staged
site-packagesand generated console scripts. pip’s stderr and debug log are kept in host temporary files outside the staged tree. Installeddirect_url.jsonmetadata with URL credentials fails pip-staged builds; virtualenv staging rejects alldirect_url.jsonmetadata. - Errors are classified and redacted. HTTP 401/403 failures name the
supported credential sources, HTTP 407 points at proxy credentials, and
connection or TLS failures say so. Excerpts are limited to a few pip lines
with URL userinfo, secret-like query values, detected URL path tokens, known
environment URL credentials and expanded placeholder values replaced by
****.
Limits
Section titled “Limits”- Detection is pattern-based and best effort. The random-looking-segment check
misses a share of genuinely random tokens (in a synthetic sample, roughly
16% of 20-character and 7% of 40-character alphanumeric tokens), and it
ignores short, word-like, single-case or all-hex values. Path tokens in
positions without a host rule (for example Gemfury’s
pypi.fury.io/<token>/<user>/) and credentials in other source files (for example application config) may not be detected. Use${VAR}placeholders for any URL path secret and keep secrets out of the build context. - Redaction covers values the builder can see: URL userinfo, secret-like query
parameters, detected URL path tokens,
PIP_*/proxy URL credentials and placeholder values. Values pip reads from config files, netrc or keyring are redacted only where they appear in those URL positions; pip itself also masks passwords in URLs it logs. - pip, keyring backends and the index are trusted. A malicious index or package is out of scope.
- The tests use a loopback basic-auth index, a loopback index authenticated by
a URL path token, and synthetic secrets (
tests/test_private_index.py); they verify that those secrets do not appear in the OCI layout blobs (decompressed), SBOM, layer cache, standard output or errors. A second loopback origin on a different hostname verifies that index credentials (environment userinfo, netrc and placeholders) are not sent to an artifact host that the index links or redirects to. Credentials configured for that artifact host itself would still be sent. The tests do not cover every index product or authentication scheme.
Security Considerations
Section titled “Security Considerations”Credential Storage
Section titled “Credential Storage”- Environment variables: Visible in process list
- Docker config: Stored unencrypted (or via credential helper)
- Azure CLI: Token cached by Azure CLI
- CLI flags: Visible in shell history
- Credentials are not written to application logs. Avoid passing secrets on command lines in shared or recorded shells.
Recommendation: Use Docker config with credential helpers or Azure CLI for best security.
Transport Security
Section titled “Transport Security”- Registry traffic is HTTPS with certificate verification by default.
--registry-ca-fileadds a trusted CA; it never disables verification. --insecure-registry HOST[:PORT]allows plain HTTP for that exact host. Credentials are sent over HTTP only to loopback hosts; credentials for any other HTTP registry, or a token challenge from one, fail withRegistryAuthenticationErrorbefore secrets leave the process.- HTTPS-to-HTTP redirects and HTTP token realms from HTTPS registries are
rejected.
Authorization,Proxy-AuthorizationandCookieare dropped on cross-origin redirects; blob storage redirects are credential-free. - Embedded URL credentials are rejected and error URLs omit userinfo/query secrets. Registry connections ignore environment/system proxies.
- Concurrent initial bearer challenges share token acquisition and replay once; rejection of the current token is reported without indefinite refresh. See REGISTRY_PUSH.md.
- Base-image mirrors (
--registry-mirror) resolve credentials withget_mirror_auth, which uses only host-scoped sources (the mirror’s own Docker config entry, Azure CLI for*.azurecr.io,GITHUB_TOKENforghcr.io). GenericREGISTRY_*variables, explicit flags, and upstream credentials are never sent to a mirror. See REGISTRY_PUSH.md.
Token Scopes
Section titled “Token Scopes”- GitHub: Require minimum scope (
write:packages) - Docker Hub: Use access tokens, not account password
- ACR: Use service principals with least privilege
For CI/CD pipelines:
- Use secrets management (GitHub Secrets, Azure Key Vault)
- Set as environment variables in pipeline
- Avoid logging credentials
- Use short-lived tokens when possible
Testing
Section titled “Testing”Unit Tests (tests/test_auth.py)
Section titled “Unit Tests (tests/test_auth.py)”- Mock environment variables
- Create temporary Docker configs
- Test provider chain behavior
- Verify base64 decoding
Integration Tests
Section titled “Integration Tests”To test live authentication (manual):
# Test GitHubexport GITHUB_TOKEN="ghp_xxxxx"pycontainer build --tag ghcr.io/test/myapp:test --push
# Test Docker Hubdocker loginpycontainer build --tag username/myapp:test --push
# Test ACRaz loginpycontainer build --tag myregistry.azurecr.io/myapp:test --pushTroubleshooting
Section titled “Troubleshooting”“401 Unauthorized”
Section titled ““401 Unauthorized””- Check token has correct permissions
- Verify token is for correct registry
- Try
docker loginto verify credentials work
“No Location header”
Section titled ““No Location header””- Registry may not support monolithic uploads
- Check registry version (requires v2 API)
Azure CLI Timeout
Section titled “Azure CLI Timeout”- Run
az acr login --name myregistrymanually first - Check Azure subscription is active
- Verify registry name is correct
Docker Config Not Found
Section titled “Docker Config Not Found”- Check
~/.docker/config.jsonexists - Run
docker loginto create it - Verify registry key matches (try variations)
Future Enhancements
Section titled “Future Enhancements”Phase 1.4+
Section titled “Phase 1.4+”- Credential refresh for long-running operations
- Support for Docker credential helpers
- Token caching to avoid repeated OAuth2 exchanges
- GitHub App JWT authentication
- Workload identity for cloud environments