use crate::config::TagMapping;
use crate::auth::VerifiedClaims;
pub(crate) fn json_value_to_string(v: &serde_json::Value) -> String {
match v {
serde_json::Value::String(s) => s.clone(),
other => other.to_string(),
}
}
pub struct TagMapper {
mappings: Vec<TagMapping>,
}
impl TagMapper {
pub fn new(mappings: Vec<TagMapping>) -> Self {
Self { mappings }
}
pub fn map(&self, claims: &VerifiedClaims) -> Vec<String> {
let mut tags = Vec::new();
for mapping in &self.mappings {
if mapping.match_rule.issuer != claims.issuer_name {
continue;
}
let all_claims_match = mapping.match_rule.claims.iter().all(|(key, pattern)| {
match claims.claims.get(key) {
Some(value) => {
let value_str = json_value_to_string(value);
glob_match::glob_match(pattern, &value_str)
}
None => false,
}
});
if all_claims_match {
tags.extend(mapping.tags.iter().cloned());
}
}
tags
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::TagMatchRule;
use std::collections::HashMap;
fn make_claims(issuer_name: &str, claims: Vec<(&str, &str)>) -> VerifiedClaims {
let mut claim_map = HashMap::new();
for (k, v) in claims {
claim_map.insert(k.to_string(), serde_json::Value::String(v.to_string()));
}
VerifiedClaims {
issuer_name: issuer_name.to_string(),
claims: claim_map,
}
}
fn make_mapping(issuer: &str, claims: &[(&str, &str)], tags: &[&str]) -> TagMapping {
TagMapping {
match_rule: TagMatchRule {
issuer: issuer.to_string(),
claims: claims.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(),
},
tags: tags.iter().map(|t| t.to_string()).collect(),
}
}
#[test]
fn test_exact_claim_match() {
let mapper = TagMapper::new(vec![
make_mapping("github-actions", &[("repository", "myorg/my-repo")], &["team:platform"]),
]);
let tags = mapper.map(&make_claims("github-actions", vec![("repository", "myorg/my-repo")]));
assert_eq!(tags, vec!["team:platform"]);
}
#[test]
fn test_glob_claim_match() {
let mapper = TagMapper::new(vec![
make_mapping("github-actions", &[("repository", "myorg/*")], &["team:platform"]),
]);
let tags = mapper.map(&make_claims("github-actions", vec![("repository", "myorg/any-repo")]));
assert_eq!(tags, vec!["team:platform"]);
}
#[test]
fn test_no_match_returns_empty() {
let mapper = TagMapper::new(vec![
make_mapping("github-actions", &[("repository", "otherorg/*")], &["team:platform"]),
]);
let tags = mapper.map(&make_claims("github-actions", vec![("repository", "myorg/my-repo")]));
assert!(tags.is_empty());
}
#[test]
fn test_multiple_mappings_accumulate_tags() {
let mapper = TagMapper::new(vec![
make_mapping("github-actions", &[("repository", "myorg/*")], &["team:platform"]),
make_mapping("github-actions", &[("ref", "refs/heads/main")], &["env:prod"]),
]);
let tags = mapper.map(&make_claims(
"github-actions",
vec![("repository", "myorg/my-repo"), ("ref", "refs/heads/main")],
));
assert_eq!(tags, vec!["team:platform", "env:prod"]);
}
#[test]
fn test_wrong_issuer_no_match() {
let mapper = TagMapper::new(vec![
make_mapping("kubernetes", &[("repository", "myorg/*")], &["team:platform"]),
]);
let tags = mapper.map(&make_claims("github-actions", vec![("repository", "myorg/my-repo")]));
assert!(tags.is_empty());
}
}