31ccb52114
Wave B/C/D of P01 mTLS implementation.
- internal/audit/audit.go — thin wrapper around engine.Audit for
cert/handshake events (Action* and Result* constants; REQ-038).
- internal/certpaths/ — extracted path constants out of cli to break
the cli<->doctor import cycle; cli re-exports the helpers for
backward compat.
- internal/security/ca.go — public WriteCert/WriteKey helpers (0600
for keys, 0644 for certs; REQ-033); used by the cert CLI and
integration test.
- internal/daemon/tls.go — mTLS server with GetCertificate hot-swap
callback. Plaintext HTTP remains the default for v0.1 compat;
StartMTLS() flips the server into mTLS mode.
- internal/daemon/server.go — adds mtls *MTLSState field; MTLSActive()
getter for health endpoints.
- internal/transport/mtls.go — mTLS client with VerifyPeerCertificate
for pinned peer identity; DialContext for raw TLS.
- internal/transport/handshake_log.go — structured slog helpers for
handshake ok/fail (REQ-038 fields: event, result, peer, cert_fp).
- internal/cli/cert.go — orca cert {ca-init,gen,show,renew,fingerprint}
subcommands; file mode enforcement at every entry; redacted cert
show (REQ-035).
- internal/cli/doctor.go — orca doctor {cert,network,db} subcommands
(REQ-032); --json output supported.
- internal/cli/node.go — adds --ca-fingerprint to orca node join
(REQ-026); fails fast on mismatch.
- internal/doctor/doctor.go — 6 checks: cert.ca, cert.server,
cert.expiry, cert.fingerprint, network stub, db stub.
- internal/doctor/doctor_test.go — happy + sad path coverage.
- internal/security/integration_test.go — end-to-end: CA-init, CSR
generation, mTLS handshake, mismatch failure, rotation alarm,
redaction, file mode enforcement.
All tests pass with -race; gofmt -l . clean; go vet ./... clean.
---ci---
project: orca
phase: 8
milestone: v0.2
status: execute
---/ci---
126 lines
4.4 KiB
Go
126 lines
4.4 KiB
Go
// Package audit provides a thin convenience wrapper around
|
|
// engine.Audit tailored to mTLS / cert lifecycle events. It exists so
|
|
// that cert, transport, and daemon code can call a small, semantically
|
|
// clear API (Emit with explicit action + result) without depending on
|
|
// the more general-purpose engine.Audit.
|
|
package audit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"git.cloudinit.dev/coreci/orca/internal/engine"
|
|
)
|
|
|
|
// Result enumerates the result strings persisted to audit_log. Keeping
|
|
// these as constants (rather than free-form strings) prevents typos at
|
|
// call sites and makes log analytics trivial.
|
|
type Result string
|
|
|
|
const (
|
|
ResultSuccess Result = "success"
|
|
ResultFailure Result = "failure"
|
|
ResultDenied Result = "denied"
|
|
)
|
|
|
|
// Action enumerates the cert / handshake event names used across the
|
|
// security surface. Matches REQ-038 / P01 must-haves:
|
|
//
|
|
// cert.issued — a CSR was signed, server cert persisted
|
|
// cert.renewed — a server cert was re-issued (rotation)
|
|
// cert.joined — a node joined the trust domain (CA pinned)
|
|
// node.handshake_ok — mTLS handshake succeeded
|
|
// node.handshake_failed — mTLS handshake failed
|
|
type Action string
|
|
|
|
const (
|
|
ActionCertIssued Action = "cert.issued"
|
|
ActionCertRenewed Action = "cert.renewed"
|
|
ActionCertJoined Action = "cert.joined"
|
|
ActionNodeHandshakeOK Action = "node.handshake_ok"
|
|
ActionNodeHandshakeFail Action = "node.handshake_failed"
|
|
)
|
|
|
|
// Audit wraps engine.Audit with a cert/handshake-focused API.
|
|
type Audit struct {
|
|
engine *engine.Audit
|
|
}
|
|
|
|
// New constructs an Audit backed by the given engine.Audit. The engine
|
|
// instance persists to the audit_log table; the wrapper just shapes
|
|
// the call signature.
|
|
func New(e *engine.Audit) *Audit {
|
|
return &Audit{engine: e}
|
|
}
|
|
|
|
// Emit persists an audit entry. The `event` is a free-form description
|
|
// that ends up in the resource field, paired with action + result. Use
|
|
// the Action* constants for `action`; free-form strings for `event` are
|
|
// allowed for extensibility but should be stable for analytics.
|
|
func (a *Audit) Emit(ctx context.Context, action Action, event string, result Result, metadata map[string]any) {
|
|
if a == nil || a.engine == nil {
|
|
return
|
|
}
|
|
// Resource field is conventionally <event>:<id>; we just use event
|
|
// as-is here. Callers can stuff the relevant id into metadata.
|
|
a.engine.Record(ctx, "security", string(action), event, string(result), nil, metadata)
|
|
}
|
|
|
|
// EmitWithErr persists a failure entry whose err is also recorded in the
|
|
// audit_log.error column. Use this for handshake failures and similar
|
|
// error paths where the underlying error is useful for postmortem.
|
|
func (a *Audit) EmitWithErr(ctx context.Context, action Action, event string, err error, metadata map[string]any) {
|
|
if a == nil || a.engine == nil {
|
|
return
|
|
}
|
|
a.engine.Record(ctx, "security", string(action), event, string(ResultFailure), err, metadata)
|
|
}
|
|
|
|
// LogHandshakeOK emits a structured slog record for a successful mTLS
|
|
// handshake. This is a SEPARATE log line from the audit_log entry —
|
|
// structured slog is for operators; audit_log is for compliance.
|
|
func LogHandshakeOK(log *slog.Logger, peer, certFP string) {
|
|
if log == nil {
|
|
return
|
|
}
|
|
log.Info("mtls.handshake",
|
|
slog.String("event", "mtls.handshake"),
|
|
slog.String("result", "ok"),
|
|
slog.String("peer", peer),
|
|
slog.String("cert_fp", certFP),
|
|
)
|
|
}
|
|
|
|
// LogHandshakeFailed emits a structured slog record for a failed mTLS
|
|
// handshake. Per REQ-038, the fields are: event=mtls.handshake, peer,
|
|
// cert_fp (may be empty if no cert was presented), err.
|
|
func LogHandshakeFailed(log *slog.Logger, peer, certFP string, err error) {
|
|
if log == nil {
|
|
return
|
|
}
|
|
attrs := []any{
|
|
slog.String("event", "mtls.handshake"),
|
|
slog.String("result", "failed"),
|
|
slog.String("peer", peer),
|
|
slog.String("cert_fp", certFP),
|
|
}
|
|
if err != nil {
|
|
attrs = append(attrs, slog.String("err", err.Error()))
|
|
}
|
|
log.Warn("mtls.handshake", attrs...)
|
|
}
|
|
|
|
// String converts an Action to its canonical string form. Useful in
|
|
// tests and CLI surface.
|
|
func (a Action) String() string { return string(a) }
|
|
|
|
// String converts a Result to its canonical string form.
|
|
func (r Result) String() string { return string(r) }
|
|
|
|
// FormatAction formats an action+result pair as "action=... result=...",
|
|
// used by callers building structured log lines.
|
|
func FormatAction(action Action, result Result) string {
|
|
return fmt.Sprintf("action=%s result=%s", action, result)
|
|
}
|