public void SetApiKey_DefaultSource()
        {
            using (var testFolder = TestDirectory.Create())
            {
                var configFile = Path.Combine(testFolder, "nuget.config");
                Util.CreateFile(configFile, "<configuration/>");

                var testApiKey = Guid.NewGuid().ToString();

                // Act
                var result = CommandRunner.Run(
                    NuGetExePath,
                    testFolder,
                    $"setApiKey {testApiKey} -ConfigFile {configFile}",
                    waitForExit: true);

                // Assert
                Assert.True(0 == result.Item1, $"{result.Item2} {result.Item3}");
                Assert.Contains($"The API Key '{testApiKey}' was saved for the NuGet gallery (https://www.nuget.org) and the symbol server (https://nuget.smbsrc.net/)", result.Item2);

                var settings = Configuration.Settings.LoadDefaultSettings(
                    Path.GetDirectoryName(configFile),
                    Path.GetFileName(configFile),
                    null);

                var actualApiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, NuGetConstants.DefaultGalleryServerUrl);
                Assert.NotNull(actualApiKey);
                Assert.Equal(testApiKey, actualApiKey);
            }
        }
Esempio n. 2
0
        public void SetApiKey_DefaultSource()
        {
            using (var testFolder = TestDirectory.Create())
            {
                var configFile = Path.Combine(testFolder, "nuget.config");
                Util.CreateFile(configFile, "<configuration/>");

                var testApiKey = Guid.NewGuid().ToString();

                // Act
                var result = CommandRunner.Run(
                    NuGetExePath,
                    testFolder,
                    $"setApiKey {testApiKey} -ConfigFile {configFile}",
                    waitForExit: true);

                // Assert
                Assert.True(0 == result.ExitCode, $"{result.Output} {result.Errors}");
                Assert.Contains($"The API Key '{testApiKey}' was saved for the NuGet gallery (https://www.nuget.org)", result.Output);
                Assert.DoesNotContain($"symbol", result.Output);

                var settings = Configuration.Settings.LoadDefaultSettings(
                    Path.GetDirectoryName(configFile),
                    Path.GetFileName(configFile),
                    null);

                var actualApiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, NuGetConstants.DefaultGalleryServerUrl);
                Assert.NotNull(actualApiKey);
                Assert.Equal(testApiKey, actualApiKey);
                XElement apiKeySection = SimpleTestSettingsContext.GetOrAddSection(XmlUtility.Load(configFile), ConfigurationConstants.ApiKeys);
                Assert.Equal(1, apiKeySection.Elements().Count());
            }
        }
Esempio n. 3
0
        public void GetDecryptedValueForAddItem_WithNullSettings_Throws()
        {
            var ex = Record.Exception(() => SettingsUtility.GetDecryptedValueForAddItem(settings: null, section: "randomSection", key: "randomKey"));

            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 4
0
        public void GetDecryptedValueForAddItem_ReturnsDecryptedValue()
        {
            // Arrange
            var configFile = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, @"<configuration></configuration>");
                var settings = new Settings(mockBaseDirectory);

                SettingsUtility.SetEncryptedValueForAddItem(settings, "SectionName", "key", "value");

                // Act
                var result = SettingsUtility.GetDecryptedValueForAddItem(settings, "SectionName", "key");

                // Assert
                result.Should().Be("value");
            }
        }
Esempio n. 5
0
        public static string GetApiKey(ISettings settings, string endpoint, string source)
        {
            // try searching API key by endpoint first
            // needed to support config key mappings like 'https://www.nuget.org/api/v2/package'
            var apiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, endpoint);

            // if not found try finding it by source url
            apiKey = apiKey ?? SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, source);

            // fallback for a case of nuget.org source
            // try to retrieve an api key mapped to a default "gallery" url
            if (apiKey == null &&
                UriUtility.IsNuGetOrg(source))
            {
                var defaultConfigKey = NuGetConstants.DefaultGalleryServerUrl;
                apiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, defaultConfigKey);
            }

            // return an API key when found or null when not found
            return(apiKey);
        }
