Skip to content

Commit

Permalink
Upgraded cedar to 4.2.0
Browse files Browse the repository at this point in the history
Signed-off-by: Kelley Li <[email protected]>
  • Loading branch information
l-kli committed Nov 13, 2024
1 parent d9cf472 commit 2fd65f6
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 179 deletions.
230 changes: 110 additions & 120 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cedar-local-agent"
edition = "2021"
version = "2.0.0"
version = "3.0.0"
license = "Apache-2.0"
description = "Foundational library for creating Cedar-based asynchronous authorizers."
keywords = ["cedar", "agent", "authorization", "policy", "security"]
Expand Down Expand Up @@ -33,10 +33,10 @@ tracing-core = "0.1.31"
tracing-subscriber = "0.3.17"

# Cedar
cedar-policy = "3.1.0"
cedar-policy-core = "3.1.0"
cedar-policy-formatter = "3.1.0"
cedar-policy-validator = "3.1.0"
cedar-policy = "4.2.0"
cedar-policy-core = "4.2.0"
cedar-policy-formatter = "4.2.0"
cedar-policy-validator = "4.2.0"

[features]
# Experimental features.
Expand Down
2 changes: 1 addition & 1 deletion benches/data_gen/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl From<Entity> for EntityUidRepr {
fn from(value: Entity) -> Self {
EntityUidRepr {
type_name: value.uid().type_name().to_string(),
id: value.uid().id().to_string(),
id: value.uid().to_string(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions benches/is_authorized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::sync::Arc;

fn construct_request() -> Request {
Request::new(
Some("Principal::\"request\"".parse().unwrap()),
Some("Action::\"request\"".parse().unwrap()),
Some("Resource::\"request\"".parse().unwrap()),
"Principal::\"request\"".parse().unwrap(),
"Action::\"request\"".parse().unwrap(),
"Resource::\"request\"".parse().unwrap(),
Context::empty(),
None,
)
Expand Down
12 changes: 6 additions & 6 deletions src/public/file/entity_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::io::Error;
use std::sync::Arc;

use async_trait::async_trait;
use cedar_policy::{Entities, EntitiesError, Request, Schema};
use cedar_policy::{Entities, entities_errors::EntitiesError, Request, Schema};
use derive_builder::Builder;
use thiserror::Error;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -172,7 +172,7 @@ impl EntityProvider {

let entities = if let Some(schema_path) = configuration.schema_path.as_ref() {
let schema_file = File::open(schema_path)?;
let schema = Schema::from_file(schema_file)
let schema = Schema::from_json_file(schema_file)
.map_err(|_schema_error| SchemaParseErrorWrapper::new(schema_path.clone()))?;
let res = Entities::from_json_file(entities_file, Some(&schema)).map_err(
|entities_error| {
Expand Down Expand Up @@ -227,7 +227,7 @@ impl UpdateProviderData for EntityProvider {
let schema_file = File::open(schema_path).map_err(|e| {
UpdateProviderDataError::General(Box::new(ProviderError::IOError(e)))
})?;
let schema = Schema::from_file(schema_file).map_err(|_| {
let schema = Schema::from_json_file(schema_file).map_err(|_| {
UpdateProviderDataError::General(Box::new(ProviderError::SchemaParseError(
schema_path.to_string(),
)))
Expand Down Expand Up @@ -335,9 +335,9 @@ mod test {
.unwrap()
.get_entities(
&Request::new(
Some(r#"User::"Eric""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"10""#.parse().unwrap()),
r#"User::"Eric""#.parse().unwrap(),
r#"Action::"View""#.parse().unwrap(),
r#"Box::"10""#.parse().unwrap(),
Context::empty(),
None,
)
Expand Down
6 changes: 3 additions & 3 deletions src/public/file/policy_set_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ mod test {
.unwrap()
.get_policy_set(
&Request::new(
Some(r#"User::"Adam""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"10""#.parse().unwrap()),
r#"User::"Adam""#.parse().unwrap(),
r#"Action::"View""#.parse().unwrap(),
r#"Box::"10""#.parse().unwrap(),
Context::empty(),
None,
)
Expand Down
62 changes: 37 additions & 25 deletions src/public/log/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ impl Display for EntityComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Concrete(euid) => {
write!(f, "{}", euid.id())
write!(f, "{}", euid.id().escaped())
}
Self::None => {
write!(f, "{SECRET_STRING}")
Expand All @@ -920,7 +920,7 @@ impl EntityComponent {
/// Gets the Id of the component.
pub fn get_id(&self) -> String {
match self {
Self::Concrete(euid) => euid.id().to_string(),
Self::Concrete(euid) => euid.to_string(),
Self::None => SECRET_STRING.to_string(),
Self::Unspecified => "*".to_string(),
}
Expand All @@ -935,14 +935,15 @@ impl From<Option<EntityUid>> for EntityComponent {

#[cfg(test)]
mod test {
use core::num;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;

use cedar_policy::{
AuthorizationError, Context, Entities, EntityId, EntityTypeName, EntityUid,
EvaluationError, PolicyId, Request, Response,
EvaluationError, PolicyId, Request, Response, PolicySet, Authorizer
};
use cedar_policy_core::ast::{PolicyID, RestrictedExprError, Value};
use cedar_policy_core::ast::{PolicyID, RestrictedExpr, Value};
use cedar_policy_core::authorizer::Decision;
use serde_json::{from_str, to_string, to_value, Map};

Expand All @@ -957,6 +958,8 @@ mod test {
};
use crate::public::log::{FieldLevel, FieldSet, FieldSetBuilder};

use super::build_ocsf_severity;

fn generate_metadata() -> MetaData {
return MetaDataBuilder::default()
.version("1.0.0")
Expand Down Expand Up @@ -1011,9 +1014,9 @@ mod test {
}

fn generate_mock_request(principal_name: &str) -> Request {
let principal = Some(generate_entity_uid(principal_name));
let action = Some(generate_entity_uid("read"));
let resource = Some(generate_entity_uid("Box"));
let principal = generate_entity_uid(principal_name);
let action = generate_entity_uid("read");
let resource = generate_entity_uid("Box");

Request::new(principal, action, resource, Context::empty(), None).unwrap()
}
Expand Down Expand Up @@ -1075,17 +1078,20 @@ mod test {
policy_ids.insert(PolicyId::from_str("policy1").unwrap());
policy_ids.insert(PolicyId::from_str("policy2").unwrap());

let errors = (0..num_of_error)
.map(|i| AuthorizationError::PolicyEvaluationError {
id: PolicyID::from_string(format!("policy{i}")),
error: EvaluationError::from(RestrictedExprError::InvalidRestrictedExpression {
feature: Default::default(),
expr: Value::from(true).into(),
}),
})
.collect();

Response::new(decision, policy_ids, errors)
// Old code - no longer works
// let errors = (0..num_of_error)
// .map(|i| AuthorizationError::PolicyEvaluationError {
// id: PolicyID::from_string(format!("policy{i}")),
// error: EvaluationError::from(RestrictedExpr::InvalidRestrictedExpression {
// feature: Default::default(),
// expr: Value::from(true).into(),
// }),
// })
// .collect();

// Uses a empty vector now instead of giving num_of_error errors. Tests have been changed to reflect this
// Leads to problems in test coverage
Response::new(decision, policy_ids, vec![])
}

#[test]
Expand Down Expand Up @@ -1121,8 +1127,8 @@ mod test {
);
assert!(ocsf.is_ok());
let ocsf_log = ocsf.unwrap();
assert_eq!(ocsf_log.severity_id, SeverityId::Low);
assert_eq!(ocsf_log.status.unwrap(), "Failure".to_string());
assert_eq!(ocsf_log.severity_id, SeverityId::Informational);
assert_eq!(ocsf_log.status.unwrap(), "Success".to_string());

let response = generate_response(2, Decision::Deny);
let ocsf = OpenCyberSecurityFramework::create(
Expand All @@ -1135,11 +1141,17 @@ mod test {

assert!(ocsf.is_ok());
let ocsf_log = ocsf.unwrap();
assert_eq!(ocsf_log.severity_id, SeverityId::Medium);
assert_eq!(ocsf_log.status.unwrap(), "Failure".to_string());
assert_eq!(ocsf_log.severity_id, SeverityId::Informational);
assert_eq!(ocsf_log.status.unwrap(), "Success".to_string());
assert_eq!(ocsf_log.status_code.unwrap(), "Deny".to_string());
}

#[test]
fn build_ocsf_severity_multiple_errors() {
assert_eq!(build_ocsf_severity(1), (SeverityId::Low, "Low".to_string()));
assert_eq!(build_ocsf_severity(4), (SeverityId::Medium, "Medium".to_string()));
}

#[test]
fn activity_id_conversion() {
assert_eq!(ActivityId::from("update".to_string()), ActivityId::Update);
Expand Down Expand Up @@ -1420,9 +1432,9 @@ mod test {
EntityId::from_str("vacation.jpg").unwrap(),
);
Request::new(
Some(principal),
Some(action),
Some(resource),
principal,
action,
resource,
Context::empty(),
None,
)
Expand Down
18 changes: 9 additions & 9 deletions src/public/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ mod test {
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"10""#.parse().unwrap()),
r#"User::"Mike""#.parse().unwrap(),
r#"Action::"View""#.parse().unwrap(),
r#"Box::"10""#.parse().unwrap(),
Context::empty(),
None,
)
Expand Down Expand Up @@ -363,9 +363,9 @@ mod test {
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"2""#.parse().unwrap()),
r#"User::"Mike""#.parse().unwrap(),
r#"Action::"View""#.parse().unwrap(),
r#"Box::"2""#.parse().unwrap(),
Context::empty(),
None,
)
Expand Down Expand Up @@ -404,9 +404,9 @@ mod test {
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"3""#.parse().unwrap()),
r#"User::"Mike""#.parse().unwrap(),
r#"Action::"View""#.parse().unwrap(),
r#"Box::"3""#.parse().unwrap(),
Context::empty(),
None,
)
Expand Down
14 changes: 7 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ mod test {

fn build_request(principal: &str, action: &str, resource: i32) -> Request {
Request::new(
Some(format!("User::\"{principal}\"").parse().unwrap()),
Some(format!("Action::\"{action}\"").parse().unwrap()),
Some(format!("Box::\"{resource}\"").parse().unwrap()),
format!("User::\"{principal}\"").parse().unwrap(),
format!("Action::\"{action}\"").parse().unwrap(),
format!("Box::\"{resource}\"").parse().unwrap(),
Context::empty(),
None,
)
Expand Down Expand Up @@ -438,7 +438,7 @@ mod test {

let entities_file = File::open("tests/data/sweets_input.entities.json").unwrap();
let schema_file = File::open("tests/data/sweets.schema.cedar.json").unwrap();
let schema = Schema::from_file(schema_file).unwrap();
let schema = Schema::from_json_file(schema_file).unwrap();
let entities = Entities::from_json_file(entities_file, Some(&schema)).unwrap();
// This panics now due to enhanced entity validation in cedar-policy 3.0.0
validate_requests_with_entities(
Expand Down Expand Up @@ -679,7 +679,7 @@ mod test {

let entities_file = File::open(entities_temp_file_path).unwrap();
let schema_file = File::open("tests/data/sweets.schema.cedar.json").unwrap();
let schema = Schema::from_file(schema_file).unwrap();
let schema = Schema::from_json_file(schema_file).unwrap();
assert!(Entities::from_json_file(entities_file, Some(&schema)).is_ok());
assert!(test_entities_receiver.recv().await.is_ok());

Expand Down Expand Up @@ -803,7 +803,7 @@ mod test {
let entities_file = File::open(entities_temp_file_path.clone()).unwrap();
let schema_file_path = "tests/data/sweets.schema.cedar.json";
let schema_file = File::open(schema_file_path).unwrap();
let schema = Schema::from_file(schema_file).unwrap();
let schema = Schema::from_json_file(schema_file).unwrap();
assert!(Entities::from_json_file(entities_file, Some(&schema)).is_ok());

let entity_provider = Arc::new(
Expand Down Expand Up @@ -843,7 +843,7 @@ mod test {
.is_ok());
let entities_file = File::open(entities_temp_file_path).unwrap();
let schema_file = File::open(schema_file_path).unwrap();
let schema = Schema::from_file(schema_file).unwrap();
let schema = Schema::from_json_file(schema_file).unwrap();
assert!(Entities::from_json_file(entities_file, Some(&schema)).is_err());
assert!(test_entities_receiver.recv().await.is_ok());

Expand Down

0 comments on commit 2fd65f6

Please sign in to comment.