configuration: allow callers to provide the environment used for overrides

Author: aguidirhCreated Sep 25, 2026Updated Sep 26, 2026

Description

Summary

Allow applications embedding distribution to explicitly provide the environment used by the configuration parser for overrides, instead of always inheriting the complete process environment through os.Environ().

Motivation

Environment-based configuration overrides are useful and should remain supported. However, the current parser constructor unconditionally snapshots the complete process environment. The public configuration.Parse(io.Reader) API constructs this parser internally, so embedding applications cannot filter or disable environment overrides.

This can cause namespace collisions when an embedding application uses a REGISTRY_* variable for a different purpose.

A concrete example is https://github.com/openshift/oc-mirror/issues/1453. oc-mirror uses REGISTRY_AUTH_FILE as a client authentication-file setting. When it starts an embedded distribution registry, the parser also interprets this variable as the configuration path Auth -> File. The scalar filesystem path cannot be decoded into the structured Auth parameters, so configuration parsing fails.

Callers currently have no supported way to say: "apply overrides from this selected environment only."

Proposed behavior

Add an opt-in API that allows callers to explicitly provide the complete environment used for configuration overrides.

For example:

go
    type ParseOption func(*Parser)

    // WithEnvironment sets the complete environment used for configuration
    // overrides. A provided empty slice disables environment overrides.
    func WithEnvironment(env []string) ParseOption

    // Parse retains the existing behavior and reads os.Environ().
    func Parse(rd io.Reader) (*Configuration, error)

    // ParseWithOptions permits callers to control parser inputs.
    func ParseWithOptions(rd io.Reader, opts ...ParseOption) (*Configuration, error)

Equivalent naming, or an option on NewParser, would also solve the problem. The important requirements are:

  • Existing callers retain the current os.Environ() behavior by default.
  • Callers can supply a filtered environment.
  • Callers can explicitly disable environment overrides with an empty environment.
  • The parser defensively copies caller-provided input.

Backward compatibility

This proposal does not change existing override semantics or the meaning of REGISTRY_* variables. Existing Parse and NewParser calls should continue to inherit the process environment.

The new behavior is opt-in for applications embedding the registry as a library.

Why this should be supported upstream

The issue is broader than one conflicting variable. An embedding application may legitimately use environment variables that share the REGISTRY_ prefix but are not intended as distribution server configuration.

A caller-controlled environment provides a general isolation boundary without adding application-specific exclusions to distribution.

Alternatives considered

Temporarily unset variables in the embedding process: this mutates global process state and can affect other goroutines.

  • Blacklist REGISTRY_AUTH_FILE in distribution: this addresses one collision but not future or application-specific REGISTRY_* variables.
  • Require embedding applications to implement their own parser: this duplicates configuration logic and makes compatibility harder.

Suggested tests

  1. Existing APIs continue to use os.Environ().
  2. An explicitly supplied environment is used instead of the process environment.
  3. A filtered environment does not apply omitted variables.
  4. An explicit empty environment disables overrides.
  5. Caller-provided environment data is defensively copied.

Source: distribution/distribution