use crate::interceptor::Cloud;
const AWS_IDENTITY_SERVICES: [&str; 12] = [
"iam",
"sts",
"organizations",
"sso",
"sso-oauth",
"awsssoportal",
"identitystore",
"cloudformation",
"cloudcontrolapi",
"cognito-identity",
"rolesanywhere",
"ram",
];
const GCP_IDENTITY_SERVICES: [&str; 5] = [
"iam",
"iamcredentials",
"sts",
"cloudidentity",
"deploymentmanager",
];
const AZURE_IDENTITY_PROVIDERS: [&str; 3] = [
"microsoft.authorization",
"microsoft.managedidentity",
"microsoft.managedservices",
];
pub fn is_identity_call(
cloud: Cloud,
services: &[&str],
method: &str,
segments: &[&str],
query: &[(String, String)],
) -> bool {
let writes = !matches!(method, "GET" | "HEAD");
let batch = segments.first() == Some(&"batch");
match cloud {
Cloud::Aws => services
.iter()
.any(|name| AWS_IDENTITY_SERVICES.contains(name)),
Cloud::Gcp => {
services
.iter()
.any(|name| GCP_IDENTITY_SERVICES.contains(name))
|| batch
|| segments.iter().any(|segment| segment.contains("setiampolicy"))
|| (services.contains(&"storage")
&& (segments.contains(&"hmackeys")
|| (writes
&& (segments.last() == Some(&"iam")
|| segments
.iter()
.any(|s| matches!(*s, "acl" | "defaultobjectacl"))))
|| query
.iter()
.any(|(name, _)| name.to_ascii_lowercase().contains("predefined"))))
}
Cloud::Azure => {
batch
|| segments.windows(2).any(|pair| {
pair[0] == "providers" && AZURE_IDENTITY_PROVIDERS.contains(&pair[1])
})
|| segments.windows(3).any(|w| {
w[0] == "providers"
&& w[1] == "microsoft.resources"
&& w[2].starts_with("deployment")
})
}
Cloud::Kubernetes => false,
}
}
pub fn decoded(path: &str) -> String {
let bytes = path.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut at = 0;
while at < bytes.len() {
let escaped = (bytes[at] == b'%')
.then(|| bytes.get(at + 1..at + 3))
.flatten()
.and_then(|hex| u8::from_str_radix(std::str::from_utf8(hex).ok()?, 16).ok());
match escaped {
Some(byte) => {
out.push(byte);
at += 3;
}
None => {
out.push(bytes[at]);
at += 1;
}
}
}
String::from_utf8_lossy(&out).into_owned()
}