Application Security & DevSecOps Agents
Problem Statement
Kubernetes clusters often contain overly permissive Role-Based Access Control (RBAC) bindings, such as service accounts with cluster-admin privileges or pods running in privileged mode (‘privileged: true’) with access to host namespaces.
Goal
Build an agentic K8s audit scanner that inspects YAML cluster manifests, identifies RBAC escalation risks, and auto-generates hardened manifest manifests.
Outcome Measurement
- Manifest Coverage: 100% of privileged containers and dangerous RBAC bindings detected.
- Remediation Speed: Generated secure deployment manifests automatically.
How the Code was Built
Parses Kubernetes YAML definitions into a Pydantic evaluation engine.
Executable Code
from pydantic import BaseModel, Fieldfrom langchain_openai import ChatOpenAIclass K8sAuditResult(BaseModel): is_secure: bool detected_risks: list[str] remediated_yaml: strdef audit_k8s_manifest(yaml_manifest: str) -> K8sAuditResult: llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output(K8sAuditResult) prompt = f"Audit this Kubernetes manifest for security misconfigurations (e.g. privileged pods, hostPath mounts, cluster-admin bindings) and output corrected YAML:\n{yaml_manifest}" return structured_llm.invoke(prompt)if __name__ == "__main__": bad_k8s = """apiVersion: apps/v1kind: Deploymentmetadata: name: web-appspec: template: spec: containers: - name: app image: nginx:latest securityContext: privileged: true runAsUser: 0""" res = audit_k8s_manifest(bad_k8s) print(f"Is Secure: {res.is_secure}") print(f"Risks Identified: {res.detected_risks}") print("Remediated YAML:\n", res.remediated_yaml)






Leave a Reply