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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*!
Provides a convenient and fluent builder interface for constructing policies.

# Example

```rust
use aws_iam::model::*;
use aws_iam::model::builder::*;
use aws_iam::io::write_to_writer;
use std::io::stdout;

let policy: Policy = PolicyBuilder::new()
    .named("confidential-data-access")
    .evaluate_statement(
        StatementBuilder::new()
            .auto_named()
            .allows()
            .unspecified_principals()
            .may_perform_actions(vec!["s3:List*", "s3:Get*"])
            .on_resources(vec![
                "arn:aws:s3:::confidential-data",
                "arn:aws:s3:::confidential-data/_*",
            ])
            .if_condition(
                ConditionBuilder::new_bool()
                    .right_hand_bool("aws:MultiFactorAuthPresent", true)
                    .if_exists(),
            ),
    )
    .into();
write_to_writer(stdout(), &policy);
```
*/

use crate::model::*;
use std::collections::HashMap;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// The top-level `Policy` builder.
///
#[derive(Debug)]
pub struct PolicyBuilder {
    version: Option<Version>,
    id: Option<String>,
    statements: Vec<Statement>,
}

///
/// A `Statement` builder, used with `PolicyBuilder::evaluate_statement()`.
///
#[derive(Debug, Clone)]
pub struct StatementBuilder {
    sid: Option<String>,
    effect: Effect,
    principals: HashMap<PrincipalType, Vec<String>>,
    p_direction: Option<bool>,
    actions: Vec<QString>,
    a_direction: Option<bool>,
    resources: Vec<String>,
    r_direction: Option<bool>,
    condition: Option<HashMap<ConditionOperator, HashMap<QString, OneOrAll<ConditionValue>>>>,
}

///
/// A `Condition` builder, used with `StatementBuilder::if_condition()`.
#[derive(Debug)]
pub struct ConditionBuilder {
    operator: ConditionOperator,
    rhs: HashMap<QString, OneOrAll<ConditionValue>>,
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Default for PolicyBuilder {
    fn default() -> Self {
        PolicyBuilder {
            version: None,
            id: None,
            statements: Vec::new(),
        }
    }
}

impl PolicyBuilder {
    /// Create a new, empty, policy builder
    pub fn new() -> Self {
        Default::default()
    }

    /// Set the version of this policy.
    pub fn version(&mut self, version: Version) -> &mut Self {
        self.version = Some(version);
        self
    }

    /// Use the IAM default for the version of this policy
    pub fn default_version(&mut self) -> &mut Self {
        self.version = Some(Policy::default_version());
        self
    }

    /// Set the id of this policy
    pub fn named(&mut self, id: &str) -> &mut Self {
        self.id = Some(id.to_string());
        self
    }

    /// Set the id of this policy to a randomly generate value.
    pub fn auto_named(&mut self) -> &mut Self {
        self.id = Some(Policy::new_id());
        self
    }

    /// Add a statement to this policy.
    pub fn evaluate_statement(&mut self, statement: &mut StatementBuilder) -> &mut Self {
        self.statements.push(statement.into());
        self
    }

    /// Add a list of statements to this policy.
    pub fn evaluate_statements(&mut self, statements: &mut Vec<StatementBuilder>) -> &mut Self {
        self.statements.extend(
            statements
                .iter_mut()
                .map(|sb| sb.into())
                .collect::<Vec<Statement>>(),
        );
        self
    }
}

impl From<&mut PolicyBuilder> for Policy {
    fn from(pb: &mut PolicyBuilder) -> Self {
        Policy {
            version: pb.version.clone(),
            id: pb.id.clone(),
            statement: match pb.statements.len() {
                0 => panic!("no statements!"),
                1 => OneOrAll::One(pb.statements.remove(0)),
                _ => OneOrAll::All(pb.statements.drain(0..).map(|sb| sb).collect()),
            },
        }
    }
}

impl Default for StatementBuilder {
    fn default() -> Self {
        StatementBuilder {
            sid: None,
            effect: Effect::Deny,
            principals: HashMap::new(),
            p_direction: None,
            actions: Vec::new(),
            a_direction: None,
            resources: Vec::new(),
            r_direction: None,
            condition: None,
        }
    }
}
impl StatementBuilder {
    /// Create a new, empty, statement builder
    pub fn new() -> Self {
        Default::default()
    }

