use crate::graph::{AuthorityGraph, EdgeKind, NodeKind};
#[derive(Debug)]
pub struct MapRow {
pub step_name: String,
pub trust_zone: String,
pub access: Vec<bool>,
}
#[derive(Debug)]
pub struct AuthorityMap {
pub authorities: Vec<String>,
pub rows: Vec<MapRow>,
}
pub fn authority_map(graph: &AuthorityGraph) -> AuthorityMap {
let authorities: Vec<_> = graph
.authority_sources()
.map(|n| (n.id, n.name.clone()))
.collect();
let authority_names: Vec<String> = authorities.iter().map(|(_, name)| name.clone()).collect();
let mut rows = Vec::new();
for step in graph.nodes_of_kind(NodeKind::Step) {
let mut access = vec![false; authorities.len()];
for edge in graph.edges_from(step.id) {
if edge.kind != EdgeKind::HasAccessTo {
continue;
}
if let Some(idx) = authorities.iter().position(|(id, _)| *id == edge.to) {
access[idx] = true;
}
}
rows.push(MapRow {
step_name: step.name.clone(),
trust_zone: format!("{:?}", step.trust_zone),
access,
});
}
AuthorityMap {
authorities: authority_names,
rows,
}
}
pub fn render_map(map: &AuthorityMap) -> String {
if map.rows.is_empty() && map.authorities.is_empty() {
return "No steps or authority sources found.\n".to_string();
}
let step_width = map
.rows
.iter()
.map(|r| r.step_name.len())
.max()
.unwrap_or(4)
.max(4);
let zone_width = map
.rows
.iter()
.map(|r| r.trust_zone.len())
.max()
.unwrap_or(4)
.max(4);
let auth_widths: Vec<usize> = map.authorities.iter().map(|a| a.len().max(3)).collect();
let mut out = String::new();
out.push_str(&format!(
"{:<step_w$} {:<zone_w$}",
"Step",
"Zone",
step_w = step_width,
zone_w = zone_width
));
for (i, auth) in map.authorities.iter().enumerate() {
out.push_str(&format!(" {:^w$}", auth, w = auth_widths[i]));
}
out.push('\n');
out.push_str(&"-".repeat(step_width));
out.push_str(" ");
out.push_str(&"-".repeat(zone_width));
for w in &auth_widths {
out.push_str(" ");
out.push_str(&"-".repeat(*w));
}
out.push('\n');
for row in &map.rows {
out.push_str(&format!(
"{:<step_w$} {:<zone_w$}",
row.step_name,
row.trust_zone,
step_w = step_width,
zone_w = zone_width
));
for (i, &has) in row.access.iter().enumerate() {
let marker = if has { "X" } else { "." };
out.push_str(&format!(" {:^w$}", marker, w = auth_widths[i]));
}
out.push('\n');
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::*;
fn source(file: &str) -> PipelineSource {
PipelineSource {
file: file.into(),
repo: None,
git_ref: None,
}
}
#[test]
fn map_shows_step_access() {
let mut g = AuthorityGraph::new(source("ci.yml"));
let secret = g.add_node(NodeKind::Secret, "API_KEY", TrustZone::FirstParty);
let token = g.add_node(NodeKind::Identity, "GITHUB_TOKEN", TrustZone::FirstParty);
let build = g.add_node(NodeKind::Step, "build", TrustZone::FirstParty);
let deploy = g.add_node(NodeKind::Step, "deploy", TrustZone::ThirdParty);
g.add_edge(build, secret, EdgeKind::HasAccessTo);
g.add_edge(build, token, EdgeKind::HasAccessTo);
g.add_edge(deploy, token, EdgeKind::HasAccessTo);
let map = authority_map(&g);
assert_eq!(map.authorities.len(), 2);
assert_eq!(map.rows.len(), 2);
let build_row = &map.rows[0];
assert!(build_row.access[0]); assert!(build_row.access[1]);
let deploy_row = &map.rows[1];
assert!(!deploy_row.access[0]); assert!(deploy_row.access[1]); }
#[test]
fn map_renders_table() {
let mut g = AuthorityGraph::new(source("ci.yml"));
let secret = g.add_node(NodeKind::Secret, "KEY", TrustZone::FirstParty);
let step = g.add_node(NodeKind::Step, "build", TrustZone::FirstParty);
g.add_edge(step, secret, EdgeKind::HasAccessTo);
let map = authority_map(&g);
let table = render_map(&map);
assert!(table.contains("build"));
assert!(table.contains("KEY"));
assert!(table.contains("X"));
}
}