refactor(webui): extract the identities and tokens module (#1778)
Quality / check (push) Canceled after 1m9s
deploy / gate (push) Successful in 4m34s
deploy / archied-image (push) Successful in 2m52s
deploy / agent-image (push) Successful in 10s
deploy / dist-zip (push) Skipped

This commit is contained in:
sam committed 2026-11-03 08:32:44 +11:00
1 parent 66862a1bab
commit 369e5038ca
5 files changed
+49 -34

No files matched your search

+1 -1
View File
@@ -245,7 +245,7 @@ func TestIdentityTokenCreate(t *testing.T) {
req.Header.Set("Content-Type", "application/json")
req.SetPathValue("id", tt.id)
rec := httptest.NewRecorder()
srv.handleIdentityTokenCreate(rec, req)
newIdentitiesAPI(srv).handleIdentityTokenCreate(rec, req)
if rec.Code != tt.want {
t.Fatalf("status=%d want=%d body=%s", rec.Code, tt.want, rec.Body.String())
}
+4 -4
View File
@@ -119,7 +119,7 @@ func TestSharedTokenOperatorPostsPersonalToken(t *testing.T) {
Principals: stubPrincipals(true),
PersonalTokens: tokens,
}
handler := s.requireToken(s.authorize(http.HandlerFunc(s.handlePersonalTokenCreate)))
handler := s.requireToken(s.authorize(http.HandlerFunc(newIdentitiesAPI(s).handlePersonalTokenCreate)))
rec := httptest.NewRecorder()
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/tokens", strings.NewReader(`{"name":"ci","days":30}`))
req.Header.Set("Authorization", "Bearer shared")
@@ -260,11 +260,11 @@ func TestPersonalTokensWithoutPerson(t *testing.T) {
rec := httptest.NewRecorder()
switch method {
case http.MethodGet:
s.handlePersonalTokensList(rec, req)
newIdentitiesAPI(s).handlePersonalTokensList(rec, req)
case http.MethodPost:
s.handlePersonalTokenCreate(rec, req)
newIdentitiesAPI(s).handlePersonalTokenCreate(rec, req)
case http.MethodDelete:
s.handlePersonalTokenRevoke(rec, req)
newIdentitiesAPI(s).handlePersonalTokenRevoke(rec, req)
}
if method == http.MethodGet {
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"available":false`) {
@@ -19,6 +19,27 @@ import (
// route needs: binding a token to a NAMED identity. The remote client's
// caller-scoped PersonalTokens view cannot express that, so the store fronts
// it as its own RPC and the Client carries it under this name.
// identitiesAPI owns the identity routes and the signed-in person's API
// tokens. Either store may be nil; its routes then answer Unavailable.
type identitiesAPI struct {
identities identity.Repository
tokens identity.PersonalTokens
}
func newIdentitiesAPI(s *Server) *identitiesAPI {
return &identitiesAPI{identities: s.Identities, tokens: s.PersonalTokens}
}
func (a *identitiesAPI) register(mux *http.ServeMux) {
mux.HandleFunc("GET /api/identities", instanceOnly(a.handleIdentitiesList))
mux.HandleFunc("POST /api/identities", instanceOnly(a.handleIdentityCreate))
mux.HandleFunc("POST /api/identities/{id}/{command}", instanceOnly(a.handleIdentityCommand))
mux.HandleFunc("POST /api/identities/{id}/tokens", instanceOnly(a.handleIdentityTokenCreate))
mux.HandleFunc("GET /api/tokens", a.handlePersonalTokensList)
mux.HandleFunc("POST /api/tokens", a.handlePersonalTokenCreate)
mux.HandleFunc("DELETE /api/tokens/{id}", a.handlePersonalTokenRevoke)
}
type identityTokenBinder interface {
BindPersonalToken(ctx context.Context, id identity.IdentityID, subject identity.Subject, name string, expiresAt *time.Time, audit identity.Audit) error
}
@@ -32,12 +53,12 @@ type identityRequest struct {
ExpectedVersion int64 `json:"expected_version"`
}
func (s *Server) handleIdentitiesList(w http.ResponseWriter, r *http.Request) {
if s.Identities == nil {
func (a *identitiesAPI) handleIdentitiesList(w http.ResponseWriter, r *http.Request) {
if a.identities == nil {
http.Error(w, "identity store unavailable", http.StatusServiceUnavailable)
return
}
values, err := s.Identities.List(r.Context())
values, err := a.identities.List(r.Context())
if err != nil {
writeIdentityError(w, err)
return
@@ -48,7 +69,7 @@ func (s *Server) handleIdentitiesList(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{"identities": values})
}
func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
func (a *identitiesAPI) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
request, ok := decodeBody[identityRequest](w, r)
if !ok {
return
@@ -65,7 +86,7 @@ func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
}
value, err := identity.New(id, request.Kind, request.DisplayName)
if err == nil {
value, err = s.Identities.Create(r.Context(), value, audit)
value, err = a.identities.Create(r.Context(), value, audit)
}
if err != nil {
writeIdentityError(w, err)
@@ -75,7 +96,7 @@ func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
writeJSON(w, value)
}
func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) {
func (a *identitiesAPI) handleIdentityCommand(w http.ResponseWriter, r *http.Request) {
request, ok := decodeBody[identityRequest](w, r)
if !ok {
return
@@ -86,7 +107,7 @@ func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) {
return
}
command := identity.Command{Type: identity.CommandType(r.PathValue("command")), DisplayName: request.DisplayName}
value, err := s.Identities.Apply(r.Context(), identity.IdentityID(r.PathValue("id")), request.ExpectedVersion, command, audit)
value, err := a.identities.Apply(r.Context(), identity.IdentityID(r.PathValue("id")), request.ExpectedVersion, command, audit)
if err != nil {
writeIdentityError(w, err)
return
@@ -98,13 +119,13 @@ func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) {
// is how a non-user identity gets its bearer credential: a person's token
// comes from /api/tokens, a service account's from here. The raw token is
// returned exactly once; the store keeps only its hash.
func (s *Server) handleIdentityTokenCreate(w http.ResponseWriter, r *http.Request) {
func (a *identitiesAPI) handleIdentityTokenCreate(w http.ResponseWriter, r *http.Request) {
id := identity.IdentityID(r.PathValue("id"))
if s.Identities == nil {
if a.identities == nil {
http.Error(w, "identity store unavailable", http.StatusServiceUnavailable)
return
}
value, err := s.Identities.Get(r.Context(), id)
value, err := a.identities.Get(r.Context(), id)
if errors.Is(err, identity.ErrNotFound) {
http.Error(w, "No such identity.", http.StatusBadRequest)
return
@@ -123,7 +144,7 @@ func (s *Server) handleIdentityTokenCreate(w http.ResponseWriter, r *http.Reques
}
// The store side of the PersonalTokens contract binds a token to a named
// identity; the remote caller's view only ever names its own principal.
binder, ok := s.PersonalTokens.(identityTokenBinder)
binder, ok := a.tokens.(identityTokenBinder)
if !ok {
http.Error(w, "identity token binding unavailable", http.StatusServiceUnavailable)
return
@@ -16,15 +16,15 @@ const maxTokenNameLength = 40
// Personal API tokens belong to the signed-in person; the State Store acts
// on the principal the request carries, so these handlers name no one.
func (s *Server) handlePersonalTokensList(w http.ResponseWriter, r *http.Request) {
func (a *identitiesAPI) handlePersonalTokensList(w http.ResponseWriter, r *http.Request) {
if !personalTokenOwner(r) {
writeJSON(w, map[string]any{"tokens": []identity.PersonalToken{}, "available": false})
return
}
if !s.personalTokensReady(w, r) {
if !a.personalTokensReady(w, r) {
return
}
tokens, err := s.PersonalTokens.ListPersonalTokens(r.Context())
tokens, err := a.tokens.ListPersonalTokens(r.Context())
if err != nil {
writeIdentityError(w, err)
return
@@ -37,8 +37,8 @@ func (s *Server) handlePersonalTokensList(w http.ResponseWriter, r *http.Request
// handlePersonalTokenCreate makes a token here and sends only its hash to the
// State Store; the response is the one time the token is shown.
func (s *Server) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Request) {
if !s.personalTokensReady(w, r) {
func (a *identitiesAPI) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Request) {
if !a.personalTokensReady(w, r) {
return
}
name, expiresAt, ok := decodeTokenRequest(w, r)
@@ -50,7 +50,7 @@ func (s *Server) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Reques
http.Error(w, "cannot create token", http.StatusInternalServerError)
return
}
if err := s.PersonalTokens.AddPersonalToken(r.Context(), subject, name, expiresAt); err != nil {
if err := a.tokens.AddPersonalToken(r.Context(), subject, name, expiresAt); err != nil {
writeIdentityError(w, err)
return
}
@@ -88,11 +88,11 @@ func decodeTokenRequest(w http.ResponseWriter, r *http.Request) (string, *time.T
return name, expiresAt, true
}
func (s *Server) handlePersonalTokenRevoke(w http.ResponseWriter, r *http.Request) {
if !s.personalTokensReady(w, r) {
func (a *identitiesAPI) handlePersonalTokenRevoke(w http.ResponseWriter, r *http.Request) {
if !a.personalTokensReady(w, r) {
return
}
if err := s.PersonalTokens.RevokePersonalToken(r.Context(), r.PathValue("id")); err != nil {
if err := a.tokens.RevokePersonalToken(r.Context(), r.PathValue("id")); err != nil {
writeIdentityError(w, err)
return
}
@@ -104,13 +104,13 @@ func personalTokenOwner(r *http.Request) bool {
return ok && p.IdentityID != "" && p.IdentityID != identity.SystemID
}
func (s *Server) personalTokensReady(w http.ResponseWriter, r *http.Request) bool {
func (a *identitiesAPI) personalTokensReady(w http.ResponseWriter, r *http.Request) bool {
if !personalTokenOwner(r) {
http.Error(w, "Personal tokens need a signed-in person.", http.StatusForbidden)
return false
}
if s.PersonalTokens == nil {
if a.tokens == nil {
http.Error(w, "personal tokens unavailable", http.StatusServiceUnavailable)
return false
}
+1 -7
View File
@@ -367,15 +367,9 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/control-plane/resources/{kind}", s.handleControlPlaneQuery)
mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory)
mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand)
mux.HandleFunc("GET /api/identities", instanceOnly(s.handleIdentitiesList))
newOrgsAPI(s).register(mux)
newIdentitiesAPI(s).register(mux)
mux.HandleFunc("PUT /api/orgs/current", s.handleOrgSelect)
mux.HandleFunc("GET /api/tokens", s.handlePersonalTokensList)
mux.HandleFunc("POST /api/tokens", s.handlePersonalTokenCreate)
mux.HandleFunc("DELETE /api/tokens/{id}", s.handlePersonalTokenRevoke)
mux.HandleFunc("POST /api/identities", instanceOnly(s.handleIdentityCreate))
mux.HandleFunc("POST /api/identities/{id}/{command}", instanceOnly(s.handleIdentityCommand))
mux.HandleFunc("POST /api/identities/{id}/tokens", instanceOnly(s.handleIdentityTokenCreate))
mux.HandleFunc("GET /api/config", instanceOnly(s.handleConfig))
mux.HandleFunc("GET /api/secret-engines", instanceOnly(s.handleSecretEngines))
mux.HandleFunc("POST /api/secret-references/check", instanceOnly(s.handleSecretCheck))