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
39 changes: 16 additions & 23 deletions crates/openshell-server/src/grpc/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use openshell_core::{
settings::{self, SettingValueKind},
};
use openshell_ocsf::{
ConfigStateChangeBuilder, OCSF_TARGET, OcsfEvent, SandboxContext, SeverityId, StateId, StatusId,
ConfigStateChangeBuilder, OcsfEvent, SandboxContext, SeverityId, StateId, StatusId,
};
use openshell_policy::{
PolicyMergeOp, ProviderPolicyLayer, canonicalize_advisor_add_rule, compose_effective_policy,
Expand Down Expand Up @@ -162,7 +162,7 @@ fn emit_gateway_policy_audit_log(
version: i64,
policy_hash: &str,
) {
let message = build_gateway_policy_audit_message(
let event = build_gateway_policy_audit_event(
sandbox_id,
sandbox_name,
state_label,
Expand All @@ -171,11 +171,7 @@ fn emit_gateway_policy_audit_log(
policy_hash,
&[],
);
info!(
target: OCSF_TARGET,
sandbox_id = %sandbox_id,
message = %message
);
openshell_ocsf::ocsf_emit!(event);
}

/// Emit a `CONFIG:APPROVED` audit event for an auto-approval — same event
Expand All @@ -200,7 +196,7 @@ fn emit_gateway_policy_auto_approve_audit_log(
("prover_delta", "empty".to_string()),
("resolved_from", resolved_from.to_string()),
];
let message = build_gateway_policy_audit_message(
let event = build_gateway_policy_audit_event(
sandbox_id,
sandbox_name,
"approved",
Expand All @@ -209,22 +205,18 @@ fn emit_gateway_policy_auto_approve_audit_log(
policy_hash,
&extra,
);
info!(
target: OCSF_TARGET,
sandbox_id = %sandbox_id,
message = %message
);
openshell_ocsf::ocsf_emit!(event);
}

fn build_gateway_policy_audit_message(
fn build_gateway_policy_audit_event(
sandbox_id: &str,
sandbox_name: &str,
state_label: &str,
detail: impl Into<String>,
version: i64,
policy_hash: &str,
extra_fields: &[(&str, String)],
) -> String {
) -> OcsfEvent {
let ctx = SandboxContext {
sandbox_id: sandbox_id.to_string(),
sandbox_name: sandbox_name.to_string(),
Expand All @@ -248,8 +240,7 @@ fn build_gateway_policy_audit_message(
for (key, value) in extra_fields {
builder = builder.unmapped(key, value.clone());
}
let event: OcsfEvent = builder.build();
event.format_shorthand()
builder.build()
}

fn summarize_cli_policy_merge_op(operation: &PolicyMergeOp) -> String {
Expand Down Expand Up @@ -15486,16 +15477,17 @@ mod tests {
}

#[test]
fn build_gateway_policy_audit_message_formats_ocsf_config_line() {
let message = build_gateway_policy_audit_message(
fn build_gateway_policy_audit_event_formats_ocsf_config_line() {
let message = build_gateway_policy_audit_event(
"sb-123",
"demo-sandbox",
"merged",
"gateway merged incremental policy op: add-allow api.github.com:443 [POST /repos/*/issues]",
7,
"sha256:testhash",
&[],
);
)
.format_shorthand();

assert_eq!(
message,
Expand All @@ -15510,21 +15502,22 @@ mod tests {
/// findings" — never "safe" — because the claim is about the prover's
/// reasoning, not the world.
#[test]
fn build_gateway_policy_audit_message_carries_auto_approve_provenance() {
fn build_gateway_policy_audit_event_carries_auto_approve_provenance() {
let extra = [
("auto", "true".to_string()),
("source", "agent_authored".to_string()),
("prover_delta", "empty".to_string()),
];
let message = build_gateway_policy_audit_message(
let message = build_gateway_policy_audit_event(
"sb-123",
"demo-sandbox",
"approved",
"auto-approved: no new prover findings (source=agent_authored) — chunk abc: add-rule x",
12,
"sha256:autohash",
&extra,
);
)
.format_shorthand();
assert!(
message.contains("CONFIG:APPROVED"),
"auto-approval reuses CONFIG:APPROVED; got: {message}"
Expand Down
Loading
Loading