← Back to Pull requests

Provide Spire Workload Attestation Support to ztunnel #4

Closed
unknown closed this pull request without merging on Mar 09, 2026 22:09 UTC
View on GitHub
unknown unknown commented on Feb 05, 2026 19:38 UTC
Author

SPIRE Delegated Identity API Integration for ztunnel

Overview

This document describes the design and implementation of SPIRE integration in ztunnel using the Delegated Identity API. The implementation supports one attestation modes: PID-based, each with different security and efficiency trade-offs.

Background

Current ztunnel Certificate Management

The existing ztunnel certificate management uses SecretManager to cache certificates by Identity (SPIFFE ID). When multiple pods share the same service account, they share a single cached certificate, reducing CA calls and memory usage.

SPIRE Delegated Identity API

SPIRE's Delegated Identity API allows a trusted delegate (ztunnel) to request certificates on behalf of workloads. The API supports two attestation methods:

PID: Identify workloads by their process ID for stronger attestation

Design

Attestation Modes

In PID mode, each workload is attested individually using its container process ID. This approach:

  • Provides stronger security through per-workload attestation

  • Each pod receives its own certificate from SPIRE

  • Higher SPIRE server load and memory usage

  • SPIRE verifies the actual running process, not just Kubernetes metadata
  • ┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
    │  Pod A (PID 1)  │     │  Pod B (PID 2)  │     │  Pod C (PID 3)  │
    └────────┬────────┘     └────────┬────────┘     └────────┬────────┘
             │                       │                       │
             ▼                       ▼                       ▼
    ┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
    │  Certificate A  │     │  Certificate B  │     │  Certificate C  │
    │ (SPIRE call #1) │     │ (SPIRE call #2) │     │ (SPIRE call #3) │
    └─────────────────┘     └─────────────────┘     └─────────────────┘
    

    CompositeId Design

    Motivation

    The original SecretManager used Identity as the cache key. To support PID-based attestation while maintaining backward compatibility with the existing CaClientTrait interface, we introduced CompositeId<RequestKeyEnum>.

    Structure

    pub struct CompositeId<RequestKeyEnum> {
        id: Identity,           // The SPIFFE identity (ns/sa)
        key: RequestKeyEnum,    // Distinguishes individual workloads
    }
    
    pub enum RequestKeyEnum {
        Identity(Identity),     // For backwards compability
        Workload(WorkloadUid),  // For PID mode: key by workload UID
    }
    

    Trade-offs

    This design was chosen to maintain backward compatibility with CaClientTrait:

    #[async_trait]
    pub trait CaClientTrait: Send + Sync {
        async fn fetch_certificate(
            &self, 
            id: &CompositeId<RequestKeyEnum>
        ) -> Result<tls::WorkloadCertificate, Error>;
    }
    

    Benefits:

  • Single interface works for both SPIRE modes and the original CA client

  • No breaking changes to existing code paths

  • SecretManager can track per-workload state when needed
  • Consequences:

  • In PID mode, SecretManager caches by CompositeId, resulting in one cache entry per pod even if they share the same identity

  • This is intentional—each workload must be individually attested
  • PID Verification Flow

    In PID mode, ztunnel performs the following steps:

    1. Receive certificate request with WorkloadUid
    2. Query Container Runtime (CRI) for the container's PID
    3. Call SPIRE with the PID for attestation
    4. Re-verify PID after SPIRE returns (guards against PID reuse attacks)
    5. Return certificate to caller

    async fn get_cert_by_pid(&self, pid: i32, wl_uid: &WorkloadUid) -> Result<...> {
        // 1. Get certificate from SPIRE using PID
        let certs = self.get_cert_from_spire(DelegateAttestationRequest::Pid(pid)).await;
        
        // 2. Re-verify PID hasn't changed (TOCTOU protection)
        if let Some(pid_client) = &self.pid {
            let fetched_pid = pid_client.fetch_pid(wl_uid).await?;
            if fetched_pid.into_i32() != pid {
                return Err(Error::UnableToDeterminePidForWorkload(...));
            }
        }
        
        Ok(certs?)
    }
    

    Comparison Summary

    | Aspect | Selector Mode | PID Mode |
    |--------|---------------|----------|
    | Attestation Granularity | Per identity (ns/sa) | Per workload (pod) |
    | Certificate Sharing | Yes—same identity shares cert | No—each pod gets own cert |
    | SPIRE Calls | 1 per unique identity | 1 per pod |
    | Memory Usage | Lower | Higher |
    | Security Level | Standard | Enhanced |
    | Cache Key | CompositeId with Identity key | CompositeId with WorkloadUid key |

    Configuration

    # Enable SPIRE integration
    spire_enabled: true
    
    # Choose attestation mode
    spire_mode: "ByPid"  # or "BySelectors"
    
    # SPIRE socket path
    spire_socket_path: "/run/spire/sockets/agent.sock"
    
    # Timeout for SPIRE operations  
    spire_timeout: "30s"
    

    Future Considerations

    1. Certificate Caching with Per-Pod Attestation: In PID mode, we should cache and reuse certificates by Identity while still attesting every pod individually. This would reduce SPIRE server load and memory usage—multiple pods with the same identity would share one certificate after each pod passes local PID verification. The first pod triggers a SPIRE call; subsequent pods with the same identity only require local PID verification before reusing the cached certificate.

    2. Collaborate with SPIRE/SPIFFE Community: Work with the SPIRE and SPIFFE community to improve the Delegated Identity API and related interfaces to better support delegated attestation use cases like ztunnel's.

    3. Consider a different trait for attested workloads instead of modifying fetch_certificate.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: MikeZappa87 Review duration: 46231m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository