-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-cedarling): pass entities data into the context (#10275)
* feat(jans-cedarling): pass entities data into the context - automatically pass in the `user`, `workload`, `access_token`, `id_token`, and `userinfo_token` entities into the Cedar context to be able to be used for ABAC. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): remove print statement Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): rename add_entities_to_context to build_context Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> --------- Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> Co-authored-by: Oleh <olehbozhok@gmail.com>
- Loading branch information
1 parent
16371ee
commit e2e4f89
Showing
8 changed files
with
291 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use serde_json::Value; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum MergeError { | ||
#[error("Failed to merge JSON objects due to conflicting keys: {0}")] | ||
KeyConflict(String), | ||
} | ||
|
||
pub fn merge_json_values(mut base: Value, other: Value) -> Result<Value, MergeError> { | ||
if let (Some(base_map), Some(additional_map)) = (base.as_object_mut(), other.as_object()) { | ||
for (key, value) in additional_map { | ||
if base_map.contains_key(key) { | ||
return Err(MergeError::KeyConflict(key.clone())); | ||
} | ||
base_map.insert(key.clone(), value.clone()); | ||
} | ||
} | ||
Ok(base) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use serde_json::json; | ||
|
||
use crate::authz::merge_json::MergeError; | ||
|
||
use super::merge_json_values; | ||
|
||
#[test] | ||
fn can_merge_json_objects() { | ||
let obj1 = json!({ "a": 1, "b": 2 }); | ||
let obj2 = json!({ "c": 3, "d": 4 }); | ||
let expected = json!({"a": 1, "b": 2, "c": 3, "d": 4}); | ||
|
||
let result = merge_json_values(obj1, obj2).expect("Should merge JSON objects"); | ||
|
||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn errors_on_same_keys() { | ||
// Test for only two objects | ||
let obj1 = json!({ "a": 1, "b": 2 }); | ||
let obj2 = json!({ "b": 3, "c": 4 }); | ||
let result = merge_json_values(obj1, obj2); | ||
|
||
assert!( | ||
matches!(result, Err(MergeError::KeyConflict(key)) if key.as_str() == "b"), | ||
"Expected an error due to conflicting keys" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.