trust

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommonName

func CommonName(certPEM []byte) (string, error)

CommonName returns the Subject CN of the first certificate in the PEM bytes.

func ExportDir

func ExportDir(baseDir, instanceName, projectPath string) string

ExportDir returns the per-project directory where exported cert and key files live. The directory is keyed by VM instance name (which defaults to the main site key) plus a short hash of the absolute project path so two forks of the same project don't collide.

func Fingerprint

func Fingerprint(certPEM []byte) (string, error)

Fingerprint returns the hex-encoded SHA-256 fingerprint of the DER bytes of the first certificate in a PEM-encoded byte slice.

func FormatLocation

func FormatLocation(loc string) string

FormatLocation maps an internal location identifier to a human-readable label suitable for CLI output. Internal forms like "nss:/Users/.../Library/Application Support/Firefox/Profiles/abc.default" become "Firefox (abc.default)".

func Label

func Label(projectPath, siteName string) string

Label returns the label used to identify a site's cert in trust stores (macOS keychain via cert subject, NSS nicknames, Linux user CA filenames). Format: trellis-<projectID>-<site>.

func ProjectID

func ProjectID(projectPath string) string

ProjectID is a short stable identifier derived from the absolute project path. It scopes trust labels and filenames so two projects with the same site name don't collide in the user's keychain or NSS DBs.

func StatePath

func StatePath() string

Types

type Entry

type Entry struct {
	Project     string    `json:"project"`
	Site        string    `json:"site"`
	Fingerprint string    `json:"fingerprint"`
	CommonName  string    `json:"common_name,omitempty"`
	CertPath    string    `json:"cert_path"`
	KeyPath     string    `json:"key_path,omitempty"`
	Label       string    `json:"label"`
	Locations   []string  `json:"locations"`
	AddedAt     time.Time `json:"added_at"`
}

Entry records what a `vm trust` invocation added so that a later `vm untrust` can reverse exactly the same set, even when the cert on disk has since changed.

type NSSStatus

type NSSStatus struct {
	FirefoxFound    bool
	CertutilMissing bool
}

NSSStatus reports the outcome of the Firefox/NSS trust attempt so callers can decide whether to print install hints.

type Options

type Options struct {
	// TrustSystem (Linux) toggles writing to /usr/local/share/ca-certificates
	// and running `sudo update-ca-certificates`. Off by default because it
	// requires sudo.
	TrustSystem bool
	// DisableNSS skips Firefox NSS profile updates even when certutil is
	// available. Mostly useful for tests.
	DisableNSS bool
}

Options influence how Default constructs the platform store.

type RevokeOutcome

type RevokeOutcome struct {
	Site    string
	Cleaned []string
	Err     error
	ErrHint string
}

RevokeOutcome is the per-site result of RevokeSite.

func RevokeSite

func RevokeSite(store Store, state *State, project string, entry Entry) RevokeOutcome

RevokeSite removes one site's trust entry from the host stores and drops its exported cert+key files. State is updated in place on success; callers are responsible for State.Save() at the end of a batch.

type SiteInput

type SiteInput struct {
	Project      string
	Site         string
	InstanceName string
	BaseDir      string
	CertPEM      []byte
	// KeyPEM, when non-empty, is written to <ExportDir>/<Site>.key with 0o600.
	// When empty, any previously exported key file at that path is removed.
	KeyPEM []byte
}

SiteInput describes a single site's cert (and optionally key) bytes that ApplySite should export to disk and add to the host's trust stores.

type SiteOutcome

type SiteOutcome struct {
	Site        string
	Verb        string
	Locations   []string
	NSS         NSSStatus
	KeyExported bool
	Err         error
	ErrHint     string
}

SiteOutcome is the per-site result of ApplySite. The cmd layer maps Verb to a one-line user-facing message and surfaces Err / ErrHint on failure.

func ApplySite

func ApplySite(store Store, state *State, in SiteInput) SiteOutcome

ApplySite exports the cert and key (if provided) for a single site to the project's export dir, then trusts the cert in the host's stores. State is updated in place; callers are responsible for State.Save() at the end of a batch.

On a fingerprint match it verifies the live trust setting and re-trusts only if drift is detected. On a fingerprint change it removes the previous trust entries before adding the new one.

type State

type State struct {
	Entries []Entry `json:"entries"`
}

func Load

func Load() (*State, error)

func (*State) EntriesForProject

func (s *State) EntriesForProject(project string) []Entry

EntriesForProject returns entries belonging to a project, sorted by site.

func (*State) Find

func (s *State) Find(project, site string) *Entry

Find returns the entry matching project + site, or nil if absent.

func (*State) Remove

func (s *State) Remove(project, site string)

Remove drops the entry for project+site (no-op if absent).

func (*State) Save

func (s *State) Save() error

func (*State) Upsert

func (s *State) Upsert(entry Entry)

Upsert replaces an existing project+site entry or appends a new one.

type Store

type Store interface {
	// Trust adds the cert. Idempotent: re-running against an
	// already-trusted cert should succeed without error.
	Trust(input TrustInput) (TrustResult, error)
	// Untrust removes the cert from the locations recorded at trust time.
	// It returns the list of locations actually cleaned. Missing entries
	// are skipped silently.
	Untrust(input TrustInput, locations []string) ([]string, error)
	// Verify classifies each recorded location as present, missing, or
	// unknown. Used to detect drift between recorded state and reality
	// (e.g. user manually deleted from keychain).
	Verify(input TrustInput, locations []string) (VerifyResult, error)
}

Store applies trust changes for the cert across the platform's trust stores. Each implementation may write to multiple underlying stores (system keychain + NSS, etc.); TrustResult captures all of them so the caller can record state and surface NSS hints.

func Default

func Default(opts Options) (Store, error)

Default returns the trust store appropriate for the current host.

type TrustInput

type TrustInput struct {
	// CertPath is an absolute path to the PEM-encoded certificate already
	// present on disk. Stores that need a file (e.g. macOS `security`,
	// `certutil -i`) read it from here.
	CertPath string
	// CertPEM is the same content as the file at CertPath. Provided so
	// callers don't have to re-read the file.
	CertPEM []byte
	// Fingerprint is the hex-encoded SHA-256 of the DER bytes; used as a
	// stable identifier for state tracking and verification.
	Fingerprint string
	// Label is a human-readable name used for keychain entries and NSS
	// nicknames, e.g. "trellis: example.com".
	Label string
}

TrustInput describes a cert that should be added to the host's trust stores.

type TrustResult

type TrustResult struct {
	Locations []string
	NSS       NSSStatus
}

TrustResult bundles what a Trust call did across all underlying stores.

type VerifyResult

type VerifyResult struct {
	Present []string
	Missing []string
	Unknown []string
}

VerifyResult classifies each recorded location as present, missing, or unknown. Unknown means the verifier couldn't determine the state (e.g. certutil isn't on PATH so NSS can't be queried) and should be treated as not-yet-broken so we don't accidentally re-trust and lose state.

func (VerifyResult) AllAccounted

func (v VerifyResult) AllAccounted(locations []string) bool

AllAccounted reports whether every recorded location was either confirmed present or could not be checked. If anything is Missing, the caller should treat the entry as drifted and re-trust.

Jump to

Keyboard shortcuts

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