progmaweb
The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.
Clonegit clone https://git.progmasoft.com/progmaweb
Add the Tazel plugin catalog of ViGet to the API
- Commit
58fa0d19d223aaebf9973ebd446c4bb855351ef9- Author
- Leitwolf11 ·
- Parents
f22418235a0a
Changes
apps/api-tests/AccountServiceTests.cs | 6 + apps/api-tests/AccountStoreSelectionTests.cs | 5 + apps/api-tests/ViGetTests.cs | 269 +++++++++++++++++++++++ apps/api/Accounts/IAccountStore.cs | 7 + apps/api/Accounts/InMemoryAccountStore.cs | 13 ++ apps/api/Accounts/PostgresAccountStore.cs | 6 + apps/api/Program.cs | 16 +- apps/api/ViGet/JavaProperties.cs | 149 +++++++++++++ apps/api/ViGet/PostgresTazelPluginCatalog.cs | 309 +++++++++++++++++++++++++++ apps/api/ViGet/TazelPluginCatalog.cs | 177 +++++++++++++++ apps/api/ViGet/TazelPluginDescriptor.cs | 134 ++++++++++++ apps/api/ViGet/TazelPluginPublishing.cs | 148 +++++++++++++ apps/api/ViGet/ViGetCommand.cs | 85 ++++++++ apps/api/ViGet/ViGetEndpoints.cs | 107 ++++++++++ apps/api/ViGet/ViGetRegistration.cs | 57 +++++ 15 files changed, 1487 insertions(+), 1 deletion(-) diff --git a/apps/api-tests/AccountServiceTests.cs b/apps/api-tests/AccountServiceTests.cs index 28fd44f..0ff7b33 100644 --- a/apps/api-tests/AccountServiceTests.cs +++ b/apps/api-tests/AccountServiceTests.cs @@ -151,6 +151,12 @@ public sealed class AccountServiceTests public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken) => ValueTask.FromResult<AccountRecord?>(passwordAccount.Id == id ? passwordAccount : null); + public ValueTask<AccountRecord?> FindByAccountNameAsync( + string normalizedAccountName, + CancellationToken cancellationToken) => + ValueTask.FromResult<AccountRecord?>( + passwordAccount.NormalizedAccountName == normalizedAccountName ? passwordAccount : null); + public ValueTask<AccountRecord?> FindByGoogleSubjectAsync( string googleSubject, CancellationToken cancellationToken) => diff --git a/apps/api-tests/AccountStoreSelectionTests.cs b/apps/api-tests/AccountStoreSelectionTests.cs index 05ed31b..1087575 100644 --- a/apps/api-tests/AccountStoreSelectionTests.cs +++ b/apps/api-tests/AccountStoreSelectionTests.cs @@ -134,6 +134,11 @@ public sealed class ExpiredSessionCleanupTests public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken) => throw new NotSupportedException(); + public ValueTask<AccountRecord?> FindByAccountNameAsync( + string normalizedAccountName, + CancellationToken cancellationToken) => + throw new NotSupportedException(); + public ValueTask StoreSessionAsync(SessionRecord session, CancellationToken cancellationToken) => throw new NotSupportedException(); diff --git a/apps/api-tests/ViGetTests.cs b/apps/api-tests/ViGetTests.cs new file mode 100644 index 0000000..603ef2f --- /dev/null +++ b/apps/api-tests/ViGetTests.cs @@ -0,0 +1,269 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using System.IO.Compression; +using System.Net; +using System.Security.Cryptography; +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Progmasoft.Progmaweb.Api.Accounts; +using Progmasoft.Progmaweb.Api.ViGet; + +namespace Progmasoft.Progmaweb.Api.Tests; + +/// <summary>The Tazel plugin catalog of ViGet: the descriptor, publishing, the endpoints and the command.</summary> +public sealed class ViGetTests : IDisposable +{ + private readonly string artifactRoot = Path.Combine(Path.GetTempPath(), "viget-" + Guid.NewGuid().ToString("N")); + + /// <inheritdoc/> + public void Dispose() + { + if (Directory.Exists(artifactRoot)) + { + Directory.Delete(artifactRoot, recursive: true); + } + } + + [Fact] + public void PropertiesAreReadAsJavaReadsThem() + { + IReadOnlyDictionary<string, string> properties = JavaProperties.Parse( + "# a comment\n" + + "! another\n" + + "publisher=Progmasoft\n" + + "homepage = https\\://example.com/a\n" + + "description:Builds C \\u0026 C++,\\\n" + + " on every host.\n" + + "key\\ with\\ spaces value\n" + + "empty=\n"); + Assert.Equal("Progmasoft", properties["publisher"]); + Assert.Equal("https://example.com/a", properties["homepage"]); + Assert.Equal("Builds C & C++,on every host.", properties["description"]); + Assert.Equal("value", properties["key with spaces"]); + Assert.Equal(string.Empty, properties["empty"]); + Assert.Throws<FormatException>(() => JavaProperties.Parse("broken=\\u12")); + } + + [Fact] + public void TheDescriptorOfAPluginJarIsReadAndChecked() + { + Assert.Null(TazelPluginDescriptor.TryRead(Jar(), out TazelPluginDescriptor? descriptor)); + Assert.Equal( + new TazelPluginDescriptor( + "Progmasoft", "CMake", "0.1.0", 2, "Builds CMake projects.", ["First Author", "Second Author"], + "MPL-2.0", "https://example.com/cmake", null), + descriptor!, + new DescriptorComparer()); + + Assert.Equal("The JAR has no META-INF/services/com.progmasoft.api.tazel.dsl.plugin.ProjectPlugin.", + TazelPluginDescriptor.TryRead(Jar(service: false), out _)); + Assert.Equal("The descriptor needs an exact semantic version.", + TazelPluginDescriptor.TryRead(Jar(version: "1.0"), out _)); + Assert.Equal("The descriptor needs a name that is one identifier.", + TazelPluginDescriptor.TryRead(Jar(name: "c-make"), out _)); + Assert.Equal("The homepage of the descriptor is not an https address.", + TazelPluginDescriptor.TryRead(Jar(homepage: "http\\://example.com"), out _)); + Assert.Equal("The file is not a JAR.", TazelPluginDescriptor.TryRead("not a zip"u8.ToArray(), out _)); + } + + [Fact] + public async Task OnlyAnAllowedPublisherWithAnAccountPublishes() + { + (TazelPluginPublishing publishing, InMemoryAccountStore accounts, ITazelPluginCatalog catalog) = Publishing(); + CancellationToken cancellationToken = TestContext.Current.CancellationToken; + + PublishResult noAccount = await publishing.PublishAsync(Jar(), cancellationToken); + Assert.Equal("No Progmasoft Account is named 'Progmasoft'.", noAccount.Error); + + await AddAccountAsync(accounts, "Progmasoft"); + await AddAccountAsync(accounts, "Someone"); + PublishResult notAllowed = await publishing.PublishAsync(Jar(publisher: "Someone"), cancellationToken); + Assert.Equal( + "Publishing Tazel plugins is open to Progmasoft for now; 'Someone' cannot publish yet.", + notAllowed.Error); + + byte[] jar = Jar(); + PublishResult published = await publishing.PublishAsync(jar, cancellationToken); + Assert.True(published.Published, published.Error); + string file = Path.Combine(artifactRoot, "dslplugins", "tazel", "Progmasoft", "CMake", "0.1.0", "CMake-0.1.0.jar"); + Assert.Equal(jar, await File.ReadAllBytesAsync(file, cancellationToken)); + TazelPlugin plugin = (await catalog.FindAsync("Progmasoft", "CMake", cancellationToken))!; + Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(jar)), plugin.Versions.Single().Sha256); + Assert.Equal("MPL-2.0", plugin.License); + + PublishResult again = await publishing.PublishAsync(Jar(description: "Another build."), cancellationToken); + Assert.Equal("Progmasoft.CMake 0.1.0 is published already.", again.Error); + Assert.Equal(jar, await File.ReadAllBytesAsync(file, cancellationToken)); + + PublishResult folded = await publishing.PublishAsync(Jar(name: "CMAKE", version: "0.2.0"), cancellationToken); + Assert.Equal( + "Progmasoft has a plugin whose name differs from 'CMAKE' only in letter case.", folded.Error); + Assert.False(File.Exists(Path.Combine( + artifactRoot, "dslplugins", "tazel", "Progmasoft", "CMAKE", "0.2.0", "CMAKE-0.2.0.jar"))); + } + + [Fact] + public async Task ViGetNeedsADescriptionAnAuthorAndALicense() + { + (TazelPluginPublishing publishing, InMemoryAccountStore accounts, _) = Publishing(); + await AddAccountAsync(accounts, "Progmasoft"); + PublishResult result = await publishing.PublishAsync(Jar(license: null), TestContext.Current.CancellationToken); + Assert.Equal( + "ViGet needs a description, at least one author and a license in the descriptor.", result.Error); + } + + [Fact] + public async Task TheIndexIsWhatTazelReads() + { + await using AccountApiFactory factory = new(); + ITazelPluginCatalog catalog = factory.Services.GetRequiredService<ITazelPluginCatalog>(); + CancellationToken cancellationToken = TestContext.Current.CancellationToken; + Assert.Null(TazelPluginDescriptor.TryRead(Jar(), out TazelPluginDescriptor? descriptor)); + await catalog.AddVersionAsync(descriptor!, new string('a', 64), 10, DateTimeOffset.UnixEpoch, cancellationToken); + await catalog.AddVersionAsync( + descriptor! with { Version = "0.2.0" }, new string('b', 64), 10, DateTimeOffset.UnixEpoch.AddDays(1), + cancellationToken); + await catalog.SetYankedAsync("Progmasoft", "CMake", "0.2.0", true, cancellationToken); + using HttpClient client = factory.CreateClient(); + + using JsonDocument index = JsonDocument.Parse(await client.GetStringAsync( + "/api/v1/viget/dslplugins/tazel/Progmasoft/CMake/index.json", cancellationToken)); + Assert.Equal("Progmasoft", index.RootElement.GetProperty("publisher").GetString()); + Assert.Equal("CMake", index.RootElement.GetProperty("name").GetString()); + JsonElement[] versions = [.. index.RootElement.GetProperty("versions").EnumerateArray()]; + Assert.Equal(["0.2.0", "0.1.0"], versions.Select(version => version.GetProperty("version").GetString())); + Assert.True(versions[0].GetProperty("yanked").GetBoolean()); + Assert.Equal(2, versions[1].GetProperty("apiVersion").GetInt32()); + Assert.Equal(new string('a', 64), versions[1].GetProperty("sha256").GetString()); + + using JsonDocument detail = JsonDocument.Parse(await client.GetStringAsync( + "/api/v1/viget/dslplugins/tazel/Progmasoft/CMake", cancellationToken)); + Assert.Equal("0.1.0", detail.RootElement.GetProperty("latestVersion").GetString()); + Assert.Equal( + "/dslplugins/tazel/Progmasoft/CMake/0.1.0/CMake-0.1.0.jar", + detail.RootElement.GetProperty("versions")[1].GetProperty("path").GetString()); + + using JsonDocument list = JsonDocument.Parse( + await client.GetStringAsync("/api/v1/viget/dslplugins/tazel", cancellationToken)); + Assert.Equal("CMake", list.RootElement.GetProperty("plugins")[0].GetProperty("name").GetString()); + + Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync( + "/api/v1/viget/dslplugins/tazel/Progmasoft/Missing/index.json", cancellationToken)).StatusCode); + Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync( + "/api/v1/viget/dslplugins/tazel/Progmasoft/not-a-name/index.json", cancellationToken)).StatusCode); + } + + [Fact] + public async Task TheCommandPublishesAndYanks() + { + (TazelPluginPublishing publishing, InMemoryAccountStore accounts, ITazelPluginCatalog catalog) = Publishing(); + await AddAccountAsync(accounts, "Progmasoft"); + ServiceProvider services = new ServiceCollection() + .AddSingleton(publishing) + .AddSingleton(catalog) + .BuildServiceProvider(); + string jar = Path.Combine(artifactRoot, "upload.jar"); + Directory.CreateDirectory(artifactRoot); + await File.WriteAllBytesAsync(jar, Jar(), TestContext.Current.CancellationToken); + StringWriter output = new(); + StringWriter errors = new(); + + Assert.Equal(0, await ViGetCommand.RunAsync( + services, ["publish-tazel-plugin", jar], output, errors, TestContext.Current.CancellationToken)); + Assert.Contains("Published Progmasoft.CMake 0.1.0", output.ToString(), StringComparison.Ordinal); + Assert.Equal(0, await ViGetCommand.RunAsync( + services, ["yank-tazel-plugin", "Progmasoft", "CMake", "0.1.0"], output, errors, + TestContext.Current.CancellationToken)); + Assert.True((await catalog.FindAsync("Progmasoft", "CMake", TestContext.Current.CancellationToken))! + .Versions.Single().Yanked); + Assert.Equal(1, await ViGetCommand.RunAsync( + services, ["publish-tazel-plugin", jar], output, errors, TestContext.Current.CancellationToken)); + Assert.Equal(2, await ViGetCommand.RunAsync( + services, ["publish"], output, errors, TestContext.Current.CancellationToken)); + } + + private (TazelPluginPublishing, InMemoryAccountStore, ITazelPluginCatalog) Publishing() + { + InMemoryAccountStore accounts = new(); + InMemoryTazelPluginCatalog catalog = new(); + TazelPluginPublishing publishing = new( + catalog, + accounts, + Options.Create(new ViGetOptions { ArtifactRoot = artifactRoot }), + TimeProvider.System); + return (publishing, accounts, catalog); + } + + private static async Task AddAccountAsync(InMemoryAccountStore accounts, string name) => + await accounts.CreateAsync( + new AccountRecord( + Guid.NewGuid(), + name, + AccountNamePolicy.NormalizeForLookup(name), + $"{name}@example.com", + $"{name}@example.com".ToUpperInvariant(), + null, + null, + DateTimeOffset.UnixEpoch), + TestContext.Current.CancellationToken); + + private static byte[] Jar( + string publisher = "Progmasoft", + string name = "CMake", + string version = "0.1.0", + string? description = "Builds CMake projects.", + string? license = "MPL-2.0", + string homepage = "https\\://example.com/cmake", + bool service = true) + { + using MemoryStream bytes = new(); + using (ZipArchive archive = new(bytes, ZipArchiveMode.Create, leaveOpen: true)) + { + List<string> lines = + [ + "publisher=" + publisher, + "name=" + name, + "version=" + version, + "apiVersion=2", + "author.0=First Author", + "author.1=Second Author", + "homepage=" + homepage + ]; + if (description is not null) + { + lines.Add("description=" + description); + } + + if (license is not null) + { + lines.Add("license=" + license); + } + + using (StreamWriter writer = new(archive.CreateEntry(TazelPluginDescriptor.DescriptorEntry).Open())) + { + writer.Write(string.Join('\n', lines) + "\n"); + } + + if (service) + { + using StreamWriter writer = new(archive.CreateEntry(TazelPluginDescriptor.ServiceEntry).Open()); + writer.Write("com.example.CMakePlugin\n"); + } + } + + return bytes.ToArray(); + } + + /// <summary>Compares descriptors by value, with their authors as sequences.</summary> + private sealed class DescriptorComparer : IEqualityComparer<TazelPluginDescriptor> + { + public bool Equals(TazelPluginDescriptor? left, TazelPluginDescriptor? right) => + left is not null && right is not null && + left with { Authors = [] } == right with { Authors = [] } && + left.Authors.SequenceEqual(right.Authors); + + public int GetHashCode(TazelPluginDescriptor descriptor) => descriptor.Name.GetHashCode(StringComparison.Ordinal); + } +} diff --git a/apps/api/Accounts/IAccountStore.cs b/apps/api/Accounts/IAccountStore.cs index efb7c5b..ccb4c02 100644 --- a/apps/api/Accounts/IAccountStore.cs +++ b/apps/api/Accounts/IAccountStore.cs @@ -31,6 +31,13 @@ internal interface IAccountStore /// <param name="cancellationToken">Cancels the operation.</param> /// <returns>The account, or <see langword="null"/> when it does not exist.</returns> ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken); + /// <summary>Finds an account by its normalized name.</summary> + /// <param name="normalizedAccountName"> + /// Upper-case account name, as <see cref="AccountNamePolicy.NormalizeForLookup"/> produces it. + /// </param> + /// <param name="cancellationToken">Cancels the operation.</param> + /// <returns>The account, or <see langword="null"/> when no account has this name.</returns> + ValueTask<AccountRecord?> FindByAccountNameAsync(string normalizedAccountName, CancellationToken cancellationToken); /// <summary>Stores a session, replacing one with the same token digest.</summary> /// <param name="session">The session to store.</param> /// <param name="cancellationToken">Cancels the operation.</param> diff --git a/apps/api/Accounts/InMemoryAccountStore.cs b/apps/api/Accounts/InMemoryAccountStore.cs index 8606443..2f9af9d 100644 --- a/apps/api/Accounts/InMemoryAccountStore.cs +++ b/apps/api/Accounts/InMemoryAccountStore.cs @@ -81,6 +81,19 @@ internal sealed class InMemoryAccountStore : IAccountStore } } + /// <inheritdoc/> + public ValueTask<AccountRecord?> FindByAccountNameAsync( + string normalizedAccountName, + CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + lock (gate) + { + return ValueTask.FromResult( + accountIdsByName.TryGetValue(normalizedAccountName, out Guid id) ? accountsById[id] : null); + } + } + /// <inheritdoc/> public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken) { diff --git a/apps/api/Accounts/PostgresAccountStore.cs b/apps/api/Accounts/PostgresAccountStore.cs index a0e3444..3a3ea82 100644 --- a/apps/api/Accounts/PostgresAccountStore.cs +++ b/apps/api/Accounts/PostgresAccountStore.cs @@ -97,6 +97,12 @@ internal sealed class PostgresAccountStore(NpgsqlDataSource dataSource) : IAccou CancellationToken cancellationToken) => FindAccountAsync("google_subject", googleSubject, cancellationToken); + /// <inheritdoc/> + public ValueTask<AccountRecord?> FindByAccountNameAsync( + string normalizedAccountName, + CancellationToken cancellationToken) => + FindAccountAsync("normalized_account_name", normalizedAccountName, cancellationToken); + /// <inheritdoc/> public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken) => FindAccountAsync("id", id, cancellationToken); diff --git a/apps/api/Program.cs b/apps/api/Program.cs index 476cbdb..873c120 100644 --- a/apps/api/Program.cs +++ b/apps/api/Program.cs @@ -9,8 +9,12 @@ using Microsoft.AspNetCore.HttpOverrides; using Progmasoft.Progmaweb.Api.Accounts; using Progmasoft.OAuth; using Progmasoft.Progmaweb.Api.Security; +using Progmasoft.Progmaweb.Api.ViGet; -WebApplicationBuilder builder = WebApplication.CreateBuilder(args); +// `viget ...` runs a command of ViGet with the configuration of the service instead of the service itself; its +// arguments are not configuration. +bool vigetCommand = ViGetCommand.IsCommand(args); +WebApplicationBuilder builder = WebApplication.CreateBuilder(vigetCommand ? [] : args); builder.Services.Configure<AccountSessionOptions>(builder.Configuration.GetSection(AccountSessionOptions.SectionName)); builder.Services.AddSingleton(TimeProvider.System); @@ -21,6 +25,8 @@ builder.Services.AddSingleton<AccountService>(); builder.Services.AddSingleton<SessionService>(); // Progmasoft OAuth, the OAuth 2.1 and OpenID Connect provider on its own host; see OAuth/OAuthRegistration.cs. builder.Services.AddProgmasoftOAuth(builder.Configuration, accountStore, builder.Environment.IsProduction()); +// ViGet: the Tazel plugin catalog beside the accounts; see ViGet/ViGetEndpoints.cs. +builder.Services.AddViGet(builder.Configuration, accountStore); builder.Services.AddAuthorization(); AuthenticationBuilder authentication = builder.Services .AddAuthentication() @@ -103,6 +109,13 @@ builder.WebHost.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = WebApplication app = builder.Build(); +if (vigetCommand) +{ + Environment.ExitCode = await ViGetCommand.RunAsync( + app.Services, args[1..], Console.Out, Console.Error, CancellationToken.None); + return; +} + if (accountStore is AccountStoreKind.Memory) { AccountStoreRegistration.WarnThatAccountsAreInMemory(app.Logger); @@ -134,6 +147,7 @@ app.MapGet("/api/v1/status", () => Results.Ok(new })); app.MapAccountEndpoints(); app.MapProgmasoftOAuth(); +app.MapViGetEndpoints(); app.Run(); diff --git a/apps/api/ViGet/JavaProperties.cs b/apps/api/ViGet/JavaProperties.cs new file mode 100644 index 0000000..e55991e --- /dev/null +++ b/apps/api/ViGet/JavaProperties.cs @@ -0,0 +1,149 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using System.Globalization; +using System.Text; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>Reads the text of a Java <c>.properties</c> file, as <c>java.util.Properties.load</c> does.</summary> +/// <remarks> +/// A logical line may continue over several lines with a trailing backslash; <c>#</c> and <c>!</c> start a comment +/// line; a key ends at the first unescaped <c>=</c>, <c>:</c> or white space; and <c>\t</c>, <c>\n</c>, <c>\r</c>, +/// <c>\f</c> and <c>\uXXXX</c> are escapes, while a backslash before any other character stands for that character. +/// A later line with the same key replaces an earlier one. +/// </remarks> +internal static class JavaProperties +{ + /// <summary>Reads the properties of a text.</summary> + /// <param name="text">The text of the file, already decoded.</param> + /// <returns>The properties by key.</returns> + /// <exception cref="FormatException">A <c>\u</c> escape is not followed by four hexadecimal digits.</exception> + public static IReadOnlyDictionary<string, string> Parse(string text) + { + Dictionary<string, string> properties = new(StringComparer.Ordinal); + foreach (string line in LogicalLines(text)) + { + int index = 0; + while (index < line.Length && IsBlank(line[index])) + { + index++; + } + + if (index == line.Length || line[index] is '#' or '!') + { + continue; + } + + int keyStart = index; + while (index < line.Length && !IsKeyEnd(line[index])) + { + index += line[index] == '\\' ? 2 : 1; + } + + string key = Unescape(line[keyStart..Math.Min(index, line.Length)]); + while (index < line.Length && IsBlank(line[index])) + { + index++; + } + + if (index < line.Length && line[index] is '=' or ':') + { + index++; + while (index < line.Length && IsBlank(line[index])) + { + index++; + } + } + + properties[key] = Unescape(line[index..]); + } + + return properties; + } + + /// <summary>Joins the physical lines that a trailing backslash continues.</summary> + private static IEnumerable<string> LogicalLines(string text) + { + string[] lines = text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Split('\n'); + StringBuilder current = new(); + bool continuing = false; + foreach (string physical in lines) + { + // A continued line loses the white space it starts with. + string line = continuing ? physical.TrimStart(' ', '\t', '\f') : physical; + int backslashes = 0; + for (int position = line.Length - 1; position >= 0 && line[position] == '\\'; position--) + { + backslashes++; + } + + continuing = backslashes % 2 == 1; + current.Append(continuing ? line[..^1] : line); + if (!continuing) + { + yield return current.ToString(); + current.Clear(); + } + } + + if (current.Length > 0) + { + yield return current.ToString(); + } + } + + private static bool IsBlank(char character) => character is ' ' or '\t' or '\f'; + + private static bool IsKeyEnd(char character) => character is '=' or ':' || IsBlank(character); + + private static string Unescape(string text) + { + StringBuilder result = new(text.Length); + for (int index = 0; index < text.Length; index++) + { + char character = text[index]; + if (character != '\\' || index + 1 == text.Length) + { + result.Append(character); + continue; + } + + char escaped = text[++index]; + switch (escaped) + { + case 't': + result.Append('\t'); + break; + case 'n': + result.Append('\n'); + break; + case 'r': + result.Append('\r'); + break; + case 'f': + result.Append('\f'); + break; + case 'u': + if (text.Length - index - 1 < 4 || + !int.TryParse( + text.AsSpan(index + 1, 4), + NumberStyles.AllowHexSpecifier, + CultureInfo.InvariantCulture, + out int code)) + { + throw new FormatException("A \\u escape needs four hexadecimal digits."); + } + + result.Append((char)code); + index += 4; + break; + default: + result.Append(escaped); + break; + } + } + + return result.ToString(); + } +} diff --git a/apps/api/ViGet/PostgresTazelPluginCatalog.cs b/apps/api/ViGet/PostgresTazelPluginCatalog.cs new file mode 100644 index 0000000..07fa86f --- /dev/null +++ b/apps/api/ViGet/PostgresTazelPluginCatalog.cs @@ -0,0 +1,309 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using Npgsql; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>The tables of <see cref="PostgresTazelPluginCatalog"/> and the steps that bring a database to them.</summary> +/// <remarks> +/// The same numbered steps as the account tables, in a version table of their own, <c>viget_schema_versions</c>. +/// A step is never edited after it has been released. +/// </remarks> +internal static class ViGetSchema +{ + /// <summary>Key of the advisory lock that lets one process at a time change these tables.</summary> + private const long MigrationLockKey = 0x5669476574; + + private static readonly string[] Steps = + [ + // 1: Tazel plugins and their versions. + // + // A plugin is named by its publisher and its name as they are spelled; the unique index on their upper + // case keeps two plugins from differing only in letter case. A version is never removed, only yanked. + """ + CREATE TABLE viget_tazel_plugins ( + publisher text NOT NULL, + name text NOT NULL, + description text, + authors text[] NOT NULL, + license text, + homepage text, + repository text, + updated_at timestamptz NOT NULL, + PRIMARY KEY (publisher, name) + ); + + CREATE UNIQUE INDEX viget_tazel_plugins_folded_key ON viget_tazel_plugins (upper(publisher), upper(name)); + + CREATE TABLE viget_tazel_plugin_versions ( + publisher text NOT NULL, + name text NOT NULL, + version text NOT NULL, + sha256 text NOT NULL, + api_version integer NOT NULL, + size bigint NOT NULL, + published_at timestamptz NOT NULL, + yanked boolean NOT NULL DEFAULT false, + PRIMARY KEY (publisher, name, version), + FOREIGN KEY (publisher, name) REFERENCES viget_tazel_plugins (publisher, name) + ); + """ + ]; + + /// <summary>The version a database has after every step.</summary> + public static int CurrentVersion => Steps.Length; + + /// <summary>Takes the steps a database has not taken yet, in one transaction.</summary> + /// <returns>The number of steps that were taken.</returns> + /// <exception cref="InvalidOperationException">The database has taken steps this program does not know.</exception> + public static async Task<int> ApplyAsync(NpgsqlDataSource dataSource, CancellationToken cancellationToken) + { + await using NpgsqlConnection connection = await dataSource.OpenConnectionAsync(cancellationToken); + await using NpgsqlTransaction transaction = await connection.BeginTransactionAsync(cancellationToken); + await ExecuteAsync(connection, transaction, $"SELECT pg_advisory_xact_lock({MigrationLockKey})", cancellationToken); + await ExecuteAsync( + connection, + transaction, + """ + CREATE TABLE IF NOT EXISTS viget_schema_versions ( + version integer PRIMARY KEY, + applied_at timestamptz NOT NULL DEFAULT now() + ) + """, + cancellationToken); + + int version; + await using (NpgsqlCommand read = new( + "SELECT coalesce(max(version), 0) FROM viget_schema_versions", connection, transaction)) + { + version = (int)(await read.ExecuteScalarAsync(cancellationToken))!; + } + + if (version > Steps.Length) + { + throw new InvalidOperationException( + $"The ViGet tables are at schema version {version}, and this program knows version {Steps.Length}."); + } + + int taken = 0; + for (int step = version; step < Steps.Length; step++) + { + await ExecuteAsync(connection, transaction, Steps[step], cancellationToken); + await using NpgsqlCommand record = new( + "INSERT INTO viget_schema_versions (version) VALUES ($1)", connection, transaction); + record.Parameters.AddWithValue(step + 1); + await record.ExecuteNonQueryAsync(cancellationToken); + taken++; + } + + await transaction.CommitAsync(cancellationToken); + return taken; + } + + private static async Task ExecuteAsync( + NpgsqlConnection connection, + NpgsqlTransaction transaction, + string sql, + CancellationToken cancellationToken) + { + await using NpgsqlCommand command = new(sql, connection, transaction); + await command.ExecuteNonQueryAsync(cancellationToken); + } +} + +/// <summary>The Tazel plugins of ViGet in PostgreSQL, beside the accounts.</summary> +/// <param name="dataSource">Connections to the database of the accounts.</param> +internal sealed class PostgresTazelPluginCatalog(NpgsqlDataSource dataSource) : ITazelPluginCatalog +{ + private const string PluginColumns = "publisher, name, description, authors, license, homepage, repository"; + + /// <inheritdoc/> + public async ValueTask<IReadOnlyList<TazelPlugin>> ListAsync(CancellationToken cancellationToken) + { + await using NpgsqlConnection connection = await dataSource.OpenConnectionAsync(cancellationToken); + List<TazelPlugin> plugins = []; + await using (NpgsqlCommand command = new( + $"SELECT {PluginColumns} FROM viget_tazel_plugins ORDER BY publisher, name", connection)) + await using (NpgsqlDataReader reader = await command.ExecuteReaderAsync(cancellationToken)) + { + while (await reader.ReadAsync(cancellationToken)) + { + plugins.Add(ReadPlugin(reader, [])); + } + } + + List<TazelPlugin> withVersions = []; + foreach (TazelPlugin plugin in plugins) + { + withVersions.Add(plugin with + { + Versions = await VersionsAsync(connection, null, plugin.Publisher, plugin.Name, cancellationToken) + }); + } + + return withVersions; + } + + /// <inheritdoc/> + public async ValueTask<TazelPlugin?> FindAsync(string publisher, string name, CancellationToken cancellationToken) + { + await using NpgsqlConnection connection = await dataSource.OpenConnectionAsync(cancellationToken); + TazelPlugin? plugin; + await using (NpgsqlCommand command = new( + $"SELECT {PluginColumns} FROM viget_tazel_plugins WHERE publisher = $1 AND name = $2", connection)) + { + command.Parameters.AddWithValue(publisher); + command.Parameters.AddWithValue(name); + await using NpgsqlDataReader reader = await command.ExecuteReaderAsync(cancellationToken); + plugin = await reader.ReadAsync(cancellationToken) ? ReadPlugin(reader, []) : null; + } + + return plugin is null + ? null + : plugin with { Versions = await VersionsAsync(connection, null, publisher, name, cancellationToken) }; + } + + /// <inheritdoc/> + public async ValueTask<AddVersionResult> AddVersionAsync( + TazelPluginDescriptor descriptor, + string sha256, + long size, + DateTimeOffset publishedAt, + CancellationToken cancellationToken) + { + await using NpgsqlConnection connection = await dataSource.OpenConnectionAsync(cancellationToken); + await using NpgsqlTransaction transaction = await connection.BeginTransactionAsync(cancellationToken); + await using (NpgsqlCommand taken = new( + """ + SELECT count(*) FROM viget_tazel_plugins + WHERE upper(publisher) = upper($1) AND upper(name) = upper($2) AND (publisher <> $1 OR name <> $2) + """, + connection, + transaction)) + { + taken.Parameters.AddWithValue(descriptor.Publisher); + taken.Parameters.AddWithValue(descriptor.Name); + if ((long)(await taken.ExecuteScalarAsync(cancellationToken))! > 0) + { + return AddVersionResult.NameTaken; + } + } + + await using (NpgsqlCommand plugin = new( + $""" + INSERT INTO viget_tazel_plugins ({PluginColumns}, updated_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + ON CONFLICT (publisher, name) DO UPDATE SET + description = excluded.description, + authors = excluded.authors, + license = excluded.license, + homepage = excluded.homepage, + repository = excluded.repository, + updated_at = excluded.updated_at + """, + connection, + transaction)) + { + plugin.Parameters.AddWithValue(descriptor.Publisher); + plugin.Parameters.AddWithValue(descriptor.Name); + plugin.Parameters.AddWithValue((object?)descriptor.Description ?? DBNull.Value); + plugin.Parameters.AddWithValue(descriptor.Authors.ToArray()); + plugin.Parameters.AddWithValue((object?)descriptor.License ?? DBNull.Value); + plugin.Parameters.AddWithValue((object?)descriptor.Homepage ?? DBNull.Value); + plugin.Parameters.AddWithValue((object?)descriptor.Repository ?? DBNull.Value); + plugin.Parameters.AddWithValue(publishedAt); + await plugin.ExecuteNonQueryAsync(cancellationToken); + } + + await using (NpgsqlCommand version = new( + """ + INSERT INTO viget_tazel_plugin_versions (publisher, name, version, sha256, api_version, size, published_at) + VALUES ($1, $2, $3, $4, $5, $6, $7) + ON CONFLICT (publisher, name, version) DO NOTHING + """, + connection, + transaction)) + { + version.Parameters.AddWithValue(descriptor.Publisher); + version.Parameters.AddWithValue(descriptor.Name); + version.Parameters.AddWithValue(descriptor.Version); + version.Parameters.AddWithValue(sha256); + version.Parameters.AddWithValue(descriptor.ApiVersion); + version.Parameters.AddWithValue(size); + version.Parameters.AddWithValue(publishedAt); + if (await version.ExecuteNonQueryAsync(cancellationToken) == 0) + { + // The plugin row is left as it was: the transaction is rolled back with the refusal. + return AddVersionResult.VersionExists; + } + } + + await transaction.CommitAsync(cancellationToken); + return AddVersionResult.Added; + } + + /// <inheritdoc/> + public async ValueTask<bool> SetYankedAsync( + string publisher, + string name, + string version, + bool yanked, + CancellationToken cancellationToken) + { + await using NpgsqlConnection connection = await dataSource.OpenConnectionAsync(cancellationToken); + await using NpgsqlCommand command = new( + """ + UPDATE viget_tazel_plugin_versions SET yanked = $4 + WHERE publisher = $1 AND name = $2 AND version = $3 + """, + connection); + command.Parameters.AddWithValue(publisher); + command.Parameters.AddWithValue(name); + command.Parameters.AddWithValue(version); + command.Parameters.AddWithValue(yanked); + return await command.ExecuteNonQueryAsync(cancellationToken) == 1; + } + + private static TazelPlugin ReadPlugin(NpgsqlDataReader reader, IReadOnlyList<TazelPluginVersion> versions) => new( + reader.GetString(0), + reader.GetString(1), + reader.IsDBNull(2) ? null : reader.GetString(2), + reader.GetFieldValue<string[]>(3), + reader.IsDBNull(4) ? null : reader.GetString(4), + reader.IsDBNull(5) ? null : reader.GetString(5), + reader.IsDBNull(6) ? null : reader.GetString(6), + versions); + + private static async Task<IReadOnlyList<TazelPluginVersion>> VersionsAsync( + NpgsqlConnection connection, + NpgsqlTransaction? transaction, + string publisher, + string name, + CancellationToken cancellationToken) + { + await using NpgsqlCommand command = new( + """ + SELECT version, sha256, api_version, size, published_at, yanked FROM viget_tazel_plugin_versions + WHERE publisher = $1 AND name = $2 ORDER BY published_at DESC, version DESC + """, + connection, + transaction); + command.Parameters.AddWithValue(publisher); + command.Parameters.AddWithValue(name); + await using NpgsqlDataReader reader = await command.ExecuteReaderAsync(cancellationToken); + List<TazelPluginVersion> versions = []; + while (await reader.ReadAsync(cancellationToken)) + { + versions.Add(new TazelPluginVersion( + reader.GetString(0), + reader.GetString(1), + reader.GetInt32(2), + reader.GetInt64(3), + reader.GetFieldValue<DateTimeOffset>(4), + reader.GetBoolean(5))); + } + + return versions; + } +} diff --git a/apps/api/ViGet/TazelPluginCatalog.cs b/apps/api/ViGet/TazelPluginCatalog.cs new file mode 100644 index 0000000..e6fa2e9 --- /dev/null +++ b/apps/api/ViGet/TazelPluginCatalog.cs @@ -0,0 +1,177 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>One published version of a Tazel plugin.</summary> +/// <param name="Version">The version.</param> +/// <param name="Sha256">The SHA-256 digest of its JAR, in lower-case hex.</param> +/// <param name="ApiVersion">The plugin API of Tazel it was built for.</param> +/// <param name="Size">The size of its JAR in bytes.</param> +/// <param name="PublishedAt">When it was published.</param> +/// <param name="Yanked">Whether it was yanked: Tazel takes it only when a project names it.</param> +internal sealed record TazelPluginVersion( + string Version, + string Sha256, + int ApiVersion, + long Size, + DateTimeOffset PublishedAt, + bool Yanked); + +/// <summary>A Tazel plugin of the catalog, described by the descriptor of its newest publication.</summary> +/// <param name="Publisher">The Progmasoft Account it is published under.</param> +/// <param name="Name">Its name.</param> +/// <param name="Description">What it does.</param> +/// <param name="Authors">Its authors.</param> +/// <param name="License">An SPDX license expression.</param> +/// <param name="Homepage">An https address of the plugin.</param> +/// <param name="Repository">An https address of its sources.</param> +/// <param name="Versions">Its versions, newest publication first.</param> +internal sealed record TazelPlugin( + string Publisher, + string Name, + string? Description, + IReadOnlyList<string> Authors, + string? License, + string? Homepage, + string? Repository, + IReadOnlyList<TazelPluginVersion> Versions) +{ + /// <summary>The newest version that is not yanked, or the newest of all when every one is.</summary> + public TazelPluginVersion Latest => + Versions.Where(version => !version.Yanked).DefaultIfEmpty(Versions[0]).First(); +} + +/// <summary>What adding a version to the catalog did.</summary> +internal enum AddVersionResult +{ + /// <summary>The version was added.</summary> + Added, + + /// <summary>The plugin has this version already; a version is never replaced.</summary> + VersionExists, + + /// <summary>A plugin of the publisher has the same name in other letter case.</summary> + NameTaken, +} + +/// <summary>The Tazel plugins of ViGet: their descriptions and versions. The JARs live in files beside it.</summary> +internal interface ITazelPluginCatalog +{ + /// <summary>Every plugin, ordered by publisher and name.</summary> + ValueTask<IReadOnlyList<TazelPlugin>> ListAsync(CancellationToken cancellationToken); + + /// <summary>A plugin by its exact publisher and name.</summary> + ValueTask<TazelPlugin?> FindAsync(string publisher, string name, CancellationToken cancellationToken); + + /// <summary>Adds a version, and describes the plugin with the descriptor of that version.</summary> + ValueTask<AddVersionResult> AddVersionAsync( + TazelPluginDescriptor descriptor, + string sha256, + long size, + DateTimeOffset publishedAt, + CancellationToken cancellationToken); + + /// <summary>Yanks a version, or takes the yank back.</summary> + /// <returns>Whether the plugin has the version.</returns> + ValueTask<bool> SetYankedAsync( + string publisher, + string name, + string version, + bool yanked, + CancellationToken cancellationToken); +} + +/// <summary>A catalog that lives as long as the process, for development and tests.</summary> +internal sealed class InMemoryTazelPluginCatalog : ITazelPluginCatalog +{ + private readonly Lock gate = new(); + private readonly Dictionary<(string Publisher, string Name), TazelPlugin> plugins = []; + + /// <inheritdoc/> + public ValueTask<IReadOnlyList<TazelPlugin>> ListAsync(CancellationToken cancellationToken) + { + lock (gate) + { + return ValueTask.FromResult<IReadOnlyList<TazelPlugin>>(plugins.Values + .OrderBy(plugin => plugin.Publisher, StringComparer.Ordinal) + .ThenBy(plugin => plugin.Name, StringComparer.Ordinal) + .ToList()); + } + } + + /// <inheritdoc/> + public ValueTask<TazelPlugin?> FindAsync(string publisher, string name, CancellationToken cancellationToken) + { + lock (gate) + { + return ValueTask.FromResult(plugins.GetValueOrDefault((publisher, name))); + } + } + + /// <inheritdoc/> + public ValueTask<AddVersionResult> AddVersionAsync( + TazelPluginDescriptor descriptor, + string sha256, + long size, + DateTimeOffset publishedAt, + CancellationToken cancellationToken) + { + lock (gate) + { + (string, string) key = (descriptor.Publisher, descriptor.Name); + if (!plugins.ContainsKey(key) && plugins.Keys.Any(existing => + string.Equals(existing.Publisher, descriptor.Publisher, StringComparison.OrdinalIgnoreCase) && + string.Equals(existing.Name, descriptor.Name, StringComparison.OrdinalIgnoreCase))) + { + return ValueTask.FromResult(AddVersionResult.NameTaken); + } + + List<TazelPluginVersion> versions = plugins.TryGetValue(key, out TazelPlugin? existingPlugin) + ? [.. existingPlugin.Versions] + : []; + if (versions.Any(version => version.Version == descriptor.Version)) + { + return ValueTask.FromResult(AddVersionResult.VersionExists); + } + + versions.Insert( + 0, new TazelPluginVersion(descriptor.Version, sha256, descriptor.ApiVersion, size, publishedAt, false)); + plugins[key] = new TazelPlugin( + descriptor.Publisher, + descriptor.Name, + descriptor.Description, + descriptor.Authors, + descriptor.License, + descriptor.Homepage, + descriptor.Repository, + versions); + return ValueTask.FromResult(AddVersionResult.Added); + } + } + + /// <inheritdoc/> + public ValueTask<bool> SetYankedAsync( + string publisher, + string name, + string version, + bool yanked, + CancellationToken cancellationToken) + { + lock (gate) + { + if (!plugins.TryGetValue((publisher, name), out TazelPlugin? plugin) || + plugin.Versions.All(existing => existing.Version != version)) + { + return ValueTask.FromResult(false); + } + + plugins[(publisher, name)] = plugin with + { + Versions = [.. plugin.Versions.Select(existing => + existing.Version == version ? existing with { Yanked = yanked } : existing)] + }; + return ValueTask.FromResult(true); + } + } +} diff --git a/apps/api/ViGet/TazelPluginDescriptor.cs b/apps/api/ViGet/TazelPluginDescriptor.cs new file mode 100644 index 0000000..eddd541 --- /dev/null +++ b/apps/api/ViGet/TazelPluginDescriptor.cs @@ -0,0 +1,134 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using System.IO.Compression; +using System.Text; +using System.Text.RegularExpressions; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary> +/// The descriptor of a Tazel plugin, <c>META-INF/tazel-plugin.properties</c> of its JAR, as Tazel reads it, with the +/// fields ViGet shows on the page of the plugin. +/// </summary> +/// <param name="Publisher">The Progmasoft Account the plugin is published under.</param> +/// <param name="Name">The name of the plugin; one identifier.</param> +/// <param name="Version">An exact semantic version.</param> +/// <param name="ApiVersion">The plugin API of Tazel it was built for.</param> +/// <param name="Description">What the plugin does.</param> +/// <param name="Authors">The authors, in the order of the descriptor.</param> +/// <param name="License">An SPDX license expression.</param> +/// <param name="Homepage">An https address of the plugin.</param> +/// <param name="Repository">An https address of its sources.</param> +internal sealed partial record TazelPluginDescriptor( + string Publisher, + string Name, + string Version, + int ApiVersion, + string? Description, + IReadOnlyList<string> Authors, + string? License, + string? Homepage, + string? Repository) +{ + /// <summary>Where the descriptor lies in the JAR.</summary> + public const string DescriptorEntry = "META-INF/tazel-plugin.properties"; + + /// <summary>The service entry that names the class Tazel loads.</summary> + public const string ServiceEntry = "META-INF/services/com.progmasoft.api.tazel.dsl.plugin.ProjectPlugin"; + + /// <summary>Whether a text is one identifier, as Tazel requires of a publisher and a plugin name.</summary> + public static bool IsIdentifier(string value) => IdentifierPattern().IsMatch(value); + + /// <summary>Whether a text is an exact semantic version, as Tazel requires of a plugin version.</summary> + public static bool IsVersion(string value) => VersionPattern().IsMatch(value); + + /// <summary>Reads and checks the descriptor of a plugin JAR.</summary> + /// <param name="jar">The bytes of the JAR.</param> + /// <param name="descriptor">The descriptor, when the JAR is a valid plugin.</param> + /// <returns>Why the JAR is refused, or <see langword="null"/> when it is valid.</returns> + public static string? TryRead(byte[] jar, out TazelPluginDescriptor? descriptor) + { + descriptor = null; + IReadOnlyDictionary<string, string> properties; + try + { + using ZipArchive archive = new(new MemoryStream(jar, writable: false), ZipArchiveMode.Read); + ZipArchiveEntry? entry = archive.GetEntry(DescriptorEntry); + if (entry is null) + { + return $"The JAR has no {DescriptorEntry}."; + } + + ZipArchiveEntry? service = archive.GetEntry(ServiceEntry); + if (service is null || service.Length == 0) + { + return $"The JAR has no {ServiceEntry}."; + } + + using StreamReader reader = new(entry.Open(), Encoding.Latin1); + properties = JavaProperties.Parse(reader.ReadToEnd()); + } + catch (InvalidDataException) + { + return "The file is not a JAR."; + } + catch (FormatException error) + { + return $"The descriptor is not a properties file: {error.Message}"; + } + + string? Text(string key) => + properties.TryGetValue(key, out string? value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; + + string? publisher = Text("publisher"); + string? name = Text("name"); + string? version = Text("version"); + if (publisher is null || !IsIdentifier(publisher)) + { + return "The descriptor needs a publisher that is one identifier."; + } + + if (name is null || !IsIdentifier(name)) + { + return "The descriptor needs a name that is one identifier."; + } + + if (version is null || !IsVersion(version)) + { + return "The descriptor needs an exact semantic version."; + } + + if (!int.TryParse(Text("apiVersion"), out int apiVersion) || apiVersion < 1) + { + return "The descriptor needs an API version."; + } + + List<string> authors = []; + for (int index = 0; Text($"author.{index}") is { } author; index++) + { + authors.Add(author); + } + + string? homepage = Text("homepage"); + string? repository = Text("repository"); + foreach ((string key, string? address) in new[] { ("homepage", homepage), ("repository", repository) }) + { + if (address is not null && + (!Uri.TryCreate(address, UriKind.Absolute, out Uri? uri) || uri.Scheme != Uri.UriSchemeHttps)) + { + return $"The {key} of the descriptor is not an https address."; + } + } + + descriptor = new TazelPluginDescriptor( + publisher, name, version, apiVersion, Text("description"), authors, Text("license"), homepage, repository); + return null; + } + + [GeneratedRegex("^[A-Za-z_][A-Za-z0-9_]*$")] + private static partial Regex IdentifierPattern(); + + [GeneratedRegex(@"^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?$")] + private static partial Regex VersionPattern(); +} diff --git a/apps/api/ViGet/TazelPluginPublishing.cs b/apps/api/ViGet/TazelPluginPublishing.cs new file mode 100644 index 0000000..625e55d --- /dev/null +++ b/apps/api/ViGet/TazelPluginPublishing.cs @@ -0,0 +1,148 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using System.Security.Cryptography; +using Microsoft.Extensions.Options; +using Progmasoft.Progmaweb.Api.Accounts; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>The settings of ViGet, from the configuration section <c>ViGet</c>.</summary> +internal sealed class ViGetOptions +{ + /// <summary>The name of the configuration section.</summary> + public const string SectionName = "ViGet"; + + /// <summary> + /// The directory the files of ViGet are kept in, which Nginx serves: a JAR lies at + /// <c>dslplugins/tazel/<Publisher>/<Name>/<Version>/<Name>-<Version>.jar</c> below it. + /// </summary> + public string ArtifactRoot { get; set; } = "/srv/viget/files"; + + /// <summary> + /// The accounts that may publish Tazel plugins. ViGet opens publishing in stages: first to Progmasoft alone, + /// later to every account. + /// </summary> + public List<string> TazelPublishers { get; set; } = ["Progmasoft"]; +} + +/// <summary>What publishing a plugin did.</summary> +/// <param name="Plugin">The descriptor of the published version, when it was published.</param> +/// <param name="Error">Why it was refused, when it was.</param> +internal sealed record PublishResult(TazelPluginDescriptor? Plugin, string? Error) +{ + /// <summary>Whether the version was published.</summary> + public bool Published => Error is null; +} + +/// <summary>Publishes Tazel plugins: checks a JAR, keeps its file and adds its version to the catalog.</summary> +/// <param name="catalog">The catalog.</param> +/// <param name="accounts">The accounts, which name the publishers.</param> +/// <param name="options">Where the files go and who may publish.</param> +/// <param name="time">The clock.</param> +internal sealed class TazelPluginPublishing( + ITazelPluginCatalog catalog, + IAccountStore accounts, + IOptions<ViGetOptions> options, + TimeProvider time) +{ + /// <summary>The largest JAR ViGet takes.</summary> + public const long MaximumSize = 64L * 1024 * 1024; + + /// <summary>The path of the JAR of a version, relative to <see cref="ViGetOptions.ArtifactRoot"/>.</summary> + public static string RelativePath(string publisher, string name, string version) => + Path.Combine("dslplugins", "tazel", publisher, name, version, $"{name}-{version}.jar"); + + /// <summary>Publishes the plugin in a JAR.</summary> + /// <param name="jar">The bytes of the JAR.</param> + /// <param name="cancellationToken">Cancels the operation.</param> + /// <returns>The published descriptor, or why the JAR was refused.</returns> + public async Task<PublishResult> PublishAsync(byte[] jar, CancellationToken cancellationToken) + { + if (jar.LongLength > MaximumSize) + { + return Refused($"The JAR is larger than {MaximumSize / 1024 / 1024} MiB."); + } + + if (TazelPluginDescriptor.TryRead(jar, out TazelPluginDescriptor? descriptor) is { } invalid) + { + return Refused(invalid); + } + + // ViGet shows these on the page of the plugin; Tazel itself does not need them. + if (descriptor!.Description is null || descriptor.Authors.Count == 0 || descriptor.License is null) + { + return Refused("ViGet needs a description, at least one author and a license in the descriptor."); + } + + if (!options.Value.TazelPublishers.Contains(descriptor.Publisher, StringComparer.Ordinal)) + { + return Refused( + $"Publishing Tazel plugins is open to {string.Join(", ", options.Value.TazelPublishers)} for now; " + + $"'{descriptor.Publisher}' cannot publish yet."); + } + + AccountRecord? account = await accounts.FindByAccountNameAsync( + AccountNamePolicy.NormalizeForLookup(descriptor.Publisher), cancellationToken); + if (account is null || !string.Equals(account.AccountName, descriptor.Publisher, StringComparison.Ordinal)) + { + return Refused($"No Progmasoft Account is named '{descriptor.Publisher}'."); + } + + TazelPlugin? existing = await catalog.FindAsync(descriptor.Publisher, descriptor.Name, cancellationToken); + if (existing?.Versions.Any(version => version.Version == descriptor.Version) == true) + { + return Refused($"{descriptor.Publisher}.{descriptor.Name} {descriptor.Version} is published already."); + } + + string sha256 = Convert.ToHexStringLower(SHA256.HashData(jar)); + string target = Path.Combine( + options.Value.ArtifactRoot, RelativePath(descriptor.Publisher, descriptor.Name, descriptor.Version)); + bool written = await WriteAsync(target, jar, sha256, cancellationToken); + + AddVersionResult result = await catalog.AddVersionAsync( + descriptor, sha256, jar.LongLength, time.GetUtcNow(), cancellationToken); + if (result is not AddVersionResult.Added) + { + if (written) + { + File.Delete(target); + } + + return Refused(result is AddVersionResult.NameTaken + ? $"{descriptor.Publisher} has a plugin whose name differs from '{descriptor.Name}' only in letter case." + : $"{descriptor.Publisher}.{descriptor.Name} {descriptor.Version} is published already."); + } + + return new PublishResult(descriptor, null); + } + + /// <summary>Writes the JAR of a version; a file that is there already must be the same bytes.</summary> + /// <returns>Whether the file was written now.</returns> + private static async Task<bool> WriteAsync( + string target, + byte[] jar, + string sha256, + CancellationToken cancellationToken) + { + if (File.Exists(target)) + { + byte[] present = await File.ReadAllBytesAsync(target, cancellationToken); + if (Convert.ToHexStringLower(SHA256.HashData(present)) != sha256) + { + throw new InvalidOperationException( + $"{target} exists with other bytes, and no version of the catalog names it."); + } + + return false; + } + + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + string partial = target + ".partial"; + await File.WriteAllBytesAsync(partial, jar, cancellationToken); + File.Move(partial, target); + return true; + } + + private static PublishResult Refused(string error) => new(null, error); +} diff --git a/apps/api/ViGet/ViGetCommand.cs b/apps/api/ViGet/ViGetCommand.cs new file mode 100644 index 0000000..ad99aee --- /dev/null +++ b/apps/api/ViGet/ViGetCommand.cs @@ -0,0 +1,85 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using Npgsql; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary> +/// The commands that manage the Tazel plugin catalog on the server, while publishing is not open through the API: +/// <c>viget publish-tazel-plugin <jar></c> and <c>viget yank-tazel-plugin <Publisher> <Name> +/// <Version> [--undo]</c>. They run with the configuration of the service, as its user. +/// </summary> +internal static class ViGetCommand +{ + /// <summary>Whether the arguments of the program ask for a ViGet command rather than the web service.</summary> + public static bool IsCommand(string[] args) => args is ["viget", ..]; + + /// <summary>Runs a ViGet command.</summary> + /// <param name="services">The services of the API, not started.</param> + /// <param name="args">The arguments after <c>viget</c>.</param> + /// <param name="output">Where results go.</param> + /// <param name="errors">Where errors go.</param> + /// <param name="cancellationToken">Cancels the command.</param> + /// <returns>The exit code: 0 on success, 1 when the command was refused, 2 for a wrong command line.</returns> + public static async Task<int> RunAsync( + IServiceProvider services, + string[] args, + TextWriter output, + TextWriter errors, + CancellationToken cancellationToken) + { + // The service applies the schema when it starts; a command may run before it ever did. + if (services.GetService<NpgsqlDataSource>() is { } dataSource) + { + await ViGetSchema.ApplyAsync(dataSource, cancellationToken); + } + + switch (args) + { + case ["publish-tazel-plugin", string path]: + { + if (!File.Exists(path)) + { + await errors.WriteLineAsync($"{path} does not exist."); + return 1; + } + + TazelPluginPublishing publishing = services.GetRequiredService<TazelPluginPublishing>(); + PublishResult result = await publishing.PublishAsync( + await File.ReadAllBytesAsync(path, cancellationToken), cancellationToken); + if (!result.Published) + { + await errors.WriteLineAsync(result.Error); + return 1; + } + + TazelPluginDescriptor plugin = result.Plugin!; + await output.WriteLineAsync( + $"Published {plugin.Publisher}.{plugin.Name} {plugin.Version}: " + + $"https://viget.progmasoft.com/dslplugins/tazel/{plugin.Publisher}/{plugin.Name}"); + return 0; + } + case ["yank-tazel-plugin", string publisher, string name, string version, .. var rest] + when rest is [] or ["--undo"]: + { + bool yanked = rest is []; + ITazelPluginCatalog catalog = services.GetRequiredService<ITazelPluginCatalog>(); + if (!await catalog.SetYankedAsync(publisher, name, version, yanked, cancellationToken)) + { + await errors.WriteLineAsync($"{publisher}.{name} has no version {version}."); + return 1; + } + + await output.WriteLineAsync( + $"{publisher}.{name} {version} is {(yanked ? "yanked" : "no longer yanked")}."); + return 0; + } + default: + await errors.WriteLineAsync( + "usage: viget publish-tazel-plugin <jar>\n" + + " viget yank-tazel-plugin <Publisher> <Name> <Version> [--undo]"); + return 2; + } + } +} diff --git a/apps/api/ViGet/ViGetEndpoints.cs b/apps/api/ViGet/ViGetEndpoints.cs new file mode 100644 index 0000000..4e2337f --- /dev/null +++ b/apps/api/ViGet/ViGetEndpoints.cs @@ -0,0 +1,107 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>The read endpoints of the Tazel plugin catalog.</summary> +/// <remarks> +/// <para> +/// The pages of ViGet read the catalog from these endpoints on the loopback address of the API; Nginx does not pass +/// <c>/api/</c> of the ViGet host on. What the public reaches is the index of a plugin, which Tazel reads at +/// <c>https://viget.progmasoft.com/dslplugins/tazel/<Publisher>/<Name>/index.json</c>: Nginx passes +/// that address to <c>/api/v1/viget/dslplugins/tazel/<Publisher>/<Name>/index.json</c>, and serves the +/// JARs from the files of ViGet itself. +/// </para> +/// </remarks> +internal static class ViGetEndpoints +{ + /// <summary>Maps the endpoints.</summary> + /// <param name="app">The routes of the API.</param> + public static void MapViGetEndpoints(this IEndpointRouteBuilder app) + { + RouteGroupBuilder tazel = app.MapGroup("/api/v1/viget/dslplugins/tazel"); + + tazel.MapGet("/", async (ITazelPluginCatalog catalog, CancellationToken cancellationToken) => + { + IReadOnlyList<TazelPlugin> plugins = await catalog.ListAsync(cancellationToken); + return Results.Ok(new + { + plugins = plugins.Select(plugin => new + { + plugin.Publisher, + plugin.Name, + plugin.Description, + plugin.License, + latestVersion = plugin.Latest.Version, + updatedAt = plugin.Versions[0].PublishedAt + }) + }); + }); + + tazel.MapGet("/{publisher}/{name}", async ( + string publisher, + string name, + ITazelPluginCatalog catalog, + CancellationToken cancellationToken) => + { + TazelPlugin? plugin = await FindAsync(catalog, publisher, name, cancellationToken); + return plugin is null + ? Results.NotFound() + : Results.Ok(new + { + plugin.Publisher, + plugin.Name, + plugin.Description, + plugin.Authors, + plugin.License, + plugin.Homepage, + plugin.Repository, + latestVersion = plugin.Latest.Version, + versions = plugin.Versions.Select(version => new + { + version.Version, + version.Sha256, + version.ApiVersion, + version.Size, + version.PublishedAt, + version.Yanked, + path = $"/dslplugins/tazel/{plugin.Publisher}/{plugin.Name}/{version.Version}/" + + $"{plugin.Name}-{version.Version}.jar" + }) + }); + }); + + // The index Tazel reads to choose a version and check the JAR it downloads. + tazel.MapGet("/{publisher}/{name}/index.json", async ( + string publisher, + string name, + ITazelPluginCatalog catalog, + CancellationToken cancellationToken) => + { + TazelPlugin? plugin = await FindAsync(catalog, publisher, name, cancellationToken); + return plugin is null + ? Results.NotFound() + : Results.Ok(new + { + plugin.Publisher, + plugin.Name, + versions = plugin.Versions.Select(version => new + { + version.Version, + version.Sha256, + version.ApiVersion, + version.Yanked + }) + }); + }); + } + + private static ValueTask<TazelPlugin?> FindAsync( + ITazelPluginCatalog catalog, + string publisher, + string name, + CancellationToken cancellationToken) => + TazelPluginDescriptor.IsIdentifier(publisher) && TazelPluginDescriptor.IsIdentifier(name) + ? catalog.FindAsync(publisher, name, cancellationToken) + : ValueTask.FromResult<TazelPlugin?>(null); +} diff --git a/apps/api/ViGet/ViGetRegistration.cs b/apps/api/ViGet/ViGetRegistration.cs new file mode 100644 index 0000000..bb2bf6f --- /dev/null +++ b/apps/api/ViGet/ViGetRegistration.cs @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using Npgsql; +using Progmasoft.Progmaweb.Api.Accounts; + +namespace Progmasoft.Progmaweb.Api.ViGet; + +/// <summary>Registers ViGet: its catalog beside the accounts, and the publishing of plugins.</summary> +internal static class ViGetRegistration +{ + /// <summary>Adds the services of ViGet.</summary> + /// <param name="services">The services of the API.</param> + /// <param name="configuration">The configuration, whose section <c>ViGet</c> holds <see cref="ViGetOptions"/>.</param> + /// <param name="accountStore">Where the accounts are kept; the catalog is kept in the same place.</param> + public static void AddViGet( + this IServiceCollection services, + IConfiguration configuration, + AccountStoreKind accountStore) + { + services.Configure<ViGetOptions>(configuration.GetSection(ViGetOptions.SectionName)); + if (accountStore is AccountStoreKind.PostgreSql) + { + services.AddSingleton<ITazelPluginCatalog, PostgresTazelPluginCatalog>(); + services.AddHostedService<ViGetSchemaInitializer>(); + } + else + { + services.AddSingleton<ITazelPluginCatalog, InMemoryTazelPluginCatalog>(); + } + + services.AddSingleton<TazelPluginPublishing>(); + } +} + +/// <summary>Brings the ViGet tables to the current schema before the service accepts requests.</summary> +/// <param name="dataSource">Connections to the database of the accounts.</param> +/// <param name="logger">Receives what was done.</param> +internal sealed class ViGetSchemaInitializer( + NpgsqlDataSource dataSource, + ILogger<ViGetSchemaInitializer> logger) : IHostedService +{ + private static readonly Action<ILogger, int, int, Exception?> SchemaReport = LoggerMessage.Define<int, int>( + LogLevel.Information, + new EventId(1, "ViGetSchema"), + "The ViGet tables are at schema version {Version}; {Taken} step(s) were taken at this start."); + + /// <inheritdoc/> + public async Task StartAsync(CancellationToken cancellationToken) + { + int taken = await ViGetSchema.ApplyAsync(dataSource, cancellationToken); + SchemaReport(logger, ViGetSchema.CurrentVersion, taken, null); + } + + /// <inheritdoc/> + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +}