1use crate::policy;
16
17pub type Result<T> = core::result::Result<T, Error>;
18
19#[derive(thiserror::Error, Debug)]
20pub enum Error {
21 #[error(transparent)]
22 PolicyError(#[from] policy::Error),
23
24 #[error("{0}")]
25 StringError(String),
26
27 #[error("crypto: {0}")]
28 CryptoError(#[from] rustfs_crypto::Error),
29
30 #[error("user '{0}' does not exist")]
31 NoSuchUser(String),
32
33 #[error("account '{0}' does not exist")]
34 NoSuchAccount(String),
35
36 #[error("service account '{0}' does not exist")]
37 NoSuchServiceAccount(String),
38
39 #[error("temp account '{0}' does not exist")]
40 NoSuchTempAccount(String),
41
42 #[error("group '{0}' does not exist")]
43 NoSuchGroup(String),
44
45 #[error("policy does not exist")]
46 NoSuchPolicy,
47
48 #[error("policy in use")]
49 PolicyInUse,
50
51 #[error("group not empty")]
52 GroupNotEmpty,
53
54 #[error("invalid arguments specified")]
55 InvalidArgument,
56
57 #[error("not initialized")]
58 IamSysNotInitialized,
59
60 #[error("invalid service type: {0}")]
61 InvalidServiceType(String),
62
63 #[error("invalid access key length")]
64 InvalidAccessKeyLength,
65
66 #[error("invalid secret key length")]
67 InvalidSecretKeyLength,
68
69 #[error("access key contains reserved characters =,")]
70 ContainsReservedChars,
71
72 #[error("group name contains reserved characters =,")]
73 GroupNameContainsReservedChars,
74
75 #[error("jwt err {0}")]
76 JWTError(#[from] jsonwebtoken::errors::Error),
77
78 #[error("action not allowed")]
79 IAMActionNotAllowed,
80
81 #[error("no secret key with access key")]
82 NoSecretKeyWithAccessKey,
83
84 #[error("no access key with secret key")]
85 NoAccessKeyWithSecretKey,
86
87 #[error("policy too large")]
88 PolicyTooLarge,
89
90 #[error("io error: {0}")]
91 Io(std::io::Error),
92
93 #[error("system already initialized")]
94 IamSysAlreadyInitialized,
95}
96
97impl Error {
98 pub fn other<E>(error: E) -> Self
99 where
100 E: Into<Box<dyn std::error::Error + Send + Sync>>,
101 {
102 Error::Io(std::io::Error::other(error))
103 }
104}
105
106impl From<std::io::Error> for Error {
107 fn from(e: std::io::Error) -> Self {
108 Error::Io(e)
109 }
110}
111
112impl From<time::error::ComponentRange> for Error {
113 fn from(e: time::error::ComponentRange) -> Self {
114 Error::other(e)
115 }
116}
117
118impl From<serde_json::Error> for Error {
119 fn from(e: serde_json::Error) -> Self {
120 Error::other(e)
121 }
122}
123
124impl From<regex::Error> for Error {
131 fn from(e: regex::Error) -> Self {
132 Error::other(e)
133 }
134}
135
136pub fn is_err_no_such_policy(err: &Error) -> bool {
141 matches!(err, Error::NoSuchPolicy)
142}
143
144pub fn is_err_no_such_user(err: &Error) -> bool {
145 matches!(err, Error::NoSuchUser(_))
146}
147
148pub fn is_err_no_such_account(err: &Error) -> bool {
149 matches!(err, Error::NoSuchAccount(_))
150}
151
152pub fn is_err_no_such_temp_account(err: &Error) -> bool {
153 matches!(err, Error::NoSuchTempAccount(_))
154}
155
156pub fn is_err_no_such_group(err: &Error) -> bool {
157 matches!(err, Error::NoSuchGroup(_))
158}
159
160pub fn is_err_no_such_service_account(err: &Error) -> bool {
161 matches!(err, Error::NoSuchServiceAccount(_))
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167 use std::io::{Error as IoError, ErrorKind};
168
169 #[test]
170 fn test_policy_error_from_io_error() {
171 let io_error = IoError::new(ErrorKind::PermissionDenied, "permission denied");
172 let policy_error: Error = io_error.into();
173
174 match policy_error {
175 Error::Io(inner_io) => {
176 assert_eq!(inner_io.kind(), ErrorKind::PermissionDenied);
177 }
178 _ => panic!("Expected Io variant"),
179 }
180 }
181
182 #[test]
183 fn test_policy_error_other_function() {
184 let custom_error = "Custom policy error";
185 let policy_error = Error::other(custom_error);
186
187 match policy_error {
188 Error::Io(io_error) => {
189 assert_eq!(io_error.kind(), ErrorKind::Other);
190 }
191 _ => panic!("Expected Io variant"),
192 }
193 }
194
195 #[test]
196 fn test_policy_error_from_crypto_error() {
197 let crypto_error = rustfs_crypto::Error::ErrUnexpectedHeader;
199 let policy_error: Error = crypto_error.into();
200
201 match &policy_error {
202 Error::CryptoError(_) => {}
203 _ => panic!("Expected CryptoError variant, got {:?}", policy_error),
204 }
205 }
206
207 #[test]
208 fn test_policy_error_from_jwt_error() {
209 use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
210 use serde::{Deserialize, Serialize};
211
212 #[derive(Debug, Serialize, Deserialize, Clone)]
213 struct Claims {
214 sub: String,
215 exp: usize,
216 }
217
218 let invalid_token = "invalid.jwt.token";
220 let key = DecodingKey::from_secret(b"secret");
221 let validation = Validation::new(Algorithm::HS256);
222
223 let jwt_result = decode::<Claims>(invalid_token, &key, &validation);
224 assert!(jwt_result.is_err());
225
226 let jwt_error = jwt_result.unwrap_err();
227 let policy_error: Error = jwt_error.into();
228
229 match &policy_error {
230 Error::JWTError(_) => {}
231 _ => panic!("Expected JWTError variant, got {:?}", policy_error),
232 }
233 }
234
235 #[test]
236 fn test_policy_error_from_serde_json() {
237 let invalid_json = r#"{"invalid": json}"#;
239 let json_error = serde_json::from_str::<serde_json::Value>(invalid_json).unwrap_err();
240 let policy_error: Error = json_error.into();
241
242 match policy_error {
243 Error::Io(io_error) => {
244 assert_eq!(io_error.kind(), ErrorKind::Other);
245 }
246 _ => panic!("Expected Io variant"),
247 }
248 }
249
250 #[test]
251 fn test_policy_error_from_time_component_range() {
252 use time::{Date, Month};
253
254 let time_result = Date::from_calendar_date(2023, Month::January, 32); assert!(time_result.is_err());
257
258 let time_error = time_result.unwrap_err();
259 let policy_error: Error = time_error.into();
260
261 match policy_error {
262 Error::Io(io_error) => {
263 assert_eq!(io_error.kind(), ErrorKind::Other);
264 }
265 _ => panic!("Expected Io variant"),
266 }
267 }
268
269 #[test]
270 #[allow(clippy::invalid_regex)]
271 fn test_policy_error_from_regex_error() {
272 use regex::Regex;
273
274 let regex_result = Regex::new("[");
276 assert!(regex_result.is_err());
277
278 let regex_error = regex_result.unwrap_err();
279 let policy_error: Error = regex_error.into();
280
281 match policy_error {
282 Error::Io(io_error) => {
283 assert_eq!(io_error.kind(), ErrorKind::Other);
284 }
285 _ => panic!("Expected Io variant"),
286 }
287 }
288
289 #[test]
290 fn test_helper_functions() {
291 assert!(is_err_no_such_policy(&Error::NoSuchPolicy));
293 assert!(!is_err_no_such_policy(&Error::NoSuchUser("test".to_string())));
294
295 assert!(is_err_no_such_user(&Error::NoSuchUser("test".to_string())));
296 assert!(!is_err_no_such_user(&Error::NoSuchAccount("test".to_string())));
297
298 assert!(is_err_no_such_account(&Error::NoSuchAccount("test".to_string())));
299 assert!(!is_err_no_such_account(&Error::NoSuchUser("test".to_string())));
300
301 assert!(is_err_no_such_temp_account(&Error::NoSuchTempAccount("test".to_string())));
302 assert!(!is_err_no_such_temp_account(&Error::NoSuchAccount("test".to_string())));
303
304 assert!(is_err_no_such_group(&Error::NoSuchGroup("test".to_string())));
305 assert!(!is_err_no_such_group(&Error::NoSuchUser("test".to_string())));
306
307 assert!(is_err_no_such_service_account(&Error::NoSuchServiceAccount("test".to_string())));
308 assert!(!is_err_no_such_service_account(&Error::NoSuchAccount("test".to_string())));
309 }
310
311 #[test]
312 fn test_error_display_format() {
313 let test_cases = vec![
314 (Error::NoSuchUser("testuser".to_string()), "user 'testuser' does not exist"),
315 (Error::NoSuchAccount("testaccount".to_string()), "account 'testaccount' does not exist"),
316 (
317 Error::NoSuchServiceAccount("service1".to_string()),
318 "service account 'service1' does not exist",
319 ),
320 (Error::NoSuchTempAccount("temp1".to_string()), "temp account 'temp1' does not exist"),
321 (Error::NoSuchGroup("group1".to_string()), "group 'group1' does not exist"),
322 (Error::NoSuchPolicy, "policy does not exist"),
323 (Error::PolicyInUse, "policy in use"),
324 (Error::GroupNotEmpty, "group not empty"),
325 (Error::InvalidArgument, "invalid arguments specified"),
326 (Error::IamSysNotInitialized, "not initialized"),
327 (Error::InvalidServiceType("invalid".to_string()), "invalid service type: invalid"),
328 (Error::InvalidAccessKeyLength, "invalid access key length"),
329 (Error::InvalidSecretKeyLength, "invalid secret key length"),
330 (Error::ContainsReservedChars, "access key contains reserved characters =,"),
331 (Error::GroupNameContainsReservedChars, "group name contains reserved characters =,"),
332 (Error::IAMActionNotAllowed, "action not allowed"),
333 (Error::NoSecretKeyWithAccessKey, "no secret key with access key"),
334 (Error::NoAccessKeyWithSecretKey, "no access key with secret key"),
335 (Error::PolicyTooLarge, "policy too large"),
336 ];
337
338 for (error, expected_message) in test_cases {
339 assert_eq!(error.to_string(), expected_message);
340 }
341 }
342
343 #[test]
344 fn test_string_error_variant() {
345 let custom_message = "Custom error message";
346 let error = Error::StringError(custom_message.to_string());
347 assert_eq!(error.to_string(), custom_message);
348 }
349}