# Zeabur Identity architecture

> Learn how Zeabur Identity works in a Kubernetes cluster, its security properties, how its components interact, and how its crates divide responsibilities.

Zeabur Identity is an OpenID Connect Issuer that runs in a self-managed Kubernetes cluster. kube-apiserver uses [Structured Authentication Configuration][kep-3331] to verify Zeabur Identity's tokens itself. You can sign in with a passkey, an upstream account or a password, without holding a long-lived admin kubeconfig that can't be revoked. Other OpenID Connect clients, such as Argo CD, Grafana, and Headlamp, can use the same Issuer.

k3s and RKE2 use the same configuration. CI tests cover k3s.

## Security properties

- **Passkeys first.** Passkeys are the primary credentials. They use WebAuthn and require user verification. Users can also sign in with a password of at least 15 characters, or link an account from GitHub, Google or another OpenID provider.
- **Only admin accounts receive admin privileges.** Only a `User` with `accountType: Admin` receives the `zeabur:admins` group, which is bound to `cluster-admin`. The `admin-sign-in` setting decides how admin accounts sign in. By default, they use a passkey, or a password and an authenticator code. The setting can also require a passkey, or a device-bound passkey. Admin tokens expire after five minutes by default, and inactivity can end their sessions earlier. Regular accounts receive only the permissions bound to their groups. All sessions end 12 hours after sign-in.
- **Tokens are short-lived.** They are bound to an audience, and signing keys rotate regularly.
- **Users manage their own sign-in methods.** Adding or removing a passkey, or linking or unlinking an upstream account, requires the user to authenticate again with a passkey. Changing or removing a password requires the current password. Users can't remove their last passkey or unlink their last sign-in method.
- **Kubernetes stores the state.** Custom resources store users, credentials, clients, sessions, and invitations. No database is required. For the order of access revocation and data deletion, see [Sign out, recover, and remove users](https://oss.zeabur.com/docs/identity/users/offboarding).

[Specification conformance](https://oss.zeabur.com/docs/identity/reference/conformance) lists the specification requirements Zeabur Identity implements, along with the corresponding code and verification tests.

## How the components work together

```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
```

A `kubectl` request follows these steps.

### Open the sign-in page

kubelogin opens the browser to the Zeabur Identity sign-in page.

### Sign in with a passkey

The user signs in with a passkey. kubelogin receives a short-lived access token and a refresh token, and Zeabur Identity records the session in a `Grant` object.

### Send the token

`kubectl` sends the token to kube-apiserver.

### Verify the token

kube-apiserver verifies the token's signature with the keys Zeabur Identity publishes. It retrieves the keys over an internal TLS connection. Zeabur Identity doesn't participate in the request itself.

### Refresh an expired token

When the token expires, kubelogin uses the refresh token to obtain a new token without opening the browser.

The admin tool `zeabur-identityctl` runs on the administrator's computer with the administrator's own kubeconfig. It outputs installation manifests, generates a kube-apiserver `AuthenticationConfiguration` for the Kubernetes distribution, and issues one-time links for users to register passkeys.

## Crate responsibilities

The code is split into small crates, all at the same level in the `crates/` directory:

- **Dependencies point downward and form no cycles.** See [Kladov][matklad-workspaces], as well as Lakos and Martin in the references below.
- **Each crate encapsulates one design decision behind a narrow interface.** See [Parnas][parnas].
- **A set of traits defines the storage layer.** Separate in-memory and Kubernetes implementations mean most tests don't need a cluster. This follows the ports and adapters architecture; see [Cockburn][cockburn].
- **The server and admin CLI use separate executables.** Kanidm (`kanidmd`/`kanidm`), etcd (`etcd`/`etcdctl`), and cert-manager (`cmctl`) use the same division of responsibilities. Code that only administrators need to run stays out of the server image.

Each crate's README is also its crate-level documentation. Open it with `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) | the `identity.zeabur.com` custom resources and the names other programs share with Zeabur Identity | — |
| [`zeabur-identity-core`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-core) | time, cryptographic helpers, validated config values, endpoint paths, password, authenticator code and guessing-limit rules | — |
| [`zeabur-identity-keys`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-keys) | signing keys, JWK publication, JWS signing and verification, rotation | api, core |
| [`zeabur-identity-store`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-store) | storage ports (traits, records) and in-memory adapters | api, core, keys |
| [`zeabur-identity-tls`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-tls) | trust roots, certificate issuance, a reloadable TLS listener | — |
| [`zeabur-identity-ui`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-ui) | server-rendered pages, embedded assets, translations, security headers | core |
| [`zeabur-identity-oauth`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-oauth) | the OAuth 2.0 / OpenID Connect authorization server | api, core, keys, store, ui |
| [`zeabur-identity-signin`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-signin) | passkey enrollment and sign-in, password and authenticator-code sign-in, upstream providers, page routes | api, core, keys, store, oauth, ui |
| [`zeabur-identity-k8s`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity-k8s) | Kubernetes adapters, GC, manifests, kube-apiserver config | api, core, keys, store, tls |
| [`zeabur-identity`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identity) | **binary**: the in-cluster server | api, core, keys, store, tls, oauth, signin, k8s |
| [`zeabur-identityctl`](https://github.com/zeabur-oss/identity/tree/v0.3.4/crates/zeabur-identityctl) | **binary**: the administrator's 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) | test helpers; end-to-end tests on a cluster | — |

The sign-in page's TypeScript and Tailwind source lives in [`ui/`](https://github.com/zeabur-oss/identity/tree/v0.3.4/ui) and uses the Zeabur Design System as a package. Build output and the design system's style classes are committed to `crates/zeabur-identity-ui` and embedded in the executable. The style classes are stored as askama macros in `templates/ds.html`.

## Development

See [CONTRIBUTING.md](https://github.com/zeabur-oss/identity/blob/v0.3.4/CONTRIBUTING.md) for development environment setup, checks, dependency policies, and cluster tests.

## References

- 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/