    /// Set the id of this statement
    pub fn named(&mut self, sid: &str) -> &mut Self {
        self.sid = Some(sid.to_string());
        self
    }

    /// Set the id of this statement to a randomly generate value.
    pub fn auto_named(&mut self) -> &mut Self {
        self.sid = Some(Statement::new_sid());
        self
    }

    /// Set the effect of this statement to `Allow`.
    pub fn allows(&mut self) -> &mut Self {
        self.effect = Effect::Allow;
        self
    }

    /// Set the effect of this statement to `Deny`.
    pub fn does_not_allow(&mut self) -> &mut Self {
        self.effect = Effect::Deny;
        self
    }

    /// Unsets the principal associated with this statement
    pub fn unspecified_principals(&mut self) -> &mut Self {
        self.principals.clear();
        self
    }

    /// Sets the principal of this statement to be a wildcard.
    pub fn any_principal(&mut self, p_type: PrincipalType) -> &mut Self {
        self.p_direction = Some(true);
        self.principals.insert(p_type, Vec::new());
        self
    }

    /// Sets the principal of this statement to be only this value.
    pub fn only_this_principal(&mut self, p_type: PrincipalType, arn: &str) -> &mut Self {
        self.only_these_principals(p_type, vec![arn]);
        self
    }

    /// Sets the principal of this statement to be any of these values.
    pub fn only_these_principals(&mut self, p_type: PrincipalType, arns: Vec<&str>) -> &mut Self {
        match self.p_direction {
            None => self.p_direction = Some(true),
            Some(false) => panic!("you can't have principal *and* not principal"),
            _ => (),
        };
        let existing = self.principals.entry(p_type).or_default();
        existing.extend(arns.iter().map(|s| s.to_string()).collect::<Vec<String>>());
        self
    }

    /// Sets the principal of this statement to exclude this value.
    pub fn not_this_principal(&mut self, p_type: PrincipalType, arn: &str) -> &mut Self {
        self.not_these_principals(p_type, vec![arn]);
        self
    }

    /// Sets the principal of this statement to exclude of these values.
    pub fn not_these_principals(&mut self, p_type: PrincipalType, arns: Vec<&str>) -> &mut Self {
        match self.p_direction {
            None => self.p_direction = Some(false),
            Some(true) => panic!("you can't have principal *and* not principal"),
            _ => (),
        };
        let existing = self.principals.entry(p_type).or_default();
        existing.extend(arns.iter().map(|s| s.to_string()).collect::<Vec<String>>());
        self
    }

    /// Sets the action of this statement to be a wildcard.
    pub fn may_perform_any_action(&mut self) -> &mut Self {
        self.a_direction = Some(true);
        self.actions = Vec::new();
        self
    }

    /// Sets the action of this statement to be only this value.
    pub fn may_perform_action(&mut self, action: &str) -> &mut Self {
        self.may_perform_actions(vec![action]);
        self
    }

    /// Sets the action of this statement to be any of these values.
    pub fn may_perform_actions(&mut self, actions: Vec<&str>) -> &mut Self {
        match self.a_direction {
            None => self.a_direction = Some(true),
            Some(false) => panic!("you can't have action *and* not action"),
            _ => (),
        };
        self.actions.extend(
            actions
                .iter()
                .map(|s| s.parse().unwrap())
                .collect::<Vec<QString>>(),
        );
        self
    }

    /// Sets the action of this statement to exclude the wildcard.
    pub fn may_perform_no_action(&mut self) -> &mut Self {
        self.a_direction = Some(false);
        self.actions = Vec::new();
        self
    }

    /// Sets the action of this statement to exclude this value.
    pub fn may_not_perform_action(&mut self, action: &str) -> &mut Self {
        self.may_not_perform_actions(vec![action]);
        self
    }

    /// Sets the action of this statement to exclude any of these values.
    pub fn may_not_perform_actions(&mut self, actions: Vec<&str>) -> &mut Self {
        match self.a_direction {
            None => self.a_direction = Some(false),
            Some(true) => panic!("you can't have action *and* not action"),
            _ => (),
        };
        self.actions.extend(
            actions
                .iter()
                .map(|s| s.parse().unwrap())
                .collect::<Vec<QString>>(),
        );
        self
    }

