Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
422 lines
15 KiB
Go
422 lines
15 KiB
Go
package store
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func manifestIntegrityStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
database, err := db.Open(t.TempDir() + "/manifest.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = database.Close() })
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := New(database)
|
|
if err := st.EnsureConfigTable(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return st
|
|
}
|
|
|
|
func manifestFixture(t *testing.T) (*Store, Campus, []Domain) {
|
|
t.Helper()
|
|
controller := manifestIntegrityStore(t)
|
|
controller.authKey = bytes.Repeat([]byte{1}, ed25519.SeedSize)
|
|
campus := Campus{UUID: "manifest-campus", Name: "controller", Title: "Controller", DefaultLang: "ja", AuthorityMode: "controller", FQDN: "controller.example.test", Enabled: true}
|
|
domains := []Domain{
|
|
{UUID: "managed-a", CampusUUID: campus.UUID, Name: "managed-a", Title: "Managed A", Domain: "a", InstanceFQDN: "local.example.test", Enabled: true},
|
|
{UUID: "managed-b", CampusUUID: campus.UUID, Name: "managed-b", Title: "Managed B", Domain: "b", InstanceFQDN: "other.example.test", Enabled: true},
|
|
}
|
|
return controller, campus, domains
|
|
}
|
|
|
|
func buildManifest(t *testing.T, controller *Store, campus Campus, domains []Domain) SignedCampusManifest {
|
|
t.Helper()
|
|
manifest, err := controller.BuildSignedCampusManifest(campus, domains)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return manifest
|
|
}
|
|
|
|
func signTestManifest(t *testing.T, controller *Store, manifest SignedCampusManifest) SignedCampusManifest {
|
|
t.Helper()
|
|
payload, err := json.Marshal(manifest.CampusManifestPayload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
private := controller.federationSigningKey()
|
|
public := private.Public().(ed25519.PublicKey)
|
|
fingerprint := sha256.Sum256(public)
|
|
manifest.ControllerPublicKey = base64.RawStdEncoding.EncodeToString(public)
|
|
manifest.ControllerFingerprint = "sha256:" + hex.EncodeToString(fingerprint[:])
|
|
manifest.Signature = base64.RawStdEncoding.EncodeToString(ed25519.Sign(private, payload))
|
|
return manifest
|
|
}
|
|
|
|
func applyManifest(st *Store, manifest SignedCampusManifest) error {
|
|
return st.ApplySignedManagedCampusManifest(manifest, manifest.Campus.UUID, "local.example.test", "https")
|
|
}
|
|
|
|
func manifestState(t *testing.T, st *Store) string {
|
|
t.Helper()
|
|
var result []string
|
|
for _, table := range []string{"campuses", "domains", "config", "federation_manifest_state", "campus_admin_grants", "campus_admin_membership_origins", "domain_memberships", "group_members", "auth_sessions"} {
|
|
rows, err := st.DB.Query(`SELECT * FROM ` + table)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
columns, err := rows.Columns()
|
|
if err != nil {
|
|
rows.Close()
|
|
t.Fatal(err)
|
|
}
|
|
for rows.Next() {
|
|
values := make([]any, len(columns))
|
|
pointers := make([]any, len(columns))
|
|
for i := range values {
|
|
pointers[i] = &values[i]
|
|
}
|
|
if err := rows.Scan(pointers...); err != nil {
|
|
rows.Close()
|
|
t.Fatal(err)
|
|
}
|
|
encoded, err := json.Marshal(values)
|
|
if err != nil {
|
|
rows.Close()
|
|
t.Fatal(err)
|
|
}
|
|
result = append(result, table+":"+string(encoded))
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
rows.Close()
|
|
t.Fatal(err)
|
|
}
|
|
rows.Close()
|
|
}
|
|
sort.Strings(result)
|
|
return strings.Join(result, "\n")
|
|
}
|
|
|
|
func TestManifestRevisionAllocationIsAtomic(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
const count = 20
|
|
type result struct {
|
|
manifest SignedCampusManifest
|
|
err error
|
|
}
|
|
for _, identical := range []bool{true, false} {
|
|
start := make(chan struct{})
|
|
results := make(chan result, count)
|
|
for i := 0; i < count; i++ {
|
|
go func(i int) {
|
|
<-start
|
|
copy := campus
|
|
if !identical {
|
|
copy.Title = fmt.Sprintf("Concurrent %d", i)
|
|
}
|
|
manifest, err := controller.BuildSignedCampusManifest(copy, domains)
|
|
results <- result{manifest, err}
|
|
}(i)
|
|
}
|
|
close(start)
|
|
seen := map[int64]bool{}
|
|
for range count {
|
|
result := <-results
|
|
if result.err != nil {
|
|
t.Fatal(result.err)
|
|
}
|
|
if _, err := verifyCampusManifest(result.manifest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if identical && result.manifest.Revision != 1 {
|
|
t.Fatalf("identical payload allocated revision %d", result.manifest.Revision)
|
|
}
|
|
if !identical && seen[result.manifest.Revision] {
|
|
t.Fatalf("different contents reused revision %d", result.manifest.Revision)
|
|
}
|
|
seen[result.manifest.Revision] = true
|
|
}
|
|
if !identical {
|
|
for revision := int64(2); revision <= count+1; revision++ {
|
|
if !seen[revision] {
|
|
t.Fatalf("allocation lost revision %d", revision)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestManifestRevisionAllocationCanonicalOrderAndOverflow(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
first := buildManifest(t, controller, campus, domains)
|
|
reordered := buildManifest(t, controller, campus, []Domain{domains[1], domains[0]})
|
|
if first.Revision != reordered.Revision || first.Signature != reordered.Signature {
|
|
t.Fatal("Domain ordering changed manifest identity")
|
|
}
|
|
if _, err := controller.DB.Exec(`UPDATE federation_manifest_versions SET revision=? WHERE campus_uuid=?`, int64(math.MaxInt64), campus.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
campus.Title = "Overflow"
|
|
if _, err := controller.BuildSignedCampusManifest(campus, domains); err == nil {
|
|
t.Fatal("manifest sequence overflow accepted")
|
|
}
|
|
var revision int64
|
|
if err := controller.DB.QueryRow(`SELECT revision FROM federation_manifest_versions WHERE campus_uuid=?`, campus.UUID).Scan(&revision); err != nil || revision != math.MaxInt64 {
|
|
t.Fatalf("overflow corrupted revision: %d, %v", revision, err)
|
|
}
|
|
if _, err := controller.DB.Exec(`DROP TABLE federation_manifest_versions`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := controller.BuildSignedCampusManifest(campus, domains); err == nil {
|
|
t.Fatal("database error reset manifest sequence")
|
|
}
|
|
}
|
|
|
|
func TestConcurrentManifestDuplicatesAndReplayNeverRegress(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
first := buildManifest(t, controller, campus, domains)
|
|
campus.Title = "New generation"
|
|
latest := buildManifest(t, controller, campus, domains[:1])
|
|
const count = 24
|
|
start := make(chan struct{})
|
|
results := make(chan error, count)
|
|
for i := 0; i < count; i++ {
|
|
go func(i int) {
|
|
<-start
|
|
manifest := first
|
|
if i%2 == 0 {
|
|
manifest = latest
|
|
}
|
|
results <- applyManifest(managed, manifest)
|
|
}(i)
|
|
}
|
|
close(start)
|
|
for range count {
|
|
if err := <-results; err != nil && !strings.Contains(err.Error(), "replay") {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var revision int64
|
|
var payload string
|
|
if err := managed.DB.QueryRow(`SELECT applied_revision,manifest_json FROM federation_manifest_state WHERE campus_uuid=?`, campus.UUID).Scan(&revision, &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if revision != latest.Revision || !strings.Contains(payload, "New generation") {
|
|
t.Fatalf("concurrent application regressed checkpoint: %d, %s", revision, payload)
|
|
}
|
|
stored, err := managed.GetCampusByUUID(campus.UUID)
|
|
if err != nil || stored.Title != latest.Campus.Title {
|
|
t.Fatalf("checkpoint and Campus disagree: %#v, %v", stored, err)
|
|
}
|
|
before := manifestState(t, managed)
|
|
if err := applyManifest(managed, latest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after := manifestState(t, managed); after != before {
|
|
t.Fatal("duplicate manifest reapplied mutable snapshot effects")
|
|
}
|
|
if err := applyManifest(managed, first); err == nil {
|
|
t.Fatal("stale manifest accepted")
|
|
}
|
|
}
|
|
|
|
func TestConcurrentFirstManifestPinsAndConflictsSerialize(t *testing.T) {
|
|
for _, kind := range []string{"key", "payload"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
first := buildManifest(t, controller, campus, domains)
|
|
second := first
|
|
if kind == "key" {
|
|
other := manifestIntegrityStore(t)
|
|
other.authKey = bytes.Repeat([]byte{2}, ed25519.SeedSize)
|
|
second = signTestManifest(t, other, second)
|
|
} else {
|
|
second.Campus.Title = "Same revision, different content"
|
|
second = signTestManifest(t, controller, second)
|
|
}
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
for _, manifest := range []SignedCampusManifest{first, second} {
|
|
go func(manifest SignedCampusManifest) { <-start; results <- applyManifest(managed, manifest) }(manifest)
|
|
}
|
|
close(start)
|
|
success := 0
|
|
for range 2 {
|
|
err := <-results
|
|
if err == nil {
|
|
success++
|
|
} else if !strings.Contains(err.Error(), "identity changed") && !strings.Contains(err.Error(), "reused") {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if success != 1 {
|
|
t.Fatalf("competing first %s manifests succeeded %d times", kind, success)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManifestSnapshotCheckpointAndProjectionRollbackTogether(t *testing.T) {
|
|
for _, stage := range []string{"initial-checkpoint", "updated-checkpoint", "projection"} {
|
|
t.Run(stage, func(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
manifest := buildManifest(t, controller, campus, domains)
|
|
trigger := `CREATE TRIGGER reject_checkpoint BEFORE INSERT ON federation_manifest_state BEGIN SELECT RAISE(ABORT,'injected checkpoint failure'); END`
|
|
if stage != "initial-checkpoint" {
|
|
if err := applyManifest(managed, manifest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
campus.Title = "Must roll back"
|
|
manifest = buildManifest(t, controller, campus, domains[:1])
|
|
trigger = `CREATE TRIGGER reject_checkpoint BEFORE UPDATE ON federation_manifest_state WHEN NEW.applied_revision!=OLD.applied_revision BEGIN SELECT RAISE(ABORT,'injected checkpoint failure'); END`
|
|
}
|
|
if stage == "projection" {
|
|
if _, err := managed.DB.Exec(`INSERT INTO campus_admin_grants VALUES('test-group','test-member','campus_admin_inherit:test','member',1)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
trigger = `CREATE TRIGGER reject_checkpoint BEFORE DELETE ON campus_admin_grants BEGIN SELECT RAISE(ABORT,'injected projection failure'); END`
|
|
}
|
|
before := manifestState(t, managed)
|
|
if _, err := managed.DB.Exec(trigger); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := applyManifest(managed, manifest); err == nil {
|
|
t.Fatal("injected application failure was ignored")
|
|
}
|
|
if after := manifestState(t, managed); after != before {
|
|
t.Fatalf("snapshot/projection/checkpoint did not roll back atomically:\nbefore:\n%s\nafter:\n%s", before, after)
|
|
}
|
|
if _, err := managed.DB.Exec(`DROP TRIGGER reject_checkpoint`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := applyManifest(managed, manifest); err != nil {
|
|
t.Fatalf("retry after rolled-back checkpoint failed: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedSnapshotCannotTakeOverOtherAuthority(t *testing.T) {
|
|
for _, collision := range []string{"standalone-campus", "controller-campus", "foreign-domain"} {
|
|
t.Run(collision, func(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
local, err := managed.GetDefaultCampus()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if collision == "foreign-domain" {
|
|
domain, err := managed.GetDefaultDomain()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domains[0].UUID = domain.UUID
|
|
} else {
|
|
campus.UUID, campus.Name = local.UUID, local.Name
|
|
for i := range domains {
|
|
domains[i].CampusUUID = campus.UUID
|
|
}
|
|
if collision == "controller-campus" {
|
|
if _, err := managed.DB.Exec(`UPDATE campuses SET authority_mode='controller' WHERE uuid=?`, local.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
manifest := buildManifest(t, controller, campus, domains)
|
|
before := manifestState(t, managed)
|
|
if err := applyManifest(managed, manifest); err == nil {
|
|
t.Fatal("valid signature authorized cross-authority takeover")
|
|
}
|
|
if after := manifestState(t, managed); after != before {
|
|
t.Fatal("rejected takeover changed local authority")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedCampusBootstrapUpgradeAndUnsignedDowngrade(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
// Legacy enrollment still works until a signed checkpoint has pinned a key.
|
|
if err := managed.ApplyManagedCampusSnapshot(campus, nil, "local.example.test", "http"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pending := buildManifest(t, controller, campus, nil)
|
|
if err := applyManifest(managed, pending); err != nil {
|
|
t.Fatalf("legacy-to-signed upgrade failed: %v", err)
|
|
}
|
|
approved := buildManifest(t, controller, campus, domains)
|
|
if err := applyManifest(managed, approved); err != nil {
|
|
t.Fatalf("approved enrollment did not deploy Domains: %v", err)
|
|
}
|
|
for _, domain := range domains {
|
|
stored, err := managed.GetDomainByUUID(domain.UUID)
|
|
if err != nil || !stored.Enabled || stored.CampusUUID != campus.UUID {
|
|
t.Fatalf("managed Domain not deployed: %#v, %v", stored, err)
|
|
}
|
|
wantPlacement := "remote"
|
|
if domain.InstanceFQDN == "local.example.test" {
|
|
wantPlacement = "local"
|
|
}
|
|
if stored.Placement != wantPlacement {
|
|
t.Fatalf("placement=%s, want %s", stored.Placement, wantPlacement)
|
|
}
|
|
locale, err := managed.GetConfigInDomain(domain.UUID, "locale")
|
|
if err != nil || locale != "ja" {
|
|
t.Fatalf("inherited locale=%s, %v", locale, err)
|
|
}
|
|
}
|
|
before := manifestState(t, managed)
|
|
if err := managed.ApplyManagedCampusSnapshot(campus, nil, "local.example.test", "http"); err == nil {
|
|
t.Fatal("unsigned fallback bypassed pinned controller signature")
|
|
}
|
|
if after := manifestState(t, managed); after != before {
|
|
t.Fatal("unsigned downgrade modified managed state")
|
|
}
|
|
}
|
|
|
|
func TestManifestApplyUsesOneConnectionAndFailsClosedOnStateErrors(t *testing.T) {
|
|
controller, campus, domains := manifestFixture(t)
|
|
managed := manifestIntegrityStore(t)
|
|
managed.DB.SetMaxOpenConns(1)
|
|
first := buildManifest(t, controller, campus, domains)
|
|
if err := applyManifest(managed, first); err != nil {
|
|
t.Fatalf("transactional projection failed with a one-connection pool: %v", err)
|
|
}
|
|
campus.Title = "Must not apply without checkpoint table"
|
|
latest := buildManifest(t, controller, campus, domains[:1])
|
|
if _, err := managed.DB.Exec(`ALTER TABLE federation_manifest_state RENAME TO unavailable_manifest_state`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := applyManifest(managed, latest); err == nil {
|
|
t.Fatal("state-table error was treated as a first enrollment")
|
|
}
|
|
stored, err := managed.GetCampusByUUID(campus.UUID)
|
|
if err != nil || stored.Title != first.Campus.Title {
|
|
t.Fatalf("checkpoint read error partially changed Campus: %#v, %v", stored, err)
|
|
}
|
|
domain, err := managed.GetDomainByUUID(domains[1].UUID)
|
|
if err != nil || !domain.Enabled {
|
|
t.Fatalf("checkpoint read error disabled a Domain: %#v, %v", domain, err)
|
|
}
|
|
}
|