Esempio n. 1
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            var nameArg = commandLine.Arguments.ElementAtOrDefault(0);

            if (nameArg == null)
            {
                Console.Error.WriteLine($"*** ERROR: NAME argument is required.");
                Program.Exit(1);
            }

            var passwordName = NeonVault.ValidatePasswordName(nameArg);
            var path         = Path.Combine(KubeHelper.PasswordsFolder, passwordName);

            if (!File.Exists(path))
            {
                Console.Error.WriteLine($"*** ERROR: Password [{passwordName}] not found.");
                Program.Exit(1);
            }

            Console.Write(File.ReadAllText(path));
            Program.Exit(0);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            var nameArg   = commandLine.Arguments.ElementAtOrDefault(0);
            var sourceArg = commandLine.Arguments.ElementAtOrDefault(1);

            if (nameArg == null)
            {
                Console.Error.WriteLine($"*** ERROR: NAME argument is required.");
                Program.Exit(1);
            }

            var passwordName = NeonVault.ValidatePasswordName(nameArg);
            var password     = string.Empty;

            if (sourceArg == null)
            {
                // Generate a 20 character password.

                password = NeonHelper.GetCryptoRandomPassword(20);
            }
            else if (sourceArg == "-")
            {
                // Read the password from STDIN and trim.

                using (var stdin = NeonHelper.OpenStandardInput())
                {
                    using (var reader = new StreamReader(stdin))
                    {
                        password = reader.ReadLine().Trim();
                    }
                }
            }
            else
            {
                // Read the first line from the file.

                using (var input = new FileStream(sourceArg, FileMode.Open, FileAccess.Read))
                {
                    using (var reader = new StreamReader(input))
                    {
                        password = reader.ReadLine().Trim();
                    }
                }
            }

            if (password.Length == 0)
            {
                Console.Error.WriteLine($"*** ERROR: The password cannot be blank.");
                Program.Exit(1);
            }

            File.WriteAllText(Path.Combine(KubeHelper.PasswordsFolder, passwordName), password);
            Program.Exit(0);
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public override async Task RunAsync(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            Console.WriteLine();

            var nameArg = commandLine.Arguments.ElementAtOrDefault(0);
            var force   = commandLine.HasOption("--force");

            if (nameArg == null)
            {
                Console.Error.WriteLine($"*** ERROR: NAME argument is required.");
                Program.Exit(1);
            }

            if (nameArg == "*")
            {
                if (!force && !Program.PromptYesNo("Are you sure you want to remove all passwords?"))
                {
                    Program.Exit(0);
                }

                foreach (var path in Directory.GetFiles(KubeHelper.PasswordsFolder))
                {
                    File.Delete(path);
                }
            }
            else
            {
                var passwordName = NeonVault.ValidatePasswordName(nameArg);
                var passwordPath = Path.Combine(KubeHelper.PasswordsFolder, passwordName);

                if (!File.Exists(passwordPath))
                {
                    Console.Error.WriteLine($"*** ERROR: The [{passwordName}] password does not exist.");
                    Program.Exit(1);
                }
                else
                {
                    if (!force && !Program.PromptYesNo($"Are you sure you want to remove the [{passwordName}] password?"))
                    {
                        Program.Exit(0);
                    }

                    File.Delete(passwordPath);
                }
            }

            Program.Exit(0);
            await Task.CompletedTask;
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public override async Task RunAsync(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            var zipPath   = commandLine.Arguments.ElementAtOrDefault(0);
            var firstName = commandLine.Arguments.ElementAtOrDefault(1);
            var fromStdin = commandLine.HasOption("--stdin");
            var names     = new List <string>();

            if (zipPath == null)
            {
                Console.Error.WriteLine("*** ERROR: PATH argument is required.");
                Program.Exit(1);
            }

            if (firstName == null)
            {
                Console.Error.WriteLine("*** ERROR: At least one NAME argument is required.");
                Program.Exit(1);
            }

            if (firstName == "*")
            {
                foreach (var path in Directory.GetFiles(KubeHelper.PasswordsFolder))
                {
                    names.Add(Path.GetFileName(path));
                }
            }
            else
            {
                foreach (var name in commandLine.Arguments.Skip(1))
                {
                    var validatedName = NeonVault.ValidatePasswordName(name);

                    if (!File.Exists(Path.Combine(KubeHelper.PasswordsFolder, validatedName)))
                    {
                        Console.Error.WriteLine($"*** ERROR: Password [{validatedName}] does not exist.");
                        Program.Exit(1);
                    }

                    names.Add(validatedName);
                }
            }

            if (names.Count == 0)
            {
                Console.Error.WriteLine("*** ERROR: No passwords selected for export.");
                Program.Exit(1);
            }

            var zipPassword = (string)null;

            if (fromStdin)
            {
                // Read the password from STDIN and trim.

                using (var stdin = NeonHelper.OpenStandardInput())
                {
                    using (var reader = new StreamReader(stdin))
                    {
                        zipPassword = reader.ReadLine().Trim();
                    }
                }
            }

retryPassword:

            if (!fromStdin)
            {
                if (string.IsNullOrEmpty(zipPassword))
                {
                    zipPassword = NeonHelper.ReadConsolePassword("Enter Password:   "******"Confirm Password: "******"The passwords don't match.  Please try again:");
                    Console.WriteLine();

                    goto retryPassword;
                }
            }

            using (var zip = ZipFile.Create(zipPath))
            {
                zip.Password = zipPassword;
                zip.BeginUpdate();

                foreach (var name in names)
                {
                    zip.Add(Path.Combine(KubeHelper.PasswordsFolder, name), name);
                }

                zip.CommitUpdate();
            }

            Console.WriteLine();
            Console.WriteLine($"[{names.Count}] passwords exported.");
            Program.Exit(0);
            await Task.CompletedTask;
        }