Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions crates/limen-audit/src/ocsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
text(data, key)
.and_then(|s| s.parse::<std::net::IpAddr>().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<Value> {
let s = text(data, key)?;
if let Ok(ip) = s.parse::<std::net::IpAddr>() {
return Some(json!({ "ip": ip.to_string() }));
}
let sa = s.parse::<std::net::SocketAddr>().ok()?;
Some(json!({ "ip": sa.ip().to_string(), "port": sa.port() }))
}

fn product(version: &str, component: Option<&str>) -> Value {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
35 changes: 32 additions & 3 deletions crates/limen-control-plane/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down
49 changes: 44 additions & 5 deletions crates/limen-control-plane/src/scim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppState>, req: Request, next: Next) -> Response {
let Some(wanted) = &state.config.scim_token else {
return not_found("resource");
Expand All @@ -154,7 +161,7 @@ async fn authenticate(State(state): State<AppState>, 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);
Expand Down Expand Up @@ -1039,10 +1046,19 @@ fn plan_patch(v: &Value) -> Result<(Option<String>, Vec<Step>), 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")),
});
}
}
Expand Down Expand Up @@ -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"));
Expand Down
52 changes: 51 additions & 1 deletion crates/limen-worker/src/search/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ pub struct Shape {
pub struct Named {
pub all: bool,
pub names: BTreeSet<String>,
/// An `_msearch` header asks for hidden indices (`expand_wildcards`),
/// which the query string's check can't see.
pub hidden_wildcards: bool,
}

impl Named {
Expand Down Expand Up @@ -882,6 +885,16 @@ pub fn inspect_body(shape: &Shape, body: &[u8]) -> Result<BodyFacts, Refused> {
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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(&[], &[]);
Expand Down
2 changes: 1 addition & 1 deletion demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Loading