Debug Container Approach Implementation¶
This document outlines the technical implementation of the Debug Container approach for scanning distroless containers.
Core Concept¶
The Debug Container approach leverages Kubernetes ephemeral containers to:
- Attach a temporary container with debugging tools to a pod with distroless containers
- Share process and filesystem namespaces with the target container
- Access the target container's filesystem through the process filesystem
- Execute CINC Auditor scans through the debug container
- Remove the debug container when scanning is complete
Technical Workflow¶
Step 1: Create an Ephemeral Debug Container¶
This command:
- Attaches a debug container running CINC Auditor to the target pod
- Targets the specific distroless container
- Shares the process namespace
- Makes the debug container sleep to keep it alive during scanning
Step 2: Access the Target Container's Filesystem¶
Once the debug container is running:
- Find the process IDs of the target container
- Identify the main process of the target application
- Access the container's filesystem through the process namespace
Step 3: Execute CINC Auditor Scan¶
The scan can be executed in two ways:
Option 1: Direct Filesystem Scanning¶
This approach:
- Directly targets the filesystem path
- Works for filesystem-based controls
- Has limitations for process and system resource inspections
Option 2: Chroot-Based Scanning¶
This approach:
- Creates a more complete environment for scanning
- Makes the target filesystem appear as root
- Allows more accurate scanning of filesystem resources
- Requires elevated permissions for chroot
Step 4: Retrieve and Process Results¶
Step 5: Clean Up Resources¶
Script Implementation¶
The project implements this approach in the scan-distroless-container.sh
script:
The script automates the following:
- Create temporary RBAC permissions
- Generate temporary kubeconfig
- Detect if the container is distroless
- Launch debug container with CINC Auditor
- Execute scan with appropriate chroot
- Retrieve and process results
- Clean up temporary resources
Technical Implementation Details¶
Ephemeral Container Definition¶
The ephemeral container is defined as:
Key elements:
targetContainerName
: Specifies which container to attach toprivileged: true
: Required for chroot operationsshareProcessNamespace: true
: Enables access to the target container's processes
RBAC Requirements¶
Additional RBAC permissions are needed:
The key addition is the pods/ephemeralcontainers
resource permission.
Chroot Implementation¶
The chroot-based scanning uses the following approach:
Technical Diagram¶
sequenceDiagram
participant User
participant Script as scan-distroless-container.sh
participant K8sAPI as Kubernetes API
participant Pod as Target Pod
participant Debug as Debug Container
participant CINC as CINC Auditor
User->>Script: Run script with params
Script->>K8sAPI: Create RBAC resources
Script->>K8sAPI: Create ephemeral debug container
K8sAPI->>Pod: Attach debug container
Script->>K8sAPI: Exec into debug container
K8sAPI->>Debug: Execute commands
Debug->>Debug: Find target process
Debug->>Debug: Set up chroot environment
Debug->>CINC: Run CINC Auditor in chroot
CINC->>Debug: Generate results
Script->>K8sAPI: Copy results from debug container
K8sAPI->>Debug: Get result file
Debug->>Script: Return results
Script->>Script: Process results
Script->>K8sAPI: Remove debug container
Script->>K8sAPI: Clean up RBAC resources
Script->>User: Display results summary
Special Considerations¶
Detection of Distroless Containers¶
The script detects distroless containers by:
- Attempting to execute a basic command
- Checking for common shells
- Looking for specific distroless container patterns
Security Context Requirements¶
For chroot-based scanning, the debug container needs:
Without this, the chroot operation will fail.
Limitations and Fallbacks¶
The script implements fallbacks:
- First attempts direct filesystem scanning
- Falls back to chroot if more complete environment access is needed
- Can operate without chroot but with reduced scan capabilities