in guard/src/commands/rulegen.rs [81:160]
fn gen_rules(cfn_resources: HashMap<String, Value>) -> HashMap<String, HashMap<String, HashSet<String>>> {
// Create hashmap of resource name, property name and property values
// For example, the following template:
//
// {
// "Resources": {
// "NewVolume" : {
// "Type" : "AWS::EC2::Volume",
// "Properties" : {
// "Size" : 500,
// "Encrypted": false,
// "AvailabilityZone" : "us-west-2b"
// }
// },
// "NewVolume2" : {
// "Type" : "AWS::EC2::Volume",
// "Properties" : {
// "Size" : 50,
// "Encrypted": false,
// "AvailabilityZone" : "us-west-2c"
// }
// }
// }
// }
//
//
// The data structure would contain:
// <AWS::EC2::Volume> <Encrypted> <false>
// <Size> <500, 50>
// <AvailabilityZone> <us-west-2c, us-west-2b>
//
//
//
let mut rule_map: HashMap<String, HashMap<String, HashSet<String>>> = HashMap::new();
for (_name, cfn_resource) in cfn_resources {
let props: HashMap<String, Value> =
match serde_json::from_value(cfn_resource["Properties"].clone()) {
Ok(s) => s,
Err(_) => continue,
};
for (prop_name, prop_val) in props {
let stripped_val = match prop_val.as_str() {
Some(v) => String::from(v),
None => prop_val.to_string(),
};
let mut no_newline_stripped_val = stripped_val.trim().replace("\n", "");
// Preserve double quotes for strings.
if prop_val.is_string() {
let test_str = format!("{}{}{}", "\"" , no_newline_stripped_val, "\"");
no_newline_stripped_val = test_str;
}
let resource_name = format!("{}", &cfn_resource["Type"].as_str().unwrap());
if !rule_map.contains_key(&resource_name) {
let value_set: HashSet<String> =
vec![no_newline_stripped_val].into_iter().collect();
let mut property_map = HashMap::new();
property_map.insert(prop_name, value_set);
rule_map.insert(resource_name, property_map);
} else {
let property_map = rule_map.get_mut(&resource_name).unwrap();
if !property_map.contains_key(&prop_name) {
let value_set: HashSet<String> =
vec![no_newline_stripped_val].into_iter().collect();
property_map.insert(prop_name, value_set);
} else {
let value_set = property_map.get_mut(&prop_name).unwrap();
value_set.insert(no_newline_stripped_val);
}
};
}
}
return rule_map;
}