# 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-CN/docs/identity/users/offboarding)。

[规范符合度](https://oss.zeabur.com/zh-CN/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 | 内容 | 依赖 |
| - | - | - |
| [`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（无环依赖原则与稳定依赖原则）。
- 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/
