certprovider

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 13 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 certprovider defines the CertificateProvider interface and related types used to abstract certificate issuance, renewal, and trust-root discovery away from any specific PKI implementation.

Index

Constants

This section is empty.

Variables

View Source
var ErrRenewalNotSupported = errors.New("certificate renewal not supported by this provider")

ErrRenewalNotSupported is returned by providers that do not support automated certificate renewal (e.g. the filesystem provider).

Functions

This section is empty.

Types

type AutoProvider

type AutoProvider struct {
	// CertsDir is the directory where CA and issued certs are stored.
	CertsDir string
	// CAName is the Common Name for the auto-generated CA certificate.
	CAName string
}

AutoProvider implements CertificateProvider using a locally generated self-signed CA. It wraps the existing pki.InitCA and pki.IssueCert functions. This provider is used when no external PKI is configured.

func NewAutoProvider

func NewAutoProvider(certsDir, caName string) (*AutoProvider, error)

NewAutoProvider creates an AutoProvider that stores certificate material in certsDir. The CA is created lazily on the first Enroll call if it does not already exist.

func (*AutoProvider) CACertPath

func (p *AutoProvider) CACertPath() string

CACertPath returns the path to the CA certificate.

func (*AutoProvider) CAKeyPath

func (p *AutoProvider) CAKeyPath() string

CAKeyPath returns the path to the CA private key. This is needed by callers that manage cross-signing or other CA-level operations.

func (*AutoProvider) Enroll

Enroll generates a certificate signed by the auto-generated CA. If the CA does not exist yet, it is created first.

func (*AutoProvider) Renew

func (p *AutoProvider) Renew(_ context.Context, identity *Identity) error

Renew re-issues a certificate from the local CA with the same CN and SANs but a fresh validity period.

func (*AutoProvider) Roots

func (p *AutoProvider) Roots(_ context.Context) ([]*x509.Certificate, error)

Roots returns the auto-generated CA certificate.

type CertRenewFunc

type CertRenewFunc func(cfg *StepCAConfig, certPath, keyPath string, logger *slog.Logger) error

CertRenewFunc is the signature for mTLS-based certificate renewal.

type CertRequestFunc

type CertRequestFunc func(cfg *StepCAConfig, sans []string, certPath, keyPath string, logger *slog.Logger) error

CertRequestFunc is the signature for functions that request a certificate from Step CA. It matches the pattern used by the localserver package's requestCertJWK and requestCertACME functions.

type CertRole

type CertRole string

CertRole indicates whether a certificate is intended for a server or client.

const (
	// RoleServer identifies a server (TLS listener) certificate.
	RoleServer CertRole = "server"
	// RoleClient identifies a client (TLS dialer) certificate.
	RoleClient CertRole = "client"
)

type CertificateProvider

type CertificateProvider interface {
	// Enroll obtains a new certificate from the provider. For providers
	// that manage their own CA (e.g. stepca), this performs a full CSR
	// flow. For passive providers (e.g. filesystem), this returns the
	// pre-configured identity without issuing anything new.
	Enroll(ctx context.Context, req EnrollmentRequest) (*Identity, error)

	// Renew replaces the certificate in identity with a freshly issued
	// one. Providers that do not support renewal return
	// ErrRenewalNotSupported.
	Renew(ctx context.Context, identity *Identity) error

	// Roots returns the trusted CA certificates used to verify
	// certificates issued by this provider.
	Roots(ctx context.Context) ([]*x509.Certificate, error)
}

CertificateProvider abstracts the source of X.509 certificate material. Implementations include a filesystem provider (static certs on disk), an auto provider (self-signed CA), and a Step CA provider (automated PKI).

func New

New constructs a CertificateProvider by name. Supported names are "filesystem", "auto", and "stepca".

type EnrollmentRequest

type EnrollmentRequest struct {
	// CommonName is the subject CN for the issued certificate.
	CommonName string
	// SANs are additional DNS names or IP addresses for the certificate.
	SANs []string
	// Token is an optional one-time enrollment credential.
	Token string
	// Validity is the requested certificate lifetime. Providers may
	// impose their own maximum.
	Validity time.Duration
	// Role selects whether to issue a server or client certificate.
	Role CertRole
	// OutDir is the directory where certificate and key files should be
	// written. Providers that write to disk use this path.
	OutDir string
}

EnrollmentRequest contains the parameters needed to request a certificate.

type FilesystemConfig

