Пример #1
0
        private static List <string> ListCredentials(eListingType listingType)
        {
            Console.WriteLine();
            List <string> sortedNames = ProfileManager.ListProfileNames().OrderBy(p => p).ToList();

            Console.WriteLine("=== Stored Credentials ===");

            if (sortedNames.Count == 0)
            {
                Console.WriteLine("No credentials are currently stored.  Type A to add one.");
            }
            else if (listingType == eListingType.concise)
            {
                ListConcise(sortedNames);
            }
            else
            {
                ListStandard(listingType, sortedNames);
            }
            return(sortedNames);
        }
Пример #2
0
        private static void ListStandard(eListingType listingType, List <string> sortedNames)
        {
            var    currentDefaultCredential = GetDefaultCredential();
            int    maxLength   = sortedNames.Max(n => n.Length);
            string fmtShowAll  = string.Format("{{0,-{0}}} {{1}}\n{1} {{2}} {{3}}\r\n", maxLength, new string(' ', maxLength));
            string fmtStandard = string.Format("{{0,-{0}}} {{1}} {{2}}\r\n", maxLength);

            StringBuilder sbOut = new StringBuilder();

            foreach (var profileName in sortedNames)
            {
                var    creds            = ProfileManager.GetAWSCredentials(profileName).GetCredentials();
                string defaultIndicator = string.Empty;
                if (string.Compare(profileName, "default", true) == 0)
                {
                    defaultIndicator = string.Format("Careful!\n{0} See note on defaults: {1}", new string(' ', maxLength), MY_URL);
                }
                else if (creds.AccessKey == currentDefaultCredential)
                {
                    defaultCredName  = profileName;
                    defaultIndicator = "(default)";
                }

                switch (listingType)
                {
                case eListingType.standard:
                    sbOut.AppendFormat(fmtStandard, profileName, creds.AccessKey, defaultIndicator);
                    break;

                case eListingType.showall:
                    sbOut.AppendFormat(string.Format(fmtShowAll, profileName, creds.AccessKey, creds.SecretKey, defaultIndicator));
                    break;
                }
            }
            Console.WriteLine(sbOut);
        }