    /// Sets the resource of this statement to be a wildcard.
    pub fn on_any_resource(&mut self) -> &mut Self {
        self.r_direction = Some(true);
        self.resources = Vec::new();
        self
    }

    /// Sets the resource of this statement to be only this value.
    pub fn on_resource(&mut self, resource: &str) -> &mut Self {
        self.on_resources(vec![resource]);
        self
    }

    /// Sets the resource of this statement to be any of these values.
    pub fn on_resources(&mut self, resources: Vec<&str>) -> &mut Self {
        match self.r_direction {
            None => self.r_direction = Some(true),
            Some(false) => panic!("you can't have resource *and* not resource"),
            _ => (),
        };
        self.resources.extend(
            resources
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>(),
        );
        self
    }

    /// Sets the resource of this statement to exclude the wildcard.
    pub fn on_no_resource(&mut self) -> &mut Self {
        self.r_direction = Some(false);
        self.resources = Vec::new();
        self
    }

    /// Sets the resource of this statement to exclude this value.
    pub fn not_on_resource(&mut self, resource: &str) -> &mut Self {
        self.not_on_resources(vec![resource]);
        self
    }

    /// Sets the resource of this statement to exclude any of these values.
    pub fn not_on_resources(&mut self, resources: Vec<&str>) -> &mut Self {
        match self.r_direction {
            None => self.r_direction = Some(false),
            Some(true) => panic!("you can't have resource *and* not resource"),
            _ => (),
        };
        self.resources.extend(
            resources
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>(),
        );
        self
    }

    /// Adds this condition to the statement.
    pub fn if_condition(&mut self, condition: &mut ConditionBuilder) -> &mut Self {
        if self.condition.is_none() {
            self.condition = Some(HashMap::new());
        }
        let conditions = self.condition.as_mut().unwrap();
        let existing = conditions.entry(condition.operator.clone()).or_default();
        existing.extend(condition.rhs.drain());
        self
    }
}

impl From<&mut StatementBuilder> for Statement {
    fn from(sb: &mut StatementBuilder) -> Self {
        let principal = match sb.p_direction {
            None => None,
            Some(direction) => {
                let inner: HashMap<PrincipalType, OneOrAny> = sb
                    .principals
                    .iter_mut()
                    .map(|(k, v)| {
                        (
                            k.clone(),
                            match v.len() {
                                0 => OneOrAny::Any,
                                1 => OneOrAny::One(v.remove(0)),
                                _ => OneOrAny::AnyOf(v.drain(0..).map(|s| s).collect()),
                            },
                        )
                    })
                    .collect();
                Some(if direction {
                    Principal::Principal(inner)
                } else {
                    Principal::NotPrincipal(inner)
                })
            }
        };

        let action_inner = match sb.actions.len() {
            0 => OneOrAny::Any,
            1 => OneOrAny::One(sb.actions.remove(0)),
            _ => OneOrAny::AnyOf(sb.actions.drain(0..).map(|s| s).collect()),
        };
        let action = match sb.a_direction {
            None => panic!("must have an action"),
            Some(true) => Action::Action(action_inner),
            Some(false) => Action::NotAction(action_inner),
        };

        let resource_inner = match sb.resources.len() {
            0 => OneOrAny::Any,
            1 => OneOrAny::One(sb.resources.remove(0)),
            _ => OneOrAny::AnyOf(sb.resources.drain(0..).map(|s| s).collect()),
        };
        let resource = match sb.r_direction {
            None => panic!("must have a resource"),
            Some(true) => Resource::Resource(resource_inner),
            Some(false) => Resource::NotResource(resource_inner),
        };

        Statement {
            sid: sb.sid.clone(),
            principal,
            effect: sb.effect.clone(),
            action,
            resource,
            condition: sb.condition.clone(),
        }
    }
}

impl ConditionBuilder {
    /// Create a new Condition with the provided operator.
    pub fn new(operator: GlobalConditionOperator) -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Create a new Condition with operator = `StringEquals`
    pub fn new_string_equals() -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator: GlobalConditionOperator::StringEquals,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Create a new Condition with operator = `StringNotEquals`
    pub fn new_string_not_equals() -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator: GlobalConditionOperator::StringNotEquals,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Create a new Condition with operator = `NumericEquals`
    pub fn new_numeric_equals() -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator: GlobalConditionOperator::NumericEquals,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Create a new Condition with operator = `NumericNotEquals`
    pub fn new_numeric_not_equals() -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator: GlobalConditionOperator::NumericNotEquals,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Create a new Condition with operator = `Bool`
    pub fn new_bool() -> Self {
        ConditionBuilder {
            operator: ConditionOperator {
                quantifier: None,
                operator: GlobalConditionOperator::Bool,
                only_if_exists: false,
            },
            rhs: Default::default(),
        }
    }

