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 rather than only at the moment it is created, because a file that was already wide when this CLI arrived is exactly 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 rather than to a path, so nothing can be swapped in between the look and the change. Two path-based reads are left, and neither decides a mode by itself: the pre-check in restrictConfigDirectoryMode that asks whether a directory permitting traversal but not reads has anything to narrow at all—safe because the platform helper behind it re-derives the mode from its own handle and only ever ANDs with 0700—and narrowToPublishedFileMode's os.Stat in device.go, which reads the mode a rename is about to stand in for and intersects it, so it can only take bits away.
What a caller must not read past ¶
refuseUnsafeConfigFile rejects anything that is not a regular file, since O_NONBLOCK keeps the open off a named pipe but says nothing about the read behind it—os.NewFile hands a non-blocking descriptor to the poller and io.ReadAll then waits there forever—and it rejects a handle on a file this process does not own, because mode bits say nothing there: root can read anything, and a sudo that keeps HOME would otherwise take an unprivileged user's own file as root's device id. Both carry errUnsafeConfigFile, and that is the one narrowing failure LoadConfig refuses rather than warns about, since a config file another account owns names the host the CLI talks to and decides whether its certificate is checked. LoadConfig reads the very handle this narrowed, and falls back to reopening the path only for a symbolic link, which is refused a chmod but not a read—that fallback runs the same refusal on what the link resolves to.
Best effort everywhere else ¶
The mode is read back after a chmod that reported success, since a filesystem without permission bits answers the call and keeps the mode; only bits that hand another account access count as a failure, because macOS reports every file on a FAT volume as 0700. A chmod that fails outright is held to the same standard, which is what keeps a Linux vfat mount whose fmask already grants no other account access from being read as an exposure. Everything else is best effort—a read-only mount, a directory another account owns, and a modeless filesystem all refuse the chmod—so warnUnrestricted warns once per subject and cause and the command carries on rather than dying over a mode it cannot change. ~/.alpacon/device_id is the exception in the other direction: it fails closed, because an MFA presence proof binds to whatever identifier readDeviceID returns, so a handle it could not protect is treated as no identifier at all and the server falls back to an IP fingerprint.
Windows ¶
All of it is Unix only. Windows chmod changes the read-only attribute rather than access permissions, so nothing is repaired there, the two modes above are not enforced, and the symlink, hard-link and owner refusals are stubs—the device id is not protected on Windows either.
Index ¶
- Constants
- Variables
- func ClearActiveWorkSessionFor(workspaceName, uuid string) (bool, error)
- func CreateConfig(...) error
- func DeleteConfig(login Config) error
- func GetActiveWorkSession() (string, error)
- func GetAuthMethod(cfg Config) string
- func GetOrCreateDeviceID() (string, error)
- func GetSmuxConfig() *smux.Config
- func IsSaaS() (bool, error)
- func IsServiceToken(token string) bool
- func IsValidDeviceID(id string) bool
- func ResolveAuthMethod() string
- func RestoreWorkspace(switchedURL string, orig Config) error
- func SaveRefreshedAuth0Token(refreshToken, accessToken string, expiresIn int) error
- func SetActiveWorkSession(uuid string) error
- func SetActiveWorkSessionFor(workspaceName, uuid string) error
- func SetWorkspaceEnv(workspaceURL, schemaName string, kubernetesSurface bool) error
- func SwitchWorkspace(from Config, newURL, newName string, kubernetesSurface bool) error
- type Config
Constants ¶
const ( ConfigFileName = "config.json" ConfigFileDir = ".alpacon" // ServiceTokenPrefix is the default service-token key prefix, hard-coded here. // It matches the server's default SERVICE_TOKEN_PREFIX; if a self-hosted server // overrides that setting, its service tokens will not match this prefix and are // treated as generic tokens. Personal API tokens use "alpat-". ServiceTokenPrefix = "alpst-" )
const ( // DeviceIDFileName holds the per-installation device identifier. It sits // beside config.json rather than inside it so it is independent of session // state: logout deletes config.json, and the machine has not changed, so the // identifier must outlive it. This mirrors the web client, which keeps its // own identifier in localStorage rather than alongside its session. DeviceIDFileName = "device_id" )
Variables ¶
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.
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
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 CreateConfig ¶
func DeleteConfig ¶
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
GetActiveWorkSession returns the active work-session UUID for the current workspace. Returns "" (no error) when no session is set, the config is missing the map, or no config file exists.
func GetAuthMethod ¶ added in v1.4.8
GetAuthMethod returns a human-readable authentication method string for cfg.
func GetOrCreateDeviceID ¶ added in v1.11.0
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 identifier lets the server bind an MFA presence proof to the CLI installation that requested the challenge; without it the server has to fall back to an IP-based fingerprint, which two installations behind one egress IP would share.
It lives in ~/.alpacon/device_id, one line of plain text. That file is per installation rather than per workspace, so every workspace this installation logs in to shares the identifier, and it is untouched by DeleteConfig, so a logout does not orphan the presence proofs bound to it.
The identifier is not a secret in the sense a password or a token is—knowing it grants nothing to someone who cannot also authenticate as the user—but it is not inert either. It is the value an MFA presence proof binds to, and it identifies this installation to the identity provider, travelling there in OAuth requests where it may be recorded in tenant logs. So it is a stable, security-relevant identifier rather than a throwaway: the file is kept at 0600 inside the 0700 config directory, out of reach of other accounts on a shared machine, and it does not belong in logs or bug reports.
func GetSmuxConfig ¶ added in v1.0.0
GetSmuxConfig returns a ready-to-use smux configuration.
func IsSaaS ¶ added in v1.4.2
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
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
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
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 ¶
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 SetActiveWorkSession ¶ added in v1.4.6
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
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
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
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.
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"`
}
Config describes the configuration for Alpacon CLI
func LoadConfig ¶
func (Config) IsMultiWorkspaceMode ¶ added in v1.1.0
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) WorkspaceIdentity ¶ added in v1.14.3
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.