mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-20 19:25:56 +00:00
Bind upgrade compatibility to signed database source contracts
This commit is contained in:
1 parent
5b16842720
commit
40f9feab84
4 files changed
+103
-4
No files matched your search
@@ -4,4 +4,4 @@ type: added
|
||||
audience: admins
|
||||
title: Server updates can require a tested combination of platform and installed version
|
||||
---
|
||||
Signed release metadata identifies the core and module revisions and supported starting versions. Incompatible update packages are rejected before the server is stopped.
|
||||
Signed release metadata identifies core and module revisions, supported starting versions and database contracts. Incompatible packages are rejected before shutdown; changed schema contracts and incompatible databases block installation before migration.
|
||||
@@ -78,6 +78,13 @@ pub struct Revision {
|
||||
pub commit: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct SourceDatabase {
|
||||
pub manifest_sha256: String,
|
||||
pub schema_sha256: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Compatibility {
|
||||
@@ -87,6 +94,8 @@ pub struct Compatibility {
|
||||
pub module_commits: std::collections::BTreeMap<String, String>,
|
||||
pub client_patch_version: String,
|
||||
pub source_versions: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
|
||||
pub source_databases: std::collections::BTreeMap<String, SourceDatabase>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -156,10 +165,18 @@ impl Manifest {
|
||||
if c.schema != 1 || !matches!(c.platform.as_str(), "windows-x86_64" | "linux-x86_64")
|
||||
|| !is_commit(&c.core_commit) || self.core.commit.as_deref() != Some(&c.core_commit)
|
||||
|| c.module_commits.is_empty() || c.module_commits.values().any(|sha| !is_commit(sha))
|
||||
|| self.bots.as_ref().and_then(|r| r.commit.as_ref()).is_some_and(|sha| c.module_commits.get("bots") != Some(sha))
|
||||
|| parse_version(&c.client_patch_version).is_none()
|
||||
|| c.source_versions.is_empty() || c.source_versions.iter().any(|v| parse_version(v).is_none()) {
|
||||
|| c.source_versions.is_empty() || c.source_versions.iter().any(|v| parse_version(v).is_none())
|
||||
|| c.source_versions.iter().collect::<HashSet<_>>().len() != c.source_versions.len() {
|
||||
return bad("Invalid release compatibility matrix".into());
|
||||
}
|
||||
if !c.source_databases.is_empty() && (c.source_databases.len() != c.source_versions.len()
|
||||
|| c.source_versions.iter().any(|v| !c.source_databases.contains_key(v))
|
||||
|| c.source_databases.values().any(|s| !is_sha256(&s.manifest_sha256)
|
||||
|| s.schema_sha256.as_ref().is_some_and(|sha| !is_sha256(sha)))) {
|
||||
return bad("Invalid source database compatibility".into());
|
||||
}
|
||||
}
|
||||
for migration in &self.migrations {
|
||||
if !is_sha256(&migration.sha256) || migration.compatible_sha256.iter().any(|h| !is_sha256(h) || h == &"0".repeat(64)) {
|
||||
@@ -263,7 +280,7 @@ mod tests {
|
||||
m.min_manager_version = "0.6.13".into();
|
||||
m.compatibility = Some(Compatibility { schema: 1, platform: "windows-x86_64".into(),
|
||||
core_commit: "a".repeat(40), module_commits: [("squid".into(), "b".repeat(40))].into(),
|
||||
client_patch_version: "1.5.1".into(), source_versions: vec!["0.261010.32".into()] });
|
||||
source_databases: Default::default(), client_patch_version: "1.5.1".into(), source_versions: vec!["0.261010.32".into()] });
|
||||
m.validate().unwrap();
|
||||
m.compatibility.as_mut().unwrap().core_commit = "c".repeat(40);
|
||||
assert!(m.validate().is_err());
|
||||
@@ -272,6 +289,38 @@ mod tests {
|
||||
assert!(m.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatibility_cannot_claim_a_different_packaged_bot_revision() {
|
||||
let mut m = Manifest::parse(sample("").as_bytes()).unwrap();
|
||||
m.min_manager_version = "0.6.13".into();
|
||||
m.bots = Some(Revision { commit: Some("b".repeat(40)) });
|
||||
m.compatibility = Some(Compatibility { schema: 1, platform: "windows-x86_64".into(),
|
||||
core_commit: "a".repeat(40), module_commits: [("bots".into(), "c".repeat(40))].into(),
|
||||
source_databases: Default::default(), client_patch_version: "1.5.1".into(), source_versions: vec!["0.2.0".into()] });
|
||||
assert!(m.validate().is_err());
|
||||
m.compatibility.as_mut().unwrap().module_commits.insert("bots".into(), "b".repeat(40));
|
||||
m.validate().unwrap();
|
||||
m.compatibility.as_mut().unwrap().source_versions.push("0.2.0".into());
|
||||
assert!(m.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_database_contracts_must_cover_the_exact_upgrade_versions() {
|
||||
let mut m = Manifest::parse(sample("").as_bytes()).unwrap();
|
||||
m.min_manager_version = "0.6.13".into();
|
||||
m.compatibility = Some(Compatibility { schema: 1, platform: "windows-x86_64".into(),
|
||||
core_commit: "a".repeat(40), module_commits: [("squid".into(), "b".repeat(40))].into(),
|
||||
client_patch_version: "1.5.1".into(), source_versions: vec!["0.2.0".into()],
|
||||
source_databases: [("0.2.0".into(), SourceDatabase { manifest_sha256: "a".repeat(64), schema_sha256: None })].into() });
|
||||
m.validate().unwrap();
|
||||
m.compatibility.as_mut().unwrap().source_databases.get_mut("0.2.0").unwrap().schema_sha256 = Some("bad".into());
|
||||
assert!(m.validate().is_err());
|
||||
let c = m.compatibility.as_mut().unwrap();
|
||||
c.source_databases.get_mut("0.2.0").unwrap().schema_sha256 = None;
|
||||
c.source_versions = vec!["0.2.1".into()];
|
||||
assert!(m.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_traversal_bad_hash_duplicates_and_short_commit() {
|
||||
let ok = "b".repeat(64);
|
||||
|
||||
@@ -887,6 +887,27 @@ impl Env for RepackEnv<'_> {
|
||||
|
||||
fn automatic_rollback(&self) -> bool { true }
|
||||
fn preflight(&self, manifest: &Manifest) -> Result<()> {
|
||||
if let Some(compatibility) = &manifest.compatibility {
|
||||
let (_, meta) = MetaDir::open(self.meta_dir)?;
|
||||
if let Some(source) = meta.core.version.as_ref().and_then(|v| compatibility.source_databases.get(v)) {
|
||||
if let Some(expected) = &source.schema_sha256 {
|
||||
let contract = self.root.join("Scripts/database-schema.json");
|
||||
if fsx::sha256_file(&contract)? != *expected {
|
||||
return Err(Error::Invalid("The installed database schema contract differs from the qualified upgrade source; repair the installation first.".into()));
|
||||
}
|
||||
}
|
||||
crate::backup::with_database(self.root, |db| {
|
||||
let mut modes = vec![crate::realms::Mode::Coa];
|
||||
if crate::realms::state(self.root)?.wildcard_created { modes.push(crate::realms::Mode::Wildcard); }
|
||||
for mode in modes {
|
||||
if let Some(problem) = crate::schema_check::check(&db.clone().for_realm(mode), self.root)?.first() {
|
||||
return Err(Error::Invalid(format!("The installed {} database does not match its upgrade source: {}.{}: {}", mode.name(), problem.database, problem.table, problem.detail)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
}
|
||||
self.pending_migrations(manifest).map(|_| ())
|
||||
}
|
||||
|
||||
@@ -1428,7 +1449,7 @@ mod tests {
|
||||
manifest.compatibility = Some(crate::manifest::Compatibility { schema: 1,
|
||||
platform: if cfg!(windows) { "windows-x86_64" } else { "linux-x86_64" }.into(),
|
||||
core_commit: "a".repeat(40), module_commits: [("squid".into(), "b".repeat(40))].into(),
|
||||
client_patch_version: "1.5.1".into(), source_versions: vec!["1.0.0".into()] });
|
||||
source_databases: Default::default(), client_patch_version: "1.5.1".into(), source_versions: vec!["1.0.0".into()] });
|
||||
manifest.validate().unwrap();
|
||||
validate_candidate(&meta, &manifest).unwrap();
|
||||
manifest.compatibility.as_mut().unwrap().source_versions = vec!["0.9.0".into()];
|
||||
@@ -1440,6 +1461,25 @@ mod tests {
|
||||
assert!(unfinished(&w.meta).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_installed_schema_contract_blocks_before_database_access() {
|
||||
let w = world(&[], false);
|
||||
write(&w.root, "Scripts/database-schema.json", b"modified schema contract");
|
||||
let mut manifest = Manifest::parse(&fs::read(w.pkg.join("manifest.json")).unwrap()).unwrap();
|
||||
manifest.compatibility = Some(crate::manifest::Compatibility { schema: 1,
|
||||
platform: "windows-x86_64".into(), core_commit: "a".repeat(40),
|
||||
module_commits: [("squid".into(), "b".repeat(40))].into(), client_patch_version: "1.5.1".into(),
|
||||
source_versions: vec!["1.0.0".into()],
|
||||
source_databases: [("1.0.0".into(), crate::manifest::SourceDatabase {
|
||||
manifest_sha256: "a".repeat(64), schema_sha256: Some("b".repeat(64)) })].into() });
|
||||
let env = RepackEnv { root: &w.root, meta_dir: &w.meta };
|
||||
let error = env.preflight(&manifest).unwrap_err().to_string();
|
||||
assert!(error.contains("schema contract differs"), "{error}");
|
||||
assert!(!w.root.join("Runtime/python/python.exe").exists());
|
||||
assert_eq!(read(&w, "Core/worldserver.exe"), b"world-v1");
|
||||
assert!(unfinished(&w.meta).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unhealthy_restoration_preserves_recovery_and_blocks_startup() {
|
||||
let w = world(&[], true);
|
||||
|
||||
@@ -47,6 +47,16 @@ fn run() -> Result<(), String> {
|
||||
let e = |x: coa_core::Error| x.to_string();
|
||||
let part = a.get("part-size").and_then(|v| v.parse().ok()).unwrap_or(DEFAULT_PART_SIZE);
|
||||
match cmd.as_str() {
|
||||
"verify-channel" => {
|
||||
let bytes = std::fs::read(need(&a, "file")?).map_err(|x| x.to_string())?;
|
||||
let pointer = coa_core::channels::verify(&bytes, need(&a, "channel")?, coa_core::signing::EMBEDDED_PUBLIC_KEY).map_err(e)?;
|
||||
println!("{}", serde_json::to_string(&pointer).map_err(|x| x.to_string())?);
|
||||
}
|
||||
"verify-manifest" => {
|
||||
let source = coa_core::pkgsource::Source::Dir(PathBuf::from(need(&a, "dir")?));
|
||||
let (manifest, _) = coa_core::pkgsource::fetch_manifest(&source, coa_core::signing::EMBEDDED_PUBLIC_KEY).map_err(e)?;
|
||||
println!("verified signed manifest {}", manifest.version);
|
||||
}
|
||||
"extract-schema-base" => {
|
||||
coa_core::release_schema::extract_base(
|
||||
&PathBuf::from(need(&a, "package")?), &PathBuf::from(need(&a, "fixture")?),
|
||||
|
||||
Reference in new issue
Block a user