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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{
    graph::Object,
    models::scope::{Scope, ScopeError},
};
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
    collections::BTreeSet,
    fmt::{Display, Formatter, Result as FmtResult},
    str::FromStr,
};

#[derive(Serialize, PartialOrd, Ord, PartialEq, Eq, Debug, Clone, Deserialize)]
pub struct Role(pub String);
impl Display for Role {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.0)
    }
}

impl FromStr for Role {
    type Err = ScopeError;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Ok(Self(s.to_string()))
    }
}

#[derive(Serialize, PartialOrd, Ord, PartialEq, Eq, Debug, Default, Clone)]
pub struct RoleAssignments(pub BTreeSet<RoleAssignment>);

impl RoleAssignments {
    #[must_use]
    pub fn find(&self, role: &Role, scope: &Scope) -> Option<&RoleAssignment> {
        let scope = scope.0.to_lowercase();
        let role = role.0.to_lowercase();
        self.0
            .iter()
            .find(|v| v.role.0.to_lowercase() == role && v.scope.0.to_lowercase() == scope)
            .or_else(|| {
                self.0.iter().find(|v| {
                    v.role.0.to_lowercase() == role && v.scope_name.to_lowercase() == scope
                })
            })
    }

    #[must_use]
    pub fn contains(&self, entry: &RoleAssignment) -> bool {
        self.0.contains(entry)
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub(crate) fn friendly(&self) -> String {
        self.0
            .iter()
            .map(|x| format!("* {}", x.friendly()))
            .collect::<Vec<_>>()
            .join("\n")
    }

    pub fn insert(&mut self, entry: RoleAssignment) -> bool {
        self.0.insert(entry)
    }

    pub(crate) fn retain<F>(&mut self, f: F)
    where
        F: FnMut(&RoleAssignment) -> bool,
    {
        self.0.retain(f);
    }

    // NOTE: serde_json doesn't panic on failed index slicing, it returns a Value
    // that allows further nested nulls
    #[allow(clippy::indexing_slicing)]
    pub(crate) fn parse(body: &Value, with_principal: bool) -> Result<Self> {
        let Some(values) = body["value"].as_array() else {
            bail!("unable to parse response: missing value array: {body:#?}");
        };

        let mut results = Self::default();
        for entry in values {
            let Some(role) = entry["properties"]["expandedProperties"]["roleDefinition"]
                ["displayName"]
                .as_str()
                .and_then(|x| Role::from_str(x).ok())
            else {
                bail!("no role name: {entry:#?}");
            };

            let Some(scope) = entry["properties"]["expandedProperties"]["scope"]["id"]
                .as_str()
                .and_then(|x| Scope::from_str(x).ok())
            else {
                bail!("no scope id: {entry:#?}");
            };

            let Some(scope_name) = entry["properties"]["expandedProperties"]["scope"]
                ["displayName"]
                .as_str()
                .map(ToString::to_string)
            else {
                bail!("no scope name: {entry:#?}");
            };

            let Some(role_definition_id) = entry["properties"]["roleDefinitionId"]
                .as_str()
                .map(ToString::to_string)
            else {
                bail!("no role definition id: {entry:#?}");
            };

            let (principal_id, principal_type) = if with_principal {
                let principal_id = entry["properties"]["principalId"]
                    .as_str()
                    .map(ToString::to_string);

                let principal_type = entry["properties"]["principalType"]
                    .as_str()
                    .map(ToString::to_string);
                (principal_id, principal_type)
            } else {
                (None, None)
            };

            results.insert(RoleAssignment {
                role,
                scope,
                scope_name,
                role_definition_id,
                principal_id,
                principal_type,
                object: None,
            });
        }

        Ok(results)
    }
}

#[derive(Serialize, PartialOrd, Ord, PartialEq, Eq, Debug, Clone)]
pub struct RoleAssignment {
    pub role: Role,
    pub scope: Scope,
    pub scope_name: String,
    #[serde(skip)]
    pub role_definition_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub principal_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub principal_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub object: Option<Object>,
}

impl RoleAssignment {
    pub(crate) fn friendly(&self) -> String {
        format!(
            "\"{}\" in \"{}\" ({})",
            self.role, self.scope_name, self.scope
        )
    }
}

#[cfg(test)]
mod tests {
    use super::{RoleAssignments, Scope};
    use anyhow::Result;
    use insta::assert_json_snapshot;
    use uuid::Uuid;

    #[test]
    fn parse_active() -> Result<()> {
        const ASSIGNMENTS: &str = include_str!("../../tests/data/role-assignments.json");
        let assignments = RoleAssignments::parse(&serde_json::from_str(ASSIGNMENTS)?, false)?;
        assert_json_snapshot!(&assignments);
        let assignments = RoleAssignments::parse(&serde_json::from_str(ASSIGNMENTS)?, true)?;
        assert_json_snapshot!(&assignments);
        Ok(())
    }

    #[test]
    fn test_scope() {
        let uuid = Uuid::now_v7();
        let scope = Scope::from_subscription(&uuid);
        assert!(scope.is_subscription());
        assert_eq!(scope.subscription(), Some(uuid));
        let scope = Scope::from_resource_group(&uuid, "rg");
        assert!(!scope.is_subscription());
        assert_eq!(scope.subscription(), Some(uuid));
    }
}