Cluster Scanning
Spotter can scan live Kubernetes clusters to identify security vulnerabilities and misconfigurations in running workloads. This guide covers cluster scanning capabilities, setup, and best practices.
Cluster scanning provides:
- Live Resource Analysis: Scan running pods, services, and configurations
- Runtime Security: Identify security issues in active workloads
- Compliance Monitoring: Continuous compliance validation
- Drift Detection: Compare running state with desired configurations
- Multi-Cluster Support: Scan multiple clusters from a single command
Prerequisites
Section titled “Prerequisites”Kubernetes Access
Section titled “Kubernetes Access”# Verify cluster accesskubectl cluster-infokubectl get nodes
# Check current contextkubectl config current-context
# List available contextskubectl config get-contexts
RBAC Permissions
Section titled “RBAC Permissions”Spotter requires read access to cluster resources:
apiVersion: v1kind: ServiceAccountmetadata: name: spotter-scanner namespace: spotter-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: spotter-scannerrules:- apiGroups: [""] resources: - pods - services - configmaps - secrets - serviceaccounts - persistentvolumes - persistentvolumeclaims - nodes verbs: ["get", "list"]- apiGroups: ["apps"] resources: - deployments - daemonsets - statefulsets - replicasets verbs: ["get", "list"]- apiGroups: ["networking.k8s.io"] resources: - networkpolicies - ingresses verbs: ["get", "list"]- apiGroups: ["policy"] resources: - podsecuritypolicies verbs: ["get", "list"]- apiGroups: ["rbac.authorization.k8s.io"] resources: - roles - rolebindings - clusterroles - clusterrolebindings verbs: ["get", "list"]- apiGroups: ["security.openshift.io"] resources: - securitycontextconstraints verbs: ["get", "list"]---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: spotter-scannerroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: spotter-scannersubjects:- kind: ServiceAccount name: spotter-scanner namespace: spotter-system
# Apply RBAC configurationkubectl create namespace spotter-systemkubectl apply -f spotter-rbac.yaml
# Get service account token (for older clusters)kubectl create token spotter-scanner -n spotter-system
Basic Cluster Scanning
Section titled “Basic Cluster Scanning”Scan Entire Cluster
Section titled “Scan Entire Cluster”# Scan all resources in the clusterspotter scan cluster
# Scan with specific severity thresholdspotter scan cluster --min-severity medium
# Scan and output to filespotter scan cluster --output json --output-file cluster-scan.json
Namespace-Specific Scanning
Section titled “Namespace-Specific Scanning”# Scan specific namespacespotter scan cluster --namespace production
# Scan multiple namespacesspotter scan cluster --namespace production,staging,development
# Exclude system namespacesspotter scan cluster --exclude-namespace kube-system,kube-public,kube-node-lease
# Scan all namespaces except excluded onesspotter scan cluster --all-namespaces --exclude-namespace kube-system
Resource Type Filtering
Section titled “Resource Type Filtering”# Scan only podsspotter scan cluster --resource-types pods
# Scan specific resource typesspotter scan cluster --resource-types pods,services,deployments
# Exclude specific resource typesspotter scan cluster --exclude-resource-types secrets,configmaps
Advanced Scanning Options
Section titled “Advanced Scanning Options”Label and Annotation Filtering
Section titled “Label and Annotation Filtering”# Scan resources with specific labelsspotter scan cluster --label-selector app=nginx
# Scan resources with multiple label conditionsspotter scan cluster --label-selector "app=nginx,environment=production"
# Scan resources with annotation filtersspotter scan cluster --annotation-selector "security.scan=enabled"
# Combine label and annotation selectorsspotter scan cluster \ --label-selector "app=nginx" \ --annotation-selector "security.scan=enabled"
Field Selectors
Section titled “Field Selectors”# Scan only running podsspotter scan cluster --field-selector status.phase=Running
# Scan pods on specific nodesspotter scan cluster --field-selector spec.nodeName=worker-node-1
# Combine multiple field selectorsspotter scan cluster --field-selector "status.phase=Running,spec.nodeName=worker-node-1"
Time-Based Filtering
Section titled “Time-Based Filtering”# Scan resources created in the last hourspotter scan cluster --created-after 1h
# Scan resources created before a specific datespotter scan cluster --created-before 2024-01-01
# Scan resources in a time rangespotter scan cluster --created-after 2024-01-01 --created-before 2024-01-31
Output Formats and Analysis
Section titled “Output Formats and Analysis”JSON Output Analysis
Section titled “JSON Output Analysis”# Scan and analyze resultsspotter scan cluster --output json --output-file cluster-scan.json
# Count findings by severityjq '.summary' cluster-scan.json
# List critical findingsjq '.findings[] | select(.severity == "critical") | {rule_id, resource_name, namespace}' cluster-scan.json
# Group findings by namespacejq 'group_by(.namespace) | map({namespace: .[0].namespace, count: length})' cluster-scan.json
# Find specific rule violationsjq '.findings[] | select(.rule_id == "workload-security-privileged-containers")' cluster-scan.json
SARIF Output for Security Tools
Section titled “SARIF Output for Security Tools”# Generate SARIF output for security platformsspotter scan cluster --output sarif --output-file cluster-scan.sarif
# Upload to GitHub Security tabgh api repos/:owner/:repo/code-scanning/sarifs \ --method POST \ --field sarif=@cluster-scan.sarif \ --field ref=refs/heads/main
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Permission Denied
Section titled “Permission Denied”# Check current user permissionskubectl auth can-i get pods --all-namespaceskubectl auth can-i list deployments
# Check service account permissionskubectl auth can-i get pods --as=system:serviceaccount:spotter-system:spotter-scanner
# Debug RBAC issueskubectl describe clusterrolebinding spotter-scanner
Connection Issues
Section titled “Connection Issues”# Test cluster connectivitykubectl cluster-infokubectl get nodes
# Check kubeconfigkubectl config viewkubectl config current-context
# Test with verbose outputspotter scan cluster --namespace default --verbose