credstash 0.9.2

Credstash is a utility for managing credentials in the AWS cloud
Documentation
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
use clap::error::ErrorKind::{DisplayHelp, DisplayVersion};
use clap::{Arg, ArgAction, Command};
use credstash::{CredStashClient, CredStashCredential};
use either::Either;
use futures::future::join_all;
use ring::hmac::Algorithm;
use rusoto_core::region::Region;
use rusoto_dynamodb::AttributeValue;
use serde_json::{map::Map, to_string_pretty, Value};
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::Write;
use std::str::{self, FromStr};

#[derive(Debug, PartialEq, Clone)]
struct CredstashApp {
    region: Option<String>,
    table_name: Option<String>,
    credential: CredStashCredential,
    action: Action,
}

#[derive(Debug)]
pub enum CredStashAppError {
    VersionError(String),
    ClapError(clap::Error),
    MissingEnv(String),
    InsufficientContext(String),
    InvalidArguments(String),
    MissingCredential,
    MissingCredentialValue,
    DigestAlgorithmNotSupported(String),
    ClientError(credstash::CredStashClientError),
    InvalidAction(String),
    ParseError(String),
    IOError(String),
    InsertionError(String),
}

#[derive(Debug, PartialEq, Clone)]
enum AutoIncrement {
    AutoIncrement,
}

#[derive(Debug, PartialEq, Clone)]
enum ExportOption {
    Json,
    Yaml,
    Csv,
    DotEnv,
}

#[derive(Debug, PartialEq, Clone)]
struct GetAllOpts {
    version: Option<u64>,
    encryption_context: Vec<(String, String)>,
    export: ExportOption,
}

#[derive(Debug, PartialEq, Clone)]
struct PutOpts {
    key_id: Option<String>,
    comment: Option<String>,
    version: Either<u64, AutoIncrement>,
    digest_algorithm: Algorithm,
}

#[derive(Debug, PartialEq, Clone)]
struct Credential {
    name: String,
    value: String,
}

#[derive(Debug, PartialEq, Clone)]
struct PutAllOpts {
    key_id: Option<String>,
    comment: Option<String>,
    version: Either<u64, AutoIncrement>,
    digest_algorithm: Algorithm,
    content: Vec<Credential>,
    encryption_context: Vec<(String, String)>,
}

#[derive(Debug, PartialEq, Clone)]
struct SetupOpts {
    tags: Vec<(String, String)>,
}

#[derive(Debug, PartialEq, Clone)]
struct GetOpts {
    noline: bool,
    version: Option<u64>,
}

#[derive(Debug, PartialEq, Clone)]
enum Action {
    Delete(String),
    Get(String, Vec<(String, String)>, GetOpts),
    GetAll(Option<GetAllOpts>),
    Keys,
    List,
    Put(String, String, Vec<(String, String)>, PutOpts),
    PutAll(PutAllOpts),
    Setup(SetupOpts),
    Invalid(String),
}

fn parse_credential(content: String) -> Result<Vec<Credential>, CredStashAppError> {
    let credential: serde_json::Value = serde_json::from_str(&content)?;
    let mut credential_value = vec![];
    let mut result = true;
    match credential {
        serde_json::Value::Object(val) => {
            let msg = format!(
                "JSON parsing issue. Expecting an object of key value pairs, but instead got {}",
                content
            );
            for (key, value) in val {
                if value.is_string() {
                    let secret_value = value
                        .as_str()
                        .map_or(Err(CredStashAppError::ParseError(msg.clone())), |val| {
                            Ok(val.to_string())
                        })?;
                    credential_value.push(Credential {
                        name: key,
                        value: secret_value,
                    });
                } else {
                    result = false;
                }
            }
        }
        _ => {
            result = false;
        }
    }
    if result {
        Ok(credential_value)
    } else {
        let msg = format!(
            "JSON parsing issue. Expecting an object of key value pairs, but instead got {}",
            content
        );
        Err(CredStashAppError::ParseError(msg))
    }
}

