Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/explanation/authorization.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
type: explanation
audience: developer who needs to understand how the API decides what a caller may do
verified: 2026-10-28
verified: 2026-11-14
---

# How the API authorizes a request
Expand All @@ -16,7 +16,7 @@ Authentication tells who calls, OpenFGA decides what they may do (`internal/auth
| write images | `can_write_images` | `images:write` |
| read, write networks | `can_read_networks`, `can_write_networks` | `networks:read`, `networks:write` |
| read, write profiles | `can_read_profiles`, `can_write_profiles` | `profiles:read`, `profiles:write` |
| rename and delete the tenant | `can_write_tenant` | `tenants:write` |
| delete the tenant | `can_write_tenant` | `tenants:write` |
| read, write memberships, and keys | `can_read_memberships`, `can_write_memberships` | `memberships:read`, `memberships:write` |

A user gets those through a role (`admin` over `operator` over `user` over `viewer`). A key gets them from tuples of its own, `key_<resource>_<action>` on the tenant, one per scope and tenant, each with the condition `not_expired`: the API sends the current time (UTC) with every `Check` and `ListObjects`, and past its date OpenFGA itself refuses the key, with nothing deleted. No role is derived for a key, and no table in the API widens what its tuples say (`instances:write` does not give `instances:read`). `Authorizer.Require(action, ...)` is the middleware of a route: `404` when the caller cannot read the tenant (a tenant that is not yours is not shown to exist), `403` when it can but may not do this, `503` when OpenFGA cannot answer. A decision that cannot be taken is never an "allowed". Creating a tenant for oneself is not an OpenFGA question: any person with an email may, and the owner then becomes the `admin` of the tenant through the tuple the composition writes. There is no platform-administrators group in the API: whoever runs the platform acts on Kubernetes and Incus directly, and a user who needs a special case asks them. Changing the name of a tenant and deleting it is the relation `can_write_tenant` of the model, which the `admin` role holds; a key can be given it with the scope `tenants:write`, on a tenant its creator administers. `GET /v1/whoami` says whether the caller may write tenants.
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ verified: 2026-11-08
| `GET /v1/whoami` | Who the credential stands for |
| `GET /v1/limits` | What the API enforces on a request: the bounds of an instance and the value each takes when a request leaves it out, the longest names, and the ceilings of the deployment (profiles and images per tenant, tenants per person). Any credential; nothing about a tenant |
| `GET /v1/tenants`, `GET /v1/tenants/{t}` | The tenants you hold a role on |
| `POST /v1/tenants`, `PATCH /v1/tenants/{t}`, `DELETE /v1/tenants/{t}` | Create a tenant for yourself (every tenant has the same limits), rename and delete it (its admins, the owner among them) |
| `POST /v1/tenants`, `DELETE /v1/tenants/{t}` | Create a tenant for yourself (one name, and every tenant has the same limits), and delete it (its admins, the owner among them) |
| `GET`, `POST /v1/tenants/{t}/images`, `GET`, `DELETE /v1/tenants/{t}/images/{name}` | The images of a tenant: read with a role or a key tuple (`images:read`), write with `operator` or `images:write` |
| `GET /v1/tenants/{t}/quota` | What a tenant may hold (instances, vCPU, memory, disk, networks, profiles; the same for every tenant) and what it holds. Read with the right of the instances |
| `GET`, `POST /v1/tenants/{t}/profiles`, `GET`, `DELETE /v1/tenants/{t}/profiles/{name}` | The sizes an instance can take: the profiles `s`, `m` and `l` every tenant is created with, and the ones its users make. Read with any role or `profiles:read`, write with `operator` or `profiles:write` |
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/tenants.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ verified: 2026-11-14
A tenant is a `Tenant` object of the `phorge.fr` group in `phorge-system` (`internal/kube/tenants.go`); Crossplane makes its namespace `t-<name>`, its Incus project, and its network.

- **Create** is for any person signed in with Authentik, for themselves: the owner is the caller (`owner` is left out or is their own email, another one is `403`), every tenant has the same limits (2 instances, 2 vCPU, 4 GiB of memory, 128 GiB of disk, 2 networks and 2 profiles, see [Limits and quotas](limits-and-quotas.md)), and a person owns at most `MAX_TENANTS_PER_USER` tenants (`409`, `quota_exceeded`, counted by owner, whoever created them). A key never creates a tenant. `GET /v1/whoami` says `canCreateTenants`. It answers `202` with a `Location`: poll it until `phase` is `Ready`. `phase` is `Provisioning`, `Ready`, `Deleting`, or `Failed` when the tenant has not become ready for 3 minutes and either `problems` says what is wrong in words made for the user (a limit that is reached, or that the platform could not complete a step yet) or the platform could not apply what makes the tenant for 3 minutes (the same rule holds for every kind: an object whose composition fails is `Failed` with a sentence that the platform administrators can see why, instead of staying `Provisioning`). It never carries what Crossplane, a provider or Incus report: those stay in `status.problems` of the object, for the administrators.
- **Patch** changes `displayName`: the admins of the tenant, the owner among them, do. A key never patches a tenant unless it holds `tenants:write`. The owner cannot change (it is an OpenFGA tuple): a body that holds it is refused. To hand a tenant over, give the new person the `admin` role with a membership.
- **A tenant has one name**, the slug of `name` (2 to 28 characters: lowercase letters, digits and hyphens, starting with a letter), which is also the name of its Incus project. There is no display name, and a body that gives `displayName` is `400`, like any field the API does not know. Nothing about a tenant can change afterwards, so there is no `PATCH` (`405`): the name is the namespace, the Incus project and the key of the rights, and the owner is an OpenFGA tuple that cannot be edited. To hand a tenant over, give the new person the `admin` role with a membership.
- **Delete** is for the admins of the tenant, the owner among them. It removes what the tenant holds first (its instances, then its memberships, then its networks, then the profiles its users made, then its images), then the tenant, all in the foreground: `202`, then the tenant stays as `Deleting` until everything it is made of is gone (about two minutes, longer with a virtual machine to stop), and a `404` on a later get means the person no longer sees the tenant: it is gone or about to be, since the rights of the person (the owner tuple) go with the tenant a little before its last resources do (measured: the `404` came while the tenant was still `Deleting`). Stop polling on it, and do not take it for proof that Incus is empty. It destroys the virtual machines of the tenant. The order is not cosmetic: the platform deletes what a tenant holds in its namespace only while the namespace exists, so the tenant waits for them (see the Hangar issue 185).
- Whoever cannot read a tenant is told it does not exist (`404`), whoever can but may not do this gets `403`. Bodies are JSON only, 64 KiB at most, with no field the API does not know.
- **There is no plan.** Every tenant has the same limits. A body that gives a `plan`, to create or to change a tenant, is `400`, like any field the API does not know.
- **There is no plan.** Every tenant has the same limits. A body that gives a `plan` is `400`, like any field the API does not know.
6 changes: 2 additions & 4 deletions internal/api/keyscopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ func TestAKeyWithTheScopesOfTheNetworksTheProfilesAndTheTenantPassesTheirRoutes(
{"GET", "/v1/apikeys", "", http.StatusForbidden},
{"GET", "/v1/tenants/other/networks", "", http.StatusNotFound},
{"POST", "/v1/tenants/other/networks", `{"name":"x"}`, http.StatusNotFound},
// the tenant: rename it; there is no plan to give
{"PATCH", "/v1/tenants/demo", `{"displayName":"renamed"}`, http.StatusOK},
{"PATCH", "/v1/tenants/demo", `{"plan":"large"}`, http.StatusBadRequest},
// the tenant: it has nothing to patch
{"PATCH", "/v1/tenants/demo", `{"displayName":"renamed"}`, http.StatusMethodNotAllowed},
{"POST", "/v1/tenants", `{"name":"fresh","owner":"x@example.org"}`, http.StatusForbidden},
{"DELETE", "/v1/tenants/demo", "", http.StatusAccepted},
} {
Expand All @@ -65,7 +64,6 @@ func TestAKeyThatOnlyReadsTheNetworksCannotWriteThem(t *testing.T) {
{"POST", "/v1/tenants/demo/networks", `{"name":"two"}`, http.StatusForbidden},
{"DELETE", "/v1/tenants/demo/networks/lan", "", http.StatusForbidden},
{"GET", "/v1/tenants/demo/profiles", "", http.StatusForbidden},
{"PATCH", "/v1/tenants/demo", `{"displayName":"renamed"}`, http.StatusForbidden},
{"DELETE", "/v1/tenants/demo", "", http.StatusForbidden},
} {
if got := call(t, e, c.method, c.path, "svc_netro", c.body); got != c.want {
Expand Down
8 changes: 4 additions & 4 deletions internal/api/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ func TestACallerThatWritesTooMuchIsSlowedDown(t *testing.T) {
s.RateLimitPerMinute, s.RateLimitBurst = 60, 3
e := newEnvWith(t, s)
for i := 0; i < 3; i++ {
if code := call(t, e, "PATCH", "/v1/tenants/demo", "admin", `{"displayName":"x"}`); code == http.StatusTooManyRequests {
if code := call(t, e, "DELETE", "/v1/tenants/demo/networks/ghost", "admin", ""); code == http.StatusTooManyRequests {
t.Fatalf("request %d of the burst was limited", i+1)
}
}
rec := e.do(t, "PATCH", "/v1/tenants/demo", "admin", `{"displayName":"x"}`)
rec := e.do(t, "DELETE", "/v1/tenants/demo/networks/ghost", "admin", "")
if rec.Code != http.StatusTooManyRequests {
t.Fatalf("the 4th: status %d: %s", rec.Code, rec.Body)
}
Expand All @@ -203,15 +203,15 @@ func TestACallerThatWritesTooMuchIsSlowedDown(t *testing.T) {
t.Fatalf("a read was limited: %d", code)
}
}
if code := call(t, e, "PATCH", "/v1/tenants/demo", "admin2", `{"displayName":"x"}`); code == http.StatusTooManyRequests {
if code := call(t, e, "DELETE", "/v1/tenants/demo/networks/ghost", "admin2", ""); code == http.StatusTooManyRequests {
t.Error("another caller was slowed down")
}
}

func TestWithoutALimitNothingIsSlowedDown(t *testing.T) {
e := newEnv(t) // RateLimitPerMinute is 0
for i := 0; i < 50; i++ {
if code := call(t, e, "PATCH", "/v1/tenants/demo", "admin", `{"displayName":"x"}`); code == http.StatusTooManyRequests {
if code := call(t, e, "DELETE", "/v1/tenants/demo/networks/ghost", "admin", ""); code == http.StatusTooManyRequests {
t.Fatal("limited with no limit set")
}
}
Expand Down
15 changes: 2 additions & 13 deletions internal/api/selfservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func usersMay() Settings {

func TestAPersonCreatesATenantForThemselves(t *testing.T) {
e := newEnvWith(t, usersMay())
rec := e.do(t, "POST", "/v1/tenants", "mallory", `{"name":"mine","displayName":"Mine"}`)
rec := e.do(t, "POST", "/v1/tenants", "mallory", `{"name":"mine"}`)
if rec.Code != http.StatusAccepted {
t.Fatalf("status %d: %s", rec.Code, rec.Body)
}
Expand Down Expand Up @@ -134,20 +134,9 @@ func TestWhoamiSaysWhetherTheCallerMayCreateATenant(t *testing.T) {
}
}

func TestTheAdminsOfATenantChangeAndDeleteIt(t *testing.T) {
func TestTheAdminsOfATenantDeleteIt(t *testing.T) {
e := newEnvWith(t, usersMay())
e.store.tenants["demo"] = tenants.Tenant{Name: "demo", Owner: "alice@example.org", Ready: true, NotReadySince: now}
for who, want := range map[string]int{"alice": 200, "tadmin": 200, "operator": 403, "user": 403, "viewer": 403, "mallory": 404, "svc": 403, "": 401} {
if code := call(t, e, "PATCH", "/v1/tenants/demo", who, `{"displayName":"renamed"}`); code != want {
t.Errorf("rename by %q: status %d, want %d", who, code, want)
}
}
// there is no plan to change: the field is unknown, whoever asks
for who, want := range map[string]int{"alice": 400, "tadmin": 400, "admin": 400, "viewer": 403} {
if code := call(t, e, "PATCH", "/v1/tenants/demo", who, `{"plan":"large"}`); code != want {
t.Errorf("plan by %q: status %d, want %d", who, code, want)
}
}
for who, want := range map[string]int{"operator": 403, "mallory": 404, "svc": 403, "alice": 202} {
if code := call(t, e, "DELETE", "/v1/tenants/demo", who, ""); code != want {
t.Errorf("delete by %q: status %d, want %d", who, code, want)
Expand Down
27 changes: 3 additions & 24 deletions internal/api/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
// tenantRoutes are the routes of the tenants.
//
// Seeing a tenant takes a right on it (a role; for a key, the scope tenants:read too). Any person creates a tenant for themselves:
// they own it, and the number of tenants a person owns is limited (MAX_TENANTS_PER_USER). Changing the
// name of a tenant and deleting it is for its admins, the owner among them. Every tenant has the same limits (internal/quota).
// they own it, and the number of tenants a person owns is limited (MAX_TENANTS_PER_USER). Deleting
// a tenant is for its admins, the owner among them. A tenant has one name and nothing else to change: its owner cannot change either. Every tenant has the same limits (internal/quota).
// A key never creates, changes or deletes a tenant.
type tenantRoutes struct {
base
Expand All @@ -30,7 +30,6 @@ func (t *tenantRoutes) register(s *server.Server, authn func(http.Handler) http.
s.Handle("GET /v1/tenants", authn(http.HandlerFunc(t.list)))
s.Handle("POST /v1/tenants", authn(t.mayCreate(http.HandlerFunc(t.create))))
s.Handle("GET /v1/tenants/{t}", authn(t.visible(http.HandlerFunc(t.get))))
s.Handle("PATCH /v1/tenants/{t}", authn(t.visible(t.adminOfTheTenant(http.HandlerFunc(t.patch)))))
s.Handle("DELETE /v1/tenants/{t}", authn(t.visible(t.adminOfTheTenant(http.HandlerFunc(t.delete)))))
}

Expand Down Expand Up @@ -67,7 +66,6 @@ func (t *tenantRoutes) adminOfTheTenant(next http.Handler) http.Handler {
// tenantBody is how a tenant is shown.
type tenantBody struct {
Name string `json:"name"`
DisplayName string `json:"displayName,omitempty"`
Owner string `json:"owner"`
Phase tenants.Phase `json:"phase"`
Problems string `json:"problems,omitempty"`
Expand All @@ -79,7 +77,7 @@ type tenantBody struct {

func (t *tenantRoutes) body(tn tenants.Tenant) tenantBody {
return tenantBody{
Name: tn.Name, DisplayName: tn.DisplayName, Owner: tn.Owner, Phase: tn.PhaseAt(t.now()),
Name: tn.Name, Owner: tn.Owner, Phase: tn.PhaseAt(t.now()),
Problems: tn.MessageAt(t.now()), Namespace: tn.Namespace, IncusProject: tn.IncusProject, CreatedAt: tn.CreatedAt, CreatedBy: tn.CreatedBy,
}
}
Expand Down Expand Up @@ -170,25 +168,6 @@ func (t *tenantRoutes) checkOwnedTenants(r *http.Request, email string) *quota.E
return quota.CheckCount("tenants", owned, max, "you own too many tenants")
}

func (t *tenantRoutes) patch(w http.ResponseWriter, r *http.Request) {
var req tenants.PatchRequest
if !decode(w, r, &req) {
return
}
if err := req.Validate(); err != nil {
t.storeError(w, err, "tenant")
return
}
tn, err := t.store.Patch(r.Context(), r.PathValue("t"), req)
if err != nil {
t.storeError(w, err, "tenant")
return
}
id, _ := auth.FromContext(r.Context())
t.log.Info("tenant changed", "tenant", tn.Name, "by", id.Subject)
writeJSON(w, http.StatusOK, t.body(tn))
}

func (t *tenantRoutes) delete(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("t")
if err := t.store.Delete(r.Context(), name); err != nil {
Expand Down
16 changes: 3 additions & 13 deletions internal/api/tenants_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestTenantLifecycleOnTheRealCluster(t *testing.T) {
})

t.Log("create", name)
rec := e.do(t, http.MethodPost, "/v1/tenants", "owner", `{"name":"`+name+`","displayName":"created by the API tests"}`)
rec := e.do(t, http.MethodPost, "/v1/tenants", "owner", `{"name":"`+name+`"}`)
if rec.Code != http.StatusAccepted {
t.Fatalf("create: status %d: %s", rec.Code, rec.Body.String())
}
Expand All @@ -101,23 +101,13 @@ func TestTenantLifecycleOnTheRealCluster(t *testing.T) {
if code := e.do(t, http.MethodGet, "/v1/tenants/"+name, "stranger", "").Code; code != http.StatusNotFound {
t.Errorf("a stranger gets %d, want 404", code)
}
if code := e.do(t, http.MethodPatch, "/v1/tenants/"+name, "owner", `{"plan":"large"}`).Code; code != http.StatusBadRequest {
t.Errorf("the owner giving a plan gets %d, want 400: there is no plan", code)
if code := e.do(t, http.MethodPatch, "/v1/tenants/"+name, "owner", `{"plan":"large"}`).Code; code != http.StatusMethodNotAllowed {
t.Errorf("patching a tenant gets %d, want 405: it has one name and nothing else to change", code)
}
if listed := names(t, e.do(t, http.MethodGet, "/v1/tenants", "owner", "")); !slices.Equal(listed, []string{name}) {
t.Errorf("the owner sees %v, want only %s", listed, name)
}

// Rename.
rec = e.do(t, http.MethodPatch, "/v1/tenants/"+name, "owner", `{"displayName":"resized"}`)
if rec.Code != http.StatusOK {
t.Fatalf("patch: status %d: %s", rec.Code, rec.Body.String())
}
waitFor(t, 3*time.Minute, "the renamed tenant to be Ready again", func() bool {
mustDecode(t, e.do(t, http.MethodGet, "/v1/tenants/"+name, "owner", ""), &got)
return got.DisplayName == "resized" && got.Phase == tenants.Ready
})

// Delete, in the foreground: it is Deleting until everything is gone, and then it is not found.
rec = e.do(t, http.MethodDelete, "/v1/tenants/"+name, "owner", "")
if rec.Code != http.StatusAccepted {
Expand Down
Loading
Loading