catrust

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 12 Imported by: ? 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 catrust manages the host's system-wide CA trust anchors through an injected exec.Runner (plus the fs.Manager for the privileged file writes).

r, _ := exec.NewRunner(exec.Direct) // writing trust anchors needs root
m, err := catrust.New(catrust.CaCertificates, r)
if err != nil { ... }
err = m.Install(ctx, "acme-corp-root", pemBytes) // every TLS client now trusts it

This installs/removes an operator-supplied CA into the system trust store — the mechanism for distributing a private/internal CA across a fleet so devices trust internal services or a TLS-inspecting proxy. It is a high-trust, privilege-amplifying operation: a CA the host trusts can transparently MITM any TLS connection, so Install validates the certificate (well-formed, IsCA, currently valid) and the name (safe filename) BEFORE writing. Authorization of the request itself is the caller's concern (the agent gates it with a CA-signed action, as for other sensitive ops).

Three backends: CaCertificates (Debian/Ubuntu update-ca-certificates), P11Kit (Fedora/RHEL/EL/Arch update-ca-trust), and SuseCaCertificates (openSUSE/SLES, which ships a same-named update-ca-certificates but with different paths). Distrusting a distro-shipped root (a separate blocklist mechanism) is out of scope — this manages anchors WE add.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidCert = errors.New("catrust: invalid CA certificate")

ErrInvalidCert is returned when the supplied certificate is not a usable CA.

View Source
var ErrInvalidName = errors.New("catrust: invalid anchor name")

ErrInvalidName is returned when an anchor name is unsafe as a filename.

View Source
var ErrUnknownBackend = errors.New("catrust: unknown backend")

ErrUnknownBackend is returned by New for the zero value or any unimplemented backend.

Functions

This section is empty.

Types

type Anchor

type Anchor struct {
	Name    string // the operator-chosen name (the anchor's filename without .crt)
	Subject string // certificate subject DN
	Issuer  string // certificate issuer DN
}

Anchor is an installed trust anchor this package manages.

type Backend

type Backend int

Backend selects the trust-store mechanism. Zero is invalid; only implemented backends exist.

const (
	// CaCertificates is the Debian/Ubuntu update-ca-certificates flow
	// (anchors in /usr/local/share/ca-certificates).
	CaCertificates Backend = iota + 1
	// P11Kit is the Fedora/RHEL/EL/Arch p11-kit update-ca-trust flow
	// (anchors in /etc/pki/ca-trust/source/anchors).
	P11Kit
	// SuseCaCertificates is the openSUSE/SLES update-ca-certificates flow. SUSE
	// ships a tool of the SAME name as Debian's but reads anchors from
	// /etc/pki/trust/anchors, so it needs its own backend (Detect disambiguates
	// the two by which anchors dir exists).
	SuseCaCertificates
)

func Detect

func Detect(ctx context.Context) []Backend

Detect reports the trust-store backends usable on THIS host: CaCertificates when update-ca-certificates is on PATH, P11Kit when update-ca-trust is. It lists; the consumer picks one and passes it to New.

The ctx is accepted for signature uniformity; the probe is a pure PATH lookup.

func (Backend) String

func (b Backend) String() string

String renders the backend as its canonical name.

type Manager

type Manager interface {
	// Install adds (or replaces) a CA anchor under name. certPEM must be a single
	// PEM CA certificate; it is validated before being written.
	Install(ctx context.Context, name string, certPEM []byte) error
	// Remove deletes the anchor previously installed under name and refreshes the
	// trust store. It is idempotent: removing an absent name is a no-op success.
	Remove(ctx context.Context, name string) error
	// List reports the anchors this package manages (not the full system bundle).
	List(ctx context.Context) ([]Anchor, error)
}

Manager is the CA-trust surface.

func New

func New(b Backend, runner exec.Runner) (Manager, error)

New returns a Manager for the named backend. Pure: validates the backend; nil runner and unknown backend are rejected.

Example

ExampleNew installs an org CA into the system trust store, then lists the managed anchors.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/manchtools/power-manage-sdk/sys/catrust"
	"github.com/manchtools/power-manage-sdk/sys/exec"
)

func main() {
	// Writing trust anchors requires root: exec.Direct assumes the process is
	// already root; from an unprivileged process use exec.NewRunner(exec.Sudo)
	// (or exec.Doas) so the writes and update-ca-trust refresh are escalated.
	r, err := exec.NewRunner(exec.Direct)
	if err != nil {
		log.Fatal(err)
	}
	m, err := catrust.New(catrust.CaCertificates, r)
	if err != nil {
		log.Fatal(err)
	}
	pem, err := os.ReadFile("/path/to/acme-root.pem")
	if err != nil {
		log.Fatal(err)
	}
	if err := m.Install(context.Background(), "acme-corp-root", pem); err != nil {
		log.Fatal(err)
	}
	anchors, err := m.List(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	for _, a := range anchors {
		fmt.Printf("%s: %s\n", a.Name, a.Subject)
	}
}

Jump to

Keyboard shortcuts

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