#[test]
fn parse_credential_check() {
    let result = vec![
        Credential {
            name: "hello".to_string(),
            value: "world".to_string(),
        },
        Credential {
            name: "hi".to_string(),
            value: "bye".to_string(),
        },
    ];
    assert_eq!(
        parse_credential(r#"{"hello":"world", "hi":"bye"}"#.to_string()).unwrap(),
        result
    );
}

fn render_secret(secret: Vec<u8>) -> Result<String, CredStashAppError> {
    match str::from_utf8(&secret) {
        Ok(v) => Ok(v.to_string()),
        Err(err) => Err(CredStashAppError::ParseError(format!(
            "Decode failure: {}",
            err
        ))),
    }
}

fn render_comment(comment: Option<String>) -> String {
    comment.unwrap_or_default()
}

fn to_algorithm(digest: String) -> Result<Algorithm, CredStashAppError> {
    match digest.as_ref() {
        "SHA1" => Ok(ring::hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY),
        "SHA256" => Ok(ring::hmac::HMAC_SHA256),
        "SHA384" => Ok(ring::hmac::HMAC_SHA384),
        "SHA512" => Ok(ring::hmac::HMAC_SHA512),
        _ => Err(CredStashAppError::DigestAlgorithmNotSupported(format!(
            "Unsupported digest algorithm: {}",
            digest
        ))),
    }
}

fn get_table_name(table_name: Option<String>) -> String {
    table_name.map_or("credential-store".to_string(), |name| name)
}

async fn handle_action(
    app: CredstashApp,
    client: CredStashClient,
) -> Result<(), CredStashAppError> {
    let table_name = get_table_name(app.table_name);
    match app.action {
        Action::PutAll(putall_opts) => match putall_opts.version {
            Either::Left(version) => {
                let result = join_all(putall_opts.content.clone().into_iter().map(|item| {
                    client.put_secret(
                        table_name.clone(),
                        item.name,
                        item.value,
                        putall_opts.key_id.clone(),
                        putall_opts.encryption_context.clone(),
                        Some(version),
                        putall_opts.comment.clone(),
                        putall_opts.digest_algorithm,
                    )
                }))
                .await;
                let mut exit_status = true;
                for (status, credential) in result.iter().zip(putall_opts.content.iter()) {
                    if status.is_ok() {
                        println!("{} has been stored", credential.name);
                    } else {
                        exit_status = false;
                        eprintln!("Error in storing the credential {}", credential.name)
                    }
                }
                if exit_status {
                    Ok(())
                } else {
                    Err(CredStashAppError::InsertionError(
                        "Error in put operation for the credentails".to_string(),
                    ))
                }
            }
            Either::Right(_) => {
                let result = join_all(putall_opts.content.clone().into_iter().map(|item| {
                    client.put_secret_auto_version(
                        table_name.clone(),
                        item.name,
                        item.value,
                        putall_opts.key_id.clone(),
                        putall_opts.encryption_context.clone(),
                        putall_opts.comment.clone(),
                        putall_opts.digest_algorithm,
                    )
                }))
                .await;
                let mut exit_status = true;
                for (status, credential) in result.iter().zip(putall_opts.content.iter()) {
                    if status.is_ok() {
                        println!("{} has been stored", credential.name);
                    } else {
                        exit_status = false;
                        eprintln!("Error in storing the credential {}", credential.name)
                    }
                }
                if exit_status {
                    Ok(())
                } else {
                    Err(CredStashAppError::InsertionError(
                        "Error in put operation for the credentails".to_string(),
                    ))
                }
            }
        },
        Action::Put(credential_name, credential_value, encryption_context, put_opts) => {
            match put_opts.version {
                Either::Left(version) => {
                    client
                        .put_secret(
                            table_name.clone(),
                            credential_name.clone(),
                            credential_value.clone(),
                            put_opts.key_id,
                            encryption_context.clone(),
                            Some(version),
                            put_opts.comment,
                            put_opts.digest_algorithm,
                        )
                        .await?
                }
                Either::Right(_) => {
                    client
                        .put_secret_auto_version(
                            table_name,
                            credential_name.clone(),
                            credential_value,
                            put_opts.key_id,
                            encryption_context,
                            put_opts.comment,
                            put_opts.digest_algorithm,
                        )
                        .await?
                }
            };
            println!("{} has been stored", credential_name);
            Ok(())
        }
        Action::List => {
            let items = client.list_secrets(table_name).await?;
            let max_name_len: Vec<usize> = items.iter().map(|item| item.name.len()).collect();
            let max_len = max_name_len
                .iter()
                .fold(1, |acc, x| if acc < *x { *x } else { acc });
            for item in items {
                println!(
                    "{:width$} -- version {: <10} --comment {}",
                    item.name,
                    item.version,
                    render_comment(item.comment),
                    width = max_len
                );
            }
            Ok(())
        }
        Action::Delete(credential) => {
            let items = client.delete_secret(table_name, credential.clone()).await?;
            for item in items {
                println!(
                    "Deleting {} {}",
                    credential,
                    render_version(item.attributes)
                );
            }
            Ok(())
        }
        Action::Setup(setup_opts) => {
            client.create_db_table(table_name, setup_opts.tags).await?;
            println!("Creation initiated");
            Ok(())
        }

        Action::Keys => {
            let items = client.list_secrets(table_name).await?;
            for item in items {
                println!("{}", item.name)
            }
            Ok(())
        }

        Action::Get(credential_name, encryption_context, get_opts) => {
            let item = client
                .get_secret(
                    table_name,
                    credential_name,
                    encryption_context,
                    get_opts.version,
                )
                .await?;
            if get_opts.noline {
                print!("{}", render_secret(item.credential_value)?)
            } else {
                println!("{}", render_secret(item.credential_value)?)
            }
            Ok(())
        }
        Action::GetAll(get_opts) => {
            let version = get_opts.clone().map(|opts| opts.version).unwrap_or(None);
            let encryption_context = get_opts
                .clone()
                .map(|opts| opts.encryption_context)
                .unwrap_or_default();
            let val = client
                .get_all_secrets(table_name, encryption_context, version)
                .await?;
            match get_opts {
                None => (),
                Some(opts) => match opts.export {
                    ExportOption::Json => render_json_credstash_item(val)?,
                    ExportOption::Yaml => render_yaml_credstash_item(val)?,
                    ExportOption::Csv => render_csv_credstash_item(val)?,
                    ExportOption::DotEnv => render_dotenv_credstash_item(val)?,
                },
            }
            Ok(())
        }
        Action::Invalid(msg) => Err(CredStashAppError::InvalidAction(msg)),
    }
}

fn render_csv_credstash_item(val: Vec<credstash::CredstashItem>) -> Result<(), CredStashAppError> {
    for item in val {
        println!(
            "{},{}",
            item.credential_name,
            render_secret(item.credential_value)?
        )
    }
    Ok(())
}

fn render_dotenv_credstash_item(
    val: Vec<credstash::CredstashItem>,
) -> Result<(), CredStashAppError> {
    for item in val {
        println!(
            "{}='{}'",
            item.credential_name,
            render_secret(item.credential_value)?
        )
    }
    Ok(())
}

fn render_yaml_credstash_item(val: Vec<credstash::CredstashItem>) -> Result<(), CredStashAppError> {
    for item in val {
        println!(
            "{}: {}",
            item.credential_name,
            render_secret(item.credential_value)?
        )
    }
    Ok(())
}

fn render_json_credstash_item(val: Vec<credstash::CredstashItem>) -> Result<(), CredStashAppError> {
    let mut items: Map<_, _> = Map::new();
    for item in val {
        let credential_value = render_secret(item.credential_value)?;
        items.insert(item.credential_name, Value::String(credential_value));
    }
    let result: Result<String, _> = to_string_pretty(&Value::Object(items));
    match result {
        Ok(val) => {
            println!("{}", val);
            Ok(())
        }
        Err(err) => {
            let err_msg = format!("Serde JSON error: {}", err);
            Err(CredStashAppError::ParseError(err_msg))
        }
    }
}

impl From<clap::Error> for CredStashAppError {
    fn from(error: clap::Error) -> Self {
        CredStashAppError::ClapError(error)
    }
}

impl From<std::io::Error> for CredStashAppError {
    fn from(error: std::io::Error) -> Self {
        CredStashAppError::IOError(error.to_string())
    }
}

impl From<credstash::CredStashClientError> for CredStashAppError {
    fn from(error: credstash::CredStashClientError) -> Self {
        CredStashAppError::ClientError(error)
    }
}

impl From<serde_json::error::Error> for CredStashAppError {
    fn from(error: serde_json::error::Error) -> Self {
        CredStashAppError::ParseError(error.to_string())
    }
}

impl CredstashApp {
    fn new() -> Result<Self, CredStashAppError> {
        Self::new_from(std::env::args_os())
    }

    fn new_from<I, T>(args: I) -> Result<Self, CredStashAppError>
    where
        I: Iterator<Item = T>,
        T: Into<OsString> + Clone,
    {
        let version: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
        let app: Command = Command::new("rucredstash")
            .version(version.map_or(
                Err(CredStashAppError::MissingEnv(
                    "CARGO_PKG_VERSION environment variable not present".to_string(),
                )),
                Ok,
            )?)
            .about("A credential/secret storage system")
            .author("Sibi Prabakaran");

        let region_arg = Arg::new("region")
            .long("region")
            .short('r')
            .value_name("REGION")
            .help(
                "the AWS region in which to operate. If a region is \
                 not specified, credstash will use the value of the \
                 AWS_DEFAULT_REGION env variable, or if that is not \
                 set, the value in `~/.aws/config`. As a last resort, \
                 it will use us-east-1",
            );

        let table_arg = Arg::new("table")
            .long("table")
            .short('t')
            .value_name("TABLE")
            .help(
                "DynamoDB table to use for credential storage. If \
                 not specified, credstash will use the value of the \
                 CREDSTASH_DEFAULT_TABLE env variable, or if that is \
                 not set, the value `credential-store` will be used",
            );

        let profile_arg = Arg::new("profile")
            .long("profile")
            .short('p')
            .value_name("PROFILE")
            .help("Boto config profile to use when connecting to AWS");

        let arn_arg = Arg::new("arn")
            .long("arn")
            .short('a')
            .value_name("ARN")
            .help("AWS IAM ARN for AssumeRole")
            .conflicts_with("profile");

        let mfa_arg = Arg::new("mfa")
            .long("mfa_serial")
            .short('m')
            .value_name("MFA_SERIAL")
            .help("Optional MFA hardware device serial number or virtual device ARN")
            .conflicts_with("profile");

        let del_command = Command::new("delete")
            .about("Delete a credential from the store")
            .arg(
                Arg::new("credential")
                    .help("Delete a credential from the store")
                    .required(true),
            );

        let get_command = Command::new("get")
            .about("Get a credential from the store")
            .arg(
                Arg::new("credential")
                    .help("the name of the credential to get")
                    .required(true)
            ).arg(
                Arg::new("context")
                    .help("encryption context key/value pairs associated with the credential in the form of key=value")
                    .action(clap::ArgAction::Append)
	    )
            .arg(Arg::new("noline").short('n').long("noline").help("Don't append newline to returned value (useful in scripts or with binary files)").action(ArgAction::SetTrue))
            .arg(Arg::new("version").short('v').long("version").value_name("VERSION").help("Get a specific version of the credential (defaults to the latest version"));

        let get_all_command = Command::new("getall")
            .about("Get all credentials from the store")
            .arg(Arg::new("context")
                 .help("encryption context key/value pairs associated with the credential in the form of key=value")
		 .action(clap::ArgAction::Append)
            ).arg(Arg::new("version").short('v').long("version").value_name("VERSION").help("Get a specific version of the credential (defaults to the latest version")).arg(Arg::new("format").short('f').long("format").value_name("FORMAT").help("Output format. json(default) yaml, csv or dotenv.").ignore_case(true).value_parser(["json", "yaml", "csv", "dotenv"]));

        let keys_command = Command::new("keys").about("List all keys in the store");

        let list_command = Command::new("list").about("List credentials and their versions");

        let put_command = Command::new("put")
            .about("Put a credential into the store")
            .arg(Arg::new("credential").help("the name of the credential to store").required(true))
            .arg(Arg::new("value").help("the value of the credential to store").required(true).conflicts_with("prompt"))
            .arg(Arg::new("context").help("encryption context key/value pairs associated with the credential in the form of key=value").action(clap::ArgAction::Append))
            .arg(Arg::new("key").short('k').long("key").value_name("KEY").help("the KMS key-id of the master key to use. Defaults to alias/credstash"))
            .arg(Arg::new("comment").short('c').long("comment").value_name("COMMENT").help("Include reference information or a comment about value to be stored."))
            .arg(Arg::new("version").short('v').long("version").value_name("VERSION").help("Put a specific version of the credential (update the credential; defaults to version `1`)"))
            .arg(Arg::new("autoversion").short('a').long("autoversion").help("Automatically increment the version of the credential to be stored.").action(ArgAction::SetTrue).conflicts_with("version"))
            .arg(Arg::new("digest").short('d').long("digest").value_name("DIGEST").help("the hashing algorithm used to to encrypt the data. Defaults to SHA256.").ignore_case(true).value_parser(["SHA1", "SHA256", "SHA384", "SHA512"]))
            .arg(Arg::new("prompt").short('p').long("prompt").action(ArgAction::SetTrue).help("Prompt for secret"));

        let put_all_command = Command::new("putall")
            .about("Put credentials from json or file into the store")
            .arg(Arg::new("credentials").help("the value of the credential to store or, if beginning with the \"@\" \
                                                     character, the filename of the file containing the values, or \
                                                     pass \"-\" to read the values from stdin. Should be in json format.").required(true))
            .arg(Arg::new("context").help("encryption context key/value pairs associated with the credential in the form of key=value"))
            .arg(Arg::new("key").short('k').long("key").value_name("KEY").help("the KMS key-id of the master key to use. Defaults to alias/credstash"))
            .arg(Arg::new("version").short('v').long("version").value_name("VERSION").help("Put a specific version of the credential (update the credential; defaults to version `1`)"))
            .arg(Arg::new("comment").short('c').long("comment").value_name("COMMENT").help("Include reference information or a comment about value to be stored."))
            .arg(Arg::new("autoversion").short('a').long("autoversion").action(ArgAction::SetTrue).help("Automatically increment the version of the credential to be stored.").conflicts_with("version"))
            .arg(Arg::new("digest").short('d').long("digest").value_name("DIGEST").help("the hashing algorithm used to to encrypt the data. Defaults to SHA256.").ignore_case(true).value_parser(["SHA1", "SHA256", "SHA384", "SHA512"]));

        let setup_command = Command::new("setup").about("setup the credential store").arg(Arg::new("tags").value_name("TAGS").help("Tags to apply to the Dynamodb Table passed in as a space sparated list of Key=Value").long("tags").short('t'));
        let app = app
            .arg(region_arg)
            .arg(table_arg)
            .arg(profile_arg)
            .arg(arn_arg)
            .arg(mfa_arg)
            .subcommand(del_command)
            .subcommand(get_command)
            .subcommand(get_all_command)
            .subcommand(keys_command)
            .subcommand(list_command)
            .subcommand(put_command)
            .subcommand(put_all_command)
            .subcommand(setup_command);
        // extract the matches

        log::debug!("Application initialized");

        let matches: clap::ArgMatches = app.try_get_matches_from(args)?;

        log::debug!("ArgMatches parsed");

        let region: Option<&String> = matches.get_one("region");

        log::debug!("AWS Region: {:?}", region);

        let action_value: Action = match matches.subcommand() {
            Some(("get", get_matches)) => {
                let credential: &String = get_matches
                    .get_one("credential")
                    .expect("Credential not supplied");
                let context: Option<Vec<_>> = get_matches.get_many("context").and_then(|e| {
                    e.map(|item: &String| split_context_to_tuple(item.to_string()).ok())
                        .collect()
                });

                let encryption_context: Vec<_> = match context {
                    None => vec![],
                    Some(x) => x,
                };
                let version = get_matches.get_one("version").map(|ver: &String| {
                    ver.to_string()
                        .parse::<u64>()
                        .expect("Version should be positive integer")
                });
                let get_opts = GetOpts {
                    noline: get_matches.get_flag("noline"),
                    version,
                };
                Action::Get(credential.into(), encryption_context, get_opts)
            }
            Some(("getall", get_matches)) => {
                let context: Option<Vec<_>> = get_matches.get_many("context").and_then(|e| {
                    e.map(|item: &String| split_context_to_tuple(item.to_string()).ok())
                        .collect()
                });

                let encryption_context: Vec<_> = match context {
                    None => vec![],
                    Some(x) => x,
                };
                let version = get_matches.get_one("version").map(|ver: &String| {
                    ver.to_string()
                        .parse::<u64>()
                        .expect("Version should be positive integer")
                });
                let export_type =
                    get_matches
                        .get_one("format")
                        .map_or(ExportOption::Json, |export: &String| {
                            match export.to_lowercase().as_ref() {
                                "csv" => ExportOption::Csv,
                                "yaml" => ExportOption::Yaml,
                                "dotenv" => ExportOption::DotEnv,
                                _ => ExportOption::Json,
                            }
                        });
                let getall_opts = GetAllOpts {
                    version,
                    encryption_context,
                    export: export_type,
                };
                Action::GetAll(Some(getall_opts))
            }
            Some(("keys", _)) => Action::Keys,
            Some(("list", _)) => Action::List,
            Some(("setup", setup_matches)) => {
                let tags = setup_matches.get_many("tags");
                let tags_options: Option<Vec<String>> =
                    tags.map(|values| values.map(|item: &String| item.to_string()).collect());
                let table_tags: Option<Vec<(String, String)>> = tags_options.map(|item| {
                    item.into_iter()
                        .filter_map(|item| split_tags_to_tuple(item).ok())
                        .collect()
                });
                let setup_opts = SetupOpts {
                    tags: table_tags.map_or(vec![], |tag| tag),
                };
                Action::Setup(setup_opts)
            }
            Some(("putall", putall_matches)) => {
                let credential_name: String = putall_matches
                    .get_one("credentials")
                    .map_or(Err(CredStashAppError::MissingCredential), |val: &String| {
                        Ok(val.to_string())
                    })?;
                let mut credential_content = String::new();
                let credential_value: Vec<Credential> = match credential_name.chars().next() {
                    Some('@') => {
                        let mut filename = credential_name.clone();
                        filename.remove(0);
                        let mut file = File::open(filename)?;
                        file.read_to_string(&mut credential_content)?;
                        parse_credential(credential_content)?
                    }
                    Some('-') => {
                        let stdout = io::stdout();
                        let mut std_handle = stdout.lock();
                        std_handle.flush().ok();
                        io::stdin()
                            .read_line(&mut credential_content)
                            .expect("Failed to read from stdin");
                        parse_credential(credential_content)?
                    }
                    _ => parse_credential(credential_name)?,
                };

                let key_id = putall_matches
                    .get_one("key")
                    .map(|e: &String| e.to_string());
                let comment = putall_matches
                    .get_one("comment")
                    .map(|e: &String| e.to_string());
                let version: Either<u64, AutoIncrement> = {
                    let version_option =
                        putall_matches.get_one("version").map_or(1, |e: &String| {
                            e.to_string()
                                .parse::<u64>()
                                .expect("Version should be positive integer")
                        });
                    let autoversion = putall_matches.contains_id("autoversion");
                    if autoversion {
                        Either::Right(AutoIncrement::AutoIncrement)
                    } else {
                        Either::Left(version_option)
                    }
                };
                let digest_algorithm = putall_matches
                    .get_one("digest")
                    .map_or(Ok(ring::hmac::HMAC_SHA256), |e: &String| {
                        to_algorithm(e.to_string())
                    })?;

                let context: Option<Vec<_>> = putall_matches.get_many("context").and_then(|e| {
                    e.map(|item: &String| split_context_to_tuple(item.to_string()).ok())
                        .collect()
                });
                let encryption_context: Vec<_> = match context {
                    None => vec![],
                    Some(x) => x,
                };
                let putall_opts = PutAllOpts {
                    key_id,
                    comment,
                    version,
                    digest_algorithm,
                    content: credential_value,
                    encryption_context,
                };
                Action::PutAll(putall_opts)
            }
            Some(("put", put_matches)) => {
                log::debug!("Put Action");
                let credential_name: String = put_matches
                    .get_one("credential")
                    .map_or(Err(CredStashAppError::MissingCredential), |val: &String| {
                        Ok(val.to_string())
                    })?;
                log::debug!("Credential name: {}", credential_name);
                let credential_value: String = {
                    let mut value = String::new();
                    let get_input = put_matches.get_flag("prompt");
                    if get_input {
                        print!("{}: ", credential_name);
                        let stdout = io::stdout();
                        let mut std_handle = stdout.lock();
                        std_handle.flush().ok();
                        io::stdin()
                            .read_line(&mut value)
                            .expect("Failed to read from stdin");
                    } else {
                        value = put_matches.get_one("value").map_or(
                            Err(CredStashAppError::MissingCredentialValue),
                            |val: &String| Ok(val.to_string()),
                        )?;
                    }
                    value.trim().to_string()
                };
                log::debug!("Credential value: {}", credential_value);
                let key_id = put_matches.get_one("key").map(|e: &String| e.to_string());
                log::debug!("Key ID: {:?}", key_id);
                let comment = put_matches
                    .get_one("comment")
                    .map(|e: &String| e.to_string());
                log::debug!("Comment: {:?}", comment);
                let version: Either<u64, AutoIncrement> = {
                    let version_option = put_matches.get_one("version").map_or(1, |e: &String| {
                        e.to_string()
                            .parse::<u64>()
                            .expect("Version should be positive integer")
                    });
                    let autoversion = put_matches.get_flag("autoversion");
                    if autoversion {
                        Either::Right(AutoIncrement::AutoIncrement)
                    } else {
                        Either::Left(version_option)
                    }
                };
                log::debug!("Version: {:?}", version);
                let digest_algorithm = put_matches
                    .get_one("digest")
                    .map_or(Ok(ring::hmac::HMAC_SHA256), |e: &String| {
                        to_algorithm(e.to_string())
                    })?;

                let put_opts = PutOpts {
                    key_id,
                    comment,
                    version,
                    digest_algorithm,
                };
                let context: Option<Vec<_>> = put_matches.get_many("context").and_then(|e| {
                    e.map(|item: &String| split_context_to_tuple(item.to_string()).ok())
                        .collect()
                });
                log::debug!("Context: {:?}", context);
                let encryption_context: Vec<_> = match context {
                    None => vec![],
                    Some(x) => x,
                };
                Action::Put(
                    credential_name,
                    credential_value,
                    encryption_context,
                    put_opts,
                )
            }
            Some(("delete", del_matches)) => {
                let credential: String = del_matches
                    .get_one("credential")
                    .map_or(Err(CredStashAppError::MissingCredential), |val: &String| {
                        Ok(val.to_string())
                    })?;
                Action::Delete(credential)
            }
            Some((subcommand, _)) => {
                let err_msg = format!("Invalid Subcommand {} found. Use --help to see accepted subcommands and option", subcommand);
                Action::Invalid(err_msg)
            }
            None => {
                let err_msg =
                    "Invalid Subcommand found. Use --help to see accepted subcommands and option";
                Action::Invalid(err_msg.into())
            }
        };

        log::debug!("Action type: {:?}", action_value);

        let table_name = {
            match env::var("CREDSTASH_DEFAULT_TABLE ") {
                Ok(val) => Some(val),
                Err(_) => matches.get_one("table").map(|r: &String| r.to_string()),
            }
        };
        let mfa = matches.get_one("mfa").map(|r: &String| {
            print!("Enter MFA Code: ");
            let mut value = String::new();
            let stdout = io::stdout();
            let mut std_handle = stdout.lock();
            std_handle.flush().ok();
            io::stdin()
                .read_line(&mut value)
                .expect("Failed to read from stdin");
            (r.to_string(), value.trim().to_string())
        });
        let credential_type = {
            let assume_role = matches
                .get_one("arn")
                .map(|r: &String| CredStashCredential::DefaultAssumeRole((r.to_string(), mfa)));
            let profile_cred = matches
                .get_one("profile")
                .map(|r: &String| CredStashCredential::DefaultProfile(Some(r.to_string())));
            match (assume_role, profile_cred) {
                (Some(cred), _) => cred,
                (_, Some(cred)) => cred,
                _ => CredStashCredential::DefaultCredentialsProvider,
            }
        };
        Ok(CredstashApp {
            region: region.map(|r| r.to_string()),
            credential: credential_type,
            table_name,
            action: action_value,
        })
    }
}

fn split_context_to_tuple(
    encryption_context: String,
) -> Result<(String, String), CredStashAppError> {
    let context: Vec<&str> = encryption_context.split('=').collect();
    match context.len() {
        0 => Err(CredStashAppError::InsufficientContext(
            "No context supplied".to_string(),
        )),
        1 => {
            let msg = format!(
                "error: argument context: {} is not the form of key=value",
                encryption_context
            );
            Err(CredStashAppError::InsufficientContext(msg))
        }
        2 => Ok((context[0].to_string(), context[1].to_string())),
        _ => {
            let msg = format!(
                "error: argument context: {} is not the form of key=value",
                encryption_context
            );
            Err(CredStashAppError::InsufficientContext(msg))
        }
    }
}

fn split_tags_to_tuple(encryption_context: String) -> Result<(String, String), CredStashAppError> {
    let context: Vec<&str> = encryption_context.split('=').collect();
    match context.len() {
        0 => Err(CredStashAppError::InvalidArguments(
            "No arguments passed".to_string(),
        )),
        1 => {
            let msg = format!(
                "argument --tags: {} is not the form of key=value",
                encryption_context
            );
            Err(CredStashAppError::InvalidArguments(msg))
        }
        2 => Ok((context[0].to_string(), context[1].to_string())),
        _ => {
            let msg = format!(
                "argument --tags: {} is not the form of key=value",
                encryption_context
            );
            Err(CredStashAppError::InvalidArguments(msg))
        }
    }
}

fn render_version(item: Option<HashMap<String, AttributeValue>>) -> String {
    item.map_or("".to_string(), |hmap| {
        hmap.get("version").map_or("".to_string(), |version| {
            version
                .s
                .as_ref()
                .map_or("".to_string(), |ver| format!("--version {}", ver))
        })
    })
}

fn handle_error(error: CredStashAppError) {
    match error {
        CredStashAppError::VersionError(error_message) => program_exit(&error_message),
        CredStashAppError::ClapError(clap_error) => {
            if clap_error.kind() == DisplayHelp || clap_error.kind() == DisplayVersion {
                eprintln!("{}", &clap_error.to_string())
            } else {
                program_exit(&clap_error.to_string())
            }
        }
        CredStashAppError::MissingEnv(error_message) => program_exit(&error_message),
        CredStashAppError::InsufficientContext(error_message) => program_exit(&error_message),
        CredStashAppError::InvalidArguments(error_message) => program_exit(&error_message),
        CredStashAppError::MissingCredential => program_exit("Missing credentials"),
        CredStashAppError::MissingCredentialValue => program_exit("Missing credential value"),
        CredStashAppError::DigestAlgorithmNotSupported(error_message) => {
            program_exit(&error_message)
        }
        CredStashAppError::ClientError(credstash_error) => handle_credstash_error(credstash_error),
        CredStashAppError::InvalidAction(error_message) => program_exit(&error_message),
        CredStashAppError::ParseError(error_message) => program_exit(&error_message),
        CredStashAppError::IOError(error_message) => program_exit(&error_message),
        CredStashAppError::InsertionError(error_message) => program_exit(&error_message),
    }
}

fn handle_credstash_error(error: credstash::CredStashClientError) {
    match error {
        credstash::CredStashClientError::NoKeyFound => {
            program_exit("No key found in the remote DynamoDB")
        }
        credstash::CredStashClientError::AWSDynamoError(error_message) => {
            program_exit(&error_message)
        }
        credstash::CredStashClientError::AWSKMSError(error_message) => program_exit(&error_message),
        credstash::CredStashClientError::CredstashDecodeFalure(error_message) => {
            program_exit(&error_message.to_string())
        }
        credstash::CredStashClientError::CredstashHexFailure(error_message) => {
            program_exit(&error_message.to_string())
        }
        credstash::CredStashClientError::HMacMismatch => {
            program_exit("No key found in the remote DynamoDB")
        }
        credstash::CredStashClientError::ParseError(error_message) => program_exit(&error_message),
        credstash::CredStashClientError::CredentialsError(error_message) => {
            program_exit(&error_message)
        }

        credstash::CredStashClientError::TlsError(error_message) => program_exit(&error_message),
        credstash::CredStashClientError::DigestAlgorithmNotSupported(error_message) => {
            program_exit(&error_message)
        }
    }
}

fn program_exit(msg: &str) {
    eprintln!("{}", msg);
    std::process::exit(1);
}

fn init_logger() {
    use env_logger::{Builder, Target};
    let mut builder = Builder::from_default_env();
    builder.target(Target::Stderr).init();
}

#[tokio::main]
async fn main() {
    init_logger();
    log::debug!("Logger initialization done");
    let credstash_app = CredstashApp::new();
    match credstash_app {
        Ok(app) => {
            let region: Option<Region> = {
                app.clone().region.map_or(Some(Region::default()), |item| {
                    Some(Region::from_str(&item).expect("Invalid region supplied"))
                })
            };
            match CredStashClient::new(app.credential.clone(), region) {
                Ok(client) => match handle_action(app, client).await {
                    Ok(()) => (),
                    Err(error) => handle_error(error),
                },
                Err(error) => handle_error(CredStashAppError::ClientError(error)),
            }
        }
        Err(error) => handle_error(error),
    }
}