sopsenc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package sopsenc encrypts a repository's secret files for the age recipients of the matching creation rule of its .sops.yaml. It holds public keys only: there is no private key and no decryption code path anywhere in the package (sopsenc's tests assert this for the whole module). Secret values it generates exist only in the encrypted output; the package logs nothing.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRule is returned when no creation rule of the .sops.yaml matches a
	// secret file's path; such a file is never committed in plaintext.
	ErrNoRule = errors.New("no creation rule matches")
	// ErrNoRecipients is returned for a creation rule without age recipients;
	// age public keys are the only key type the package encrypts for.
	ErrNoRecipients = errors.New("creation rule has no age recipients")
)
View Source
var (
	// ErrDuplicatePath is returned when a fileset names one path twice.
	ErrDuplicatePath = errors.New("duplicate file path")
	// ErrGeneratedInPlainFile is returned when a file that is not a secret file
	// declares generated values: a generated value only lives encrypted.
	ErrGeneratedInPlainFile = errors.New("generated values are only allowed in secret files")
	// ErrValueFrozen is returned when a new secret file shares a generated
	// value with a secret file that already exists in the repository. The
	// existing value cannot be reproduced without decryption, so the caller
	// has to reference the existing Secret instead.
	ErrValueFrozen = errors.New("generated value exists in an encrypted file already in the repository")
)
View Source
var ErrInvalidGenerated = errors.New("invalid generated value declaration")

ErrInvalidGenerated is returned for a Generated declaration that is incomplete, inconsistent across files, or whose placeholder is absent from the file it is declared on.

Functions

func IsSecretFile

func IsSecretFile(p string) bool

IsSecretFile reports whether a path names a secret file by the convention the .sops.yaml creation rules follow: a base name containing "secret" or "credential".

Types

type Config

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

Config is a repository's parsed .sops.yaml.

func ParseConfig

func ParseConfig(sopsYAML []byte) (Config, error)

ParseConfig parses a .sops.yaml. Every rule's path_regex must compile and every rule must name at least one valid age recipient.

func (Config) Rule

func (c Config) Rule(path string) (Rule, error)

Rule returns the first creation rule whose path_regex matches the repository-relative path.

type Encryptor

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

Encryptor encrypts secret files for one repository's .sops.yaml.

func New

func New(sopsYAML []byte) (*Encryptor, error)

New parses the repository's .sops.yaml.

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt(files []File, exists func(path string) bool) (map[string][]byte, error)

Encrypt returns the fileset ready to commit, keyed by repository-relative path. Files that are not secret files pass through unchanged. A secret file that exists in the repository (exists reports its path) is left as it is and absent from the result: it is never re-generated. Every other secret file has its generated values filled in — one value per name across all files — and is encrypted for the recipients of its creation rule.

type File

type File struct {
	// Path is repository-relative, the path .sops.yaml's path_regex sees.
	Path    string
	Content []byte
	// Generated declares the placeholders in Content that receive generated
	// values. Only a secret file may declare them.
	Generated []Generated
}

File is one rendered file of a repository's fileset.

type Generated

type Generated struct {
	Name        string
	Placeholder string
	Kind        Kind
	Length      int
}

Generated declares a secret value the package generates at commit time and writes in place of Placeholder in the declaring file. The same Name in two files is one value; its Kind and Length must agree wherever it is declared. The value exists only in the encrypted output.

type Kind

type Kind string

Kind is the shape of a generated secret value.

const (
	// Base64 is Length random bytes in standard base64 with padding.
	Base64 Kind = "base64"
	// Alphanumeric is Length characters drawn from [A-Za-z0-9].
	Alphanumeric Kind = "alphanumeric"
)

type Rule

type Rule struct {
	PathRegex *regexp.Regexp
	// Recipients are age public keys.
	Recipients        []string
	EncryptedRegex    string
	UnencryptedRegex  string
	EncryptedSuffix   string
	UnencryptedSuffix string
}

Rule is one creation rule: the paths it covers and the recipients it encrypts for. Files match the first rule in .sops.yaml order; a rule without path_regex matches every path.

Jump to

Keyboard shortcuts

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