# Zeabur Identity 的架構

> 了解 Zeabur Identity 如何在 Kubernetes 叢集中運作、安全性質、元件關係與 crate 分工。

Zeabur Identity 是在自管 Kubernetes 叢集中執行的 OpenID Connect Issuer。kube-apiserver 使用 [Structured Authentication Configuration][kep-3331]，自行驗證 Zeabur Identity 簽發的權杖。你可以用通行密鑰、外部帳號或密碼登入，不必持有無法撤銷的長效管理員 kubeconfig。其他 OpenID Connect 用戶端，例如 Argo CD、Grafana 和 Headlamp，也能使用同一個 Issuer。

k3s 與 RKE2 使用相同設定。CI 測試涵蓋 k3s。

## 安全性質

- **通行密鑰優先**。通行密鑰是主要認證資訊，使用 WebAuthn，且必須完成使用者驗證。使用者也可以用至少 15 個字元的密碼登入，或連結 GitHub、Google 或其他 OpenID 服務的帳號登入。
- **只有管理員帳號能取得管理員權限**。只有 `accountType: Admin` 的 `User` 會取得 `zeabur:admins` 群組，這個群組綁定 `cluster-admin`。管理員帳號的登入方式由 `admin-sign-in` 設定決定，預設是通行密鑰，或密碼加上驗證器的驗證碼；也可以設為只接受通行密鑰，或只接受裝置綁定的通行密鑰。管理員權杖預設有效期限為五分鐘，工作階段也可能因閒置而提早結束。一般帳號只會取得所屬群組綁定的權限。所有工作階段都會在登入 12 小時後結束。
- **權杖有效期限短**。權杖綁定目標對象（audience），簽章金鑰會定期輪替。
- **使用者自行管理登入方式**。不論是新增或移除通行密鑰，還是連結或解除連結外部帳號，使用者都需要再次用通行密鑰完成驗證。變更或移除密碼則需要輸入目前的密碼。使用者無法移除最後一把通行密鑰，也無法解除連結最後一種登入方式。
- **狀態儲存在 Kubernetes**。自訂資源儲存使用者、認證資訊、用戶端、工作階段與邀請，不需要資料庫。撤銷存取權與刪除資料的順序，請見[登出、救援與移除使用者](https://oss.zeabur.com/zh-TW/docs/identity/users/offboarding)。

[規格符合度](https://oss.zeabur.com/zh-TW/docs/identity/reference/conformance)列出 Zeabur Identity 實作的各項規格要求，以及對應的程式碼和驗證測試。

## 元件如何協作

```text
browser ──passkey──▶ zeabur-identity (in the cluster) ──tokens──▶ kubectl / OIDC clients
                        │  ▲                                     │
                 custom resources                       bearer token
                        ▼  │                                     ▼
                  kube-apiserver ◀──discovery + keys (internal TLS)── verifies tokens itself
```

一次 `kubectl` 請求的流程如下。

### 開啟登入頁面

kubelogin 開啟瀏覽器，前往 Zeabur Identity 的登入頁面。

### 使用通行密鑰登入

使用者用通行密鑰登入。kubelogin 取得短效存取權杖和更新權杖，Zeabur Identity 則用 `Grant` 物件記錄工作階段。

### 傳送權杖

`kubectl` 將權杖傳送給 kube-apiserver。

### 驗證權杖

kube-apiserver 使用 Zeabur Identity 公開的金鑰驗證權杖簽章。金鑰經由內部 TLS 連線取得，Zeabur Identity 不參與這次請求本身。

### 更新過期權杖

權杖到期後，kubelogin 使用更新權杖取得新權杖，不需要開啟瀏覽器。

管理工具 `zeabur-identityctl` 在管理員的電腦上執行，使用管理員自己的 kubeconfig，負責輸出安裝清單、依 Kubernetes 發行版產生適用的 kube-apiserver `AuthenticationConfiguration`，以及簽發讓使用者註冊通行密鑰的一次性連結。

## Crate 分工

程式碼分成多個小型 crate，全都放在 `crates/` 目錄下的同一層：

- **相依關係只向下，不形成循環**。參考 [Kladov][matklad-workspaces]，以及下方參考文獻中的 Lakos 和 Martin。
- **每個 crate 用窄介面封裝一項設計決策**。參考 [Parnas][parnas]。
- **儲存層由一組 trait 定義**。記憶體與 Kubernetes 各有一套實作，所以多數測試不需要叢集。這裡採用 ports and adapters 架構，參考 [Cockburn][cockburn]。
- **伺服器與管理 CLI 使用不同的執行檔**。Kanidm（`kanidmd`／`kanidm`）、etcd（`etcd`／`etcdctl`）和 cert-manager（`cmctl`）也採用這種分工。只有管理員需要執行的程式碼，不會放進伺服器映像檔。

每個 crate 的 README 同時是該 crate 的 crate 層級文件，可用 `cargo doc --open` 開啟。

| Crate | What it holds | Depends on |
| - | - | - |
| [`zeabur-identity-api`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-api) | `identity.zeabur.com` 自訂資源，以及其他程式與 Zeabur Identity 共用的名稱 | — |
| [`zeabur-identity-core`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-core) | 時間、加密輔助工具、驗證過的設定值、端點路徑，以及密碼、驗證碼與猜測次數限制的規則 | — |
| [`zeabur-identity-keys`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-keys) | 簽署金鑰、JWK 發布、JWS 簽署與驗證、金鑰輪替 | api, core |
| [`zeabur-identity-store`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-store) | 儲存介面（trait、紀錄）與記憶體配接器 | api, core, keys |
| [`zeabur-identity-tls`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-tls) | 信任根、憑證簽發、可重新載入的 TLS 監聽器 | — |
| [`zeabur-identity-ui`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-ui) | 伺服器算繪的頁面、內嵌資源、翻譯、安全標頭 | core |
| [`zeabur-identity-oauth`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-oauth) | OAuth 2.0 / OpenID Connect 授權伺服器 | api, core, keys, store, ui |
| [`zeabur-identity-signin`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-signin) | 通行密鑰註冊與登入、密碼與驗證碼登入、外部登入提供者、頁面路由 | api, core, keys, store, oauth, ui |
| [`zeabur-identity-k8s`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-k8s) | Kubernetes 配接器、GC、清單、kube-apiserver 設定 | api, core, keys, store, tls |
| [`zeabur-identity`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity) | **執行檔**：叢集內的伺服器 | api, core, keys, store, tls, oauth, signin, k8s |
| [`zeabur-identityctl`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identityctl) | **執行檔**：管理員 CLI | api, core, keys, k8s |
| [`zeabur-identity-test-support`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-test-support), [`zeabur-identity-e2e`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-e2e) | 測試輔助工具；叢集上的端對端測試 | — |

登入頁面的 TypeScript 與 Tailwind 原始程式碼放在 [`ui/`](https://github.com/zeabur-oss/identity/tree/v0.3.4/ui)，並以套件形式使用 Zeabur Design System。建置產物與設計系統的樣式類別都會提交到 `crates/zeabur-identity-ui`，並嵌入執行檔。樣式類別以 askama 巨集儲存在 `templates/ds.html`。

## 開發

開發環境設定、檢查、相依套件政策與叢集測試，請見 [CONTRIBUTING.md](https://github.com/zeabur-oss/identity/blob/v0.3.4/CONTRIBUTING.md)。

## 參考文獻

- The Kubernetes Authors, "KEP-3331: Structured Authentication Configuration."
  <https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/3331-structured-authentication-configuration>
- A. Kladov, "Large Rust Workspaces," Aug. 22, 2021.
  <https://matklad.github.io/2021/08/22/large-rust-workspaces.html>
- D. L. Parnas, "On the criteria to be used in decomposing systems into modules," _Commun.
  ACM_, vol. 15, no. 12, pp. 1053–1058, Dec. 1972, doi:
  [10.1145/361598.361623](https://doi.org/10.1145/361598.361623).
- J. Lakos, _Large-Scale C++ Software Design_. Addison-Wesley, 1996.
- R. C. Martin, _Agile Software Development, Principles, Patterns, and Practices_. Prentice
  Hall, 2002 (the Acyclic and Stable Dependencies Principles).
- A. Cockburn, "The Hexagonal (Ports & Adapters) Architecture," 2005.
  <https://alistair.cockburn.us/hexagonal-architecture/>

[kep-3331]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/3331-structured-authentication-configuration

[matklad-workspaces]: https://matklad.github.io/2021/08/22/large-rust-workspaces.html

[parnas]: https://doi.org/10.1145/361598.361623

[cockburn]: https://alistair.cockburn.us/hexagonal-architecture/
