Phase 9: application services and admin completion
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s

Implements PLAN.md Phase 9. internal/appservice reads registration files
(appservice.config_files), matches namespaces the way Synapse does
(anchored at the start), and delivers events to each service as
transactions read from the roomserver's event stream at a cursor kept in
appservice_state. A transaction in flight is recorded, so after a restart
it is resent under the same ID; failures back off from 2s to 5m, and a
successful ping cuts the backoff short.

In the Client-Server API an as_token acts as the service's sender or, with
?user_id=, as any existing user in its namespace. Services register and
log in their users with m.login.application_service; exclusive user and
alias namespaces are refused to everybody else with M_EXCLUSIVE. Unknown
aliases and users in a namespace are asked of the service first. The
ping endpoint reports M_URL_NOT_SET, M_BAD_STATUS, M_CONNECTION_FAILED
and M_CONNECTION_TIMEOUT. Services' own requests, and their users' when
rate_limited is false, skip the rate limiter.

Users can change their password (POST /account/password) and deactivate
their account (POST /account/deactivate), both behind the password.
Deactivation revokes tokens, deletes devices, optionally erases the
profile and leaves every room through a new userapi.AccountObserver.

The admin API is complete: user detail, the admin flag, deactivation and
reactivation, password resets, devices, media quotas
(media.max_user_storage_bytes plus per-user overrides), registration
tokens with use limits and expiry (registration.require_token), server
notices (internal/servernotices, server_notices.*), an editor for the
federation allow/deny lists persisted in server_settings, a delivery view
of the application services, and an audit log of every change.

Migration 0011 adds the registration token, server notice room, media
quota, audit log and appservice cursor tables. Policy lists are now
returned sorted. The Complement entrypoint loads the registrations
Complement mounts in /complement/appservice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-10-17 12:58:28 +02:00
1 parent b507753172
commit a94794b866
69 files changed
+6922 -124

No files matched your search

