From 6e5dbb6a91356c87d26f07f1aa6261e9f331e763 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 6 Nov 2024 19:27:53 +0200 Subject: [PATCH 1/4] Adds multiplatform testing on HitHub Actions --- .github/workflows/run-tests.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 58995ba6..443d7c11 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,11 +8,11 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] environment: test - env: # Set Environment options that are meant to be left empty so the user fills them - JwtSettings__SecretKey: ${{ secrets.JWT_SETTINGS_SECRET_KEY }} - EncryptionSettings__SecretKey: ${{ secrets.ENCRYPTION_SETTINGS_SECRET_KEY }} steps: - uses: actions/checkout@v2 - name: Set up .NET From 01decb27d52a958bc9a911d063ca49e6c49b6254 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 6 Nov 2024 20:26:26 +0200 Subject: [PATCH 2/4] Updates code to account for MacOS as a UNIX platform --- .../Services/FileSystemPermissionsService.cs | 6 +++--- .../Strategies/Platform/PlatformContextManager.cs | 2 +- src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj | 4 +--- .../ViewComponents/FileSystemBrowserViewComponent.cs | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs index 9b55641d..73ba1d4b 100644 --- a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs +++ b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs @@ -40,7 +40,7 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T { PlatformType platformType = _platformContextManager.GetCurrentContext().Platform; if (platformType == PlatformType.Unix) - return CanAccessPathLinux(path.Path, accessMode); + return CanAccessPathUnix(path.Path, accessMode); else if (platformType == PlatformType.Windows) #pragma warning disable CA1416 // Validate platform compatibility return CanAccessPathWindows(path.Path, accessMode, isFile); @@ -50,13 +50,13 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T } /// - /// Checks if a the current user has access permissions on Linux for , with . + /// Checks if a the current user has access permissions on Unix for , with . /// /// The path for which to check the access. /// The access mode in which to check that path access. /// if the current user has rights for the specified path and acccess mode, otherwise. /// Thrown when trying to check access for a mode that is not supported - private static bool CanAccessPathLinux(string path, FileAccessMode accessMode) + private static bool CanAccessPathUnix(string path, FileAccessMode accessMode) { AccessModes modes; UnixFileSystemInfo fileInfo = new UnixFileInfo(path); diff --git a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Strategies/Platform/PlatformContextManager.cs b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Strategies/Platform/PlatformContextManager.cs index 056780ff..d1e4a766 100644 --- a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Strategies/Platform/PlatformContextManager.cs +++ b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Strategies/Platform/PlatformContextManager.cs @@ -34,7 +34,7 @@ public class PlatformContextManager : IPlatformContextManager { // set a default context if none is set if (_currentPlatformContext is null) - if (_operatingSystemInfo.IsOSPlatform(OSPlatform.Linux)) + if (_operatingSystemInfo.IsOSPlatform(OSPlatform.Linux) || _operatingSystemInfo.IsOSPlatform(OSPlatform.OSX)) SetCurrentPlatform(PlatformType.Unix); else SetCurrentPlatform(PlatformType.Windows); diff --git a/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj b/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj index 8e9f641f..ae9f905f 100644 --- a/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj +++ b/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj @@ -6,7 +6,7 @@ $(SolutionDir)..\bin\web\ false true - 6413036f-92d7-45d8-a8B1-205200089047 + 10a94da9-18ed-4c70-a9da-b461e08958c4 Linux lumina-web lumina-web @@ -35,7 +35,5 @@ $(NoWarn);TS6385 - 10a94da9-18ed-4c70-a9da-b461e08958c4 - Linux diff --git a/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs b/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs index c7b98f62..f74eec74 100644 --- a/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs +++ b/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs @@ -35,7 +35,7 @@ public class FileSystemBrowserViewComponent : ViewComponent { return View(new { - Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? @"/" : @"C:\Users\", + Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? @"/" : @"C:\Users\", ServerBasePath = $"{_serverConfigurationModel.BaseAddress}:{_serverConfigurationModel.Port}/api/v{_serverConfigurationModel.ApiVersion}/", ClientBasePath = "http://localhost:5012/", // TODO: take from appsettings or environment ViewMode = FileSystemViewMode.List, From 8f9b8fe3b8a25096b904c170ce81dc4263df0a12 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 6 Nov 2024 20:53:54 +0200 Subject: [PATCH 3/4] Removes Mono.Posix package due to incompatibility with MacOs platforms --- .../Services/FileSystemPermissionsService.cs | 122 ++++++++++++------ src/Lumina.Domain/Lumina.Domain.csproj | 1 - 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs index 73ba1d4b..93393e83 100644 --- a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs +++ b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemPermissionsService.cs @@ -2,8 +2,6 @@ using Lumina.Domain.Common.Enums.FileSystem; using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform; using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects; -using Mono.Unix; -using Mono.Unix.Native; using System; using System.IO; using System.Runtime.Versioning; @@ -55,46 +53,94 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T /// The path for which to check the access. /// The access mode in which to check that path access. /// if the current user has rights for the specified path and acccess mode, otherwise. - /// Thrown when trying to check access for a mode that is not supported + /// Thrown when trying to check access for a mode that is not supported. private static bool CanAccessPathUnix(string path, FileAccessMode accessMode) { - AccessModes modes; - UnixFileSystemInfo fileInfo = new UnixFileInfo(path); - switch (accessMode) + try { - case FileAccessMode.ReadProperties: - // we would typically need execute permissions on a directory, to list its contents or view file properties - if (fileInfo.FileType == FileTypes.Directory) - return fileInfo.CanAccess(AccessModes.X_OK); - // for files, just verifying existence might be sufficient for reading properties - return fileInfo.Exists; - case FileAccessMode.ReadContents: - return fileInfo.CanAccess(AccessModes.R_OK); - case FileAccessMode.Write: - modes = AccessModes.W_OK; - break; - case FileAccessMode.Execute: - modes = AccessModes.X_OK; - break; - case FileAccessMode.ListDirectory: - modes = AccessModes.F_OK; // check existence, not an exact match but the closest - break; - case FileAccessMode.Delete: - if (fileInfo.FileType == FileTypes.Directory) - return fileInfo.CanAccess(AccessModes.W_OK | AccessModes.X_OK); // to delete a directory, we need write and execute permissions on the directory itself - else - { - // to delete a file, we need write permission on the parent directory - string? parentDirectoryPath = Path.GetDirectoryName(path); - if (string.IsNullOrEmpty(parentDirectoryPath)) - return false; // if path is a root directory or has no parent, deletion is not possible - UnixDirectoryInfo parentDirectoryInfo = new(parentDirectoryPath); - return parentDirectoryInfo.CanAccess(AccessModes.W_OK); - } - default: - throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode"); + switch (accessMode) + { + case FileAccessMode.ReadProperties: + // for properties, we only need to check if the path exists + return File.Exists(path) || Directory.Exists(path); + case FileAccessMode.ReadContents: + // attempt to open the file for reading to verify read access + if (File.Exists(path)) + { + FileStream fileStream = File.OpenRead(path); + fileStream.Dispose(); + return true; + } + return false; + case FileAccessMode.Write: + // attempt to open the file for writing or verify directory exists + if (File.Exists(path)) + { + FileStream fileStream = File.OpenWrite(path); + fileStream.Dispose(); + return true; + } + return Directory.Exists(path); + case FileAccessMode.Execute: + // check Unix execute permission bit + FileInfo fileInfo = new(path); + return (fileInfo.UnixFileMode & UnixFileMode.UserExecute) != 0; + case FileAccessMode.ListDirectory: + // attempt to list directory contents to verify access + if (Directory.Exists(path)) + { + string[] _ = Directory.GetFiles(path); + return true; + } + return false; + case FileAccessMode.Delete: + if (File.Exists(path)) + { + // for files, check if parent directory is writable + string? directoryPath = Path.GetDirectoryName(path); + return directoryPath != null && HasWriteAccessToDirectory(directoryPath); + } + // for directories, check if the directory itself is writable + return Directory.Exists(path) && HasWriteAccessToDirectory(path); + default: + throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode"); + } + } + catch (UnauthorizedAccessException) + { + return false; // access denied by the operating system + } + catch (IOException) + { + return false; // IO operation failed, indicating no access + } + } + + /// + /// Verifies write access to a directory by attempting to create and delete a temporary file. + /// + /// The directory path to check for write access. + /// if write access is granted, otherwise. + private static bool HasWriteAccessToDirectory(string path) + { + try + { + // create a unique temporary filename + string temporaryFilePath = Path.Combine(path, string.Format(".test_{0}", Guid.NewGuid())); + // attempt to create and immediately delete a temporary file + FileStream testStream = File.Create(temporaryFilePath); + testStream.Dispose(); + File.Delete(temporaryFilePath); + return true; + } + catch (UnauthorizedAccessException) + { + return false; + } + catch (IOException) + { + return false; } - return new UnixDirectoryInfo(path).CanAccess(modes); } /// diff --git a/src/Lumina.Domain/Lumina.Domain.csproj b/src/Lumina.Domain/Lumina.Domain.csproj index 7d360bf0..b566ae5a 100644 --- a/src/Lumina.Domain/Lumina.Domain.csproj +++ b/src/Lumina.Domain/Lumina.Domain.csproj @@ -18,7 +18,6 @@ - From b041673be08e61c8bee1869598cdabf41e977242 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 6 Nov 2024 21:08:43 +0200 Subject: [PATCH 4/4] Disables parallel tests run due to database race conditions --- .github/workflows/run-tests.yml | 1 + src/Lumina.Presentation.Api/Program.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 443d7c11..88dc4ce7 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -12,6 +12,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macOS-latest] + max-parallel: 1 environment: test steps: - uses: actions/checkout@v2 diff --git a/src/Lumina.Presentation.Api/Program.cs b/src/Lumina.Presentation.Api/Program.cs index 3b6256ce..eae291ce 100644 --- a/src/Lumina.Presentation.Api/Program.cs +++ b/src/Lumina.Presentation.Api/Program.cs @@ -67,7 +67,6 @@ public class Program .ReadFrom.Services(services) .Enrich.FromLogContext(); }); - string? aa = builder.Configuration.GetValue("LOG_PATH"); WebApplication app = builder.Build(); app.UseSerilogRequestLogging();