feat(connectors): add remote gRPC mTLS runtime and default catalogue override
This commit is contained in:
6 files changed
+546
-17
No files matched your search
@@ -34,9 +34,19 @@ type catalogueSnapshot struct {
|
||||
issuedAt time.Time
|
||||
}
|
||||
|
||||
func effectiveSource(src store.Source) store.Source {
|
||||
func (m *Module) defaultCatalogue() store.Source {
|
||||
m.mu.RLock()
|
||||
def := m.defaultSource
|
||||
m.mu.RUnlock()
|
||||
if def.Location != "" {
|
||||
return def
|
||||
}
|
||||
return store.Source{Location: archipelagoLocation, PublicKey: archipelagoKey}
|
||||
}
|
||||
|
||||
func (m *Module) effectiveSource(src store.Source) store.Source {
|
||||
if src.Location == "" {
|
||||
src.Location, src.PublicKey = archipelagoLocation, archipelagoKey
|
||||
return m.defaultCatalogue()
|
||||
}
|
||||
return src
|
||||
}
|
||||
@@ -67,7 +77,7 @@ func (m *Module) getCatalogueSource(ctx context.Context, _ *struct{}) (*httpapi.
|
||||
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the catalogue source")
|
||||
}
|
||||
isDefault := src.Location == ""
|
||||
src = effectiveSource(src)
|
||||
src = m.effectiveSource(src)
|
||||
view := CatalogueSource{Location: src.Location, PublicKey: src.PublicKey}
|
||||
if src.Newest.Valid {
|
||||
view.Newest = &src.Newest.Time
|
||||
@@ -83,7 +93,8 @@ func (m *Module) putCatalogueSource(ctx context.Context, in *struct{ Body Catalo
|
||||
if _, err := catalogue.ParseKey(req.PublicKey); err != nil {
|
||||
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if req.Location == archipelagoLocation && req.PublicKey == archipelagoKey {
|
||||
def := m.defaultCatalogue()
|
||||
if req.Location == def.Location && req.PublicKey == def.PublicKey {
|
||||
return m.resetCatalogueSource(ctx, nil)
|
||||
}
|
||||
return m.saveCatalogueSource(ctx, store.Source{Location: req.Location, PublicKey: req.PublicKey})
|
||||
@@ -110,7 +121,7 @@ func (m *Module) readCatalogue(ctx context.Context) (catalogue.Document, catalog
|
||||
if err != nil {
|
||||
return catalogue.Document{}, catalogueSnapshot{}, err
|
||||
}
|
||||
src = effectiveSource(src)
|
||||
src = m.effectiveSource(src)
|
||||
key, err := catalogue.ParseKey(src.PublicKey)
|
||||
if err != nil {
|
||||
return catalogue.Document{}, catalogueSnapshot{}, err
|
||||
|
||||
@@ -48,11 +48,22 @@ type Host struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// RemoteTLS, RemoteEndpoint, and RemoteConfig configure static gRPC+mTLS
|
||||
// extension endpoints for hardened Kubernetes deployments.
|
||||
type (
|
||||
RemoteTLS = host.RemoteTLS
|
||||
RemoteEndpoint = host.RemoteEndpoint
|
||||
RemoteConfig = host.RemoteConfig
|
||||
)
|
||||
|
||||
// NewHost returns a host that logs plugin lifecycle to log.
|
||||
func NewHost(log *slog.Logger) *Host {
|
||||
return &Host{procs: host.NewHost(log), log: log}
|
||||
}
|
||||
|
||||
// UseRemotes configures remote gRPC+mTLS extension endpoints on the host.
|
||||
func (h *Host) UseRemotes(cfg RemoteConfig) error { return h.procs.UseRemotes(cfg) }
|
||||
|
||||
// Start launches spec, configures it and reads its declaration. A connector
|
||||
// already running under the same name is replaced.
|
||||
func (h *Host) Start(ctx context.Context, spec Spec) (*Connector, error) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"cmp"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -18,6 +19,8 @@ import (
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
)
|
||||
|
||||
// Handshake is shared by the host and every extension binary. Bump
|
||||
@@ -53,23 +56,40 @@ type Host struct {
|
||||
log *slog.Logger
|
||||
hclog hclog.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
procs map[string]*proc
|
||||
mu sync.Mutex
|
||||
procs map[string]*proc
|
||||
requireRemote bool
|
||||
remoteTLS *tls.Config
|
||||
remotes map[string]RemoteEndpoint
|
||||
}
|
||||
|
||||
// proc is one running extension and the egress gate it alone can reach.
|
||||
// proc is one running extension: either a local go-plugin subprocess and its
|
||||
// egress gate, or a remote gRPC client connection.
|
||||
type proc struct {
|
||||
client *goplugin.Client
|
||||
gate *gate
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func (p *proc) kill() {
|
||||
p.client.Kill()
|
||||
if p.conn != nil {
|
||||
_ = p.conn.Close()
|
||||
}
|
||||
if p.client != nil {
|
||||
p.client.Kill()
|
||||
}
|
||||
if p.gate != nil {
|
||||
p.gate.close()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *proc) alive() bool {
|
||||
if p.conn != nil {
|
||||
return p.conn.GetState() != connectivity.Shutdown
|
||||
}
|
||||
return p.client != nil && !p.client.Exited()
|
||||
}
|
||||
|
||||
func command(ctx context.Context, binary *os.File, egress []string) (*exec.Cmd, *gate, error) {
|
||||
if len(egress) == 0 {
|
||||
// Nothing to allow, so there is no relay to reach through: confining
|
||||
@@ -93,10 +113,59 @@ func NewHost(log *slog.Logger) *Host {
|
||||
return &Host{log: log, hclog: newSlogger(log), procs: make(map[string]*proc)}
|
||||
}
|
||||
|
||||
// UseRemotes configures static remote gRPC+mTLS extension endpoints for
|
||||
// hardened Kubernetes clusters where local namespace confinement is disabled.
|
||||
func (h *Host) UseRemotes(cfg RemoteConfig) error {
|
||||
tlsCfg, endpoints, err := validateRemoteConfig(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.mu.Lock()
|
||||
h.requireRemote = cfg.RequireRemote
|
||||
h.remoteTLS = tlsCfg
|
||||
h.remotes = cloneEndpoints(endpoints)
|
||||
h.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start launches spec serving surface and returns the dispensed client. An
|
||||
// extension already running under the same name is stopped first, so Start is
|
||||
// also how a changed binary or setting takes effect.
|
||||
func (h *Host) Start(ctx context.Context, spec Spec, surface string, impl goplugin.Plugin) (any, error) {
|
||||
if ep, ok := h.lookupRemote(spec); ok {
|
||||
return h.startRemote(ctx, spec, ep, impl)
|
||||
}
|
||||
h.mu.Lock()
|
||||
requireRemote := h.requireRemote
|
||||
h.mu.Unlock()
|
||||
if requireRemote {
|
||||
return nil, fmt.Errorf("extension %q: local subprocess execution is disabled and no remote endpoint is configured", spec.Name)
|
||||
}
|
||||
return h.startLocal(ctx, spec, surface, impl)
|
||||
}
|
||||
|
||||
func (h *Host) startRemote(ctx context.Context, spec Spec, ep RemoteEndpoint, impl goplugin.Plugin) (any, error) {
|
||||
gp, ok := impl.(goplugin.GRPCPlugin)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("extension %q: plugin does not support gRPC", spec.Name)
|
||||
}
|
||||
h.mu.Lock()
|
||||
baseTLS := h.remoteTLS
|
||||
h.mu.Unlock()
|
||||
conn, err := dialRemote(ep, baseTLS)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extension %q: dial remote %s: %w", spec.Name, ep.Address, err)
|
||||
}
|
||||
raw, err := gp.GRPCClient(ctx, nil, conn)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("extension %q: remote client: %w", spec.Name, err)
|
||||
}
|
||||
h.replaceProc(cmp.Or(spec.Key, spec.Name), &proc{conn: conn})
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func (h *Host) startLocal(ctx context.Context, spec Spec, surface string, impl goplugin.Plugin) (any, error) {
|
||||
sum, err := hex.DecodeString(spec.SHA256)
|
||||
if err != nil || len(sum) != sha256.Size {
|
||||
return nil, fmt.Errorf("extension %q: sha256 must be a hex digest", spec.Name)
|
||||
@@ -132,15 +201,18 @@ func (h *Host) Start(ctx context.Context, spec Spec, surface string, impl goplug
|
||||
p.kill()
|
||||
return nil, fmt.Errorf("extension %q: dispense %s: %w", spec.Name, surface, err)
|
||||
}
|
||||
key := cmp.Or(spec.Key, spec.Name)
|
||||
h.replaceProc(cmp.Or(spec.Key, spec.Name), p)
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func (h *Host) replaceProc(key string, next *proc) {
|
||||
h.mu.Lock()
|
||||
old := h.procs[key]
|
||||
h.procs[key] = p
|
||||
h.procs[key] = next
|
||||
h.mu.Unlock()
|
||||
if old != nil {
|
||||
old.kill()
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
// Stop kills the named extension. Stopping one that is not running is not an
|
||||
@@ -155,12 +227,13 @@ func (h *Host) Stop(name string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Alive reports whether the named extension's process is still running.
|
||||
// Alive reports whether the named extension's process or remote connection is
|
||||
// active.
|
||||
func (h *Host) Alive(name string) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
p := h.procs[name]
|
||||
return p != nil && !p.client.Exited()
|
||||
return p != nil && p.alive()
|
||||
}
|
||||
|
||||
// Close stops every extension.
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
// RemoteTLS holds the mutual-TLS material used to authenticate remote extension
|
||||
// gRPC endpoints.
|
||||
type RemoteTLS struct {
|
||||
CAFile string `toml:"ca_file"`
|
||||
CertFile string `toml:"cert_file"`
|
||||
KeyFile string `toml:"key_file"`
|
||||
}
|
||||
|
||||
// RemoteEndpoint names a pre-deployed gRPC extension service (sidecar socket
|
||||
// or mTLS pod address).
|
||||
type RemoteEndpoint struct {
|
||||
Address string `toml:"address"`
|
||||
ServerName string `toml:"server_name"`
|
||||
}
|
||||
|
||||
// RemoteConfig controls remote gRPC extension routing.
|
||||
type RemoteConfig struct {
|
||||
RequireRemote bool
|
||||
TLS RemoteTLS
|
||||
Endpoints map[string]RemoteEndpoint
|
||||
}
|
||||
|
||||
func validateRemoteConfig(cfg RemoteConfig) (*tls.Config, map[string]RemoteEndpoint, error) {
|
||||
endpoints := make(map[string]RemoteEndpoint, len(cfg.Endpoints))
|
||||
needTLS := false
|
||||
for k, ep := range cfg.Endpoints {
|
||||
key := strings.TrimSpace(k)
|
||||
addr := strings.TrimSpace(ep.Address)
|
||||
if key == "" || addr == "" {
|
||||
return nil, nil, fmt.Errorf("remote endpoint %q: address is required", k)
|
||||
}
|
||||
if strings.HasPrefix(addr, "unix://") {
|
||||
if err := checkUnixAddress(addr); err != nil {
|
||||
return nil, nil, fmt.Errorf("remote endpoint %q: %w", key, err)
|
||||
}
|
||||
} else {
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
return nil, nil, fmt.Errorf("remote endpoint %q: address must be host:port or unix://path: %w", key, err)
|
||||
}
|
||||
needTLS = true
|
||||
}
|
||||
endpoints[key] = RemoteEndpoint{Address: addr, ServerName: strings.TrimSpace(ep.ServerName)}
|
||||
}
|
||||
var tlsCfg *tls.Config
|
||||
hasAnyTLS := cfg.TLS.CAFile != "" || cfg.TLS.CertFile != "" || cfg.TLS.KeyFile != ""
|
||||
if needTLS || hasAnyTLS {
|
||||
var err error
|
||||
tlsCfg, err = loadRemoteTLS(cfg.TLS)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return tlsCfg, endpoints, nil
|
||||
}
|
||||
|
||||
func checkUnixAddress(addr string) error {
|
||||
sock := strings.TrimPrefix(addr, "unix://")
|
||||
if !strings.HasPrefix(sock, "/") {
|
||||
return errors.New("unix socket path must be absolute")
|
||||
}
|
||||
clean := filepath.Clean(sock)
|
||||
if clean != sock {
|
||||
return fmt.Errorf("unix socket path %q must be clean", sock)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadRemoteTLS(cfg RemoteTLS) (*tls.Config, error) {
|
||||
if cfg.CAFile == "" || cfg.CertFile == "" || cfg.KeyFile == "" {
|
||||
return nil, errors.New("remote mTLS requires ca_file, cert_file, and key_file")
|
||||
}
|
||||
caPEM, err := os.ReadFile(cfg.CAFile) //nolint:gosec // operator-configured CA path
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read remote CA %s: %w", cfg.CAFile, err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if !pool.AppendCertsFromPEM(caPEM) {
|
||||
return nil, fmt.Errorf("parse remote CA %s: no certificates found", cfg.CAFile)
|
||||
}
|
||||
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load remote client keypair: %w", err)
|
||||
}
|
||||
return &tls.Config{
|
||||
RootCAs: pool,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
MinVersion: tls.VersionTLS13,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func dialRemote(ep RemoteEndpoint, baseTLS *tls.Config) (*grpc.ClientConn, error) {
|
||||
if strings.HasPrefix(ep.Address, "unix://") {
|
||||
creds := insecure.NewCredentials()
|
||||
if baseTLS != nil && ep.ServerName != "" {
|
||||
cfg := baseTLS.Clone()
|
||||
cfg.ServerName = ep.ServerName
|
||||
creds = credentials.NewTLS(cfg)
|
||||
}
|
||||
return grpc.NewClient("unix://"+strings.TrimPrefix(ep.Address, "unix://"), grpc.WithTransportCredentials(creds))
|
||||
}
|
||||
if baseTLS == nil {
|
||||
return nil, fmt.Errorf("remote endpoint %q requires mTLS credentials", ep.Address)
|
||||
}
|
||||
cfg := baseTLS.Clone()
|
||||
if ep.ServerName != "" {
|
||||
cfg.ServerName = ep.ServerName
|
||||
} else if host, _, err := net.SplitHostPort(ep.Address); err == nil {
|
||||
cfg.ServerName = host
|
||||
}
|
||||
return grpc.NewClient(ep.Address, grpc.WithTransportCredentials(credentials.NewTLS(cfg)))
|
||||
}
|
||||
|
||||
func (h *Host) lookupRemote(spec Spec) (RemoteEndpoint, bool) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if spec.Key != "" {
|
||||
if ep, ok := h.remotes[spec.Key]; ok {
|
||||
return ep, true
|
||||
}
|
||||
}
|
||||
if spec.Name != "" {
|
||||
if ep, ok := h.remotes[spec.Name]; ok {
|
||||
return ep, true
|
||||
}
|
||||
}
|
||||
return RemoteEndpoint{}, false
|
||||
}
|
||||
|
||||
func cloneEndpoints(in map[string]RemoteEndpoint) map[string]RemoteEndpoint {
|
||||
out := make(map[string]RemoteEndpoint, len(in))
|
||||
maps.Copy(out, in)
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
|
||||
sepb "github.com/samcharles93/archie-core/internal/contracts/secretengine/v1"
|
||||
)
|
||||
|
||||
type testEnginePlugin struct {
|
||||
goplugin.NetRPCUnsupportedPlugin
|
||||
}
|
||||
|
||||
func (*testEnginePlugin) GRPCServer(*goplugin.GRPCBroker, *grpc.Server) error {
|
||||
return errors.New("server not used")
|
||||
}
|
||||
|
||||
func (*testEnginePlugin) GRPCClient(_ context.Context, _ *goplugin.GRPCBroker, conn *grpc.ClientConn) (any, error) {
|
||||
return sepb.NewSecretEngineServiceClient(conn), nil
|
||||
}
|
||||
|
||||
type fakeEngineServer struct {
|
||||
sepb.UnimplementedSecretEngineServiceServer
|
||||
}
|
||||
|
||||
func (*fakeEngineServer) Describe(context.Context, *sepb.DescribeRequest) (*sepb.DescribeResponse, error) {
|
||||
return &sepb.DescribeResponse{Version: "9.9.9"}, nil
|
||||
}
|
||||
|
||||
// TestRemoteGRPCRequiresMTLSAndEnforcesTrust guards Kubernetes remote extension
|
||||
// routing: plaintext TCP and untrusted server certs fail closed, while mTLS
|
||||
// endpoints and unix sidecar sockets dispense working gRPC clients.
|
||||
func TestRemoteGRPCRequiresMTLSAndEnforcesTrust(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
trusted := generateTestPKI(t, dir, "trusted")
|
||||
untrusted := generateTestPKI(t, dir, "untrusted")
|
||||
|
||||
// Plaintext TCP endpoints must be refused when mTLS certs are missing.
|
||||
h := NewHost(slog.New(slog.DiscardHandler))
|
||||
if err := h.UseRemotes(RemoteConfig{
|
||||
Endpoints: map[string]RemoteEndpoint{"vault": {Address: "127.0.0.1:9443"}},
|
||||
}); err == nil {
|
||||
t.Fatal("UseRemotes accepted TCP endpoint without mTLS credentials")
|
||||
}
|
||||
|
||||
// Relative/traversal unix socket paths must be rejected.
|
||||
if err := h.UseRemotes(RemoteConfig{
|
||||
Endpoints: map[string]RemoteEndpoint{"bad": {Address: "unix://../escape.sock"}},
|
||||
}); err == nil {
|
||||
t.Fatal("UseRemotes accepted non-clean unix socket path")
|
||||
}
|
||||
|
||||
// Start an mTLS gRPC server using the trusted PKI.
|
||||
serverTLS := &tls.Config{
|
||||
Certificates: []tls.Certificate{trusted.serverCert},
|
||||
ClientCAs: trusted.pool,
|
||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||
MinVersion: tls.VersionTLS13,
|
||||
}
|
||||
var lc net.ListenConfig
|
||||
ln, err := lc.Listen(context.Background(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := grpc.NewServer(grpc.Creds(credentials.NewTLS(serverTLS)))
|
||||
sepb.RegisterSecretEngineServiceServer(srv, &fakeEngineServer{})
|
||||
go func() { _ = srv.Serve(ln) }()
|
||||
t.Cleanup(srv.Stop)
|
||||
|
||||
// Start a second server with an untrusted certificate.
|
||||
untrustedSrv := grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
|
||||
Certificates: []tls.Certificate{untrusted.serverCert},
|
||||
MinVersion: tls.VersionTLS13,
|
||||
})))
|
||||
sepb.RegisterSecretEngineServiceServer(untrustedSrv, &fakeEngineServer{})
|
||||
uln, err := lc.Listen(context.Background(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go func() { _ = untrustedSrv.Serve(uln) }()
|
||||
t.Cleanup(untrustedSrv.Stop)
|
||||
|
||||
// Start a unix sidecar socket server.
|
||||
sockPath := filepath.Join(dir, "sidecar.sock")
|
||||
sln, err := lc.Listen(context.Background(), "unix", sockPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sidecarSrv := grpc.NewServer()
|
||||
sepb.RegisterSecretEngineServiceServer(sidecarSrv, &fakeEngineServer{})
|
||||
go func() { _ = sidecarSrv.Serve(sln) }()
|
||||
t.Cleanup(sidecarSrv.Stop)
|
||||
|
||||
if err := h.UseRemotes(RemoteConfig{
|
||||
RequireRemote: true,
|
||||
TLS: trusted.clientTLS,
|
||||
Endpoints: map[string]RemoteEndpoint{
|
||||
"vault": {Address: ln.Addr().String(), ServerName: "extension.local"},
|
||||
"untrusted": {Address: uln.Addr().String(), ServerName: "extension.local"},
|
||||
"sidecar": {Address: "unix://" + sockPath},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("UseRemotes: %v", err)
|
||||
}
|
||||
|
||||
// 1. Trusted mTLS endpoint works and Alive reflects Stop.
|
||||
raw, err := h.Start(context.Background(), Spec{Name: "vault"}, "secretengine", &testEnginePlugin{})
|
||||
if err != nil {
|
||||
t.Fatalf("Start remote vault: %v", err)
|
||||
}
|
||||
client, ok := raw.(sepb.SecretEngineServiceClient)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected client type %T", raw)
|
||||
}
|
||||
desc, err := client.Describe(context.Background(), &sepb.DescribeRequest{})
|
||||
if err != nil || desc.GetVersion() != "9.9.9" {
|
||||
t.Fatalf("Describe over mTLS = (%v, %v), want 9.9.9", desc, err)
|
||||
}
|
||||
if !h.Alive("vault") {
|
||||
t.Fatal("Alive(vault) = false, want true")
|
||||
}
|
||||
h.Stop("vault")
|
||||
if h.Alive("vault") {
|
||||
t.Fatal("Alive(vault) = true after Stop")
|
||||
}
|
||||
|
||||
// 2. Untrusted server cert fails TLS verification on RPC.
|
||||
rawBad, err := h.Start(context.Background(), Spec{Name: "untrusted"}, "secretengine", &testEnginePlugin{})
|
||||
if err != nil {
|
||||
t.Fatalf("Start untrusted: %v", err)
|
||||
}
|
||||
badClient, ok := rawBad.(sepb.SecretEngineServiceClient)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected client type %T", rawBad)
|
||||
}
|
||||
if _, err := badClient.Describe(context.Background(), &sepb.DescribeRequest{}); err == nil {
|
||||
t.Fatal("expected TLS failure against untrusted server")
|
||||
}
|
||||
|
||||
// 3. Unix sidecar socket works.
|
||||
rawSidecar, err := h.Start(context.Background(), Spec{Name: "sidecar"}, "secretengine", &testEnginePlugin{})
|
||||
if err != nil {
|
||||
t.Fatalf("Start sidecar: %v", err)
|
||||
}
|
||||
sidecarClient, ok := rawSidecar.(sepb.SecretEngineServiceClient)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected client type %T", rawSidecar)
|
||||
}
|
||||
if desc, err := sidecarClient.Describe(context.Background(), &sepb.DescribeRequest{}); err != nil || desc.GetVersion() != "9.9.9" {
|
||||
t.Fatalf("Describe over unix = (%v, %v)", desc, err)
|
||||
}
|
||||
|
||||
// 4. Unmapped extension fails closed when RequireRemote is true.
|
||||
if _, err := h.Start(context.Background(), Spec{Name: "local-only"}, "secretengine", &testEnginePlugin{}); err == nil || !strings.Contains(err.Error(), "local subprocess execution is disabled") {
|
||||
t.Fatalf("expected RequireRemote refusal, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type testPKI struct {
|
||||
pool *x509.CertPool
|
||||
serverCert tls.Certificate
|
||||
clientTLS RemoteTLS
|
||||
}
|
||||
|
||||
func generateTestPKI(t *testing.T, dir, prefix string) testPKI {
|
||||
t.Helper()
|
||||
caKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caTmpl := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
Subject: pkix.Name{CommonName: prefix + "-ca"},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(time.Hour),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
}
|
||||
caDER, err := x509.CreateCertificate(rand.Reader, caTmpl, caTmpl, &caKey.PublicKey, caKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caCert, err := x509.ParseCertificate(caDER)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: caDER})
|
||||
caFile := filepath.Join(dir, prefix+"-ca.pem")
|
||||
if err := os.WriteFile(caFile, caPEM, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
pool.AddCert(caCert)
|
||||
|
||||
serverCert, _, _ := issueCert(t, dir, prefix+"-server", caCert, caKey, "extension.local", x509.ExtKeyUsageServerAuth)
|
||||
_, certFile, keyFile := issueCert(t, dir, prefix+"-client", caCert, caKey, "archie-core", x509.ExtKeyUsageClientAuth)
|
||||
return testPKI{
|
||||
pool: pool,
|
||||
serverCert: serverCert,
|
||||
clientTLS: RemoteTLS{CAFile: caFile, CertFile: certFile, KeyFile: keyFile},
|
||||
}
|
||||
}
|
||||
|
||||
func issueCert(t *testing.T, dir, name string, ca *x509.Certificate, caKey *ecdsa.PrivateKey, cn string, usage x509.ExtKeyUsage) (tls.Certificate, string, string) {
|
||||
t.Helper()
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(time.Now().UnixNano()),
|
||||
Subject: pkix.Name{CommonName: cn},
|
||||
DNSNames: []string{cn},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(time.Hour),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{usage},
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, ca, &key.PublicKey, caKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keyDER, err := x509.MarshalECPrivateKey(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
|
||||
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
|
||||
certFile := filepath.Join(dir, name+".crt")
|
||||
keyFile := filepath.Join(dir, name+".key")
|
||||
if err := os.WriteFile(certFile, certPEM, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(keyFile, keyPEM, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tlsCert, certFile, keyFile
|
||||
}
|
||||
@@ -38,9 +38,10 @@ type Module struct {
|
||||
log *slog.Logger
|
||||
// sources and fetcher read the catalogue and its artifacts, and pkgMu
|
||||
// serialises changes to a package's versions.
|
||||
sources sourceStore
|
||||
fetcher *catalogue.Fetcher
|
||||
pkgMu sync.Mutex
|
||||
sources sourceStore
|
||||
defaultSource store.Source
|
||||
fetcher *catalogue.Fetcher
|
||||
pkgMu sync.Mutex
|
||||
|
||||
mu sync.RWMutex
|
||||
idx index
|
||||
@@ -79,6 +80,26 @@ func (m *Module) Use(recheck Recheck) {
|
||||
m.recheck = recheck
|
||||
}
|
||||
|
||||
// UseRemotes configures static remote gRPC+mTLS extension endpoints.
|
||||
func (m *Module) UseRemotes(cfg RemoteConfig) error {
|
||||
return m.host.UseRemotes(cfg)
|
||||
}
|
||||
|
||||
// UseDefaultCatalogue overrides the fallback extension catalogue source used
|
||||
// when the control plane has not set a custom location.
|
||||
func (m *Module) UseDefaultCatalogue(location, publicKey string) error {
|
||||
if err := catalogue.CheckSource(location); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := catalogue.ParseKey(publicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.defaultSource = store.Source{Location: location, PublicKey: publicKey}
|
||||
m.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// installationStore is where installations, their overrides and the
|
||||
// capabilities of the package they configure are kept.
|
||||
type installationStore interface {
|
||||
|
||||
Reference in new issue
Block a user