認證傳遞——端到端的 `secretKeyRef`
一個 PostgreSQL 資料庫,其密碼由認證子系統供應,依提供者展示。相同的 password 槽位,位於 core/datastore/postgres,接受任一種認證系列;開發者附加哪一個系列,決定密碼如何傳遞,而資料庫 Recipe 依其提供者所支援的方式讀取該槽位。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example credential-secretkeyref它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd credential-secretkeyref建置
此範例有三個 Releases。每次執行建置一個:
aggrega build --release release-helm.yaml --auto-pullaggrega build --release release-compose.yaml --auto-pullaggrega build --release release-negative.yaml --auto-pull # expected to fail (see below)每個 Release 都透過此工作區的 .aggrega-config.yaml,從它指名為預設的 hub 登錄庫解析其目錄實體,所以不需要 --catalog 旗標,而且 --auto-pull 會在建置期間寫入全新複製所缺少的鎖定檔。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自己建立任何資料夾。若 Aggrega 工作區資料夾中還沒有 `credential-secretkeyref` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `credential-secretkeyref` 且不指定 `dir`:它會把範例寫入新的 `credential-secretkeyref` 資料夾。接著對每個 Release 各呼叫一次 `build` 工具,依此順序——`release-helm.yaml`、`release-compose.yaml`——每次都將 `workspace_root` 設為 `credential-secretkeyref`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。然後以同樣方式呼叫 `build` 工具,將 `release` 設為 `release-negative.yaml`:它本來就會被拒絕,所以請把拒絕訊息讀給我聽。告訴我每次建置寫出了哪些檔案;它們位於 `credential-secretkeyref/dist/` 之下。Helm — 可參照的 Secret + secretKeyRef
aggregate-helm.yaml 把 core/referenced-secret 附加到 password 槽位。Manifest 把它對應到 k8s-secret-from-parameter,後者具體化一個 Kubernetes Secret,並把 Secret 的身分以 provides {name, key} 發布。postgres helm Recipe 透過 containers.attachments 參照該槽位,合成器把它渲染為環境變數的 secretKeyRef。
aggrega build --release release-helm.yaml --auto-pull產生出的 Secret(dist/helm/helm/db--password--db-secret-db-password.yaml)與資料庫的 secretKeyRef(dist/helm/helm/db-statefulset.yaml)指名同一個資源,因為兩者都讀取 .features.password.provides.name——沒有重新衍生,也沒有慣例要兩邊去達成一致:
[… omitted: the apiVersion and the Secret kind]metadata: name: db-password [… omitted: the labels and annotations every rendered document carries]stringData: POSTGRES_PASSWORD: "dev-pw-change-me"
# db-statefulset.yaml (container env)- name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: db-password # == the Secret's metadata.name key: POSTGRES_PASSWORD # == the Secret's data keyCompose — 注入值認證
aggregate-compose.yaml 附加 core/secret-value。Compose 沒有 Secret 資源/secretKeyRef 基本元素,所以 Manifest 把該認證對應到 dotenv-secret,它在注入管道上以附加項目在 values.key 中所指名的鍵傳遞該值,此處是 POSTGRES_PASSWORD,而 postgres compose Recipe 以該鍵讀取它。
aggrega build --release release-compose.yaml --auto-pull# dist/compose/compose/docker-compose.yaml → POSTGRES_PASSWORD: dev-pw-change-me負向——不可參照的填入被參照所拒絕
aggregate-negative.yaml 把注入值的 core/secret-value 附加到 password 槽位,然後在 helm 上建置,而那裡 postgres Recipe 透過 containers.attachments(一個 secretKeyRef)參照該槽位。secretKeyRef 需要一個 Secret 資源可指向,但注入值認證不發布任何資源——所以建置會明確中止:
aggrega build --release release-negative.yaml --auto-pull# [… omitted: the pull's lines, which carry the same halt, and two lint notices]# [CATALOG_ERROR E0212] instance db# rendering node "db"# [… omitted: the cause, "workload attachment slot is not referenceable", naming the Recipe and its path in your catalog, then one line each for the "name" and the "key" the slot's provides does not expose]此 Release 預期會失敗——中止就是教訓。建置以 1 結束,而不是回報成功,所以護欄對讀者與執行它的任何指令碼都是可見的。
有兩種修正。附加可參照的認證(core/referenced-secret),如 aggregate-helm.yaml;或保留 core/secret-value 附加項目,並在 Manifest 中把它對應到 k8s-secret-from-value,也就是 core/secret-value 的 Helm 實現,它把值寫入它所發出的 Secret,並發布該 Secret 的名稱與鍵,使參照有資源可指向。繫結到 env-secret 時,注入值認證改為經由注入管道到達容器,絕不是 containers.attachments 的參照。