跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

連線上的認證——每條連線一把金鑰,以及兩端同一把金鑰

一個語意記憶儲存區連到兩個模型,並向各自出示不同的金鑰,編譯為 Kubernetes 資訊清單。它以託管模型讀取匯入的素材,並以在叢集內與它並排執行的嵌入伺服器對其編碼。它向託管模型出示的金鑰,是從叢集機密儲存區取得的。嵌入伺服器檢查它自己的金鑰,而儲存區出示的正是同一把——兩端都從同一個參數讀取,所以兩者不可能被分開撰寫。

此工作區不撰寫任何自己的目錄實體。它所指名的每個 Component、Feature 與 Recipe 都隨附於標準程式庫目錄;這裡的檔案——三個 Aggregates、一個 Manifest 與三個 Releases——就是任何人要撰寫的全部內容。

它建置什麼

┌── extraction_model ──► llm a hosted model API
│ key on the connection, fetched from a secret store
memory ───┤
│
└── embedding_model ──► embeddings an embedding server that checks a key
key on the connection ═══ the server's own api_key
(both read one parameter)
實例目錄 Component它在這裡是什麼
memorycore/datastore/cognee呼叫端:一個語意記憶儲存區,它的兩個模型插槽各自宣告一個 credential 槽位,用於它在該連線上出示的金鑰
llmcore/ai-model/openai一個託管模型,標籤為 component.aggrega.ai/hosting: managed;以參照方式連到,所以叢集中沒有為它執行任何東西
embeddingscore/ai-model/tei叢集內的 Text Embeddings Inference,標籤為 self-hosted;它要求每個呼叫端提供金鑰,並把金鑰存放在它自己的 api_key 槽位中

三個 Releases 共用一個 Manifest,即 manifest.yaml,且各自有自己的 Aggregate:

ReleaseAggregate它展示什麼
release-matched.yamlaggregate-matched.yaml兩條連線都由開發者撰寫,嵌入連線的兩端讀取同一個參數
release-mismatched.yamlaggregate-mismatched.yaml同上,但兩端讀取兩個參數:建置成功,而慣例檢查會給出提示
release-operator-wired.yamlaggregate-operator-wired.yaml萃取連線留給操作員:Release 連接它,並在 infra 中指名它的金鑰

草擬範例

你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example connection-credentials

它接著會印出要接著執行的命令。第一個會切換到新資料夾:

Terminal window
cd connection-credentials

建置

每次執行建置一個 Release:

Terminal window
aggrega build --release release-matched.yaml --auto-pull # → dist/matched/
aggrega build --release release-mismatched.yaml --auto-pull # → dist/mismatched/
aggrega build --release release-operator-wired.yaml --auto-pull # → dist/operator-wired/

每個 Release 寫入自己的目錄,所以三棵渲染出的樹可以並存,並能彼此比較差異。

Release 在建置前需要它的鎖定檔,而鎖定檔是產生的,不是隨附的。先解析一個,或讓建置來填入:

Terminal window
aggrega pull --release release-matched.yaml # resolve, then build normally
aggrega build --release release-matched.yaml --auto-pull # or resolve during the build

此工作區從 .aggrega-config.yaml 所指名的預設登錄庫解析其實體,所以不需要 --catalog 旗標。

pull 會印出:

[… omitted: a warning that the declared-sensitive origin of llm.credential reaches the target with no per-key sensitivity marking, because its realization maps it through a valuesMapping]
Pull complete: release-matched.lock (entities=15, source_artifacts=0, external_inputs=0)

而建置會印出,其中 <workspace> 代表此範例的資料夾:

[… omitted: the same warning about llm.credential the pull printed]
notice: LINT: Feature "core/referenced-secret" at core/referenced-secret:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
[… omitted: a total build time, which differs per run]
Build completed: 8 files written to <workspace>/dist

