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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use {
    crate::{eval::regex_from_glob, AspenError, Context, PolicyVersion},
    scratchstack_arn::Arn,
    std::{
        fmt::{Display, Formatter, Result as FmtResult},
        str::FromStr,
    },
};

const PARTITION_START: usize = 4;

/// An Amazon Resource Name (ARN) statement in an IAM Aspen policy.
///
/// This is used to match [scratchstack_arn::Arn] objects from a resource statement in the IAM Aspen policy language. For example,
/// an [ResourceArn] created from `arn:aws*:ec2:us-*-?:123456789012:instance/i-*` would match the following [Arn]
/// objects:
/// * `arn:aws:ec2:us-east-1:123456789012:instance/i-1234567890abcdef0`
/// * `arn:aws-us-gov:ec2:us-west-2:123456789012:instance/i-1234567890abcdef0`
///
/// Patterns are similar to glob statements with a few differences:
/// * The `*` character matches any number of characters, including none, within a single segment of the ARN.
/// * The `?` character matches any single character within a single segment of the ARN.
///
/// [ResourceArn] objects are immutable.
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct ResourceArn {
    arn: String,
    service_start: usize,
    region_start: usize,
    account_id_start: usize,
    resource_start: usize,
}

impl ResourceArn {
    /// Create a new ARN pattern from the specified components.
    ///
    /// * `partition` - The partition the resource is in.
    /// * `service` - The service the resource belongs to.
    /// * `region` - The region the resource is in.
    /// * `account_id` - The account ID the resource belongs to.
    /// * `resource` - The resource name.
    pub fn new(partition: &str, service: &str, region: &str, account_id: &str, resource: &str) -> Self {
        let arn = format!("arn:{}:{}:{}:{}:{}", partition, service, region, account_id, resource);
        let service_start = PARTITION_START + partition.len() + 1;
        let region_start = service_start + service.len() + 1;
        let account_id_start = region_start + region.len() + 1;
        let resource_start = account_id_start + account_id.len() + 1;

        Self {
            arn,
            service_start,
            region_start,
            account_id_start,
            resource_start,
        }
    }

    /// Retrieve the partition string pattern.
    #[inline]
    pub fn partition_pattern(&self) -> &str {
        &self.arn[PARTITION_START..self.service_start - 1]
    }

    /// Retrieve the service string pattern.
    #[inline]
    pub fn service_pattern(&self) -> &str {
        &self.arn[self.service_start..self.region_start - 1]
    }

    /// Retrieve the region string pattern.
    #[inline]
    pub fn region_pattern(&self) -> &str {
        &self.arn[self.region_start..self.account_id_start - 1]
    }

    /// Retrieve the account ID string pattern.
    #[inline]
    pub fn account_id_pattern(&self) -> &str {
        &self.arn[self.account_id_start..self.resource_start - 1]
    }

    /// Retrieve the resource name string pattern.
    #[inline]
    pub fn resource_pattern(&self) -> &str {
        &self.arn[self.resource_start..]
    }

    pub fn matches(&self, context: &Context, pv: PolicyVersion, candidate: &Arn) -> Result<bool, AspenError> {
        let partition_pattern = self.partition_pattern();
        let service_pattern = self.service_pattern();
        let region_pattern = self.region_pattern();
        let account_id_pattern = self.account_id_pattern();
        let resource_pattern = self.resource_pattern();

        let partition = regex_from_glob(partition_pattern).build().expect("Failed to build partition regex");
        let service = regex_from_glob(service_pattern).build().expect("Failed to build service regex");
        let region = regex_from_glob(region_pattern).build().expect("Failed to build region regex");
        let account_id = regex_from_glob(account_id_pattern).build().unwrap();
        let resource = context.matcher(resource_pattern, pv)?.build().unwrap();

        let partition_match = partition.is_match(candidate.partition());
        let service_match = service.is_match(candidate.service());
        let region_match = region.is_match(candidate.region());
        let account_id_match = account_id.is_match(candidate.account_id());
        let resource_match = resource.is_match(candidate.resource());
        let result = partition_match && service_match && region_match && account_id_match && resource_match;

        log::trace!("arn_pattern_matches: pattern={:?}, candidate={} -> partition={:?} ({}) service={:?} ({}) region={:?} ({}) account_id={:?} ({}) resource={:?} vs {:?} ({}) -> result={}", self, candidate, partition, partition_match, service, service_match, region, region_match, account_id, account_id_match, resource, candidate.resource(), resource_match, result);

        Ok(result)
    }
}

