{"record":{"id":"e5f868d375342aca","repo":"kubernetes/kops","slug":"failed-to-marshal-token-data-w-e5f868","errorCode":null,"errorMessage":"failed to marshal token data: %w","messagePattern":"failed to marshal token data: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/cloudup/gce/tpm/gcetpmsigner/tpmauthenticator.go","lineNumber":98,"sourceCode":"\t}\n\tdefer key.Close()\n\n\tklog.V(2).Infof(\"attestation key is %v\", debugToPEM(key.PublicKey()))\n\n\tklog.Infof(\"TPM initialization took %v\", time.Since(tpmStart))\n\n\tdata := gcetpm.AuthTokenData{\n\t\tGCPProjectID: a.projectID,\n\t\tZone:         a.zone,\n\t\tInstance:     a.instance,\n\t\tTimestamp:    time.Now().Unix(),\n\t\tAudience:     gcetpm.AudienceNodeAuthentication,\n\t\tRequestHash:  requestHash[:],\n\t}\n\n\tpayload, err := json.Marshal(&data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to marshal token data: %w\", err)\n\t}\n\n\tsignature, err := tpmSign(key, payload)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to sign token data: %w\", err)\n\t}\n\ttoken := &gcetpm.AuthToken{\n\t\tData:      payload,\n\t\tSignature: signature,\n\t}\n\n\tb, err := json.Marshal(token)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to marshal token: %w\", err)\n\t}\n\treturn gcetpm.GCETPMAuthenticationTokenPrefix + base64.StdEncoding.EncodeToString(b), nil\n}\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/gce/tpm/gcetpmsigner/tpmauthenticator.go#L80-L116","documentation":"After building the AuthTokenData struct (audience, request hash), CreateToken serializes it with json.Marshal. This error wraps any failure of that marshal step. In practice it is near-impossible with this plain struct, so it indicates a programming error or an exotic json.Marshaler bug rather than an environment problem.","triggerScenarios":"json.Marshal(&data) returns an error — only possible if AuthTokenData (or a field of it) implements MarshalJSON and fails, or an unsupported type is present.","commonSituations":"Custom MarshalJSON on AuthTokenData that panics/returns error after a refactor; adding an unsupported field type (chan, func) to the struct.","solutions":["Inspect the wrapped error for the offending field/type","Remove or fix any custom MarshalJSON implementation on gcetpm.AuthTokenData","Ensure all struct fields are JSON-serializable types","Rebuild with the version of kops/gcetpm package that matches your vendored types"],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"if _, err := json.Marshal(gcetpm.AuthTokenData{}); err != nil {\n    // detect serialization problem in struct definition early (tests)\n    panic(err)\n}","typeGuard":null,"tryCatchPattern":"token, err := authenticator.CreateToken(ctx, request)\nif err != nil {\n    var me *json.MarshalTypeError\n    if errors.As(err, &me) { log.Fatalf(\"bad field %s\", me.Field) }\n    return err\n}","preventionTips":["Add a unit test marshalling AuthTokenData with representative data","Keep AuthTokenData fields to JSON-native types","Avoid custom MarshalJSON unless necessary and well-tested"],"tags":["json","serialization","gce","tpm"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}