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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::fmt::Debug;

use crate::{validate_account_id, validate_name, validate_partition, validate_path, PrincipalError};

/// A trait bound/alias for principal flavor-specific data. This is automatically implemented for any type which
/// matches the required bounds.
pub trait Data
where
    Self: Clone + Debug + PartialEq + Eq + Send + Sized + Sync + 'static,
{
}

impl<T> Data for T where T: Clone + Debug + PartialEq + Eq + Send + Sized + Sync + 'static {}

/// Details about an assumed role.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AssumedRoleDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Name of the role, case-insensitive.
    pub role_name: String,

    /// Session name for the assumed role.
    pub session_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> AssumedRoleDetails<T> {
    /// Create an [AssumedRoleDetails] object.
    ///
    /// # Arguments:
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `role_name`: The name of the role being assumed. This must meet the following requirements or a
    ///     [PrincipalError::InvalidRoleName] error will be returned:
    ///     *   The name must contain between 1 and 64 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `session_name`: A name to assign to the session. This must meet the following requirements or a
    ///     [PrincipalError::InvalidSessionName] error will be returned:
    ///     *   The session name must contain between 2 and 64 characters.
    ///     *   The session name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    ///
    /// If all of the requirements are met, an [AssumedRoleDetails] object is returned. Otherwise,
    /// a [PrincipalError] error is returned.
    pub fn new<S1, S2, S3, S4>(
        partition: S1,
        account_id: S2,
        role_name: S3,
        session_name: S4,
        data: T,
    ) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    {
        let partition = validate_partition(partition)?;
        let account_id = validate_account_id(account_id)?;
        let role_name = validate_name(role_name, 64).map_err(PrincipalError::InvalidRoleName)?;
        let session_name = validate_name(session_name, 64).map_err(PrincipalError::InvalidSessionName)?;

        if session_name.len() < 2 {
            Err(PrincipalError::InvalidSessionName(session_name))
        } else {
            Ok(Self {
                partition,
                account_id,
                role_name,
                session_name,
                data,
            })
        }
    }
}

/// Details about a federated user.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FederatedUserDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Name of the principal, case-insensitive.
    pub user_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> FederatedUserDetails<T> {
    /// Create a [FederatedUserDetails] object.
    ///
    /// # Arguments:
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `user_name`: The name of the federated user. This must meet the following requirements or a
    ///     [PrincipalError::InvalidFederatedUserName] error will be returned:
    ///     *   The name must contain between 2 and 64 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    ///
    /// If all of the requirements are met, a [FederatedUserDetails] object is returned. Otherwise,
    /// a [PrincipalError] error is returned.
    pub fn new<S1, S2, S3>(partition: S1, account_id: S2, user_name: S3, data: T) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
    {
        let partition = validate_partition(partition)?;
        let account_id = validate_account_id(account_id)?;
        let user_name = validate_name(user_name, 32).map_err(PrincipalError::InvalidFederatedUserName)?;

        if user_name.len() < 2 {
            Err(PrincipalError::InvalidFederatedUserName(user_name))
        } else {
            Ok(Self {
                partition,
                account_id,
                user_name,
                data,
            })
        }
    }
}

/// Details about an IAM group.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GroupDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Path, starting with a `/`.
    pub path: String,

    /// Name of the group, case-insensitive.
    pub group_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> GroupDetails<T> {
    /// Create a [GroupDetails] object
    ///
    /// # Arguments
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `path`: The IAM path the group is under. This must meet the following requirements or a
    ///     [PrincipalError::InvalidPath] error will be returned:
    ///     *   The path must contain between 1 and 512 characters.
    ///     *   The path must start and end with `/`.
    ///     *   All characters in the path must be in the ASCII range 0x21 (`!`) through 0x7E (`~`). The AWS
    ///         documentation erroneously indicates that 0x7F (DEL) is acceptable; however, the IAM APIs reject this
    ///         character.
    /// * `group_name`: The name of the group. This must meet the following requirements or a
    ///     [PrincipalError::InvalidGroupName] error will be returned:
    ///     *   The name must contain between 1 and 128 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    /// If all of the requirements are met, a [GroupDetails] object is returned. Otherwise, a [PrincipalError] error
    /// is returned.
    pub fn new<S1, S2, S3, S4>(
        partition: S1,
        account_id: S2,
        path: S3,
        group_name: S4,
        data: T,
    ) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    {
        Ok(Self {
            partition: validate_partition(partition)?,
            account_id: validate_account_id(account_id)?,
            path: validate_path(path)?,
            group_name: validate_name(group_name, 128).map_err(PrincipalError::InvalidGroupName)?,
            data,
        })
    }
}

/// Details about an AWS IAM instance profile.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InstanceProfileDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Path, starting with a `/`.
    pub path: String,

    /// Name of the principal, case-insensitive.
    pub instance_profile_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> InstanceProfileDetails<T> {
    /// Create an [InstanceProfileDetails] object
    ///
    /// # Arguments
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `path`: The IAM path the group is under. This must meet the following requirements or a
    ///     [PrincipalError::InvalidPath] error will be returned:
    ///     *   The path must contain between 1 and 512 characters.
    ///     *   The path must start and end with `/`.
    ///     *   All characters in the path must be in the ASCII range 0x21 (`!`) through 0x7E (`~`). The AWS documentation
    ///         erroneously indicates that 0x7F (DEL) is acceptable; however, the IAM APIs reject this character.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    ///
    /// If all of the requirements are met, an [InstanceProfileDetails] object is returned.
    /// Otherwise, a [PrincipalError] error is returned.
    pub fn new<S1, S2, S3, S4>(
        partition: S1,
        account_id: S2,
        path: S3,
        instance_profile_name: S4,
        data: T,
    ) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    {
        Ok(Self {
            partition: validate_partition(partition)?,
            account_id: validate_account_id(account_id)?,
            path: validate_path(path)?,
            instance_profile_name: validate_name(instance_profile_name, 128)
                .map_err(PrincipalError::InvalidInstanceProfileName)?,
            data,
        })
    }
}

/// Details about an AWS IAM role.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RoleDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Path, starting with a `/`.
    pub path: String,

    /// Name of the principal, case-insensitive.
    pub role_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> RoleDetails<T> {
    /// Create a [RoleDetails] object
    ///
    /// # Arguments
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `path`: The IAM path the group is under. This must meet the following requirements or a
    ///     [PrincipalError::InvalidPath] error will be returned:
    ///     *   The path must contain between 1 and 512 characters.
    ///     *   The path must start and end with `/`.
    ///     *   All characters in the path must be in the ASCII range 0x21 (`!`) through 0x7E (`~`). The AWS documentation
    ///         erroneously indicates that 0x7F (DEL) is acceptable; however, the IAM APIs reject this character.
    /// * `role_name`: The name of the role. This must meet the following requirements or a
    ///     [PrincipalError::InvalidRoleName] error will be returned:
    ///     *   The name must contain between 1 and 64 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    ///
    /// If all of the requirements are met, a [RoleDetails] object is returned. Otherwise, a [PrincipalError] error
    /// is returned.
    pub fn new<S1, S2, S3, S4>(
        partition: S1,
        account_id: S2,
        path: S3,
        role_name: S4,
        data: T,
    ) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    {
        Ok(Self {
            partition: validate_partition(partition)?,
            account_id: validate_account_id(account_id)?,
            path: validate_path(path)?,
            role_name: validate_name(role_name, 64).map_err(PrincipalError::InvalidRoleName)?,
            data,
        })
    }
}

/// Details about an AWS root user.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RootUserDetails {
    /// The partition this principal exists in. If None, the current partition is assumed.
    pub partition: Option<String>,

    /// The account id.
    pub account_id: String,
}

impl RootUserDetails {
    /// Create a [RootUserDetails] object
    ///
    /// # Arguments
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    ///
    /// # Return value
    ///
    /// If the requirement is met, a [RootUserDetails] object is returned. Otherwise, a
    /// [PrincipalError] error is returned.
    pub fn new<S1>(partition: Option<String>, account_id: S1) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
    {
        let partition = match partition {
            None => None,
            Some(partition) => Some(validate_partition(partition)?),
        };
        let account_id = validate_account_id(account_id)?;
        Ok(Self {
            partition,
            account_id,
        })
    }
}

/// Details about a service.
#[doc(cfg(feature = "service"))]
#[cfg(feature = "service")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ServiceDetails<T: Data> {
    /// The partition this principal exists in. If None, the current partition is assumed.
    pub partition: Option<String>,

    /// Name of the service.
    pub service_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

#[cfg(feature = "service")]
impl<T: Data> ServiceDetails<T> {
    #[doc(cfg(feature = "service"))]
    /// Create a [ServiceDetails] object
    ///
    /// # Arguments
    ///
    /// * `service_name`: The name of the service. This must meet the following requirements or a
    ///     [PrincipalError::InvalidServiceName] error will be returned:
    ///     *   The name must contain between 1 and 32 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// If all of the requirements are met, a [ServiceDetails] object is returned.  Otherwise, a [PrincipalError]
    /// error is returned.
    pub fn new<S>(partition: Option<String>, service_name: S, data: T) -> Result<Self, PrincipalError>
    where
        S: Into<String>,
    {
        let partition = match partition {
            None => None,
            Some(partition) => Some(validate_partition(partition)?),
        };
        let service_name = validate_name(service_name, 32).map_err(PrincipalError::InvalidServiceName)?;

        Ok(Self {
            partition,
            service_name,
            data,
        })
    }
}

/// Details about an AWS IAM user.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UserDetails<T: Data> {
    /// The partition this principal exists in.
    pub partition: String,

    /// The account id.
    pub account_id: String,

    /// Path, starting with a `/`.
    pub path: String,

    /// Name of the principal, case-insensitive.
    pub user_name: String,

    /// Principal flavor-specific data.
    pub data: T,
}

impl<T: Data> UserDetails<T> {
    /// Create a [UserDetails] object.
    ///
    /// # Arguments
    ///
    /// * `account_id`: The 12 digit account id. This must be composed of 12 ASCII digits or a
    ///     [PrincipalError::InvalidAccountId] error will be returned.
    /// * `path`: The IAM path the group is under. This must meet the following requirements or a
    ///     [PrincipalError::InvalidPath] error will be returned:
    ///     *   The path must contain between 1 and 512 characters.
    ///     *   The path must start and end with `/`.
    ///     *   All characters in the path must be in the ASCII range 0x21 (`!`) through 0x7E (`~`). The AWS documentation
    ///         erroneously indicates that 0x7F (DEL) is acceptable; however, the IAM APIs reject this character.
    /// * `user_name`: The name of the user. This must meet the following requirements or a
    ///     [PrincipalError::InvalidUserName] error will be returned:
    ///     *   The name must contain between 1 and 64 characters.
    ///     *   The name must be composed to ASCII alphanumeric characters or one of `, - . = @ _`.
    /// * `data`: Principal flavor-specific data.
    ///
    /// # Return value
    ///
    /// If all of the requirements are met, a [UserDetails] object is returned. Otherwise, a [PrincipalError] error
    /// is returned.
    pub fn new<S1, S2, S3, S4>(
        partition: S1,
        account_id: S2,
        path: S3,
        user_name: S4,
        data: T,
    ) -> Result<Self, PrincipalError>
    where
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    {
        Ok(Self {
            partition: validate_partition(partition)?,
            account_id: validate_account_id(account_id)?,
            path: validate_path(path)?,
            user_name: validate_name(user_name, 64).map_err(PrincipalError::InvalidUserName)?,
            data,
        })
    }
}