Files
2026-10-25 22:59:58 -04:00

239 lines
9.2 KiB
Go

// Copyright 2026 Kryptkeeper Authors
//
// SPDX-License-Identifier: Apache-2.0
package bootstrap
// Renewal through delivery plugins (W20-20): install_mode "plugin" targets
// push renewed material through their enabled plugin, sandboxed, with the
// hub's own verify and rollback (internal/plugins/delivery.Deliver).
import (
"context"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"kryptkeeper/internal/logging"
"net"
"net/http"
"sort"
"strings"
"time"
"github.com/bpmforge/kryptkeeper/sdk/delivery-go/abi"
"kryptkeeper/internal/agentinstall"
"kryptkeeper/internal/plugins/delivery"
)
// WithPluginDeliverer arms install_mode "plugin" targets with the process's
// plugin manager; audit receives one row per plugin run and per refused
// host call. A nil manager (plugins off) makes plugin targets fail plainly.
func (e *RenewalExecutor) WithPluginDeliverer(m *DeliveryPluginManager, audit AuditProvider) *RenewalExecutor {
e.plugins = m
e.pluginAudit = audit
return e
}
// pluginRun is a plugin, the version that runs, and the environment one
// target gives it.
type pluginRun struct {
plugin *delivery.Plugin
version DeliveryPluginVersion
env delivery.Env
}
// preparePlugin resolves the target's enabled plugin and its credential,
// and checks both against the plugin's schemas (D-119): required fields
// present, defaults applied, and nothing the schema does not name handed
// to the plugin.
func (e *RenewalExecutor) preparePlugin(ctx context.Context, target ManagedTarget) (pluginRun, error) {
if e.plugins == nil {
return pluginRun{}, errPluginsOff
}
p, v, err := e.plugins.Enabled(ctx, target.PluginID)
if err != nil {
return pluginRun{}, fmt.Errorf("delivery plugin %q: %w", target.PluginID, err)
}
if v.Kind != abi.KindDelivery {
return pluginRun{}, fmt.Errorf("plugin %q is a %s plugin, not a delivery plugin", target.PluginID, v.Kind)
}
if v.Manifest.Capabilities.RemoteExec {
return pluginRun{}, fmt.Errorf("delivery plugin %q needs remote_exec, which plugin targets do not support yet", target.PluginID)
}
env, err := buildPluginEnv(ctx, e.creds, e.pushClient, v, target.CredentialRef, target.PluginSettings)
if err != nil {
return pluginRun{}, err
}
return pluginRun{plugin: p, version: v, env: env}, nil
}
// buildPluginEnv resolves a stored credential and the target's settings into
// the Env one plugin operation runs with, checked against the manifest's
// schemas (D-119). Delivery targets and notification channels share it, so a
// credential is read, validated and redacted the same way for both.
func buildPluginEnv(ctx context.Context, creds OutboundCredentialStore, base *http.Client, v DeliveryPluginVersion, credentialRef string, settings map[string]string) (delivery.Env, error) {
pluginID := v.PluginID
if strings.TrimSpace(credentialRef) == "" {
return delivery.Env{}, errRenewalNoCredential
}
if creds == nil {
return delivery.Env{}, errors.New("outbound credential store not configured (set APP_CREDENTIAL_STORE_PASSPHRASE or KRYPTKEEPER_KEYSTORE_PASSPHRASE)")
}
meta, err := creds.Get(ctx, credentialRef)
if err != nil {
return delivery.Env{}, fmt.Errorf("credential %q: %w", credentialRef, err)
}
if meta.CredentialType != CredentialTypeAPIToken {
return delivery.Env{}, fmt.Errorf("%w: plugins need an %s credential holding a JSON object of the plugin's credential fields; %q is %s",
errRenewalBadCredential, CredentialTypeAPIToken, credentialRef, meta.CredentialType)
}
plaintext, err := creds.Resolve(ctx, credentialRef)
if err != nil {
return delivery.Env{}, fmt.Errorf("credential %q: %w", credentialRef, err)
}
defer zeroize(plaintext)
var raw map[string]string
if err := json.Unmarshal(plaintext, &raw); err != nil {
return delivery.Env{}, fmt.Errorf("%w: credential %q is not a JSON object of string fields", errRenewalBadCredential, credentialRef)
}
client, err := pluginHTTPClient(base, raw[credentialFieldTLSCA])
if err != nil {
return delivery.Env{}, fmt.Errorf("%w: credential %q: %v", errRenewalBadCredential, credentialRef, err)
}
env := delivery.Env{Credential: map[string]string{}, Target: map[string]string{}, HTTPClient: client}
var missing []string
for name, f := range v.Manifest.CredentialSchema {
val, ok := raw[name]
if !ok || val == "" {
val = f.Default
}
if f.Required && val == "" {
missing = append(missing, "credential."+name)
continue
}
if val != "" {
env.Credential[name] = val
}
if f.Secret {
env.Secrets = append(env.Secrets, name)
}
}
for name, f := range v.Manifest.TargetSchema {
val := settings[name]
if val == "" {
val = f.Default
}
if f.Required && val == "" {
missing = append(missing, "plugin_settings."+name)
continue
}
if val != "" {
env.Target[name] = val
}
}
if len(missing) > 0 {
sort.Strings(missing)
return delivery.Env{}, fmt.Errorf("plugin %q is missing required fields: %s", pluginID, strings.Join(missing, ", "))
}
return env, nil
}
// credentialFieldTLSCA is a credential field the hub itself reads: a PEM CA
// bundle the device's management certificate chains to (devices often use
// an internal CA). It extends trust for this target's plugin HTTPS only and
// reaches the plugin only if its schema names the field.
const credentialFieldTLSCA = "tls_ca_pem"
// pluginHTTPClient is base (or the guarded push client) trusting the system
// roots plus caPEM, when given. The plugin cannot change TLS trust itself.
// The tls_ca_pem path builds from the egress client too: a fresh stdlib
// client would take proxies from the environment and skip the dial-time
// address check, letting a granted hostname that resolves to link-local or
// metadata space through.
func pluginHTTPClient(base *http.Client, caPEM string) (*http.Client, error) {
if strings.TrimSpace(caPEM) == "" {
if base != nil {
return base, nil
}
return pushDeviceHTTPClient(nil), nil
}
pool, err := x509.SystemCertPool()
if err != nil || pool == nil {
pool = x509.NewCertPool()
}
if !pool.AppendCertsFromPEM([]byte(caPEM)) {
return nil, errors.New("tls_ca_pem holds no PEM certificate")
}
return pushDeviceHTTPClient(pool), nil
}
// maxPluginVerifyWindow is the longest verify propagation window a plugin
// manifest may declare (W27-37). Execution budgets that wrap a plugin push
// add it so a declared window is never cut short by the caller's context.
const maxPluginVerifyWindow = time.Duration(abi.MaxVerifyPropagationSeconds) * time.Second
// pushViaPlugin delivers already-renewed material through the target's
// plugin. The result's Verified and RolledBack are the hub's findings.
func (e *RenewalExecutor) pushViaPlugin(ctx context.Context, target ManagedTarget, profile RenewalInstallProfile, chainPEM, keyPEM []byte, leafFP string) (agentinstall.InstallResult, error) {
run, err := e.preparePlugin(ctx, target)
if err != nil {
return agentinstall.InstallResult{}, err
}
refused := 0
run.env.Events = func(ev delivery.Event) {
switch ev.Kind {
case "refused":
refused++
e.appendPluginAudit(ctx, "delivery_plugin.refused", target.PluginID, AuditOutcomeDenied,
fmt.Sprintf("target=%s version=%s %s", target.ID, run.version.Version, ev.Detail))
case "log":
logging.Infof("delivery plugin %s (target %s): %s", target.PluginID, target.ID, ev.Detail)
}
}
// A read-back plugin (secret store: nothing to dial) is verified by the
// hub fingerprinting the PEM the store returns; an operator-set
// verify_host_port always forces the stronger TLS dial instead.
readback := run.version.Manifest.Verification == abi.VerificationReadback && profile.VerifyHostPort == ""
verification := abi.VerificationTLS
verifyHostPort := profile.VerifyHostPort
if readback {
verification, verifyHostPort = abi.VerificationReadback, ""
} else if verifyHostPort == "" {
verifyHostPort = net.JoinHostPort(target.Host, "443")
}
verify := e.verifier
if verify == nil {
verify = agentinstall.HubVerifier
}
res := run.plugin.Deliver(ctx, run.env, delivery.DeliverRequest{
Name: pushObjectName(target.Name), CertPEM: chainPEM, KeyPEM: keyPEM,
VerifyHostPort: verifyHostPort, ExpectedFingerprint: leafFP, ReadbackVerify: readback,
}, verify)
outcome := AuditOutcomeSuccess
if !res.Verified && res.Outcome != agentinstall.OutcomeInstalled {
outcome = AuditOutcomeFailure
}
e.appendPluginAudit(ctx, "delivery_plugin.push", target.PluginID, outcome,
fmt.Sprintf("target=%s version=%s sha256=%s verification=%s verify_window=%ds outcome=%s verified=%t rolled_back=%t refused_calls=%d",
target.ID, run.version.Version, run.version.SHA256, verification, run.version.Manifest.VerifyPropagationSeconds, res.Outcome, res.Verified, res.RolledBack, refused))
var pushErr error
if outcome == AuditOutcomeFailure {
pushErr = fmt.Errorf("delivery plugin %s: %s: %s", target.PluginID, res.Outcome, res.Detail)
}
return res, pushErr
}
func (e *RenewalExecutor) appendPluginAudit(ctx context.Context, action, pluginID string, outcome AuditOutcome, detail string) {
if e.pluginAudit == nil {
return
}
if err := e.pluginAudit.AppendEntry(context.WithoutCancel(ctx), AuditEntry{
Timestamp: time.Now().UTC().Format(time.RFC3339), Action: action, ActorType: "system", ActorID: "renewal",
ResourceType: "delivery_plugin", ResourceID: pluginID, Outcome: outcome, ChangeSummary: detail,
}); err != nil {
logging.Warnf("audit append failed action=%s: %v", action, err)
}
}