另外兩個 Releases 會印出相同的行,每次 pull 指名各自的鎖定檔,而每次建置寫出八個檔案。notice: 那一行是關於這裡每個附加項目所使用的認證 Feature:core/referenced-secret 接受其機密的名稱 origin,來自開發者的 values 或操作員的 infra 皆可,而提示回報該葉節點在兩個支柱中都有宣告。它不改變任何判定,也不改變任何渲染出的位元組;認證 Features說明為何該葉節點兩邊都有。

在此頁面印出建置產生內容之處,該區塊是從真實執行複製而來——關於慣例與用於省略行的標記,請見閱讀這些頁面上的輸出。

搭配 AI 助理

以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。

只使用 Aggrega 工具,且不要自己建立任何資料夾。若 Aggrega 工作區資料夾中還沒有 `connection-credentials` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `connection-credentials` 且不指定 `dir`:它會把範例寫入新的 `connection-credentials` 資料夾。接著對每個 Release 各呼叫一次 `build` 工具,依此順序——`release-matched.yaml`、`release-mismatched.yaml`、`release-operator-wired.yaml`——每次都將 `workspace_root` 設為 `connection-credentials`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `connection-credentials/dist/` 之下。

渲染樹

dist/matched/helm/
├── embeddings-default-service.yaml
├── embeddings-headless-service.yaml
├── embeddings-statefulset.yaml
├── llm-service.yaml
├── memory--llm-zdcredential--memory-externalsecret-memory-memory-llm-credential.yaml
├── memory-default-service.yaml
├── memory-headless-service.yaml
└── memory-statefulset.yaml

有一個檔案是由附加項目帶來的,而不是元件自己的 Recipe:ExternalSecret,它取得儲存區向託管模型出示的金鑰。其檔名帶有它所附加的元件 memory,以及附加項目的名稱 llm.credential,寫成 llm-zdcredential,因為點號與檔案自己的名稱共用同一個字串;請見命名渲染資源。沒有檔案帶有嵌入金鑰:它位於命名空間已持有的 Secret 中,如下面各節所示。

哪份文件寫了什麼

  • 目錄宣告繫結點。儲存區的 Component 在其兩個模型插槽各宣告一個 credential 槽位;嵌入伺服器的 Component 在自己身上宣告 api_key;每個 Recipe 決定金鑰以何種變數傳遞。
  • Aggregate——開發者——撰寫儲存區連到哪些模型、每條連線上的認證、伺服器所驗證的金鑰,以及嵌入連線兩端都讀取的那一個參數。
  • Manifest——操作員針對此目標的路由——決定每個認證在 Kubernetes 上成為什麼:對每個 core/referenced-secret 附加項目,成為對命名空間已持有的 Secret 的參照,唯獨連到託管模型的連線那個例外,它是從叢集機密儲存區取得。
  • Release——操作員針對一個環境的文件——履行參數、把邊界放在一個命名空間中,而在操作員連接的 Release 中,連接萃取連線並指名其上出示的金鑰。

每條連線一把金鑰

儲存區透過其目錄實體所宣告的兩個插槽 extraction_model 與 embedding_model 連到兩個模型,且每個插槽宣告一個 credential 槽位。所以儲存區出示的金鑰位於它被出示的那條連線上,每條連線帶有自己的一把。取自 aggregate-matched.yaml:

memory:
type: core/datastore/cognee
values:
# The width of the vectors the embedding model above returns.
embedding_dimensions: 384
uses:
extraction_model:
llm:
target: llm
features:
credential: # the key presented to the hosted model
type: core/referenced-secret
values:
key: api-key
origin: "{{ .parameters.provider_key_entry }}"
embedding_model:
embeddings:
target: embeddings
features:
credential: # the key presented to the embedding server
type: core/referenced-secret
values:
key: api-key
origin: "{{ .parameters.embedding_key_secret }}"

寫在連線上的附加項目,會成為儲存區自己的附加項目之一,以連線的別名、一個點號與槽位命名:llm.credential 與 embeddings.credential。以下是儲存區容器 env 的前兩個與最後四個項目,取自 dist/matched/helm/memory-statefulset.yaml:

