Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

Merge remote-tracking branch 'upstream/main' into feat/ism-ilm-lifecycle-engine

Vincenzo Lombardoac4d10219c37
diff --cc engine/crates/xerj-engine/src/engine.rs
index 4a7dcee9b,b6a962b9f..89dc6a254
--- a/engine/crates/xerj-engine/src/engine.rs
+++ b/engine/crates/xerj-engine/src/engine.rs
@@@ -2482,72 -2674,114 +2903,184 @@@ mod snapshot_path_security_tests
}
}
+#[cfg(test)]
+mod ism_policy_persistence_validation_tests {
+ use crate::lifecycle::{LifecyclePolicy, LifecycleState};
+ use crate::Engine;
+ use xerj_common::config::Config;
+
+ fn valid_policy() -> LifecyclePolicy {
+ LifecyclePolicy {
+ description: None,
+ default_state: "only".to_string(),
+ states: vec![LifecycleState {
+ name: "only".to_string(),
+ actions: vec![],
+ transitions: vec![],
+ }],
+ }
+ }
+
+ /// `load_persisted_ism_policies` is the one path into `ism_policies`
+ /// that doesn't go through the REST layer's `validate()` call (native
+ /// PUT and the ILM-translation path both validate before ever storing
+ /// a policy) — it just reads back whatever was written to disk. A
+ /// hand-edited or otherwise corrupted single entry must not silently
+ /// load: `change_ism_policy` only checks "does this policy_id exist",
+ /// so an unvalidated entry here would be reachable through it without
+ /// ever having passed the same structural checks a direct PUT enforces.
+ #[tokio::test]
+ async fn corrupt_single_entry_is_skipped_valid_entries_still_restored() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.server.data_dir = dir.path().to_string_lossy().into_owned();
+
+ // "broken" transitions to a state that doesn't exist — exactly what
+ // `LifecyclePolicy::validate()` rejects, but nothing stops it being
+ // written to disk directly (bypassing the REST layer entirely).
+ let mut policies = std::collections::HashMap::new();
+ policies.insert("good-policy".to_string(), valid_policy());
+ policies.insert(
+ "broken-policy".to_string(),
+ LifecyclePolicy {
+ description: None,
+ default_state: "start".to_string(),
+ states: vec![LifecycleState {
+ name: "start".to_string(),
+ actions: vec![],
+ transitions: vec![crate::lifecycle::LifecycleTransition {
+ state_name: "does-not-exist".to_string(),
+ conditions: None,
+ }],
+ }],
+ },
+ );
+ std::fs::write(
+ dir.path().join("ism_policies.json"),
+ serde_json::to_vec(&policies).unwrap(),
+ )
+ .unwrap();
+
+ let engine = Engine::new(config).unwrap();
+ assert!(
+ engine.ism_policies.contains_key("good-policy"),
+ "a structurally valid persisted policy must still load"
+ );
+ assert!(
+ !engine.ism_policies.contains_key("broken-policy"),
+ "a policy that would fail validate() must not be silently restored"
+ );
+ }
+}
++
+ #[cfg(test)]
+ mod api_key_record_migration_tests {
+ use super::ApiKeyRecord;
+
+ /// A record as it comes off disk, before the load path touches it.
+ fn loaded(secret_hash: &str, plaintext: Option<&str>) -> ApiKeyRecord {
+ ApiKeyRecord {
+ name: "loaded".to_string(),
+ secret_hash: secret_hash.to_string(),
+ legacy_plaintext_secret: plaintext.map(str::to_string),
+ creation_ms: 1_753_600_000_000,
+ expiration_ms: None,
+ invalidated: false,
+ invalidation_ms: None,
+ roles: Vec::new(),
+ }
+ }
+
+ /// The discriminator is `is_usable_hash`, not `is_empty` and not a check
+ /// of the `$ssha256$` tag: a `secret_hash` that does not decode can never
+ /// verify against anything, so a record carrying only that is as unusable
+ /// as one carrying nothing and must be dropped, not restored as a live
+ /// key. Three of these carry the tag — a prefix test would call them
+ /// migrated and keep them.
+ #[test]
+ fn an_unparseable_hash_with_no_plaintext_is_dropped() {
+ for stored in [
+ "",
+ "$ssha256$",
+ "$ssha256$truncated",
+ "$ssha256$deadbeef$cafe",
+ "$argon2id$v=19$m=1,t=1,p=1$c2FsdA$aGFzaA",
+ "left-over-plaintext",
+ ] {
+ for plaintext in [None, Some("")] {
+ let mut rec = loaded(stored, plaintext);
+ assert_eq!(
+ rec.migrate_from_plaintext(),
+ Err(()),
+ "{stored:?} + {plaintext:?} must be dropped, not restored"
+ );
+ }
+ }
+ }
+
+ /// A plaintext beside a hash-shaped value that does not decode is a store
+ /// nobody can explain — not the pre-#201 shape (that has no `secret_hash`
+ /// at all), and not a migrated one. Deriving a live credential from the
+ /// half of it #201 exists to delete is the fail-open reading, so the
+ /// record is dropped instead.
+ #[test]
+ fn a_plaintext_beside_an_unparseable_hash_is_dropped_not_repaired() {
+ for stored in [
+ "$ssha256$truncated",
+ "$argon2id$v=19$m=1,t=1,p=1$c2FsdA$aGFzaA",
+ "left-over-plaintext",
+ ] {
+ let mut rec = loaded(stored, Some("the-plaintext"));
+ assert_eq!(
+ rec.migrate_from_plaintext(),
+ Err(()),
+ "{stored:?} + a plaintext is ambiguous and must be dropped"
+ );
+ }
+ }
+
+ /// A usable hash still wins over a leftover plaintext, and the plaintext is
+ /// discarded — re-deriving there could silently swap which secret the
+ /// record authenticates.
+ #[test]
+ fn a_usable_hash_wins_over_a_leftover_plaintext() {
+ let hash = crate::secret_hash::hash_secret("hashed-secret");
+ let mut rec = loaded(&hash, Some("stale-plaintext"));
+ assert_eq!(rec.migrate_from_plaintext(), Ok(true));
+ assert_eq!(rec.secret_hash, hash);
+ assert!(rec.legacy_plaintext_secret.is_none());
+ assert!(rec.verify_secret("hashed-secret"));
+ assert!(!rec.verify_secret("stale-plaintext"));
+ }
+
+ /// An already-migrated record is left alone and reports no rewrite.
+ #[test]
+ fn an_already_hashed_record_is_untouched() {
+ let hash = crate::secret_hash::hash_secret("s");
+ let mut rec = loaded(&hash, None);
+ assert_eq!(rec.migrate_from_plaintext(), Ok(false));
+ assert_eq!(rec.secret_hash, hash);
+ }
+
+ /// The pre-#201 shape: plaintext only.
+ #[test]
+ fn a_pre_201_plaintext_record_is_hashed() {
+ let mut rec = loaded("", Some("legacy-secret"));
+ assert_eq!(rec.migrate_from_plaintext(), Ok(true));
+ assert!(crate::secret_hash::is_usable_hash(&rec.secret_hash));
+ assert!(rec.verify_secret("legacy-secret"));
+ }
+
+ /// Even if one reached the auth path unmigrated, an unusable hash denies.
+ #[test]
+ fn verify_secret_denies_an_unusable_hash() {
+ for stored in ["", "$ssha256$truncated", "plaintext-secret"] {
+ let rec = loaded(stored, None);
+ assert!(
+ !rec.verify_secret(stored),
+ "{stored:?} must not verify against itself"
+ );
+ assert!(!rec.verify_secret("anything"));
+ }
+ }
+ }
diff --cc engine/crates/xerj-engine/src/lib.rs
index 60fadf50f,f7d78c142..397c2112c
--- a/engine/crates/xerj-engine/src/lib.rs
+++ b/engine/crates/xerj-engine/src/lib.rs
@@@ -25,10 -25,11 +25,12 @@@ pub mod governor
pub mod index;
pub mod index_guard;
pub mod ingest_memory;
+pub mod lifecycle;
pub mod memtable;
pub mod painless;
+ pub mod pools;
pub mod rbac;
+ pub mod secret_hash;
pub mod segment_cache_budget;
pub mod segment_cache_estimates;
pub mod slow_query;