+96
View File
@@ -13,9 +13,11 @@ package settings
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"slices"
"strconv"
"sync"
@@ -26,6 +28,11 @@ import (
const (
KeyFederationEnabled = "federation.enabled"
KeyRegistrationEnabled = "registration.enabled"
// KeyFederationLists holds the allow and deny lists an admin set
// through the admin API, as one JSON object, so that the two are
// always written and read together.
KeyFederationLists = "federation.lists"
)
// ErrNotToggleable is returned when a setting is pinned by the config file.
@@ -38,6 +45,21 @@ type Defaults struct {
RegistrationEnabled bool
RegistrationAllowRuntimeToggle bool
// FederationAllowlist and FederationDenylist are the config file's
// lists, in force until an admin sets others.
FederationAllowlist []string
FederationDenylist []string
}
// FederationLists are the federation allow and deny lists in force.
type FederationLists struct {
Allowlist []string `json:"allowlist"`
Denylist []string `json:"denylist"`
// Edited reports lists set through the admin API, which replace the
// config file's until they are reset.
Edited bool `json:"-"`
}
// Snapshot is a consistent view of every runtime setting.
@@ -54,6 +76,7 @@ type Service struct {
mu sync.RWMutex
current Snapshot
lists FederationLists
listeners []func(Snapshot)
// writeMu serialises Set calls; see set.
@@ -88,6 +111,20 @@ func New(ctx context.Context, store storage.SettingsStore, defaults Defaults, lo
s.current.RegistrationEnabled = s.boolOr(stored, KeyRegistrationEnabled, defaults.RegistrationEnabled)
}
// The lists an admin edited win over the file's whatever the toggle
// settings say: they are not a switch the file can pin, and an admin
// who set them expects them to survive a restart.
s.lists = FederationLists{Allowlist: defaults.FederationAllowlist, Denylist: defaults.FederationDenylist}
if raw, ok := stored[KeyFederationLists]; ok {
var edited FederationLists
if err := json.Unmarshal([]byte(raw), &edited); err != nil {
s.log.Warn("ignoring unparseable stored federation lists", "error", err)
} else {
edited.Edited = true
s.lists = edited
}
}
s.log.Info("runtime settings loaded",
"federation_enabled", s.current.FederationEnabled,
"registration_enabled", s.current.RegistrationEnabled)
@@ -151,6 +188,65 @@ func (s *Service) SetRegistrationEnabled(ctx context.Context, enabled bool) erro
return s.set(ctx, KeyRegistrationEnabled, enabled, func(snap *Snapshot) { snap.RegistrationEnabled = enabled })
}
// FederationLists returns the allow and deny lists in force.
func (s *Service) FederationLists() FederationLists {
s.mu.RLock()
defer s.mu.RUnlock()
return FederationLists{
Allowlist: slices.Clone(s.lists.Allowlist),
Denylist: slices.Clone(s.lists.Denylist),
Edited: s.lists.Edited,
}
}
// SetFederationLists persists new allow and deny lists. Applying them to
// the federation policy is the caller's job.
func (s *Service) SetFederationLists(ctx context.Context, allowlist, denylist []string) error {
s.writeMu.Lock()
defer s.writeMu.Unlock()
lists := FederationLists{Allowlist: nonNil(allowlist), Denylist: nonNil(denylist), Edited: true}
raw, err := json.Marshal(lists)
if err != nil {
return err
}
if err := s.store.Set(ctx, KeyFederationLists, string(raw)); err != nil {
return err
}
s.mu.Lock()
s.lists = lists
s.mu.Unlock()
s.log.Info("federation lists changed", "allowlist", lists.Allowlist, "denylist", lists.Denylist)
return nil
}
// ResetFederationLists forgets the edited lists and puts the config file's
// back in force, returning them.
func (s *Service) ResetFederationLists(ctx context.Context) (FederationLists, error) {
s.writeMu.Lock()
defer s.writeMu.Unlock()
if err := s.store.Delete(ctx, KeyFederationLists); err != nil {
return FederationLists{}, err
}
lists := FederationLists{
Allowlist: nonNil(s.defaults.FederationAllowlist),
Denylist: nonNil(s.defaults.FederationDenylist),
}
s.mu.Lock()
s.lists = lists
s.mu.Unlock()
s.log.Info("federation lists reset to the configuration file's")
return lists, nil
}
func nonNil(list []string) []string {
if list == nil {
return []string{}
}
return slices.Clone(list)
}
// set writes the store first: if persistence fails the in-memory value must
// not change, or a restart would silently undo what the admin API reported
// as done.
+47
View File
@@ -198,3 +198,50 @@ func TestConcurrentReadsAndWrites(t *testing.T) {
}
wg.Wait()
}
func TestFederationListsPersist(t *testing.T) {
ctx := t.Context()
store := newMemStore(nil)
defaults := settings.Defaults{
FederationAllowlist: []string{"a.example"},
FederationDenylist: []string{"bad.example"},
}
s, err := settings.New(ctx, store, defaults, nil)
if err != nil {
t.Fatal(err)
}
if got := s.FederationLists(); got.Edited || len(got.Allowlist) != 1 || got.Denylist[0] != "bad.example" {
t.Fatalf("defaults = %+v", got)
}
if err := s.SetFederationLists(ctx, []string{"b.example"}, nil); err != nil {
t.Fatal(err)
}
// A restart keeps the edited lists over the file's.
again, err := settings.New(ctx, store, defaults, nil)
if err != nil {
t.Fatal(err)
}
got := again.FederationLists()
if !got.Edited || len(got.Allowlist) != 1 || got.Allowlist[0] != "b.example" || got.Denylist == nil || len(got.Denylist) != 0 {
t.Fatalf("after restart = %+v", got)
}
reset, err := again.ResetFederationLists(ctx)
if err != nil || reset.Edited || reset.Allowlist[0] != "a.example" {
t.Fatalf("reset = %+v, %v", reset, err)
}
if _, ok, _ := store.Get(ctx, settings.KeyFederationLists); ok {
t.Error("the stored lists survived a reset")
}
// A failed write changes nothing.
store.failSet = errors.New("disk full")
if err := again.SetFederationLists(ctx, []string{"c.example"}, nil); err == nil {
t.Fatal("a failed write succeeded")
}
if got := again.FederationLists(); got.Edited || got.Allowlist[0] != "a.example" {
t.Errorf("after a failed write = %+v", got)
}
}