Skip to content

Nil pointer dereference in pullImage when using --private-registry without credentials #132

Description

@ddjain

krknctl run panics with a nil pointer dereference when --private-registry and --private-registry-scenarios are provided without authentication flags (--private-registry-username, --private-registry-token). This makes it impossible to use a public custom registry.

Steps to Reproduce

krknctl run network-chaos \
    --private-registry quay.io \
    --private-registry-scenarios rh-ee-darjain/krkn-chaos \
    --traffic-type egress \
    --kubeconfig ~/.kube/config

Expected Behavior

The image should be pulled without authentication (the repository is public) and the scenario should execute normally.

Actual Behavior

[🔐 Private Registry] quay.io

container runtime: Docker
🌍pulling scenario image...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10099cca0]

goroutine 1 [running]:
github.com/krkn-chaos/krknctl/pkg/scenarioorchestrator/docker.pullImage(...)
    pkg/scenarioorchestrator/docker/scenario_orchestrator.go:246

Root Cause

In pkg/scenarioorchestrator/docker/scenario_orchestrator.go, pullImage calls registry.ToDockerV2AuthString() when registry is non-nil. However, ToDockerV2AuthString() returns (nil, nil) when neither Token nor Username is set (i.e., no credentials were provided). The return value is dereferenced without a nil check:

// scenario_orchestrator.go:240-247
pullOptions := dockerimage.PullOptions{}
if registry != nil {
    registryAuth, err := registry.ToDockerV2AuthString()
    if err != nil {
        return err
    }
    pullOptions.RegistryAuth = *registryAuth // panic: registryAuth is nil
}

ToDockerV2AuthString() in pkg/provider/models/models.go:104-124:

func (r *RegistryV2) ToDockerV2AuthString() (*string, error) {
    authConfig := registry.AuthConfig{}
    if r.Token != nil {
        authConfig.RegistryToken = *r.Token
    } else {
        if r.Username != nil {
            authConfig.Username = *r.Username
            authConfig.Password = *r.Password
        } else {
            return nil, nil // no credentials → nil return
        }
    }
    // ...
}

Fix

Add a nil check on registryAuth before dereferencing:

if registry != nil {
    registryAuth, err := registry.ToDockerV2AuthString()
    if err != nil {
        return err
    }
    if registryAuth != nil {
        pullOptions.RegistryAuth = *registryAuth
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions