config

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config keeps the CLI's credentials on disk and narrows what the rest of the machine can do with them.

What is narrowed

`~/.alpacon` is held at 0700 and the files inside it at 0600. Anything found wider is narrowed in place before it is read or written, not only at creation, because a file already wide when this CLI arrived is the one worth closing. This file decides what to narrow; permissions_unix.go holds the guards it refuses on, and the platform-specific opens live in permissions_darwin.go and permissions_linux.go.

Handles, not paths

Each chmod is bound to an open handle, so nothing can be swapped in between the look and the change. Two path-based reads remain, and neither can widen a mode: restrictConfigDirectoryMode's pre-check for a directory permitting traversal but not reads, safe because restrictSearchableDirectory re-derives the mode from its own handle and only ANDs with 0700, and narrowToPublishedFileMode's os.Stat in device.go, which only intersects.

What a caller must not read past

refuseUnsafeConfigFile rejects anything that is not a regular file—O_NONBLOCK keeps the open off a named pipe but not the read behind it—and a file this process does not own, since root can read anything and a sudo that keeps HOME would otherwise take a user's own file as root's device id. Both carry errUnsafeConfigFile, the one narrowing failure LoadConfig refuses rather than warns about: a config file another account owns names the host the CLI talks to and decides whether its certificate is checked.

Best effort everywhere else

The mode is read back after every chmod, accepted or refused, since a filesystem without permission bits can keep the mode either way; only bits granting another account access count as a failure, because macOS reports every FAT file as 0700. Any other failure—a read-only mount, a directory another account owns—makes warnUnrestricted warn once per subject and cause, and the command carries on. ~/.alpacon/device_id is the exception: it fails closed, so a handle it could not protect means no identifier and the server falls back to an IP fingerprint.

Windows

All of it is Unix only. Windows chmod changes the read-only attribute, so nothing is repaired there, the modes are not enforced, and the symlink, hard-link and owner refusals are stubs—the device id is not protected either.

Index

Constants

View Source
const (
	ConfigFileName = "config.json"
	ConfigFileDir  = ".alpacon"
	// KubeSessionDir, under ConfigFileDir, holds the kube sessions
	// 'alpacon kube login' opened; logout removes it with the config.
	KubeSessionDir = "kube"

	// ServiceTokenPrefix is the server's default service-token key prefix. A self-hosted server
	// configured with another prefix issues tokens this treats as generic. Personal API tokens
	// use "alpat-".
	ServiceTokenPrefix = "alpst-"
)
View Source
const (
	// DeviceIDFileName holds the per-installation device identifier. It sits beside config.json
	// rather than inside it because logout deletes config.json and the identifier must outlive it.
	DeviceIDFileName = "device_id"
)

Variables

View Source
var ErrLoginChanged = errors.New("the config now holds another login")

ErrLoginChanged reports that the config holds another login than the one a change was decided for, so nothing was written or removed.

View Source
var ErrWorkspaceChanged = errors.New("the config now names another workspace")

ErrWorkspaceChanged reports that the config names a different workspace than the one a value was fetched for, so the value was not written.

Functions

func ClearActiveWorkSessionFor added in v1.16.0

func ClearActiveWorkSessionFor(workspaceName, uuid string) (bool, error)

ClearActiveWorkSessionFor clears workspaceName's active work-session only while it is still uuid, so ending one session never drops another the user has since switched to. Reports whether it cleared.

func ClearSignerToken added in v1.18.0

func ClearSignerToken(accessToken string) error

ClearSignerToken drops the cached signer token while it is still accessToken, the one the signer refused; a token another shell cached since is kept.

func CreateConfig

func CreateConfig(workspaceURL, workspaceName, token, expiresAt, accessToken, refreshToken, baseDomain string, expiresIn int, insecure bool) error

func DeleteConfig

func DeleteConfig(login Config) error

DeleteConfig removes the config while it still holds login's credentials; a login another shell made since stays and DeleteConfig returns ErrLoginChanged. A config that is already gone is no error.

func GetActiveWorkSession added in v1.4.6

func GetActiveWorkSession() (string, error)

GetActiveWorkSession returns the active work-session UUID for the current workspace, or "" when none is set or no config file exists. A caller holding a client uses GetActiveWorkSessionFor with the client's workspace instead.

func GetActiveWorkSessionFor added in v1.18.0

func GetActiveWorkSessionFor(workspaceName string) (string, error)

GetActiveWorkSessionFor is GetActiveWorkSession for workspaceName, a parameter rather than a fresh read so another shell's 'alpacon ws use' cannot swap in a foreign session.

func GetAuthMethod added in v1.4.8

func GetAuthMethod(cfg Config) string

GetAuthMethod returns a human-readable authentication method string for cfg.

func GetOrCreateDeviceID added in v1.11.0

func GetOrCreateDeviceID() (string, error)

GetOrCreateDeviceID returns the stable per-installation device identifier, generating and persisting a new one when the file is missing or holds a malformed value. The server binds an MFA presence proof to it instead of an IP-based fingerprint that installations behind one egress IP would share.

It lives in ~/.alpacon/device_id, shared by every workspace this installation logs in to and untouched by DeleteConfig, so a logout does not orphan the proofs bound to it.

The identifier grants nothing on its own, but presence proofs bind to it and it may be recorded in the identity provider's logs, so the file is kept at 0600 and does not belong in logs or bug reports.

func GetSmuxConfig added in v1.0.0

func GetSmuxConfig() *smux.Config

GetSmuxConfig returns a ready-to-use smux configuration.

func IsSaaS added in v1.4.2

func IsSaaS() (bool, error)

IsSaaS returns true if the workspace is an Alpacon Cloud (SaaS) deployment authenticated via Auth0. OnPrem deployments use a legacy API token and return false.

func IsServiceToken added in v1.7.4

func IsServiceToken(token string) bool

IsServiceToken reports whether token is a service token, identified by its key prefix. Service tokens are application principals and have no user profile.

func IsValidDeviceID added in v1.11.0

func IsValidDeviceID(id string) bool

IsValidDeviceID reports whether id is acceptable as a device identifier, applying the same rule as the Auth0 action that consumes it.

func ResolveAuthMethod added in v1.4.8

func ResolveAuthMethod() string

ResolveAuthMethod loads config and returns the auth method string.

func RestoreWorkspace added in v1.14.3

func RestoreWorkspace(switchedURL string, orig Config) error

RestoreWorkspace puts orig's workspace back in one save while the config still holds orig's login on switchedURL, the workspace a failed switch saved; otherwise it writes nothing.

func SaveRefreshedAuth0Token

func SaveRefreshedAuth0Token(refreshToken, accessToken string, expiresIn int) error

SaveRefreshedAuth0Token stores accessToken while the config still holds refreshToken; once a login made during the grant has replaced it, it returns ErrLoginChanged and writes nothing.

func SaveSignerToken added in v1.18.0

func SaveSignerToken(login Config, target SignerTarget, accessToken string, expiresIn int) error

SaveSignerToken caches accessToken for login's workspace and target. It writes nothing and returns ErrLoginChanged or ErrWorkspaceChanged once login is replaced.

func SetActiveWorkSession added in v1.4.6

func SetActiveWorkSession(uuid string) error

SetActiveWorkSession persists the work-session UUID for the config's current workspace ("" clears it); a caller holding a client must use SetActiveWorkSessionFor with the client's pinned workspace instead.

func SetActiveWorkSessionFor added in v1.12.0

func SetActiveWorkSessionFor(workspaceName, uuid string) error

SetActiveWorkSessionFor persists the work-session UUID under workspaceName ("" clears it); workspaceName is a parameter, not a fresh read, because create --wait --use can block on approval long enough for another shell's 'alpacon ws use' to file it under a workspace it was never created in.

func SetWorkspaceEnv added in v1.17.0

func SetWorkspaceEnv(workspaceURL, schemaName string, kubernetesSurface bool) error

SetWorkspaceEnv records the schema name (empty keeps the stored one) and Kubernetes surface the workspace at workspaceURL reported, and writes nothing once another login or switch has moved the config elsewhere.

