feat(connectors): pick a local or remote runtime for every extension
Track 2 of the enterprise readiness plan. Extensions now start through a Runtime: RemoteGRPCRuntime dials an endpoint the operator mapped, over mTLS for TCP, and LocalNamespaceRuntime runs the verified binary in its own user and network namespace. A mapped endpoint is the only runtime that may serve that extension, so a broken endpoint refuses instead of quietly falling back to a subprocess. Local availability is checked instead of assumed: user and network namespace limits read from /proc/sys/user report a stage that disabled them, which is what restricted Pod Security Standards and hardened kernels do. A host where the chosen runtime cannot run refuses the extension and names why both runtimes declined, so the operator learns to configure a remote endpoint.
This commit is contained in:
6 files changed
+258
-93
No files matched your search
@@ -10,6 +10,8 @@ import (
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
@@ -58,6 +60,33 @@ func confinedCommand(ctx context.Context, binary *os.File, args []string, socket
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// namespacesAvailable reports why this process cannot create the user and
|
||||
// network namespaces an extension runs in, or nil when it can. A kernel or a
|
||||
// container runtime that sets these limits to zero, which restricted Pod
|
||||
// Security Standards and hardened STIG profiles do, leaves remote endpoints as
|
||||
// the only way to run an extension.
|
||||
func namespacesAvailable() error {
|
||||
if err := namespaceLimit("/proc/sys/user/max_user_namespaces", "user namespaces"); err != nil {
|
||||
return err
|
||||
}
|
||||
return namespaceLimit("/proc/sys/user/max_net_namespaces", "network namespaces")
|
||||
}
|
||||
|
||||
func namespaceLimit(path, what string) error {
|
||||
value, err := os.ReadFile(path) //nolint:gosec // fixed path under /proc/sys/user
|
||||
if err != nil {
|
||||
return fmt.Errorf("read the %s limit: %w", what, err)
|
||||
}
|
||||
n, err := strconv.Atoi(strings.TrimSpace(string(value)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse the %s limit: %w", what, err)
|
||||
}
|
||||
if n == 0 {
|
||||
return fmt.Errorf("%s are disabled: %s is 0", what, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// openCommand runs the verified binary by its open descriptor with the host's
|
||||
// network, for a package that declares no egress hosts: a connector that
|
||||
// reaches hosts an operator configured, not hosts a manifest lists.
|
||||
|
||||
@@ -15,6 +15,12 @@ func confinedCommand(context.Context, *os.File, []string, string) (*exec.Cmd, er
|
||||
return nil, errors.New("extensions need Linux network namespaces to confine their egress")
|
||||
}
|
||||
|
||||
// namespacesAvailable refuses for the same reason: this platform has no
|
||||
// namespace to confine an extension to.
|
||||
func namespacesAvailable() error {
|
||||
return errors.New("extensions need Linux network namespaces to confine their egress")
|
||||
}
|
||||
|
||||
func openCommand(ctx context.Context, binary *os.File, args []string) *exec.Cmd {
|
||||
//nolint:gosec // the binary is the host-verified one, named by its path.
|
||||
return exec.CommandContext(ctx, binary.Name(), args...)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -123,83 +122,57 @@ func (h *Host) UseRemotes(cfg RemoteConfig) error {
|
||||
h.mu.Lock()
|
||||
h.requireRemote = cfg.RequireRemote
|
||||
h.remoteTLS = tlsCfg
|
||||
h.remotes = cloneEndpoints(endpoints)
|
||||
h.remotes = endpoints
|
||||
h.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// remoteRuntime returns the remote endpoints this host can dial.
|
||||
func (h *Host) remoteRuntime() RemoteGRPCRuntime {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return RemoteGRPCRuntime{tls: h.remoteTLS, endpoints: h.remotes}
|
||||
}
|
||||
|
||||
// localRuntime returns the subprocess runtime, which an operator disables by
|
||||
// requiring remote endpoints.
|
||||
func (h *Host) localRuntime() LocalNamespaceRuntime {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return LocalNamespaceRuntime{hclog: h.hclog, disabled: h.requireRemote}
|
||||
}
|
||||
|
||||
// runtimeFor picks the one runtime that serves spec. An endpoint the operator
|
||||
// mapped is the only runtime that may serve that extension, so a broken
|
||||
// endpoint is a refusal and never a fallback to a subprocess; without one,
|
||||
// local confinement is the only runtime. A host with neither runs no extension.
|
||||
func (h *Host) runtimeFor(spec Spec) (Runtime, error) {
|
||||
remote, local := h.remoteRuntime(), h.localRuntime()
|
||||
if _, mapped := remote.endpoint(spec); mapped {
|
||||
if err := remote.Available(spec); err != nil {
|
||||
return nil, fmt.Errorf("extension %q: %s runtime: %w", spec.Name, remote.Name(), err)
|
||||
}
|
||||
return remote, nil
|
||||
}
|
||||
localErr := local.Available(spec)
|
||||
if localErr == nil {
|
||||
return local, nil
|
||||
}
|
||||
return nil, fmt.Errorf("extension %q: no extension runtime is available (%s: %w; %s: %w)",
|
||||
spec.Name, remote.Name(), remote.Available(spec), local.Name(), localErr)
|
||||
}
|
||||
|
||||
// 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)
|
||||
rt, err := h.runtimeFor(spec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extension %q: dial remote %s: %w", spec.Name, ep.Address, err)
|
||||
return nil, err
|
||||
}
|
||||
raw, err := gp.GRPCClient(ctx, nil, conn)
|
||||
raw, p, err := rt.Start(ctx, spec, surface, impl)
|
||||
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)
|
||||
}
|
||||
// The verified file is what runs: the confined launcher execs this open
|
||||
// descriptor, so swapping the path after the check changes nothing.
|
||||
binary, err := openVerified(spec.Path, sum)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extension %q: %w", spec.Name, err)
|
||||
}
|
||||
defer func() { _ = binary.Close() }()
|
||||
cmd, g, err := command(ctx, binary, spec.Egress)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extension %q: %w", spec.Name, err)
|
||||
}
|
||||
cmd.Env = passEnv(spec.Env)
|
||||
p := &proc{gate: g, client: goplugin.NewClient(&goplugin.ClientConfig{
|
||||
HandshakeConfig: Handshake,
|
||||
Plugins: goplugin.PluginSet{surface: impl},
|
||||
Cmd: cmd,
|
||||
AllowedProtocols: []goplugin.Protocol{goplugin.ProtocolGRPC},
|
||||
SkipHostEnv: true,
|
||||
AutoMTLS: true,
|
||||
Logger: h.hclog.Named(spec.Name),
|
||||
})}
|
||||
rpc, err := p.client.Client()
|
||||
if err != nil {
|
||||
p.kill()
|
||||
return nil, fmt.Errorf("extension %q: connect: %w", spec.Name, err)
|
||||
}
|
||||
raw, err := rpc.Dispense(surface)
|
||||
if err != nil {
|
||||
p.kill()
|
||||
return nil, fmt.Errorf("extension %q: dispense %s: %w", spec.Name, surface, err)
|
||||
return nil, fmt.Errorf("extension %q: %s runtime: %w", spec.Name, rt.Name(), err)
|
||||
}
|
||||
h.replaceProc(cmp.Or(spec.Key, spec.Name), p)
|
||||
return raw, nil
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
@@ -52,3 +54,35 @@ func TestCommandConfinesOnlyWithEgress(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestNamespaceLimitRefusesDisabledNamespaces guards fail-closed extension
|
||||
// startup: a kernel or container runtime that caps user or network namespaces
|
||||
// at zero must be reported before a fork the kernel would deny, so an operator
|
||||
// learns to configure a remote endpoint. Namespace support was detected only by
|
||||
// the failing fork, which surfaced as an opaque plugin start error.
|
||||
func TestNamespaceLimitRefusesDisabledNamespaces(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "max_user_namespaces")
|
||||
cases := []struct {
|
||||
name string
|
||||
content string
|
||||
path string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "enabled", content: "255325\n"},
|
||||
{name: "disabled", content: "0\n", wantErr: true},
|
||||
{name: "unparseable", content: "yes\n", wantErr: true},
|
||||
{name: "absent", path: filepath.Join(dir, "absent"), wantErr: true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
if err := os.WriteFile(path, []byte(c.content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := namespaceLimit(cmp.Or(c.path, path), "user namespaces")
|
||||
if (err != nil) != c.wantErr {
|
||||
t.Fatalf("namespaceLimit(%q) = %v, want error = %v", c.content, err, c.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,40 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
// Runtime starts one extension. The host tries its runtimes in order and
|
||||
// refuses to start an extension when none of them can serve it, so an
|
||||
// environment with neither local confinement nor a remote endpoint runs no
|
||||
// extension at all.
|
||||
type Runtime interface {
|
||||
// Name identifies the runtime in a refusal.
|
||||
Name() string
|
||||
// Available reports why this runtime cannot serve spec, or nil when it can.
|
||||
Available(spec Spec) error
|
||||
// Start runs, or dials, the extension serving surface and returns the
|
||||
// dispensed client with the state that keeps it running. A runtime that
|
||||
// fails leaves nothing running.
|
||||
Start(ctx context.Context, spec Spec, surface string, impl goplugin.Plugin) (any, *proc, error)
|
||||
}
|
||||
|
||||
// RemoteTLS holds the mutual-TLS material used to authenticate remote extension
|
||||
// gRPC endpoints.
|
||||
type RemoteTLS struct {
|
||||
@@ -38,6 +57,130 @@ type RemoteConfig struct {
|
||||
Endpoints map[string]RemoteEndpoint
|
||||
}
|
||||
|
||||
// RemoteGRPCRuntime dials an extension that runs outside this process: a
|
||||
// sidecar socket or a dedicated pod reached over mTLS. It is the runtime a
|
||||
// Kubernetes cluster uses, where restricted Pod Security Standards forbid the
|
||||
// namespaces LocalNamespaceRuntime needs and NetworkPolicy admits only the
|
||||
// endpoint.
|
||||
type RemoteGRPCRuntime struct {
|
||||
tls *tls.Config
|
||||
endpoints map[string]RemoteEndpoint
|
||||
}
|
||||
|
||||
func (RemoteGRPCRuntime) Name() string { return "remote gRPC" }
|
||||
|
||||
func (r RemoteGRPCRuntime) Available(spec Spec) error {
|
||||
ep, ok := r.endpoint(spec)
|
||||
if !ok {
|
||||
return errors.New("no endpoint is configured")
|
||||
}
|
||||
if r.tls == nil && !strings.HasPrefix(ep.Address, "unix://") {
|
||||
return fmt.Errorf("endpoint %s needs mTLS credentials", ep.Address)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RemoteGRPCRuntime) Start(ctx context.Context, spec Spec, _ string, impl goplugin.Plugin) (any, *proc, error) {
|
||||
ep, ok := r.endpoint(spec)
|
||||
if !ok {
|
||||
return nil, nil, errors.New("no endpoint is configured")
|
||||
}
|
||||
gp, ok := impl.(goplugin.GRPCPlugin)
|
||||
if !ok {
|
||||
return nil, nil, errors.New("plugin does not support gRPC")
|
||||
}
|
||||
conn, err := dialRemote(ep, r.tls)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("dial %s: %w", ep.Address, err)
|
||||
}
|
||||
raw, err := gp.GRPCClient(ctx, nil, conn)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, nil, fmt.Errorf("client: %w", err)
|
||||
}
|
||||
return raw, &proc{conn: conn}, nil
|
||||
}
|
||||
|
||||
// endpoint finds the endpoint the operator mapped to spec, by installation ID
|
||||
// before name.
|
||||
func (r RemoteGRPCRuntime) endpoint(spec Spec) (RemoteEndpoint, bool) {
|
||||
if spec.Key != "" {
|
||||
if ep, ok := r.endpoints[spec.Key]; ok {
|
||||
return ep, true
|
||||
}
|
||||
}
|
||||
if spec.Name != "" {
|
||||
if ep, ok := r.endpoints[spec.Name]; ok {
|
||||
return ep, true
|
||||
}
|
||||
}
|
||||
return RemoteEndpoint{}, false
|
||||
}
|
||||
|
||||
// LocalNamespaceRuntime runs an extension as a local subprocess in its own user
|
||||
// and network namespace, which holds nothing but a relay to the egress gate. It
|
||||
// is the Compose and VM runtime; where the kernel or the container runtime
|
||||
// refuses those namespaces, an operator configures remote endpoints instead.
|
||||
type LocalNamespaceRuntime struct {
|
||||
hclog hclog.Logger
|
||||
// disabled is set when the operator requires remote endpoints, so no
|
||||
// extension runs as a subprocess of this process.
|
||||
disabled bool
|
||||
}
|
||||
|
||||
func (LocalNamespaceRuntime) Name() string { return "local namespace" }
|
||||
|
||||
func (r LocalNamespaceRuntime) Available(spec Spec) error {
|
||||
if r.disabled {
|
||||
return errors.New("local subprocess execution is disabled")
|
||||
}
|
||||
if len(spec.Egress) == 0 {
|
||||
// Nothing to project into a namespace: this package runs on the host's
|
||||
// network either way.
|
||||
return nil
|
||||
}
|
||||
return namespacesAvailable()
|
||||
}
|
||||
|
||||
func (r LocalNamespaceRuntime) Start(ctx context.Context, spec Spec, surface string, impl goplugin.Plugin) (any, *proc, error) {
|
||||
sum, err := hex.DecodeString(spec.SHA256)
|
||||
if err != nil || len(sum) != sha256.Size {
|
||||
return nil, nil, errors.New("sha256 must be a hex digest")
|
||||
}
|
||||
// The verified file is what runs: the confined launcher execs this open
|
||||
// descriptor, so swapping the path after the check changes nothing.
|
||||
binary, err := openVerified(spec.Path, sum)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer func() { _ = binary.Close() }()
|
||||
cmd, g, err := command(ctx, binary, spec.Egress)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cmd.Env = passEnv(spec.Env)
|
||||
p := &proc{gate: g, client: goplugin.NewClient(&goplugin.ClientConfig{
|
||||
HandshakeConfig: Handshake,
|
||||
Plugins: goplugin.PluginSet{surface: impl},
|
||||
Cmd: cmd,
|
||||
AllowedProtocols: []goplugin.Protocol{goplugin.ProtocolGRPC},
|
||||
SkipHostEnv: true,
|
||||
AutoMTLS: true,
|
||||
Logger: r.hclog.Named(spec.Name),
|
||||
})}
|
||||
rpc, err := p.client.Client()
|
||||
if err != nil {
|
||||
p.kill()
|
||||
return nil, nil, fmt.Errorf("connect: %w", err)
|
||||
}
|
||||
raw, err := rpc.Dispense(surface)
|
||||
if err != nil {
|
||||
p.kill()
|
||||
return nil, nil, fmt.Errorf("dispense %s: %w", surface, err)
|
||||
}
|
||||
return raw, p, nil
|
||||
}
|
||||
|
||||
func validateRemoteConfig(cfg RemoteConfig) (*tls.Config, map[string]RemoteEndpoint, error) {
|
||||
endpoints := make(map[string]RemoteEndpoint, len(cfg.Endpoints))
|
||||
needTLS := false
|
||||
@@ -127,25 +270,3 @@ func dialRemote(ep RemoteEndpoint, baseTLS *tls.Config) (*grpc.ClientConn, error
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -169,9 +169,11 @@ func TestRemoteGRPCRequiresMTLSAndEnforcesTrust(t *testing.T) {
|
||||
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)
|
||||
// 4. An unmapped extension fails closed when RequireRemote is true, and the
|
||||
// refusal names both runtimes so an operator sees what is missing.
|
||||
_, err = h.Start(context.Background(), Spec{Name: "local-only"}, "secretengine", &testEnginePlugin{})
|
||||
if err == nil || !strings.Contains(err.Error(), "local subprocess execution is disabled") || !strings.Contains(err.Error(), "remote gRPC") {
|
||||
t.Fatalf("expected a fail-closed refusal naming both runtimes, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user