我們?nèi)绾螛?gòu)建了一個LangGraph代理以確定GitOps漏洞的優(yōu)先級? 原創(chuàng)
一款基于LangGraph的開源工具可幫助你確定在特定的Kubernetes環(huán)境中最需要優(yōu)先解決的漏洞。
在當(dāng)今復(fù)雜的Kubernetes環(huán)境中,管理漏洞并確定優(yōu)先級很快會變得令人不堪重負(fù)。由于數(shù)十甚至數(shù)百個容器跨多個服務(wù)運(yùn)行,你如何決定先處理哪些漏洞?
這時候AI可以助一臂之力。我在本文中將介紹我們使用LangGraph和LangChain構(gòu)建基于AI的漏洞優(yōu)先級排序器HAIstings方面的經(jīng)驗,并使用Stacklok開發(fā)的開源AI網(wǎng)關(guān)CodeGate增強(qiáng)安全性。
漏洞太多,時間太少?
如果你曾經(jīng)針對Kubernetes集群運(yùn)行過Trivy之類的漏洞掃描程序,對此就會深有體會:出現(xiàn)在數(shù)十個映像中的成百上千個常見漏洞和暴露(CVE),而解決漏洞的時間和資源有限。應(yīng)該先處理哪些漏洞?
傳統(tǒng)方法依賴嚴(yán)重性分?jǐn)?shù)(即嚴(yán)重、高、中、低),但這種分?jǐn)?shù)并未考慮到你的特定基礎(chǔ)設(shè)施環(huán)境。比如說,內(nèi)部非關(guān)鍵服務(wù)中的高漏洞可能不如一個面向互聯(lián)網(wǎng)的組件中的中等漏洞來得緊迫。
我們想看看是否可以使用AI來幫助解決這個優(yōu)先級確定問題。受阿加莎小說中的Hercule Poirot偵探的助手Arthur Hastings的啟發(fā),我們構(gòu)建了HAIstings來幫助基礎(chǔ)設(shè)施團(tuán)隊根據(jù)以下因素確定漏洞的優(yōu)先級:
- 嚴(yán)重性(嚴(yán)重/高/中/低)。
- 基礎(chǔ)設(shè)施上下文(來自 GitOps存儲庫)。
- 用戶提供的有關(guān)組件關(guān)鍵性的見解。
- 通過對話不斷加深理解。
使用LangGraph和LangChain構(gòu)建HAIstings?
基于LangChain而建的LangGraph提供了一個出色的框架,用于創(chuàng)建具有記憶的對話式AI代理。下面是我們構(gòu)建HAIstings的方式:
1. 核心組件
HAIstings的主要組件包括如下:
- k8sreport:連接到Kubernetes,從trivy-operator收集漏洞報告。
- repo_ingest:提取基礎(chǔ)設(shè)施存儲庫文件以提供上下文。
- vector_db:使用向量嵌入來存儲和檢索相關(guān)文件。
- memory:維護(hù)跨會話的對話歷史記錄。
2. 對話流
HAIstings使用LangGraph狀態(tài)機(jī),流程如下:
graph_builder = StateGraph(State)
# Nodes
graph_builder.add_node("retrieve", retrieve) # Get vulnerability data
graph_builder.add_node("generate_initial", generate_initial) # Create initial report
graph_builder.add_node("extra_userinput", extra_userinput) # Get more context
# Edges
graph_builder.add_edge(START, "retrieve")
graph_builder.add_edge("retrieve", "generate_initial")
graph_builder.add_edge("generate_initial", "extra_userinput")
graph_builder.add_conditional_edges("extra_userinput", needs_more_info, ["extra_userinput", END])
這會創(chuàng)建一個循環(huán),其中HAIstings負(fù)責(zé):
- 檢索漏洞數(shù)據(jù)。
- 生成初始報告。
- 要求提供更多上下文。
- 根據(jù)新信息完善評估。
3. 相關(guān)上下文的RAG
挑戰(zhàn)之一在于從可能龐大的GitOps存儲庫中高效地檢索相關(guān)文件。為此,我們采用了一種檢索增強(qiáng)生成(RAG)方法:
def retrieve_relevant_files(repo_url: str, query: str, k: int = 5) -> List[Dict]:
"""Retrieve relevant files from the vector database based on a query."""
vector_db = VectorDatabase()
documents = vector_db.similarity_search(query, k=k)
results = []
for doc in documents:
results.append({
"path": doc.metadata["path"],
"content": doc.page_content,
"is_kubernetes": doc.metadata.get("is_kubernetes", False),
})
return results
這確保上下文中僅包含每個易受攻擊組件最相關(guān)的文件,從而使提示大小易于控制。
安全考量?
使用LLM和基礎(chǔ)設(shè)施數(shù)據(jù)時,安全至關(guān)重要。我們在分析的漏洞報告和基礎(chǔ)設(shè)施文件可能含有敏感信息,比如:
- 配置詳細(xì)信息。
- 身份驗證機(jī)制。
- 基礎(chǔ)設(shè)施文件中可能泄露的憑據(jù)。
這時候,開源項目CodeGate顯得必不可少。CodeGate充當(dāng)HAIstings和LLM提供程序之間的保護(hù)層,提供了關(guān)鍵保護(hù)。
1. 機(jī)密信息編輯
CodeGate會自動識別并編輯提示中的機(jī)密信息,比如API密鑰、token和憑據(jù),然后它們才會到達(dá)大語言模型(LLM)提供程序。這可以防止敏感數(shù)據(jù)意外泄露給第三方云服務(wù)。
比如說,如果你的Kubernetes清單或GitOps存儲庫含有:
apiVersion: v1
kind: Secret
metadata:
name: database-credentials
type: Opaque
data:
username: YWRtaW4= # "admin" in base64
password: c3VwZXJzZWNyZXQ= # "supersecret" in base64
CodeGate在這些值到達(dá)LLM之前從提示中刪除這些值,然后它在響應(yīng)中無縫地取消編輯。
你可能會說:“等一下。我們依靠ExternalSecretsOperator之類的機(jī)制來保護(hù)Kubernetes機(jī)密,所以我們很安全……是不是?”
你可能正在試用集群,并將token存儲在本地存儲庫或當(dāng)前工作目錄中的文件中。代理可能有點(diǎn)過于雄心勃勃,意外將其添加到你的上下文中,就像我們在代碼編輯器中經(jīng)??吹降哪菢?。這時候CodeGate就會介入,在敏感信息被無意共享之前對其進(jìn)行編輯。
2. PII編輯
除了機(jī)密外,CodeGate還可以檢測和編輯可能存在于你基礎(chǔ)設(shè)施文件或部署清單中的個人身份信息(PII)。
3. 受控模型訪問
CodeGate含有模型多路復(fù)用功能,可幫助確?;A(chǔ)設(shè)施漏洞信息僅發(fā)送給擁有適當(dāng)安全措施的經(jīng)過批準(zhǔn)的受信任模型。
模型多路復(fù)用允許你創(chuàng)建規(guī)則,將特定文件類型、項目或代碼模式傳送到不同AI模型。比如說,你可能希望基礎(chǔ)設(shè)施代碼由私有的本地托管模型處理,而一般的應(yīng)用程序代碼則由基于云的模型處理。
模型多路復(fù)用支持:
- 數(shù)據(jù)敏感度控制:將敏感代碼(比如基礎(chǔ)設(shè)施、安全或身份驗證模塊)傳送到具有更嚴(yán)格隱私保證的模型。
- 合規(guī)要求:確保某些類型的代碼永遠(yuǎn)不會離開環(huán)境,以滿足監(jiān)管部門的要求。
- 成本優(yōu)化:僅對關(guān)鍵代碼部分使用成本昂貴的高性能模型。
- 性能調(diào)整:將代碼復(fù)雜性與最合適的模型功能相匹配。
- 以下是使用基礎(chǔ)設(shè)施存儲庫的示例模型多路復(fù)用策略:
- 規(guī)則:*.tf、*.yaml或*-infra.*可以多路復(fù)用到本地托管的Ollama模型。
好處:Terraform文件和基礎(chǔ)設(shè)施YAML永遠(yuǎn)不會離開你的環(huán)境,從而防止機(jī)密、IP地址或基礎(chǔ)設(shè)施設(shè)計可能被泄露。
4. 可追溯的歷史記錄
CodeGate維護(hù)與AI模型的所有交互的中央記錄,創(chuàng)建所有漏洞評估和建議的審計跟蹤記錄。
使用CodeGate配置HAIstings
配置HAIstings以便與CodeGate配合使用非常簡單。更新HAIstings中的LangChain配置:
# HAIstings configuration for using CodeGate
self.llm = init_chat_model(
# Using CodeGate's Muxing feature
model="gpt-4o", # This will be routed appropriately by CodeGate
model_provider="openai",
# API key not needed as it's handled by CodeGate
api_key="fake-api-key",
# CodeGate Muxing API URL
base_url="http://127.0.0.1:8989/v1/mux",
)
結(jié)果
鑒于HAIstings和CodeGate協(xié)同工作,生成的系統(tǒng)可提供智能、上下文感知的漏洞優(yōu)先級確定機(jī)制,同時保持嚴(yán)格的安全控制。
來自HAIstings的示例報告可能就像這樣:
# HAIsting's Security Report
## Introduction
Good day! Arthur Hastings at your service. I've meticulously examined the vulnerability reports from your Kubernetes infrastructure and prepared a prioritized assessment of the security concerns that require your immediate attention.
## Summary
After careful analysis, I've identified several critical vulnerabilities that demand prompt remediation:
1. **example-service (internet-facing service)**
- Critical vulnerabilities: 3
- High vulnerabilities: 7
- Most concerning: CVE-2023-1234 (Remote code execution)
This service is particularly concerning due to its internet-facing nature, as mentioned in your notes. I recommend addressing these vulnerabilities with the utmost urgency.
2. **Flux (GitOps controller)**
- Critical vulnerabilities: 2
- High vulnerabilities: 5
- Most concerning: CVE-2023-5678 (Git request processing vulnerability)
As you've noted, Flux is critical to your infrastructure, and this Git request processing vulnerability aligns with your specific concerns.
## Conclusion
I say, these vulnerabilities require prompt attention, particularly the ones affecting your internet-facing services and deployment controllers. I recommend addressing the critical vulnerabilities in example-service and Flux as your top priorities.
性能考量
LLM交互本身很慢,你不應(yīng)該依賴它們來獲取實時的關(guān)鍵警報。代理LLM流量會增加一些延遲。這是可以預(yù)料到的,因為這番操作需要耗費(fèi)大量的計算資源。話雖如此,我們認(rèn)為這么做帶來的安全好處卻是值得的。你只需多花幾秒鐘的處理時間,就能獲得針對你特定基礎(chǔ)設(shè)施需求的大為改進(jìn)的漏洞優(yōu)先級確定機(jī)制。
為基礎(chǔ)設(shè)施確保安全的AI
使用LangGraph和LangChain構(gòu)建HAIstings表明了AI如何幫助解決現(xiàn)代基礎(chǔ)設(shè)施中的漏洞優(yōu)先級確定問題。結(jié)合使用CodeGate確保了這種AI幫助不會以犧牲安全為代價。你可以獲得智能的上下文感知指導(dǎo),而不降低安全標(biāo)準(zhǔn),讓你的團(tuán)隊可以專注于修復(fù)最重要的漏洞。
隨著基礎(chǔ)設(shè)施變得越來越復(fù)雜,漏洞越來越多,HAIstings等工具代表了基礎(chǔ)設(shè)施安全管理的未來,在保持最嚴(yán)格安全標(biāo)準(zhǔn)的同時提供智能的上下文感知指導(dǎo)。
你可以使用我們GitHub存儲庫中的代碼:??https://github.com/StacklokLabs/HAIstings??,試用 HAIstings。?
原文標(biāo)題:??How We Built a LangGraph Agent To Prioritize GitOps Vulns?,作者:Juan Antonio "Ozz" Osorio和Radoslav Dimitrov
