mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 22:11:10 +00:00
Adds bundled plugin trust and cross-platform native deployment
- embeds the append-only historical and deny trust lists in the API and marks matching bundled plugins as Official without blocking loading - builds and records the current plugin manifest only for official release builds - deploys the PDF native libraries for the supported Linux, macOS and Windows runtimes from the plugin dependency manifest - records release hashes by appending only missing entries to master's manifest and rejects any diff that removes a line - documents the release flow, the supported runtimes and the plugin installer limits
This commit is contained in:
1 parent
3fb8912c7f
commit
b30cbd6b9e
114 files changed
+2708
-243
No files matched your search
+3
-3
@@ -19,7 +19,7 @@ public class PluginLoadContextTests
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
PluginLoadContext sut = new(temporaryPluginDirectory.Path, "test-context");
|
||||
PluginLoadContext sut = new(Path.Combine(temporaryPluginDirectory.Path, "Plugin.dll"), "test-context");
|
||||
|
||||
// Act
|
||||
Assembly result = sut.LoadFromAssemblyName(typeof(object).Assembly.GetName());
|
||||
@@ -35,7 +35,7 @@ public class PluginLoadContextTests
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string destinationPath = temporaryPluginDirectory.CopyDll(Path.Combine(AppContext.BaseDirectory, "Nemera.TestPlugin.dll"), "PluginLocalDependency.dll");
|
||||
PluginLoadContext sut = new(temporaryPluginDirectory.Path, "test-context");
|
||||
PluginLoadContext sut = new(destinationPath, "test-context");
|
||||
MethodInfo loadMethod = typeof(PluginLoadContext).GetMethod("Load", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
|
||||
// Act
|
||||
@@ -52,7 +52,7 @@ public class PluginLoadContextTests
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
PluginLoadContext sut = new(temporaryPluginDirectory.Path, "test-context");
|
||||
PluginLoadContext sut = new(Path.Combine(temporaryPluginDirectory.Path, "Missing.dll"), "test-context");
|
||||
|
||||
// Act
|
||||
Action act = () => sut.LoadFromAssemblyName(new AssemblyName($"Definitely.Not.Resolvable.{Guid.NewGuid()}"));
|
||||
|
||||
+227
-13
@@ -1,12 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
|
||||
using Nemera.Infrastructure.Common.Models.DTO.Plugins;
|
||||
using Nemera.Infrastructure.Core.Plugins;
|
||||
using Nemera.Plugins.Contracts.Core.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nemera.Plugins.Contracts.Core.Reading;
|
||||
using Nemera.Plugins.Pdf.Fixtures.Core.Pdf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Infrastructure.IntegrationTests.Core.Plugins;
|
||||
@@ -17,6 +23,46 @@ namespace Nemera.Infrastructure.IntegrationTests.Core.Plugins;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PluginLoaderTests
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<string, IReadOnlySet<string>> s_emptyBundledPluginChecksums = new Dictionary<string, IReadOnlySet<string>>();
|
||||
|
||||
/// <summary>
|
||||
/// The unique identifier of the bundled PDF plugin used by the deployed plugin loading test.
|
||||
/// </summary>
|
||||
private static readonly Guid s_pdfPluginId = new("c2f9b8f0-5a4b-4e3d-8b2f-7e4a9c1d3e5f");
|
||||
|
||||
[Fact]
|
||||
public async Task LoadPlugins_WhenLoadingTheBundledPdfPlugin_ShouldRenderAPdfPage()
|
||||
{
|
||||
// Arrange
|
||||
string repositoryRoot = FindRepositoryRoot();
|
||||
DirectoryInfo testOutputDirectory = new(AppContext.BaseDirectory);
|
||||
string configuration = testOutputDirectory.Parent?.Name
|
||||
?? throw new InvalidOperationException("The test configuration could not be determined from the test output directory.");
|
||||
string targetFramework = testOutputDirectory.Name;
|
||||
string pluginsDirectory = Path.Combine(repositoryRoot, "bin", "api", configuration, targetFramework, "plugins");
|
||||
Assert.True(Directory.Exists(pluginsDirectory), $"The deployed plugins directory '{pluginsDirectory}' does not exist. Build the solution before running this test so the bundled plugins are deployed.");
|
||||
using TemporaryPluginDirectory temporaryDirectory = new();
|
||||
string pdfPath = Path.Combine(temporaryDirectory.Path, "minimal.pdf");
|
||||
TestPdfFileFactory.CreateMinimalPdf(pdfPath);
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(pluginsDirectory, services, s_emptyBundledPluginChecksums);
|
||||
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
IBookReader pdfReader = serviceProvider.GetRequiredKeyedService<IBookReader>(s_pdfPluginId);
|
||||
string workingDirectory = Path.Combine(temporaryDirectory.Path, "work");
|
||||
ReadingDocumentDto document = await pdfReader.OpenAsync(pdfPath, workingDirectory, shouldRenderPdfAsImages: false, CancellationToken.None);
|
||||
byte[] pageImage = await pdfReader.GetResourceAsync(pdfPath, workingDirectory, "page:1", CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Empty(result.Warnings);
|
||||
Assert.Contains(result.Plugins, plugin => plugin.Id == s_pdfPluginId);
|
||||
Assert.NotEmpty(result.LoadedPlugins);
|
||||
Assert.Single(document.Spine);
|
||||
Assert.NotEmpty(pageImage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryDoesNotExist_ShouldReturnNoPluginsAndNoErrors()
|
||||
{
|
||||
@@ -25,11 +71,12 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(nonExistentDirectory, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(nonExistentDirectory, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Empty(result.LoadedPlugins);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -41,7 +88,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -60,7 +107,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -77,7 +124,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -93,14 +140,16 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
IPlugin loadedPlugin = Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
// A plugin that is not Official is expected to come from an uploaded assembly without a dependency manifest, so the loader does not warn.
|
||||
Assert.Empty(result.Warnings);
|
||||
Assert.Equal(Guid.Parse("7B5A3E5D-9B7F-4A1E-8D9C-2F3A4B5C6D7E"), loadedPlugin.Id);
|
||||
Assert.Equal("Test Plugin", loadedPlugin.Name);
|
||||
Assert.Contains(services, descriptor => descriptor.ImplementationInstance as string == "plugin-services-registered");
|
||||
Assert.Contains(services, descriptor => (descriptor.ImplementationInstance as string) == "plugin-services-registered");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -112,7 +161,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
@@ -121,6 +170,130 @@ public class PluginLoaderTests
|
||||
Assert.Single(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenPluginDependencyManifestIsInvalid_ShouldLoadPluginAndRecordWarning()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
temporaryPluginDirectory.CreateFile("Nemera.TestPlugin.deps.json", "{ this is not a valid dependency manifest");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
string warning = Assert.Single(result.Warnings);
|
||||
Assert.Contains("Nemera.TestPlugin.dll", warning);
|
||||
Assert.Contains("dependency manifest", warning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenOfficialPluginHasNoDependencyManifest_ShouldLoadPluginAndRecordWarning()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string assemblyPath = GetTestPluginAssemblyPath();
|
||||
temporaryPluginDirectory.CopyDll(assemblyPath);
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> bundledPluginChecksums =
|
||||
CreateBundledPluginChecksums("Nemera.TestPlugin.dll", ComputeChecksum(assemblyPath));
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, bundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
string warning = Assert.Single(result.Warnings);
|
||||
Assert.Contains("Nemera.TestPlugin.dll", warning);
|
||||
Assert.Contains("no dependency manifest", warning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenBundledPluginChecksumMatches_ShouldLoadPluginAndMarkItOfficial()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string assemblyPath = GetTestPluginAssemblyPath();
|
||||
temporaryPluginDirectory.CopyDll(assemblyPath);
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> bundledPluginChecksums =
|
||||
CreateBundledPluginChecksums("Nemera.TestPlugin.dll", ComputeChecksum(assemblyPath));
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, bundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
IPlugin loadedPlugin = Assert.Single(result.Plugins);
|
||||
Assert.Equal(Guid.Parse("7B5A3E5D-9B7F-4A1E-8D9C-2F3A4B5C6D7E"), loadedPlugin.Id);
|
||||
Assert.Empty(result.Errors);
|
||||
LoadedPluginMetadataDto metadata = Assert.Single(result.LoadedPlugins);
|
||||
Assert.Equal("Nemera.TestPlugin.dll", metadata.AssemblyFileName);
|
||||
Assert.True(metadata.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenBundledPluginChecksumDoesNotMatch_ShouldLoadPluginWithoutMarkingItOfficial()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> bundledPluginChecksums =
|
||||
CreateBundledPluginChecksums("Nemera.TestPlugin.dll", new string('0', 64));
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, bundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
LoadedPluginMetadataDto metadata = Assert.Single(result.LoadedPlugins);
|
||||
Assert.False(metadata.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenPluginIsNotInTheBundledManifest_ShouldLoadPluginWithoutMarkingItOfficial()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
LoadedPluginMetadataDto metadata = Assert.Single(result.LoadedPlugins);
|
||||
Assert.False(metadata.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenBundledManifestListsMultipleChecksums_ShouldMarkTheMatchingOneOfficial()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string assemblyPath = GetTestPluginAssemblyPath();
|
||||
temporaryPluginDirectory.CopyDll(assemblyPath);
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> bundledPluginChecksums =
|
||||
CreateBundledPluginChecksums("Nemera.TestPlugin.dll", new string('0', 64), ComputeChecksum(assemblyPath));
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, bundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
LoadedPluginMetadataDto metadata = Assert.Single(result.LoadedPlugins);
|
||||
Assert.True(metadata.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsManagedDependencyAssemblyAlongsideAPlugin_ShouldSkipTheDependencyAndLoadOnlyThePlugin()
|
||||
{
|
||||
@@ -134,7 +307,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
IPlugin loadedPlugin = Assert.Single(result.Plugins);
|
||||
@@ -154,7 +327,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -173,7 +346,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -192,7 +365,7 @@ public class PluginLoaderTests
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
@@ -224,6 +397,47 @@ public class PluginLoaderTests
|
||||
return Path.Combine(AppContext.BaseDirectory, "Microsoft.AspNetCore.SignalR.Core.dll");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a bundled plugin trust list containing one assembly with the provided checksums.
|
||||
/// </summary>
|
||||
/// <param name="assemblyFileName">The file name of the bundled plugin assembly.</param>
|
||||
/// <param name="checksums">The checksums the assembly is allowed to have.</param>
|
||||
/// <returns>The bundled plugin trust list.</returns>
|
||||
private static IReadOnlyDictionary<string, IReadOnlySet<string>> CreateBundledPluginChecksums(string assemblyFileName, params string[] checksums)
|
||||
{
|
||||
return new Dictionary<string, IReadOnlySet<string>>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
[assemblyFileName] = new HashSet<string>(checksums, StringComparer.OrdinalIgnoreCase)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the SHA-256 checksum of the file at <paramref name="path"/>, in the hexadecimal form the bundled plugin trust manifest uses.
|
||||
/// </summary>
|
||||
/// <param name="path">The path of the file to hash.</param>
|
||||
/// <returns>The uppercase hexadecimal SHA-256 checksum.</returns>
|
||||
private static string ComputeChecksum(string path)
|
||||
{
|
||||
using (FileStream fileStream = new(path, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
return Convert.ToHexString(SHA256.HashData(fileStream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locates the repository root from the test output directory.
|
||||
/// </summary>
|
||||
/// <returns>The absolute path of the repository root.</returns>
|
||||
private static string FindRepositoryRoot()
|
||||
{
|
||||
DirectoryInfo? directory = new(AppContext.BaseDirectory);
|
||||
while (directory is not null)
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory.FullName, "src", "Nemera.slnx")))
|
||||
return directory.FullName;
|
||||
directory = directory.Parent;
|
||||
}
|
||||
throw new InvalidOperationException("The repository root could not be located from the test output directory.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test helper managing a temporary plugins directory, deleted when the test finishes.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user