use crate::config::TagMapping;
use crate::jwt::VerifiedClaims;
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 = match value {
serde_json::Value::String(s) => s.clone(),
other => other.to_string(),
};
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: "https://token.actions.githubusercontent.com".to_string(),
issuer_name: issuer_name.to_string(),
subject: "repo:myorg/my-repo:ref:refs/heads/main".to_string(),
audience: vec!["kzagent".to_string()],
claims: claim_map,
}
}
#[test]
fn test_exact_claim_match() {
let mappings = vec![TagMapping {
match_rule: TagMatchRule {
issuer: "github-actions".to_string(),
claims: HashMap::from([("repository".to_string(), "myorg/my-repo".to_string())]),
},
tags: vec!["team:platform".to_string()],
}];
let mapper = TagMapper::new(mappings);
let claims = make_claims("github-actions", vec![("repository", "myorg/my-repo")]);
let tags = mapper.map(&claims);
assert_eq!(tags, vec!["team:platform"]);
}
#[test]
fn test_glob_claim_match() {
let mappings = vec![TagMapping {
match_rule: TagMatchRule {
issuer: "github-actions".to_string(),
claims: HashMap::from([("repository".to_string(), "myorg/*".to_string())]),
},
tags: vec!["team:platform".to_string()],
}];
let mapper = TagMapper::new(mappings);
let claims = make_claims("github-actions", vec![("repository", "myorg/any-repo")]);
let tags = mapper.map(&claims);
assert_eq!(tags, vec!["team:platform"]);
}
#[test]
fn test_no_match_returns_empty() {
let mappings = vec![TagMapping {
match_rule: TagMatchRule {
issuer: "github-actions".to_string(),
claims: HashMap::from([("repository".to_string(), "otherorg/*".to_string())]),
},
tags: vec!["team:platform".to_string()],
}];
let mapper = TagMapper::new(mappings);
let claims = make_claims("github-actions", vec![("repository", "myorg/my-repo")]);
let tags = mapper.map(&claims);
assert!(tags.is_empty());
}
#[test]
fn test_multiple_mappings_accumulate_tags() {
let mappings = vec![
TagMapping {
match_rule: TagMatchRule {
issuer: "github-actions".to_string(),
claims: HashMap::from([("repository".to_string(), "myorg/*".to_string())]),
},
tags: vec!["team:platform".to_string()],
},
TagMapping {
match_rule: TagMatchRule {
issuer: "github-actions".to_string(),
claims: HashMap::from([("ref".to_string(), "refs/heads/main".to_string())]),
},
tags: vec!["env:prod".to_string()],
},
];
let mapper = TagMapper::new(mappings);
let claims = make_claims(
"github-actions",
vec![("repository", "myorg/my-repo"), ("ref", "refs/heads/main")],
);
let tags = mapper.map(&claims);
assert_eq!(tags, vec!["team:platform", "env:prod"]);
}
#[test]
fn test_wrong_issuer_no_match() {
let mappings = vec![TagMapping {
match_rule: TagMatchRule {
issuer: "kubernetes".to_string(),
claims: HashMap::from([("repository".to_string(), "myorg/*".to_string())]),
},
tags: vec!["team:platform".to_string()],
}];
let mapper = TagMapper::new(mappings);
let claims = make_claims("github-actions", vec![("repository", "myorg/my-repo")]);
let tags = mapper.map(&claims);
assert!(tags.is_empty());
}
}