diff --git a/crates/limen-audit/src/ocsf.rs b/crates/limen-audit/src/ocsf.rs index 4c8bce4a..5c62c4f5 100644 --- a/crates/limen-audit/src/ocsf.rs +++ b/crates/limen-audit/src/ocsf.rs @@ -413,12 +413,16 @@ fn millis(event: &Event) -> i64 { i64::try_from(event.time.unix_timestamp_nanos() / 1_000_000).unwrap_or(0) } -/// An IP address from the event, if it is one (the native field is free -/// text for some producers). -fn ip(data: &Value, key: &str) -> Option { - text(data, key) - .and_then(|s| s.parse::().ok()) - .map(|a| a.to_string()) +/// An OCSF endpoint from the event's address, if it is one. The native field +/// is free text: the proxy writes a socket address (`203.0.113.9:51234`, +/// `[2001:db8::1]:22`), the control plane a bare IP. +fn endpoint(data: &Value, key: &str) -> Option { + let s = text(data, key)?; + if let Ok(ip) = s.parse::() { + return Some(json!({ "ip": ip.to_string() })); + } + let sa = s.parse::().ok()?; + Some(json!({ "ip": sa.ip().to_string(), "port": sa.port() })) } fn product(version: &str, component: Option<&str>) -> Value { @@ -504,8 +508,8 @@ pub fn to_ocsf(event: &Event, product_version: &str) -> Value { obj.insert("user".into(), who); } } - if let Some(addr) = ip(&event.data, "client_addr") { - obj.insert("src_endpoint".into(), json!({ "ip": addr })); + if let Some(src) = endpoint(&event.data, "client_addr") { + obj.insert("src_endpoint".into(), src); } if class.uid == HTTP_ACTIVITY.uid { let mut req = Map::new(); @@ -768,6 +772,28 @@ mod tests { assert_eq!(denied["severity_id"], 2); } + #[test] + fn a_socket_address_becomes_an_endpoint_with_its_port() { + // The proxy writes `SocketAddr::to_string()` for its sessions and denials. + let src = |addr: &str| { + to_ocsf( + &event(EventKind::SessionStart, json!({"client_addr": addr})), + "1", + )["src_endpoint"] + .clone() + }; + assert_eq!( + src("203.0.113.9:51234"), + json!({"ip": "203.0.113.9", "port": 51234}) + ); + assert_eq!( + src("[2001:db8::1]:22"), + json!({"ip": "2001:db8::1", "port": 22}) + ); + assert_eq!(src("2001:db8::1"), json!({"ip": "2001:db8::1"})); + assert_eq!(src("not an ip"), Value::Null); + } + #[test] fn http_methods_and_statuses_map() { let v = to_ocsf( diff --git a/crates/limen-control-plane/src/config.rs b/crates/limen-control-plane/src/config.rs index 65a3e9dd..51c28740 100644 --- a/crates/limen-control-plane/src/config.rs +++ b/crates/limen-control-plane/src/config.rs @@ -664,13 +664,16 @@ impl Config { .as_deref() .is_some_and(|a| a.trim().eq_ignore_ascii_case(bucket.trim())); // The same name on another endpoint is another store; on the same one it is not. + // S3 bucket names are global on AWS, so every AWS endpoint (any region, + // or none at all) is the same store. let norm = |e: &str| e.trim().trim_end_matches('/').to_ascii_lowercase(); + let aws = |e: &str| e.contains("amazonaws.com"); let other_endpoint = self.audit_ocsf_s3_endpoint.as_deref().is_some_and(|e| { let e = norm(e); - match self.audit_s3_endpoint.as_deref() { - Some(a) => e != norm(a), + match self.audit_s3_endpoint.as_deref().map(norm) { + Some(a) => e != a && !(aws(&e) && aws(&a)), // The audit bucket is on AWS (no endpoint): an AWS endpoint is the same store. - None => !e.contains("amazonaws.com"), + None => !aws(&e), } }); if same_bucket && !other_endpoint { @@ -919,6 +922,32 @@ mod tests { ]) .is_err() ); + // Two AWS regions are still one global bucket namespace. + for ocsf in [ + "https://s3.eu-west-1.amazonaws.com", + "https://s3.amazonaws.com", + ] { + assert!( + ok(&[ + "--audit-s3-bucket=audit", + "--audit-s3-endpoint=https://s3.us-east-1.amazonaws.com", + "--audit-ocsf-s3-bucket=audit", + &format!("--audit-ocsf-s3-endpoint={ocsf}"), + ]) + .is_err(), + "{ocsf}" + ); + } + // An S3-compatible store and AWS are different stores. + assert!( + ok(&[ + "--audit-s3-bucket=audit", + "--audit-s3-endpoint=http://s3:8333", + "--audit-ocsf-s3-bucket=audit", + "--audit-ocsf-s3-endpoint=https://s3.eu-west-1.amazonaws.com", + ]) + .is_ok() + ); // Spelling out the audit endpoint again does not make it another store. for same in ["http://s3:8333", "http://s3:8333/"] { assert!( diff --git a/crates/limen-control-plane/src/scim.rs b/crates/limen-control-plane/src/scim.rs index 647fcbcd..62a85358 100644 --- a/crates/limen-control-plane/src/scim.rs +++ b/crates/limen-control-plane/src/scim.rs @@ -146,6 +146,13 @@ fn token_matches(given: &str, wanted: &str) -> bool { Sha256::digest(given.as_bytes()) == Sha256::digest(wanted.as_bytes()) } +/// The credentials of a `Bearer` authorization header. The scheme is +/// case-insensitive (RFC 7235 §2.1). +fn bearer_token(header: &str) -> Option<&str> { + let (scheme, token) = header.split_once(' ')?; + scheme.eq_ignore_ascii_case("bearer").then_some(token) +} + async fn authenticate(State(state): State, req: Request, next: Next) -> Response { let Some(wanted) = &state.config.scim_token else { return not_found("resource"); @@ -154,7 +161,7 @@ async fn authenticate(State(state): State, req: Request, next: Next) - .headers() .get(header::AUTHORIZATION) .and_then(|v| v.to_str().ok()) - .and_then(|v| v.strip_prefix("Bearer ")) + .and_then(bearer_token) .unwrap_or(""); if !token_matches(given.trim(), wanted.expose().trim()) { metrics::counter!("limen_scim_requests_total", "outcome" => "unauthorized").increment(1); @@ -1039,10 +1046,19 @@ fn plan_patch(v: &Value) -> Result<(Option, Vec), Response> { } if let Some(m) = obj.get("members") { let ids = member_ids(m)?; - steps.push(if kind == "add" { - Step::Add(ids) - } else { - Step::Replace(ids) + // RFC 7644 §3.5.2.2: a remove needs a path. Reading it (or + // an unknown op) as a replace would drop every other member. + steps.push(match kind.as_str() { + "add" => Step::Add(ids), + "replace" => Step::Replace(ids), + "remove" => { + return Err(error( + StatusCode::BAD_REQUEST, + Some("noTarget"), + "a remove of members needs a path", + )); + } + _ => return Err(bad_request("unsupported members operation")), }); } } @@ -1160,6 +1176,29 @@ mod tests { assert!(patched_active(&json!({})).is_err()); } + #[test] + fn a_pathless_members_patch_needs_a_known_op() { + let m = json!({ "members": [{ "value": "00000000-0000-0000-0000-000000000001" }] }); + let id = Uuid::parse_str("00000000-0000-0000-0000-000000000001").expect("a uuid"); + let plan = |op: &str| plan_patch(&json!({ "Operations": [{ "op": op, "value": m }] })); + assert!(matches!(&plan("Add").expect("plans").1[..], [Step::Add(v)] if v == &[id])); + assert!(matches!(&plan("replace").expect("plans").1[..], [Step::Replace(v)] if v == &[id])); + // A remove without a path must not replace the members with its value. + for op in ["remove", "Remove", "bogus", ""] { + assert!(plan(op).is_err(), "{op}"); + } + } + + #[test] + fn the_bearer_scheme_is_case_insensitive() { + assert_eq!(bearer_token("Bearer abc"), Some("abc")); + assert_eq!(bearer_token("bearer abc"), Some("abc")); + assert_eq!(bearer_token("BEARER abc"), Some("abc")); + assert_eq!(bearer_token("Basic abc"), None); + assert_eq!(bearer_token("Bearerabc"), None); + assert_eq!(bearer_token(""), None); + } + #[test] fn tokens_compare_whole() { assert!(token_matches("abc", "abc")); diff --git a/crates/limen-worker/src/search/rules.rs b/crates/limen-worker/src/search/rules.rs index dce6afab..34c53a57 100644 --- a/crates/limen-worker/src/search/rules.rs +++ b/crates/limen-worker/src/search/rules.rs @@ -94,6 +94,9 @@ pub struct Shape { pub struct Named { pub all: bool, pub names: BTreeSet, + /// An `_msearch` header asks for hidden indices (`expand_wildcards`), + /// which the query string's check can't see. + pub hidden_wildcards: bool, } impl Named { @@ -882,6 +885,16 @@ pub fn inspect_body(shape: &Shape, body: &[u8]) -> Result { if !listed { named.add_scope(&shape.scope); } + // Each header may carry its own `expand_wildcards`. + if let Some(v) = h.get("expand_wildcards") { + named.hidden_wildcards |= token_names(v, "an _msearch expand_wildcards")? + .iter() + .flat_map(|x| x.split(',')) + .any(|x| { + let x = x.trim().to_ascii_lowercase(); + x == "all" || x == "hidden" + }); + } if shape.body == BodyKind::MsearchTemplate { walk_template(&parse_json(search)?, &mut named)?; } else { @@ -1036,7 +1049,7 @@ fn grant_allows( if restricted && shape.source_param { return Err("the source query parameter isn't allowed with index-restricted access; send the query as the body".into()); } - if shape.hidden_wildcards && !g.indices.iter().any(|p| hidden(p)) { + if (shape.hidden_wildcards || named.hidden_wildcards) && !g.indices.iter().any(|p| hidden(p)) { return Err("expand_wildcards for hidden indices isn't allowed".into()); } if named.all { @@ -1381,6 +1394,43 @@ mod tests { ); } + #[test] + fn msearch_headers_cannot_ask_for_hidden_indices() { + let plain = grant(&[Read], &["logs-*"]); + let body = |h: &str| format!("{h}\n{{}}\n").into_bytes(); + assert!(allowed(&plain, "POST", "/_msearch", &body(r#"{"index":"logs-*"}"#)).is_ok()); + for h in [ + r#"{"index":"logs-*","expand_wildcards":"all"}"#, + r#"{"index":"logs-*","expand_wildcards":"open,Hidden"}"#, + r#"{"index":"logs-*","expand_wildcards":["open","hidden"]}"#, + ] { + assert!( + allowed(&plain, "POST", "/_msearch", &body(h)).is_err(), + "{h}" + ); + } + assert!( + allowed( + &plain, + "POST", + "/_msearch", + &body(r#"{"index":"logs-*","expand_wildcards":"open"}"#) + ) + .is_ok() + ); + // A grant that names hidden indices may expand to them, as with the query string. + let dot = grant(&[Read], &["logs-*", ".kibana*"]); + assert!( + allowed( + &dot, + "POST", + "/_msearch", + &body(r#"{"index":"logs-*","expand_wildcards":"hidden"}"#) + ) + .is_ok() + ); + } + #[test] fn classes_must_be_granted() { let read = grant(&[], &[]); diff --git a/demos/README.md b/demos/README.md index f14ca4d6..9136a229 100644 --- a/demos/README.md +++ b/demos/README.md @@ -24,6 +24,6 @@ running one: they build the CLI and start containers. | `demo-phase14.sh` | The management API (ADR-0042) | | `demo-shadow.sh` | Shadow mode for deny rules (ADR-0046) | | `demo-idp-offboarding.sh` | IdP-driven onboarding (groups become roles) and offboarding against a real Dex (ADR-0047) | -| `demo-ocsf.sh` | OCSF export of the audit trail for SIEMs (ADR-0048) | +| `demo-ocsf.sh` | OCSF export of the audit trail for SIEMs (ADR-0049) | | `demo-dx.sh` | First five minutes: `limen doctor`, ssh, scp, rsync, git, `limen kube config` | | `demo-website.sh` | Builds and checks the documentation site |