tokensvc

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 16 Imported by: 0 modules, 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 tokensvc does the I/O for token management: it finds the token config, reads and writes the five target files (Composer's auth.json, .npmrc, Docker's config.json, the git credential store and .netrc), and keeps every write inside the sandbox in dev mode. The formats themselves live in internal/core/tokens.

What it records is metadata only: the audit log and history get the target, the host and the file, never a secret.

Index

Constants

View Source
const (
	ConfigEnv        = envnames.TokensConfig
	LocalConfigName  = "authama-tokens.json"
	LegacyConfigName = "auth-token-manager.json"
)

Config file names and variables.

View Source
const (
	// DockerLogin runs `docker login --password-stdin`, which stores the
	// credential where docker will actually read it.
	DockerLogin = "login"
	// DockerRefuse skips the host with a warning.
	DockerRefuse = "refuse"
)

The docker credential-store modes.

View Source
const AliasBackupSuffix = ".authama.bak"

AliasBackupSuffix names the copy of a startup file taken before an edit.

View Source
const (
	CatalogEnv = envnames.TokensCatalog
)

Catalog override variables. auth-token-manager read its catalog from $AUTH_TOKEN_MANAGER_CATALOG when set; authama reads $AUTHAMA_TOKENS_CATALOG first, then its pre-0.1.1 name $SSH_MANAGER_TOKENS_CATALOG, and still honours the oldest one.

View Source
const ConfigMode os.FileMode = 0o644

ConfigMode is the mode of a saved token config: it names variables, not secrets, so it is readable like any other config and safe to commit.

View Source
const SecretMode os.FileMode = 0o600

SecretMode is the mode every rendered credential file gets.

Variables

View Source
var DockerLoginRunner = func(configDir, host, user, pass string) error {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()
	cmd := exec.CommandContext(ctx, "docker", "--config", configDir, "login", host, "--username", user, "--password-stdin")
	cmd.Stdin = strings.NewReader(pass + "\n")
	out, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("docker login %s: %v: %s", host, err, strings.TrimSpace(string(out)))
	}
	return nil
}

DockerLoginRunner runs docker login; tests replace it.

View Source
var ErrNoLegacyConfig = errors.New("no auth-token-manager config found")

ErrNoLegacyConfig means there is no auth-token-manager config to import.

View Source
var ErrOutsideSandbox = errors.New("is outside the dev-mode sandbox")

ErrOutsideSandbox is returned for a dev-mode write that would land outside the sandbox root.

Functions

func Builtin

func Builtin() *tokens.Config

Builtin is the config used when none is found: GitHub for Composer, from $GH_TOKEN.

func Delegated

func Delegated(configText string, hosts []string) map[string]string

Delegated reports which of hosts docker reads from a credential store or helper given the config text, and the helper that holds each.

func DockerModes

func DockerModes() []string

DockerModes lists the valid modes.

func HasDockerDelegation

func HasDockerDelegation(set *tokens.Set, path string) bool

HasDockerDelegation reports whether the docker config at path delegates any of set's docker hosts, so a caller can ask for a mode before writing.

func LoadConfig

func LoadConfig(path string) (*tokens.Config, error)

LoadConfig reads a token config.

func ValidDockerMode

func ValidDockerMode(m string) bool

ValidDockerMode reports whether m is "", login or refuse.

Types

type AliasStatus

type AliasStatus struct {
	File    string
	Exists  bool
	Present bool
	Legacy  bool // an auth-token-manager block is still there
	Lines   []string
}

AliasStatus is what a startup file holds.

type CatalogSource

type CatalogSource struct {
	// Path is the file in use; "" means the embedded catalog.
	Path string
	// Var is the variable that named it.
	Var string
	// Legacy is set when Var is auth-token-manager's name for it.
	Legacy bool
	// Warning is set when a variable named a file that is not there; the
	// embedded catalog is used instead, as auth-token-manager did.
	Warning string
	// Err is set when the file is there but is not a usable catalog. The
	// embedded catalog stays in use; the token commands refuse to run.
	Err error
}

CatalogSource is the catalog a variable selected, if any.

type ConfigSource

type ConfigSource struct {
	Path string
	// Legacy is set for an auth-token-manager location; the caller prints a
	// deprecation notice pointing at `token import`.
	Legacy bool
}

ConfigSource is where a token config came from.

type DockerModeFunc

type DockerModeFunc func(dest string) (string, error)

DockerModeFunc settles the credential-store mode for docker's file at dest. The CLI asks a person; the web and the TUI's callers pass a fixed answer.

func FixedDockerMode

func FixedDockerMode(m string) DockerModeFunc

FixedDockerMode always answers m, refusing when m is empty.

type DockerResult

type DockerResult struct {
	Written  []string // inline auths merged into config.json
	LoggedIn []string // stored through docker login
	Skipped  []string // delegated and refused
	Warnings []string
}

DockerResult says what happened to each docker host.

type Env

type Env struct {
	Paths paths.Paths
	// Home is the directory the targets resolve under: the real home, or the
	// sandbox's home in dev mode.
	Home string
	// Cwd anchors ./authama-tokens.json and the .gitignore check.
	Cwd     string
	Windows bool
	MacOS   bool

	// Catalog says where the token catalog came from when a variable
	// replaced the embedded one; see CatalogEnv.
	Catalog CatalogSource
	// contains filtered or unexported fields
}

Env is where one invocation's token work happens.

func NewEnv

func NewEnv(p paths.Paths, getenv func(string) string, lookup tokens.Lookup, cwd string) Env

NewEnv builds the token environment for a resolved layout. getenv and lookup read the process environment when nil.

