infrastructure

package
v4.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 21 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

Index

Constants

View Source
const DefaultCLITimeout = 30 * time.Second

DefaultCLITimeout bounds how long a forge's CLI, together with the Azure tenant lookup before it, may take to hand over a token. Issuing one can involve a network round trip (az contacts Microsoft Entra), and an unreachable endpoint must not hang the caller.

Variables

View Source
var ErrRelativeExecutable = errors.New("refusing to run an executable found at a relative path")

ErrRelativeExecutable reports a CLI that PATH lookup resolved to a relative path, which would run a program from the current directory.

View Source
var ErrTenantDiscovery = errors.New("tenant discovery failed")

ErrTenantDiscovery reports that Azure DevOps did not answer the tenant lookup with a usable tenant.

Functions

func CheckGitLabCIJobTokenServer added in v4.8.0

func CheckGitLabCIJobTokenServer(lookup func(string) string) error

CheckGitLabCIJobTokenServer returns nil when a GitLab CI job's CI_JOB_TOKEN may be sent to gitlab.com: GitLabCIServerHost says the job runs there. Otherwise it returns an error matching entities.ErrNoCredential that says why not: the job runs on another server, whose token gitlab.com must not receive, or no CI variable names the server at all. A nil lookup reads the process environment.

func CheckGitLabTokenServer added in v4.8.0

func CheckGitLabTokenServer(lookup func(string) string, tokenVariable string) error

CheckGitLabTokenServer returns an error matching entities.ErrNoCredential when a variable that names a GitLab server names one other than gitlab.com, or holds a value it cannot read: the GitLab token in tokenVariable then belongs to that server and must not be sent to gitlab.com. The variables are those of glab, semantic-release, Lerna, Terraform's gitlab provider and a GitLab CI job (see the README). It returns nil when none of them does. EnvCredentialResolver applies it to GITLAB_TOKEN and GL_TOKEN; a consumer applies it to another GitLab token it exported itself. A GitLab CI job's own CI_JOB_TOKEN is judged by CheckGitLabCIJobTokenServer instead. A nil lookup reads the process environment.

func GitLabCIServerHost added in v4.8.0

func GitLabCIServerHost(lookup func(string) string) (string, error)

GitLabCIServerHost returns the host of the GitLab server a GitLab CI job runs on, read from CI_SERVER_URL, CI_SERVER_HOST, CI_SERVER_FQDN and CI_API_V4_URL: that server issued the job's CI_JOB_TOKEN and is the only one that accepts it. Other variables that name a GitLab server, such as GITLAB_HOST, describe other tokens and are not read. It fails, with an error matching entities.ErrNoCredential, when none of the four is set (the run is no GitLab CI job), when one cannot be read, or when two name different hosts. A nil lookup reads the process environment.

Types

type CLICredentialResolver

type CLICredentialResolver struct {
	// Runner finds and runs the CLI. Nil means an ExecCLIRunner.
	Runner entities.CLIRunner
	// TenantResolver finds an Azure DevOps organization's tenant. Nil means an
	// HTTPAzureTenantResolver.
	TenantResolver entities.AzureTenantResolver
	// Timeout bounds the tenant lookup and the CLI together. Zero means
	// DefaultCLITimeout.
	Timeout time.Duration
}

CLICredentialResolver asks a forge's own CLI for a token. Someone who has run `gh auth login` or `az login` is already authenticated for that forge, so requiring a separately exported token asks them to prove the same thing twice.

func NewCLICredentialResolver

func NewCLICredentialResolver() *CLICredentialResolver

NewCLICredentialResolver creates a resolver that runs the real gh and az found on PATH.

func (*CLICredentialResolver) Resolve

Resolve asks the forge's CLI for a token: `gh auth token --hostname github.com` for GitHub, and `az account get-access-token` for the Azure DevOps resource, in the organization's Microsoft Entra tenant when the request names an organization.

type ChainCredentialResolver

type ChainCredentialResolver struct {
	Resolvers []entities.CredentialResolver
}

ChainCredentialResolver tries each resolver in order and returns the first credential one supplies. When every resolver declines, its error names the forge and lists every reason, so the user sees each way of authenticating rather than only the last one that failed.

func NewChainCredentialResolver

func NewChainCredentialResolver(resolvers ...entities.CredentialResolver) *ChainCredentialResolver

NewChainCredentialResolver creates a chain over the given resolvers, in priority order.

func NewDefaultCredentialResolver

func NewDefaultCredentialResolver() *ChainCredentialResolver

NewDefaultCredentialResolver returns the standard chain: the forge's token environment variables first, so an explicitly exported token always overrides whichever account a CLI happens to be signed in to, then the forge's own CLI (gh for GitHub, az for Azure DevOps).

