fc94326b0e
P00 — Re-architecture Foundation (deprecation/migration/test-infra/persona/docs). Deprecation sweep (REQ-068, REQ-072, REQ-089): - Add // Deprecated: doc comments to internal/daemon (R-001), internal/transport (REQ-073), internal/security/ca.go+csr.go (D-101/REQ-076), internal/engine/ dispatcher.go+peer.go (CLI-side scheduler), internal/cli/daemon.go. - orca daemon emits slog.Warn deprecation banner on every run (ungated); fires R-001 + v0.10-P05 drain-and-stop + v0.10-P14 deletion. - orca cert and orca node join (mTLS path) emit deprecation warnings; proxmox SSH path (the v0.9 replacement) does not warn. - Add --no-deprecation-warnings global flag on root command (PersistentPreRunE) for orca upgrade migrations. - 12 new daemon/cert/node deprecation tests in internal/cli/daemon_test.go (cli coverage 81.9%, warnDeprecated 100%). - Add DEPRECATED banners to v0.8 sections of ARCHITECTURE.md (verified the v0.9 supersession section + Supersession Table from prior turn are present). Bash tooling gate (grill C-06, C-15, C-16, C-17, C-18): - scripts/tests/test_helper.bash + example_test.bash — bats framework + helpers. - scripts/lib/orca-log.sh — slog-compatible JSON logging to syslog (C-17). - scripts/orca-verify-render.sh — render-contract validator skeleton (C-16). - scripts/tests/orca-log_test.bash + orca-verify-render_test.bash — 20 bats tests total (happy + failure paths per C-15). - .shellcheckrc — project shellcheck config. - Makefile: test-bash + lint-bash targets (graceful skip if tools missing); wired into test + lint targets. - internal/emit/contract.go + contract_test.go — versioned JSON render contract (orca.emit/v1) between Go emitters and bash appliers (C-16). - .ciagent/BASH_CAPABILITY_MAP_v0.9.md — maps shipped internal/transport capabilities to bash-side equivalents or accepted drops (C-18). - D-186 recorded in PROJECT.md: bash exempt from Go coverage gate; compensating control is bats + shellcheck + shfmt (C-06). verify-reqs: 90 requirements consistent. Build/test/lint/fmt all green. 20 bats tests pass. Go tests pass. No v0.8 code deleted — only marked deprecated (deletion deferred to v0.10-P14 per REQ-090 dual-write window). ---ci--- project: orca phase: P00 milestone: v0.9 status: execute ---/ci---
133 lines
4.8 KiB
Go
133 lines
4.8 KiB
Go
// Package transport contains the cross-node transport primitives for
|
|
// orca. mTLS is the v0.2 baseline (D-011..D-015); clients and servers
|
|
// use stdlib crypto/tls with TLS 1.3 only and an AEAD cipher allowlist.
|
|
//
|
|
// The transport layer deliberately depends on the stdlib only — no
|
|
// gRPC, no ConnectRPC, no third-party transport libraries. This keeps
|
|
// the binary lean (matches the minimalist pillar) and the trust chain
|
|
// auditable (one library: the Go stdlib).
|
|
//
|
|
// Deprecated: v0.9 re-architecture replaces this with
|
|
// internal/sshpush (REQ-073). The daemon-to-daemon mTLS transport is
|
|
// removed because servers no longer run the orca binary (R-001); the
|
|
// CLI pushes config via SSH instead. Scheduled for deletion in
|
|
// v0.10-P14. See .ciagent/PRD_v0.9.md R-001/R-006.
|
|
package transport
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.cloudinit.dev/coreci/orca/internal/security"
|
|
)
|
|
|
|
// MTLSClient wraps an http.Client configured for mTLS. The client
|
|
// verifies the server cert against the pinned CA and the expected
|
|
// server name (typically the SAN on the server cert).
|
|
type MTLSClient struct {
|
|
caPath string
|
|
serverName string
|
|
clientCert string
|
|
clientKey string
|
|
http *http.Client
|
|
}
|
|
|
|
// NewMTLSClient constructs an mTLS client.
|
|
//
|
|
// caPath is the path to the CA cert (PEM). The client's RootCAs is set
|
|
// to this single CA, so the server cert MUST be signed by it (REQ-011).
|
|
// serverName is the expected DNS name on the server cert's SAN list
|
|
// (REQ-036).
|
|
//
|
|
// certPath and keyPath are optional; if both are non-empty, the client
|
|
// presents them during the handshake. Pass empty strings for clients
|
|
// that don't authenticate themselves.
|
|
func NewMTLSClient(caPath, serverName, certPath, keyPath string) (*MTLSClient, error) {
|
|
if caPath == "" {
|
|
return nil, errors.New("NewMTLSClient: caPath is required")
|
|
}
|
|
if serverName == "" {
|
|
return nil, errors.New("NewMTLSClient: serverName is required (must match server cert SAN)")
|
|
}
|
|
tlsCfg, err := security.ClientTLSConfig(caPath, serverName, certPath, keyPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewMTLSClient: %w", err)
|
|
}
|
|
// Tighten the http.Client transport. The defaults (DefaultTransport)
|
|
// would reuse connections too aggressively for our needs; we want
|
|
// per-request timeout and a fresh dial per request to ensure cert
|
|
// rotation is picked up promptly.
|
|
tr := &http.Transport{
|
|
TLSClientConfig: tlsCfg,
|
|
MaxIdleConns: 10,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
DisableCompression: true,
|
|
}
|
|
return &MTLSClient{
|
|
caPath: caPath,
|
|
serverName: serverName,
|
|
clientCert: certPath,
|
|
clientKey: keyPath,
|
|
http: &http.Client{Transport: tr, Timeout: 30 * time.Second},
|
|
}, nil
|
|
}
|
|
|
|
// Do executes an HTTP request over mTLS. Returns the response or an
|
|
// error. On TLS handshake failure, wraps the error with structured
|
|
// context for the audit/handshake_log package.
|
|
func (c *MTLSClient) Do(req *http.Request) (*http.Response, error) {
|
|
if c == nil || c.http == nil {
|
|
return nil, errors.New("MTLSClient: nil receiver")
|
|
}
|
|
return c.http.Do(req)
|
|
}
|
|
|
|
// VerifyPeerCertificate is a tls.Config.VerifyPeerCertificate callback
|
|
// that enforces a pinned peer identity. Use it on the client side to
|
|
// reject certs that match the CA but are not the expected server.
|
|
//
|
|
// expectedFingerprint is the SHA-256 hex of the server cert DER. If it
|
|
// matches, the connection is allowed. If not, the handshake is
|
|
// aborted with a clear error.
|
|
func VerifyPeerCertificate(expectedFingerprint string) func([][]byte, [][]*x509.Certificate) error {
|
|
return func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
if len(rawCerts) == 0 {
|
|
return errors.New("VerifyPeerCertificate: no peer certs presented")
|
|
}
|
|
leaf, err := x509.ParseCertificate(rawCerts[0])
|
|
if err != nil {
|
|
return fmt.Errorf("VerifyPeerCertificate: parse leaf: %w", err)
|
|
}
|
|
got := security.FingerprintOf(leaf.Raw)
|
|
if got != expectedFingerprint {
|
|
return fmt.Errorf("VerifyPeerCertificate: peer fingerprint mismatch: got %s, want %s",
|
|
got, expectedFingerprint)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// DialContext dials a TCP address over raw TLS (no HTTP). Returns a
|
|
// tls.Conn. Used for low-level handshake tests; the mTLS client above
|
|
// is what production code uses.
|
|
func DialContext(ctx context.Context, network, addr, caPath, serverName string) (net.Conn, error) {
|
|
if caPath == "" {
|
|
return nil, errors.New("DialContext: caPath is required")
|
|
}
|
|
tlsCfg, err := security.ClientTLSConfig(caPath, serverName, "", "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("DialContext: %w", err)
|
|
}
|
|
d := &net.Dialer{Timeout: 5 * time.Second}
|
|
return tls.DialWithDialer(d, network, addr, tlsCfg)
|
|
}
|