1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::{
    helpers::Connection, training_learning_objective::TrainingLearningObjective, Formalize,
};

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
pub struct Goal {
    #[serde(alias = "Name", alias = "NAME")]
    pub name: Option<String>,
    #[serde(alias = "Description", alias = "DESCRIPTION")]
    pub description: Option<String>,
    #[serde(alias = "Tlos", alias = "TLOS")]
    pub tlos: Vec<String>,
}

pub type Goals = HashMap<String, Goal>;

impl Formalize for Goal {
    fn formalize(&mut self) -> Result<()> {
        if self.tlos.is_empty() {
            return Err(anyhow::anyhow!("Goal requires at least one TLO"));
        }
        Ok(())
    }
}

impl Connection<TrainingLearningObjective> for (&String, &Goal) {
    fn validate_connections(&self, potential_tlo_names: &Option<Vec<String>>) -> Result<()> {
        let tlos = &self.1.tlos;

        if let Some(tlo_names) = potential_tlo_names {
            for tlo_name in tlos {
                if !tlo_names.contains(tlo_name) {
                    return Err(anyhow!("TLO \"{tlo_name}\" not found under Scenario TLOs"));
                }
            }
        } else {
            return Err(anyhow!(
                "Goal requires at least one TLO but none found under Scenario"
            ));
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse_sdl;

    #[test]
    fn parses_sdl_with_goals() {
        let sdl = r#"
          name: test-scenario
          description: some-description
          conditions:
            condition-1:
                command: executable/path.sh
                interval: 30
          metrics:
              metric-1:
                  type: MANUAL
                  artifact: true
                  max-score: 10
              metric-2:
                  type: CONDITIONAL
                  max-score: 10
                  condition: condition-1
          vulnerabilities:
              vulnerability-1:
                  name: Some other vulnerability
                  description: some-description
                  technical: false
                  class: CWE-1343
              vulnerability-2:
                  name: Some vulnerability
                  description: some-description
                  technical: false
                  class: CWE-1341
          evaluations:
              evaluation-1:
                  description: some description
                  metrics:
                      - metric-1
                      - metric-2
                  min-score: 50
          tlos:
              tlo-1:
                  description: some description
                  evaluation: evaluation-1
          goals:
            goal-1:
                description: "new goal"
                tlos: 
                  - tlo-1                   
      "#;
        let goals = parse_sdl(sdl).unwrap().goals;
        insta::with_settings!({sort_maps => true}, {
                insta::assert_yaml_snapshot!(goals);
        });
    }

    #[test]
    #[should_panic(expected = "Goal requires at least one TLO but none found under Scenario")]
    fn fails_without_tlos() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            goals:
                goal-1:
                    description: "new goal"
                    tlos: 
                        - tlo-1                   
      "#;
        parse_sdl(sdl).unwrap();
    }

    #[test]
    fn parses_single_goal() {
        let goal_yml = r#"
          description: "new goal"
          tlos: 
            - tlo-1
            - tlo-2
            - tlo-3
        "#;
        serde_yaml::from_str::<Goal>(goal_yml).unwrap();
    }

    #[test]
    #[should_panic(expected = "Goal requires at least one TLO")]
    fn fails_with_empty_tlo_list() {
        let goal_yml = r#"
          description: "new goal"
          tlos:
        "#;
        serde_yaml::from_str::<Goal>(goal_yml)
            .unwrap()
            .formalize()
            .unwrap();
    }
}