Пример #1
0
        public static void Run(UpdateSourceArgs args, Func <ILogger> getLogger)
        {
            var settings       = RunnerHelper.GetSettings(args.Configfile);
            var sourceProvider = RunnerHelper.GetSourceProvider(settings);

            var existingSource = sourceProvider.GetPackageSourceByName(args.Name);

            if (existingSource == null)
            {
                throw new CommandException(Strings.SourcesCommandNoMatchingSourcesFound, args.Name);
            }

            if (!string.IsNullOrEmpty(args.Source) && !existingSource.Source.Equals(args.Source, StringComparison.OrdinalIgnoreCase))
            {
                if (!PathValidator.IsValidSource(args.Source))
                {
                    throw new CommandException(Strings.SourcesCommandInvalidSource);
                }

                // If the user is updating the source, verify we don't have a duplicate.
                var duplicateSource = sourceProvider.GetPackageSourceBySource(args.Source);
                if (duplicateSource != null)
                {
                    throw new CommandException(Strings.SourcesCommandUniqueSource);
                }

                existingSource = new Configuration.PackageSource(args.Source, existingSource.Name);

                // If the existing source is not http, warn the user
                if (existingSource.IsHttp && !existingSource.IsHttps)
                {
                    getLogger().LogWarning(string.Format(CultureInfo.CurrentCulture, Strings.Warning_HttpServerUsage, "update source", args.Source));
                }
            }

            RunnerHelper.ValidateCredentials(args.Username, args.Password, args.ValidAuthenticationTypes);

            if (!string.IsNullOrEmpty(args.Username))
            {
                var hasExistingAuthTypes = existingSource.Credentials?.ValidAuthenticationTypes.Any() ?? false;
                if (hasExistingAuthTypes && string.IsNullOrEmpty(args.ValidAuthenticationTypes))
                {
                    getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                         Strings.SourcesCommandClearingExistingAuthTypes, args.Name));
                }

                var credentials = Configuration.PackageSourceCredential.FromUserInput(
                    args.Name,
                    args.Username,
                    args.Password,
                    args.StorePasswordInClearText,
                    args.ValidAuthenticationTypes);
                existingSource.Credentials = credentials;
            }

            sourceProvider.UpdatePackageSource(existingSource, updateCredentials: existingSource.Credentials != null, updateEnabled: false);

            getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                 Strings.SourcesCommandUpdateSuccessful, args.Name));
        }
Пример #2
0
        public static void Run(DisableSourceArgs args, Func <ILogger> getLogger)
        {
            var settings       = RunnerHelper.GetSettings(args.Configfile);
            var sourceProvider = RunnerHelper.GetSourceProvider(settings);

            RunnerHelper.EnableOrDisableSource(sourceProvider, args.Name, enable: false, getLogger);
        }
        public static void Run(AddClientCertArgs args, Func <ILogger> getLogger)
        {
            args.Validate();
            var settings = RunnerHelper.GetSettings(args.Configfile);
            var clientCertificateProvider = new ClientCertificateProvider(settings);

            var item = clientCertificateProvider.GetClientCertificate(args.PackageSource);

            if (item != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_ClientCertificateAlreadyExist,
                                                          args.PackageSource));
            }

            if (args.IsFileCertSettingsProvided())
            {
                item = new FileClientCertItem(args.PackageSource,
                                              args.Path,
                                              args.Password,
                                              args.StorePasswordInClearText,
                                              args.Configfile);
            }
            else if (args.IsStoreCertSettingsProvided())
            {
                item = new StoreClientCertItem(args.PackageSource,
                                               args.FindValue,
                                               args.GetStoreLocation(),
                                               args.GetStoreName(),
                                               args.GetFindBy());
            }
            else
            {
                throw new CommandLineArgumentCombinationException(string.Format(CultureInfo.CurrentCulture,
                                                                                Strings.Error_UnknownClientCertificateStoreType));
            }

            try
            {
                var certificates = item.Search();
                if (!certificates.Any())
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_ClientCertificatesNotFound));
                }
            }
            catch
            {
                if (!args.Force)
                {
                    throw;
                }
            }


            clientCertificateProvider.AddOrUpdate(item);

            getLogger().LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.ClientCertificateSuccessfullyAdded, args.PackageSource));
        }
Пример #4
0
        public static void Run(RemoveSourceArgs args, Func <ILogger> getLogger)
        {
            var settings       = RunnerHelper.GetSettings(args.Configfile);
            var sourceProvider = RunnerHelper.GetSourceProvider(settings);

            // Check to see if we already have a registered source with the same name or source
            var source = sourceProvider.GetPackageSourceByName(args.Name);

            if (source == null)
            {
                throw new CommandException(Strings.SourcesCommandNoMatchingSourcesFound, args.Name);
            }

            sourceProvider.RemovePackageSource(args.Name);
            getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                 Strings.SourcesCommandSourceRemovedSuccessfully, args.Name));
        }