Пример #3
0
        static void Main(string[] args)
        {
            try
            {
                char          input;
                bool          showPrompt           = true;
                List <string> names                = null;
                bool          showCredentialSecret = false;
                eListingType  listingType          = eListingType.concise;

                do
                {
                    if (showPrompt)
                    {
                        Console.WriteLine();
                        names = ListCredentials(listingType);

                        Console.WriteLine("A: Add    stored credential\tR: Remove stored credential");
                        Console.WriteLine("U: Update stored credential\tE: Rename stored credential");
                        Console.WriteLine("S: Set    default credential");
                        Console.WriteLine("P: Push   to a Named Profile");
                        Console.WriteLine("W: Whois  the associated IAM user");
                        Console.WriteLine("X: Exit\n");
                    }

                    var    keyInfo = Console.ReadKey(true);
                    string sKey    = keyInfo.Key.ToString();
                    if (sKey.Length > 1)
                    {
                        input = char.MinValue;  //omit non-characters (e.g. F1, Escape)
                    }
                    else
                    {
                        input = sKey.ToLower()[0];
                    }

                    showPrompt = true;

                    switch (input)
                    {
                    case 'a':
                    {
                        var profileName = ReadLine("New profile name: "); if (profileName.Length == 0)
                        {
                            break;
                        }
                        var accessKey = ReadLine("Access key: "); if (accessKey.Length == 0)
                        {
                            break;
                        }
                        var secretKey = ReadLine("Secret key: "); if (secretKey.Length == 0)
                        {
                            break;
                        }
                        ProfileManager.RegisterProfile(profileName, accessKey, secretKey);
                        break;
                    }

                    case 'r':
                    {
                        Console.Write("Select credential to remove (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names);
                        if (!string.IsNullOrEmpty(selected))
                        {
                            Console.WriteLine("\nRemoving " + selected);
                            ProfileManager.UnregisterProfile(selected);
                        }
                        break;
                    }

                    case 'e':
                    {
                        Console.Write("Rename credential (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names);
                        if (string.IsNullOrEmpty(selected))
                        {
                            break;
                        }

                        var newName = ReadLine("Rename to: ");
                        if (string.IsNullOrEmpty(newName))
                        {
                            break;
                        }

                        //create new one credential, delete old one.
                        var olcCredential = ProfileManager.GetAWSCredentials(selected).GetCredentials();
                        ProfileManager.RegisterProfile(newName, olcCredential.AccessKey, olcCredential.SecretKey);
                        ProfileManager.UnregisterProfile(selected);
                        break;
                    }

                    case 'u':
                    {
                        Console.Write("Update credential (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names); if (string.IsNullOrEmpty(selected))
                        {
                            break;
                        }

                        //Determine if the cred being changed  is the default
                        var isDefault = ProfileManager.GetAWSCredentials(selected).GetCredentials().AccessKey == GetDefaultCredential();

                        var accessKey = ReadLine("Access key: "); if (accessKey.Length == 0)
                        {
                            break;
                        }
                        var secretKey = ReadLine("Secret key: "); if (secretKey.Length == 0)
                        {
                            break;
                        }
                        ProfileManager.UnregisterProfile(selected);
                        ProfileManager.RegisterProfile(selected, accessKey, secretKey);

                        //if it was a default credential, automatically reset it.
                        if (isDefault)
                        {
                            Console.WriteLine("\nResetting default credential.");
                            SetDefaultCredential(ProfileManager.GetAWSCredentials(selected));
                        }

                        break;
                    }

                    case 's':
                    case 'd':
                    {
                        Console.Write("Set credential as default (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names);
                        if (!string.IsNullOrEmpty(selected))
                        {
                            Console.WriteLine("\nSetting default to " + selected);
                            AWSCredentials creds = ProfileManager.GetAWSCredentials(selected);
                            SetDefaultCredential(creds);
                        }
                        break;
                    }

                    //whois information on a credential (select)
                    //should default to current default
                    case 'w':
                    {
                        Console.Write("Choose credential (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names, defaultCredName);
                        if (!string.IsNullOrEmpty(selected))
                        {
                            AWSCredentials creds = ProfileManager.GetAWSCredentials(selected);
                            ReportWhois(creds);
                            showPrompt = false;
                        }
                        break;
                    }

                    //push credential to named credential
                    case 'p':
                    {
                        Console.Write("Select credential to push (use arrows or type): ");
                        string selected = Utilities.StringChoice.Read(names);
                        if (!string.IsNullOrEmpty(selected))
                        {
                            //var region = ReadRegion();
                            AWSCredentials creds = ProfileManager.GetAWSCredentials(selected);
                            AddStoredCredential(creds, selected);
                        }

                        break;
                    }

                    case 'c':
                    //concise listing


                    case 'l':
                        listingType++;
                        listingType = (eListingType)((int)listingType % Enum.GetValues(typeof(eListingType)).Length);
                        if (keyInfo.Modifiers == ConsoleModifiers.Control)
                        {
                            showCredentialSecret = !showCredentialSecret;
                        }
                        break;

                    //version info
                    case 'v':
                        Console.WriteLine(VersionInfo);
                        showPrompt = false;
                        break;

                    //test
                    case 'z':
                    {
                        //List<string> choices = null;  //test case 1
                        //List<string> choices = new List<string>(); //test case 2
                        List <string> choices = new List <string>()
                        {
                            "one", "two", "three", "aaa", "bbb", "bbc", "bba", "ccc"
                        };
                        Console.Write("Make your choice: ");
                        string selected = Utilities.StringChoice.Read(choices, 2);
                        Console.WriteLine("You chose: " + selected);
                        break;
                    }

                    default:
                        showPrompt = false;
                        break;
                    }
                } while (input != 'x' && input != 'q');
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
            }
        }