Documentation
¶
Overview ¶
Package atrest protects small local files, such as token caches, with the operating system's own data-protection facility, so their contents are useless once copied off the account that wrote them: into a backup, a synced home directory, or a disk image.
Seal wraps data in a JSON envelope naming the algorithm that protected it. Open reverses Seal and also accepts plaintext, reporting which it was given, so a caller that finds plaintext written by an older build can read it and seal it again instead of discarding it. Where no facility exists, and where one exists but nothing on the machine can reach it right now, such as a container with no D-Bus session and no usable kernel keyring, Seal returns data unchanged; Available reports whether the platform has a facility at all, not whether this call will succeed.
The envelope is JSON so that an older build of a program which hands the file to a JSON parser sees an object it does not recognise, rather than bytes it cannot parse. MSAL's token cache, for one, reads an unrecognised object as an empty cache and prompts for sign-in, where unparseable bytes fail every call.
Protection is against the file leaving its context. Any process running as the same user can open what Seal produced, on every platform.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCannotOpen = errors.New("atrest: sealed data cannot be opened here")
ErrCannotOpen reports sealed data that this build cannot open: sealed on another machine or by another user, sealed by an algorithm this platform lacks, or written by a newer envelope version. The data is not corrupt, but nothing here will read it; a cache in this state should be treated as empty.
var ErrLocked = errors.New("atrest: the key store is locked")
ErrLocked is an ErrCannotOpen whose cause is the key store that holds the data's key being locked, or unreachable from this process, rather than the key being absent. The data opens once the store does, which Unlock can ask for, so a caller that would otherwise discard the data and start over can offer that first. errors.Is reports both ErrLocked and ErrCannotOpen for it.
Functions ¶
func Available ¶
func Available() bool
Available reports whether this platform has a sealing facility at all. It is true on Linux and macOS even where nothing is reachable at the moment — no D-Bus session, no usable kernel keyring, no Keychain — since that can change between one call and the next; Seal itself is what falls back to plaintext on a call it cannot seal.
func Open ¶
Open returns the plaintext of data and whether data was sealed. Data that is not an envelope is returned as plaintext with sealed false; a caller that can seal should then store it sealed.
A JSON object carrying envelope fields alongside others is also plaintext: an older build that preserves unknown fields read the envelope as an empty document and saved its own contents back around the envelope's fields. Those fields are removed before the plaintext is returned.
An envelope that cannot be opened here returns an error wrapping ErrCannotOpen, and also ErrLocked when unlocking its key store would open it.
func Persistent ¶ added in v0.2.0
func Persistent() bool
Persistent reports whether what Seal produces right now would still open after a reboot. It is false on Linux when only the kernel keyring is reachable, because that key does not survive one, and where the platform has no facility. A caller whose cache is worth more than the protection, such as a refresh token that costs an interactive sign-in to replace, can store it unsealed while this is false rather than lose it at every boot.
func Seal ¶
Seal protects data under name and returns the envelope to store. name must be the same at every Open of the result, and should be specific to the program and the file, since it keeps one program's sealed data from opening under another's name.
func Unlock ¶ added in v0.3.0
Unlock asks the platform to unlock the key store behind an ErrLocked through the desktop's own prompt, on Linux the Secret Service's dialog for the login keyring. It returns nil once the store is unlocked, and an error when it stays locked: the prompt was dismissed, ctx ended first, this process has no display to show a prompt on, or the platform has nothing to unlock.
Types ¶
This section is empty.