Пример #5
0
        public static void Run(RemoveClientCertArgs args, Func <ILogger> getLogger)
        {
            args.Validate();

            var settings = RunnerHelper.GetSettings(args.Configfile);
            var clientCertificateProvider = new ClientCertificateProvider(settings);

            var item = clientCertificateProvider.GetClientCertificate(args.PackageSource);

            if (item == null)
            {
                getLogger().LogInformation(string.Format(CultureInfo.CurrentCulture,
                                                         Strings.NoClientCertificatesMatching,
                                                         args.PackageSource));
                return;
            }

            clientCertificateProvider.Remove(new[] { item });

            getLogger().LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.ClientCertificateSuccessfullyRemoved, args.PackageSource));
        }
        public static void Run(ListClientCertArgs args, Func <ILogger> getLogger)
        {
            var settings = RunnerHelper.GetSettings(args.Configfile);
            var clientCertificateProvider = new ClientCertificateProvider(settings);

            var items = clientCertificateProvider.GetClientCertificates();

            if (!items.Any())
            {
                getLogger().LogInformation(Strings.NoClientCertificates);
                return;
            }

            var clientCertificatesLogs = new List <LogMessage>();

            getLogger().LogInformation(Strings.RegsiteredClientCertificates);
            getLogger().LogInformation(string.Empty);

            var defaultIndentation = string.Empty.PadRight(PaddingWidth);

            for (var i = 0; i < items.Count; i++)
            {
                var item = items[i];

                var builder          = new StringBuilder();
                var indexIndentation = $" {i + 1}.".PadRight(PaddingWidth);

                builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesLogTitle, indexIndentation, item.PackageSource, item.ElementName);
                builder.AppendLine();

                switch (item)
                {
                case FileClientCertItem fileCertItem:
                {
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesFileCertFilePath, defaultIndentation, fileCertItem.FilePath);
                    builder.AppendLine();

                    if (string.IsNullOrEmpty(fileCertItem.Password))
                    {
                        builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesFileCertNoPassword, defaultIndentation);
                    }
                    else
                    {
                        builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesFileCertWithPassword, defaultIndentation);
                    }

                    builder.AppendLine();

                    break;
                }

                case StoreClientCertItem storeCertItem:
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesStoreCertStoreLocation, defaultIndentation, StoreClientCertItem.GetString(storeCertItem.StoreLocation));
                    builder.AppendLine();
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesStoreCertStoreName, defaultIndentation, StoreClientCertItem.GetString(storeCertItem.StoreName));
                    builder.AppendLine();
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesStoreCertFindBy, defaultIndentation, StoreClientCertItem.GetString(storeCertItem.FindType));
                    builder.AppendLine();
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesStoreCertFindValue, defaultIndentation, storeCertItem.FindValue);
                    builder.AppendLine();
                    break;
                }

                try
                {
                    var certificates = item.Search();
                    foreach (var certificate in certificates)
                    {
                        builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesItemCertificateMessage, defaultIndentation, certificate.GetCertHashString());
                        builder.AppendLine();
                    }
                }
                catch (Exception e)
                {
                    builder.AppendFormat(CultureInfo.CurrentCulture, Strings.ClientCertificatesItemCertificateError, defaultIndentation, e.GetBaseException().Message);
                    builder.AppendLine();
                }

                clientCertificatesLogs.Add(new LogMessage(LogLevel.Information, builder.ToString()));
            }

            getLogger().LogMessages(clientCertificatesLogs);
        }
Пример #7
0
        public static void Run(AddSourceArgs args, Func <ILogger> getLogger)
        {
            var settings       = RunnerHelper.GetSettings(args.Configfile);
            var sourceProvider = RunnerHelper.GetSourceProvider(settings);

            if (string.IsNullOrEmpty(args.Name))
            {
                // find first unused name of pattern: prefixN, where N is an integer.
                string defaultNamePrefix = Strings.Source_DefaultNamePrefix;
                var    namesSet          = sourceProvider.GetPackageSourceNamesMatchingNamePrefix(defaultNamePrefix);
                int    i = 1;
                while (true)
                {
                    var defaultNameToUse = defaultNamePrefix + i.ToString();
                    if (!namesSet.Contains(defaultNameToUse))
                    {
                        args.Name = defaultNameToUse;
                        break;
                    }
                    i++;
                }
            }
            else if (string.Equals(args.Name, Strings.ReservedPackageNameAll))
            {
                throw new CommandException(Strings.SourcesCommandAllNameIsReserved);
            }

            // Make sure that the Source given is a valid one.
            if (!PathValidator.IsValidSource(args.Source))
            {
                throw new CommandException(Strings.SourcesCommandInvalidSource);
            }

            RunnerHelper.ValidateCredentials(args.Username, args.Password, args.ValidAuthenticationTypes);

            // Check to see if we already have a registered source with the same name or source
            var existingSourceWithName = sourceProvider.GetPackageSourceByName(args.Name);

            if (existingSourceWithName != null)
            {
                throw new CommandException(Strings.SourcesCommandUniqueName);
            }

            var existingSourceWithSource = sourceProvider.GetPackageSourceBySource(args.Source);

            if (existingSourceWithSource != null)
            {
                throw new CommandException(Strings.SourcesCommandUniqueSource);
            }

            var newPackageSource = new Configuration.PackageSource(args.Source, args.Name);

            if (newPackageSource.IsHttp && !newPackageSource.IsHttps)
            {
                getLogger().LogWarning(
                    string.Format(CultureInfo.CurrentCulture,
                                  Strings.Warning_HttpServerUsage,
                                  "add source",
                                  args.Source));
            }

            if (!string.IsNullOrEmpty(args.Username))
            {
                var credentials = Configuration.PackageSourceCredential.FromUserInput(
                    args.Name,
                    args.Username,
                    args.Password,
                    args.StorePasswordInClearText,
                    args.ValidAuthenticationTypes);
                newPackageSource.Credentials = credentials;
            }

            sourceProvider.AddPackageSource(newPackageSource);
            getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                 Strings.SourcesCommandSourceAddedSuccessfully, args.Name));
        }
