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.

fix(api): implement PUT/DELETE /_security/privilege (real store, not just honest-empty)

Vincenzo Lombardo0c95a3b2a70e
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 16a0d3936..aec3ea2df 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -21701,54 +21701,143 @@ pub async fn security_create_api_key(
}
// ─────────────────────────────────────────────────────────────────────────────
-// GET /_security/privilege
-// GET /_security/privilege/{application}
-// GET /_security/privilege/{application}/{name}
+// GET /_security/privilege
+// GET /_security/privilege/{application}
+// GET /_security/privilege/{application}/{name}
+// PUT /_security/privilege
+// DELETE /_security/privilege/{application}/{name}
// ─────────────────────────────────────────────────────────────────────────────
-// xerj has no application-privilege store: `xerj_engine::rbac::Privilege` is a
-// closed enum of index-scoped operations (read/write/admin index, snapshot,
-// security admin, audit), a different concept from ES's user-defined
-// per-application privilege objects (`application`/`name`/`actions`/
-// `metadata`). Kibana polls this endpoint on startup to check/register its
-// own `kibana-.kibana` application privileges; the route didn't exist at all,
-// so every poll 404'd and Kibana logged "Error registering Kibana Privileges"
-// every ~5s (non-fatal, but noisy). Report the honest state instead of a
-// missing route: no application privileges are registered, ES-shaped `{}`
-// with 200 — that's what real ES returns for "no privileges match", as
-// opposed to "this endpoint doesn't exist".
-
-pub async fn security_get_all_privileges(State(_state): State<AppState>) -> impl IntoResponse {
- Json(json!({})).into_response()
+// xerj has no application-privilege store built into RBAC:
+// `xerj_engine::rbac::Privilege` is a closed enum of index-scoped operations
+// (read/write/admin index, snapshot, security admin, audit), a different
+// concept from ES's user-defined per-application privilege objects
+// (`application`/`name`/`actions`/`metadata`) — so this gets its own store,
+// `state.engine.application_privileges`, keyed by `"{application}\0{name}"`.
+//
+// Kibana polls GET at startup to check its own `kibana-.kibana` application
+// privileges, and calls PUT to register them if the GET doesn't already
+// match — originally NEITHER route existed, so every poll 404'd and Kibana
+// logged "Error registering Kibana Privileges" every ~5s (non-fatal, but
+// noisy). Not enforced (same honest-surface convention as `roles`/minted API
+// keys' `role_descriptors`): privileges round-trip through this store, but
+// nothing in the auth path actually gates on them yet.
+
+pub async fn security_get_all_privileges(State(state): State<AppState>) -> impl IntoResponse {
+ Json(privileges_grouped_by_application(&state, None)).into_response()
}
pub async fn security_get_application_privileges(
- State(_state): State<AppState>,
- Path(_application): Path<String>,
+ State(state): State<AppState>,
+ Path(application): Path<String>,
) -> impl IntoResponse {
- Json(json!({})).into_response()
+ Json(privileges_grouped_by_application(
+ &state,
+ Some(&application),
+ ))
+ .into_response()
}
pub async fn security_get_privilege(
- State(_state): State<AppState>,
+ State(state): State<AppState>,
Path((application, name)): Path<(String, String)>,
) -> impl IntoResponse {
// Unlike the list forms above, looking up one specific privilege by name
// is a resource lookup: ES responds 404 when it doesn't exist, matching
// the convention used for other named-resource lookups in this file
// (e.g. `get_task_by_id`).
- (
- StatusCode::NOT_FOUND,
- Json(json!({
- "error": {
- "type": "resource_not_found_exception",
- "reason": format!(
- "application privilege [{name}] not found for application [{application}]"
- ),
- },
- "status": 404
- })),
- )
- .into_response()
+ match state
+ .engine
+ .application_privileges
+ .get(&application_privilege_key(&application, &name))
+ {
+ Some(entry) => Json(json!({ application: { name: entry.value() } })).into_response(),
+ None => (
+ StatusCode::NOT_FOUND,
+ Json(json!({
+ "error": {
+ "type": "resource_not_found_exception",
+ "reason": format!(
+ "application privilege [{name}] not found for application [{application}]"
+ ),
+ },
+ "status": 404
+ })),
+ )
+ .into_response(),
+ }
+}
+
+pub async fn security_put_privileges(
+ State(state): State<AppState>,
+ Json(body): Json<Value>,
+) -> impl IntoResponse {
+ let Some(apps) = body.as_object() else {
+ return ApiError::new(xerj_common::XerjError::invalid_query(
+ "request body must be an object of {application: {privilege_name: {...}}}",
+ ))
+ .into_response();
+ };
+ let mut result = serde_json::Map::new();
+ for (application, privileges) in apps {
+ let Some(privileges) = privileges.as_object() else {
+ continue;
+ };
+ let mut app_result = serde_json::Map::new();
+ for (name, spec) in privileges {
+ let key = application_privilege_key(application, name);
+ let created = !state.engine.application_privileges.contains_key(&key);
+ let stored = json!({
+ "application": application,
+ "name": name,
+ "actions": spec.get("actions").cloned().unwrap_or(json!([])),
+ "metadata": spec.get("metadata").cloned().unwrap_or(json!({})),
+ });
+ state.engine.application_privileges.insert(key, stored);
+ app_result.insert(name.clone(), json!({ "created": created }));
+ }
+ result.insert(application.clone(), Value::Object(app_result));
+ }
+ Json(Value::Object(result)).into_response()
+}
+
+pub async fn security_delete_privilege(
+ State(state): State<AppState>,
+ Path((application, name)): Path<(String, String)>,
+) -> impl IntoResponse {
+ let key = application_privilege_key(&application, &name);
+ let found = state.engine.application_privileges.remove(&key).is_some();
+ Json(json!({ "found": found })).into_response()
+}
+
+fn application_privilege_key(application: &str, name: &str) -> String {
+ format!("{application}\0{name}")
+}
+
+/// Build the ES-shaped `{application: {name: {...}}}` response, optionally
+/// filtered to one application (`None` = every application).
+fn privileges_grouped_by_application(state: &AppState, application: Option<&str>) -> Value {
+ let mut by_app: serde_json::Map<String, Value> = serde_json::Map::new();
+ for entry in state.engine.application_privileges.iter() {
+ let priv_obj = entry.value();
+ let Some(app) = priv_obj.get("application").and_then(Value::as_str) else {
+ continue;
+ };
+ if let Some(want) = application {
+ if app != want {
+ continue;
+ }
+ }
+ let Some(name) = priv_obj.get("name").and_then(Value::as_str) else {
+ continue;
+ };
+ by_app
+ .entry(app.to_string())
+ .or_insert_with(|| json!({}))
+ .as_object_mut()
+ .expect("inserted as object above")
+ .insert(name.to_string(), priv_obj.clone());
+ }
+ Value::Object(by_app)
}
/// Parse an ES time-value expiration (e.g. `"7d"`, `"1h"`, `"30m"`, `"500ms"`)
diff --git a/engine/crates/xerj-api/src/router.rs b/engine/crates/xerj-api/src/router.rs
index 7319c9f89..000632f71 100644
--- a/engine/crates/xerj-api/src/router.rs
+++ b/engine/crates/xerj-api/src/router.rs
@@ -566,7 +566,7 @@ pub fn build_es_compat_router(state: AppState) -> Router {
)
.route(
"/_security/privilege",
- get(es_compat::security_get_all_privileges),
+ get(es_compat::security_get_all_privileges).put(es_compat::security_put_privileges),
)
.route(
"/_security/privilege/:application",
@@ -574,7 +574,7 @@ pub fn build_es_compat_router(state: AppState) -> Router {
)
.route(
"/_security/privilege/:application/:name",
- get(es_compat::security_get_privilege),
+ get(es_compat::security_get_privilege).delete(es_compat::security_delete_privilege),
)
// ── License ───────────────────────────────────────────────────────────
.route(
diff --git a/engine/crates/xerj-engine/src/engine.rs b/engine/crates/xerj-engine/src/engine.rs
index ba2fa037f..03d64ea14 100644
--- a/engine/crates/xerj-engine/src/engine.rs
+++ b/engine/crates/xerj-engine/src/engine.rs
@@ -265,6 +265,15 @@ pub struct Engine {
/// auth middleware can re-authenticate `Authorization: ApiKey <encoded>`.
/// In-memory only (lost on restart).
pub api_keys: Arc<DashMap<String, ApiKeyRecord>>,
+ /// `"{application}\0{name}"` → the ES-shaped privilege object
+ /// (`application`/`name`/`actions`/`metadata`). Populated by
+ /// `PUT /_security/privilege`, read by `GET /_security/privilege*`.
+ /// Not enforced (same honest-surface convention as `roles`/`api_keys`'
+ /// role_descriptors) — Kibana registers its `kibana-.kibana` privileges
+ /// here at startup so `GET` stops reporting an empty set on every
+ /// subsequent poll, but nothing actually gates on them yet.
+ /// In-memory only (lost on restart).
+ pub application_privileges: Arc<DashMap<String, Value>>,
/// legacy index template name (v1 /_template) → template JSON
pub legacy_templates: Arc<DashMap<String, Value>>,
/// pipeline_name → compiled, executable Pipeline (typed transform pipeline)
@@ -382,6 +391,7 @@ impl Engine {
rollup_jobs: Arc::new(DashMap::new()),
ccr_auto_follow: Arc::new(DashMap::new()),
api_keys: Arc::new(DashMap::new()),
+ application_privileges: Arc::new(DashMap::new()),
legacy_templates: Arc::new(DashMap::new()),
transform_pipelines: Arc::new(DashMap::new()),
pits: Arc::new(DashMap::new()),