feat(secret-engines): port age, sops and vault as secretengine.v1 extensions
This commit is contained in:
7 files changed
+226
No files matched your search
@@ -0,0 +1,63 @@
|
||||
package secretengine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
secretenginev1 "github.com/samcharles93/archipelago/sdk/secretengine/v1"
|
||||
)
|
||||
|
||||
// ErrNotFound is what a resolve function returns for a key the backend does
|
||||
// not hold.
|
||||
var ErrNotFound = errors.New("key not found")
|
||||
|
||||
type funcEngine struct {
|
||||
secretenginev1.UnimplementedSecretEngineServiceServer
|
||||
resolve func(ctx context.Context, key string) (string, error)
|
||||
}
|
||||
|
||||
func (funcEngine) Configure(context.Context, *secretenginev1.ConfigureRequest) (*secretenginev1.ConfigureResponse, error) {
|
||||
return &secretenginev1.ConfigureResponse{}, nil
|
||||
}
|
||||
|
||||
func (e funcEngine) Resolve(ctx context.Context, r *secretenginev1.ResolveRequest) (*secretenginev1.ResolveResponse, error) {
|
||||
value, err := e.resolve(ctx, r.GetKey())
|
||||
switch {
|
||||
case errors.Is(err, ErrNotFound):
|
||||
return nil, status.Error(codes.NotFound, err.Error())
|
||||
case err != nil:
|
||||
return nil, status.Error(codes.Unavailable, err.Error())
|
||||
}
|
||||
return &secretenginev1.ResolveResponse{Value: value}, nil
|
||||
}
|
||||
|
||||
// ServeFunc runs an engine whose whole behaviour is one resolve function and
|
||||
// that needs no settings.
|
||||
func ServeFunc(resolve func(ctx context.Context, key string) (string, error)) {
|
||||
Serve(funcEngine{resolve: resolve})
|
||||
}
|
||||
|
||||
const cliTimeout = 30 * time.Second
|
||||
|
||||
// Run executes a CLI with the extension's own environment and returns its
|
||||
// trimmed stdout. Stderr is discarded so a CLI cannot leak a secret into logs.
|
||||
func Run(ctx context.Context, name string, args ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, cliTimeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Stderr = io.Discard
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// age resolves a key from an age-encrypted file of "key=value" lines. It needs
|
||||
// the age CLI on PATH, AGE_FILE (the encrypted file) and AGE_IDENTITY (the age
|
||||
// private-key file). Blank lines and "#" comments are skipped, a value may
|
||||
// contain "=", and an empty value counts as missing.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archipelago/sdk/secretengine"
|
||||
)
|
||||
|
||||
func resolve(ctx context.Context, key string) (string, error) {
|
||||
file, identity := os.Getenv("AGE_FILE"), os.Getenv("AGE_IDENTITY")
|
||||
if file == "" || identity == "" {
|
||||
return "", errors.New("AGE_FILE and AGE_IDENTITY must be set")
|
||||
}
|
||||
out, err := secretengine.Run(ctx, "age", "-d", "-i", identity, file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt %s: %w", file, err)
|
||||
}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
name, value, ok := strings.Cut(line, "=")
|
||||
if line == "" || strings.HasPrefix(line, "#") || !ok || strings.TrimSpace(name) != key {
|
||||
continue
|
||||
}
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
return value, nil
|
||||
}
|
||||
break
|
||||
}
|
||||
return "", secretengine.ErrNotFound
|
||||
}
|
||||
|
||||
func main() { secretengine.ServeFunc(resolve) }
|
||||
@@ -0,0 +1,15 @@
|
||||
apiVersion: dev.archie.package.v1
|
||||
displayName: age
|
||||
description: Resolve secret references from an age-encrypted key=value file through the age CLI.
|
||||
version: 1.0.0
|
||||
contributes:
|
||||
extensions:
|
||||
- surface: secretengine
|
||||
path: bin/age
|
||||
authority:
|
||||
env:
|
||||
- AGE_FILE
|
||||
- AGE_IDENTITY
|
||||
files:
|
||||
- path: bin/age
|
||||
mode: 493
|
||||
@@ -0,0 +1,33 @@
|
||||
// sops resolves a top-level key from a sops-encrypted file. It needs the sops
|
||||
// CLI on PATH and SOPS_FILE (the encrypted file).
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archipelago/sdk/secretengine"
|
||||
)
|
||||
|
||||
func resolve(ctx context.Context, key string) (string, error) {
|
||||
file := os.Getenv("SOPS_FILE")
|
||||
if file == "" {
|
||||
return "", errors.New("SOPS_FILE is not set")
|
||||
}
|
||||
if strings.ContainsAny(key, `"\[`) {
|
||||
return "", fmt.Errorf("key %q is not a valid --extract path", key)
|
||||
}
|
||||
value, err := secretengine.Run(ctx, "sops", "-d", "--extract", `["`+key+`"]`, file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt %s: %w", file, err)
|
||||
}
|
||||
if value == "" {
|
||||
return "", secretengine.ErrNotFound
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func main() { secretengine.ServeFunc(resolve) }
|
||||
@@ -0,0 +1,14 @@
|
||||
apiVersion: dev.archie.package.v1
|
||||
displayName: SOPS
|
||||
description: Resolve secret references from a sops-encrypted file through the sops CLI.
|
||||
version: 1.0.0
|
||||
contributes:
|
||||
extensions:
|
||||
- surface: secretengine
|
||||
path: bin/sops
|
||||
authority:
|
||||
env:
|
||||
- SOPS_FILE
|
||||
files:
|
||||
- path: bin/sops
|
||||
mode: 493
|
||||
@@ -0,0 +1,45 @@
|
||||
// vault resolves a field of a KV secret through the vault CLI (also works with
|
||||
// OpenBao). It needs VAULT_ADDR and VAULT_TOKEN. A key is "path/field"; a key
|
||||
// with no slash is a field under VAULT_SECRET_PATH.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archipelago/sdk/secretengine"
|
||||
)
|
||||
|
||||
func resolve(ctx context.Context, key string) (string, error) {
|
||||
if os.Getenv("VAULT_ADDR") == "" || os.Getenv("VAULT_TOKEN") == "" {
|
||||
return "", errors.New("VAULT_ADDR and VAULT_TOKEN must be set")
|
||||
}
|
||||
path, field, err := target(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
value, err := secretengine.Run(ctx, "vault", "kv", "get", "-field="+field, path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s/%s: %w", path, field, err)
|
||||
}
|
||||
if value == "" {
|
||||
return "", secretengine.ErrNotFound
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func target(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("key has no path and VAULT_SECRET_PATH is not set")
|
||||
}
|
||||
return prefix, key, nil
|
||||
}
|
||||
|
||||
func main() { secretengine.ServeFunc(resolve) }
|
||||
@@ -0,0 +1,16 @@
|
||||
apiVersion: dev.archie.package.v1
|
||||
displayName: Vault / OpenBao
|
||||
description: Resolve secret references from a KV secret through the vault CLI.
|
||||
version: 1.0.0
|
||||
contributes:
|
||||
extensions:
|
||||
- surface: secretengine
|
||||
path: bin/vault
|
||||
authority:
|
||||
env:
|
||||
- VAULT_ADDR
|
||||
- VAULT_TOKEN
|
||||
- VAULT_SECRET_PATH
|
||||
files:
|
||||
- path: bin/vault
|
||||
mode: 493
|
||||
Reference in new issue
Block a user