    /// Add the _for-all-values_ quantifier.
    pub fn for_all(&mut self) -> &mut Self {
        self.operator.quantifier = Some(ConditionOperatorQuantifier::ForAllValues);
        self
    }

    /// Add the _for-any-value_ quantifier.
    pub fn for_any(&mut self) -> &mut Self {
        self.operator.quantifier = Some(ConditionOperatorQuantifier::ForAnyValue);
        self
    }

    /// Add a list of values to the _right-hand-sidse_ of this condition.
    pub fn right_hand_side(&mut self, key: &str, values: &mut Vec<ConditionValue>) -> &mut Self {
        let values = match values.len() {
            0 => panic!("you must specify at least one value"),
            1 => OneOrAll::One(values.remove(0)),
            _ => OneOrAll::All(values.drain(0..).collect()),
        };
        self.rhs.insert(key.parse().unwrap(), values);
        self
    }

    /// Add a string value to the _right-hand-sidse_ of this condition.
    pub fn right_hand_str(&mut self, key: &str, value: &str) -> &mut Self {
        self.rhs.insert(
            key.parse().unwrap(),
            OneOrAll::One(ConditionValue::String(value.to_string())),
        );
        self
    }

    /// Add a integer value to the _right-hand-sidse_ of this condition.
    pub fn right_hand_int(&mut self, key: &str, value: i64) -> &mut Self {
        self.rhs.insert(
            key.parse().unwrap(),
            OneOrAll::One(ConditionValue::Integer(value)),
        );
        self
    }

    /// Add a float value to the _right-hand-sidse_ of this condition.
    pub fn right_hand_float(&mut self, key: &str, value: f64) -> &mut Self {
        self.rhs.insert(
            key.parse().unwrap(),
            OneOrAll::One(ConditionValue::Float(value)),
        );
        self
    }

    /// Add a boolean value to the _right-hand-sidse_ of this condition.
    pub fn right_hand_bool(&mut self, key: &str, value: bool) -> &mut Self {
        self.rhs.insert(
            key.parse().unwrap(),
            OneOrAll::One(ConditionValue::Bool(value)),
        );
        self
    }

    /// Add the _if-exists_ constraint
    pub fn if_exists(&mut self) -> &mut Self {
        self.operator.only_if_exists = true;
        self
    }

    ///
    /// Convert this one condition into a complete Condition for a statement.
    ///
    pub fn build_as_condition(
        &self,
    ) -> HashMap<ConditionOperator, HashMap<QString, OneOrAll<ConditionValue>>> {
        let mut map: HashMap<ConditionOperator, HashMap<QString, OneOrAll<ConditionValue>>> =
            HashMap::default();
        map.insert(self.operator.clone(), self.rhs.clone());
        map
    }
}

// ------------------------------------------------------------------------------------------------
// Unit Tests
// ------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::write_to_writer;
    use std::io::stdout;

    #[test]
    fn test_simple_builder() {
        let policy: Policy = PolicyBuilder::new()
            .named("confidential-data-access")
            .evaluate_statement(
                StatementBuilder::new()
                    .auto_named()
                    .allows()
                    .unspecified_principals()
                    .may_perform_actions(vec!["s3:List*", "s3:Get*"])
                    .on_resources(vec![
                        "arn:aws:s3:::confidential-data",
                        "arn:aws:s3:::confidential-data/*",
                    ])
                    .if_condition(
                        ConditionBuilder::new_bool()
                            .right_hand_bool("aws:MultiFactorAuthPresent", true)
                            .if_exists(),
                    ),
            )
            .into();
        write_to_writer(stdout(), &policy).expect("well that was unexpected");
    }
}