internal static SnapNugetFeed BuildSnapNugetFeed([NotNull] this PackageSource packageSource, [NotNull] INuGetPackageSources nuGetPackageSources) { if (packageSource == null) { throw new ArgumentNullException(nameof(packageSource)); } if (nuGetPackageSources == null) { throw new ArgumentNullException(nameof(nuGetPackageSources)); } var apiKey = packageSource.GetDecryptedValue(nuGetPackageSources, ConfigurationConstants.ApiKeys); var snapFeed = new SnapNugetFeed { Name = packageSource.Name, Source = packageSource.SourceUri, ProtocolVersion = (NuGetProtocolVersion)packageSource.ProtocolVersion, Username = packageSource.Credentials?.Username, Password = packageSource.Credentials?.Password, ApiKey = apiKey }; return(snapFeed); }
public async Task TestBuildNugetSources_Source_Uri_Is_Null(NuGetProtocolVersion protocolVersion) { await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem); var snapNugetFeed = new SnapNugetFeed { Name = "nuget.org", ProtocolVersion = protocolVersion, Source = null }; var snapChannel = new SnapChannel { Name = "test", PushFeed = snapNugetFeed, UpdateFeed = snapNugetFeed }; var snapApp = new SnapApp { Channels = new List <SnapChannel> { snapChannel } }; var nugetPackageSources = snapApp.BuildNugetSources(nugetTempDirectory); Assert.Empty(nugetPackageSources); }
public void TestBuildNugetSources_SnapApp() { var nugetOrgFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl) }; var snapApp = new SnapApp { Channels = new List <SnapChannel> { new SnapChannel { UpdateFeed = nugetOrgFeed, PushFeed = nugetOrgFeed, Name = "test" }, new SnapChannel { UpdateFeed = nugetOrgFeed, PushFeed = nugetOrgFeed, Name = "staging" } } }; var nugetPackageSources = snapApp.BuildNugetSources(_baseFixture.NugetTempDirectory); Assert.Single(nugetPackageSources.Items); var packageSource = nugetPackageSources.Items.First(); Assert.Equal(packageSource.Name, nugetOrgFeed.Name); }
internal static bool IsPasswordEncryptionSupported([NotNull] this SnapNugetFeed nugetFeed) { if (nugetFeed == null) { throw new ArgumentNullException(nameof(nugetFeed)); } return(IsPasswordEncryptionSupportedImpl()); }
internal static PackageSource BuildPackageSource([NotNull] this SnapNugetFeed snapFeed, [NotNull] ISettings settings) { if (snapFeed == null) { throw new ArgumentNullException(nameof(snapFeed)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var packageSource = new PackageSource(snapFeed.Source.ToString(), snapFeed.Name, true, true, false); var storePasswordInClearText = !settings.IsPasswordEncryptionSupported(); if (snapFeed.Username != null && snapFeed.Password != null) { snapFeed.Password = storePasswordInClearText ? snapFeed.Password : EncryptionUtility.EncryptString(snapFeed.Password); // Comma-delimited list of authentication types the credential is valid for as stored in the config file. // If null or empty, all authentication types are valid. Example: 'basic,negotiate' string validAuthenticationTypesText = null; packageSource.Credentials = new PackageSourceCredential(packageSource.Name, // ReSharper disable once ExpressionIsAlwaysNull snapFeed.Username, snapFeed.Password, storePasswordInClearText, validAuthenticationTypesText); settings.AddOrUpdate(ConfigurationConstants.CredentialsSectionName, packageSource.Credentials.AsCredentialsItem()); } if (snapFeed.ApiKey != null) { if (storePasswordInClearText) { settings.AddOrUpdate(ConfigurationConstants.ApiKeys, new AddItem(packageSource.Source, snapFeed.ApiKey)); } else { SettingsUtility.SetEncryptedValueForAddItem(settings, ConfigurationConstants.ApiKeys, packageSource.Source, snapFeed.ApiKey); } } packageSource.ProtocolVersion = (int)snapFeed.ProtocolVersion; return(packageSource); }
public SnapApp BuildSnapApp(string id = "demoapp", bool isGenesis = false, string rid = null, OSPlatform osPlatform = default) { var pushFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, ApiKey = "myapikey" }; var updateFeedNuget = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, Username = "******", Password = "******" }; var updateFeedHttp = new SnapHttpFeed { Source = new Uri("https://mydynamicupdatefeed.com") }; var testChannel = new SnapChannel { Name = "test", PushFeed = pushFeed, UpdateFeed = updateFeedNuget, Current = true }; var stagingChannel = new SnapChannel { Name = "staging", PushFeed = pushFeed, UpdateFeed = updateFeedHttp }; var productionChannel = new SnapChannel { Name = "production", PushFeed = pushFeed, UpdateFeed = updateFeedNuget }; if (osPlatform == default) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { osPlatform = OSPlatform.Windows; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { osPlatform = OSPlatform.Linux; } } if (osPlatform != OSPlatform.Windows && osPlatform != OSPlatform.Linux) { throw new NotSupportedException($"Unsupported OS platform: {osPlatform}"); } var snapApp = new SnapApp { Id = id, SuperVisorId = Guid.NewGuid().ToString(), Version = new SemanticVersion(1, 0, 0), IsGenesis = isGenesis, IsFull = isGenesis, Channels = new List <SnapChannel> { testChannel, stagingChannel, productionChannel }, Target = new SnapTarget { Os = osPlatform, Framework = "netcoreapp2.1", Rid = rid ?? "win-x64", Shortcuts = new List <SnapShortcutLocation> { SnapShortcutLocation.Desktop, SnapShortcutLocation.Startup, SnapShortcutLocation.StartMenu }, PersistentAssets = new List <string> { "application.json" } } }; return(snapApp); }
public async Task TestBuildSnapApp_Throws_If_Multiple_Nuget_Push_Feed_Names() { await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem); var nugetOrgFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, Username = "******", Password = "******", ApiKey = "myapikey" }; var nugetOrgFeed2 = new SnapNugetFeed { Name = "nuget2.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, Username = "******", Password = "******", ApiKey = "myapikey" }; var testChannel = new SnapChannel { Name = "test", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed, Current = true }; var stagingChannel = new SnapChannel { Name = "staging", PushFeed = nugetOrgFeed2, UpdateFeed = nugetOrgFeed2 }; var productionChannel = new SnapChannel { Name = "production", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed }; var snapAppBefore = new SnapApp { Id = "demoapp", SuperVisorId = Guid.NewGuid().ToString(), Version = new SemanticVersion(1, 0, 0), Channels = new List <SnapChannel> { testChannel, stagingChannel, productionChannel }, Target = new SnapTarget { Os = OSPlatform.Windows, Framework = "netcoreapp2.1", Rid = "win-x64", Shortcuts = new List <SnapShortcutLocation> { SnapShortcutLocation.Desktop, SnapShortcutLocation.Startup }, PersistentAssets = new List <string> { "subdirectory", "myjsonfile.json" } } }; var snapApps = new SnapApps(snapAppBefore); var ex = Assert.Throws <Exception>(() => snapApps.BuildSnapApp(snapAppBefore.Id, snapAppBefore.Target.Rid, snapAppBefore.BuildNugetSources(nugetTempDirectory), _fileSystem)); Assert.Equal($"Multiple nuget push feeds is not supported: nuget.org,nuget2.org. Application id: {snapAppBefore.Id}", ex.Message); }
public async Task TestBuildSnapApp() { await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem); var nugetOrgFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, Username = "******", Password = "******", ApiKey = "myapikey" }; var updateFeedHttp = new SnapHttpFeed { Source = new Uri("https://mydynamicupdatefeed.com") }; var testChannel = new SnapChannel { Name = "test", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed, Current = true }; var stagingChannel = new SnapChannel { Name = "staging", PushFeed = nugetOrgFeed, UpdateFeed = updateFeedHttp }; var productionChannel = new SnapChannel { Name = "production", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed }; var snapAppBefore = new SnapApp { Id = "demoapp", MainExe = "demoapp", InstallDirectoryName = "demoapp", SuperVisorId = Guid.NewGuid().ToString(), Version = new SemanticVersion(1, 0, 0), Channels = new List <SnapChannel> { testChannel, stagingChannel, productionChannel }, Target = new SnapTarget { Os = OSPlatform.Windows, Framework = "netcoreapp2.1", Rid = "win-x64", Shortcuts = new List <SnapShortcutLocation> { SnapShortcutLocation.Desktop, SnapShortcutLocation.Startup }, PersistentAssets = new List <string> { "subdirectory", "myjsonfile.json" } } }; var snapApps = new SnapApps(snapAppBefore); var snapAppAfter = snapApps.BuildSnapApp(snapAppBefore.Id, snapAppBefore.Target.Rid, snapAppBefore.BuildNugetSources(nugetTempDirectory), _fileSystem); snapAppAfter.Version = snapAppBefore.Version.BumpMajor(); // Generic Assert.Equal(snapAppBefore.Id, snapAppAfter.Id); Assert.Equal(snapAppBefore.InstallDirectoryName, snapAppAfter.InstallDirectoryName); Assert.Equal(snapAppBefore.MainExe, snapAppAfter.MainExe); Assert.NotNull(snapAppAfter.MainExe); Assert.Equal(snapAppBefore.SuperVisorId, snapAppAfter.SuperVisorId); Assert.True(snapAppBefore.Version < snapAppAfter.Version); // Target Assert.NotNull(snapAppBefore.Target); Assert.NotNull(snapAppAfter.Target); Assert.Equal(snapAppBefore.Target.Os, snapAppAfter.Target.Os); Assert.Equal(snapAppBefore.Target.Rid, snapAppAfter.Target.Rid); Assert.NotNull(snapAppBefore.Target.Framework); Assert.NotNull(snapAppAfter.Target.Framework); Assert.Equal(snapAppBefore.Target.Framework, snapAppAfter.Target.Framework); Assert.Equal(snapAppBefore.Target.Rid, snapAppAfter.Target.Rid); Assert.Equal(snapAppBefore.Target.Shortcuts, snapAppAfter.Target.Shortcuts); Assert.Equal(snapAppBefore.Target.PersistentAssets, snapAppAfter.Target.PersistentAssets); // Channels Assert.Equal(snapAppBefore.Channels.Count, snapAppAfter.Channels.Count); for (var index = 0; index < snapAppAfter.Channels.Count; index++) { var lhsChannel = snapAppBefore.Channels[index]; var rhsChannel = snapAppAfter.Channels[index]; Assert.Equal(lhsChannel.Name, rhsChannel.Name); Assert.NotNull(lhsChannel.PushFeed); Assert.NotNull(rhsChannel.PushFeed); Assert.NotNull(lhsChannel.UpdateFeed); Assert.NotNull(rhsChannel.UpdateFeed); if (index == 0) { Assert.True(lhsChannel.Current); Assert.True(rhsChannel.Current); } else { Assert.False(lhsChannel.Current); Assert.False(rhsChannel.Current); } var lhsNugetPushFeed = lhsChannel.PushFeed; var rhsNugetPushFeed = rhsChannel.PushFeed; Assert.Equal(lhsNugetPushFeed.Name, rhsNugetPushFeed.Name); Assert.Equal(lhsNugetPushFeed.Source, rhsNugetPushFeed.Source); Assert.Equal(lhsNugetPushFeed.ProtocolVersion, rhsNugetPushFeed.ProtocolVersion); Assert.Equal(lhsNugetPushFeed.ApiKey, rhsNugetPushFeed.ApiKey); Assert.Equal(lhsNugetPushFeed.Username, rhsNugetPushFeed.Username); // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression if (lhsNugetPushFeed.IsPasswordEncryptionSupported()) { Assert.Equal(EncryptionUtility.DecryptString(lhsNugetPushFeed.Password), rhsNugetPushFeed.Password); } else { Assert.Equal(lhsNugetPushFeed.Password, rhsNugetPushFeed.Password); } var lhsUpdateFeed = lhsChannel.UpdateFeed; var rhsUpdateFeed = rhsChannel.UpdateFeed; switch (rhsUpdateFeed) { case SnapNugetFeed rhsNugetUpdateFeed: var lhsNugetUpdateFeed = (SnapNugetFeed)lhsUpdateFeed; Assert.Equal(lhsNugetUpdateFeed.Name, rhsNugetUpdateFeed.Name); Assert.Equal(lhsNugetUpdateFeed.Source, rhsNugetUpdateFeed.Source); Assert.Equal(lhsNugetUpdateFeed.ProtocolVersion, rhsNugetUpdateFeed.ProtocolVersion); Assert.Equal(lhsNugetUpdateFeed.ApiKey, rhsNugetUpdateFeed.ApiKey); Assert.Equal(lhsNugetUpdateFeed.Username, rhsNugetUpdateFeed.Username); Assert.Equal( lhsNugetUpdateFeed.IsPasswordEncryptionSupported() ? EncryptionUtility.DecryptString(lhsNugetUpdateFeed.Password) : lhsNugetUpdateFeed.Password, rhsNugetUpdateFeed.Password); break; case SnapHttpFeed rhsHttpUpdateFeed: var lhsHttpUpdateFeed = (SnapHttpFeed)lhsUpdateFeed; Assert.NotNull(lhsHttpUpdateFeed.Source); Assert.NotNull(rhsHttpUpdateFeed.Source); Assert.Equal(lhsHttpUpdateFeed.Source, rhsHttpUpdateFeed.Source); break; default: throw new NotSupportedException(rhsUpdateFeed.GetType().ToString()); } } }
public async Task TestBuildSnapFeedsFromNugetPackageSources(NuGetProtocolVersion protocolVersion) { await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem); var feedUrl = protocolVersion switch { NuGetProtocolVersion.V2 => NuGetConstants.V2FeedUrl, NuGetProtocolVersion.V3 => NuGetConstants.V3FeedUrl, _ => throw new NotSupportedException(protocolVersion.ToString()) }; var snapNugetFeed = new SnapNugetFeed { Name = "nuget.org", ProtocolVersion = protocolVersion, Source = new Uri(feedUrl), Username = "******", Password = "******", ApiKey = "myapikey" }; var snapChannel = new SnapChannel { Name = "test", PushFeed = snapNugetFeed, UpdateFeed = snapNugetFeed }; var snapApp = new SnapApp { Channels = new List <SnapChannel> { snapChannel } }; var nugetPackageSources = snapApp.BuildNugetSources(nugetTempDirectory); Assert.NotNull(nugetPackageSources.Settings); Assert.Single(nugetPackageSources.Items); var snapFeeds = snapApp.BuildNugetSources(nugetTempDirectory); Assert.NotNull(snapFeeds.Settings); Assert.Single(snapFeeds.Items); var snapNugetFeedAfter = snapFeeds.Items.Single(); Assert.Equal(snapNugetFeed.Name, snapNugetFeedAfter.Name); Assert.Equal((int)snapNugetFeed.ProtocolVersion, snapNugetFeedAfter.ProtocolVersion); Assert.Equal(snapNugetFeed.Source, snapNugetFeedAfter.SourceUri); Assert.Equal(snapNugetFeed.Username, snapNugetFeedAfter.Credentials.Username); var credential = snapNugetFeedAfter.Credentials; if (nugetPackageSources.IsPasswordEncryptionSupported()) { Assert.False(credential.IsPasswordClearText); Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password); Assert.Equal(snapNugetFeed.Password, credential.PasswordText); } else { Assert.True(credential.IsPasswordClearText); Assert.Equal(snapNugetFeed.Password, credential.Password); } Assert.Equal(snapNugetFeed.ApiKey, snapNugetFeedAfter.GetDecryptedValue(nugetPackageSources, ConfigurationConstants.ApiKeys)); }
static SnapApp BuildSnap() { var nugetOrgFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl), ProtocolVersion = NuGetProtocolVersion.V3, Username = "******", Password = "******", ApiKey = "myapikey" }; var updateFeedHttp = new SnapHttpFeed { Source = new Uri("https://mydynamicupdatefeed.com") }; var testChannel = new SnapChannel { Name = "test", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed, Current = true }; var stagingChannel = new SnapChannel { Name = "staging", PushFeed = nugetOrgFeed, UpdateFeed = updateFeedHttp }; var productionChannel = new SnapChannel { Name = "production", PushFeed = nugetOrgFeed, UpdateFeed = nugetOrgFeed }; var snapApp = new SnapApp { Id = "demoapp", Version = new SemanticVersion(1, 0, 0), Channels = new List <SnapChannel> { testChannel, stagingChannel, productionChannel }, Target = new SnapTarget { Os = OSPlatform.Windows, Framework = "netcoreapp2.1", Rid = "win7-x64", Shortcuts = new List <SnapShortcutLocation> { SnapShortcutLocation.Desktop, SnapShortcutLocation.Desktop, SnapShortcutLocation.StartMenu }, PersistentAssets = new List <string> { "application.json" } } }; return(snapApp); }
public void TestBuildNugetSources_SnapApps() { var nugetOrgFeed = new SnapNugetFeed { Name = "nuget.org", Source = new Uri(NuGetConstants.V3FeedUrl) }; var nugetOrgMirrorFeed = new SnapNugetFeed { Name = "nuget.org (mirror)", Source = new Uri(NuGetConstants.V3FeedUrl) }; var snapApp = new SnapApp { Id = "demoapp", Version = new SemanticVersion(1, 0, 0), Channels = new List <SnapChannel> { new SnapChannel { UpdateFeed = nugetOrgFeed, PushFeed = nugetOrgFeed, Name = "test" }, new SnapChannel { UpdateFeed = nugetOrgFeed, PushFeed = nugetOrgFeed, Name = "staging" }, new SnapChannel { UpdateFeed = nugetOrgFeed, PushFeed = nugetOrgMirrorFeed, Name = "production" } }, Target = new SnapTarget { Os = OSPlatform.Windows, Rid = "win-x64", Framework = "netcoreapp2.1" } }; var snapApps = new SnapApps { Schema = 1, Channels = snapApp.Channels.Select(x => new SnapsChannel(x)).ToList(), Apps = new List <SnapsApp> { new SnapsApp(snapApp) }, Generic = new SnapAppsGeneric { Packages = "./packages" } }; var nugetPackageSources = snapApps.BuildNugetSources(new NuGetInMemoryPackageSources(_baseFixture.NugetTempDirectory, new List <PackageSource> { new PackageSource(nugetOrgFeed.Source.ToString(), nugetOrgFeed.Name), new PackageSource(nugetOrgMirrorFeed.Source.ToString(), nugetOrgMirrorFeed.Name) })); Assert.Equal(2, nugetPackageSources.Items.Count); var nugetOrgPackageSource = nugetPackageSources.Items.First(); Assert.Equal(nugetOrgPackageSource.Name, nugetOrgFeed.Name); var nugetOrgMirrorPackageSource = nugetPackageSources.Items.Skip(1).First(); Assert.Equal(nugetOrgMirrorPackageSource.Name, nugetOrgMirrorFeed.Name); }
public void TestBuildNugetSourcesFromSnapApp(NuGetProtocolVersion protocolVersion) { string feedUrl; switch (protocolVersion) { case NuGetProtocolVersion.V2: feedUrl = NuGetConstants.V2FeedUrl; break; case NuGetProtocolVersion.V3: feedUrl = NuGetConstants.V3FeedUrl; break; default: throw new NotSupportedException(protocolVersion.ToString()); } var snapNugetFeed = new SnapNugetFeed { Name = "nuget.org", ProtocolVersion = protocolVersion, Source = new Uri(feedUrl), Username = "******", Password = "******", ApiKey = "myapikey" }; var snapChannel = new SnapChannel { Name = "test", PushFeed = snapNugetFeed, UpdateFeed = snapNugetFeed }; var snapApp = new SnapApp { Channels = new List <SnapChannel> { snapChannel } }; var nuGetPackageSources = snapApp.BuildNugetSources(_baseFixture.NugetTempDirectory); Assert.Single(nuGetPackageSources.Items); var packageSource = nuGetPackageSources.Items.Single(); Assert.True(packageSource.IsEnabled); Assert.True(packageSource.IsOfficial); Assert.False(packageSource.IsPersistable); Assert.False(packageSource.IsMachineWide); Assert.Equal(snapNugetFeed.Name, packageSource.Name); Assert.Equal(snapNugetFeed.Source.ToString(), packageSource.TrySourceAsUri.ToString()); Assert.Equal((int)snapNugetFeed.ProtocolVersion, packageSource.ProtocolVersion); Assert.NotNull(packageSource.Credentials); var credential = packageSource.Credentials; Assert.Equal(snapNugetFeed.Username, credential.Username); if (nuGetPackageSources.IsPasswordEncryptionSupported()) { Assert.False(credential.IsPasswordClearText); Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password); Assert.Equal(snapNugetFeed.Password, credential.PasswordText); } else { Assert.True(credential.IsPasswordClearText); Assert.Equal(snapNugetFeed.Password, credential.Password); } Assert.Equal(snapNugetFeed.Name, credential.Source); Assert.Equal(snapNugetFeed.ApiKey, packageSource.GetDecryptedValue(nuGetPackageSources, ConfigurationConstants.ApiKeys)); }