Skip to content

Commit 17aa87d

Browse files
fix: audit sparsekernel artifact access grants
1 parent f595a81 commit 17aa87d

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

  • crates/sparsekernel-core/src

crates/sparsekernel-core/src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,11 +1502,25 @@ impl SparseKernelDb {
15021502
subject_id: &str,
15031503
permission: &str,
15041504
) -> Result<()> {
1505-
self.conn.execute(
1505+
let inserted = self.conn.execute(
15061506
"INSERT OR IGNORE INTO artifact_access(artifact_id, subject_type, subject_id, permission, created_at)
15071507
VALUES(?, ?, ?, ?, ?)",
15081508
params![artifact_id, subject_type, subject_id, permission, now_iso()],
15091509
)?;
1510+
if inserted > 0 {
1511+
self.record_audit(AuditInput {
1512+
actor_type: Some("runtime".to_string()),
1513+
actor_id: None,
1514+
action: "artifact_access.granted".to_string(),
1515+
object_type: Some("artifact".to_string()),
1516+
object_id: Some(artifact_id.to_string()),
1517+
payload: Some(json!({
1518+
"subjectType": subject_type,
1519+
"subjectId": subject_id,
1520+
"permission": permission,
1521+
})),
1522+
})?;
1523+
}
15101524
Ok(())
15111525
}
15121526

@@ -4035,11 +4049,24 @@ mod tests {
40354049
assert!(store
40364050
.read(&first.id, Some(("agent", "other", "read")))
40374051
.is_err());
4052+
db.grant_artifact_access(&first.id, "agent", "main", "read")
4053+
.unwrap();
40384054
let export_path = dir.path().join("export.txt");
40394055
store
40404056
.export_file(&first.id, &export_path, Some(("agent", "main", "read")))
40414057
.unwrap();
40424058
assert_eq!(fs::read(export_path).unwrap(), b"hello");
4059+
let audit = db.list_audit(10).unwrap();
4060+
assert_eq!(
4061+
audit
4062+
.iter()
4063+
.filter(|event| event.action == "artifact_access.granted")
4064+
.count(),
4065+
1
4066+
);
4067+
assert!(audit
4068+
.iter()
4069+
.any(|event| event.action == "artifact_access.denied"));
40434070
}
40444071

40454072
#[test]

0 commit comments

Comments
 (0)