- name: LLM_API_KEY
valueFrom:
secretKeyRef:
name: memory-llm.credential
key: api-key
- name: EMBEDDING_API_KEY
valueFrom:
secretKeyRef:
name: embedding-server-key
key: api-key
[… omitted: the store's own settings, HTTP_PORT to EMBEDDING_DIMENSIONS]
- name: EMBEDDING_ENDPOINT
value: http://embeddings-default:8080/v1
- name: EMBEDDING_MODEL
value: BAAI/bge-small-en-v1.5
- name: LLM_ENDPOINT
value: https://api.openai.com/v1
- name: LLM_MODEL
value: gpt-4o-mini

前兩個是金鑰,各自來自其所在連線上的認證,使用儲存區的 Recipe 賦予該一側的變數——萃取用 LLM_API_KEY,嵌入用 EMBEDDING_API_KEY。兩者都是參照,不是值,而 helm 提供者會把從 Secret 讀取的項目寫在一般項目之前。最後四個來自兩條連線:每個模型發布其位址與模型名稱,而儲存區的 Recipe 依插槽讀取它們。

儲存區的 Component 也在自己身上宣告一個金鑰槽位 model_api_key。它的 Recipe 只對未連接的插槽讀取該槽位,此時儲存區以提供者自己的位址連到提供者,沒有連線可攜帶金鑰。這裡兩個插槽都已連接,所以不會讀取它。

兩端同一把金鑰

嵌入伺服器檢查一把金鑰。它把該金鑰存放在自己 Component 的一個槽位 api_key 中,而它的 Recipe 把填入該槽位的任何內容以 API_KEY 傳遞,也就是 Text Embeddings Inference 在啟動時讀取的變數。取自 aggregate-matched.yaml:

embeddings:
type: core/ai-model/tei
values:
model: BAAI/bge-small-en-v1.5
features:
api_key: # the key the server verifies
type: core/referenced-secret
values:
key: api-key
origin: "{{ .parameters.embedding_key_secret }}"

那是伺服器上的一個附加項目。儲存區出示的金鑰是另一個,位於儲存區的連線上,如上所示。填入一端的槽位,並不會填入另一端的任何東西,所以兩端都宣告金鑰是最低要求,而不是重複。讓兩者成為同一把金鑰的,是兩者都讀取同一個參數 embedding_key_secret,它由 Aggregate 宣告並由 Release 履行:

parameters:
provider_key_entry: ai/openai-api-key
embedding_key_secret: embedding-server-key

Manifest 把 core/referenced-secret 繫結到 k8s-existing-secret,它把 origin 讀作命名空間已持有的 Secret 的名稱,且不渲染任何東西。所以兩端都參照那一個 Secret。在伺服器容器的 env 中,取自 dist/matched/helm/embeddings-statefulset.yaml:

- name: API_KEY
valueFrom:
secretKeyRef:
name: embedding-server-key
key: api-key

而在儲存區的容器中,取自 dist/matched/helm/memory-statefulset.yaml:

- name: EMBEDDING_API_KEY
valueFrom:
secretKeyRef:
name: embedding-server-key
key: api-key

Secret 由 Release 之外、由部署它的人建立,所以金鑰的內容從不經過撰寫的檔案或渲染出的成品。

為何伺服器從不看到呼叫端的金鑰

連線上的附加項目只落在呼叫端。儲存區的兩個認證是儲存區的附加項目:它們渲染的內容位於儲存區旁邊,而儲存區的容器參照它。沒有任何關於它們的東西到達連線所指向的元件,所以無論到達它的連線帶有什麼,目標渲染出的都相同。

託管模型清楚地顯示了這一點。它渲染一個檔案,一個把叢集內名稱對應到提供者主機的 Service——整個 spec,取自 dist/matched/helm/llm-service.yaml:

spec:
type: ExternalName
externalName: api.openai.com
ports:
- port: 443

儲存區向它出示的金鑰,是在儲存區旁邊取得的,如下一節所示。嵌入伺服器則以對照顯示這點:下方不相符的 Release 改變了儲存區嵌入連線上的金鑰,而讓伺服器的不變,且伺服器在 dist/mismatched/ 中的三個檔案與 dist/matched/ 中的檔案,只在 aggrega.ai/aggregate-name 標籤上不同,該標籤指名各自所建置自的 Aggregate。

所以伺服器的設定中沒有任何東西指名呼叫端所持有的金鑰。在執行階段,請求帶有呼叫端的金鑰,而伺服器把它與自己所持有的那一把比較;呼叫端則只讀取伺服器發布的內容:一個位址與一個模型名稱。

每個伺服器一把金鑰

Text Embeddings Inference 檢查一把金鑰。每個呼叫端出示同一把金鑰,出示任何其他金鑰的呼叫端會被拒絕,且伺服器無法分辨一個呼叫端與另一個,所以撤回某一個呼叫端的存取,意味著要更改每個呼叫端所出示的金鑰。無論有多少連線到達它,它的 api_key 槽位都只持有一把金鑰。該限制是伺服器自己的,目錄的 core/ai-model/vllm 與 core/ai-model/tgi 也同樣有。它不是連線的限制:到達多個伺服器的呼叫端,每條連線持有一把金鑰,就像這裡的儲存區持有兩把。

一條連線的金鑰來自機密儲存區

Manifest 依型別繫結每個 core/referenced-secret 附加項目,並依名稱繫結其中一個。取自 manifest.yaml:

features:
# One realization for every core/referenced-secret attachment: the Secret
# already exists in the namespace, and origin is its name.
type:
core/referenced-secret:1.0.0: helm/k8s-existing-secret:1.0.0
# One attachment bound differently, by its name. An attachment written on
# a connection is named for the connection's alias, a dot and the slot,
# so the memory store's credential on its llm connection is
# llm.credential. That key is fetched from the store, and origin is the
# store entry to fetch.
name:
llm.credential: helm/k8s-external-secret:1.0.0

k8s-external-secret 把 origin 讀作要取得的儲存項目。該連線的 origin 是參數 provider_key_entry,履行為 ai/openai-api-key,所以該附加項目渲染出一個 ExternalSecret,由叢集中的機密儲存區 Operator 協調成 Secret。整個 spec,取自 dist/matched/helm/memory--llm-zdcredential--memory-externalsecret-memory-memory-llm-credential.yaml:

spec:
refreshInterval: "1h"
secretStoreRef:
name: platform-secret-store
kind: ClusterSecretStore
target:
name: memory-llm.credential
creationPolicy: Owner
data:
- secretKey: api-key
remoteRef:
key: "ai/openai-api-key"

它所填入的 Secret memory-llm.credential,以儲存區與連線命名,而且它就是上面儲存區的 LLM_API_KEY 所參照的名稱。此實現從一個名為 platform-secret-store 的 ClusterSecretStore 讀取。

參數攜帶什麼,取決於讀取它的實現:託管模型金鑰是儲存項目,嵌入金鑰是 Secret 的名稱。一條連線的金鑰採用它自己的實現,而同一型別的其他每個附加項目則維持該型別的實現。

兩端在何處解析機密

目標平台在機密範圍內以參照來解析機密:一個資源可以指名另一個資源的邊界。當連線的兩端位於同一個範圍時,兩者參照同一個機密資源。當它們位於不同範圍時,各端持有自己的機密資源,由同一個儲存項目實現。Manifest 選擇各端的實現——對該型別的每個附加項目依 Feature 型別,或對單一附加項目依其名稱。

每個目標以自己的方式劃定範圍。在 Kubernetes 上,也就是此工作區所建置的目標,它是命名空間:工作負載只參照自己命名空間中的 Secret。這裡每個 Release 把它唯一的邊界放在一個命名空間中,aggrega.ai/namespace: memory,且每份渲染出的文件都帶有它——兩個 StatefulSet 的開頭:

dist/matched/helm/memory-statefulset.yaml
metadata:
name: memory
namespace: memory
# dist/matched/helm/embeddings-statefulset.yaml
metadata:
name: embeddings
namespace: memory

所以兩端參照上面那同一個 Secret。若嵌入伺服器位於自己的命名空間,Manifest 會依名稱——api_key 與 embeddings.credential——把兩端的認證繫結到 k8s-external-secret,Release 以儲存項目履行該參數,而每端在自己的命名空間中持有一個 ExternalSecret,兩者取得的都是那同一個項目。儲存區接著跨命名空間連到伺服器,這部分在 cross-boundary-networking 中說明。在其他目標上,範圍是 Compose 專案、Terraform 根設定與 CloudFormation 堆疊;認證模型說明了每一個。

操作員連接一條連線

aggregate-operator-wired.yaml 撰寫了嵌入連線,並讓儲存區的 extraction_model 插槽未繫結:儲存區的目錄實體宣告了它,而開發者把它留給操作員。Release 把它連接到 Aggregate 所宣告的託管模型。取自 release-operator-wired.yaml:

memory:
uses:
# A connection the Aggregate does not write, into a socket the
# store's catalog entity declares, to a component the Aggregate
# declares. A Release writes no values, so the credential on it
# names its secret in infra.
extraction_model:
llm:
target: llm
features:
credential:
type: core/referenced-secret
infra:
origin: ai/openai-api-key

Release 可以把一條連線連接到目錄實體所宣告、而 Aggregate 留作未繫結的插槽,連到 Aggregate 所宣告的元件。它不新增任何元件,也絕不重新指定 Aggregate 所撰寫的連線。它不寫 values,所以該連線上的認證在 infra 中指名其機密:core/referenced-secret 的 origin 取自開發者的 values 或操作員的 infra,二者恰好其一,而建置會在兩者皆有或皆無的附加項目上中止。

附加項目名為 llm.credential,與相符的 Release 中完全相同,所以 Manifest 的名稱層級以同樣方式繫結它。除了指名 Aggregate 的標籤之外,dist/operator-wired/ 與 dist/matched/ 之間有兩行不同。其一是 data,出自 ExternalSecret:

data:
- secretKey: value
remoteRef:
key: "ai/openai-api-key"

以及儲存區對它所填入之 Secret 的參照,位於 dist/operator-wired/helm/memory-statefulset.yaml:

- name: LLM_API_KEY
valueFrom:
secretKeyRef:
name: memory-llm.credential
key: value

資料鍵是 value 而不是 api-key,因為 key 是 values 的葉節點,只有 Aggregate 會寫,所以僅由 Release 建立的附加項目會讀取 Feature 的預設值。實現會發布該鍵,而儲存區從已發布的內容讀取它,所以兩者仍然一致。

託管模型標示為 managed,所以連到它的連線要帶有認證,或在自己的 values 中表明該模型不需要,unauthenticated: true。無論是哪份文件寫了該連線,這都成立。把 features: 區塊從 Release 的連線中移除,建置就會中止。該中止是 E0033,即儲存區 Component 的斷言失敗,而它的訊息以規則自己的句子結尾:the extraction_model connection llm of “memory” reaches “llm”, whose component.aggrega.ai/hosting label is managed, with no credential: attach one under credential on that connection, or set unauthenticated: true in its values(extraction_model 連線 llm,屬於 “memory”,連到 “llm”,其 component.aggrega.ai/hosting 標籤為 managed,卻沒有認證:請在該連線的 credential 之下附加一個,或在其 values 中設定 unauthenticated: true)。

兩端不一致時

aggregate-mismatched.yaml 給伺服器的金鑰與儲存區的金鑰兩個參數:

parameters:
provider_key_entry:
type: string
description: >
The store entry holding the key the memory store presents to the hosted
model provider.
embedding_key_secret:
type: string
description: The Secret holding the key the embedding server verifies.
# The fault: a second parameter for what is meant to be the same key.
memory_embedding_key_secret:
type: string
description: The Secret holding the key the memory store presents to the embedding server.

伺服器的 api_key 讀取 embedding_key_secret,而儲存區的連線讀取 memory_embedding_key_secret,Release 以兩個不同的 Secret 履行它們。建置成功並寫出八個檔案:編譯器評估它所得到的每個附加項目,而不在兩端之間比較任何東西。伺服器仍然參照 embedding-server-key,而儲存區現在出示另一個,取自 dist/mismatched/helm/memory-statefulset.yaml:

- name: EMBEDDING_API_KEY
valueFrom:
secretKeyRef:
name: embedding-client-key
key: api-key

部署後,只有在這兩個 Secret 碰巧持有同一把金鑰時,伺服器才會接受儲存區的請求。

Release 的發布慣例檢查就是抓到這一點之處:

Terminal window
aggrega lint --release release-mismatched.yaml
release-mismatched.yaml:
warn: E0329 connection-credential-matches-target-key: connection "embeddings" on socket "embedding_model" of component "memory" carries a credential in its credential slot and its target "embeddings" holds a key in its api_key slot, but the two ends write their secret's locator differently, so they may name different secrets and the target would refuse the key this caller presents. The locators are compared as written, and neither is shown here. Reading one parameter at both ends keeps them one key; that agreement is the standard-catalog convention, and the build does not check it.
conventions checked: 10 entities, 1 advisory finding(s)

對相符的 Release 做同樣的檢查,沒有任何回報:

Terminal window
aggrega lint --release release-matched.yaml
conventions checked: 10 entities, 0 advisory finding(s)

對操作員連接的 Release 也同樣沒有。

規則依標準目錄賦予它們的名稱找到兩端——目標的 api_key 與連線的 credential——並比較每端所寫的 origin,依其所寫:讀取同一個參數的兩端比較為相等,且不解析任何東西。它從不引用任何一端,因為寫在那裡的可能就是機密本身。它不判斷標示為 managed 的目標,其 api_key(若它宣告一個)是向其廠商出示的金鑰,而不是其呼叫端所出示的;所以連到 llm 的連線在三個 Releases 中都不產生任何內容。該發現是建議:aggrega lint 以 0 結束,而兩端刻意不同的工作區可用 allow_connection_credential_mismatches 選擇退出。編輯器向常駐程式詢問同一個問題,以 aggrega/lint 指名 Release,會收到同一個發現,其代碼為 connection-credential-matches-target-key,簡短代碼為 E0329。該規則及其所讀取的內容,描述於紀律規則。

確定性

三個 Releases 全都在 --verify-determinism 之下建置,它會把每個節點渲染兩次並比較發出的位元組:

Terminal window
aggrega build --release release-matched.yaml --verify-determinism

從乾淨的樹重建三者中的任何一個,都會逐位元組重現其渲染輸出。

調整它

  • 新增嵌入伺服器的第二個呼叫端。 它的連線認證也讀取 embedding_key_secret。伺服器對每個呼叫端持有一把金鑰,所以第二個呼叫端出示同一把金鑰。
  • 也從儲存區取得嵌入金鑰。 依名稱把 api_key 與 embeddings.credential 繫結到 k8s-external-secret,並以儲存項目而不是 Secret 的名稱履行 embedding_key_secret。兩端一起改變,因為兩者都讀取該參數。
  • 連到不檢查金鑰的模型。 讓連線不帶認證。當目標標示為 managed 時,在連線上以其 values 中的 unauthenticated: true 表明這點。

關於在多個呼叫端的連線之間共用一把金鑰,請見 model-api-key-sharing。關於代表其呼叫端持有提供者金鑰的路由器,請見 ai-agent-and-model-router。這一切背後的模型是認證模型。