feat(secretengine): serve secret engines as go-plugin extensions

A secret engine is now a subprocess serving secretengine.v1, installed through
the package store's new extensions family and enabled by extension-settings.
bws leaves the binary, as do the Yaegi engine loader and its examples.
This commit is contained in:
sam committed 2026-10-04 00:40:23 +10:00
1 parent 435961f78a
commit 8b28857ff3
51 files changed
+1568 -742

No files matched your search

-74
View File
@@ -1,74 +0,0 @@
//go:build ignore
// age secret engine plugin for archied.
//
// Resolves a SecretRef key from an age-encrypted file of "key=value"
// lines. Requires the age CLI on PATH and the AGE_FILE (encrypted file
// path) plus AGE_IDENTITY (age private-key file) env vars.
//
// Install: copy this file into the configured secret_engine_dir (default
// ~/.config/archie/secret-engines) and restart archied. The daemon
// evaluates it with Yaegi and registers engine "age". The //go:build
// ignore tag is a comment to the interpreter, so this file loads normally;
// the Go toolchain skips it, so it may live inside the module tree.
//
// The CLI is run through enginehost.Run rather than os/exec because
// yaegi's interpreted os/exec cannot pass an environment to child
// processes; env config is read with os.Getenv, which the daemon seeds
// with the host environment.
//
// Create the encrypted file with:
//
// age -e -r <recipient> secrets.txt -o secrets.age
//
// where secrets.txt holds lines like "DB_PASSWORD=..." — blank lines and
// "#" comments are skipped, the value may contain "=" (split at the first
// "="), and an empty value is treated as missing.
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/secret/enginehost"
)
var Engine = secret._Engine{
WName: func() string { return "age" },
WVersion: func() string { return "1.0.0" },
WResolve: resolveAge,
}
func resolveAge(key string) (string, error) {
file := os.Getenv("AGE_FILE")
identity := os.Getenv("AGE_IDENTITY")
if file == "" || identity == "" {
return "", errors.New("age engine: AGE_FILE and AGE_IDENTITY must be set")
}
out, err := enginehost.Run("age", []string{"-d", "-i", identity, file})
if err != nil {
return "", fmt.Errorf("age engine: decrypt %s: %w", file, err)
}
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
i := strings.Index(line, "=")
if i < 0 {
continue // malformed line, skip
}
if strings.TrimSpace(line[:i]) != key {
continue
}
value := strings.TrimSpace(line[i+1:])
if value == "" {
return "", fmt.Errorf("age engine: key %q is empty in %s", key, file)
}
return value, nil
}
return "", fmt.Errorf("age engine: key %q not found in %s", key, file)
}
-58
View File
@@ -1,58 +0,0 @@
//go:build ignore
// sops secret engine plugin for archied.
//
// Resolves a SecretRef key to its plaintext value from a sops-encrypted
// file. Requires the sops CLI on PATH and the SOPS_FILE env var naming the
// encrypted file. Decryption keys are handled by sops itself via .sops.yaml
// or --age/--pgp configuration; no credentials are configured here.
//
// Install: copy this file into the configured secret_engine_dir (default
// ~/.config/archie/secret-engines) and restart archied. The daemon
// evaluates it with Yaegi and registers engine "sops". The //go:build
// ignore tag is a comment to the interpreter, so this file loads normally;
// the Go toolchain skips it, so it may live inside the module tree.
//
// The CLI is run through enginehost.Run rather than os/exec because
// yaegi's interpreted os/exec cannot pass an environment to child
// processes; env config is read with os.Getenv, which the daemon seeds
// with the host environment.
//
// Key names must be plain leaf names (e.g. "DB_PASSWORD"). Nested
// sops --extract JSON paths are not supported; quotes, backslashes and
// brackets in keys are rejected with a clear error.
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/secret/enginehost"
)
var Engine = secret._Engine{
WName: func() string { return "sops" },
WVersion: func() string { return "1.0.0" },
WResolve: resolveSops,
}
func resolveSops(key string) (string, error) {
file := os.Getenv("SOPS_FILE")
if file == "" {
return "", errors.New("sops engine: SOPS_FILE is not set")
}
if strings.ContainsAny(key, `"\[`) {
return "", fmt.Errorf("sops engine: key %q is not a valid --extract path", key)
}
value, err := enginehost.Run("sops", []string{"-d", "--extract", `["` + key + `"]`, file})
if err != nil {
return "", fmt.Errorf("sops engine: decrypt %s: %w", file, err)
}
if value == "" {
return "", fmt.Errorf("sops engine: key %q not found in %s", key, file)
}
return value, nil
}
-73
View File
@@ -1,73 +0,0 @@
//go:build ignore
// vault secret engine plugin for archied.
//
// Resolves a SecretRef key to its plaintext value from HashiCorp Vault or
// OpenBao. Requires the vault CLI on PATH and the VAULT_ADDR + VAULT_TOKEN
// env vars, which the CLI reads natively (OpenBao's CLI accepts the same
// variables for compatibility).
//
// Install: copy this file into the configured secret_engine_dir (default
// ~/.config/archie/secret-engines) and restart archied. The daemon
// evaluates it with Yaegi and registers engine "vault". The //go:build
// ignore tag is a comment to the interpreter, so this file loads normally;
// the Go toolchain skips it, so it may live inside the module tree.
//
// The CLI is run through enginehost.Run rather than os/exec: VAULT_ADDR
// and VAULT_TOKEN must reach the child process, and yaegi's interpreted
// os/exec cannot pass an environment to children. enginehost.Run inherits
// the full host environment, so the vault CLI sees both vars.
//
// Key format: "path/field" resolves field within the KV path (e.g.
// "secret/archie/DB_PASSWORD"). A bare key resolves within the
// VAULT_SECRET_PATH prefix, which is required in that case. KV v1/v2 path
// translation is delegated to the CLI.
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/secret/enginehost"
)
var Engine = secret._Engine{
WName: func() string { return "vault" },
WVersion: func() string { return "1.0.0" },
WResolve: resolveVault,
}
func resolveVault(key string) (string, error) {
if os.Getenv("VAULT_ADDR") == "" || os.Getenv("VAULT_TOKEN") == "" {
return "", errors.New("vault engine: VAULT_ADDR and VAULT_TOKEN must be set")
}
path, field, err := vaultTarget(key)
if err != nil {
return "", err
}
value, err := enginehost.Run("vault", []string{"kv", "get", "-field=" + field, path})
if err != nil {
return "", fmt.Errorf("vault engine: read %s/%s: %w", path, field, err)
}
if value == "" {
return "", fmt.Errorf("vault engine: key %q not found at %s", field, path)
}
return value, nil
}
// vaultTarget splits a key into a KV path and field name. A key containing
// a slash names the full location ("secret/archie/DB_PASSWORD"); a bare key
// resolves within VAULT_SECRET_PATH.
func vaultTarget(key string) (path, field string, err error) {
if i := strings.LastIndex(key, "/"); i >= 0 {
return key[:i], key[i+1:], nil
}
prefix := os.Getenv("VAULT_SECRET_PATH")
if prefix == "" {
return "", "", errors.New("vault engine: key has no path and VAULT_SECRET_PATH is not set")
}
return prefix, key, nil
}