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
use {
    crate::{display_json, from_str_json, StatementList},
    derive_builder::Builder,
    serde::{Deserialize, Serialize},
};

/// The top-level structure for holding an Aspen policy.
#[derive(Builder, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Policy {
    /// The version of the policy. Currently allowed values are `2008-10-17` and `2012-10-17`. Features such as
    /// policy variables are only available with version `2012-10-17` (or later, should a newer version be published).
    /// If omitted, this is equivalent to `2008-10-17`.
    #[builder(setter(into, strip_option), default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    version: Option<String>,

    /// An optional identifier for the policy. Some services may require this element and have uniqueness requirements.
    #[builder(setter(into, strip_option), default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<String>,

    /// One or more statements describing the policy. Aspen allows single statements to be encoded directly as a map
    /// instead of being enclosed in a list.
    #[builder(setter(into))]
    statement: StatementList,
}

impl Policy {
    #[inline]
    pub fn builder() -> PolicyBuilder {
        PolicyBuilder::default()
    }

    #[inline]
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    #[inline]
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    #[inline]
    pub fn statement(&self) -> &StatementList {
        &self.statement
    }
}

display_json!(Policy);
from_str_json!(Policy);

#[cfg(test)]
mod tests {
    use {
        crate::{Action, AwsPrincipal, Effect, Policy, Principal, Resource, SpecifiedPrincipal, Statement},
        indoc::indoc,
        pretty_assertions::{assert_eq, assert_ne},
        std::{str::FromStr, sync::Arc},
    };

    #[test_log::test]
    fn test_serialization() {
        let p1_str = include_str!("test-policy-1.json");
        let p2_str = include_str!("test-policy-2.json");
        let p1: Policy = serde_json::from_str(p1_str).unwrap();
        let p2: Policy = serde_json::from_str(p2_str).unwrap();

        let statements = p1.statement.to_vec();
        assert_eq!(statements.len(), 2);
        let actions = statements[0].action().unwrap().to_vec();
        assert_eq!(actions.len(), 1);
        assert_eq!(actions[0], &Action::new("s3", "ListBucket").unwrap());

        assert_eq!(*statements[1].effect(), Effect::Deny);

        let actions = statements[1].not_action().unwrap().to_vec();
        assert_eq!(actions.len(), 3);
        assert_eq!(actions[0], &Action::new("ec2", "*").unwrap());
        assert_eq!(actions[1], &Action::new("s3", "*").unwrap());
        assert_eq!(actions[2], &Action::new("rds", "*").unwrap());

        let resources = statements[1].resource().unwrap().to_vec();
        assert_eq!(resources.len(), 3);
        assert_eq!(*resources[0], Resource::from_str("arn:aws:ec2:*:*:instance/*").unwrap());
        assert_eq!(*resources[1], Resource::from_str("arn:aws:s3:*:*:bucket/*").unwrap());
        assert_eq!(*resources[2], Resource::from_str("arn:aws:rds:*:*:db/*").unwrap());

        let principals = statements[1].principal().unwrap();
        if let Principal::Specified(ref specified) = principals {
            let aws = specified.aws().unwrap().to_vec();
            assert_eq!(aws.len(), 2);
            assert_eq!(*aws[0], AwsPrincipal::from_str("arn:aws:iam::123456789012:root").unwrap());
            assert_eq!(*aws[1], AwsPrincipal::from_str("arn:aws:iam::123456789012:user/*").unwrap());

            let canonical_users = specified.canonical_user().unwrap().to_vec();
            assert_eq!(canonical_users.len(), 2);
            assert_eq!(canonical_users[0], "d04207a7d9311e77f5837e0e4f4b025322bf2f626f0872c85be8c6bb1290c88b");
            assert_eq!(canonical_users[1], "2cdb0173470eb5b200f82c8e1b51a88562924cda12e2ccce60d7f00e1567ee7c");

            let federated = specified.federated().unwrap().to_vec();
            assert_eq!(federated.len(), 1);
            assert_eq!(federated[0], "dacut@kanga.org");

            let service = specified.service().unwrap().to_vec();
            assert_eq!(service.len(), 3);
            assert_eq!(service[0], "ec2.amazonaws.com");
            assert_eq!(service[1], "edgelambda.amazonaws.com");
            assert_eq!(service[2], "lambda.amazonaws.com");
        } else {
            panic!("Expected SpecifiedPrincipal");
        }

        let json = serde_json::to_string_pretty(&p1).unwrap();
        assert_eq!(json, p1_str);

        assert_ne!(p1, p2);
    }

    #[test_log::test]
    fn test_builder() {
        let s = Arc::new(
            Statement::builder()
                .effect(Effect::Allow)
                .action(Action::from_str("ec2:RunInstances").unwrap())
                .resource(
                    Resource::from_str("arn:aws:ec2:us-east-1:123456789012:instance/i-01234567890abcdef").unwrap(),
                )
                .principal(
                    SpecifiedPrincipal::builder().aws(AwsPrincipal::from_str("123456789012").unwrap()).build().unwrap(),
                )
                .build()
                .unwrap(),
        );
        let p1a = Policy::builder().statement(s.clone()).build().unwrap();
        let p1b = Policy::builder().statement(s.clone()).build().unwrap();
        let p2 = Policy::builder().version("2012-10-17").id("test").statement(s).build().unwrap();

        assert_eq!(p1a, p1b);
        assert_eq!(p1a, p1a.clone());
        assert_ne!(p1a, p2);

        let _ = format!("{:?}", p1a);
        let json = format!("{}", p2);

        assert_eq!(
            json,
            indoc! {r#"
            {
                "Version": "2012-10-17",
                "Id": "test",
                "Statement": {
                    "Effect": "Allow",
                    "Action": "ec2:RunInstances",
                    "Resource": "arn:aws:ec2:us-east-1:123456789012:instance/i-01234567890abcdef",
                    "Principal": {
                        "AWS": "123456789012"
                    }
                }
            }"#}
        );
    }

    #[test_log::test]
    fn test_bad_from_str() {
        let e = Policy::from_str("{}").unwrap_err();
        assert_eq!(e.to_string(), "missing field `Statement` at line 1 column 2");
    }
}