publish / publish (push) Successful in 2m53s
The host connector answers filesystem_usage, directory_sizes and old_files on configured hosts over SSH with pinned host keys, dialling through the egress tunnel when confined. The sops engine decrypts with the sops library instead of the CLI, takes its file from settings and an age identity from its secrets.
127 lines
3.6 KiB
Go
127 lines
3.6 KiB
Go
// sops resolves a top-level key from a sops-encrypted file (YAML, JSON or
|
|
// dotenv) with the sops library, so no sops CLI is needed. Its settings name
|
|
// the file; an age identity, if the file is encrypted to age, arrives as its
|
|
// "age.identity" secret. Other sops key sources (KMS, PGP, Vault) read their
|
|
// usual environment variables, which the package must be accepted with.
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/getsops/sops/v3/decrypt"
|
|
"go.yaml.in/yaml/v3"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archipelago/sdk/secretengine"
|
|
secretenginev1 "github.com/samcharles93/archipelago/sdk/secretengine/v1"
|
|
)
|
|
|
|
type engine struct {
|
|
secretenginev1.UnimplementedSecretEngineServiceServer
|
|
|
|
mu sync.RWMutex
|
|
file string
|
|
}
|
|
|
|
func (e *engine) Configure(_ context.Context, r *secretenginev1.ConfigureRequest) (*secretenginev1.ConfigureResponse, error) {
|
|
file := r.GetSettings()["file"]
|
|
if file == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "the file setting names the sops file")
|
|
}
|
|
// sops reads an age identity from SOPS_AGE_KEY. This process is the
|
|
// engine's alone, so its environment is the place to hand it over.
|
|
if id := r.GetSecrets()["age.identity"]; id != "" {
|
|
if err := os.Setenv("SOPS_AGE_KEY", id); err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
}
|
|
e.mu.Lock()
|
|
e.file = file
|
|
e.mu.Unlock()
|
|
return &secretenginev1.ConfigureResponse{}, nil
|
|
}
|
|
|
|
func (e *engine) Resolve(_ context.Context, r *secretenginev1.ResolveRequest) (*secretenginev1.ResolveResponse, error) {
|
|
e.mu.RLock()
|
|
file := e.file
|
|
e.mu.RUnlock()
|
|
if file == "" {
|
|
return nil, status.Error(codes.FailedPrecondition, "not configured")
|
|
}
|
|
// The whole file decrypts and the key is looked up here, so a missing key
|
|
// reads as missing rather than as a failed decrypt.
|
|
plain, err := decrypt.File(file, "")
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Unavailable, "decrypt %s: %v", file, err)
|
|
}
|
|
doc, err := parse(file, plain)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Unavailable, "read decrypted %s: %v", file, err)
|
|
}
|
|
value, err := lookup(doc, r.GetKey())
|
|
if errors.Is(err, secretengine.ErrNotFound) {
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.Unavailable, err.Error())
|
|
}
|
|
return &secretenginev1.ResolveResponse{Value: value}, nil
|
|
}
|
|
|
|
// parse reads a decrypted file into its top-level keys. YAML covers JSON.
|
|
func parse(file string, plain []byte) (map[string]any, error) {
|
|
if strings.HasSuffix(file, ".env") {
|
|
return parseDotenv(plain)
|
|
}
|
|
var doc map[string]any
|
|
if err := yaml.Unmarshal(plain, &doc); err != nil {
|
|
return nil, err
|
|
}
|
|
return doc, nil
|
|
}
|
|
|
|
func parseDotenv(plain []byte) (map[string]any, error) {
|
|
doc := map[string]any{}
|
|
sc := bufio.NewScanner(bytes.NewReader(plain))
|
|
for sc.Scan() {
|
|
line := strings.TrimSpace(sc.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
k, v, ok := strings.Cut(line, "=")
|
|
if !ok {
|
|
return nil, fmt.Errorf("line %q has no '='", line)
|
|
}
|
|
doc[k] = v
|
|
}
|
|
return doc, sc.Err()
|
|
}
|
|
|
|
// lookup returns a top-level key's value: a string as written, anything else
|
|
// as JSON.
|
|
func lookup(doc map[string]any, key string) (string, error) {
|
|
v, ok := doc[key]
|
|
if !ok || v == nil || v == "" {
|
|
return "", secretengine.ErrNotFound
|
|
}
|
|
if s, ok := v.(string); ok {
|
|
return s, nil
|
|
}
|
|
out, err := json.Marshal(v)
|
|
if err != nil {
|
|
return "", fmt.Errorf("encode %q: %w", key, err)
|
|
}
|
|
return string(out), nil
|
|
}
|
|
|
|
func main() { secretengine.Serve(&engine{}) }
|