impl FromStr for ResourceArn {
    type Err = AspenError;

    /// Create an [ResourceArn] from a string.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.splitn(6, ':').collect();
        if parts.len() != 6 || parts[0] != "arn" {
            return Err(AspenError::InvalidResource(s.to_string()));
        }

        let arn = s.to_string();
        let service_start = PARTITION_START + parts[1].len() + 1;
        let region_start = service_start + parts[2].len() + 1;
        let account_id_start = region_start + parts[3].len() + 1;
        let resource_start = account_id_start + parts[4].len() + 1;

        Ok(Self {
            arn,
            service_start,
            region_start,
            account_id_start,
            resource_start,
        })
    }
}

impl Display for ResourceArn {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(&self.arn)
    }
}

#[cfg(test)]
mod tests {
    use {
        super::ResourceArn,
        crate::AspenError,
        pretty_assertions::{assert_eq, assert_ne},
        std::{collections::hash_map::DefaultHasher, hash::Hash, str::FromStr},
    };

    #[test_log::test]
    fn check_arn_pattern_derived() {
        let pat1a = ResourceArn::from_str("arn:*:ec2:us-*-1:123456789012:instance/*").unwrap();
        let pat1b = ResourceArn::new("*", "ec2", "us-*-1", "123456789012", "instance/*");
        let pat1c = pat1a.clone();
        let pat2 = ResourceArn::from_str("arn:aws:ec2:us-east-1:123456789012:instance/*").unwrap();
        let pat3 = ResourceArn::from_str("arn:aws:ec*:us-*-1::*").unwrap();

        assert_eq!(pat1a, pat1b);
        assert_ne!(pat1a, pat2);
        assert_eq!(pat1c, pat1b);

        assert_eq!(pat1a.partition_pattern(), "*");
        assert_eq!(pat1a.service_pattern(), "ec2");
        assert_eq!(pat1a.region_pattern(), "us-*-1");
        assert_eq!(pat1a.account_id_pattern(), "123456789012");
        assert_eq!(pat1a.resource_pattern(), "instance/*");

        // Ensure we can derive a hash for the arn.
        let mut h2 = DefaultHasher::new();
        pat3.hash(&mut h2);

        // Ensure we can debug print the arn.
        _ = format!("{:?}", pat3);

        // Ensure we can print the arn.
        assert_eq!(pat3.to_string(), "arn:aws:ec*:us-*-1::*".to_string());
    }

    #[test_log::test]
    fn check_arn_pattern_components() {
        let pat = ResourceArn::from_str("arn:aws:ec*:us-*-1::*").unwrap();
        assert_eq!(pat.partition_pattern(), "aws");
        assert_eq!(pat.service_pattern(), "ec*");
        assert_eq!(pat.region_pattern(), "us-*-1");
        assert_eq!(pat.account_id_pattern(), "");
        assert_eq!(pat.resource_pattern(), "*");
    }

    #[test_log::test]
    fn check_malformed_patterns() {
        let wrong_parts =
            vec!["arn", "arn:aw*", "arn:aw*:e?2", "arn:aw*:e?2:us-*-1", "arn:aw*:e?2:us-*-1:123456789012"];
        for wrong_part in wrong_parts {
            assert_eq!(
                ResourceArn::from_str(wrong_part).unwrap_err().to_string(),
                format!("Invalid resource: {}", wrong_part)
            );
        }

        let err =
            ResourceArn::from_str("https:aws:ec2:us-east-1:123456789012:instance/i-1234567890abcdef0").unwrap_err();
        assert_eq!(
            err,
            AspenError::InvalidResource(
                "https:aws:ec2:us-east-1:123456789012:instance/i-1234567890abcdef0".to_string()
            )
        );
    }
}