{"record":{"id":"43ec64c461c4289b","repo":"OpenNHP/opennhp","slug":"failed-to-generate-uuid-v4-w","errorCode":null,"errorMessage":"failed to generate UUID v4: %w","messagePattern":"failed to generate UUID v4: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/utils/uuid.go","lineNumber":49,"sourceCode":"\n// RandNumber returns a random 4-digit number (1000-10999) for non-security purposes.\n//\n//nolint:gosec // G404: math/rand is acceptable for non-security random numbers\nfunc RandNumber() int {\n\trng := rand.New(rand.NewSource(time.Now().UnixNano()))\n\trandomNumber := rng.Intn(10000)\n\tif randomNumber < 1000 {\n\t\trandomNumber += 1000\n\t}\n\n\treturn randomNumber\n}\n\n// GenerateUUIDv4 creates a random UUID (version 4)\nfunc GenerateUUIDv4() (string, error) {\n\tu, err := uuid.NewRandom()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to generate UUID v4: %w\", err)\n\t}\n\treturn u.String(), nil\n}\n","sourceCodeStart":31,"sourceCodeEnd":53,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/uuid.go#L31-L53","documentation":"GenerateUUIDv4 wraps any failure from github.com/google/uuid's uuid.NewRandom() with the message 'failed to generate UUID v4: %w'. NewRandom reads cryptographic randomness (crypto/rand); if the OS entropy source is unavailable the call fails and this error is returned instead of a UUID string. It preserves the underlying cause via %w for errors.Is/As inspection.","triggerScenarios":"Calling GenerateUUIDv4 (directly or via registerTAService, NewSmartPolicy, or TestGenerateUUIDv4) when the kernel CSPRNG cannot serve random bytes — e.g. uuid.NewRandom returns a rand.Read error.","commonSituations":"Running on restricted/hardened containers or VMs with a broken or missing /dev/urandom; seccomp/AppArmor profiles blocking getrandom(2); extremely early boot environments before entropy initialization; exotic platforms where crypto/rand is stubbed.","solutions":["Inspect the wrapped cause with errors.Is/As — the underlying rand.Read error names the OS-level problem (e.g. 'getrandom: function not implemented').","Fix the environment: ensure /dev/urandom exists and the container runtime/seccomp profile permits getrandom(2).","Upgrade the Go toolchain: modern versions fall back from getrandom to /dev/urandom instead of failing.","If failures are persistent in a sandbox, generate UUIDs via uuid.New() (V4 with the same entropy) only after verifying crypto/rand works, or run the process with a less restrictive profile.","Retry the call once on transient entropy errors before failing the registration/policy operation."],"exampleFix":"// before\nu, err := uuid.NewRandom()\nif err != nil {\n\treturn \"\", fmt.Errorf(\"failed to generate UUID v4: %w\", err)\n}\n// after\nu, err := uuid.NewRandom()\nif err != nil {\n\tif errors.Is(err, rand.ErrUnsupported) {\n\t\t// entropy unavailable: surface a targeted, actionable message\n\t\treturn \"\", fmt.Errorf(\"failed to generate UUID v4: system entropy source unavailable: %w\", err)\n\t}\n\treturn \"\", fmt.Errorf(\"failed to generate UUID v4: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"// Best-effort pre-check that the entropy source is usable before generating UUIDs\nif f, err := os.Open(\"/dev/urandom\"); err != nil {\n\treturn fmt.Errorf(\"entropy source unavailable: %w\", err)\n} else {\n\tf.Close()\n}","typeGuard":null,"tryCatchPattern":"id, err := nhputils.GenerateUUIDv4()\nif err != nil {\n\tvar pathErr *os.PathError\n\tif errors.As(err, &pathErr) {\n\t\t// entropy source problem: fall back or abort with clear context\n\t}\n\treturn fmt.Errorf(\"cannot create request id: %w\", err)\n}","preventionTips":["Run in environments where /dev/urandom is present and getrandom(2) is permitted by seccomp/AppArmor.","Check errors.Is(err, rand.ErrUnsupported) to distinguish missing entropy support from other failures.","Retry once on transient entropy errors before failing registration/policy creation.","Keep the Go toolchain current — newer versions handle getrandom fallbacks more robustly."],"tags":["uuid","entropy","go","cryptography"],"backgroundTag":"missing-optional-dependency","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}