Esempio n. 6
0
        public static string GetApiKey(ISettings settings, string endpoint, string source, string defaultApiKey, bool isSymbolApiKey)
        {
            // try searching API key by endpoint first
            // needed to support config key mappings like 'https://www.nuget.org/api/v2/package'
            var apiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, endpoint);

            // if not found try finding it by source url
            apiKey = apiKey ?? SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, source);

            // fallback for a case of nuget.org source
            // try to retrieve an api key mapped to a default "gallery" url
            if (apiKey == null &&
                source.IndexOf(NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase) >= 0)
            {
                var defaultConfigKey = isSymbolApiKey ? NuGetConstants.DefaultSymbolServerUrl : NuGetConstants.DefaultGalleryServerUrl;
                apiKey = SettingsUtility.GetDecryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, defaultConfigKey);
            }

            // return an API key when found or the default one
            return(apiKey ?? defaultApiKey);
        }
Esempio n. 7
0
        public void GetDecryptedValueForAddItem_WithNoKey_ReturnsNull()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <add key=""key"" value=""value"" />
    </SectionName>
</configuration>";

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

                // Act
                var result = SettingsUtility.GetDecryptedValueForAddItem(settings, "SectionName", "NoKeyByThatName");

                // Assert
                result.Should().BeNull();
            }
        }
Esempio n. 8
0
        internal static string GetDecryptedValue([NotNull] this PackageSource packageSource, [NotNull] INuGetPackageSources nuGetPackageSources, [NotNull] string sectionName)
        {
            if (packageSource == null)
            {
                throw new ArgumentNullException(nameof(packageSource));
            }
            if (nuGetPackageSources == null)
            {
                throw new ArgumentNullException(nameof(nuGetPackageSources));
            }
            if (sectionName == null)
            {
                throw new ArgumentNullException(nameof(sectionName));
            }

            var nuGetSupportsEncryption = nuGetPackageSources.IsPasswordEncryptionSupported();

            var decryptedValue = nuGetSupportsEncryption ?
                                 SettingsUtility.GetDecryptedValueForAddItem(nuGetPackageSources.Settings, sectionName, packageSource.Source) :
                                 SettingsUtility.GetValueForAddItem(nuGetPackageSources.Settings, sectionName, packageSource.Source);

            return(string.IsNullOrWhiteSpace(decryptedValue) ? null : decryptedValue);
        }
Esempio n. 9
0
        public void SetApiKey_WithSpecifiedSource_SetApiKeyBySourceKey(string serverUri)
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                // Add source into NuGet.Config file
                SimpleTestSettingsContext settings = pathContext.Settings;
                var    packageSourcesSection       = SimpleTestSettingsContext.GetOrAddSection(settings.XML, ConfigurationConstants.PackageSources);
                string sourceKey = serverUri.Contains("random") ? "random" : serverUri.Contains("contos") ? "contoso" : "nuget.org";
                SimpleTestSettingsContext.AddEntry(packageSourcesSection, sourceKey, serverUri);
                settings.Save();

                var testApiKey = Guid.NewGuid().ToString();

                // Act
                var result = CommandRunner.Run(
                    NuGetExePath,
                    pathContext.WorkingDirectory,
                    $"setApiKey {testApiKey} -Source {sourceKey} -ConfigFile {settings.ConfigPath}",
                    waitForExit: true);

                var iSettings = Configuration.Settings.LoadDefaultSettings(
                    Path.GetDirectoryName(settings.ConfigPath),
                    Path.GetFileName(settings.ConfigPath),
                    null);

                // Assert
                Assert.True(0 == result.ExitCode, $"{result.Output} {result.Errors}");
                Assert.Contains($"The API Key '{testApiKey}' was saved for '{serverUri}'", result.Output);
                Assert.DoesNotContain($"symbol", result.Output);

                var actualApiKey = SettingsUtility.GetDecryptedValueForAddItem(iSettings, ConfigurationConstants.ApiKeys, serverUri);
                Assert.Equal(testApiKey, actualApiKey);
                XElement apiKeySection = SimpleTestSettingsContext.GetOrAddSection(XmlUtility.Load(settings.ConfigPath), ConfigurationConstants.ApiKeys);
                Assert.Equal(1, apiKeySection.Elements().Count());
            }
        }