Skip to content
Open
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
12 changes: 9 additions & 3 deletions crates/openshell-server/src/auth/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};
use tonic::{Code, Status};

use super::descriptor_authz;
use super::identity::IdentityProvider;
use super::principal::{Principal, SandboxIdentitySource};
use crate::grpc::mutation_replay::REPLAYED_HEADER;

pub mod catalog;
#[cfg(test)]
pub mod test_support;

Expand Down Expand Up @@ -463,7 +463,7 @@ where
F: Future<Output = Result<http::Response<tonic::body::Body>, E>>,
{
let audited = matches!(principal, Principal::User(_) | Principal::Operator(_))
&& catalog::lookup(&path).is_some();
&& audited_activity(&path).is_some();
if !audited {
return with_request(path, origin, handler).await;
}
Expand Down Expand Up @@ -845,6 +845,12 @@ fn emit_denial(
}
}

/// The activity a method declares with `audit` in its authorization option,
/// if the gateway audits it.
fn audited_activity(path: &str) -> Option<ActivityId> {
descriptor_authz::lookup(path).and_then(|entry| entry.audit)
}

/// An API Activity event for `principal`'s request to `path` from `origin`,
/// naming the activity of a cataloged method.
fn api_activity<'a>(
Expand All @@ -868,7 +874,7 @@ fn api_activity<'a>(
.api_service(Service::new(service))
.http_request(post(path));
}
if let Some(activity) = catalog::lookup(path) {
if let Some(activity) = audited_activity(path) {
builder = builder.activity(activity);
}
builder
Expand Down
116 changes: 0 additions & 116 deletions crates/openshell-server/src/auth/audit/catalog.rs

This file was deleted.

51 changes: 51 additions & 0 deletions crates/openshell-server/src/auth/descriptor_authz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use std::collections::HashMap;
use std::sync::LazyLock;

use openshell_ocsf::ActivityId;
use prost_reflect::{DescriptorPool, Value};

use super::method_authz::{AuthMode, Role};
Expand All @@ -36,6 +37,8 @@ const SCOPE_ONLY_METHODS: &[&str] = &["/openshell.v1.OpenShell/GetGatewayConfig"
pub struct DescriptorAuthEntry {
pub auth_mode: AuthMode,
pub scope: Option<String>,
/// The API Activity recorded for each call, when the method is audited.
pub audit: Option<ActivityId>,
pub workspace_role: Option<String>,
pub global_role: Option<String>,
}
Expand Down Expand Up @@ -117,6 +120,7 @@ impl DescriptorAuthTable {
let workspace_role_str = string_field(auth_msg, "workspace_role");
let global_role_str = string_field(auth_msg, "global_role");
let scope_str = string_field(auth_msg, "scope");
let audit_str = string_field(auth_msg, "audit");

let auth_mode = match auth_mode_str.as_str() {
"unauthenticated" => AuthMode::Unauthenticated,
Expand All @@ -143,11 +147,14 @@ impl DescriptorAuthTable {
scope.as_deref(),
)?;

let audit = audit_activity(&path, scope.as_deref(), &audit_str)?;

entries.insert(
path,
DescriptorAuthEntry {
auth_mode,
scope,
audit,
workspace_role,
global_role,
},
Expand Down Expand Up @@ -222,6 +229,35 @@ fn validate_entry(
Ok(())
}

/// The audit activity a method declares. A write-scoped method must declare
/// one; `read` marks a read the gateway audits because it returns secrets.
fn audit_activity(
path: &str,
scope: Option<&str>,
audit: &str,
) -> Result<Option<ActivityId>, String> {
let activity = match audit {
"" => None,
"create" => Some(ActivityId::Open),
"update" => Some(ActivityId::Reset),
"delete" => Some(ActivityId::Fail),
"other" => Some(ActivityId::Other),
"read" => Some(ActivityId::Close),
other => {
return Err(format!(
"method {path}: unknown audit '{other}' (expected one of create, update, delete, other, read)"
));
}
};
let writes = scope.is_some_and(|scope| scope.ends_with(":write"));
if writes && matches!(audit, "" | "read") {
return Err(format!(
"method {path}: write-scoped method must declare audit create, update, delete, or other"
));
}
Ok(activity)
}

fn string_field(msg: &prost_reflect::DynamicMessage, name: &str) -> String {
msg.get_field_by_name(name)
.and_then(|v| match &*v {
Expand Down Expand Up @@ -323,6 +359,21 @@ mod tests {
);
}

#[test]
fn write_scoped_rpc_must_declare_audit() {
let err = audit_activity(FUTURE_RPC, Some("sandbox:write"), "")
.expect_err("write-scoped RPC without audit must be rejected");
assert!(err.contains("must declare audit"));
assert!(audit_activity(FUTURE_RPC, Some("sandbox:write"), "read").is_err());
assert!(audit_activity(FUTURE_RPC, Some("sandbox:write"), "create").is_ok());
assert_eq!(
audit_activity(FUTURE_RPC, Some("sandbox:read"), "").unwrap(),
None
);
assert!(audit_activity(FUTURE_RPC, None, "read").unwrap().is_some());
assert!(audit_activity(FUTURE_RPC, None, "bogus").is_err());
}

#[test]
fn bearer_rpc_requires_scope() {
let missing_scope = validate_entry(FUTURE_RPC, AuthMode::Bearer, Some("user"), None, None)
Expand Down
5 changes: 3 additions & 2 deletions proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ identifies the service being exposed.
wire identity and may therefore differ from declaration order.
- Document authorization-sensitive selector variants and omission semantics on
the containing request.
- Give an RPC that changes gateway state a `*:write` scope and add it to the
audit catalog in `crates/openshell-server/src/auth/audit/catalog.rs`.
- Give an RPC that changes gateway state a `*:write` scope and an `audit`
activity (`create`, `update`, `delete`, or `other`) in its authorization
option. The gateway refuses to start without one.

## Schema evolution

Expand Down
Loading
Loading