Documentation
¶
Overview ¶
Package spritestate stores what the sprites commands remember between runs: the default organization and the Sprites API token minted for each organization. It is a separate package so that `fly auth logout` can clear it without importing the sprites commands.
Index ¶
- Variables
- func Clear(configDir string) error
- func KeepDefaultOrgFor(ctx context.Context, configDir, userID string) error
- func LockMint(ctx context.Context, configDir string) (unlock func(), err error)
- func LockMintForLogin(ctx context.Context, configDir string) (unlock func(), err error)
- func MintToken(ctx context.Context, apiURL, org, flyMacaroon string) (string, error)
- func Path(configDir string) string
- func RevokeAll(ctx context.Context, configDir, flyMacaroon string, keepDefaultOrg bool) (failed []string, err error)
- func RevokeForLoginChange(ctx context.Context, configDir, configFile string, keepDefaultOrg bool) (warnings []string)
- func RevokeToken(ctx context.Context, apiURL, org, flyMacaroon, token string) error
- func Save(configDir string, st *State) error
- func TokenKey(apiURL, org string) string
- func Update(ctx context.Context, configDir string, fn func(*State)) error
- func Used(configDir string) bool
- type State
Constants ¶
This section is empty.
Variables ¶
var ErrCorrupt = errors.New("the saved Sprites state could not be read")
ErrCorrupt reports that the state file could not be parsed. Load moves such a file aside, so that saving doesn't overwrite tokens that may still be valid, and returns an empty state along with the error. Callers can warn and carry on.
var RevokeTimeout = 30 * time.Second
RevokeTimeout caps how long a login or logout spends revoking Sprites tokens, so an unreachable Sprites API delays it by at most this much. It is a variable so tests can shorten it.
Functions ¶
func KeepDefaultOrgFor ¶
KeepDefaultOrgFor drops the saved default organization unless userID, the user now logged in, is the one who chose it. An empty userID, for a user that couldn't be found, always drops it.
func LockMint ¶
LockMint serializes cached token reads and minting with login and logout. A login change holds it through both clearing state and saving the login, so an in-flight mint cannot repopulate state after it has been cleared. It is separate from Update's lock, allowing the holder to save state.
func LockMintForLogin ¶
LockMintForLogin is LockMint for login and logout, which wait less: the caller warns and goes ahead without the lock rather than fail the login change over Sprites state.
func RevokeAll ¶
func RevokeAll(ctx context.Context, configDir, flyMacaroon string, keepDefaultOrg bool) (failed []string, err error)
RevokeAll revokes every saved token and then removes the state file. It returns the tokens' organizations that could not be revoked; the tokens are forgotten regardless, since the login that could revoke them is going away. With keepDefaultOrg, a default organization whose user is known is kept, for KeepDefaultOrgFor to check once the next login's user is known.
func RevokeForLoginChange ¶
func RevokeForLoginChange(ctx context.Context, configDir, configFile string, keepDefaultOrg bool) (warnings []string)
RevokeForLoginChange revokes and forgets the saved tokens before the Fly.io login saved in configFile is replaced or removed, since that login minted them and is the only one that can revoke them. It never fails the login change: what went wrong comes back as warnings for the caller to print. keepDefaultOrg is for a login, which may be the same user again; see RevokeAll.
func RevokeToken ¶
RevokeToken deletes a Sprites API token minted in org. A token that is already gone counts as revoked.
func Save ¶
Save writes the state file with owner-only permissions, since it holds tokens. It replaces the file in one step, so a reader never sees it half written.
func TokenKey ¶
TokenKey identifies a token by the API it is for and the organization it belongs to.
func Update ¶
Update applies fn to the current state and saves the result, holding a lock so that concurrent sprites commands do not drop each other's changes.
func Used ¶
Used reports whether the sprites commands have ever run with this config directory: whether the state file or the mint lock, which a sprites command takes before it saves anything, exists. Login and logout skip all Sprites work when it is false, so they are unchanged for people who never use Sprites. Anything but a definite "not there" counts as used, so that the cleanup is attempted rather than skipped.
Types ¶
type State ¶
type State struct {
// DefaultOrg is the organization used when none is specified.
DefaultOrg string `json:"default_org,omitempty"`
// DefaultOrgUser is the ID of the Fly.io user who chose DefaultOrg. A
// login as that same user keeps the default; any other login drops it.
DefaultOrgUser string `json:"default_org_user,omitempty"`
// Tokens holds Sprites API tokens keyed by TokenKey.
Tokens map[string]string `json:"tokens,omitempty"`
}
State is the content of the sprites state file.