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 ¶
const KeyFile = "secret.key"
KeyFile is the suggested name of the key file inside an app's data directory.
const KeySize = 32
KeySize is the length of a key in bytes (AES-256).
Variables ¶
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 GenerateKeyBase64 ¶
func GenerateKeyBase64() string
GenerateKeyBase64 returns a new random key in standard base64, the format NewFromBase64 reads.
func LoadOrCreateKey ¶
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 ¶
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 ¶
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 ¶
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 ¶
Open decrypts what Seal returned. Any change to sealed, another key or another name yields ErrDecrypt.
func (*Box) OpenString ¶
OpenString decrypts what SealString returned. Invalid base64 is reported as ErrDecrypt as well.
func (*Box) Seal ¶
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 ¶
SealString is Seal for text: it returns the sealed value as standard base64, ready for a text column.