From 2b45e421dda285b73bf7eb3387915030cd751b88 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 9 Oct 2024 23:19:38 +0100 Subject: [PATCH] GetMetaData OK --- src/isn/Program.cs | 4 +-- src/isn/Settings.cs | 30 +++++++++---------- src/isn/commands/PushCommand.cs | 2 +- src/isn/commands/push.cs | 2 +- src/isn/commands/store-api-key.cs | 2 +- src/isnd/Data/Catalog/PackageDetails.cs | 15 ++-------- .../Data/Packages/PackageDependencyGroup.cs | 11 ++----- src/isnd/Services/ApiKeyProvider.cs | 2 +- src/isnd/Services/PackageManager.cs | 4 +-- src/isnd/ViewModels/PackageSearchResult.cs | 18 ++++++----- test/data/test-isn/test-isn.csproj | 3 -- test/isn.tests/PushTest.cs | 7 ++--- test/isnd.tests/UnitTestWebHost.cs | 6 ++-- test/isnd.tests/WebServerFixture.cs | 1 + 14 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/isn/Program.cs b/src/isn/Program.cs index d47e8f0..0eebd42 100644 --- a/src/isn/Program.cs +++ b/src/isn/Program.cs @@ -26,7 +26,6 @@ namespace isn { Settings= Settings.Create(); } - rsa = RSA.Create(Settings.RSAParameters); } static readonly OptionSet storeoptions = new OptionSet { { "s|source=", "use source", val => Settings.CurrentSourceKey ??= val }, @@ -47,7 +46,7 @@ namespace isn }; static readonly OptionSet pushoptions = new OptionSet { - { "k|api-key=", "use api key", val => Settings.CurrentSource.SetApiKey(rsa, val)}, + { "k|api-key=", "use api key", val => Settings.CurrentSource.SetApiKey(val)}, { "s|source=", "use source", val => Settings.CurrentSourceKey = val }, { "h|help", "show this message and exit", h => shouldShowPushHelp = h != null }, }; @@ -63,7 +62,6 @@ namespace isn private static bool shouldShowVersion; private static bool shouldShowSourceHelp; private static bool shouldShowPushHelp; - public static RSA rsa; public static Settings Settings { diff --git a/src/isn/Settings.cs b/src/isn/Settings.cs index c8988c6..f35a73e 100644 --- a/src/isn/Settings.cs +++ b/src/isn/Settings.cs @@ -14,7 +14,7 @@ namespace isn /// Protected API Key /// /// - public string ProtectedApiKey { get; set; } + public string ApiKey { get; set; } /// /// Key alias @@ -26,16 +26,14 @@ namespace isn { } - public string GetClearApiKey(RSA rsa) + public string GetClearApiKey() { - var decrypted = rsa.Decrypt(System.Convert.FromBase64String(ProtectedApiKey), RSAEncryptionPadding.Pkcs1); - return Encoding.Default.GetString(decrypted); + return ApiKey; } - public void SetApiKey(RSA rsa, string key) + public void SetApiKey(string key) { - var ciphered =rsa.Encrypt(Encoding.Default.GetBytes(key), RSAEncryptionPadding.Pkcs1); - ProtectedApiKey = System.Convert.ToBase64String(ciphered); + ApiKey = key; } } @@ -48,14 +46,11 @@ namespace isn public static Settings Create() { - var rsaParams = CreateCipheringParameters(); return new Settings { - RSAParameters = rsaParams, Sources = new Dictionary() }; } - public RSAParameters RSAParameters { get; set; } public Dictionary Sources { get; set; } private string defSourceKey; @@ -77,16 +72,19 @@ namespace isn [JsonIgnore, NotMapped] public string CurrentSourceKey {get; set;} - private static RSAParameters CreateCipheringParameters() - { - var provider = new RSACryptoServiceProvider(2048); - return provider.ExportParameters(true); - } [JsonIgnore, NotMapped] public SourceSettings CurrentSource { - get => this.Sources[CurrentSourceKey]; + get + { + if (CurrentSourceKey==null) return null; + if (!Sources.ContainsKey(CurrentSourceKey)) + { + Sources.Add(CurrentSourceKey, new SourceSettings()); + } + return Sources[CurrentSourceKey]; + } } } } diff --git a/src/isn/commands/PushCommand.cs b/src/isn/commands/PushCommand.cs index 71a41a6..0898350 100644 --- a/src/isn/commands/PushCommand.cs +++ b/src/isn/commands/PushCommand.cs @@ -45,7 +45,7 @@ namespace isn )) try { - return client.UploadFilesToServer(new Uri(pubRes.Id), fi, settings.Sources[source].GetClearApiKey(Program.rsa)); + return client.UploadFilesToServer(new Uri(pubRes.Id), fi, settings.Sources[source].ApiKey); } catch (HttpRequestException httpEx) { diff --git a/src/isn/commands/push.cs b/src/isn/commands/push.cs index df1aed6..d05599e 100644 --- a/src/isn/commands/push.cs +++ b/src/isn/commands/push.cs @@ -18,7 +18,7 @@ namespace isn var source = Settings.CurrentSource; foreach (string pkg in pkgs) { - var report = cmd.Run(pkg, source.Url, source.GetClearApiKey(rsa)); + var report = cmd.Run(pkg, source.Url, source.ApiKey); pushReports.Add(report); } return pushReports; diff --git a/src/isn/commands/store-api-key.cs b/src/isn/commands/store-api-key.cs index 778ce57..b1f9c8d 100644 --- a/src/isn/commands/store-api-key.cs +++ b/src/isn/commands/store-api-key.cs @@ -25,7 +25,7 @@ namespace isn } else { - Settings.Sources[Settings.CurrentSourceKey].SetApiKey(Program.rsa, args[0]); + Settings.CurrentSource.SetApiKey(args[0]); SaveConfig(); } } diff --git a/src/isnd/Data/Catalog/PackageDetails.cs b/src/isnd/Data/Catalog/PackageDetails.cs index db78671..1a2fc25 100644 --- a/src/isnd/Data/Catalog/PackageDetails.cs +++ b/src/isnd/Data/Catalog/PackageDetails.cs @@ -35,8 +35,7 @@ namespace isnd.Data.Catalog IsListed = !pkg.IsDeleted && pkg.Package.Public; if (pkg.DependencyGroups!=null) { - DependencySets = pkg.DependencyGroups.ToDepSetInfo(); - + DependencySets = pkg.DependencyGroups; } PackageDetailsUrl = new Uri(this.id); @@ -210,7 +209,8 @@ namespace isnd.Data.Catalog public LicenseMetadata LicenseMetadata { get; set; } - public IEnumerable DependencySets {get; set;} + [JsonProperty("dependencyGroups")] + public IEnumerable DependencySets {get; set;} IEnumerable IPackageSearchMetadata.DependencySets => throw new NotImplementedException(); @@ -225,13 +225,4 @@ namespace isnd.Data.Catalog } } - public class PackageDependencyGroupInfo - { - private PackageDependencyGroup group; - - public PackageDependencyGroupInfo(PackageDependencyGroup group) - { - this.group = group; - } - } } \ No newline at end of file diff --git a/src/isnd/Data/Packages/PackageDependencyGroup.cs b/src/isnd/Data/Packages/PackageDependencyGroup.cs index ba412bc..a1f8555 100644 --- a/src/isnd/Data/Packages/PackageDependencyGroup.cs +++ b/src/isnd/Data/Packages/PackageDependencyGroup.cs @@ -14,13 +14,6 @@ using NuGet.Versioning; namespace isnd.Data { - static class Helpers - { - public static PackageDependencyGroupInfo[] ToDepSetInfo(this IEnumerable groups) - { - return groups.Select(group => new PackageDependencyGroupInfo(group)).ToArray(); - } - } public class PackageDependencyGroup { [JsonIgnore] @@ -28,7 +21,7 @@ namespace isnd.Data public string Id { get ; set;} [Required] - [JsonProperty("id")] + [JsonIgnore] public string PackageId { get ; set;} [Required] @@ -39,7 +32,7 @@ namespace isnd.Data [Required] public string TargetFramework { get; set; } - [JsonIgnore] + [JsonProperty("dependencies")] [ForeignKey("DependencyGroupId")] public virtual List Dependencies { get; set; } diff --git a/src/isnd/Services/ApiKeyProvider.cs b/src/isnd/Services/ApiKeyProvider.cs index 4f226db..15ed673 100644 --- a/src/isnd/Services/ApiKeyProvider.cs +++ b/src/isnd/Services/ApiKeyProvider.cs @@ -31,7 +31,7 @@ public class ApiKeyProvider : IApiKeyProvider { var newKey = new ApiKey{ UserId = model.UserId, - CreationDate = DateTime.Now, + CreationDate = DateTime.Now.ToUniversalTime(), Name = model.Name, ValidityPeriodInDays = model.ValidityPeriodInDays }; diff --git a/src/isnd/Services/PackageManager.cs b/src/isnd/Services/PackageManager.cs index 70799f6..adfbbf2 100644 --- a/src/isnd/Services/PackageManager.cs +++ b/src/isnd/Services/PackageManager.cs @@ -13,14 +13,11 @@ using isnd.Data; using isnd.Data.Catalog; using isnd.Data.Packages; using isnd.Entities; -using isnd.Helpers; using isnd.Interfaces; using isnd.ViewModels; -using NuGet.Packaging.Core; using NuGet.Versioning; using System.Xml; using System.Xml.Linq; -using System.Threading; using NuGet.Protocol; namespace isnd.Services @@ -241,6 +238,7 @@ namespace isnd.Services && v.LatestCommit != null && (pkgType == null || pkgType == v.Type) ).SingleOrDefaultAsync(); + if (version==null) return null; foreach (var g in version.DependencyGroups) { g.Dependencies = dbContext.Dependencies.Where(d => d.DependencyGroupId == g.Id).ToList(); diff --git a/src/isnd/ViewModels/PackageSearchResult.cs b/src/isnd/ViewModels/PackageSearchResult.cs index 7bed892..a930f63 100644 --- a/src/isnd/ViewModels/PackageSearchResult.cs +++ b/src/isnd/ViewModels/PackageSearchResult.cs @@ -34,16 +34,20 @@ namespace isnd.ViewModels private static PackageHit NewPackageHit(string apiBase, Package package) { string regId = $"{apiBase}{ApiConfig.Registration}/{package.Id}/index.json"; - return new PackageHit(regId, package.Id) + + var pkgHit = new PackageHit(regId, package.Id) { version = package.GetLatestVersion(), description = package.Description, - versions = package.Versions.Select(v => new SearchVersionInfo(apiBase, v)).ToArray(), - packageTypes = package.Versions.Select(v=>new PackageType(v.Type, new System.Version(v.Major,v.Minor,v.Patch, v.Revision))) - .ToArray(), - - - }; + }; + if (package.Versions!=null) + { + pkgHit.versions = package.Versions + .Select(v => new SearchVersionInfo(apiBase, v)).ToArray(); + pkgHit.packageTypes = package.Versions + .Select(v=>new PackageType(v.Type ?? "Legacy", new System.Version(v.Major,v.Minor,v.Patch, v.Revision)))?.ToArray(); + } + return pkgHit; } public PackageHit[] data { get; protected set; } diff --git a/test/data/test-isn/test-isn.csproj b/test/data/test-isn/test-isn.csproj index cd4513a..0247e7a 100644 --- a/test/data/test-isn/test-isn.csproj +++ b/test/data/test-isn/test-isn.csproj @@ -12,7 +12,4 @@ - - - diff --git a/test/isn.tests/PushTest.cs b/test/isn.tests/PushTest.cs index b8e310c..c8a9884 100644 --- a/test/isn.tests/PushTest.cs +++ b/test/isn.tests/PushTest.cs @@ -30,7 +30,7 @@ namespace isn.tests public void TestPush() { Program.LoadConfig(); - var report = Program.PushPkg(new string[] { "./src/isn.abst/bin/Debug/isn.abst.1.0.1.nupkg" }); + var report = Program.PushPkg(new string[] { "./src/isn.abst/bin/Release/isn.abst.1.0.24.nupkg" }); } [Fact] @@ -50,9 +50,8 @@ namespace isn.tests var setting = Settings.Create(); setting.Sources[source] = new SourceSettings{ Url=source }; string testingKey = "CfDJ8LF3SbIJ4FJAgs7uIQKhdCAYCNVXRwU6TEoaXOo1_ZpG2u8TCGFP2z13hw9xR0LC0gdbr1QGwNndiXUl4DI74nxyBi-T1oC33PWtE-5vgiJWeCH223PYtoSEdzDiWovwJZWJbQON0WqoG8vSfbrBXTmicD6oxF4ghwXXexY0RiRR"; - var rsa = RSA.Create(setting.RSAParameters); - setting.Sources[source].SetApiKey(rsa,testingKey); - Assert.Equal(testingKey, setting.Sources[source].GetClearApiKey(rsa)); + + Assert.Equal(testingKey, setting.Sources[source].ApiKey); } } diff --git a/test/isnd.tests/UnitTestWebHost.cs b/test/isnd.tests/UnitTestWebHost.cs index 90c7875..c1a619d 100644 --- a/test/isnd.tests/UnitTestWebHost.cs +++ b/test/isnd.tests/UnitTestWebHost.cs @@ -67,7 +67,7 @@ namespace isnd.host.tests string pkgSourceUrl = isnSettings.ExternalUrl + apiindex + ".json"; ProcessStartInfo psi = new ProcessStartInfo("nuget"); psi.ArgumentList.Add("install"); - psi.ArgumentList.Add("isnd"); + psi.ArgumentList.Add("isn.abst"); psi.ArgumentList.Add("-PreRelease"); psi.ArgumentList.Add("-Source"); psi.ArgumentList.Add(pkgSourceUrl); @@ -128,7 +128,7 @@ namespace isnd.host.tests PackageMetadataResource resource = await repository.GetResourceAsync(); IEnumerable packages = await resource.GetMetadataAsync( - "isnd", + "isn.abst", includePrerelease: true, includeUnlisted: true, cache, @@ -178,7 +178,7 @@ namespace isnd.host.tests PackageUpdateResource pushRes = await repository.GetResourceAsync(); SymbolPackageUpdateResourceV3 symbolPackageResource = await repository.GetResourceAsync(); - await pushRes.Push(new List{ "../../../../../src/isn.abst/bin/Release/isn.abst.1.0.1.nupkg" }, null, + await pushRes.Push(new List{ "../../../../../src/isn.abst/bin/Release/isn.abst.1.0.24.nupkg" }, null, 5000, false, GetApiKey, GetSymbolsApiKey, false, false, symbolPackageResource, logger); } diff --git a/test/isnd.tests/WebServerFixture.cs b/test/isnd.tests/WebServerFixture.cs index 69f7cc7..e241b40 100644 --- a/test/isnd.tests/WebServerFixture.cs +++ b/test/isnd.tests/WebServerFixture.cs @@ -101,6 +101,7 @@ namespace isnd.tests Name = "Testing Key", UserId = TestingUser.Id, ValidityPeriodInDays = 1 + }; testKey = keyProvider.CreateApiKeyAsync(apiKeyQuery).Result;