Use it only after the caller's own configured token, if any, came up empty. The token it returns belongs to the operator running the program, so send it only to the forge it was issued for: gitforge's providers call fixed API hosts (api.github.com, dev.azure.com), which is safe, while a git remote's own host is not. The providers' MatchesURL now takes an https:// remote for a forge only on the forge's own hosts, but it still accepts a plaintext http:// remote there, which would carry the token unencrypted.

func (*ChainCredentialResolver) Resolve

Resolve returns the first credential with a token that a resolver supplies.

type EnvCredentialResolver

type EnvCredentialResolver struct {
	// Lookup reads one variable. Nil means os.Getenv; tests set it instead of changing
	// the process environment, so they can run in parallel.
	Lookup func(name string) string
}

EnvCredentialResolver reads a forge's token from the environment variables gitforge knows for it (configHelpers.TokenEnvVars), in priority order: GH_TOKEN then GITHUB_TOKEN (gh's own precedence, so a token gh would use wins here too), GITLAB_TOKEN then GL_TOKEN, AZURE_DEVOPS_EXT_PAT then SYSTEM_ACCESSTOKEN, and CODEBERG_TOKEN.

func NewEnvCredentialResolver

func NewEnvCredentialResolver() *EnvCredentialResolver

NewEnvCredentialResolver creates a resolver that reads the process environment.

func (*EnvCredentialResolver) Resolve

Resolve returns the first non-empty variable's value, trimmed, with the variable's name as the source. It declines that token instead, with an error matching entities.ErrNoCredential, when a variable of tokenServerVariables says it belongs to another server: a GitHub token while GITHUB_SERVER_URL names a host other than github.com or GITHUB_API_URL one other than api.github.com, and a GitLab token while a GitLab server variable names a host other than gitlab.com.

type ExecCLIRunner

type ExecCLIRunner struct {
	// Dir is the directory the CLI runs in. Empty means the user's home directory, or the
	// filesystem root when there is none, and never the caller's own directory: the
	// consumers work inside a repository they were pointed at, and a CLI such as a
	// pip-installed az puts its working directory on Python's module path, so a file in
	// that checkout would run as the user while their credentials are at hand.
	Dir string
}

ExecCLIRunner finds forge CLIs on PATH and runs them as child processes, with the environment of this process minus the variables that hold a forge token (see cliEnvironment), so a CLI reports only the login it stored itself.

func NewExecCLIRunner

func NewExecCLIRunner() *ExecCLIRunner

NewExecCLIRunner creates a runner that runs CLIs from the user's home directory.

func (*ExecCLIRunner) LookPath

func (r *ExecCLIRunner) LookPath(name string) (string, error)

LookPath returns the absolute path PATH lookup finds for name.

func (*ExecCLIRunner) Output

func (r *ExecCLIRunner) Output(ctx context.Context, path string, args ...string) (string, error)

Output runs the executable at path and returns its trimmed standard output. On failure the error reports standard error only, because standard output is where a CLI prints the token. The CLI gets this process's environment without the forge token variables.

On Unix the CLI runs in a process group of its own, so that the context's end can kill everything it started, and so a terminal's interrupt no longer reaches it. While it runs, Output therefore relays SIGINT, SIGTERM and SIGHUP from this process to the CLI's group; once the CLI has exited it stops relaying and, if one of them arrived, sends the first to this process again, which then ends, or runs its own handler, as it would have without the separate group. A program with its own handler for that signal receives it twice: once while the CLI runs and once more after it exits.

type HTTPAzureTenantResolver

type HTTPAzureTenantResolver struct {
	// BaseURL replaces https://dev.azure.com, for a local test server.
	BaseURL string
	// Client sends the request. Nil means a client with DefaultCLITimeout.
	Client *http.Client
}

HTTPAzureTenantResolver reads the tenant of an Azure DevOps organization from its public connectionData endpoint. The request carries no credential: Azure DevOps returns the tenant on the authentication challenge itself.

func NewHTTPAzureTenantResolver

func NewHTTPAzureTenantResolver() *HTTPAzureTenantResolver

NewHTTPAzureTenantResolver creates a resolver that asks https://dev.azure.com.

func (*HTTPAzureTenantResolver) GetTenant

func (r *HTTPAzureTenantResolver) GetTenant(ctx context.Context, organization string) (string, error)

GetTenant returns the organization's Microsoft Entra tenant ID, or "" when the organization has none.

type ProviderCLI

type ProviderCLI struct {
	Binary    string
	TokenArgs []string
	LoginHint string
}

ProviderCLI describes the command-line tool that already holds a user's credentials for a forge: the executable to look for on PATH, the arguments that print a token on standard output, and the command that signs the user in when it cannot.

func ProviderCLIFor

func ProviderCLIFor(serviceType globalEntities.ServiceType) (ProviderCLI, bool)

ProviderCLIFor returns the CLI registered for a forge, and whether there is one. The returned TokenArgs are a copy.

Jump to

Keyboard shortcuts

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