vault

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 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 vault is the store of secrets that the controller resolves a secret://<vault>/<key> reference from. A vault has a name and a type. A type registers with its name, a constructor and the Go struct of its config block, so a different store, such as HashiCorp Vault, is a type that plugs in. osapi ships one type, nats, in package natsvault.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("secret not found")

ErrNotFound is the error of a key that a vault does not have.

Functions

func Actor

func Actor(
	ctx context.Context,
) string

Actor is who WithActor put in ctx, or "".

func CheckKey

func CheckKey(
	key string,
) error

CheckKey is an error when key is not a name that a vault takes: 1 to 128 letters, digits, - or _, such as registry-token.

func IsReference

func IsReference(
	s string,
) bool

IsReference reports whether s starts with secret.Scheme. A value that does is a reference, and never a literal value.

func Unregister

func Unregister(
	name string,
)

Unregister removes the type name. A test that registers a type removes it with this.

func WithActor

func WithActor(
	ctx context.Context,
	who string,
) context.Context

WithActor is ctx with who, the subject of the caller that changes a secret. Put records it as the By of the new version.

Types

type Env

type Env struct {
	NATS *natsclient.Client
	// Bucket is the KV bucket of nats.secrets.bucket, with the namespace,
	// such as osapi-secrets. Empty when the config names none.
	Bucket string
	FS     avfs.VFS
	// LookupEnv reads an environment variable, as os.LookupEnv does.
	LookupEnv func(key string) (string, bool)
}

Env is what the controller gives each type when it opens a vault: the NATS connection, the secrets bucket with its namespace, the file system and the environment.

type Meta

type Meta struct {
	// Key is the name of the key in its vault, such as registry-token.
	Key string `json:"key"`
	// Version is 1 for the first value of the key, and one more for each
	// Put after it.
	Version uint64 `json:"version"`
	// Changed is when the last Put occurred.
	Changed time.Time `json:"changed"`
	// By is who did the last Put, from Actor. Empty when it is not known.
	By string `json:"by,omitempty"`
}

Meta is what a vault tells about a key, without its value.

type Reference

type Reference struct {
	Vault string
	Key   string
}

Reference names a key in a vault: secret://<vault>/<key>.

func ParseReference

func ParseReference(
	s string,
) (Reference, error)

ParseReference is the reference s: the name of a vault, then its key.

func (Reference) String

func (r Reference) String() string

String is the reference as a config value or an input gives it.

type Set

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

Set is the vaults of the controller, by name, each with its spec.

func Open

func Open(
	ctx context.Context,
	env Env,
	specs []Spec,
) (*Set, error)

Open makes a vault of each spec, from its registered type, in the order of specs. A spec with a type that is not registered, a config block that does not decode, a check that fails or a vault that does not open is an error that names the vault.

func (*Set) Get

func (s *Set) Get(
	name string,
) (Vault, bool)

Get is the vault name.

func (*Set) Names

func (s *Set) Names() []string

Names is the name of each vault, in the order of the config.

func (*Set) Resolve

func (s *Set) Resolve(
	ctx context.Context,
	ref Reference,
) ([]byte, error)

Resolve is the value of the key that ref names, from the vault of s that ref names. An error names the reference, and never a value.

func (*Set) Spec

func (s *Set) Spec(
	name string,
) (Spec, bool)

Spec is the spec of the vault name, with its type and its targets.

type Spec

type Spec struct {
	Name    string         `` /* 155-byte string literal not displayed */
	Type    string         `mapstructure:"type"     validate:"required"                      doc:"The type of the vault, such as nats."`
	Targets []string       `` /* 163-byte string literal not displayed */
	KeyFile string         `` /* 189-byte string literal not displayed */
	KeyEnv  string         `` /* 175-byte string literal not displayed */
	Config  map[string]any `` /* 135-byte string literal not displayed */
}

Spec is one vault of controller.secrets.vaults: its name, its type, the targets that its secrets can go to, the key of a type that encrypts, and the config block of its type.

type Type

type Type struct {
	// Name is what the type of a vault names, such as nats.
	Name string
	// Doc says what the type is, in one line.
	Doc string
	// Config is the struct of the config block. Each field has a
	// mapstructure name, a doc tag and its validate rules.
	Config reflect.Type
	// contains filtered or unexported fields
}

Type is a kind of vault, such as nats: its name, what it is, the Go struct of its config block, and how to make a vault of it. Define makes one, and Register adds it to the types that the controller and the config check know.

func Define

func Define[C any](
	name string,
	doc string,
	check func(spec Spec) error,
	build func(ctx context.Context, env Env, spec Spec, cfg C) (Vault, error),
) Type

Define is a type of vault: name, what it is in one line, and build, which makes a vault from its spec and its config block as a C. check, when it is not nil, checks the spec of a vault of the type, such as that it names its key. The config check runs it, and Open runs it before build.

func Lookup

func Lookup(
	name string,
) (Type, bool)

Lookup is the registered type name.

func Register

func Register(
	t Type,
) Type

Register adds t to the types that the controller and the config check know, and returns it. Two types with one name are a mistake in the code, so it panics.

func TypeOf

func TypeOf(
	spec Spec,
) (Type, error)

TypeOf is the registered type of spec, or an error that names the types that there are.

func Types

func Types() []Type

Types is each registered type, sorted by name.

func (Type) Check

func (t Type) Check(
	spec Spec,
) error

Check is the check of the type on spec, or nil for a type with none.

func (Type) Decode

func (t Type) Decode(
	spec Spec,
) (any, error)

Decode is the config block of spec as the struct of t: a pointer to a new value of t.Config. A key that the struct does not have is an error, so a typo does not pass.

type Vault

type Vault interface {
	// Name is the name of the vault in the config, such as default.
	Name() string
	// Get is the value of key and its metadata. A key that is not there
	// is ErrNotFound.
	Get(ctx context.Context, key string) ([]byte, Meta, error)
	// Put writes value as the next version of key. Actor(ctx) is who
	// changed it.
	Put(ctx context.Context, key string, value []byte) (Meta, error)
	// Delete deletes key. A key that is not there is ErrNotFound.
	Delete(ctx context.Context, key string) error
	// List is the metadata of each key, sorted by key. It has no value.
	List(ctx context.Context) ([]Meta, error)
}

Vault is a store of secrets, by key. A type, such as nats, makes it from the config of the vault. The controller resolves each reference through one. A value is []byte until secret.Value replaces it (osapi #1240).

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.
Package natsvault is the nats type of vault, the one that osapi ships.
Package natsvault is the nats type of vault, the one that osapi ships.
mocks
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

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