Esempio n. 1
0
        public void run(string[] args, ChocolateyConfiguration config, Container container)
        {
            var commandLine = Environment.CommandLine;

            if (ArgumentsUtility.arguments_contain_sensitive_information(commandLine))
            {
                this.Log().Debug(() => "Command line not shown - sensitive arguments may have been passed.");
            }
            else
            {
                this.Log().Debug(() => "Command line: {0}".format_with(commandLine));
                this.Log().Debug(() => "Received arguments: {0}".format_with(string.Join(" ", args)));
            }

            IList <string> commandArgs = new List <string>();
            //shift the first arg off
            int count = 0;

            foreach (var arg in args)
            {
                if (count == 0)
                {
                    count += 1;
                    continue;
                }

                commandArgs.Add(arg);
            }

            var runner = new GenericRunner();

            runner.run(config, container, isConsole: true, parseArgs: command =>
            {
                if (!ChocolateyOptionSet.Instance.Parse(commandArgs, command, config))
                {
                    this.Log().Debug(() => "Performing validation checks.");
                    command.handle_validation(config);

                    var validationResults = new List <ValidationResult>();
                    var validationChecks  = container.GetAllInstances <IValidation>();
                    foreach (var validationCheck in validationChecks)
                    {
                        validationResults.AddRange(validationCheck.validate(config));
                    }

                    var validationErrors = report_validation_summary(validationResults, config);

                    if (validationErrors != 0)
                    {
                        // NOTE: This is intentionally left blank, as the reason for throwing is
                        // documented in the report_validation_summary above, and a duplication
                        // is not required in the exception.
                        throw new ApplicationException("");
                    }
                }
            });
        }
Esempio n. 2
0
        public void run(string[] args, ChocolateyConfiguration config, Container container)
        {
            var commandLine = Environment.CommandLine;

            if (ArgumentsUtility.arguments_contain_sensitive_information(commandLine))
            {
                this.Log().Debug(() => "Command line not shown - sensitive arguments may have been passed.");
            }
            else
            {
                this.Log().Debug(() => "Command line: {0}".format_with(commandLine));
                this.Log().Debug(() => "Received arguments: {0}".format_with(string.Join(" ", args)));
            }

            IList <string> commandArgs = new List <string>();
            //shift the first arg off
            int count = 0;

            foreach (var arg in args)
            {
                if (count == 0)
                {
                    count += 1;
                    continue;
                }

                commandArgs.Add(arg);
            }

            var runner = new GenericRunner();

            runner.run(config, container, isConsole: true, parseArgs: command =>
            {
                ConfigurationOptions.parse_arguments_and_update_configuration(
                    commandArgs,
                    config,
                    (optionSet) => command.configure_argument_parser(optionSet, config),
                    (unparsedArgs) => {
                    // if debug is bundled with local options, it may not get picked up when global
                    // options are parsed. Attempt to set it again once local options are set.
                    // This does mean some output from debug will be missed (but not much)
                    if (config.Debug)
                    {
                        Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string()));
                    }

                    command.handle_additional_argument_parsing(unparsedArgs, config);

                    if (!config.Features.IgnoreInvalidOptionsSwitches)
                    {
                        // all options / switches should be parsed,
                        //  so show help menu if there are any left
                        foreach (var unparsedArg in unparsedArgs.or_empty_list_if_null())
                        {
                            if (unparsedArg.StartsWith("-") || unparsedArg.StartsWith("/"))
                            {
                                config.HelpRequested       = true;
                                config.UnsuccessfulParsing = true;
                            }
                        }
                    }
                },
                    () => command.handle_validation(config),
                    () => command.help_message(config));
            });
        }
Esempio n. 3
0
 public override void Because()
 {
     _result = ArgumentsUtility.arguments_contain_sensitive_information(_commandArguments);
 }
Esempio n. 4
0
        public IEnumerable <string> DecryptPackageArgumentsFile(string id, string version)
        {
            var argumentsPath = _fileSystem.combine_paths(_chocolateyConfigurationProvider.ChocolateyInstall, ".chocolatey", "{0}.{1}".format_with(id, version));
            var argumentsFile = _fileSystem.combine_paths(argumentsPath, ".arguments");

            string arguments = string.Empty;

            // Get the arguments decrypted in here and return them
            try
            {
                if (_fileSystem.file_exists(argumentsFile))
                {
                    arguments = _fileSystem.read_file(argumentsFile);
                }
            }
            catch (Exception ex)
            {
                var message = L(nameof(Resources.Application_PackageArgumentsError), version, id);
                Logger.Error(ex, message);
            }

            if (string.IsNullOrEmpty(arguments))
            {
                Logger.Debug(
                    string.Empty,
                    L(nameof(Resources.PackageView_UnableToFindArgumentsFile), version, id));
                yield break;
            }

            // The following code is borrowed from the Chocolatey codebase, should
            // be extracted to a separate location in choco executable so we can re-use it.
            var packageArgumentsUnencrypted = arguments.contains(" --") && arguments.to_string().Length > 4
                ? arguments
                : _encryptionUtility.decrypt_string(arguments);

            // Lets do a global check first to see if there are any sensitive arguments
            // before we filter out the values used later.
            var sensitiveArgs = ArgumentsUtility.arguments_contain_sensitive_information(packageArgumentsUnencrypted);

            var packageArgumentsSplit =
                packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var packageArgument in packageArgumentsSplit.or_empty_list_if_null())
            {
                var isSensitiveArgument = sensitiveArgs && ArgumentsUtility.arguments_contain_sensitive_information(packageArgument);

                var packageArgumentSplit =
                    packageArgument.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries);

                var optionName  = packageArgumentSplit[0].to_string();
                var optionValue = string.Empty;

                if (packageArgumentSplit.Length == 2 && isSensitiveArgument)
                {
                    optionValue = L(nameof(Resources.PackageArgumentService_RedactedArgument));
                }
                else if (packageArgumentSplit.Length == 2)
                {
                    optionValue = packageArgumentSplit[1].to_string().remove_surrounding_quotes();
                    if (optionValue.StartsWith("'"))
                    {
                        optionValue.remove_surrounding_quotes();
                    }
                }

                yield return("--{0}{1}".format_with(
                                 optionName,
                                 string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue));
            }
        }