AlexxIT/go2rtc · error

credentials: storage not initialized

Error message

credentials: storage not initialized

What it means

pkg/creds/creds.go:24 backs the credential store with a package-level storage set via SetStorage. SetValue checks that storage first; if SetStorage was never called (storage == nil) it returns "credentials: storage not initialized". The library deliberately fails fast instead of silently dropping credentials — there is nowhere to persist the value.

Solutions

  1. Call creds.SetStorage(impl) at startup before any SetValue, with a real Storage implementation.
  2. If you don't need persistence, use the library's provided in-memory/file storage implementation rather than leaving storage nil.
  3. Fix initialization order so SetStorage runs in main/init before workers call SetValue.
  4. Wrap SetValue calls with a check/error message pointing to the missing SetStorage call.

Example fix

// before
func main() {
    _ = creds.SetValue("camera_password", "secret") // storage not initialized
}
// after
func main() {
    creds.SetStorage(NewFileStorage("/data/creds.json"))
    _ = creds.SetValue("camera_password", "secret")
}
Defensive patterns

Strategy: validation

Validate before calling

if !credsInitialized { // set true right after SetStorage
    return errors.New("call creds.SetStorage before using credentials")
}

Try / catch

if err := creds.SetValue(name, value); err != nil && strings.Contains(err.Error(), "storage not initialized") {
    return fmt.Errorf("credentials backend missing: %w (call creds.SetStorage at startup)", err)
}

Prevention

When it happens

Trigger: Calling creds.SetValue (directly or via anything that stores credentials) in a process where creds.SetStorage(s) was never invoked — e.g. a service that uses this library without wiring a credentials backend (file, database, etc.).

Common situations: Embedding the library in a custom binary while forgetting to initialize the creds backend at startup; running components (tests, CLI tools) that skip the storage setup step; ordering bug where SetValue runs before SetStorage during init.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/50e391784e936fd4. Report an issue: GitHub.

Appendix: source

Thrown at pkg/creds/creds.go:24

	"path/filepath"
	"regexp"
	"strings"
)

type Storage interface {
	SetValue(name, value string) error
	GetValue(name string) (string, bool)
}

var storage Storage

func SetStorage(s Storage) {
	storage = s
}

func SetValue(name, value string) error {
	if storage == nil {
		return errors.New("credentials: storage not initialized")
	}
	if err := storage.SetValue(name, value); err != nil {
		return err
	}
	AddSecret(value)
	return nil
}

func GetValue(name string) (value string, ok bool) {
	value, ok = getValue(name)
	AddSecret(value)
	return
}

func getValue(name string) (string, bool) {
	if storage != nil {
		if value, ok := storage.GetValue(name); ok {
			return value, true

View on GitHub (pinned to c245815e75)