panzerschrank

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 10 Imported by: ? 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.

README

panzerschrank

Encrypts small secrets for your Go app's database: API tokens, passwords for other services, secret environment variables. AES-256-GCM from the standard library, the key outside the database, and every secret bound to its name.

box, err := panzerschrank.OpenKeyFile(filepath.Join(dataDir, panzerschrank.KeyFile))
if err != nil { … }

sealed := box.SealString(token, "dns-token:42")       // store this
token, err := box.OpenString(sealed, "dns-token:42")  // and get it back

What you get

  • The key stays out of the database. OpenKeyFile keeps it in a file next to the database (created on first start, mode 0600). Or pass it in base64 from an environment variable with NewFromBase64. A copy of the database alone reveals nothing.
  • Secrets are bound to their name. The name (a column, a variable name, "dns-token:42") is authenticated with the secret but not stored. A sealed value copied into another row or field does not open there.
  • Changes are detected. Changed data, another key or another name give ErrDecrypt, never garbage and never secret material in the message.
  • A fresh random nonce for every seal; sealing the same value twice gives different results.
  • No dependencies besides the standard library.

Format

Seal returns nonce (12 bytes) || ciphertext || tag (16 bytes); SealString returns the same in standard base64. The format is fixed by a test vector, so values stored today still open after an update.

Data sealed by the earlier copies in gpanel (nonce || ciphertext) opens as is. Data from static-hosting (ciphertext and nonce in two base64 columns, base64 key) opens after joining the two:

box, _ := panzerschrank.NewFromBase64(os.Getenv("SECRETS_KEY"))
ct, _ := base64.StdEncoding.DecodeString(row.ValueEnc)
nonce, _ := base64.StdEncoding.DecodeString(row.Nonce)
plain, err := box.Open(append(nonce, ct...), row.Name)

Keep in mind

  • Back up the key, apart from the database. Without it the secrets are gone; with both together, so is the protection.
  • It protects a stolen or leaked database (backups, dumps, a copied file), not a compromised server: whoever can read the key file can open everything.
  • One key encrypts many secrets with random nonces. Rotate it well before 2³² seals (about 4 billion), which small secrets do not reach in practice.
  • For passwords that people sign in with, use a password hash (argon2id), not this package: those never need to be decrypted.

Install

go get github.com/danielgtmn/panzerschrank@latest

Documentation with examples: pkg.go.dev.

Development

make test   # vet and tests with the race detector
make fuzz   # fuzz Open (FUZZTIME=5m)
make lint   # shellcheck, script tests, actionlint

See AGENTS.md and CONTRIBUTING.md.

License

Apache-2.0.

Documentation

Overview

Package panzerschrank encrypts small secrets (API tokens, passwords for other services, secret environment variables) for storage in a database, with AES-256-GCM.

The key is kept outside the database (a file next to it, or an environment variable), so a copy of the database alone reveals nothing. Every secret is bound to a name, such as the column, the variable name or "dns-token:42", as additional authenticated data: the name is not stored, and a sealed value only opens under the name it was sealed with, so a secret cannot be copied into another row or field and used there.

box, err := panzerschrank.OpenKeyFile(filepath.Join(dataDir, panzerschrank.KeyFile))
sealed := box.Seal([]byte(token), "dns-token")
token, err := box.Open(sealed, "dns-token")

Sealed data is nonce || ciphertext || tag (12 + len(plaintext) + 16 bytes). SealString and OpenString wrap it in standard base64 for text columns and environment variables.

Example

The key lives in a file next to the database and is created on first start.

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/danielgtmn/panzerschrank"
)

