mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-21 11:25:56 +00:00
Phase 10: central realm Registry, Host publishing lifecycle, deployment
crates/coa-registry-proto (protocol 1: types, limits, signed-request canonicalisation, test vectors), services/coa-registry (axum + PostgreSQL, migrations, rate limits, Dockerfile, deploy files), services/coa-registry-e2e (Host lifecycle against the real service, live VPS gate), coa-core realm_registry (identity keys, client, publishing loop on its own thread), Settings -> Public listing card, docs REGISTRY_PROTOCOL.md and REGISTRY_DEPLOYMENT.md. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec40f146aa
commit
1267bcc41e
56 files changed
+10198
-13
No files matched your search
Generated
+2749
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "coa-registry-e2e"
|
||||
description = "End-to-end tests: the Host Manager's publishing lifecycle (coa-core) against the real Registry service code. Test-only; separate from the service so that its image build never needs coa-core."
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
coa-core = { path = "../../crates/coa-core" }
|
||||
coa-registry = { path = "../coa-registry" }
|
||||
coa-registry-proto = { path = "../../crates/coa-registry-proto" }
|
||||
axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "net", "sync"] }
|
||||
ed25519-dalek = "2"
|
||||
serde_json = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json"] }
|
||||
tempfile = "3"
|
||||
@@ -0,0 +1,301 @@
|
||||
//! The Host Manager's publishing lifecycle (coa-core) against the real Registry code over HTTP, on virtual time.
|
||||
|
||||
#[path = "../../coa-registry/tests/common/mod.rs"]
|
||||
mod common;
|
||||
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use coa_core::realm_registry::host::{Clock, PublishState, RegistryHost, Timing};
|
||||
use coa_core::realm_registry::{AdvertSource, FileKeyStore, KeyStore, LocalAdvert};
|
||||
use coa_registry::api::ApiConfig;
|
||||
use coa_registry::limits::Rate;
|
||||
use coa_registry::store::MemoryStore;
|
||||
use common::*;
|
||||
|
||||
struct Source(Mutex<LocalAdvert>);
|
||||
|
||||
impl Source {
|
||||
fn new(level: u32) -> Arc<Self> {
|
||||
Arc::new(Self(Mutex::new(LocalAdvert { running: true, capabilities: Some(caps(level)), player_count: Some(3), player_capacity: None, manager_version: "0.6.6".into() })))
|
||||
}
|
||||
fn set(&self, f: impl FnOnce(&mut LocalAdvert)) {
|
||||
f(&mut self.0.lock().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
impl AdvertSource for Source {
|
||||
fn current(&self, _local_id: &str) -> Option<LocalAdvert> {
|
||||
Some(self.0.lock().unwrap().clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn lax() -> ApiConfig {
|
||||
ApiConfig { general: Rate { burst: 100_000.0, refill: 1000.0 }, register: Rate { burst: 100_000.0, refill: 1000.0 }, heartbeat: Rate { burst: 100_000.0, refill: 1000.0 }, ..ApiConfig::default() }
|
||||
}
|
||||
|
||||
struct Rig {
|
||||
server: TestServer,
|
||||
store: Arc<MemoryStore>,
|
||||
dir: tempfile::TempDir,
|
||||
source: Arc<Source>,
|
||||
t0: Instant,
|
||||
elapsed: Duration,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
fn new() -> Self {
|
||||
let store = Arc::new(MemoryStore::new());
|
||||
let server = spawn(store.clone(), lax());
|
||||
Self { server, store, dir: tempfile::tempdir().unwrap(), source: Source::new(60), t0: Instant::now(), elapsed: Duration::ZERO }
|
||||
}
|
||||
|
||||
fn clock(&self) -> Clock {
|
||||
let now = self.server.now.clone();
|
||||
Arc::new(move || now.load(Ordering::SeqCst))
|
||||
}
|
||||
|
||||
fn keys(&self) -> Arc<dyn KeyStore> {
|
||||
Arc::new(FileKeyStore::new(self.dir.path().join("keys")))
|
||||
}
|
||||
|
||||
/// A Manager start: everything is read back from the folder.
|
||||
fn open(&self) -> RegistryHost {
|
||||
let mut host = RegistryHost::open(self.dir.path(), self.keys(), self.source.clone(), self.clock(), Timing::default(), self.t0 + self.elapsed).unwrap();
|
||||
host.set_url(Some(&self.server.base), self.t0 + self.elapsed).unwrap();
|
||||
host
|
||||
}
|
||||
|
||||
/// Let `secs` pass for the Registry's clock and for the Host's timers, then run one pass.
|
||||
fn pass(&mut self, host: &mut RegistryHost, secs: u64) {
|
||||
self.server.advance(secs as i64);
|
||||
self.elapsed += Duration::from_secs(secs);
|
||||
host.tick(self.t0 + self.elapsed);
|
||||
}
|
||||
|
||||
fn now(&self) -> Instant {
|
||||
self.t0 + self.elapsed
|
||||
}
|
||||
}
|
||||
|
||||
fn state(host: &RegistryHost, rig: &Rig) -> PublishState {
|
||||
host.status(rig.now()).realms[0].state
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fresh_realm_makes_an_identity_registers_and_heartbeats() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Friends' realm", "Plain text.", "en", rig.now()).unwrap();
|
||||
let realm = host.realm_id("srv-1").expect("an identity was made");
|
||||
assert!(rig.dir.path().join("keys").join(format!("{realm}.key")).is_file());
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online);
|
||||
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!((rec.realm_id, rec.display_name.as_str(), rec.published, rec.online, rec.metadata_revision), (realm, "Friends' realm", true, true, 1));
|
||||
assert_eq!(Some(rec.public_key.clone()), host.public_key("srv-1"));
|
||||
assert_eq!(rec.player_count, Some(3));
|
||||
|
||||
let before = rec.last_seen_at;
|
||||
rig.pass(&mut host, 35);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert!(rec.last_seen_at > before, "the heartbeat moved last_seen");
|
||||
assert_eq!(rec.metadata_revision, 1, "an unchanged realm is not a metadata update");
|
||||
|
||||
// nothing secret in anything the Host writes or shows
|
||||
let key_text = std::fs::read_to_string(rig.dir.path().join("keys").join(format!("{realm}.key"))).unwrap();
|
||||
let status = serde_json::to_string(&host.status(rig.now())).unwrap();
|
||||
let settings = std::fs::read_to_string(rig.dir.path().join("registry.json")).unwrap();
|
||||
for text in [&status, &settings] {
|
||||
assert!(!text.contains(key_text.trim()), "the private key is only in its own file");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_manager_restart_keeps_the_realm_id_and_the_key() {
|
||||
let mut rig = Rig::new();
|
||||
let (realm, key) = {
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Restarting", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online);
|
||||
(host.realm_id("srv-1").unwrap(), host.public_key("srv-1").unwrap())
|
||||
};
|
||||
rig.server.advance(5);
|
||||
let mut host = rig.open();
|
||||
assert_eq!((host.realm_id("srv-1"), host.public_key("srv-1")), (Some(realm), Some(key.clone())), "the same identity after the restart");
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online, "it resumes by itself");
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!((rec.realm_id, rec.public_key), (realm, key));
|
||||
assert_eq!(rec.metadata_revision, 1, "registering again as itself changed nothing");
|
||||
assert_eq!(rig.dir.path().join("keys").read_dir().unwrap().count(), 1, "no second key was made");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_capabilities_reach_the_registry_with_the_next_heartbeat() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Caps", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
rig.source.set(|a| a.capabilities = Some(caps(70)));
|
||||
rig.pass(&mut host, 35);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!((rec.capabilities.progression.unwrap().max_player_level, rec.metadata_revision), (70, 2));
|
||||
rig.pass(&mut host, 35);
|
||||
assert_eq!(host.registry_record("srv-1").unwrap().metadata_revision, 2, "and then nothing is rewritten");
|
||||
// a rename
|
||||
host.publish("srv-1", "Renamed", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
rig.pass(&mut host, 0);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!(rec.display_name, "Renamed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_heartbeats_make_the_realm_offline_and_it_comes_back() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Gone and back", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
// the Manager is closed: nothing is sent
|
||||
rig.server.advance(125);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!((rec.online, rec.published), (false, true), "offline, not deleted");
|
||||
drop(host);
|
||||
let mut host = rig.open();
|
||||
rig.pass(&mut host, 0);
|
||||
assert!(host.registry_record("srv-1").unwrap().online, "the restarted Manager brings it back");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stopped_realm_sends_no_heartbeats_and_goes_offline() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Stoppable", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
rig.source.set(|a| a.running = false);
|
||||
for _ in 0..5 {
|
||||
rig.pass(&mut host, 35);
|
||||
}
|
||||
assert_eq!(state(&host, &rig), PublishState::RealmStopped);
|
||||
assert!(!host.registry_record("srv-1").unwrap().online, "no worldserver, no presence");
|
||||
rig.source.set(|a| a.running = true);
|
||||
rig.pass(&mut host, 35);
|
||||
assert!(host.registry_record("srv-1").unwrap().online);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unpublishing_stops_the_publication_and_the_identity_is_kept() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Brief", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
let realm = host.realm_id("srv-1").unwrap();
|
||||
host.unpublish("srv-1", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Disabled);
|
||||
assert!(!host.registry_record("srv-1").unwrap().published);
|
||||
rig.pass(&mut host, 60);
|
||||
assert!(!host.registry_record("srv-1").unwrap().published, "no heartbeat republishes it");
|
||||
host.publish("srv-1", "Brief", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
assert_eq!((rec.realm_id, rec.published, rec.online), (realm, true, true), "the same realm id, published again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_registry_that_is_down_is_retried_with_a_growing_delay_and_nothing_else_happens() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = RegistryHost::open(rig.dir.path(), rig.keys(), rig.source.clone(), rig.clock(), Timing::default(), rig.now()).unwrap();
|
||||
host.set_url(Some("http://127.0.0.1:9"), rig.now()).unwrap();
|
||||
host.publish("srv-1", "Offline registry", "", "en", rig.now()).unwrap();
|
||||
let mut waits = Vec::new();
|
||||
for _ in 0..6 {
|
||||
host.tick(rig.now());
|
||||
let s = host.status(rig.now());
|
||||
assert_eq!(s.realms[0].state, PublishState::Retrying);
|
||||
assert!(s.realms[0].last_error.as_deref().unwrap_or("").contains("could not be reached"));
|
||||
let wait = s.realms[0].retry_in_secs.unwrap();
|
||||
waits.push(wait);
|
||||
rig.elapsed += Duration::from_secs(wait + 1);
|
||||
}
|
||||
assert!(waits.windows(2).filter(|w| w[1] >= w[0]).count() >= 4, "the delay grows: {waits:?}");
|
||||
assert!(waits.iter().all(|w| *w <= 300 * 5 / 4 + 1), "and is capped: {waits:?}");
|
||||
// the Registry comes back: the same Host recovers by itself
|
||||
host.set_url(Some(&rig.server.base), rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_refusal_that_repeating_cannot_fix_stops_the_publication() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Contested", "", "en", rig.now()).unwrap();
|
||||
// somebody else got to this realm id first with another key
|
||||
let mut squatter = Host::new(&rig.server, 99);
|
||||
squatter.realm = host.realm_id("srv-1").unwrap();
|
||||
assert_eq!(squatter.register("Squatter", 60).status, 200);
|
||||
rig.pass(&mut host, 0);
|
||||
let s = host.status(rig.now());
|
||||
assert_eq!(s.realms[0].state, PublishState::Rejected);
|
||||
assert!(s.realms[0].retry_in_secs.is_none(), "no further attempt is scheduled");
|
||||
assert!(s.realms[0].last_error.as_deref().unwrap_or("").contains("RealmKeyMismatch"));
|
||||
let seen = rig.store.get_for_test(&squatter.realm);
|
||||
rig.pass(&mut host, 600);
|
||||
rig.pass(&mut host, 600);
|
||||
assert_eq!(state(&host, &rig), PublishState::Rejected, "still stopped");
|
||||
assert_eq!(rig.store.get_for_test(&squatter.realm), seen, "and nothing was sent in the meantime");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_registry_that_lost_its_data_gets_the_realm_again() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Amnesia", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
rig.store.forget_all();
|
||||
rig.pass(&mut host, 35);
|
||||
assert_eq!(state(&host, &rig), PublishState::Retrying, "the heartbeat of an unknown realm is refused");
|
||||
rig.pass(&mut host, 2);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online, "and it registers again as itself");
|
||||
assert!(host.registry_record("srv-1").unwrap().online);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_host_never_publishes_what_is_not_discovery_metadata() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Plain", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
let rec = host.registry_record("srv-1").unwrap();
|
||||
let json = serde_json::to_string(&rec).unwrap().to_lowercase();
|
||||
for word in ["password", "credential", "secret", "snapshot", "inventory", "canonical", "character_id", "ra_user", "mysql", "token"] {
|
||||
assert!(!json.contains(word), "the Registry's record mentions {word}: {json}");
|
||||
}
|
||||
assert!(host.publish("srv-1", &"x".repeat(81), "", "en", rig.now()).is_err());
|
||||
assert!(host.publish("srv-1", "ok", "", "not a language", rig.now()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_realm_whose_key_is_gone_is_stopped_and_publishing_again_makes_a_new_identity() {
|
||||
let mut rig = Rig::new();
|
||||
let mut host = rig.open();
|
||||
host.publish("srv-1", "Lost key", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
let old = host.realm_id("srv-1").unwrap();
|
||||
drop(host);
|
||||
std::fs::remove_file(rig.dir.path().join("keys").join(format!("{old}.key"))).unwrap();
|
||||
let mut host = rig.open();
|
||||
rig.pass(&mut host, 0);
|
||||
let s = host.status(rig.now());
|
||||
assert_eq!(s.realms[0].state, PublishState::Rejected);
|
||||
assert!(s.realms[0].last_error.as_deref().unwrap_or("").contains("key is missing"), "{:?}", s.realms[0].last_error);
|
||||
host.publish("srv-1", "Lost key", "", "en", rig.now()).unwrap();
|
||||
rig.pass(&mut host, 0);
|
||||
assert_eq!(state(&host, &rig), PublishState::Online);
|
||||
assert_ne!(host.realm_id("srv-1").unwrap(), old, "a new identity, not the old id with a guessed key");
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
//! The Phase-10 gate against a real node (`REGISTRY_URL=http://<VPS_IP>`), through the public path: Caddy, the Registry container, PostgreSQL.
|
||||
//! `#[ignore]`d. The infrastructure items (container restarts, port scan, database contents) are driven by `tools/registry_gate.ps1` / `.sh`.
|
||||
|
||||
#[path = "../../coa-registry/tests/common/mod.rs"]
|
||||
mod common;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use coa_core::realm_registry::host::{system_clock, PublishState, RegistryHost, Timing};
|
||||
use coa_core::realm_registry::{AdvertSource, FileKeyStore, KeyStore, LocalAdvert};
|
||||
use coa_registry_proto::sign::sign_request;
|
||||
use coa_registry_proto::*;
|
||||
use common::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn base() -> Option<String> {
|
||||
std::env::var("REGISTRY_URL").ok().filter(|u| !u.is_empty())
|
||||
}
|
||||
|
||||
struct Fixed(LocalAdvert);
|
||||
|
||||
impl AdvertSource for Fixed {
|
||||
fn current(&self, _: &str) -> Option<LocalAdvert> {
|
||||
Some(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn pass(n: u32, what: &str) {
|
||||
println!("GATE {n:>2} PASS {what}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gate_1_to_12_against_the_node() {
|
||||
let Some(base) = base() else { return };
|
||||
|
||||
// 1. a fresh realm generates its identity and registers (through the Host Manager code)
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let keys: Arc<dyn KeyStore> = Arc::new(FileKeyStore::new(dir.path().join("keys")));
|
||||
let source = Arc::new(Fixed(LocalAdvert { running: true, capabilities: Some(caps(60)), player_count: Some(2), player_capacity: None, manager_version: "0.6.6".into() }));
|
||||
let t0 = Instant::now();
|
||||
let mut elapsed = Duration::ZERO;
|
||||
let mut host = RegistryHost::open(dir.path(), keys.clone(), source.clone(), system_clock(), Timing::default(), t0).unwrap();
|
||||
host.set_url(Some(&base), t0).unwrap();
|
||||
host.publish("srv-gate", "Gate realm", "Phase 10 gate", "en", t0).unwrap();
|
||||
host.tick(t0);
|
||||
assert_eq!(host.status(t0).realms[0].state, PublishState::Online, "{:?}", host.status(t0));
|
||||
let realm = host.realm_id("srv-gate").unwrap();
|
||||
let key = host.public_key("srv-gate").unwrap();
|
||||
let rec = host.registry_record("srv-gate").unwrap();
|
||||
assert_eq!((rec.realm_id, rec.published, rec.online, rec.metadata_revision), (realm, true, true, 1));
|
||||
pass(1, &format!("fresh realm {realm} generated an identity and registered"));
|
||||
|
||||
// 3. a Host Manager restart keeps the same RealmId and key
|
||||
drop(host);
|
||||
let mut host = RegistryHost::open(dir.path(), keys.clone(), source.clone(), system_clock(), Timing::default(), t0).unwrap();
|
||||
host.set_url(Some(&base), t0).unwrap();
|
||||
assert_eq!((host.realm_id("srv-gate"), host.public_key("srv-gate")), (Some(realm), Some(key.clone())));
|
||||
std::thread::sleep(Duration::from_millis(1200));
|
||||
host.tick(t0);
|
||||
assert_eq!(host.status(t0).realms[0].state, PublishState::Online);
|
||||
let rec2 = host.registry_record("srv-gate").unwrap();
|
||||
assert_eq!((rec2.realm_id, rec2.public_key.clone(), rec2.metadata_revision), (realm, key, 1));
|
||||
pass(3, "after a Manager restart: same RealmId, same key, registered again as itself, revision unchanged");
|
||||
|
||||
// 4. a valid heartbeat updates last_seen
|
||||
std::thread::sleep(Duration::from_millis(2100));
|
||||
elapsed += Duration::from_secs(31);
|
||||
host.tick(t0 + elapsed + Duration::from_secs(10));
|
||||
let rec3 = host.registry_record("srv-gate").unwrap();
|
||||
assert!(rec3.last_seen_at > rec2.last_seen_at, "{} -> {}", rec2.last_seen_at, rec3.last_seen_at);
|
||||
pass(4, &format!("heartbeat moved last_seen_at {} -> {}", rec2.last_seen_at, rec3.last_seen_at));
|
||||
|
||||
// 6. a capability change updates the record with the next heartbeat
|
||||
let source2 = Arc::new(Fixed(LocalAdvert { running: true, capabilities: Some(caps(70)), player_count: Some(3), player_capacity: None, manager_version: "0.6.6".into() }));
|
||||
drop(host);
|
||||
let mut host = RegistryHost::open(dir.path(), keys.clone(), source2, system_clock(), Timing::default(), t0).unwrap();
|
||||
host.set_url(Some(&base), t0).unwrap();
|
||||
std::thread::sleep(Duration::from_millis(1200));
|
||||
host.tick(t0);
|
||||
std::thread::sleep(Duration::from_millis(1200));
|
||||
elapsed += Duration::from_secs(31);
|
||||
host.tick(t0 + elapsed + Duration::from_secs(40));
|
||||
let rec4 = host.registry_record("srv-gate").unwrap();
|
||||
assert_eq!(rec4.capabilities.progression.as_ref().unwrap().max_player_level, 70, "{rec4:?}");
|
||||
assert!(rec4.metadata_revision > rec3.metadata_revision);
|
||||
pass(6, &format!("capability change (cap 60 -> 70) applied; metadata revision {} -> {}", rec3.metadata_revision, rec4.metadata_revision));
|
||||
|
||||
// 12. unpublish stops publication (the Manager's path)
|
||||
host.unpublish("srv-gate", t0 + elapsed).unwrap();
|
||||
host.tick(t0 + elapsed);
|
||||
let rec5 = host.registry_record("srv-gate").unwrap();
|
||||
assert!(!rec5.published && !rec5.online);
|
||||
pass(12, "unpublish: published=false, online=false");
|
||||
|
||||
// 7-11 on a separate realm with the raw protocol
|
||||
let mut h = Host::remote(&base);
|
||||
assert_eq!(h.register("Raw realm", 60).status, 200);
|
||||
let mut thief = Host::remote(&base);
|
||||
thief.realm = h.realm;
|
||||
let r = thief.register("Stolen", 60);
|
||||
assert_eq!((r.status, r.code().as_str()), (403, "realm_key_mismatch"));
|
||||
let r = thief.heartbeat(60, true);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"));
|
||||
assert_eq!(h.me().body["display_name"], "Raw realm");
|
||||
pass(7, "same RealmId signed by another key: register 403 realm_key_mismatch, heartbeat 401 invalid_signature, record untouched");
|
||||
|
||||
h.sync_clock();
|
||||
let path = path_heartbeat(&h.realm);
|
||||
let body = serde_json::to_vec(&h.heartbeat_body(60, false)).unwrap();
|
||||
h.ts += 1;
|
||||
let headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body);
|
||||
let mut tampered = body.clone();
|
||||
tampered.push(b' ');
|
||||
let r = h.send_with("POST", &path, &tampered, &headers.pairs());
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"));
|
||||
pass(8, "body tampered after signing: 401 invalid_signature");
|
||||
|
||||
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64;
|
||||
for skew in [-3600, 3600] {
|
||||
h.ts = now + skew;
|
||||
let r = h.send("POST", &path, &body);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "bad_timestamp"), "skew {skew}");
|
||||
}
|
||||
h.ts = now;
|
||||
h.sync_clock();
|
||||
let ok = h.send("POST", &path, &body);
|
||||
assert_eq!(ok.status, 200, "{:?}", ok.body);
|
||||
h.ts -= 5;
|
||||
let r = h.send("POST", &path, &body);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "timestamp_not_monotonic"));
|
||||
pass(9, "old (-1h) and future (+1h) timestamps: 401 bad_timestamp; a non-increasing one: 401 timestamp_not_monotonic");
|
||||
|
||||
h.sync_clock();
|
||||
let mut hv = sign_request(&h.key, "POST", &path, &h.realm, h.ts + 1, &body).pairs();
|
||||
hv[0] = (sign_header_version(), "2".into());
|
||||
let r = h.send_with("POST", &path, &body, &hv);
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"));
|
||||
let mut v2 = h.heartbeat_body(60, false);
|
||||
v2.protocol_version = 2;
|
||||
let r = h.send("POST", &path, &serde_json::to_vec(&v2).unwrap());
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"));
|
||||
pass(10, "protocol version 2 (header and body): 400 unsupported_protocol_version");
|
||||
|
||||
let big = vec![b'x'; MAX_REQUEST_BYTES + 1];
|
||||
let r = h.send("POST", PATH_REGISTER, &big);
|
||||
assert_eq!((r.status, r.code().as_str()), (413, "request_too_large"));
|
||||
let mut hostile = Host::remote(&base);
|
||||
let base_body = serde_json::to_value(hostile.register_body("Hostile", 60)).unwrap();
|
||||
for (field, value) in [("display_name", json!("x".repeat(500))), ("description", json!("d".repeat(5000))), ("language", json!("<script>alert(1)</script>")), ("display_name", json!("evil\u{202E}name"))] {
|
||||
let mut v = base_body.clone();
|
||||
v[field] = value;
|
||||
let r = hostile.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap());
|
||||
assert_eq!(r.status, 400, "{field}");
|
||||
}
|
||||
for extra in ["character", "snapshot", "password", "ra_password", "db_credentials", "canonical_revision"] {
|
||||
let mut v = base_body.clone();
|
||||
v[extra] = json!("secret");
|
||||
let r = hostile.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap());
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "malformed_request"), "{extra}");
|
||||
}
|
||||
let nested = format!("{}1{}", "[".repeat(30_000), "]".repeat(30_000));
|
||||
assert_eq!(hostile.send("POST", PATH_REGISTER, nested.as_bytes()).status, 400);
|
||||
let mut clean = Host::remote(&base);
|
||||
clean.realm = hostile.realm;
|
||||
assert_eq!(clean.me().status, 404, "nothing was stored by the hostile requests");
|
||||
pass(11, "oversized body 413; hostile/oversized metadata, unknown fields, JSON nesting bomb: 400; nothing stored");
|
||||
|
||||
// the per-realm allowance (burst 4, one more every 10 s) was used up by the refusals above
|
||||
std::thread::sleep(Duration::from_secs(22));
|
||||
h.sync_clock();
|
||||
assert_eq!(h.unpublish().status, 200);
|
||||
std::thread::sleep(Duration::from_secs(12));
|
||||
h.sync_clock();
|
||||
let r = h.heartbeat(60, false);
|
||||
assert_eq!((r.status, r.code().as_str()), (409, "not_published"));
|
||||
pass(12, "raw unpublish: heartbeat afterwards 409 not_published");
|
||||
|
||||
println!("GATE_REALM {realm}");
|
||||
}
|
||||
|
||||
fn sign_header_version() -> &'static str {
|
||||
coa_registry_proto::sign::HEADER_VERSION
|
||||
}
|
||||
|
||||
/// 5. a realm that stops heartbeating goes offline after the TTL and is not deleted (takes just over two minutes).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gate_5_missing_heartbeats_mean_offline_not_deleted() {
|
||||
let Some(base) = base() else { return };
|
||||
let mut h = Host::remote(&base);
|
||||
assert_eq!(h.register("TTL realm", 60).status, 200);
|
||||
h.sync_clock();
|
||||
assert_eq!(h.me().body["online"], true);
|
||||
let started = Instant::now();
|
||||
std::thread::sleep(Duration::from_secs(125));
|
||||
h.sync_clock();
|
||||
let me = h.me();
|
||||
assert_eq!((me.status, me.body["online"].as_bool(), me.body["published"].as_bool()), (200, Some(false), Some(true)), "{:?}", me.body);
|
||||
h.sync_clock();
|
||||
assert_eq!(h.heartbeat(60, false).status, 200);
|
||||
h.sync_clock();
|
||||
assert_eq!(h.me().body["online"], true);
|
||||
pass(5, &format!("no heartbeat for {}s: online=false, published=true, record kept; a heartbeat brings it back", started.elapsed().as_secs()));
|
||||
println!("GATE_REALM {}", h.realm);
|
||||
}
|
||||
|
||||
/// The record a Registry restart (item 2) and a database restart (item 14) are checked against: written once, read back by the driver script.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gate_persist_probe_write() {
|
||||
let Some(base) = base() else { return };
|
||||
let mut h = Host::remote(&base);
|
||||
assert_eq!(h.register("Persist realm", 60).status, 200);
|
||||
let seed = h.key.to_bytes();
|
||||
println!("PERSIST_REALM {} {}", h.realm, hex_of(&seed));
|
||||
}
|
||||
|
||||
fn hex_of(b: &[u8]) -> String {
|
||||
b.iter().map(|x| format!("{x:02x}")).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gate_persist_probe_read() {
|
||||
let Some(base) = base() else { return };
|
||||
let (Ok(realm), Ok(seed)) = (std::env::var("PERSIST_REALM"), std::env::var("PERSIST_SEED")) else { return };
|
||||
let mut h = Host::remote(&base);
|
||||
h.realm = RealmId::parse(&realm).unwrap();
|
||||
let bytes: Vec<u8> = (0..seed.len() / 2).map(|i| u8::from_str_radix(&seed[2 * i..2 * i + 2], 16).unwrap()).collect();
|
||||
h.key = ed25519_dalek::SigningKey::from_bytes(&bytes.try_into().unwrap());
|
||||
h.sync_clock();
|
||||
let me = h.me();
|
||||
assert_eq!((me.status, me.body["display_name"].as_str(), me.body["published"].as_bool()), (200, Some("Persist realm"), Some(true)), "{:?}", me.body);
|
||||
h.sync_clock();
|
||||
assert_eq!(h.heartbeat(60, false).status, 200, "and it can still heartbeat with its own key");
|
||||
println!("PERSIST_OK {realm}");
|
||||
}
|
||||
Generated
+1918
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,35 @@
|
||||
[package]
|
||||
name = "coa-registry"
|
||||
description = "CoA realm Registry service (Phase 10): discovery and presence only. Standalone; not part of the Manager workspace."
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-only"
|
||||
publish = false
|
||||
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
coa-registry-proto = { path = "../../crates/coa-registry-proto" }
|
||||
axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio", "query"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "signal", "time", "net", "sync"] }
|
||||
tower = { version = "0.5", features = ["limit", "util"] }
|
||||
tower-http = { version = "0.6", features = ["timeout"] }
|
||||
tokio-postgres = { version = "0.7", features = ["with-serde_json-1"] }
|
||||
deadpool-postgres = "0.14"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
ed25519-dalek = "2"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
|
||||
thiserror = "2"
|
||||
hex = "0.4"
|
||||
pin-project-lite = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json"] }
|
||||
tempfile = "3"
|
||||
|
||||
[profile.release]
|
||||
lto = "thin"
|
||||
codegen-units = 4
|
||||
strip = true
|
||||
@@ -0,0 +1,15 @@
|
||||
# Build context: the repository root. docker build -f services/coa-registry/Dockerfile -t coa-registry:<tag> .
|
||||
FROM rust:1-bookworm AS build
|
||||
WORKDIR /src
|
||||
COPY crates/coa-registry-proto crates/coa-registry-proto
|
||||
COPY services/coa-registry services/coa-registry
|
||||
WORKDIR /src/services/coa-registry
|
||||
RUN cargo build --release --locked --bin coa-registry
|
||||
|
||||
FROM debian:bookworm-slim
|
||||
RUN useradd --system --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin coa
|
||||
COPY --from=build /src/services/coa-registry/target/release/coa-registry /usr/local/bin/coa-registry
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD ["/usr/local/bin/coa-registry", "--healthcheck"]
|
||||
ENTRYPOINT ["/usr/local/bin/coa-registry"]
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
# No domain yet: plain HTTP on :80 only, no ACME, no admin API. HTTPS is required before the public phases.
|
||||
auto_https off
|
||||
admin off
|
||||
}
|
||||
|
||||
:80 {
|
||||
respond /healthz "ok" 200
|
||||
|
||||
handle /registry/* {
|
||||
request_body {
|
||||
max_size 70KB
|
||||
}
|
||||
reverse_proxy coa-registry:8080 {
|
||||
# the Registry rate-limits by the address Caddy saw, taken from the last entry of this header
|
||||
header_up X-Forwarded-For {remote_host}
|
||||
transport http {
|
||||
dial_timeout 3s
|
||||
response_header_timeout 15s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
respond "not found" 404
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
# Run on the node as root: sudo sh audit-db.sh (the password is read inside the container from its own secret)
|
||||
set -eu
|
||||
docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec psql -U coa -d coa_registry -X -q' < "$(dirname "$0")/audit-db.sql"
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Phase-10 gate item 17: what the database holds. Run: sudo sh audit-db.sh
|
||||
\echo == tables
|
||||
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY 1;
|
||||
\echo == columns of realms
|
||||
SELECT column_name || ' ' || data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'realms' ORDER BY ordinal_position;
|
||||
\echo == any table or column that looks like players, characters, accounts, credentials, snapshots (must be empty)
|
||||
SELECT table_name || '.' || column_name FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND (column_name ~* '(password|passwd|secret|token|credential|account|character|snapshot|inventory|payload|session|canonical|guid|ra_user|mysql)' OR table_name ~* '(player|character|account|session|snapshot|credential)');
|
||||
\echo == rows containing forbidden words anywhere in their text form (must be 0)
|
||||
SELECT count(*) FROM realms r WHERE row_to_json(r)::text ~* '(password|credential|secret|snapshot|inventory|canonical|character_id|ra_password|mysql|private)';
|
||||
\echo == the keys stored are public keys of 32 bytes (private keys are 32 bytes too: they are never sent, there is no field for them)
|
||||
SELECT count(*) AS realms, count(*) FILTER (WHERE octet_length(public_key) = 32) AS keys32 FROM realms;
|
||||
\echo == largest stored capabilities (bytes)
|
||||
SELECT max(octet_length(capabilities::text)) FROM realms;
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# Build the Registry image on the node from the working tree and (re)start the stack. Usage (from the repository root):
|
||||
# VPS=ubuntu@<VPS_IP> TAG=p10c sh services/coa-registry/deploy/deploy.sh
|
||||
# Secrets are never touched here: /opt/coa/secrets/{postgres_password,registry_db_password} must already exist (see docs/REGISTRY_DEPLOYMENT.md).
|
||||
set -eu
|
||||
: "${VPS:?set VPS=user@host}" "${TAG:?set TAG=<image tag>}"
|
||||
tar --exclude=target -czf /tmp/registry-src.tgz crates/coa-registry-proto services/coa-registry .dockerignore
|
||||
scp -q /tmp/registry-src.tgz "$VPS:/tmp/registry-src.tgz"
|
||||
ssh "$VPS" "set -e
|
||||
sudo rm -rf /opt/coa/registry/src && mkdir -p /opt/coa/registry/src && tar -xzf /tmp/registry-src.tgz -C /opt/coa/registry/src
|
||||
cd /opt/coa/registry/src && sudo docker build -q -f services/coa-registry/Dockerfile -t coa-registry:$TAG . >/dev/null
|
||||
cd /opt/coa/registry && sudo sed -i 's/^REGISTRY_TAG=.*/REGISTRY_TAG=$TAG/' .env && sudo docker compose up -d
|
||||
sleep 8 && sudo docker ps --format '{{.Names}} {{.Status}}'"
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
# Phase-10 gate items 2 and 14, driven from a workstation: VPS=ubuntu@<VPS_IP> REGISTRY_URL=http://<VPS_IP> sh gate-restarts.sh
|
||||
# A realm is registered once (random key); after every restart below it must still be there and must still be able to heartbeat with its own key.
|
||||
set -u
|
||||
: "${VPS:?}" "${REGISTRY_URL:?}"
|
||||
E2E="$(cd "$(dirname "$0")/../../coa-registry-e2e" && pwd)"
|
||||
run() { (cd "$E2E" && REGISTRY_URL="$REGISTRY_URL" "$@" 2>&1); }
|
||||
remote() { ssh "$VPS" "$1"; }
|
||||
wait_healthy() { # $1 container, $2 seconds
|
||||
i=0; while [ $i -lt "$2" ]; do
|
||||
s=$(remote "sudo docker inspect -f '{{.State.Health.Status}}' $1 2>/dev/null"); [ "$s" = healthy ] && return 0; i=$((i+2)); sleep 2; done; return 1; }
|
||||
wait_api() { i=0; while [ $i -lt 90 ]; do code=$(curl -s -o /dev/null -m 5 -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz"); [ "$code" = 200 ] && return 0; i=$((i+2)); sleep 2; done; return 1; }
|
||||
|
||||
out=$(run cargo test --test live_vps gate_persist_probe_write -- --ignored --nocapture | grep PERSIST_REALM)
|
||||
set -- $out; REALM=$2; SEED=$3
|
||||
[ -n "$REALM" ] || { echo "GATE FAIL: probe write"; exit 1; }
|
||||
echo "probe realm $REALM written"
|
||||
check() { # $1 label
|
||||
r=$(PERSIST_REALM=$REALM PERSIST_SEED=$SEED run cargo test --test live_vps gate_persist_probe_read -- --ignored --nocapture | grep -E "PERSIST_OK|panicked")
|
||||
case "$r" in *PERSIST_OK*) echo "GATE PASS $1: realm still registered, own key still accepted";; *) echo "GATE FAIL $1: $r"; return 1;; esac; }
|
||||
|
||||
echo "--- 2. Registry container restart"
|
||||
remote "sudo docker restart coa-registry-registry-1 >/dev/null"; wait_healthy coa-registry-registry-1 90 && wait_api && check "2: after restarting the Registry container"
|
||||
|
||||
echo "--- 14a. PostgreSQL container restart (health is degraded meanwhile, then recovers by itself)"
|
||||
remote "sudo docker restart coa-postgres-postgres-1 >/dev/null"
|
||||
sleep 1; echo "health during the restart: $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz")"
|
||||
wait_healthy coa-postgres-postgres-1 90 && wait_api && check "14a: after restarting PostgreSQL (the Registry container was not restarted)"
|
||||
|
||||
echo "--- 14b. both stopped, PostgreSQL started later than the Registry (the Registry waits for it)"
|
||||
remote "sudo docker stop coa-registry-registry-1 coa-postgres-postgres-1 >/dev/null; sudo docker start coa-registry-registry-1 >/dev/null"
|
||||
sleep 6; echo "Registry up without a database: health $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz")"
|
||||
remote "sudo docker start coa-postgres-postgres-1 >/dev/null"
|
||||
wait_healthy coa-postgres-postgres-1 90 && wait_healthy coa-registry-registry-1 120 && wait_api && check "14b: after a cold start in the wrong order"
|
||||
|
||||
echo "--- 14c. the stack taken down and brought up again (the data lives in the volume)"
|
||||
remote "cd /opt/coa/registry && sudo docker compose down >/dev/null 2>&1; cd /opt/coa/postgres && sudo docker compose down >/dev/null 2>&1; cd /opt/coa/postgres && sudo docker compose up -d >/dev/null 2>&1; cd /opt/coa/registry && sudo docker compose up -d >/dev/null 2>&1"
|
||||
wait_healthy coa-postgres-postgres-1 90 && wait_healthy coa-registry-registry-1 120 && wait_api && check "14c: after compose down/up of both stacks"
|
||||
remote "sudo docker ps --format '{{.Names}} {{.Status}}'"
|
||||
@@ -0,0 +1,37 @@
|
||||
# /opt/coa/postgres/compose.yaml (Phase 10). No `ports:`: PostgreSQL is reachable only on the internal coa-db network.
|
||||
# The superuser password is read from /opt/coa/secrets (never in git); the Registry has its own least-privilege role and database.
|
||||
name: coa-postgres
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: coa
|
||||
POSTGRES_DB: coa
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --auth-local=scram-sha-256"
|
||||
command: ["postgres", "-c", "max_connections=40", "-c", "shared_buffers=128MB", "-c", "password_encryption=scram-sha-256", "-c", "log_min_duration_statement=1000"]
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
networks:
|
||||
coa-db:
|
||||
aliases:
|
||||
- coa-postgres
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U coa -d coa"]
|
||||
interval: 15s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
mem_limit: 768m
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: /opt/coa/secrets/postgres_password
|
||||
volumes:
|
||||
pgdata:
|
||||
networks:
|
||||
coa-db:
|
||||
external: true
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
# Create the Registry's database role and database (idempotent). Run on the node as root: sudo sh provision-db.sh
|
||||
# The password is read from /opt/coa/secrets/registry_db_password and goes to psql on stdin, never on a command line.
|
||||
set -eu
|
||||
PW=$(cat /opt/coa/secrets/registry_db_password)
|
||||
docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec psql -U coa -d coa -v ON_ERROR_STOP=1 -q' <<SQL
|
||||
DO \$\$ BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'coa_registry') THEN
|
||||
CREATE ROLE coa_registry LOGIN PASSWORD '$PW' NOSUPERUSER NOCREATEDB NOCREATEROLE CONNECTION LIMIT 20;
|
||||
ELSE
|
||||
ALTER ROLE coa_registry PASSWORD '$PW';
|
||||
END IF;
|
||||
END \$\$;
|
||||
SELECT 'CREATE DATABASE coa_registry OWNER coa_registry' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'coa_registry')\gexec
|
||||
REVOKE ALL ON DATABASE coa_registry FROM PUBLIC;
|
||||
GRANT CONNECT ON DATABASE coa_registry TO coa_registry;
|
||||
SQL
|
||||
echo "registry role and database are in place"
|
||||
@@ -0,0 +1,45 @@
|
||||
# /opt/coa/registry/compose.yaml (Phase 10). The Registry publishes no host port: Caddy reaches it as coa-registry:8080 on coa-ingress,
|
||||
# and it reaches PostgreSQL as coa-postgres on coa-db. The image is built on the node from the repository (see docs/REGISTRY_DEPLOYMENT.md).
|
||||
name: coa-registry
|
||||
services:
|
||||
registry:
|
||||
image: coa-registry:${REGISTRY_TAG:-latest}
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
REGISTRY_LISTEN: "0.0.0.0:8080"
|
||||
REGISTRY_TRUST_PROXY: "1"
|
||||
REGISTRY_DB_HOST: coa-postgres
|
||||
REGISTRY_DB_NAME: coa_registry
|
||||
REGISTRY_DB_USER: coa_registry
|
||||
REGISTRY_DB_PASSWORD_FILE: /run/secrets/registry_db_password
|
||||
REGISTRY_DB_POOL: "8"
|
||||
REGISTRY_DB_STATEMENT_TIMEOUT_MS: "5000"
|
||||
REGISTRY_MAX_CONNECTIONS: "256"
|
||||
REGISTRY_MAX_IN_FLIGHT: "64"
|
||||
REGISTRY_REQUEST_TIMEOUT_SECS: "10"
|
||||
# staging: enough registrations from one address for the Phase-10 gate; the built-in default is 10 per hour
|
||||
REGISTRY_REGISTER_BURST: "${REGISTRY_REGISTER_BURST:-10}"
|
||||
REGISTRY_REGISTER_PER_HOUR: "${REGISTRY_REGISTER_PER_HOUR:-10}"
|
||||
RUST_LOG: "info"
|
||||
secrets:
|
||||
- registry_db_password
|
||||
read_only: true
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
mem_limit: 256m
|
||||
pids_limit: 128
|
||||
networks:
|
||||
coa-ingress:
|
||||
aliases:
|
||||
- coa-registry
|
||||
coa-db: {}
|
||||
secrets:
|
||||
registry_db_password:
|
||||
file: /opt/coa/secrets/registry_db_password
|
||||
networks:
|
||||
coa-ingress:
|
||||
external: true
|
||||
coa-db:
|
||||
external: true
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Registry schema, migration 1. Forward-only: a released migration is never edited.
|
||||
-- Discovery and presence data only. There is deliberately no table for players, characters, accounts, sessions or credentials.
|
||||
CREATE TABLE realms (
|
||||
realm_id uuid PRIMARY KEY,
|
||||
public_key bytea NOT NULL CHECK (octet_length(public_key) = 32),
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
last_seen_at timestamptz NOT NULL,
|
||||
published boolean NOT NULL,
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 80),
|
||||
description text NOT NULL CHECK (octet_length(description) <= 1024),
|
||||
language text NOT NULL CHECK (char_length(language) BETWEEN 2 AND 16),
|
||||
ruleset text NOT NULL CHECK (ruleset IN ('coa', 'wildcard')),
|
||||
manager_version text NOT NULL CHECK (char_length(manager_version) BETWEEN 1 AND 32),
|
||||
capabilities jsonb NOT NULL CHECK (octet_length(capabilities::text) <= 65536),
|
||||
capabilities_hash text NOT NULL CHECK (capabilities_hash ~ '^[0-9a-f]{64}$'),
|
||||
metadata_revision bigint NOT NULL CHECK (metadata_revision >= 1),
|
||||
-- the timestamp of the last signed request that was applied; the next one must be strictly later (replay protection)
|
||||
last_request_ts bigint NOT NULL,
|
||||
player_count integer CHECK (player_count BETWEEN 0 AND 100000),
|
||||
player_capacity integer CHECK (player_capacity BETWEEN 0 AND 100000)
|
||||
);
|
||||
CREATE INDEX realms_published_seen ON realms (published, last_seen_at);
|
||||
@@ -0,0 +1,380 @@
|
||||
//! The HTTP surface of Registry protocol 1.
|
||||
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use axum::body::Body;
|
||||
use axum::extract::{ConnectInfo, Path, State};
|
||||
use axum::http::{HeaderMap, Method, Request, StatusCode, Uri};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::{Json, Router};
|
||||
use coa_registry_proto::sign::{self, SignedHeaders};
|
||||
use coa_registry_proto::*;
|
||||
use std::result::Result;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::limits::{Limiter, Rate};
|
||||
use crate::listener::Peer;
|
||||
use crate::store::{RealmRow, Store, StoreError};
|
||||
|
||||
pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ApiConfig {
|
||||
pub online_ttl_secs: u64,
|
||||
/// Behind a reverse proxy the client address is the last entry of `X-Forwarded-For` (the one the proxy itself appended).
|
||||
pub trust_proxy: bool,
|
||||
pub general: Rate,
|
||||
pub register: Rate,
|
||||
pub heartbeat: Rate,
|
||||
}
|
||||
|
||||
impl Default for ApiConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
online_ttl_secs: ONLINE_TTL_SECS,
|
||||
trust_proxy: false,
|
||||
general: Rate::per_minute(120, 120.0),
|
||||
register: Rate::per_hour(10, 10.0),
|
||||
heartbeat: Rate { burst: 4.0, refill: 1.0 / 10.0 },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppState<S: Store> {
|
||||
pub store: S,
|
||||
pub clock: Clock,
|
||||
pub cfg: ApiConfig,
|
||||
ip_general: Limiter<IpAddr>,
|
||||
ip_register: Limiter<IpAddr>,
|
||||
realm_heartbeat: Limiter<RealmId>,
|
||||
started: Instant,
|
||||
}
|
||||
|
||||
impl<S: Store> AppState<S> {
|
||||
pub fn new(store: S, clock: Clock, cfg: ApiConfig) -> Self {
|
||||
Self {
|
||||
ip_general: Limiter::new(cfg.general, 100_000),
|
||||
ip_register: Limiter::new(cfg.register, 100_000),
|
||||
realm_heartbeat: Limiter::new(cfg.heartbeat, 100_000),
|
||||
store,
|
||||
clock,
|
||||
cfg,
|
||||
started: Instant::now(),
|
||||
}
|
||||
}
|
||||
fn now(&self) -> i64 {
|
||||
(self.clock)()
|
||||
}
|
||||
fn mono(&self) -> f64 {
|
||||
self.started.elapsed().as_secs_f64()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ApiError {
|
||||
status: StatusCode,
|
||||
code: ErrorCode,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
fn new(status: StatusCode, code: ErrorCode, message: impl Into<String>) -> Self {
|
||||
Self { status, code, message: message.into() }
|
||||
}
|
||||
fn malformed(message: impl Into<String>) -> Self {
|
||||
Self::new(StatusCode::BAD_REQUEST, ErrorCode::MalformedRequest, message)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> Response {
|
||||
(self.status, Json(ErrorBody { error: ErrorDetail { code: self.code, message: self.message } })).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoreError> for ApiError {
|
||||
fn from(e: StoreError) -> Self {
|
||||
match e {
|
||||
StoreError::Unknown => Self::new(StatusCode::NOT_FOUND, ErrorCode::UnknownRealm, "this realm is not registered"),
|
||||
StoreError::KeyMismatch => Self::new(StatusCode::FORBIDDEN, ErrorCode::RealmKeyMismatch, "this realm id belongs to another key"),
|
||||
StoreError::Stale => Self::new(StatusCode::UNAUTHORIZED, ErrorCode::TimestampNotMonotonic, "the timestamp is not later than the last request of this realm"),
|
||||
StoreError::NotPublished => Self::new(StatusCode::CONFLICT, ErrorCode::NotPublished, "the realm is not published; register it again"),
|
||||
StoreError::Backend(m) => {
|
||||
tracing::error!(error = %m, "storage failure");
|
||||
Self::new(StatusCode::SERVICE_UNAVAILABLE, ErrorCode::Unavailable, "storage is unavailable")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn router<S: Store>(state: Arc<AppState<S>>) -> Router {
|
||||
Router::new()
|
||||
.route("/registry/v1/healthz", get(healthz::<S>))
|
||||
.route("/registry/v1/realms/register", post(register::<S>))
|
||||
.route("/registry/v1/realms/{realm_id}/heartbeat", post(heartbeat::<S>))
|
||||
.route("/registry/v1/realms/{realm_id}/unpublish", post(unpublish::<S>))
|
||||
.route("/registry/v1/realms/{realm_id}", get(self_info::<S>))
|
||||
.fallback(|| async { ApiError::new(StatusCode::NOT_FOUND, ErrorCode::MalformedRequest, "no such endpoint") })
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
fn client_ip(headers: &HeaderMap, peer: SocketAddr, trust_proxy: bool) -> IpAddr {
|
||||
if trust_proxy {
|
||||
if let Some(last) = headers.get("x-forwarded-for").and_then(|v| v.to_str().ok()).and_then(|v| v.rsplit(',').next()) {
|
||||
if let Ok(ip) = last.trim().parse::<IpAddr>() {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
peer.ip()
|
||||
}
|
||||
|
||||
fn short(hash: &str) -> &str {
|
||||
hash.get(..8).unwrap_or("")
|
||||
}
|
||||
|
||||
fn log(endpoint: &str, realm: Option<&RealmId>, started: Instant, result: &Result<(StatusCode, String), ApiError>) {
|
||||
let (status, caps) = match result {
|
||||
Ok((s, c)) => (s.as_u16(), c.as_str()),
|
||||
Err(e) => (e.status.as_u16(), ""),
|
||||
};
|
||||
let code = result.as_ref().err().map(|e| format!("{:?}", e.code)).unwrap_or_default();
|
||||
tracing::info!(endpoint, realm = realm.map(|r| r.to_string()).unwrap_or_default(), status, error = %code, latency_ms = started.elapsed().as_millis() as u64, caps = caps, "request");
|
||||
}
|
||||
|
||||
/// The IP-level admission every request passes before anything is parsed.
|
||||
fn admit<S: Store>(st: &AppState<S>, ip: IpAddr, cost: f64) -> Result<(), ApiError> {
|
||||
if st.ip_general.take(&ip, cost, st.mono()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ApiError::new(StatusCode::TOO_MANY_REQUESTS, ErrorCode::RateLimited, "too many requests"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_body(body: Body) -> Result<Vec<u8>, ApiError> {
|
||||
match axum::body::to_bytes(body, MAX_REQUEST_BYTES).await {
|
||||
Ok(b) => Ok(b.to_vec()),
|
||||
Err(_) => Err(ApiError::new(StatusCode::PAYLOAD_TOO_LARGE, ErrorCode::RequestTooLarge, format!("a request is limited to {MAX_REQUEST_BYTES} bytes"))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Headers, id and timestamp: everything that can be checked before the key is known.
|
||||
fn prelude<S: Store>(st: &AppState<S>, headers: &HeaderMap, uri: &Uri, path_realm: Option<&str>) -> Result<(SignedHeaders, RealmId), ApiError> {
|
||||
if uri.query().is_some() {
|
||||
return Err(ApiError::malformed("a signed endpoint takes no query string"));
|
||||
}
|
||||
let h = SignedHeaders::parse(|n| headers.get(n).and_then(|v| v.to_str().ok())).map_err(|e| ApiError::malformed(e.to_string()))?;
|
||||
if h.version != REGISTRY_PROTOCOL_VERSION {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::UnsupportedProtocolVersion, format!("protocol version {} is not supported; this Registry speaks {REGISTRY_PROTOCOL_VERSION}", h.version)));
|
||||
}
|
||||
let realm = match path_realm {
|
||||
Some(text) => RealmId::parse(text).map_err(|_| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidRealmId, "the realm id in the path is not a canonical UUIDv7"))?,
|
||||
None => h.realm,
|
||||
};
|
||||
if h.realm != realm {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidRealmId, "the realm header and the realm id disagree"));
|
||||
}
|
||||
if (h.timestamp - st.now()).abs() > MAX_CLOCK_SKEW_SECS {
|
||||
return Err(ApiError::new(StatusCode::UNAUTHORIZED, ErrorCode::BadTimestamp, format!("the timestamp is more than {MAX_CLOCK_SKEW_SECS} seconds from the Registry's clock")));
|
||||
}
|
||||
Ok((h, realm))
|
||||
}
|
||||
|
||||
fn check_signature<S: Store>(st: &AppState<S>, ip: IpAddr, key: &[u8; 32], h: &SignedHeaders, method: &Method, path: &str, body: &[u8]) -> Result<(), ApiError> {
|
||||
let ok = ed25519_dalek::VerifyingKey::from_bytes(key).map(|k| sign::verify_request(&k, h, method.as_str(), path, body)).unwrap_or(false);
|
||||
if ok {
|
||||
return Ok(());
|
||||
}
|
||||
// a forged request costs its sender more than an honest one
|
||||
st.ip_general.take(&ip, 4.0, st.mono());
|
||||
Err(ApiError::new(StatusCode::UNAUTHORIZED, ErrorCode::InvalidSignature, "the signature does not match"))
|
||||
}
|
||||
|
||||
fn realm_admit<S: Store>(st: &AppState<S>, realm: &RealmId) -> Result<(), ApiError> {
|
||||
if st.realm_heartbeat.take(realm, 1.0, st.mono()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ApiError::new(StatusCode::TOO_MANY_REQUESTS, ErrorCode::RateLimited, "this realm sends requests too fast"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn healthz<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, req: Request<Body>) -> Response {
|
||||
let ip = client_ip(req.headers(), peer, st.cfg.trust_proxy);
|
||||
if let Err(e) = admit(&st, ip, 1.0) {
|
||||
return e.into_response();
|
||||
}
|
||||
if st.store.ping().await {
|
||||
Json(Health { status: "ok".into(), protocol_version: REGISTRY_PROTOCOL_VERSION }).into_response()
|
||||
} else {
|
||||
(StatusCode::SERVICE_UNAVAILABLE, Json(Health { status: "degraded".into(), protocol_version: REGISTRY_PROTOCOL_VERSION })).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
fn json<T: Serialize>(value: &T) -> (StatusCode, Response) {
|
||||
(StatusCode::OK, Json(value).into_response())
|
||||
}
|
||||
|
||||
async fn register<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, req: Request<Body>) -> Response {
|
||||
let started = Instant::now();
|
||||
let (parts, body) = req.into_parts();
|
||||
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
|
||||
let mut realm_for_log = None;
|
||||
let result: Result<(StatusCode, Response, String), ApiError> = async {
|
||||
admit(&st, ip, 1.0)?;
|
||||
if !st.ip_register.take(&ip, 1.0, st.mono()) {
|
||||
return Err(ApiError::new(StatusCode::TOO_MANY_REQUESTS, ErrorCode::RateLimited, "too many registrations from this address"));
|
||||
}
|
||||
let bytes = read_body(body).await?;
|
||||
let (h, realm) = prelude(&st, &parts.headers, &parts.uri, None)?;
|
||||
realm_for_log = Some(realm);
|
||||
let req: RegisterRequest = serde_json::from_slice(&bytes).map_err(|_| ApiError::malformed("the body is not a registration"))?;
|
||||
if req.protocol_version != REGISTRY_PROTOCOL_VERSION {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::UnsupportedProtocolVersion, format!("protocol version {} is not supported", req.protocol_version)));
|
||||
}
|
||||
if req.realm_id != realm {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidRealmId, "the realm header and the body disagree"));
|
||||
}
|
||||
req.validate().map_err(|e| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidMetadata, e.to_string()))?;
|
||||
let public = sign::decode_public_key(&req.public_key).map_err(|e| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidMetadata, e.to_string()))?;
|
||||
check_signature(&st, ip, public.as_bytes(), &h, &parts.method, parts.uri.path(), &bytes)?;
|
||||
let now = st.now();
|
||||
let (row, created) = st.store.register(&req, *public.as_bytes(), now, h.timestamp).await?;
|
||||
let resp = RegisterResponse {
|
||||
protocol_version: REGISTRY_PROTOCOL_VERSION,
|
||||
realm_id: realm,
|
||||
created,
|
||||
metadata_revision: row.metadata_revision,
|
||||
published: row.published,
|
||||
capabilities_hash: row.capabilities_hash.clone(),
|
||||
server_time: now,
|
||||
heartbeat_interval_secs: HEARTBEAT_INTERVAL_SECS,
|
||||
online_ttl_secs: st.cfg.online_ttl_secs,
|
||||
};
|
||||
let (status, response) = json(&resp);
|
||||
Ok((status, response, row.capabilities_hash))
|
||||
}
|
||||
.await;
|
||||
finish("register", realm_for_log.as_ref(), started, result)
|
||||
}
|
||||
|
||||
fn finish(endpoint: &str, realm: Option<&RealmId>, started: Instant, result: Result<(StatusCode, Response, String), ApiError>) -> Response {
|
||||
match result {
|
||||
Ok((status, response, caps)) => {
|
||||
log(endpoint, realm, started, &Ok((status, short(&caps).to_string())));
|
||||
response
|
||||
}
|
||||
Err(e) => {
|
||||
log(endpoint, realm, started, &Err(ApiError { status: e.status, code: e.code, message: String::new() }));
|
||||
e.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared by heartbeat, unpublish and the self endpoint: an existing realm, a valid signature, and (for writes) the per-realm rate.
|
||||
async fn authenticate<S: Store>(st: &AppState<S>, ip: IpAddr, parts: &axum::http::request::Parts, path_realm: &str, bytes: &[u8], limited: bool) -> Result<(RealmId, i64), ApiError> {
|
||||
let (h, realm) = prelude(st, &parts.headers, &parts.uri, Some(path_realm))?;
|
||||
let key = st.store.key_of(&realm).await?.ok_or(StoreError::Unknown)?;
|
||||
check_signature(st, ip, &key, &h, &parts.method, parts.uri.path(), bytes)?;
|
||||
if limited {
|
||||
realm_admit(st, &realm)?;
|
||||
}
|
||||
Ok((realm, h.timestamp))
|
||||
}
|
||||
|
||||
async fn heartbeat<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, Path(realm_text): Path<String>, req: Request<Body>) -> Response {
|
||||
let started = Instant::now();
|
||||
let (parts, body) = req.into_parts();
|
||||
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
|
||||
let mut realm_for_log = None;
|
||||
let result: Result<(StatusCode, Response, String), ApiError> = async {
|
||||
admit(&st, ip, 1.0)?;
|
||||
let bytes = read_body(body).await?;
|
||||
let (realm, ts) = authenticate(&st, ip, &parts, &realm_text, &bytes, true).await?;
|
||||
realm_for_log = Some(realm);
|
||||
let hb: HeartbeatRequest = serde_json::from_slice(&bytes).map_err(|_| ApiError::malformed("the body is not a heartbeat"))?;
|
||||
if hb.protocol_version != REGISTRY_PROTOCOL_VERSION {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::UnsupportedProtocolVersion, format!("protocol version {} is not supported", hb.protocol_version)));
|
||||
}
|
||||
hb.validate().map_err(|e| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidMetadata, e.to_string()))?;
|
||||
let now = st.now();
|
||||
let out = st.store.heartbeat(&realm, &hb, now, ts).await?;
|
||||
let resp = HeartbeatResponse { protocol_version: REGISTRY_PROTOCOL_VERSION, metadata_revision: out.metadata_revision, published: true, capabilities_hash: out.capabilities_hash.clone(), resend_capabilities: out.resend_capabilities, server_time: now };
|
||||
let (status, response) = json(&resp);
|
||||
Ok((status, response, out.capabilities_hash))
|
||||
}
|
||||
.await;
|
||||
finish("heartbeat", realm_for_log.as_ref(), started, result)
|
||||
}
|
||||
|
||||
async fn unpublish<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, Path(realm_text): Path<String>, req: Request<Body>) -> Response {
|
||||
let started = Instant::now();
|
||||
let (parts, body) = req.into_parts();
|
||||
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
|
||||
let mut realm_for_log = None;
|
||||
let result: Result<(StatusCode, Response, String), ApiError> = async {
|
||||
admit(&st, ip, 1.0)?;
|
||||
let bytes = read_body(body).await?;
|
||||
let (realm, ts) = authenticate(&st, ip, &parts, &realm_text, &bytes, true).await?;
|
||||
realm_for_log = Some(realm);
|
||||
let un: UnpublishRequest = serde_json::from_slice(&bytes).map_err(|_| ApiError::malformed("the body is not an unpublish request"))?;
|
||||
if un.protocol_version != REGISTRY_PROTOCOL_VERSION {
|
||||
return Err(ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::UnsupportedProtocolVersion, format!("protocol version {} is not supported", un.protocol_version)));
|
||||
}
|
||||
let now = st.now();
|
||||
let revision = st.store.unpublish(&realm, now, ts).await?;
|
||||
let (status, response) = json(&UnpublishResponse { protocol_version: REGISTRY_PROTOCOL_VERSION, published: false, metadata_revision: revision, server_time: now });
|
||||
Ok((status, response, String::new()))
|
||||
}
|
||||
.await;
|
||||
finish("unpublish", realm_for_log.as_ref(), started, result)
|
||||
}
|
||||
|
||||
fn self_response(row: RealmRow, now: i64, ttl: u64) -> SelfResponse {
|
||||
let online = row.published && now - row.last_seen_at <= ttl as i64;
|
||||
SelfResponse {
|
||||
protocol_version: REGISTRY_PROTOCOL_VERSION,
|
||||
realm_id: row.realm_id,
|
||||
public_key: sign::encode_public_key(&ed25519_dalek::VerifyingKey::from_bytes(&row.public_key).expect("a stored key was validated on the way in")),
|
||||
display_name: row.display_name,
|
||||
description: row.description,
|
||||
language: row.language,
|
||||
ruleset: row.ruleset,
|
||||
manager_version: row.manager_version,
|
||||
capabilities: row.capabilities,
|
||||
capabilities_hash: row.capabilities_hash,
|
||||
metadata_revision: row.metadata_revision,
|
||||
published: row.published,
|
||||
online,
|
||||
created_at: row.created_at,
|
||||
updated_at: row.updated_at,
|
||||
last_seen_at: row.last_seen_at,
|
||||
player_count: row.player_count,
|
||||
player_capacity: row.player_capacity,
|
||||
server_time: now,
|
||||
}
|
||||
}
|
||||
|
||||
/// The authenticated read of a realm's own record. It does not advance the replay counter (a read changes nothing), so it is accepted with any timestamp inside the window.
|
||||
async fn self_info<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, Path(realm_text): Path<String>, req: Request<Body>) -> Response {
|
||||
let started = Instant::now();
|
||||
let (parts, body) = req.into_parts();
|
||||
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
|
||||
let mut realm_for_log = None;
|
||||
let result: Result<(StatusCode, Response, String), ApiError> = async {
|
||||
admit(&st, ip, 1.0)?;
|
||||
let bytes = read_body(body).await?;
|
||||
if !bytes.is_empty() {
|
||||
return Err(ApiError::malformed("a read has no body"));
|
||||
}
|
||||
let (realm, _) = authenticate(&st, ip, &parts, &realm_text, &bytes, false).await?;
|
||||
realm_for_log = Some(realm);
|
||||
let row = st.store.get(&realm).await?.ok_or(StoreError::Unknown)?;
|
||||
let caps = row.capabilities_hash.clone();
|
||||
let (status, response) = json(&self_response(row, st.now(), st.cfg.online_ttl_secs));
|
||||
Ok((status, response, caps))
|
||||
}
|
||||
.await;
|
||||
finish("self", realm_for_log.as_ref(), started, result)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
//! Configuration from the environment. The database password is read from a file (a Docker secret), never from the environment or the image.
|
||||
|
||||
use std::env;
|
||||
|
||||
use crate::api::ApiConfig;
|
||||
use crate::limits::Rate;
|
||||
use crate::pg::PgConfig;
|
||||
|
||||
pub struct Config {
|
||||
pub listen: String,
|
||||
pub max_connections: usize,
|
||||
pub request_timeout_secs: u64,
|
||||
pub max_in_flight: usize,
|
||||
pub api: ApiConfig,
|
||||
pub pg: PgConfig,
|
||||
}
|
||||
|
||||
fn var(name: &str, default: &str) -> String {
|
||||
env::var(name).ok().filter(|v| !v.is_empty()).unwrap_or_else(|| default.to_string())
|
||||
}
|
||||
|
||||
fn num<T: std::str::FromStr>(name: &str, default: T) -> Result<T, String> {
|
||||
match env::var(name).ok().filter(|v| !v.is_empty()) {
|
||||
Some(v) => v.parse().map_err(|_| format!("{name} is not a valid number")),
|
||||
None => Ok(default),
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_env() -> Result<Self, String> {
|
||||
let password_file = env::var("REGISTRY_DB_PASSWORD_FILE").map_err(|_| "REGISTRY_DB_PASSWORD_FILE is required".to_string())?;
|
||||
let password = std::fs::read_to_string(&password_file).map_err(|e| format!("cannot read the database password file: {e}"))?.trim().to_string();
|
||||
if password.is_empty() {
|
||||
return Err("the database password file is empty".into());
|
||||
}
|
||||
let mut api = ApiConfig { trust_proxy: var("REGISTRY_TRUST_PROXY", "0") == "1", ..ApiConfig::default() };
|
||||
api.online_ttl_secs = num("REGISTRY_ONLINE_TTL_SECS", api.online_ttl_secs)?;
|
||||
api.register = Rate::per_hour(num("REGISTRY_REGISTER_BURST", 10)?, num("REGISTRY_REGISTER_PER_HOUR", 10.0)?);
|
||||
api.general = Rate::per_minute(num("REGISTRY_GENERAL_BURST", 120)?, num("REGISTRY_GENERAL_PER_MINUTE", 120.0)?);
|
||||
Ok(Self {
|
||||
listen: var("REGISTRY_LISTEN", "0.0.0.0:8080"),
|
||||
max_connections: num("REGISTRY_MAX_CONNECTIONS", 256)?,
|
||||
request_timeout_secs: num("REGISTRY_REQUEST_TIMEOUT_SECS", 10)?,
|
||||
max_in_flight: num("REGISTRY_MAX_IN_FLIGHT", 64)?,
|
||||
api,
|
||||
pg: PgConfig {
|
||||
host: var("REGISTRY_DB_HOST", "postgres"),
|
||||
port: num("REGISTRY_DB_PORT", 5432)?,
|
||||
dbname: var("REGISTRY_DB_NAME", "coa"),
|
||||
user: var("REGISTRY_DB_USER", "coa"),
|
||||
password,
|
||||
pool_size: num("REGISTRY_DB_POOL", 8)?,
|
||||
statement_timeout_ms: num("REGISTRY_DB_STATEMENT_TIMEOUT_MS", 5000)?,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//! The CoA realm Registry (Phase 10): who publishes a realm and whether it is alive. Nothing else.
|
||||
//!
|
||||
//! The Registry never receives or stores database or RA credentials, game account passwords, character snapshots, canonical revisions,
|
||||
//! inventories, session payloads or player character ids. Every mutating request is signed by the realm's own Ed25519 key.
|
||||
|
||||
pub mod api;
|
||||
pub mod config;
|
||||
pub mod limits;
|
||||
pub mod listener;
|
||||
pub mod pg;
|
||||
pub mod store;
|
||||
@@ -0,0 +1,88 @@
|
||||
//! In-memory token buckets for abuse control. One process, one node: no shared store is needed.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Rate {
|
||||
pub burst: f64,
|
||||
/// Tokens per second.
|
||||
pub refill: f64,
|
||||
}
|
||||
|
||||
impl Rate {
|
||||
pub fn per_minute(burst: u32, per_minute: f64) -> Self {
|
||||
Self { burst: burst as f64, refill: per_minute / 60.0 }
|
||||
}
|
||||
pub fn per_hour(burst: u32, per_hour: f64) -> Self {
|
||||
Self { burst: burst as f64, refill: per_hour / 3600.0 }
|
||||
}
|
||||
}
|
||||
|
||||
struct Bucket {
|
||||
tokens: f64,
|
||||
at: f64,
|
||||
}
|
||||
|
||||
pub struct Limiter<K: Eq + Hash + Clone> {
|
||||
rate: Rate,
|
||||
max_keys: usize,
|
||||
buckets: Mutex<HashMap<K, Bucket>>,
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash + Clone> Limiter<K> {
|
||||
pub fn new(rate: Rate, max_keys: usize) -> Self {
|
||||
Self { rate, max_keys, buckets: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
|
||||
/// Take `cost` tokens at time `now` (seconds, any monotonic origin). `false`: refused, nothing taken.
|
||||
pub fn take(&self, key: &K, cost: f64, now: f64) -> bool {
|
||||
let mut map = self.buckets.lock().unwrap();
|
||||
if map.len() >= self.max_keys && !map.contains_key(key) {
|
||||
// a full table is cleaned of buckets that have refilled completely (they carry no information)
|
||||
let full = self.rate.burst;
|
||||
let refill = self.rate.refill;
|
||||
map.retain(|_, b| b.tokens + (now - b.at).max(0.0) * refill < full);
|
||||
if map.len() >= self.max_keys {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let b = map.entry(key.clone()).or_insert(Bucket { tokens: self.rate.burst, at: now });
|
||||
b.tokens = (b.tokens + (now - b.at).max(0.0) * self.rate.refill).min(self.rate.burst);
|
||||
b.at = now;
|
||||
if b.tokens >= cost {
|
||||
b.tokens -= cost;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_bucket_allows_a_burst_then_refills() {
|
||||
let l: Limiter<&str> = Limiter::new(Rate { burst: 3.0, refill: 0.5 }, 100);
|
||||
assert!(l.take(&"a", 1.0, 0.0) && l.take(&"a", 1.0, 0.0) && l.take(&"a", 1.0, 0.0));
|
||||
assert!(!l.take(&"a", 1.0, 0.0), "the burst is used up");
|
||||
assert!(l.take(&"b", 1.0, 0.0), "another key is unaffected");
|
||||
assert!(!l.take(&"a", 1.0, 1.0), "half a token is not enough");
|
||||
assert!(l.take(&"a", 1.0, 2.0), "one token after two seconds");
|
||||
assert!(l.take(&"a", 3.0, 1000.0), "a long pause refills to the burst");
|
||||
assert!(!l.take(&"a", 1.0, 1000.0), "and never beyond it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_table_is_bounded() {
|
||||
let l: Limiter<u32> = Limiter::new(Rate { burst: 1.0, refill: 0.0 }, 4);
|
||||
for k in 0..4 {
|
||||
assert!(l.take(&k, 1.0, 0.0));
|
||||
}
|
||||
assert!(!l.take(&99, 1.0, 0.0), "a new key is refused when the table is full of live buckets");
|
||||
assert!(!l.take(&0, 1.0, 0.0), "an existing one keeps its state");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
//! A TCP listener that never has more than a fixed number of connections open: a new one waits in the kernel backlog until a slot is free.
|
||||
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||
|
||||
pub struct LimitedListener {
|
||||
inner: TcpListener,
|
||||
slots: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl LimitedListener {
|
||||
pub fn new(inner: TcpListener, max_connections: usize) -> Self {
|
||||
Self { inner, slots: Arc::new(Semaphore::new(max_connections)) }
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
pub struct LimitedIo {
|
||||
#[pin]
|
||||
stream: TcpStream,
|
||||
_slot: OwnedSemaphorePermit,
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for LimitedIo {
|
||||
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
|
||||
self.project().stream.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for LimitedIo {
|
||||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
|
||||
self.project().stream.poll_write(cx, buf)
|
||||
}
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
self.project().stream.poll_flush(cx)
|
||||
}
|
||||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
self.project().stream.poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl axum::serve::Listener for LimitedListener {
|
||||
type Io = LimitedIo;
|
||||
type Addr = SocketAddr;
|
||||
|
||||
async fn accept(&mut self) -> (Self::Io, Self::Addr) {
|
||||
loop {
|
||||
let slot = self.slots.clone().acquire_owned().await.expect("the semaphore is never closed");
|
||||
match self.inner.accept().await {
|
||||
Ok((stream, addr)) => return (LimitedIo { stream, _slot: slot }, addr),
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "accept failed");
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn local_addr(&self) -> io::Result<Self::Addr> {
|
||||
self.inner.local_addr()
|
||||
}
|
||||
}
|
||||
|
||||
/// The address of the other end of a connection, whichever listener accepted it.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Peer(pub SocketAddr);
|
||||
|
||||
impl axum::extract::connect_info::Connected<axum::serve::IncomingStream<'_, LimitedListener>> for Peer {
|
||||
fn connect_info(stream: axum::serve::IncomingStream<'_, LimitedListener>) -> Self {
|
||||
Peer(*stream.remote_addr())
|
||||
}
|
||||
}
|
||||
|
||||
impl axum::extract::connect_info::Connected<axum::serve::IncomingStream<'_, TcpListener>> for Peer {
|
||||
fn connect_info(stream: axum::serve::IncomingStream<'_, TcpListener>) -> Self {
|
||||
Peer(*stream.remote_addr())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use coa_registry::api::{router, AppState, Clock};
|
||||
use coa_registry::config::Config;
|
||||
use coa_registry::listener::LimitedListener;
|
||||
use coa_registry::pg::PgStore;
|
||||
use tower::limit::ConcurrencyLimitLayer;
|
||||
use tower_http::timeout::TimeoutLayer;
|
||||
|
||||
fn clock() -> Clock {
|
||||
Arc::new(|| std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0))
|
||||
}
|
||||
|
||||
async fn shutdown() {
|
||||
let ctrl_c = tokio::signal::ctrl_c();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("a SIGTERM handler");
|
||||
tokio::select! { _ = ctrl_c => {}, _ = term.recv() => {} }
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = ctrl_c.await;
|
||||
}
|
||||
}
|
||||
|
||||
/// `coa-registry --healthcheck`: ask the local server for its health and exit 0 or 1 (the image has no curl).
|
||||
fn healthcheck() -> i32 {
|
||||
use std::io::{Read, Write};
|
||||
let addr = std::env::var("REGISTRY_LISTEN").unwrap_or_else(|_| "0.0.0.0:8080".into());
|
||||
let port = addr.rsplit(':').next().unwrap_or("8080");
|
||||
let Ok(mut s) = std::net::TcpStream::connect(format!("127.0.0.1:{port}")) else { return 1 };
|
||||
let _ = s.set_read_timeout(Some(Duration::from_secs(3)));
|
||||
if s.write_all(b"GET /registry/v1/healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").is_err() {
|
||||
return 1;
|
||||
}
|
||||
let mut out = String::new();
|
||||
let _ = s.read_to_string(&mut out);
|
||||
i32::from(!out.starts_with("HTTP/1.1 200"))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
if std::env::args().nth(1).as_deref() == Some("--healthcheck") {
|
||||
std::process::exit(healthcheck());
|
||||
}
|
||||
tracing_subscriber::fmt().json().with_env_filter(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into())).with_current_span(false).init();
|
||||
let cfg = match Config::from_env() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "configuration");
|
||||
std::process::exit(2);
|
||||
}
|
||||
};
|
||||
let store = match PgStore::connect(&cfg.pg).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "database pool");
|
||||
std::process::exit(2);
|
||||
}
|
||||
};
|
||||
// the database may still be starting: wait for it instead of crashing in a loop
|
||||
let mut waited = 0;
|
||||
loop {
|
||||
match store.migrate().await {
|
||||
Ok(()) => break,
|
||||
Err(e) if e.contains("refusing to start") || e.starts_with("migration ") => {
|
||||
tracing::error!(error = %e, "migration");
|
||||
std::process::exit(3);
|
||||
}
|
||||
Err(e) => {
|
||||
waited += 1;
|
||||
if waited > 60 {
|
||||
tracing::error!(error = %e, "the database never became ready");
|
||||
std::process::exit(3);
|
||||
}
|
||||
tracing::warn!(error = %e, "waiting for the database");
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
let state = Arc::new(AppState::new(store, clock(), cfg.api.clone()));
|
||||
let app = router(state).layer(ConcurrencyLimitLayer::new(cfg.max_in_flight)).layer(TimeoutLayer::with_status_code(axum::http::StatusCode::REQUEST_TIMEOUT, Duration::from_secs(cfg.request_timeout_secs)));
|
||||
let tcp = match tokio::net::TcpListener::bind(&cfg.listen).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, listen = %cfg.listen, "bind");
|
||||
std::process::exit(2);
|
||||
}
|
||||
};
|
||||
tracing::info!(listen = %cfg.listen, "registry started");
|
||||
let listener = LimitedListener::new(tcp, cfg.max_connections);
|
||||
let serve = axum::serve(listener, app.into_make_service_with_connect_info::<coa_registry::listener::Peer>()).with_graceful_shutdown(shutdown());
|
||||
if let Err(e) = serve.await {
|
||||
tracing::error!(error = %e, "server");
|
||||
std::process::exit(1);
|
||||
}
|
||||
tracing::info!("registry stopped");
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
//! PostgreSQL storage and forward-only migrations.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset};
|
||||
use coa_registry_proto::{HeartbeatRequest, RealmId, RegisterRequest};
|
||||
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod, Runtime};
|
||||
use tokio_postgres::{NoTls, Row};
|
||||
|
||||
use crate::store::*;
|
||||
|
||||
const MIGRATIONS: &[(i32, &str, &str)] = &[(1, "realms", include_str!("../migrations/0001_realms.sql"))];
|
||||
|
||||
pub struct PgConfig {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
pub dbname: String,
|
||||
pub user: String,
|
||||
pub password: String,
|
||||
pub pool_size: usize,
|
||||
pub statement_timeout_ms: u64,
|
||||
}
|
||||
|
||||
pub struct PgStore {
|
||||
pool: Pool,
|
||||
}
|
||||
|
||||
fn backend(e: impl std::fmt::Display) -> StoreError {
|
||||
StoreError::Backend(e.to_string())
|
||||
}
|
||||
|
||||
impl PgStore {
|
||||
pub async fn connect(cfg: &PgConfig) -> Result<Self, String> {
|
||||
let mut pg = tokio_postgres::Config::new();
|
||||
pg.host(&cfg.host).port(cfg.port).dbname(&cfg.dbname).user(&cfg.user).password(&cfg.password);
|
||||
pg.connect_timeout(Duration::from_secs(5));
|
||||
pg.application_name("coa-registry");
|
||||
pg.options(&format!("-c statement_timeout={} -c idle_in_transaction_session_timeout=10000 -c lock_timeout=3000", cfg.statement_timeout_ms));
|
||||
let manager = Manager::from_config(pg, NoTls, ManagerConfig { recycling_method: RecyclingMethod::Fast });
|
||||
let pool = Pool::builder(manager)
|
||||
.max_size(cfg.pool_size)
|
||||
.wait_timeout(Some(Duration::from_secs(5)))
|
||||
.create_timeout(Some(Duration::from_secs(5)))
|
||||
.runtime(Runtime::Tokio1)
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(Self { pool })
|
||||
}
|
||||
|
||||
async fn conn(&self) -> StoreResult<Object> {
|
||||
self.pool.get().await.map_err(backend)
|
||||
}
|
||||
|
||||
/// Apply every migration the database has not seen. A database that is ahead of this binary is refused: migrations only go forward.
|
||||
pub async fn migrate(&self) -> Result<(), String> {
|
||||
let mut c = self.pool.get().await.map_err(|e| e.to_string())?;
|
||||
c.batch_execute("SELECT pg_advisory_lock(7300100)").await.map_err(|e| e.to_string())?;
|
||||
let result = Self::migrate_locked(&mut c).await;
|
||||
let _ = c.batch_execute("SELECT pg_advisory_unlock(7300100)").await;
|
||||
result
|
||||
}
|
||||
|
||||
async fn migrate_locked(c: &mut Object) -> Result<(), String> {
|
||||
c.batch_execute("CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY, name text NOT NULL, applied_at timestamptz NOT NULL DEFAULT now())").await.map_err(|e| e.to_string())?;
|
||||
let applied: Vec<i32> = c.query("SELECT version FROM schema_migrations ORDER BY version", &[]).await.map_err(|e| e.to_string())?.iter().map(|r| r.get(0)).collect();
|
||||
let newest = MIGRATIONS.last().map(|m| m.0).unwrap_or(0);
|
||||
if let Some(ahead) = applied.iter().find(|v| **v > newest) {
|
||||
return Err(format!("the database is at migration {ahead}, this binary knows up to {newest}: refusing to start"));
|
||||
}
|
||||
for (version, name, sql) in MIGRATIONS {
|
||||
if applied.contains(version) {
|
||||
continue;
|
||||
}
|
||||
let tx = c.transaction().await.map_err(|e| e.to_string())?;
|
||||
tx.batch_execute(sql).await.map_err(|e| format!("migration {version} {name}: {e}"))?;
|
||||
tx.execute("INSERT INTO schema_migrations(version, name) VALUES ($1, $2)", &[version, name]).await.map_err(|e| e.to_string())?;
|
||||
tx.commit().await.map_err(|e| e.to_string())?;
|
||||
tracing::info!(version, name, "migration applied");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const COLUMNS: &str = "realm_id::text, public_key, EXTRACT(EPOCH FROM created_at)::bigint, EXTRACT(EPOCH FROM updated_at)::bigint, EXTRACT(EPOCH FROM last_seen_at)::bigint, published, display_name, description, language, ruleset, manager_version, capabilities, capabilities_hash, metadata_revision, last_request_ts, player_count, player_capacity";
|
||||
|
||||
fn to_row(r: &Row) -> StoreResult<RealmRow> {
|
||||
let id: String = r.get(0);
|
||||
let key: Vec<u8> = r.get(1);
|
||||
let ruleset: String = r.get(9);
|
||||
let caps: serde_json::Value = r.get(11);
|
||||
let revision: i64 = r.get(13);
|
||||
let count: Option<i32> = r.get(15);
|
||||
let capacity: Option<i32> = r.get(16);
|
||||
Ok(RealmRow {
|
||||
realm_id: RealmId::parse(&id).map_err(backend)?,
|
||||
public_key: key.try_into().map_err(|_| StoreError::Backend("a stored key is not 32 bytes".into()))?,
|
||||
created_at: r.get(2),
|
||||
updated_at: r.get(3),
|
||||
last_seen_at: r.get(4),
|
||||
published: r.get(5),
|
||||
display_name: r.get(6),
|
||||
description: r.get(7),
|
||||
language: r.get(8),
|
||||
ruleset: match ruleset.as_str() {
|
||||
"coa" => Ruleset::Coa,
|
||||
"wildcard" => Ruleset::Wildcard,
|
||||
other => return Err(StoreError::Backend(format!("a stored ruleset is unknown: {other}"))),
|
||||
},
|
||||
manager_version: r.get(10),
|
||||
capabilities: serde_json::from_value::<AdvertisedCapabilities>(caps).map_err(backend)?,
|
||||
capabilities_hash: r.get(12),
|
||||
metadata_revision: revision as u64,
|
||||
last_request_ts: r.get(14),
|
||||
player_count: count.map(|n| n as u32),
|
||||
player_capacity: capacity.map(|n| n as u32),
|
||||
})
|
||||
}
|
||||
|
||||
fn caps_json(c: &AdvertisedCapabilities) -> StoreResult<serde_json::Value> {
|
||||
serde_json::to_value(c).map_err(backend)
|
||||
}
|
||||
|
||||
impl Store for PgStore {
|
||||
async fn ping(&self) -> bool {
|
||||
match self.pool.get().await {
|
||||
Ok(c) => c.query_one("SELECT 1", &[]).await.is_ok(),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
async fn key_of(&self, id: &RealmId) -> StoreResult<Option<[u8; 32]>> {
|
||||
let c = self.conn().await?;
|
||||
let row = c.query_opt("SELECT public_key FROM realms WHERE realm_id = $1::text::uuid", &[&id.to_string()]).await.map_err(backend)?;
|
||||
row.map(|r| r.get::<_, Vec<u8>>(0).try_into().map_err(|_| StoreError::Backend("a stored key is not 32 bytes".into()))).transpose()
|
||||
}
|
||||
|
||||
async fn get(&self, id: &RealmId) -> StoreResult<Option<RealmRow>> {
|
||||
let c = self.conn().await?;
|
||||
let row = c.query_opt(&format!("SELECT {COLUMNS} FROM realms WHERE realm_id = $1::text::uuid"), &[&id.to_string()]).await.map_err(backend)?;
|
||||
row.as_ref().map(to_row).transpose()
|
||||
}
|
||||
|
||||
async fn register(&self, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> StoreResult<(RealmRow, bool)> {
|
||||
let mut c = self.conn().await?;
|
||||
let id = req.realm_id.to_string();
|
||||
for _ in 0..3 {
|
||||
let tx = c.transaction().await.map_err(backend)?;
|
||||
let existing = tx.query_opt(&format!("SELECT {COLUMNS} FROM realms WHERE realm_id = $1::text::uuid FOR UPDATE"), &[&id]).await.map_err(backend)?;
|
||||
let existing = existing.as_ref().map(to_row).transpose()?;
|
||||
let (row, created) = plan_register(existing.as_ref(), req, key, now, ts)?;
|
||||
let caps = caps_json(&row.capabilities)?;
|
||||
let ruleset = row.ruleset.as_str();
|
||||
let players = (row.player_count.map(|n| n as i32), row.player_capacity.map(|n| n as i32));
|
||||
if created {
|
||||
let n = tx
|
||||
.execute(
|
||||
"INSERT INTO realms (realm_id, public_key, created_at, updated_at, last_seen_at, published, display_name, description, language, ruleset, manager_version, capabilities, capabilities_hash, metadata_revision, last_request_ts, player_count, player_capacity) \
|
||||
VALUES ($1::text::uuid, $2, to_timestamp($3::bigint), to_timestamp($3::bigint), to_timestamp($3::bigint), true, $4, $5, $6, $7, $8, $9, $10, 1, $11, $12, $13) ON CONFLICT (realm_id) DO NOTHING",
|
||||
&[&id, &row.public_key.as_slice(), &now, &row.display_name, &row.description, &row.language, &ruleset, &row.manager_version, &caps, &row.capabilities_hash, &ts, &players.0, &players.1],
|
||||
)
|
||||
.await
|
||||
.map_err(backend)?;
|
||||
if n == 0 {
|
||||
tx.rollback().await.map_err(backend)?;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
tx.execute(
|
||||
"UPDATE realms SET updated_at = to_timestamp($2::bigint), last_seen_at = to_timestamp($3::bigint), published = true, display_name = $4, description = $5, language = $6, ruleset = $7, manager_version = $8, capabilities = $9, capabilities_hash = $10, metadata_revision = $11, last_request_ts = $12, player_count = $13, player_capacity = $14 WHERE realm_id = $1::text::uuid",
|
||||
&[&id, &row.updated_at, &now, &row.display_name, &row.description, &row.language, &ruleset, &row.manager_version, &caps, &row.capabilities_hash, &(row.metadata_revision as i64), &ts, &players.0, &players.1],
|
||||
)
|
||||
.await
|
||||
.map_err(backend)?;
|
||||
}
|
||||
tx.commit().await.map_err(backend)?;
|
||||
return Ok((row, created));
|
||||
}
|
||||
Err(StoreError::Backend("the registration kept racing another one".into()))
|
||||
}
|
||||
|
||||
async fn heartbeat(&self, id: &RealmId, hb: &HeartbeatRequest, now: i64, ts: i64) -> StoreResult<HeartbeatOutcome> {
|
||||
let mut c = self.conn().await?;
|
||||
let text = id.to_string();
|
||||
let tx = c.transaction().await.map_err(backend)?;
|
||||
let r = tx
|
||||
.query_opt("SELECT published, last_request_ts, capabilities_hash, display_name, description, language, manager_version, metadata_revision FROM realms WHERE realm_id = $1::text::uuid FOR UPDATE", &[&text])
|
||||
.await
|
||||
.map_err(backend)?
|
||||
.ok_or(StoreError::Unknown)?;
|
||||
let revision: i64 = r.get(7);
|
||||
let view = HeartbeatView { published: r.get(0), last_request_ts: r.get(1), capabilities_hash: r.get(2), display_name: r.get(3), description: r.get(4), language: r.get(5), manager_version: r.get(6), metadata_revision: revision as u64 };
|
||||
let plan = plan_heartbeat(&view, hb, ts)?;
|
||||
let players = (hb.player_count.map(|n| n as i32), hb.player_capacity.map(|n| n as i32));
|
||||
tx.execute(
|
||||
"UPDATE realms SET last_seen_at = to_timestamp($2::bigint), last_request_ts = $3, player_count = $4, player_capacity = $5, display_name = $6, description = $7, language = $8, manager_version = $9, capabilities_hash = $10, metadata_revision = $11, updated_at = CASE WHEN $12 THEN to_timestamp($2::bigint) ELSE updated_at END WHERE realm_id = $1::text::uuid",
|
||||
&[&text, &now, &ts, &players.0, &players.1, &plan.display_name, &plan.description, &plan.language, &plan.manager_version, &plan.capabilities_hash, &(plan.metadata_revision as i64), &plan.metadata_changed],
|
||||
)
|
||||
.await
|
||||
.map_err(backend)?;
|
||||
if let Some(caps) = &plan.capabilities {
|
||||
let json = caps_json(caps)?;
|
||||
tx.execute("UPDATE realms SET capabilities = $2, ruleset = $3 WHERE realm_id = $1::text::uuid", &[&text, &json, &caps.content.ruleset.as_str()]).await.map_err(backend)?;
|
||||
}
|
||||
tx.commit().await.map_err(backend)?;
|
||||
Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, capabilities_hash: plan.capabilities_hash, resend_capabilities: plan.resend_capabilities })
|
||||
}
|
||||
|
||||
async fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> StoreResult<u64> {
|
||||
let mut c = self.conn().await?;
|
||||
let text = id.to_string();
|
||||
let tx = c.transaction().await.map_err(backend)?;
|
||||
let r = tx.query_opt("SELECT published, last_request_ts, metadata_revision FROM realms WHERE realm_id = $1::text::uuid FOR UPDATE", &[&text]).await.map_err(backend)?.ok_or(StoreError::Unknown)?;
|
||||
let (published, last, revision): (bool, i64, i64) = (r.get(0), r.get(1), r.get(2));
|
||||
if ts <= last {
|
||||
return Err(StoreError::Stale);
|
||||
}
|
||||
let revision = revision as u64 + u64::from(published);
|
||||
tx.execute(
|
||||
"UPDATE realms SET published = false, metadata_revision = $2, last_request_ts = $3, updated_at = CASE WHEN $4 THEN to_timestamp($5::bigint) ELSE updated_at END WHERE realm_id = $1::text::uuid",
|
||||
&[&text, &(revision as i64), &ts, &published, &now],
|
||||
)
|
||||
.await
|
||||
.map_err(backend)?;
|
||||
tx.commit().await.map_err(backend)?;
|
||||
Ok(revision)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
//! What the Registry keeps, and the rules for changing it. The rules are pure functions shared by the in-memory store (tests) and PostgreSQL,
|
||||
//! so both behave identically; the stores only load and save.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset};
|
||||
use coa_registry_proto::{HeartbeatRequest, RealmId, RegisterRequest};
|
||||
|
||||
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
|
||||
pub enum StoreError {
|
||||
#[error("the realm is not known")]
|
||||
Unknown,
|
||||
#[error("another key owns this realm id")]
|
||||
KeyMismatch,
|
||||
#[error("the request is not newer than the last one applied")]
|
||||
Stale,
|
||||
#[error("the realm is not published")]
|
||||
NotPublished,
|
||||
#[error("{0}")]
|
||||
Backend(String),
|
||||
}
|
||||
|
||||
pub type StoreResult<T> = Result<T, StoreError>;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct RealmRow {
|
||||
pub realm_id: RealmId,
|
||||
pub public_key: [u8; 32],
|
||||
pub created_at: i64,
|
||||
pub updated_at: i64,
|
||||
pub last_seen_at: i64,
|
||||
pub published: bool,
|
||||
pub display_name: String,
|
||||
pub description: String,
|
||||
pub language: String,
|
||||
pub ruleset: Ruleset,
|
||||
pub manager_version: String,
|
||||
pub capabilities: AdvertisedCapabilities,
|
||||
pub capabilities_hash: String,
|
||||
pub metadata_revision: u64,
|
||||
pub last_request_ts: i64,
|
||||
pub player_count: Option<u32>,
|
||||
pub player_capacity: Option<u32>,
|
||||
}
|
||||
|
||||
/// The light part of a row a heartbeat decides on (the capabilities JSON stays in the database unless it changes).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HeartbeatView {
|
||||
pub published: bool,
|
||||
pub last_request_ts: i64,
|
||||
pub capabilities_hash: String,
|
||||
pub display_name: String,
|
||||
pub description: String,
|
||||
pub language: String,
|
||||
pub manager_version: String,
|
||||
pub metadata_revision: u64,
|
||||
}
|
||||
|
||||
impl From<&RealmRow> for HeartbeatView {
|
||||
fn from(r: &RealmRow) -> Self {
|
||||
Self {
|
||||
published: r.published,
|
||||
last_request_ts: r.last_request_ts,
|
||||
capabilities_hash: r.capabilities_hash.clone(),
|
||||
display_name: r.display_name.clone(),
|
||||
description: r.description.clone(),
|
||||
language: r.language.clone(),
|
||||
manager_version: r.manager_version.clone(),
|
||||
metadata_revision: r.metadata_revision,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct HeartbeatPlan {
|
||||
pub display_name: String,
|
||||
pub description: String,
|
||||
pub language: String,
|
||||
pub manager_version: String,
|
||||
/// Set only when the hash changed: the only case in which the large JSON is written.
|
||||
pub capabilities: Option<AdvertisedCapabilities>,
|
||||
pub capabilities_hash: String,
|
||||
pub metadata_revision: u64,
|
||||
pub metadata_changed: bool,
|
||||
pub resend_capabilities: bool,
|
||||
}
|
||||
|
||||
pub fn plan_heartbeat(view: &HeartbeatView, hb: &HeartbeatRequest, ts: i64) -> StoreResult<HeartbeatPlan> {
|
||||
if ts <= view.last_request_ts {
|
||||
return Err(StoreError::Stale);
|
||||
}
|
||||
if !view.published {
|
||||
return Err(StoreError::NotPublished);
|
||||
}
|
||||
let mut plan = HeartbeatPlan {
|
||||
display_name: hb.display_name.clone().unwrap_or_else(|| view.display_name.clone()),
|
||||
description: hb.description.clone().unwrap_or_else(|| view.description.clone()),
|
||||
language: hb.language.clone().unwrap_or_else(|| view.language.clone()),
|
||||
manager_version: hb.manager_version.clone().unwrap_or_else(|| view.manager_version.clone()),
|
||||
capabilities: None,
|
||||
capabilities_hash: view.capabilities_hash.clone(),
|
||||
metadata_revision: view.metadata_revision,
|
||||
metadata_changed: false,
|
||||
resend_capabilities: false,
|
||||
};
|
||||
plan.metadata_changed = plan.display_name != view.display_name || plan.description != view.description || plan.language != view.language || plan.manager_version != view.manager_version;
|
||||
match &hb.capabilities {
|
||||
Some(c) if hb.capabilities_hash != view.capabilities_hash => {
|
||||
plan.capabilities = Some(c.clone());
|
||||
plan.capabilities_hash = hb.capabilities_hash.clone();
|
||||
plan.metadata_changed = true;
|
||||
}
|
||||
None if hb.capabilities_hash != view.capabilities_hash => plan.resend_capabilities = true,
|
||||
_ => {}
|
||||
}
|
||||
if plan.metadata_changed {
|
||||
plan.metadata_revision += 1;
|
||||
}
|
||||
Ok(plan)
|
||||
}
|
||||
|
||||
/// The new row for a registration: a first one, or the same key announcing itself again (a restart, a changed name, a republication).
|
||||
pub fn plan_register(existing: Option<&RealmRow>, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> StoreResult<(RealmRow, bool)> {
|
||||
let mut row = RealmRow {
|
||||
realm_id: req.realm_id,
|
||||
public_key: key,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
last_seen_at: now,
|
||||
published: true,
|
||||
display_name: req.display_name.clone(),
|
||||
description: req.description.clone(),
|
||||
language: req.language.clone(),
|
||||
ruleset: req.ruleset,
|
||||
manager_version: req.manager_version.clone(),
|
||||
capabilities: req.capabilities.clone(),
|
||||
capabilities_hash: req.capabilities_hash.clone(),
|
||||
metadata_revision: 1,
|
||||
last_request_ts: ts,
|
||||
player_count: req.player_count,
|
||||
player_capacity: req.player_capacity,
|
||||
};
|
||||
let Some(old) = existing else { return Ok((row, true)) };
|
||||
if old.public_key != key {
|
||||
return Err(StoreError::KeyMismatch);
|
||||
}
|
||||
if ts <= old.last_request_ts {
|
||||
return Err(StoreError::Stale);
|
||||
}
|
||||
row.created_at = old.created_at;
|
||||
let changed = !old.published
|
||||
|| old.display_name != row.display_name
|
||||
|| old.description != row.description
|
||||
|| old.language != row.language
|
||||
|| old.ruleset != row.ruleset
|
||||
|| old.manager_version != row.manager_version
|
||||
|| old.capabilities_hash != row.capabilities_hash;
|
||||
row.metadata_revision = old.metadata_revision + u64::from(changed);
|
||||
row.updated_at = if changed { now } else { old.updated_at };
|
||||
Ok((row, false))
|
||||
}
|
||||
|
||||
pub trait Store: Send + Sync + 'static {
|
||||
fn ping(&self) -> impl Future<Output = bool> + Send;
|
||||
/// The key a realm id is bound to, if it is registered.
|
||||
fn key_of(&self, id: &RealmId) -> impl Future<Output = StoreResult<Option<[u8; 32]>>> + Send;
|
||||
fn get(&self, id: &RealmId) -> impl Future<Output = StoreResult<Option<RealmRow>>> + Send;
|
||||
/// `Ok((row, created))`
|
||||
fn register(&self, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> impl Future<Output = StoreResult<(RealmRow, bool)>> + Send;
|
||||
fn heartbeat(&self, id: &RealmId, hb: &HeartbeatRequest, now: i64, ts: i64) -> impl Future<Output = StoreResult<HeartbeatOutcome>> + Send;
|
||||
/// The metadata revision after the change.
|
||||
fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> impl Future<Output = StoreResult<u64>> + Send;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HeartbeatOutcome {
|
||||
pub metadata_revision: u64,
|
||||
pub capabilities_hash: String,
|
||||
pub resend_capabilities: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MemoryStore {
|
||||
rows: Mutex<HashMap<RealmId, RealmRow>>,
|
||||
}
|
||||
|
||||
impl MemoryStore {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn get_for_test(&self, id: &RealmId) -> Option<RealmRow> {
|
||||
self.rows.lock().unwrap().get(id).cloned()
|
||||
}
|
||||
|
||||
/// Forget everything (tests: a Registry that lost its data).
|
||||
#[doc(hidden)]
|
||||
pub fn forget_all(&self) {
|
||||
self.rows.lock().unwrap().clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// A store shared between a test and the server it runs.
|
||||
impl<T: Store> Store for std::sync::Arc<T> {
|
||||
fn ping(&self) -> impl Future<Output = bool> + Send {
|
||||
(**self).ping()
|
||||
}
|
||||
fn key_of(&self, id: &RealmId) -> impl Future<Output = StoreResult<Option<[u8; 32]>>> + Send {
|
||||
(**self).key_of(id)
|
||||
}
|
||||
fn get(&self, id: &RealmId) -> impl Future<Output = StoreResult<Option<RealmRow>>> + Send {
|
||||
(**self).get(id)
|
||||
}
|
||||
fn register(&self, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> impl Future<Output = StoreResult<(RealmRow, bool)>> + Send {
|
||||
(**self).register(req, key, now, ts)
|
||||
}
|
||||
fn heartbeat(&self, id: &RealmId, hb: &HeartbeatRequest, now: i64, ts: i64) -> impl Future<Output = StoreResult<HeartbeatOutcome>> + Send {
|
||||
(**self).heartbeat(id, hb, now, ts)
|
||||
}
|
||||
fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> impl Future<Output = StoreResult<u64>> + Send {
|
||||
(**self).unpublish(id, now, ts)
|
||||
}
|
||||
}
|
||||
|
||||
impl Store for MemoryStore {
|
||||
async fn ping(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
async fn key_of(&self, id: &RealmId) -> StoreResult<Option<[u8; 32]>> {
|
||||
Ok(self.rows.lock().unwrap().get(id).map(|r| r.public_key))
|
||||
}
|
||||
|
||||
async fn get(&self, id: &RealmId) -> StoreResult<Option<RealmRow>> {
|
||||
Ok(self.rows.lock().unwrap().get(id).cloned())
|
||||
}
|
||||
|
||||
async fn register(&self, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> StoreResult<(RealmRow, bool)> {
|
||||
let mut rows = self.rows.lock().unwrap();
|
||||
let (row, created) = plan_register(rows.get(&req.realm_id), req, key, now, ts)?;
|
||||
rows.insert(req.realm_id, row.clone());
|
||||
Ok((row, created))
|
||||
}
|
||||
|
||||
async fn heartbeat(&self, id: &RealmId, hb: &HeartbeatRequest, now: i64, ts: i64) -> StoreResult<HeartbeatOutcome> {
|
||||
let mut rows = self.rows.lock().unwrap();
|
||||
let row = rows.get_mut(id).ok_or(StoreError::Unknown)?;
|
||||
let plan = plan_heartbeat(&HeartbeatView::from(&*row), hb, ts)?;
|
||||
row.display_name = plan.display_name;
|
||||
row.description = plan.description;
|
||||
row.language = plan.language;
|
||||
row.manager_version = plan.manager_version;
|
||||
if let Some(c) = plan.capabilities {
|
||||
row.ruleset = c.content.ruleset;
|
||||
row.capabilities = c;
|
||||
}
|
||||
row.capabilities_hash = plan.capabilities_hash.clone();
|
||||
if plan.metadata_changed {
|
||||
row.updated_at = now;
|
||||
}
|
||||
row.metadata_revision = plan.metadata_revision;
|
||||
row.last_seen_at = now;
|
||||
row.last_request_ts = ts;
|
||||
row.player_count = hb.player_count;
|
||||
row.player_capacity = hb.player_capacity;
|
||||
Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, capabilities_hash: plan.capabilities_hash, resend_capabilities: plan.resend_capabilities })
|
||||
}
|
||||
|
||||
async fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> StoreResult<u64> {
|
||||
let mut rows = self.rows.lock().unwrap();
|
||||
let row = rows.get_mut(id).ok_or(StoreError::Unknown)?;
|
||||
if ts <= row.last_request_ts {
|
||||
return Err(StoreError::Stale);
|
||||
}
|
||||
if row.published {
|
||||
row.published = false;
|
||||
row.metadata_revision += 1;
|
||||
row.updated_at = now;
|
||||
}
|
||||
row.last_request_ts = ts;
|
||||
Ok(row.metadata_revision)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
//! The protocol and its refusals, against a real HTTP server. The same checks run on the in-memory store and, when `REGISTRY_TEST_PG` is set, on PostgreSQL.
|
||||
|
||||
mod common;
|
||||
|
||||
use coa_registry::api::ApiConfig;
|
||||
use coa_registry::limits::Rate;
|
||||
use coa_registry::store::MemoryStore;
|
||||
use coa_registry_proto::sign::{sign_request, HEADER_SIGNATURE, HEADER_TIMESTAMP, HEADER_VERSION};
|
||||
use coa_registry_proto::*;
|
||||
use common::*;
|
||||
use ed25519_dalek::SigningKey;
|
||||
use serde_json::json;
|
||||
|
||||
/// Limits high enough to stay out of the way of the functional checks (the limits have their own test).
|
||||
fn lax() -> ApiConfig {
|
||||
ApiConfig { general: Rate { burst: 100_000.0, refill: 1000.0 }, register: Rate { burst: 100_000.0, refill: 1000.0 }, heartbeat: Rate { burst: 100_000.0, refill: 1000.0 }, ..ApiConfig::default() }
|
||||
}
|
||||
|
||||
fn memory() -> TestServer {
|
||||
spawn(MemoryStore::new(), lax())
|
||||
}
|
||||
|
||||
/// 1, 2, 4: a fresh realm registers, registers again as itself, and a heartbeat updates last_seen.
|
||||
fn lifecycle(s: &TestServer) {
|
||||
let mut h = Host::new(s, 1);
|
||||
let r = h.register("Friends' realm", 60);
|
||||
assert_eq!(r.status, 200, "{:?}", r.body);
|
||||
assert_eq!((r.body["created"].as_bool(), r.body["metadata_revision"].as_u64(), r.body["published"].as_bool()), (Some(true), Some(1), Some(true)));
|
||||
assert_eq!((r.body["heartbeat_interval_secs"].as_u64(), r.body["online_ttl_secs"].as_u64()), (Some(30), Some(120)));
|
||||
|
||||
let me = h.me();
|
||||
assert_eq!(me.status, 200);
|
||||
assert_eq!((me.body["online"].as_bool(), me.body["display_name"].as_str(), me.body["last_seen_at"].as_i64()), (Some(true), Some("Friends' realm"), Some(s.time())));
|
||||
assert_eq!(me.body["capabilities"]["progression"]["max_player_level"], 60);
|
||||
|
||||
s.advance(30);
|
||||
h.ts = s.time();
|
||||
let hb = h.heartbeat(60, false);
|
||||
assert_eq!(hb.status, 200, "{:?}", hb.body);
|
||||
assert_eq!((hb.body["metadata_revision"].as_u64(), hb.body["resend_capabilities"].as_bool()), (Some(1), Some(false)));
|
||||
let me = h.me();
|
||||
assert_eq!((me.body["last_seen_at"].as_i64(), me.body["player_count"].as_u64()), (Some(s.time()), Some(2)), "the heartbeat moved last_seen and the player count");
|
||||
assert_eq!(me.body["metadata_revision"], 1, "a heartbeat of unchanged metadata is not a metadata update");
|
||||
|
||||
// the same key announcing itself again (a Manager restart) keeps the identity; unchanged metadata keeps the revision
|
||||
let again = h.register("Friends' realm", 60);
|
||||
assert_eq!((again.status, again.body["created"].as_bool(), again.body["metadata_revision"].as_u64()), (200, Some(false), Some(1)));
|
||||
let renamed = h.register("Friends' realm II", 60);
|
||||
assert_eq!((renamed.status, renamed.body["metadata_revision"].as_u64()), (200, Some(2)));
|
||||
}
|
||||
|
||||
/// 5: a missing heartbeat makes the realm offline after the TTL; it is not deleted and the next heartbeat brings it back.
|
||||
fn presence(s: &TestServer) {
|
||||
let mut h = Host::new(s, 2);
|
||||
assert_eq!(h.register("Presence", 60).status, 200);
|
||||
s.advance(100);
|
||||
h.ts = s.time();
|
||||
assert_eq!(h.me().body["online"], true, "inside the TTL");
|
||||
s.advance(21);
|
||||
h.ts = s.time();
|
||||
let me = h.me();
|
||||
assert_eq!((me.status, me.body["online"].as_bool(), me.body["published"].as_bool()), (200, Some(false), Some(true)), "offline after the TTL, still there and still published");
|
||||
let hb = h.heartbeat(60, false);
|
||||
assert_eq!(hb.status, 200);
|
||||
assert_eq!(h.me().body["online"], true);
|
||||
}
|
||||
|
||||
/// 6: new capabilities arrive with a heartbeat and move the metadata revision; a changed hash without them asks for them.
|
||||
fn metadata_changes(s: &TestServer) {
|
||||
let mut h = Host::new(s, 3);
|
||||
assert_eq!(h.register("Caps", 60).status, 200);
|
||||
let hb = h.heartbeat(70, false);
|
||||
assert_eq!(hb.status, 200);
|
||||
assert_eq!((hb.body["resend_capabilities"].as_bool(), hb.body["metadata_revision"].as_u64()), (Some(true), Some(1)), "the hash changed but the capabilities were not sent");
|
||||
let me = h.me();
|
||||
assert_eq!(me.body["capabilities"]["progression"]["max_player_level"], 60, "nothing was applied without the capabilities");
|
||||
let hb = h.heartbeat(70, true);
|
||||
assert_eq!((hb.status, hb.body["resend_capabilities"].as_bool(), hb.body["metadata_revision"].as_u64()), (200, Some(false), Some(2)));
|
||||
let me = h.me();
|
||||
assert_eq!((me.body["capabilities"]["progression"]["max_player_level"].as_u64(), me.body["metadata_revision"].as_u64()), (Some(70), Some(2)));
|
||||
let hb = h.heartbeat(70, true);
|
||||
assert_eq!(hb.body["metadata_revision"], 2, "the same capabilities again change nothing");
|
||||
// a heartbeat may also rename
|
||||
let mut body = h.heartbeat_body(70, false);
|
||||
body.display_name = Some("Renamed".into());
|
||||
let path = path_heartbeat(&h.realm);
|
||||
let r = h.send("POST", &path, &serde_json::to_vec(&body).unwrap());
|
||||
assert_eq!((r.status, r.body["metadata_revision"].as_u64()), (200, Some(3)));
|
||||
assert_eq!(h.me().body["display_name"], "Renamed");
|
||||
}
|
||||
|
||||
/// 7: an unrelated key can neither take a realm id over nor speak for it.
|
||||
fn takeover(s: &TestServer) {
|
||||
let mut owner = Host::new(s, 4);
|
||||
assert_eq!(owner.register("Mine", 60).status, 200);
|
||||
let mut thief = Host::new(s, 5);
|
||||
thief.realm = owner.realm;
|
||||
thief.ts = s.time() + 10;
|
||||
let r = thief.register("Stolen", 60);
|
||||
assert_eq!((r.status, r.code().as_str()), (403, "realm_key_mismatch"), "{:?}", r.body);
|
||||
let r = thief.heartbeat(60, true);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"));
|
||||
let r = thief.unpublish();
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"));
|
||||
let r = thief.me();
|
||||
assert_eq!(r.status, 401);
|
||||
let me = owner.me();
|
||||
assert_eq!((me.body["display_name"].as_str(), me.body["published"].as_bool()), (Some("Mine"), Some(true)), "the owner's record is untouched");
|
||||
}
|
||||
|
||||
/// 8, 9, 10: tampering, time, and protocol version.
|
||||
fn authentication(s: &TestServer) {
|
||||
let mut h = Host::new(s, 6);
|
||||
assert_eq!(h.register("Auth", 60).status, 200);
|
||||
let path = path_heartbeat(&h.realm);
|
||||
let body = serde_json::to_vec(&h.heartbeat_body(60, false)).unwrap();
|
||||
|
||||
// a body changed after it was signed
|
||||
h.ts += 1;
|
||||
let headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body);
|
||||
let mut tampered = body.clone();
|
||||
tampered.extend_from_slice(b" ");
|
||||
let r = h.send_with("POST", &path, &tampered, &headers.pairs());
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"));
|
||||
// the method and the path are signed too
|
||||
let r = h.send_with("POST", &path_unpublish(&h.realm), &serde_json::to_vec(&UnpublishRequest { protocol_version: 1 }).unwrap(), &headers.pairs());
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"), "a signature for one endpoint is not valid for another");
|
||||
let r = h.send_with("POST", &path, &body, &headers.pairs());
|
||||
assert_eq!(r.status, 200, "the untampered request is fine: {:?}", r.body);
|
||||
let r = h.send_with("POST", &path, &body, &headers.pairs());
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "timestamp_not_monotonic"), "a captured request cannot be replayed");
|
||||
|
||||
// timestamps outside the window, either way
|
||||
for skew in [-400, 400] {
|
||||
h.ts = s.time() + skew;
|
||||
let r = h.send("POST", &path, &body);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "bad_timestamp"), "skew {skew}");
|
||||
}
|
||||
// a timestamp that is in the window but not later than the last applied one
|
||||
h.ts = s.time() + 100;
|
||||
assert_eq!(h.send("POST", &path, &body).status, 200);
|
||||
h.ts = s.time() + 50;
|
||||
let r = h.send("POST", &path, &body);
|
||||
assert_eq!((r.status, r.code().as_str()), (401, "timestamp_not_monotonic"));
|
||||
h.ts = s.time() + 101;
|
||||
assert_eq!(h.send("POST", &path, &body).status, 200, "later ones are accepted again");
|
||||
|
||||
// protocol version in the header and in the body
|
||||
h.ts += 1;
|
||||
let mut headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body).pairs();
|
||||
headers[0] = (HEADER_VERSION, "2".into());
|
||||
let r = h.send_with("POST", &path, &body, &headers);
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"));
|
||||
let mut v2 = h.heartbeat_body(60, false);
|
||||
v2.protocol_version = 2;
|
||||
let r = h.send("POST", &path, &serde_json::to_vec(&v2).unwrap());
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"));
|
||||
// missing or garbled headers
|
||||
let r = h.send_with("POST", &path, &body, &[]);
|
||||
assert_eq!(r.status, 400);
|
||||
let mut bad = sign_request(&h.key, "POST", &path, &h.realm, h.ts + 1, &body).pairs();
|
||||
bad[3] = (HEADER_SIGNATURE, "not-a-signature".into());
|
||||
assert_eq!(h.send_with("POST", &path, &body, &bad).status, 400);
|
||||
let mut bad = sign_request(&h.key, "POST", &path, &h.realm, h.ts + 1, &body).pairs();
|
||||
bad[2] = (HEADER_TIMESTAMP, "yesterday".into());
|
||||
assert_eq!(h.send_with("POST", &path, &body, &bad).status, 400);
|
||||
}
|
||||
|
||||
/// 11: hostile input is refused safely and stores nothing.
|
||||
fn hostile_input(s: &TestServer) {
|
||||
let mut h = Host::new(s, 7);
|
||||
// oversized
|
||||
let big = vec![b'x'; MAX_REQUEST_BYTES + 1];
|
||||
let r = h.send("POST", PATH_REGISTER, &big);
|
||||
assert_eq!((r.status, r.code().as_str()), (413, "request_too_large"));
|
||||
// not JSON, not an object, deeply nested
|
||||
for body in [b"not json".as_slice(), b"[]", b"null", &b"{\"a\":".repeat(1)] {
|
||||
let r = h.send("POST", PATH_REGISTER, body);
|
||||
assert_eq!(r.status, 400, "{:?}", String::from_utf8_lossy(body));
|
||||
}
|
||||
let nested = format!("{}1{}", "[".repeat(30_000), "]".repeat(30_000));
|
||||
assert_eq!(h.send("POST", PATH_REGISTER, nested.as_bytes()).status, 400);
|
||||
// metadata outside its limits, one at a time
|
||||
let base = serde_json::to_value(h.register_body("Hostile", 60)).unwrap();
|
||||
let cases: Vec<(&str, serde_json::Value)> = vec![
|
||||
("display_name", json!("x".repeat(81))),
|
||||
("display_name", json!("")),
|
||||
("display_name", json!("evil\u{202E}name")),
|
||||
("display_name", json!("new\nline")),
|
||||
("description", json!("d".repeat(1025))),
|
||||
("language", json!("<script>")),
|
||||
("manager_version", json!("1.0 <b>")),
|
||||
("player_count", json!(1_000_000)),
|
||||
("capabilities_hash", json!("0".repeat(64))),
|
||||
("public_key", json!("short")),
|
||||
("ruleset", json!("wildcard")),
|
||||
];
|
||||
for (field, value) in cases {
|
||||
let mut v = base.clone();
|
||||
v[field] = value;
|
||||
let r = h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap());
|
||||
assert!(r.status == 400, "{field}: {} {:?}", r.status, r.body);
|
||||
}
|
||||
// what the protocol has no place for is not accepted, whatever it is
|
||||
for extra in ["character", "snapshot", "password", "ra_password", "db_credentials", "canonical_revision"] {
|
||||
let mut v = base.clone();
|
||||
v[extra] = json!("secret");
|
||||
let r = h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap());
|
||||
assert_eq!((r.status, r.code().as_str()), (400, "malformed_request"), "{extra}");
|
||||
}
|
||||
let mut v = base.clone();
|
||||
v["capabilities"]["content"]["client_catalog"]["../../etc/passwd"] = json!({"sha256": "a".repeat(64), "records": 1});
|
||||
assert_eq!(h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()).status, 400, "capabilities are checked field by field");
|
||||
let mut v = base.clone();
|
||||
v["capabilities"]["notes"] = json!("free text to store");
|
||||
assert_eq!(h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()).status, 400, "no free-form storage in the capabilities");
|
||||
// ids and paths
|
||||
let mut other = Host::new(s, 7);
|
||||
other.ts = h.ts;
|
||||
let r = h.send("GET", "/registry/v1/realms/not-a-uuid", b"");
|
||||
assert_eq!(r.status, 400);
|
||||
let path = format!("/registry/v1/realms/{}", other.realm.to_string().to_uppercase());
|
||||
assert_eq!(h.send("GET", &path, b"").status, 400, "upper case is not the canonical spelling");
|
||||
assert_eq!(h.send("GET", &format!("{}?x=1", path_self(&h.realm)), b"").status, 400, "no query on a signed endpoint");
|
||||
let r = h.send("GET", &path_self(&other.realm), b"");
|
||||
assert_eq!(r.status, 400, "the realm header names another realm than the path");
|
||||
let r = h.send("GET", "/registry/v1/realms/018f2d9e-5c3a-4b21-8c4d-0e5f6a7b8c9d", b"");
|
||||
assert_eq!(r.status, 400, "a version-4 UUID is not a realm id");
|
||||
// nothing was stored by any of it
|
||||
let mut clean = Host::new(s, 7);
|
||||
clean.realm = h.realm;
|
||||
clean.ts = h.ts + 100;
|
||||
assert_eq!(clean.me().status, 404, "the hostile registrations left no realm behind");
|
||||
}
|
||||
|
||||
/// 12: an unpublished realm stops being published and cannot heartbeat; registering again publishes it.
|
||||
fn unpublishing(s: &TestServer) {
|
||||
let mut h = Host::new(s, 8);
|
||||
assert_eq!(h.register("Brief", 60).status, 200);
|
||||
let r = h.unpublish();
|
||||
assert_eq!((r.status, r.body["published"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(false), Some(2)));
|
||||
let me = h.me();
|
||||
assert_eq!((me.body["published"].as_bool(), me.body["online"].as_bool()), (Some(false), Some(false)), "stopped, and not online even though it was just heard of");
|
||||
let hb = h.heartbeat(60, false);
|
||||
assert_eq!((hb.status, hb.code().as_str()), (409, "not_published"));
|
||||
let r = h.unpublish();
|
||||
assert_eq!((r.status, r.body["metadata_revision"].as_u64()), (200, Some(2)), "unpublishing twice changes nothing");
|
||||
let r = h.register("Brief", 60);
|
||||
assert_eq!((r.status, r.body["created"].as_bool(), r.body["published"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(false), Some(true), Some(3)));
|
||||
assert_eq!(h.heartbeat(60, false).status, 200);
|
||||
let unknown = {
|
||||
let mut u = Host::new(s, 9);
|
||||
u.ts = s.time();
|
||||
u.unpublish()
|
||||
};
|
||||
assert_eq!((unknown.status, unknown.code().as_str()), (404, "unknown_realm"));
|
||||
}
|
||||
|
||||
fn healthz(s: &TestServer) {
|
||||
let r = reqwest::blocking::get(format!("{}/registry/v1/healthz", s.base)).unwrap();
|
||||
assert_eq!(r.status(), 200);
|
||||
let v: serde_json::Value = r.json().unwrap();
|
||||
assert_eq!((v["status"].as_str(), v["protocol_version"].as_u64()), (Some("ok"), Some(1)));
|
||||
assert_eq!(reqwest::blocking::get(format!("{}/registry/v1/realms", s.base)).unwrap().status(), 404, "there is no public list");
|
||||
assert_eq!(reqwest::blocking::get(format!("{}/admin", s.base)).unwrap().status(), 404, "and no admin surface");
|
||||
}
|
||||
|
||||
fn all(s: &TestServer) {
|
||||
healthz(s);
|
||||
lifecycle(s);
|
||||
presence(s);
|
||||
metadata_changes(s);
|
||||
takeover(s);
|
||||
authentication(s);
|
||||
hostile_input(s);
|
||||
unpublishing(s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_protocol_on_the_memory_store() {
|
||||
all(&memory());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_flood_is_limited() {
|
||||
let cfg = ApiConfig { register: Rate { burst: 3.0, refill: 0.0 }, heartbeat: Rate { burst: 2.0, refill: 0.0 }, general: Rate { burst: 1000.0, refill: 0.0 }, ..ApiConfig::default() };
|
||||
let s = spawn(MemoryStore::new(), cfg);
|
||||
let mut limited = 0;
|
||||
for i in 0..6 {
|
||||
let mut h = Host::new(&s, 20 + i);
|
||||
if h.register("Flood", 60).status == 429 {
|
||||
limited += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(limited, 3, "three registrations from one address, then a limit");
|
||||
|
||||
let s = spawn(MemoryStore::new(), ApiConfig { heartbeat: Rate { burst: 2.0, refill: 0.0 }, ..ApiConfig::default() });
|
||||
let mut h = Host::new(&s, 30);
|
||||
assert_eq!(h.register("Beat", 60).status, 200);
|
||||
let a = h.heartbeat(60, false).status;
|
||||
let b = h.heartbeat(60, false).status;
|
||||
let c = h.heartbeat(60, false);
|
||||
assert_eq!((a, b, c.status, c.code().as_str()), (200, 200, 429, "rate_limited"), "heartbeats are limited per realm");
|
||||
let mut other = Host::new(&s, 31);
|
||||
assert_eq!(other.register("Other", 60).status, 200);
|
||||
assert_eq!(other.heartbeat(60, false).status, 200, "another realm has its own allowance");
|
||||
|
||||
// forged requests cost their sender, and never the realm they pretend to be
|
||||
let s = spawn(MemoryStore::new(), ApiConfig { general: Rate { burst: 12.0, refill: 0.0 }, heartbeat: Rate { burst: 2.0, refill: 0.0 }, ..ApiConfig::default() });
|
||||
let mut victim = Host::new(&s, 40);
|
||||
assert_eq!(victim.register("Victim", 60).status, 200);
|
||||
let mut forger = Host::new(&s, 41);
|
||||
forger.realm = victim.realm;
|
||||
forger.ts = s.time() + 5;
|
||||
let mut statuses = Vec::new();
|
||||
for _ in 0..6 {
|
||||
statuses.push(forger.heartbeat(60, false).status);
|
||||
}
|
||||
assert_eq!(&statuses[..2], &[401, 401]);
|
||||
assert_eq!(*statuses.last().unwrap(), 429, "the forger runs out of allowance: {statuses:?}");
|
||||
// the victim's own per-realm allowance was never spent by the forgeries (its address allowance may be, which is the forger's address too here)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_realm_key_is_what_identifies_it_not_its_address_or_name() {
|
||||
let s = memory();
|
||||
let mut a = Host::new(&s, 50);
|
||||
let mut b = Host::new(&s, 51);
|
||||
assert_eq!(a.register("Same name", 60).status, 200);
|
||||
assert_eq!(b.register("Same name", 60).status, 200, "names are not identities");
|
||||
assert_ne!(a.realm, b.realm);
|
||||
let wrong = SigningKey::from_bytes(&[52; 32]);
|
||||
let path = path_heartbeat(&a.realm);
|
||||
let body = serde_json::to_vec(&a.heartbeat_body(60, false)).unwrap();
|
||||
let h = sign_request(&wrong, "POST", &path, &a.realm, s.time() + 5, &body);
|
||||
assert_eq!(a.send_with("POST", &path, &body, &h.pairs()).status, 401);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn the_protocol_on_postgresql() {
|
||||
let Ok(url) = std::env::var("REGISTRY_TEST_PG") else { return };
|
||||
// host:port:dbname:user:password
|
||||
let parts: Vec<&str> = url.splitn(5, ':').collect();
|
||||
let cfg = coa_registry::pg::PgConfig { host: parts[0].into(), port: parts[1].parse().unwrap(), dbname: parts[2].into(), user: parts[3].into(), password: parts[4].into(), pool_size: 4, statement_timeout_ms: 5000 };
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let store = rt.block_on(async {
|
||||
let s = coa_registry::pg::PgStore::connect(&cfg).await.unwrap();
|
||||
s.migrate().await.unwrap();
|
||||
s.migrate().await.unwrap();
|
||||
s
|
||||
});
|
||||
let s = spawn(store, lax());
|
||||
all(&s);
|
||||
std::mem::forget(rt);
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
//! A Registry running in-process on an ephemeral port with a clock the test moves, and a signing client built from the protocol crate.
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::sync::atomic::{AtomicI64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use coa_registry::api::{router, ApiConfig, AppState};
|
||||
use coa_registry::listener::Peer;
|
||||
use coa_registry::store::Store;
|
||||
use coa_registry_proto::caps::{AdvertisedCapabilities, CatalogEntry, CharacterFormats, ContentProfile, CoreIdentity, ExtensionSupport, Feature, FormatRange, Progression, Ruleset};
|
||||
use coa_registry_proto::sign::{encode_public_key, sign_request};
|
||||
use coa_registry_proto::*;
|
||||
use ed25519_dalek::SigningKey;
|
||||
|
||||
pub struct TestServer {
|
||||
pub base: String,
|
||||
pub now: Arc<AtomicI64>,
|
||||
_stop: tokio::sync::oneshot::Sender<()>,
|
||||
}
|
||||
|
||||
pub const START: i64 = 1_790_000_000;
|
||||
|
||||
pub fn spawn<S: Store>(store: S, cfg: ApiConfig) -> TestServer {
|
||||
let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
std_listener.set_nonblocking(true).unwrap();
|
||||
let addr = std_listener.local_addr().unwrap();
|
||||
let now = Arc::new(AtomicI64::new(START));
|
||||
let clock = {
|
||||
let now = now.clone();
|
||||
Arc::new(move || now.load(Ordering::SeqCst))
|
||||
};
|
||||
let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build().unwrap();
|
||||
rt.block_on(async move {
|
||||
let state = Arc::new(AppState::new(store, clock, cfg));
|
||||
let listener = tokio::net::TcpListener::from_std(std_listener).unwrap();
|
||||
let app = router(state);
|
||||
let _ = axum::serve(listener, app.into_make_service_with_connect_info::<Peer>()).with_graceful_shutdown(async move { let _ = stopped.await; }).await;
|
||||
});
|
||||
});
|
||||
TestServer { base: format!("http://{addr}"), now, _stop: stop }
|
||||
}
|
||||
|
||||
impl TestServer {
|
||||
pub fn set_time(&self, t: i64) {
|
||||
self.now.store(t, Ordering::SeqCst);
|
||||
}
|
||||
pub fn advance(&self, secs: i64) {
|
||||
self.now.fetch_add(secs, Ordering::SeqCst);
|
||||
}
|
||||
pub fn time(&self) -> i64 {
|
||||
self.now.load(Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn caps(max_level: u32) -> AdvertisedCapabilities {
|
||||
let content = ContentProfile {
|
||||
ruleset: Ruleset::Coa,
|
||||
character_formats: CharacterFormats { readable: FormatRange { min: 1, max: 2 }, writable: FormatRange { min: 2, max: 2 } },
|
||||
online_import_job_formats: vec![2],
|
||||
session_protocol: 2,
|
||||
collection_protocol: 1,
|
||||
features: [Feature::RuntimeSessions, Feature::Wardrobe, Feature::LevelProjection].into_iter().collect(),
|
||||
collection_kinds: ["coa:appearance".to_string()].into_iter().collect(),
|
||||
extensions: vec![ExtensionSupport { namespace: "coa".into(), module_version: "1".into(), formats: FormatRange { min: 1, max: 1 } }],
|
||||
client_catalog: [("Appearances.dbc".to_string(), CatalogEntry { sha256: "a".repeat(64), records: 5 })].into_iter().collect(),
|
||||
};
|
||||
let content_profile_hash = content.hash();
|
||||
AdvertisedCapabilities {
|
||||
profile_version: 2,
|
||||
core: Some(CoreIdentity { commit: "abc123".into(), branch: "main".into(), date: "2026-10-01".into() }),
|
||||
content,
|
||||
content_profile_hash,
|
||||
progression: Some(Progression { max_player_level: max_level, projection_protocol: 1, projection_policy_version: 1, progression_signature: "b".repeat(64), scaling_enabled: false }),
|
||||
}
|
||||
}
|
||||
|
||||
/// One realm's signing client.
|
||||
pub struct Host {
|
||||
pub base: String,
|
||||
pub key: SigningKey,
|
||||
pub realm: RealmId,
|
||||
pub ts: i64,
|
||||
http: reqwest::blocking::Client,
|
||||
}
|
||||
|
||||
fn uuid_bytes() -> [u8; 16] {
|
||||
*RealmId::new().as_uuid().as_bytes()
|
||||
}
|
||||
|
||||
pub struct Reply {
|
||||
pub status: u16,
|
||||
pub body: serde_json::Value,
|
||||
}
|
||||
|
||||
impl Reply {
|
||||
pub fn code(&self) -> String {
|
||||
self.body["error"]["code"].as_str().unwrap_or("").to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Host {
|
||||
pub fn new(server: &TestServer, seed: u8) -> Self {
|
||||
Self { base: server.base.clone(), key: SigningKey::from_bytes(&[seed; 32]), realm: RealmId::new(), ts: server.time(), http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(10)).build().unwrap() }
|
||||
}
|
||||
|
||||
/// A client for a Registry that is not in this process (a real node): a random key, the real clock.
|
||||
pub fn remote(base: &str) -> Self {
|
||||
let mut seed = [0u8; 32];
|
||||
seed[..16].copy_from_slice(uuid_bytes().as_slice());
|
||||
seed[16..].copy_from_slice(uuid_bytes().as_slice());
|
||||
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64;
|
||||
Self { base: base.trim_end_matches('/').to_string(), key: SigningKey::from_bytes(&seed), realm: RealmId::new(), ts, http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(20)).build().unwrap() }
|
||||
}
|
||||
|
||||
/// Bring the timestamp up to the real clock (after a pause); it never goes backwards.
|
||||
pub fn sync_clock(&mut self) {
|
||||
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64;
|
||||
self.ts = self.ts.max(now);
|
||||
}
|
||||
|
||||
pub fn register_body(&self, name: &str, level: u32) -> RegisterRequest {
|
||||
let capabilities = caps(level);
|
||||
RegisterRequest {
|
||||
protocol_version: 1,
|
||||
realm_id: self.realm,
|
||||
public_key: encode_public_key(&self.key.verifying_key()),
|
||||
display_name: name.into(),
|
||||
description: "A test realm.".into(),
|
||||
language: "en".into(),
|
||||
ruleset: Ruleset::Coa,
|
||||
manager_version: "0.6.6".into(),
|
||||
capabilities_hash: capabilities.advert_hash(),
|
||||
capabilities,
|
||||
player_count: Some(1),
|
||||
player_capacity: Some(50),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn heartbeat_body(&self, level: u32, with_caps: bool) -> HeartbeatRequest {
|
||||
let capabilities = caps(level);
|
||||
HeartbeatRequest { protocol_version: 1, capabilities_hash: capabilities.advert_hash(), capabilities: with_caps.then_some(capabilities), manager_version: None, player_count: Some(2), player_capacity: Some(50), display_name: None, description: None, language: None }
|
||||
}
|
||||
|
||||
/// A fully signed request; the timestamp advances by one for every call.
|
||||
pub fn send(&mut self, method: &str, path: &str, body: &[u8]) -> Reply {
|
||||
self.ts += 1;
|
||||
let headers = sign_request(&self.key, method, path, &self.realm, self.ts, body);
|
||||
self.send_with(method, path, body, &headers.pairs())
|
||||
}
|
||||
|
||||
pub fn send_with(&self, method: &str, path: &str, body: &[u8], headers: &[(&str, String)]) -> Reply {
|
||||
let url = format!("{}{}", self.base, path);
|
||||
let mut req = match method {
|
||||
"GET" => self.http.get(&url),
|
||||
_ => self.http.post(&url).body(body.to_vec()),
|
||||
};
|
||||
for (n, v) in headers {
|
||||
req = req.header(*n, v);
|
||||
}
|
||||
let resp = req.send().expect("the Registry answers");
|
||||
let status = resp.status().as_u16();
|
||||
let text = resp.text().unwrap_or_default();
|
||||
Reply { status, body: serde_json::from_str(&text).unwrap_or(serde_json::Value::Null) }
|
||||
}
|
||||
|
||||
pub fn register(&mut self, name: &str, level: u32) -> Reply {
|
||||
let body = serde_json::to_vec(&self.register_body(name, level)).unwrap();
|
||||
self.send("POST", PATH_REGISTER, &body)
|
||||
}
|
||||
|
||||
pub fn heartbeat(&mut self, level: u32, with_caps: bool) -> Reply {
|
||||
let body = serde_json::to_vec(&self.heartbeat_body(level, with_caps)).unwrap();
|
||||
let path = path_heartbeat(&self.realm);
|
||||
self.send("POST", &path, &body)
|
||||
}
|
||||
|
||||
pub fn unpublish(&mut self) -> Reply {
|
||||
let body = serde_json::to_vec(&UnpublishRequest { protocol_version: 1 }).unwrap();
|
||||
let path = path_unpublish(&self.realm);
|
||||
self.send("POST", &path, &body)
|
||||
}
|
||||
|
||||
pub fn me(&mut self) -> Reply {
|
||||
let path = path_self(&self.realm);
|
||||
self.send("GET", &path, b"")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user