hashicorp/nomad · error

error encoding basic auth: %v

Error message

error encoding basic auth: %v

What it means

encodeAuth marshals a registrytypes.AuthConfig (username/password) to JSON before base64-encoding it into cfg.Auth; this error surfaces only if json.Marshal fails. In practice the struct is JSON-serializable, so this indicates a programmatic misuse (e.g. calling encodeAuth on a nil/unsupported value or a struct carrying channels/funcs).

Source

Thrown at drivers/docker/utils.go:241

			return nil, err
		}

		if authIsEmpty(auth) {
			return nil, nil
		}
		return auth, nil
	}
}

// some docker api calls require a base64 encoded basic auth string
func encodeAuth(cfg *registrytypes.AuthConfig) error {
	auth := &registrytypes.AuthConfig{
		Username: cfg.Username,
		Password: cfg.Password,
	}
	encodedJSON, err := json.Marshal(auth)
	if err != nil {
		return fmt.Errorf("error encoding basic auth: %v", err)
	}

	cfg.Auth = base64.URLEncoding.EncodeToString(encodedJSON)
	return nil
}

// authIsEmpty returns if auth is nil or an empty structure
func authIsEmpty(auth *registrytypes.AuthConfig) bool {
	if auth == nil {
		return false
	}
	return auth.Username == "" &&
		auth.Password == "" &&
		auth.ServerAddress == ""
}

func validateCgroupPermission(s string) bool {
	for _, c := range s {

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Inspect the wrapped %v error; confirm the AuthConfig only holds string fields.
  2. Ensure cfg passed to encodeAuth is a properly initialized *AuthConfig/credential struct, not a custom type.
  3. If you patched the code, revert to marshalling a plain registrytypes.AuthConfig.
Defensive patterns

Strategy: type-guard

Validate before calling

if cfg == nil || cfg.Username == "" || cfg.Password == "" {
  return fmt.Errorf("incomplete auth config before encode")
}

Type guard

func encodeableAuth(a *registrytypes.AuthConfig) bool { return a != nil }

Try / catch

if err := encodeAuth(cfg); err != nil {
  return fmt.Errorf("cannot build registry auth: %w", err)
}

Prevention

When it happens

Trigger: json.Marshal(&registrytypes.AuthConfig{...}) returning an error — practically only when auth configuration is built with unsupported types or the struct definition changes to non-serializable fields.

Common situations: Rarely hit in the field; seen mostly when library internals are modified or when reflection-built AuthConfig values contain unsupported fields.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/b3b14a28e2663073. Report an issue: GitHub.