func main() {
	dataDir, _ := os.MkdirTemp("", "app")
	defer os.RemoveAll(dataDir)

	box, err := panzerschrank.OpenKeyFile(filepath.Join(dataDir, panzerschrank.KeyFile))
	if err != nil {
		log.Fatal(err)
	}

	// Store sealed in the database, bound to what it is.
	sealed := box.SealString("hetzner-token-123", "dns-token")

	token, err := box.OpenString(sealed, "dns-token")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(token)

	// The same value under another name does not open.
	_, err = box.OpenString(sealed, "smtp-password")
	fmt.Println(err != nil)
}
Output:
hetzner-token-123
true

Index

Examples

Constants

View Source
const KeyFile = "secret.key"

KeyFile is the suggested name of the key file inside an app's data directory.

View Source
const KeySize = 32

KeySize is the length of a key in bytes (AES-256).

Variables

View Source
var ErrDecrypt = errors.New("panzerschrank: cannot decrypt (wrong key, wrong name or damaged data)")

ErrDecrypt means sealed data could not be opened: it was changed, it was sealed with another key, or under another name. The message never contains secret material.

Functions

func GenerateKey

func GenerateKey() []byte

GenerateKey returns a new random 32-byte key.

func GenerateKeyBase64

func GenerateKeyBase64() string

GenerateKeyBase64 returns a new random key in standard base64, the format NewFromBase64 reads.

func LoadOrCreateKey

func LoadOrCreateKey(file string) ([]byte, error)

LoadOrCreateKey reads the raw 32-byte key in file. If the file does not exist it is created with a new random key and mode 0600; the directory must exist. When several processes start at once, all of them end up with the key of the one that created the file. The file never appears partly written, also not after a crash during creation.

Losing this file means losing every secret sealed with it: back it up, separately from the database.

Types

type Box

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

Box seals and opens secrets with one key. It is safe for concurrent use.

func New

func New(key []byte) (*Box, error)

New returns a Box for a 32-byte key. The key is not retained beyond the cipher state; the caller may clear its copy afterwards.

func NewFromBase64

func NewFromBase64(encoded string) (*Box, error)

NewFromBase64 returns a Box for a key given as standard base64 (with padding), for example from an environment variable. Surrounding whitespace is ignored. Create such a key with GenerateKeyBase64 or `openssl rand -base64 32`.

Example

The key comes from an environment variable in base64, e.g. created with `openssl rand -base64 32`.

package main

import (
	"fmt"
	"log"

	"github.com/danielgtmn/panzerschrank"
)

func main() {
	key := panzerschrank.GenerateKeyBase64() // os.Getenv("APP_SECRET_KEY")
	box, err := panzerschrank.NewFromBase64(key)
	if err != nil {
		log.Fatal(err)
	}
	sealed := box.Seal([]byte("s3cret"), "DB_PASSWORD")
	plain, _ := box.Open(sealed, "DB_PASSWORD")
	fmt.Println(string(plain))
}
Output:
s3cret

func OpenKeyFile

func OpenKeyFile(file string) (*Box, error)

OpenKeyFile returns a Box for the key in file, creating the file with a new random key (mode 0600) if it does not exist. See LoadOrCreateKey.

func (*Box) Open

func (b *Box) Open(sealed []byte, name string) ([]byte, error)

Open decrypts what Seal returned. Any change to sealed, another key or another name yields ErrDecrypt.

func (*Box) OpenString

func (b *Box) OpenString(sealed, name string) (string, error)

OpenString decrypts what SealString returned. Invalid base64 is reported as ErrDecrypt as well.

func (*Box) Overhead

func (b *Box) Overhead() int

Overhead is how many bytes Seal adds to the plaintext (nonce and tag).

func (*Box) Seal

func (b *Box) Seal(plaintext []byte, name string) []byte

Seal encrypts plaintext with a fresh random nonce. name is authenticated but not stored: Open must be given the same name, so a secret sealed for one use cannot be passed off as another. Sealing the same plaintext twice gives different results.

func (*Box) SealString

func (b *Box) SealString(plaintext, name string) string

SealString is Seal for text: it returns the sealed value as standard base64, ready for a text column.

Jump to

Keyboard shortcuts

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