vault

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsForbidden

func IsForbidden(err error) bool

IsForbidden returns true if the error is a Vault 403 response, indicating the token is invalid, revoked, or lacks permissions.

func ReadSecondsField

func ReadSecondsField(data map[string]any, key string) (int64, bool)

ReadSecondsField extracts an integer-seconds value from a Vault secret data map, handling the json.Number / float64 / int variants the underlying decoder may produce. Returns (0, false) when the key is missing or the value isn't a parseable number.

Lives in internal/vault because every call site is reading a field off a *vaultapi.Secret's data map. Used by the lifecycle manager for ttl / creation_ttl and by the CLI login-check for the same fields; centralising avoids the duplicated type switch silently diverging (e.g. when a future Vault SDK change introduces a uint64 wire form).

Types

type Client

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

Client wraps the Vault API client.

func NewClient

func NewClient(cfg Config) (*Client, error)

NewClient creates a new Vault API client.

func (*Client) DeleteKVv2

func (c *Client) DeleteKVv2(ctx context.Context, mount, path string) error

DeleteKVv2 deletes all versions of a KVv2 secret (the full metadata record, not just a soft-delete of the latest version). Used by the refresh manager when the upstream credential has been permanently revoked and the local state needs to be wiped so the user can re-enrol.

func (*Client) EnableKVv2

func (c *Client) EnableKVv2(ctx context.Context, path string) error

EnableKVv2 enables a KVv2 secrets engine at the given path. Used for testing. Returns an error if it already exists (non-fatal).

func (*Client) IssueCertificate added in v0.23.0

func (c *Client) IssueCertificate(ctx context.Context, mount, role, commonName, ttl string) (*IssuedCert, error)

IssueCertificate asks the PKI engine to generate a keypair and certificate. Used only when the PKI role forbids the sign endpoint; the returned PrivateKeyPEM must be handed to the secure store immediately.

func (*Client) ListKVv2

func (c *Client) ListKVv2(ctx context.Context, mount, path string) ([]string, error)

ListKVv2 lists keys under the given path in a KVv2 mount.

func (*Client) LoginCert added in v0.23.0

func (c *Client) LoginCert(ctx context.Context, mount, role string) error

LoginCert authenticates via the TLS cert auth method. The client must have been built with a ClientCert so the certificate is presented during the handshake; Vault matches it against the registered CA and the named role's policies. On success the returned token is adopted onto the client.

func (*Client) LoginLDAP

func (c *Client) LoginLDAP(ctx context.Context, mount, username, password string) (*LoginResult, error)

LoginLDAP authenticates via LDAP and detects if MFA is required.

func (*Client) LookupSelf

func (c *Client) LookupSelf(ctx context.Context) (*vaultapi.Secret, error)

LookupSelf returns the current token's metadata, or an error if invalid.

func (*Client) Raw

func (c *Client) Raw() *vaultapi.Client

Raw returns the underlying Vault API client for direct access.

func (*Client) ReadKVv2

func (c *Client) ReadKVv2(ctx context.Context, mount, path string) (*Secret, error)

ReadKVv2 reads a KVv2 secret at the given mount and path. Returns nil (not error) if the secret doesn't exist.

func (*Client) RenewSelf

func (c *Client) RenewSelf(ctx context.Context, increment int) (*vaultapi.Secret, error)

RenewSelf renews the current token.

func (*Client) ServerHealth

func (c *Client) ServerHealth(ctx context.Context) (*HealthResponse, error)

ServerHealth returns the Vault server health status.

func (*Client) SetToken

func (c *Client) SetToken(token string)

SetToken sets the auth token on the client.

func (*Client) SignCSR added in v0.23.0

func (c *Client) SignCSR(ctx context.Context, mount, role, csrPEM, commonName, ttl string) (*IssuedCert, error)

SignCSR submits a CSR to the PKI engine's sign endpoint and returns the signed certificate. The private key behind the CSR never leaves the host — this is the preferred issuance path for both mtls and mtls+tpm.

func (*Client) SubscribeEvents

func (c *Client) SubscribeEvents(ctx context.Context, eventType string) (<-chan Event, <-chan error, error)

SubscribeEvents connects to the Vault Events API via WebSocket and returns a channel of events and an error channel. The caller should cancel the context to disconnect.

func (*Client) Token

func (c *Client) Token() string

Token returns the current auth token.

func (*Client) ValidateMFA

func (c *Client) ValidateMFA(ctx context.Context, mfaRequestID, methodID, passcode string) (string, error)

ValidateMFA validates an MFA challenge. For push methods (Duo), pass an empty passcode — the call blocks until the user approves or the context is cancelled. For TOTP, pass the user-provided code. Returns the authenticated client token on success.

func (*Client) WriteKVv2

func (c *Client) WriteKVv2(ctx context.Context, mount, path string, data map[string]any) error

WriteKVv2 writes data to a KVv2 secret. Used for testing/seeding.

type Config

type Config struct {
	Address       string
	Token         string
	CACert        string
	TLSSkipVerify bool
	// ClientCert, when non-nil, is presented during the TLS handshake. It is
	// used by the cert auth method (mtls / mtls+tpm): the certificate's
	// private key may be a hardware-backed crypto.Signer, so GetClientCertificate
	// invokes it lazily per handshake rather than holding key bytes.
	ClientCert *tls.Certificate
}

Config holds Vault connection settings.

type Event

type Event struct {
	EventType string
	Path      string
	DataPath  string
	MountPath string
	Version   int
}

Event represents a Vault event notification.

type HealthResponse

type HealthResponse struct {
	Version     string
	Enterprise  bool
	ClusterName string
}

HealthResponse contains selected fields from the Vault health endpoint.

type IssuedCert added in v0.23.0

type IssuedCert struct {
	// CertPEM is the leaf certificate followed by any CA chain, PEM-encoded.
	CertPEM string
	// PrivateKeyPEM is populated only by IssueCertificate (Vault generates the
	// key). SignCSR leaves it empty because the key never left the host.
	PrivateKeyPEM string
	// Serial is the certificate serial number, for audit/revocation.
	Serial string
}

IssuedCert is the result of a PKI issue or sign operation.

type LoginResult

type LoginResult struct {
	Token        string
	MFARequired  bool
	MFARequestID string
	MFAMethods   []MFAMethod
}

LoginResult holds the outcome of an LDAP login attempt.

type MFAMethod

type MFAMethod struct {
	ID           string `json:"id"`
	Type         string `json:"type"`
	UsesPasscode bool   `json:"uses_passcode"`
}

MFAMethod describes an MFA method required for authentication.

type Secret

type Secret struct {
	Data    map[string]any
	Version int
}

Secret represents a KVv2 secret with its data and version metadata.

Jump to

Keyboard shortcuts

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