Skip to content

Commit 1c1f08c

Browse files
author
cleissonbr
committed
Fix: review change requests
1 parent c549722 commit 1c1f08c

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Cedar Agent configuration is available using environment variables and command l
9898
- Load data from json file. Defaults to `None`.
9999
`DATA` environment variable.
100100
`--data`, `-d` command line argument.
101+
- Load policies from json file. Defaults to `None`.
102+
`POLICIES` environment variable.
103+
`--policies`, `-p` command line argument.
101104

102105
**command line arguments take precedence over environment variables when configuring the Cedar Agent**
103106

src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Config {
2222
#[arg(short, long)]
2323
pub data: Option<PathBuf>,
2424
#[arg(long)]
25-
pub policy: Option<PathBuf>,
25+
pub policies: Option<PathBuf>,
2626
}
2727

2828
impl Into<rocket::figment::Figment> for &Config {
@@ -42,8 +42,8 @@ impl Into<rocket::figment::Figment> for &Config {
4242
if let Some(data) = self.data.borrow() {
4343
config = config.merge(("data", data));
4444
}
45-
if let Some(policy) = self.policy.borrow() {
46-
config = config.merge(("policy", policy));
45+
if let Some(policies) = self.policies.borrow() {
46+
config = config.merge(("policies", policies));
4747
}
4848

4949
config
@@ -58,7 +58,7 @@ impl Config {
5858
port: None,
5959
log_level: None,
6060
data: None,
61-
policy: None,
61+
policies: None,
6262
}
6363
}
6464

@@ -70,7 +70,7 @@ impl Config {
7070
config.port = c.port.or(config.port);
7171
config.log_level = c.log_level.or(config.log_level);
7272
config.data = c.data.or(config.data);
73-
config.policy = c.policy.or(config.policy);
73+
config.policies = c.policies.or(config.policies);
7474
}
7575

7676
config

src/services/policies/load_from_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ pub struct InitPoliciesFairing;
1717

1818
pub(crate) async fn init(conf: &config::Config, policy_store: &Box<dyn PolicyStore>) {
1919

20-
if conf.policy.is_none() {
20+
if conf.policies.is_none() {
2121
return;
2222
}
2323

24-
let file_path = conf.policy.clone().unwrap();
24+
let file_path = conf.policies.clone().unwrap();
2525
let policies_file_path = &file_path;
2626
let policies = match load_policies_from_file(policies_file_path.to_path_buf()).await {
2727
Ok(policies) => policies,

tests/services/data_tests.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
use std::sync::Arc;
2+
use std::path::PathBuf;
3+
use std::fs::File;
4+
use std::io::Write;
5+
16
use crate::services::utils;
2-
use cedar_agent::data::memory::MemoryDataStore;
37

8+
use cedar_agent::data::memory::MemoryDataStore;
9+
use cedar_agent::data::load_from_file::load_entities_from_file;
410
use cedar_agent::DataStore;
511

612
#[tokio::test]
@@ -18,3 +24,28 @@ async fn memory_tests() {
1824
let entities = store.get_entities().await;
1925
assert_eq!(entities.len(), 0);
2026
}
27+
28+
#[tokio::test]
29+
async fn test_load_entities_from_file() {
30+
let temp_file_path = Arc::new(PathBuf::from("test_entities.json"));
31+
let test_data = r#"[{
32+
"attrs": {},
33+
"parents": [
34+
{
35+
"id": "Admin",
36+
"type": "Role"
37+
}
38+
],
39+
"uid": {
40+
"id": "admin.1@domain.com",
41+
"type": "User"
42+
}
43+
}]"#;
44+
let mut temp_file = File::create(&*temp_file_path.clone()).unwrap();
45+
temp_file.write_all(test_data.as_bytes()).unwrap();
46+
47+
let entities = load_entities_from_file(temp_file_path.clone().to_path_buf()).await.unwrap();
48+
assert_eq!(entities.len(), 1);
49+
50+
std::fs::remove_file(temp_file_path.clone().to_path_buf()).unwrap();
51+
}

tests/services/policies_tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use std::str::FromStr;
2+
use std::sync::Arc;
3+
use std::path::PathBuf;
4+
use std::fs::File;
5+
use std::io::Write;
26

37
use cedar_policy::PolicyId;
48

59
use crate::services::utils::*;
10+
611
use cedar_agent::policies::memory::MemoryPolicyStore;
712
use cedar_agent::schemas::policies::PolicyUpdate;
813
use cedar_agent::PolicyStore;
14+
use cedar_agent::policies::load_from_file::load_policies_from_file;
915

1016
#[tokio::test]
1117
async fn memory_tests() {
@@ -82,3 +88,20 @@ async fn memory_tests() {
8288
.policy(&PolicyId::from_str("test").unwrap())
8389
.is_none());
8490
}
91+
92+
#[tokio::test]
93+
async fn test_load_policies_from_file() {
94+
let temp_file_path = Arc::new(PathBuf::from("test_policies.json"));
95+
let test_data = r#"[{
96+
"id": "test",
97+
"content": "permit(principal in Role::\"Viewer\",action in [Action::\"get\",Action::\"list\"],resource == Document::\"cedar-agent.pdf\");"
98+
}]"#;
99+
let mut temp_file = File::create(&*temp_file_path.clone()).unwrap();
100+
temp_file.write_all(test_data.as_bytes()).unwrap();
101+
102+
let policies = load_policies_from_file(temp_file_path.clone().to_path_buf()).await.unwrap();
103+
assert_eq!(policies.len(), 1);
104+
assert_eq!(policies[0].id, "test".to_string());
105+
106+
std::fs::remove_file(temp_file_path.clone().to_path_buf()).unwrap();
107+
}

0 commit comments

Comments
 (0)