Credentials are typed (machine, age), write-only through the API and sealed to the age identity in the bootstrap config. A field holds a value or names a secret engine key; connectors and engines are accepted with credential slots that resolve into their Configure secrets. The secret engine contract gains secrets so an engine can receive its own key.
82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
goplugin "github.com/hashicorp/go-plugin"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
sepb "github.com/samcharles93/archie-core/internal/contracts/secretengine/v1"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/host"
|
|
)
|
|
|
|
// engineSurface names the secret engine contract in the plugin handshake.
|
|
const engineSurface = "secretengine"
|
|
|
|
// resolveTimeout bounds one secret lookup, so a hung engine can't hold a
|
|
// connector's start for ever.
|
|
const resolveTimeout = 30 * time.Second
|
|
|
|
// ErrSecretNotFound means a secret engine holds nothing under the key.
|
|
var ErrSecretNotFound = errors.New("secret engine holds no such key")
|
|
|
|
// Engine is a running secret engine: an extension that resolves the keys
|
|
// credentials name, such as entries in a sops file.
|
|
type Engine struct {
|
|
name string
|
|
client sepb.SecretEngineServiceClient
|
|
}
|
|
|
|
// Resolve returns the secret the engine holds under key.
|
|
func (e *Engine) Resolve(ctx context.Context, key string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, resolveTimeout)
|
|
defer cancel()
|
|
resp, err := e.client.Resolve(ctx, &sepb.ResolveRequest{Key: key})
|
|
if status.Code(err) == codes.NotFound {
|
|
return "", fmt.Errorf("secret engine %q: key %q: %w", e.name, key, ErrSecretNotFound)
|
|
}
|
|
if err != nil {
|
|
return "", fmt.Errorf("secret engine %q: %w", e.name, err)
|
|
}
|
|
return resp.GetValue(), nil
|
|
}
|
|
|
|
// StartEngine launches a secret engine and configures it with its settings
|
|
// and its own secrets.
|
|
func (h *Host) StartEngine(ctx context.Context, spec Spec) (*Engine, error) {
|
|
raw, err := h.procs.Start(ctx, host.Spec{
|
|
Name: spec.Name, Path: spec.Path, SHA256: spec.SHA256, Env: spec.Env, Egress: spec.Egress,
|
|
}, engineSurface, &enginePlugin{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
client, ok := raw.(sepb.SecretEngineServiceClient)
|
|
if !ok {
|
|
h.procs.Stop(spec.Name)
|
|
return nil, fmt.Errorf("secret engine %q: plugin is not a secret engine", spec.Name)
|
|
}
|
|
if _, err := client.Configure(ctx, &sepb.ConfigureRequest{Settings: spec.Settings, Secrets: spec.Secrets}); err != nil {
|
|
h.procs.Stop(spec.Name)
|
|
return nil, fmt.Errorf("secret engine %q: configure: %w", spec.Name, err)
|
|
}
|
|
return &Engine{name: spec.Name, client: client}, nil
|
|
}
|
|
|
|
// enginePlugin is the host side of the go-plugin handshake for secret engines.
|
|
type enginePlugin struct {
|
|
goplugin.NetRPCUnsupportedPlugin
|
|
}
|
|
|
|
func (*enginePlugin) GRPCServer(*goplugin.GRPCBroker, *grpc.Server) error {
|
|
return errors.New("secret engines are served by extensions, not by the host")
|
|
}
|
|
|
|
func (*enginePlugin) GRPCClient(_ context.Context, _ *goplugin.GRPCBroker, conn *grpc.ClientConn) (any, error) {
|
|
return sepb.NewSecretEngineServiceClient(conn), nil
|
|
}
|