keyring

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 6 Imported by: 0

README

keyring

Secure credential storage for Go, backed by the OS keyring (Windows Credential Manager, macOS Keychain, Linux Secret Service).

Two APIs on the same backend:

  • Keyring — generic, service-scoped key/value store. Ideal as the SecretStore of github.com/tinywasm/git and for CLI auth flows.
  • KeyManager — the classic manager for the updater-cicd service secrets (HMAC secret + GitHub PAT) with Setup/Rotate/Reset semantics.
import "github.com/tinywasm/keyring"

// Service-scoped store: same key names never collide across services.
kr, err := keyring.NewKeyring("my-app")
if err != nil { /* OS keyring missing; it auto-installs on Linux */ }
kr.SetLog(log.Printf)
kr.Set("github_token", pat)
tok, err := kr.Get("github_token")
kr.Delete("github_token")

// Classic manager (updater-cicd service).
km := keyring.New()
km.Setup(hmacSecret, pat)
No keyring on the machine?

NewKeyring verifies the backend on startup and, on Linux, tries to install gnome-keyring + libsecret with the distro package manager, then starts gnome-keyring-daemon. Tests never touch the OS keyring: they swap the backend with keyring.SetProvider and restore it with keyring.GetProvider.

Documentation

Documentation

Index

Constants

View Source
const (
	ServiceName   = "updater-cicd"
	HMACSecretKey = "hmac-secret"
	GitHubPATKey  = "github-pat"
)

Variables

This section is empty.

Functions

func SetProvider added in v0.0.2

func SetProvider(p Provider)

SetProvider replaces the OS keyring backend. Tests use it to inject an in-memory provider; production code never calls it.

Types

type KeyManager added in v0.0.2

type KeyManager struct {
	// contains filtered or unexported fields
}

KeyManager manages the service-exposed secrets (HMAC secret, GitHub PAT) of ServiceName on top of a generic Keyring. Same API as before; now backed by the shared Keyring type.

func New

func New() *KeyManager

New creates a KeyManager over the ServiceName service.

func (*KeyManager) DeleteAll added in v0.0.2

func (m *KeyManager) DeleteAll() error

DeleteAll elimina todos los secretos (reset)

func (*KeyManager) GetGitHubPAT added in v0.0.2

func (m *KeyManager) GetGitHubPAT() (string, error)

GetGitHubPAT obtiene el GitHub PAT

func (*KeyManager) GetHMACSecret added in v0.0.2

func (m *KeyManager) GetHMACSecret() (string, error)

GetHMACSecret obtiene el HMAC secret

func (*KeyManager) IsConfigured added in v0.0.2

func (m *KeyManager) IsConfigured() bool

IsConfigured verifica si están configurados

func (*KeyManager) RotateGitHubPAT added in v0.0.2

func (m *KeyManager) RotateGitHubPAT(newPAT string) error

RotateGitHubPAT rota el GitHub PAT

func (*KeyManager) RotateHMACSecret added in v0.0.2

func (m *KeyManager) RotateHMACSecret(newSecret string) error

RotateHMACSecret rota el HMAC secret

func (*KeyManager) SetLog added in v0.0.2

func (m *KeyManager) SetLog(fn func(...any))

SetLog sets the logging function.

func (*KeyManager) Setup added in v0.0.2

func (m *KeyManager) Setup(hmacSecret, githubPAT string) error

Setup realiza el setup inicial - solo primera ejecución

type Keyring

type Keyring struct {
	// contains filtered or unexported fields
}

Keyring provides scoped credential storage through the system keyring. The service name is a namespace: the same key under different services never collides, so one process can hold secrets for several apps.

func NewKeyring added in v0.0.2

func NewKeyring(service string) (*Keyring, error)

NewKeyring creates a Keyring scoped to service and verifies the system keyring actually works, installing its dependencies on Linux when missing.

func (*Keyring) Delete added in v0.0.2

func (k *Keyring) Delete(key string) error

Delete removes key from this service's namespace.

func (*Keyring) Get added in v0.0.2

func (k *Keyring) Get(key string) (string, error)

Get returns the value stored under key in this service's namespace.

func (*Keyring) Set added in v0.0.2

func (k *Keyring) Set(key, value string) error

Set stores value under key in this service's namespace.

func (*Keyring) SetLog added in v0.0.2

func (k *Keyring) SetLog(fn func(...any))

SetLog sets the logging function.

type Provider added in v0.0.2

type Provider interface {
	// Set stores password for user under service.
	Set(service, user, password string) error
	// Get returns the password stored for user under service.
	Get(service, user string) (string, error)
	// Delete removes the password stored for user under service.
	Delete(service, user string) error
	// DeleteAll removes every entry under service.
	DeleteAll(service string) error
}

Provider abstracts the OS keyring backend (Secret Service, Keychain, Credential Manager). The default provider talks to the system; tests swap it with SetProvider to avoid touching real credentials.

func GetProvider added in v0.0.2

func GetProvider() Provider

GetProvider returns the active keyring backend. Used to restore the real provider after swapping it in tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL