use std::collections::{BTreeMap, HashMap};
use serde_json::Value;
use tracing::warn;
use crate::{cosign::SignatureLayer, errors::Result};
use super::Constraint;
#[derive(Debug)]
pub struct AnnotationMarker {
pub annotations: HashMap<String, String>,
}
impl AnnotationMarker {
pub fn new(annotations: HashMap<String, String>) -> Self {
Self { annotations }
}
}
impl Constraint for AnnotationMarker {
fn add_constraint(&self, signature_layer: &mut SignatureLayer) -> Result<bool> {
let mut annotations = match &signature_layer.simple_signing.optional {
Some(opt) => {
warn!(optional = ?opt, "already has an annotation field");
opt.extra.clone()
}
None => BTreeMap::new(),
};
for (k, v) in &self.annotations {
if annotations.contains_key(k) && annotations[k] != *v {
warn!(key = ?k, "extra field already has a value");
return Ok(false);
}
annotations.insert(k.to_owned(), Value::String(v.into()));
}
let mut opt = signature_layer
.simple_signing
.optional
.clone()
.unwrap_or_default();
opt.extra = annotations;
signature_layer.simple_signing.optional = Some(opt);
Ok(true)
}
}