func SwitchWorkspace added in v1.1.0

func SwitchWorkspace(from Config, newURL, newName string, kubernetesSurface bool) error

SwitchWorkspace moves from's login to newURL, saving the name, identity and Kubernetes surface answer in one write; once another login or switch has replaced from, it writes nothing.

func WhileLoggedIn added in v1.18.0

func WhileLoggedIn(login Config, fn func() error) error

WhileLoggedIn runs fn under the config lock while the config still holds login's credentials, and returns ErrLoginChanged without running it once a logout or another login has replaced them. A logout takes the same lock, so nothing fn writes outside the config can land after the logout that should have removed it. fn must not write the config: the lock is not reentrant.

Types

type Config

type Config struct {
	WorkspaceURL string `json:"workspace_url"`
	// WorkspaceName is the label of the workspace URL's host, as typed at login.
	// A workspace's URL slug can be renamed, so it is not the workspace's
	// identity; see SchemaName and WorkspaceIdentity.
	WorkspaceName string `json:"workspace_name"`
	// SchemaName is the workspace's frozen identity, taken from the server's
	// auth environment at login. Empty in a config written before the field
	// existed, or by a server that does not report it.
	SchemaName           string `json:"schema_name,omitempty"`
	Token                string `json:"token,omitempty"`
	ExpiresAt            string `json:"expires_at,omitempty"`
	AccessToken          string `json:"access_token,omitempty"`
	RefreshToken         string `json:"refresh_token,omitempty"`
	AccessTokenExpiresAt string `json:"access_token_expires_at,omitempty"`
	BaseDomain           string `json:"base_domain,omitempty"`
	Insecure             bool   `json:"insecure"`
	// ActiveWorkSessions maps workspace identity (see WorkspaceIdentity) to active work-session UUID.
	// Nil (not an empty map) when the key is absent from the JSON config file.
	ActiveWorkSessions map[string]string `json:"active_work_sessions,omitempty"`
	// KubernetesSurface records the server's surfaces.kubernetes answer from
	// /api/auth/env, taken at login and on workspace switch; absent means the
	// server never said yes. It only decides whether 'alpacon kube' is shown—
	// the server refuses the endpoints on its own, so this is not a control.
	KubernetesSurface bool `json:"kubernetes_surface,omitempty"`
	// Signer caches the command signer's token and what it was issued for;
	// nil when nothing is cached.
	Signer *SignerGrant `json:"signer,omitempty"`
}

Config describes the configuration for Alpacon CLI.

func LoadConfig

func LoadConfig() (Config, error)

func (Config) IsMultiWorkspaceMode added in v1.1.0

func (c Config) IsMultiWorkspaceMode() bool

IsMultiWorkspaceMode returns true if the user logged in via Auth0 with a known base domain, enabling workspace listing and switching from the JWT.

func (Config) IsSaaS added in v1.4.2

func (c Config) IsSaaS() bool

func (Config) SignerToken added in v1.18.0

func (c Config) SignerToken(now time.Time, target SignerTarget) (string, bool)

SignerToken returns the cached signer token when it was issued for the workspace the config names and for target, and is outside its expiry margin.

func (Config) WorkspaceIdentity added in v1.14.3

func (c Config) WorkspaceIdentity() string

WorkspaceIdentity is the value that names the current workspace to the server and in the JWT workspaces claim: the schema name, or the host label for a config that predates it. The next login records the schema name.

type SignerGrant added in v1.18.0

type SignerGrant struct {
	AccessToken  string       `json:"access_token"`
	ExpiresAt    string       `json:"expires_at"`
	WorkspaceURL string       `json:"workspace_url"`
	Target       SignerTarget `json:"target"`
}

SignerGrant is a cached signer token. It has no refresh token, so it is authorized again once it expires.

type SignerTarget added in v1.18.0

type SignerTarget struct {
	URL      string `json:"url"`
	Audience string `json:"audience"`
}

SignerTarget is the signer a token is sent to and the audience it is issued for. A cached token is used only with the target it was issued for.

Jump to

Keyboard shortcuts

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