type FilesystemConfig struct {
	// CACertPath is the path to the CA certificate or bundle.
	CACertPath string
	// CertPath is the path to the end-entity certificate.
	CertPath string
	// KeyPath is the path to the private key matching CertPath.
	KeyPath string
}

FilesystemConfig holds paths to PEM-encoded certificate material on disk.

type FilesystemProvider

type FilesystemProvider struct {
	// contains filtered or unexported fields
}

FilesystemProvider implements CertificateProvider by reading pre-existing certificate material from the local filesystem. It does not issue or renew certificates — that responsibility belongs to an external PKI.

func NewFilesystemProvider

func NewFilesystemProvider(cfg FilesystemConfig) (*FilesystemProvider, error)

NewFilesystemProvider creates a FilesystemProvider and validates that the configured certificate material is present, well-formed, and consistent.

func (*FilesystemProvider) Enroll

Enroll returns the pre-configured identity. The filesystem provider does not perform actual enrollment — certificates are managed externally.

func (*FilesystemProvider) Renew

Renew returns ErrRenewalNotSupported. Filesystem certificates are managed by an external process.

func (*FilesystemProvider) Roots

Roots parses and returns the CA certificates from the configured CA bundle file.

type Identity

type Identity struct {
	// CertPath is the path to the PEM-encoded certificate (or chain).
	CertPath string
	// KeyPath is the path to the PEM-encoded private key.
	KeyPath string
	// CACertPath is the path to the CA bundle used to verify this
	// identity.
	CACertPath string
	// Provider is the name of the provider that issued this identity
	// (e.g. "filesystem", "auto", "stepca").
	Provider string
	// CommonName is the subject CN of the certificate.
	CommonName string
	// NotBefore is the start of the certificate validity window.
	NotBefore time.Time
	// NotAfter is the end of the certificate validity window.
	NotAfter time.Time
	// Renewable indicates whether the provider supports automatic
	// renewal for this identity.
	Renewable bool
}

Identity represents an issued certificate and its associated material.

func (*Identity) NeedsRenewal

func (id *Identity) NeedsRenewal() bool

NeedsRenewal reports whether the identity's certificate should be renewed. It returns true when less than one-third of the original validity period remains, matching the standard renewal heuristic.

type ProviderConfig

type ProviderConfig struct {
	// Filesystem provider configuration.
	Filesystem *FilesystemConfig

	// Auto provider configuration.
	AutoCertsDir string
	AutoCAName   string

	// Step CA provider configuration.
	StepCA *StepCAConfig
	Logger *slog.Logger
}

ProviderConfig holds configuration for constructing a CertificateProvider. Only the fields relevant to the selected provider name need to be set.

type StepCAConfig

type StepCAConfig struct {
	// URL is the Step CA server URL (e.g. "https://step-ca.internal:443").
	URL string
	// RootPath is the path to the Step CA root certificate.
	RootPath string
	// Provisioner is the provisioner name (e.g. "acme", "bridge-jwk").
	Provisioner string
	// ProvisionerPasswordFile is the path to the JWK provisioner password file.
	ProvisionerPasswordFile string
}

StepCAConfig holds the settings needed to communicate with a Step CA instance.

type StepCAProvider

type StepCAProvider struct {

	// Injectable functions for testability. When nil, operations that
	// require them return errors.
	RequestJWK  CertRequestFunc
	RequestACME CertRequestFunc
	RenewMTLS   CertRenewFunc
	// contains filtered or unexported fields
}

StepCAProvider implements CertificateProvider by delegating certificate operations to a Step CA instance. It uses injectable function variables so that tests can substitute mock implementations.

func NewStepCAProvider

func NewStepCAProvider(cfg StepCAConfig, logger *slog.Logger) (*StepCAProvider, error)

NewStepCAProvider creates a StepCAProvider with the given configuration. The request and renew functions must be injected by the caller — typically from the localserver package's exported function variables.

func (*StepCAProvider) Enroll

Enroll obtains a new certificate from Step CA using the configured provisioner (JWK or ACME).

func (*StepCAProvider) Renew

func (p *StepCAProvider) Renew(_ context.Context, identity *Identity) error

Renew attempts to renew the certificate using mTLS-based renewal first, then falls back to a full provisioner-based request if mTLS renewal fails.

func (*StepCAProvider) Roots

func (p *StepCAProvider) Roots(_ context.Context) ([]*x509.Certificate, error)

Roots returns the Step CA root certificate(s).

Jump to

Keyboard shortcuts

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