scratchstack_aws_principal/
error.rs

1use {
2    scratchstack_arn::ArnError,
3    std::{
4        error::Error,
5        fmt::{Debug, Display, Formatter, Result as FmtResult},
6    },
7};
8
9/// Errors that can be raise during the parsing of principals.
10#[derive(Debug, Eq, PartialEq)]
11pub enum PrincipalError {
12    /// Entity does not have a valid ARN.
13    CannotConvertToArn,
14
15    /// Invalid AWS account id. The argument contains the specified account id.
16    InvalidAccountId(String),
17
18    /// Invalid ARN. The argument contains the specified ARN.
19    InvalidArn(String),
20
21    /// Invalid Canonical User Id. The argument contains the spcified canonical user id.
22    InvalidCanonicalUserId(String),
23
24    /// Invalid partition. The argument contains the specified partition.
25    InvalidPartition(String),
26
27    /// Invalid federated user name. The argument contains the specified user name.
28    InvalidFederatedUserName(String),
29
30    /// Invalid group name. The argument contains the specified group name.
31    InvalidGroupName(String),
32
33    /// Invalid group id. The argument contains the specified group id.
34    InvalidGroupId(String),
35
36    /// Invalid instance profile name. The argument contains the specified instance profile name.
37    InvalidInstanceProfileName(String),
38
39    /// Invalid instance profile id. The argument contains the specified instance profile id.
40    InvalidInstanceProfileId(String),
41
42    /// Invalid IAM path. The argument contains the specified path.
43    InvalidPath(String),
44
45    /// Invalid region. The argument contains the specified region.
46    InvalidRegion(String),
47
48    /// Invalid resource. The argument contains the specified resource.
49    InvalidResource(String),
50
51    /// Invalid role name. The argument contains the specified role name.
52    InvalidRoleName(String),
53
54    /// Invalid role id. The argument contains the specified role id.
55    InvalidRoleId(String),
56
57    /// Invalid scheme. The argument contains the specified scheme.
58    InvalidScheme(String),
59
60    /// Invalid service. The argument contains the specified service name.
61    InvalidService(String),
62
63    /// Invalid session name. The argument contains the specified session name.
64    InvalidSessionName(String),
65
66    /// Invalid user name. The argument contains the specified user name.
67    InvalidUserName(String),
68
69    /// Invalid user id. The argument contains the specified user id.
70    InvalidUserId(String),
71}
72
73impl Error for PrincipalError {}
74
75impl Display for PrincipalError {
76    fn fmt(&self, f: &mut Formatter) -> FmtResult {
77        match self {
78            Self::CannotConvertToArn => f.write_str("Cannot convert entity to ARN"),
79            Self::InvalidArn(arn) => write!(f, "Invalid ARN: {arn:#?}"),
80            Self::InvalidAccountId(account_id) => write!(f, "Invalid account id: {account_id:#?}"),
81            Self::InvalidCanonicalUserId(canonical_user_id) => {
82                write!(f, "Invalid canonical user id: {canonical_user_id:#?}")
83            }
84            Self::InvalidFederatedUserName(user_name) => {
85                write!(f, "Invalid federated user name: {user_name:#?}")
86            }
87            Self::InvalidGroupName(group_name) => {
88                write!(f, "Invalid group name: {group_name:#?}")
89            }
90            Self::InvalidGroupId(group_id) => write!(f, "Invalid group id: {group_id:#?}"),
91            Self::InvalidInstanceProfileName(instance_profile_name) => {
92                write!(f, "Invalid instance profile name: {instance_profile_name:#?}")
93            }
94            Self::InvalidInstanceProfileId(instance_profile_id) => {
95                write!(f, "Invalid instance profile id: {instance_profile_id:#?}")
96            }
97            Self::InvalidPartition(partition) => write!(f, "Invalid partition: {partition:#?}"),
98            Self::InvalidPath(path) => write!(f, "Invalid path: {path:#?}"),
99            Self::InvalidRegion(region) => write!(f, "Invalid region: {region:#?}"),
100            Self::InvalidResource(resource) => write!(f, "Invalid resource: {resource:#?}"),
101            Self::InvalidRoleName(role_name) => write!(f, "Invalid role name: {role_name:#?}"),
102            Self::InvalidRoleId(role_id) => write!(f, "Invalid role id: {role_id:#?}"),
103            Self::InvalidScheme(scheme) => write!(f, "Invalid scheme: {scheme:#?}"),
104            Self::InvalidService(service_name) => {
105                write!(f, "Invalid service name: {service_name:#?}")
106            }
107            Self::InvalidSessionName(session_name) => {
108                write!(f, "Invalid session name: {session_name:#?}")
109            }
110            Self::InvalidUserName(user_name) => write!(f, "Invalid user name: {user_name:#?}"),
111            Self::InvalidUserId(user_id) => write!(f, "Invalid user id: {user_id:#?}"),
112        }
113    }
114}
115
116impl From<ArnError> for PrincipalError {
117    fn from(err: ArnError) -> Self {
118        match err {
119            ArnError::InvalidScheme(scheme) => Self::InvalidScheme(scheme),
120            ArnError::InvalidPartition(partition) => Self::InvalidPartition(partition),
121            ArnError::InvalidService(service_name) => Self::InvalidService(service_name),
122            ArnError::InvalidRegion(region) => Self::InvalidRegion(region),
123            ArnError::InvalidAccountId(account_id) => Self::InvalidAccountId(account_id),
124            ArnError::InvalidResource(resource) => Self::InvalidResource(resource),
125            ArnError::InvalidArn(arn) => Self::InvalidArn(arn),
126        }
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use {super::PrincipalError, scratchstack_arn::ArnError};
133
134    #[test]
135    fn exercise_unused_in_crate() {
136        // These errors are not created in code used here, but are used in other crates.
137        let err = PrincipalError::InvalidGroupName("test+1".to_string());
138        assert_eq!(err.to_string(), r#"Invalid group name: "test+1""#);
139
140        let err = PrincipalError::InvalidInstanceProfileName("test+1".to_string());
141        assert_eq!(err.to_string(), r#"Invalid instance profile name: "test+1""#);
142    }
143
144    fn check_arn_err_into(arn_err: ArnError) {
145        let arn_err_string = arn_err.to_string();
146        let principal_err = PrincipalError::from(arn_err);
147        assert_eq!(principal_err.to_string(), arn_err_string);
148
149        // Ensure we can debug print the principal error.
150        let _ = format!("{principal_err:?}");
151    }
152
153    #[test]
154    fn test_from_arn_error() {
155        check_arn_err_into(ArnError::InvalidAccountId("abcd".to_string()));
156        check_arn_err_into(ArnError::InvalidArn("arn:foo:bar".to_string()));
157        check_arn_err_into(ArnError::InvalidPartition("-foo".to_string()));
158        check_arn_err_into(ArnError::InvalidRegion("foo-".to_string()));
159        check_arn_err_into(ArnError::InvalidResource("".to_string()));
160        check_arn_err_into(ArnError::InvalidScheme("https".to_string()));
161        check_arn_err_into(ArnError::InvalidService("foo".to_string()));
162    }
163}
164// end tests -- do not delete; needed for coverage.