|
1 |
| -use serde::{Deserialize, Serialize}; |
| 1 | +use std::{borrow::Cow, collections::HashMap, fmt}; |
| 2 | + |
| 3 | +use serde::{ |
| 4 | + de::{Error as DeError, MapAccess, Visitor}, |
| 5 | + Deserialize, Deserializer, Serialize, |
| 6 | +}; |
2 | 7 |
|
3 | 8 | /// `IamPolicyDocument` represents an IAM policy document.
|
4 | 9 | #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
|
5 |
| -#[serde(rename_all = "camelCase")] |
| 10 | +#[serde(rename_all = "PascalCase")] |
6 | 11 | pub struct IamPolicyDocument {
|
7 | 12 | #[serde(default)]
|
8 |
| - #[serde(rename = "Version")] |
9 | 13 | pub version: Option<String>,
|
10 |
| - #[serde(rename = "Statement")] |
11 | 14 | pub statement: Vec<IamPolicyStatement>,
|
12 | 15 | }
|
13 | 16 |
|
14 |
| -/// `IamPolicyStatement` represents one statement from IAM policy with action, effect and resource. |
15 |
| -#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] |
16 |
| -#[serde(rename_all = "camelCase")] |
| 17 | +/// `IamPolicyStatement` represents one statement from IAM policy with action, effect and resource |
| 18 | +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] |
| 19 | +#[serde(rename_all = "PascalCase")] |
17 | 20 | pub struct IamPolicyStatement {
|
18 |
| - #[serde(rename = "Action")] |
| 21 | + #[serde(deserialize_with = "deserialize_string_or_slice")] |
19 | 22 | pub action: Vec<String>,
|
20 |
| - #[serde(default)] |
21 |
| - #[serde(rename = "Effect")] |
22 |
| - pub effect: Option<String>, |
23 |
| - #[serde(rename = "Resource")] |
| 23 | + #[serde(default = "default_statement_effect")] |
| 24 | + pub effect: IamPolicyEffect, |
| 25 | + #[serde(deserialize_with = "deserialize_string_or_slice")] |
24 | 26 | pub resource: Vec<String>,
|
| 27 | + #[serde(default, deserialize_with = "deserialize_policy_condition")] |
| 28 | + pub condition: Option<IamPolicyCondition>, |
| 29 | +} |
| 30 | + |
| 31 | +pub type IamPolicyCondition = HashMap<String, HashMap<String, Vec<String>>>; |
| 32 | + |
| 33 | +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] |
| 34 | +pub enum IamPolicyEffect { |
| 35 | + #[default] |
| 36 | + Allow, |
| 37 | + Deny, |
| 38 | +} |
| 39 | + |
| 40 | +fn default_statement_effect() -> IamPolicyEffect { |
| 41 | + IamPolicyEffect::Allow |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(serde::Deserialize)] |
| 45 | +#[serde(untagged)] |
| 46 | +enum StringOrSlice { |
| 47 | + String(String), |
| 48 | + Slice(Vec<String>), |
| 49 | +} |
| 50 | + |
| 51 | +/// Deserializes `Vec<String>`, from a JSON `string` or `[string]`. |
| 52 | +fn deserialize_string_or_slice<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error> |
| 53 | +where |
| 54 | + D: Deserializer<'de>, |
| 55 | +{ |
| 56 | + let string_or_slice = StringOrSlice::deserialize(deserializer)?; |
| 57 | + |
| 58 | + match string_or_slice { |
| 59 | + StringOrSlice::Slice(slice) => Ok(slice), |
| 60 | + StringOrSlice::String(s) => Ok(vec![s]), |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn deserialize_policy_condition<'de, D>(de: D) -> Result<Option<IamPolicyCondition>, D::Error> |
| 65 | +where |
| 66 | + D: Deserializer<'de>, |
| 67 | +{ |
| 68 | + de.deserialize_option(IamPolicyConditionVisitor) |
| 69 | +} |
| 70 | + |
| 71 | +struct IamPolicyConditionVisitor; |
| 72 | + |
| 73 | +impl<'de> Visitor<'de> for IamPolicyConditionVisitor { |
| 74 | + type Value = Option<IamPolicyCondition>; |
| 75 | + |
| 76 | + // Format a message stating what data this Visitor expects to receive. |
| 77 | + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 78 | + formatter.write_str("lots of things can go wrong with a IAM Policy Condition") |
| 79 | + } |
| 80 | + |
| 81 | + fn visit_unit<E>(self) -> Result<Self::Value, E> |
| 82 | + where |
| 83 | + E: DeError, |
| 84 | + { |
| 85 | + Ok(None) |
| 86 | + } |
| 87 | + |
| 88 | + fn visit_none<E>(self) -> Result<Self::Value, E> |
| 89 | + where |
| 90 | + E: DeError, |
| 91 | + { |
| 92 | + Ok(None) |
| 93 | + } |
| 94 | + |
| 95 | + fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 96 | + where |
| 97 | + D: Deserializer<'de>, |
| 98 | + { |
| 99 | + deserializer.deserialize_map(self) |
| 100 | + } |
| 101 | + |
| 102 | + fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error> |
| 103 | + where |
| 104 | + M: MapAccess<'de>, |
| 105 | + { |
| 106 | + let mut map = HashMap::with_capacity(access.size_hint().unwrap_or(0)); |
| 107 | + |
| 108 | + while let Some((key, val)) = access.next_entry::<Cow<'_, str>, HashMap<Cow<'_, str>, StringOrSlice>>()? { |
| 109 | + let mut value = HashMap::with_capacity(val.len()); |
| 110 | + for (val_key, string_or_slice) in val { |
| 111 | + let val = match string_or_slice { |
| 112 | + StringOrSlice::Slice(slice) => slice, |
| 113 | + StringOrSlice::String(s) => vec![s], |
| 114 | + }; |
| 115 | + value.insert(val_key.into_owned(), val); |
| 116 | + } |
| 117 | + |
| 118 | + map.insert(key.into_owned(), value); |
| 119 | + } |
| 120 | + |
| 121 | + Ok(Some(map)) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[cfg(test)] |
| 126 | +mod tests { |
| 127 | + use super::*; |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_deserialize_string_condition() { |
| 131 | + let data = serde_json::json!({ |
| 132 | + "condition": { |
| 133 | + "StringEquals": { |
| 134 | + "iam:RegisterSecurityKey": "Activate", |
| 135 | + "iam:FIDO-certification": "L1plus" |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + #[derive(Deserialize)] |
| 141 | + struct Test { |
| 142 | + #[serde(deserialize_with = "deserialize_policy_condition")] |
| 143 | + condition: Option<IamPolicyCondition>, |
| 144 | + } |
| 145 | + |
| 146 | + let test: Test = serde_json::from_value(data).unwrap(); |
| 147 | + let condition = test.condition.unwrap(); |
| 148 | + assert_eq!(1, condition.len()); |
| 149 | + |
| 150 | + assert_eq!(vec!["Activate"], condition["StringEquals"]["iam:RegisterSecurityKey"]); |
| 151 | + assert_eq!(vec!["L1plus"], condition["StringEquals"]["iam:FIDO-certification"]); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_deserialize_slide_condition() { |
| 156 | + let data = serde_json::json!({ |
| 157 | + "condition": {"StringLike": {"s3:prefix": ["janedoe/*"]}} |
| 158 | + }); |
| 159 | + |
| 160 | + #[derive(Deserialize)] |
| 161 | + struct Test { |
| 162 | + #[serde(deserialize_with = "deserialize_policy_condition")] |
| 163 | + condition: Option<IamPolicyCondition>, |
| 164 | + } |
| 165 | + |
| 166 | + let test: Test = serde_json::from_value(data).unwrap(); |
| 167 | + let condition = test.condition.unwrap(); |
| 168 | + assert_eq!(1, condition.len()); |
| 169 | + |
| 170 | + assert_eq!(vec!["janedoe/*"], condition["StringLike"]["s3:prefix"]); |
| 171 | + } |
25 | 172 | }
|
0 commit comments