Help us shape the future of Go! Take the 2026 Go Developer Survey

secret

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package secret holds the key that encrypts private response bodies and the encryption itself (AES-256-GCM). The key comes from the environment, the macOS Keychain or 1Password, or, for a project that keeps everything in its own folder (baselines: local), from a key file there that git ignores and only its owner can read.

Index

Constants

View Source
const EnvKey = "DRIFTGATE_KEY"

EnvKey is the environment variable that may hold the base64 key.

View Source
const KeySize = 32

KeySize is the length of an AES-256 key.

Variables

View Source
var ErrNoKey = errors.New("no encryption key: set " + EnvKey + ", run 'driftgate key create', or configure key.op")

ErrNoKey means no key source produced a key.

Functions

func Create

func Create(project string) error

Create generates a key and stores it in the macOS Keychain. The key is passed to `security` on stdin, never as an argument visible in the process list.

func CreateFile

func CreateFile(path string) error

CreateFile generates a key and writes it to path (base64, open to its owner only). An existing file is never replaced: private bodies encrypted with its key would become unreadable.

func Environ

func Environ() []string

Environ is the environment of a command DriftGate runs (hooks, adapters, token commands, git): its own, without the variables that hold a key. The commands come from the repository, which a pull request may change.

A variable is left out by its name (DRIFTGATE_KEY, key.env) or by its value: the project key as loaded, or the key a hidden variable holds, in any of keyForms, so a copy of the key under another name (a CI secret exported twice) is left out too. This is defence in depth, not a boundary: a key in another form (in a file, inside a longer value) or a command that reads the key from where DriftGate got it (1Password, the Keychain) is not stopped.

func HideEnv

func HideEnv(name string)

HideEnv keeps an environment variable that holds a key from the commands DriftGate runs.

func WriteFile

func WriteFile(path string, k Key) error

WriteFile writes k to a new key file at path.

Types

type Key

type Key []byte

Key is an AES-256 key.

func Load

func Load(project string, src Source, file string) (Key, error)

Load finds the key for a project. file is the key file of a project that keeps its key in its own folder; when it is set it takes the place of the Keychain.

func ReadFile

func ReadFile(path string) (Key, error)

ReadFile reads a key file: a base64 key that only its owner can read.

func (Key) Open

func (k Key) Open(sealed, aad []byte) ([]byte, error)

Open decrypts what Seal produced with the same aad.

func (Key) OpenInPlace

func (k Key) OpenInPlace(sealed, aad []byte) ([]byte, error)

OpenInPlace is Open that decrypts into sealed's own storage, so a body is held once: sealed is overwritten (and holds nothing usable after an error), and the plaintext returned shares its memory.

func (Key) Ref

func (k Key) Ref(plaintext []byte) string

Ref names a private body without revealing it: the HMAC-SHA256 of the plaintext under a key derived from k. Equal bodies get equal names under one key, but a guess of a short body cannot be checked against the name without the key, as it could against a plain SHA-256.

func (Key) RefHash

func (k Key) RefHash() hash.Hash

RefHash is Ref for content read in parts: its sum, in hex, is the ref of what was written to it.

func (Key) Seal

func (k Key) Seal(plaintext, aad []byte) ([]byte, error)

Seal encrypts plaintext; aad binds the ciphertext to its name.

func (Key) SealInPlace

func (k Key) SealInPlace(plaintext, aad []byte) (header, ciphertext []byte, err error)

SealInPlace is Seal that encrypts into plaintext's own storage, so a body is held once: plaintext is overwritten and must not be used afterwards. The header (format mark and nonce) comes back apart from the ciphertext; written one after the other they are what Seal returns. Without room for the tag in plaintext's capacity the ciphertext gets storage of its own.

type Source

type Source struct {
	Env string `yaml:"env,omitempty"`
	Op  string `yaml:"op,omitempty"`
}

Source says where the key comes from; empty fields fall back to the environment and then the macOS Keychain.

Jump to

Keyboard shortcuts

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