{"record":{"id":"b3b14a28e2663073","repo":"hashicorp/nomad","slug":"error-encoding-basic-auth-v","errorCode":null,"errorMessage":"error encoding basic auth: %v","messagePattern":"error encoding basic auth: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/docker/utils.go","lineNumber":241,"sourceCode":"\t\t\treturn nil, err\n\t\t}\n\n\t\tif authIsEmpty(auth) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn auth, nil\n\t}\n}\n\n// some docker api calls require a base64 encoded basic auth string\nfunc encodeAuth(cfg *registrytypes.AuthConfig) error {\n\tauth := &registrytypes.AuthConfig{\n\t\tUsername: cfg.Username,\n\t\tPassword: cfg.Password,\n\t}\n\tencodedJSON, err := json.Marshal(auth)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error encoding basic auth: %v\", err)\n\t}\n\n\tcfg.Auth = base64.URLEncoding.EncodeToString(encodedJSON)\n\treturn nil\n}\n\n// authIsEmpty returns if auth is nil or an empty structure\nfunc authIsEmpty(auth *registrytypes.AuthConfig) bool {\n\tif auth == nil {\n\t\treturn false\n\t}\n\treturn auth.Username == \"\" &&\n\t\tauth.Password == \"\" &&\n\t\tauth.ServerAddress == \"\"\n}\n\nfunc validateCgroupPermission(s string) bool {\n\tfor _, c := range s {","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/docker/utils.go#L223-L259","documentation":"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).","triggerScenarios":"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.","commonSituations":"Rarely hit in the field; seen mostly when library internals are modified or when reflection-built AuthConfig values contain unsupported fields.","solutions":["Inspect the wrapped %v error; confirm the AuthConfig only holds string fields.","Ensure cfg passed to encodeAuth is a properly initialized *AuthConfig/credential struct, not a custom type.","If you patched the code, revert to marshalling a plain registrytypes.AuthConfig."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"if cfg == nil || cfg.Username == \"\" || cfg.Password == \"\" {\n  return fmt.Errorf(\"incomplete auth config before encode\")\n}","typeGuard":"func encodeableAuth(a *registrytypes.AuthConfig) bool { return a != nil }","tryCatchPattern":"if err := encodeAuth(cfg); err != nil {\n  return fmt.Errorf(\"cannot build registry auth: %w\", err)\n}","preventionTips":["Only pass plain registrytypes.AuthConfig values to encodeAuth.","Avoid custom field types in patched auth structs.","Unit-test encodeAuth once per library version upgrade."],"tags":["docker","json","serialization","registry-auth"],"backgroundTag":"json-marshal-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}