In dev mode the variables that relocate a target file (COMPOSER_HOME, DOCKER_CONFIG, NETRC, ...) are hidden, so a sandboxed run resolves every target under the sandbox no matter what the shell exports. Secret variables are still read: a sandbox needs real values to render.

func (Env) Checkup

func (e Env) Checkup(set *tokens.Set, project string) (out []Finding, failed bool)

Checkup reports, for each target (the set's, or all five), whether its file exists and is owner-only, that a present auth.json overrides $COMPOSER_AUTH, and whether .gitignore covers the files that hold secrets. failed is set when anything needs fixing.

func (Env) Find

func (e Env) Find(target, host, project string) (tokens.Installed, bool)

Find returns the installed credential for (target, host), secret included.

func (Env) FindConfig

func (e Env) FindConfig(explicit string) (ConfigSource, error)

FindConfig discovers the token config: --config, then $AUTHAMA_TOKENS_CONFIG, ./authama-tokens.json, <config home>/tokens.json, and then, deprecated, auth-token-manager's own locations. It returns a zero ConfigSource when there is none.

func (Env) Forget

func (e Env) Forget(target, host, project string) (bool, error)

Forget deletes the credential for host from target's file, rewritten atomically and owner-only. It reports whether host was there.

func (Env) Getenv

func (e Env) Getenv(k string) string

Getenv reads a variable as the targets see it (hidden ones are "").

func (Env) Guard

func (e Env) Guard(path string) error

Guard refuses, in dev mode, a write to a path outside the sandbox or to a symlink (which could point anywhere).

func (Env) HomeFiles

func (e Env) HomeFiles(fix bool) (out []Finding, failed bool, fixed []string)

HomeFiles is the doctor's view of the token files: each per-user target file that exists, and whether it is owner-only. With fix, a file that is not is chmod'ed to SecretMode first (inside the sandbox in dev mode), and fixed names it. The project-scoped .gitignore check stays in Checkup: the doctor runs from any directory and has no project to judge.

func (Env) Host

func (e Env) Host() tokens.Host

Host is the core view of where the targets live.

func (Env) Import

func (e Env) Import(from string, dryRun bool) (src, to string, err error)

Import copies a token config to <config home>/tokens.json: from, when given, else the first auth-token-manager config it finds, so the deprecated locations stop being read. The file is parsed first, so a broken one is refused rather than copied; the original is left in place, and an existing tokens.json is never overwritten. With dryRun nothing is written.

func (Env) Inventory

func (e Env) Inventory(project string, reveal bool) []tokens.Installed

Inventory lists every credential installed in the target files. Secrets are blanked unless reveal is set.

func (*Env) LoadEnvFile

func (e *Env) LoadEnvFile(path string) error

LoadEnvFile reads VAR=value lines into the lookup, without overriding variables that are already set, as --source did.

func (Env) Lookup

func (e Env) Lookup(k string) (string, bool)

Lookup reads a secret variable.

func (Env) Profile

func (e Env) Profile(file string, login bool) Profile

Profile picks the startup file: file when given, else the shell's rc (or login profile with login).

func (Env) ResolveConfig

func (e Env) ResolveConfig(explicit string) (cfg *tokens.Config, src ConfigSource, err error)

ResolveConfig finds the token config the way `token apply` does - explicit, then $AUTHAMA_TOKENS_CONFIG (or $SSH_MANAGER_TOKENS_CONFIG), ./authama-tokens.json, tokens.json in the config home, the legacy locations - and falls back to the built-in default. note says which of the last two it used, for the caller to show.

func (Env) SaveConfig

func (e Env) SaveConfig(path string, c *tokens.Config) error

SaveConfig writes a token config atomically.

func (Env) Scaffold

func (e Env) Scaffold(dest string, targets []string) (string, error)

Scaffold writes a starter config to dest, never over an existing file: the chosen targets' templates, or the all-targets example.

func (Env) SyncDocker

func (e Env) SyncDocker(set *tokens.Set, path, mode string) (DockerResult, error)

SyncDocker writes the docker entries. Hosts docker reads inline are merged into config.json; hosts delegated to a credential store follow mode, which must already be resolved to login or refuse.

func (Env) TargetPath

func (e Env) TargetPath(target, project string) string

TargetPath is the file a target reads, optionally scoped to a project.

func (Env) WriteAll

func (e Env) WriteAll(set *tokens.Set, targets []string, project string, mode DockerModeFunc, out, errw io.Writer) error

WriteAll writes each target's entries into its file, saying where on out and warning on errw. Every front end writes through here, so `token set`, `token apply`, the TUI and the web report a write the same way.

func (Env) WriteTarget

func (e Env) WriteTarget(set *tokens.Set, target, path string) ([]string, error)

WriteTarget merges set's entries for target into path. Docker hosts whose credentials are delegated to a credential store are not written here; see SyncDocker.

type Finding

type Finding struct {
	Level  string // ok | absent | warn | note
	Target string
	Text   string
}

Finding is one line of the token audit.

type Profile

type Profile struct {
	File string
	// contains filtered or unexported fields
}

Profile is a shell startup file the aliases go in.

func (Profile) Install

func (pr Profile) Install(command string, backup bool) (updated bool, warns []string, err error)

Install writes the block into the file (replacing an old one, and an auth-token-manager block). updated is set when a block was already there.

func (Profile) Remove

func (pr Profile) Remove(backup bool) (removed bool, err error)

Remove deletes the block from the file; removed is false when there was none.

func (Profile) Status

func (pr Profile) Status() AliasStatus

Status reports the alias block in the file.

Jump to

Keyboard shortcuts

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