Documentation
¶
Overview ¶
The keychain package for Linux uses the org.freedesktop.secret service API over dbus. For more information on the Secret Service API, see https://specifications.freedesktop.org/secret-service-spec/latest/index.html.
Index ¶
Constants ¶
This section is empty.
Variables ¶
ErrKeychainUnavailable is returned by New when the keychain backend cannot be reached at construction time, so no keychain operation could ever succeed. Callers can use errors.Is to detect this eagerly and fall back to another store instead of discovering the failure deep inside a later Get, Save, Delete, Filter or GetAllMetadata call.
On Linux it covers both failure modes of the freedesktop Secret Service:
- there is no D-Bus session bus at all (for example WSL, a headless host, or DBUS_SESSION_BUS_ADDRESS unset), so the session bus cannot be dialed; or
- the session bus exists but no process owns the org.freedesktop.secrets name (no gnome-keyring, kwallet or other Secret Service daemon running).
NOTE: like ErrNoDefaultCollection this condition is currently specific to the Linux keyring. macOS and Windows have no equivalent unreachable-backend concept, so New never returns this error on those platforms. The sentinel is declared here, in the cross-platform file (rather than the Linux-specific one), so platform-agnostic callers can reference it on every platform without build tags; on non-Linux platforms it simply never matches.
It is DISTINCT from ErrNoDefaultCollection: ErrKeychainUnavailable means the backend itself is unreachable, whereas ErrNoDefaultCollection means the backend IS reachable but has no usable default collection. The eager availability check deliberately does not assert that a collection exists, so a reachable-but-uninitialized keyring still passes New and surfaces ErrNoDefaultCollection lazily on the first operation, exactly as before.
var ErrNoDefaultCollection = errors.New("no default keychain collection available")
ErrNoDefaultCollection is returned when the secret service has no usable default collection (no 'login' collection and no collection assigned to the 'default' alias). This typically happens on headless hosts where the keyring has not been initialized.
NOTE: this condition is currently specific to the Linux keyring (the freedesktop Secret Service). macOS and Windows have no equivalent "default collection" concept, so the keychain store never returns this error on those platforms. The sentinel is nonetheless declared here, in the cross-platform file (rather than the Linux-specific one), so that platform-agnostic callers can reference it on every platform without build tags. On non-Linux platforms it simply never matches.
It is exported so callers can use errors.Is to detect the absence of usable keychain infrastructure and fall back gracefully, rather than relying on fragile error message comparisons.
Functions ¶
func New ¶
func New[T store.Secret](ctx context.Context, serviceGroup, serviceName string, factory store.Factory[T], opts ...Option) (store.Store, error)
New creates a new keychain store.
It takes ServiceGroup and ServiceName and a [Factory] as input.
A ServiceGroup is added to an item stored by the keychain under the item's attributes and label. Many applications can share the same serviceGroup.
On macOS it is important that the service group matches the Keychain Access Groups. This prevents access from other applications not inside the Keychain Access group. https://developer.apple.com/documentation/security/sharing-access-to-keychain-items-among-a-collection-of-apps#Set-your-apps-access-groups
On Linux the service group is added to the attributes of a secret to tag the item. The secrets service API does not have the concept of a scoped item per application inside the collection. Thus, adding a service group does not prevent other applications from accessing the secret.
A ServiceName is a unique name of the application storing credentials, it is important to keep the service name unchanged once the service has stored credentials. Changing the service name can be done, but would require migrating existing credentials.
[Factory] is a function used to instantiate new secrets of type T.
ctx bounds the eager backend-availability probe New performs before returning (see ErrKeychainUnavailable). On Linux it bounds the probe's D-Bus connection handshake and its NameHasOwner round-trip, and lets the caller cancel construction; if ctx carries no deadline, New applies a short internal default so construction stays responsive on an unreachable host, and a caller-supplied deadline always takes precedence. On macOS/Windows the probe is a no-op and ctx is unused. New does not retain ctx: it governs construction only, not later store operations.
Types ¶
type DarwinOptions ¶ added in v0.0.17
type DarwinOptions optionFunc[darwinOptions]
func WithUseDataProtectionKeychain ¶ added in v0.0.17
func WithUseDataProtectionKeychain() DarwinOptions
WithUseDataProtectionKeychain forces the use of entitlements to share credentials stored in the keychain between applications
type Option ¶ added in v0.0.17
type Option interface {
// contains filtered or unexported methods
}
func WithDarwinOptions ¶ added in v0.0.17
func WithDarwinOptions(opt DarwinOptions) Option