Bỏ qua để đến nội dung
AggregaAggrega
Chọn ngôn ngữ: Tiếng Việt
Trang này được dịch bằng máy. Đọc bản gốc tiếng Anh.

Cung cấp thông tin xác thực — `secretKeyRef` từ đầu đến cuối

Một cơ sở dữ liệu PostgreSQL duy nhất có mật khẩu do hệ thống con thông tin xác thực cung cấp, được trình bày theo từng provider. Cùng một slot password trên core/datastore/postgres chấp nhận cả hai họ thông tin xác thực; họ mà nhà phát triển gắn vào quyết định cách mật khẩu được cung cấp, và Recipe của cơ sở dữ liệu đọc slot theo cách mà provider của nó hỗ trợ.

Tạo ví dụ

Bạn có thể build ví dụ này bằng các lệnh bên dưới, hoặc để trợ lý AI của bạn build giúp bạn.

Ghi ví dụ vào một thư mục mới đặt theo tên của nó:

Terminal window
aggrega scaffold --example credential-secretkeyref

Sau đó nó in ra các lệnh cần chạy tiếp theo. Lệnh đầu tiên chuyển vào thư mục mới:

Terminal window
cd credential-secretkeyref

Build

Ví dụ này có ba release. Mỗi lần chạy, build một release:

Terminal window
aggrega build --release release-helm.yaml --auto-pull
aggrega build --release release-compose.yaml --auto-pull
aggrega build --release release-negative.yaml --auto-pull # expected to fail (see below)

Mỗi release phân giải các thực thể catalog của nó qua .aggrega-config.yaml của workspace này, từ registry hub mà tệp đó chỉ định làm mặc định, nên không cần cờ --catalog, và --auto-pull ghi trong lúc build lockfile mà một bản clone mới còn thiếu.

Với trợ lý AI

Một trợ lý AI được thiết lập với các công cụ của Aggrega cũng có thể làm như vậy: hãy dán prompt này vào trợ lý.

Chỉ sử dụng các công cụ của Aggrega và không tự tạo thư mục nào. Nếu thư mục không gian làm việc chưa chứa thư mục `credential-secretkeyref`, hãy gọi công cụ `scaffold` một lần với `example` đặt thành `credential-secretkeyref` và không có `dir`: công cụ này ghi ví dụ vào một thư mục `credential-secretkeyref` mới. Sau đó gọi công cụ `build` một lần cho mỗi Release, theo thứ tự này — `release-helm.yaml`, `release-compose.yaml` — mỗi lần với `workspace_root` đặt thành `credential-secretkeyref`, `release` đặt thành tệp đó, `auto_pull` đặt thành true và `write` đặt thành true. Sau đó gọi công cụ `build` theo cách tương tự với `release` đặt thành `release-negative.yaml`: lần build này được thiết kế để bị từ chối, vì vậy hãy đọc cho tôi thông báo từ chối. Hãy cho tôi biết mỗi lần build đã ghi những tệp nào; chúng nằm trong `credential-secretkeyref/dist/`.

Helm — một Secret có thể tham chiếu + secretKeyRef

aggregate-helm.yaml gắn core/referenced-secret vào slot password. Manifest ánh xạ nó tới k8s-secret-from-parameter, thứ tạo ra một Kubernetes Secret và công bố danh tính của Secret dưới dạng provides {name, key}. Recipe helm của postgres tham chiếu slot thông qua containers.attachments, mà bộ tổng hợp kết xuất thành một env secretKeyRef.

Terminal window
aggrega build --release release-helm.yaml --auto-pull

Secret được sinh ra (dist/helm/helm/db--password--db-secret-db-password.yaml) và secretKeyRef của cơ sở dữ liệu (dist/helm/helm/db-statefulset.yaml) gọi tên cùng một tài nguyên, vì cả hai đều đọc .features.password.provides.name — không có việc suy dẫn lại và không có quy ước nào mà hai phía phải thống nhất:

db--password--db-secret-db-password.yaml
[… 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 key

Compose — một thông tin xác thực dạng inject-value

aggregate-compose.yaml gắn core/secret-value. Compose không có nguyên thủy tài nguyên Secret / secretKeyRef, nên Manifest ánh xạ thông tin xác thực tới dotenv-secret, thứ cung cấp giá trị qua đường inject dưới khóa mà phần gắn kèm nêu trong values.key, ở đây là POSTGRES_PASSWORD, và Recipe compose của postgres đọc nó dưới khóa đó.

Terminal window
aggrega build --release release-compose.yaml --auto-pull
# dist/compose/compose/docker-compose.yaml → POSTGRES_PASSWORD: dev-pw-change-me

Trường hợp phủ định — một giá trị điền không thể tham chiếu bị từ chối khỏi một tham chiếu

aggregate-negative.yaml gắn core/secret-value dạng inject-value vào slot password, rồi build trên helm, nơi Recipe của postgres tham chiếu slot qua containers.attachments (một secretKeyRef). Một secretKeyRef cần một tài nguyên Secret để trỏ tới, nhưng thông tin xác thực dạng inject-value không công bố tài nguyên nào — nên quá trình build dừng lại với lỗi rõ ràng:

Terminal window
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 này được dự kiến sẽ thất bại — việc dừng lại chính là bài học. Bản build thoát với 1 thay vì báo thành công, nên cơ chế bảo vệ hiển thị rõ với người đọc và với bất kỳ script nào chạy nó.

Có hai cách khắc phục. Gắn một thông tin xác thực có thể tham chiếu (core/referenced-secret), như trong aggregate-helm.yaml; hoặc giữ attachment core/secret-value và ánh xạ nó trong Manifest tới k8s-secret-from-value, bản hiện thực Helm của core/secret-value ghi giá trị vào một Secret mà nó phát ra và công bố tên và khóa của Secret đó, để tham chiếu có một tài nguyên để trỏ tới. Khi được gắn với env-secret, thay vào đó một thông tin xác thực dạng inject-value đến container qua đường inject, không bao giờ qua một tham chiếu containers.attachments.