Пример #8
0
        public static void Run(ListSourceArgs args, Func <ILogger> getLogger)
        {
            SourcesListFormat format;

            if (string.IsNullOrEmpty(args.Format))
            {
                format = SourcesListFormat.Detailed;
            }
            else
            {
                Enum.TryParse <SourcesListFormat>(args.Format, ignoreCase: true, out format);
            }

            switch (format)
            {
            case SourcesListFormat.Detailed:
            {
                var settings       = RunnerHelper.GetSettings(args.Configfile);
                var sourceProvider = RunnerHelper.GetSourceProvider(settings);

                var sourcesList = sourceProvider.LoadPackageSources().ToList();
                if (!sourcesList.Any())
                {
                    getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                         Strings.SourcesCommandNoSources));

                    return;
                }

                getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandRegisteredSources));
                var sourcePadding = new string(' ', 6);
                for (var i = 0; i < sourcesList.Count; i++)
                {
                    var source      = sourcesList[i];
                    var indexNumber = i + 1;
                    var namePadding = new string(' ', i >= 9 ? 1 : 2);

                    getLogger().LogMinimal(string.Format(
                                               "  {0}.{1}{2} [{3}]",
                                               indexNumber,
                                               namePadding,
                                               source.Name,
                                               source.IsEnabled ? string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandEnabled) : string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandDisabled)));
                    getLogger().LogMinimal(string.Format("{0}{1}", sourcePadding, source.Source));
                }

                WarnForHttpSources(sourcesList, getLogger);
            }
            break;

            case SourcesListFormat.Short:
            {
                var settings       = RunnerHelper.GetSettings(args.Configfile);
                var sourceProvider = RunnerHelper.GetSourceProvider(settings);

                var sourcesList = sourceProvider.LoadPackageSources();

                foreach (var source in sourcesList)
                {
                    string legend = source.IsEnabled ? "E" : "D";

                    if (source.IsMachineWide)
                    {
                        legend += "M";
                    }
                    if (source.IsOfficial)
                    {
                        legend += "O";
                    }
                    legend += " ";
                    getLogger().LogMinimal(legend + source.Source);
                }

                WarnForHttpSources(sourcesList, getLogger);
            }
            break;

            case SourcesListFormat.None:
                // This validation could move to the Command or Args and be code-generated.
                throw new CommandException(string.Format(Strings.Source_InvalidFormatValue, args.Format));
            }
        }
        public static void Run(UpdateClientCertArgs args, Func <ILogger> getLogger)
        {
            args.Validate();

            var settings = RunnerHelper.GetSettings(args.Configfile);
            var clientCertificateProvider = new ClientCertificateProvider(settings);

            var item = clientCertificateProvider.GetClientCertificate(args.PackageSource);

            if (item == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_ClientCertificateNotExist,
                                                          args.PackageSource));
            }

            switch (item)
            {
            case FileClientCertItem fileCertItem:
                if (args.IsStoreCertSettingsProvided())
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Strings.Error_ClientCertificateTypeMismatch,
                                                              args.PackageSource));
                }

                fileCertItem.Update(args.Path, args.Password, args.StorePasswordInClearText);
                break;

            case StoreClientCertItem storeCertItem:
                if (args.IsFileCertSettingsProvided())
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Strings.Error_ClientCertificateTypeMismatch,
                                                              args.PackageSource));
                }

                storeCertItem.Update(args.FindValue,
                                     args.GetStoreLocation(),
                                     args.GetStoreName(),
                                     args.GetFindBy());
                break;
            }

            try
            {
                var certificates = item.Search();
                if (!certificates.Any())
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_ClientCertificatesNotFound));
                }
            }
            catch
            {
                if (!args.Force)
                {
                    throw;
                }
            }

            clientCertificateProvider.AddOrUpdate(item);

            getLogger().LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.ClientCertificateSuccessfullyUpdated, args.PackageSource));
        }