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));
        }
        public void StoreClientCert_FromDocumentation_ParsedCorrectly()
        {
            // Arrange
            var config = @"
<configuration>
   <SectionName>
      <storeCert packageSource=""Contoso""
           storeLocation = ""currentUser""
           storeName = ""my""
           findBy = ""thumbprint""
           findValue = ""4894671ae5aa84840cc1079e89e82d426bc24ec6"" />
   </SectionName>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);

                // Act and Assert
                var settingsFile = new SettingsFile(mockBaseDirectory);
                var section      = settingsFile.GetSection("SectionName");
                section.Should().NotBeNull();
                var items = section.Items.ToList();

                items.Count.Should().Be(1);

                var storeClientCertItem = (StoreClientCertItem)items[0];
                storeClientCertItem.ElementName.Should().Be("storeCert");
                storeClientCertItem.PackageSource.Should().Be("Contoso");
                storeClientCertItem.StoreLocation.Should().Be(StoreLocation.CurrentUser);
                storeClientCertItem.StoreName.Should().Be(StoreName.My);
                storeClientCertItem.FindType.Should().Be(X509FindType.FindByThumbprint);
                storeClientCertItem.FindValue.Should().Be("4894671ae5aa84840cc1079e89e82d426bc24ec6");

                var expectedStoreClientCertItem = new StoreClientCertItem("Contoso",
                                                                          "4894671ae5aa84840cc1079e89e82d426bc24ec6",
                                                                          StoreLocation.CurrentUser,
                                                                          StoreName.My,
                                                                          X509FindType.FindByThumbprint);
                SettingsTestUtils.DeepEquals(storeClientCertItem, expectedStoreClientCertItem).Should().BeTrue();
            }
        }
        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);
        }
예제 #4
0
 private static bool StoreClientCertItem_DeepEquals(StoreClientCertItem item1, StoreClientCertItem item2)
 {
     return(ItemBase_DeepEquals(item1, item2));
 }