[backend/asp] Allow customizing the unix socket permissions that are set on startup (ISH-671)

This commit is contained in:
Laura Hausmann 2025-01-08 12:04:53 +01:00
parent ce6784b4c4
commit 62493dbe19
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 31 additions and 13 deletions

View file

@ -36,6 +36,7 @@ public sealed class Config
[Range(1, 65535)] public int ListenPort { get; init; } = 3000; [Range(1, 65535)] public int ListenPort { get; init; } = 3000;
[Required] public string ListenHost { get; init; } = "localhost"; [Required] public string ListenHost { get; init; } = "localhost";
public string? ListenSocket { get; init; } public string? ListenSocket { get; init; }
public string ListenSocketPerms { get; init; } = "660";
[Required] public string WebDomain { get; init; } = null!; [Required] public string WebDomain { get; init; } = null!;
[Required] public string AccountDomain { get; init; } = null!; [Required] public string AccountDomain { get; init; } = null!;
[Range(1, 100000)] public int CharacterLimit { get; init; } = 8192; [Range(1, 100000)] public int CharacterLimit { get; init; } = 8192;

View file

@ -302,8 +302,8 @@ public static class WebApplicationExtensions
public static void SetKestrelUnixSocketPermissions(this WebApplication app) public static void SetKestrelUnixSocketPermissions(this WebApplication app)
{ {
var config = app.Configuration.GetSection("Instance").Get<Config.InstanceSection>() ?? var config = app.Configuration.GetSection("Instance").Get<Config.InstanceSection>()
throw new Exception("Failed to read instance config"); ?? throw new Exception("Failed to read instance config");
if (config.ListenSocket == null) return; if (config.ListenSocket == null) return;
using var scope = app.Services.CreateScope(); using var scope = app.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILoggerFactory>() var logger = scope.ServiceProvider.GetRequiredService<ILoggerFactory>()
@ -312,14 +312,30 @@ public static class WebApplicationExtensions
if (!OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS() && !OperatingSystem.IsFreeBSD()) if (!OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS() && !OperatingSystem.IsFreeBSD())
throw new Exception("Can't set unix socket permissions on a non-UNIX system"); throw new Exception("Can't set unix socket permissions on a non-UNIX system");
var perms = "660"; int perms;
var exitCode = chmod(config.ListenSocket, Convert.ToInt32(perms, 8)); try
{
perms = Convert.ToInt32(config.ListenSocketPerms, 8);
}
catch
{
logger.LogError("Failed to set Kestrel unix socket permissions to {SocketPerms}: failed to parse octal digits",
config.ListenSocketPerms);
Environment.Exit(1);
return;
}
var exitCode = chmod(config.ListenSocket, perms);
if (exitCode < 0) if (exitCode < 0)
{
logger.LogError("Failed to set Kestrel unix socket permissions to {SocketPerms}, return code: {ExitCode}", logger.LogError("Failed to set Kestrel unix socket permissions to {SocketPerms}, return code: {ExitCode}",
perms, exitCode); config.ListenSocketPerms, exitCode);
}
else else
logger.LogInformation("Kestrel unix socket permissions were set to {SocketPerms}", perms); {
logger.LogInformation("Kestrel unix socket permissions were set to {SocketPerms}",
config.ListenSocketPerms);
}
return; return;

View file

@ -4,6 +4,7 @@ ListenHost = localhost
;; If you want to have the application listen on a unix socket instead, uncomment the line below. Make sure to configure filesystem permissions correctly! ;; If you want to have the application listen on a unix socket instead, uncomment the line below. Make sure to configure filesystem permissions correctly!
;;ListenSocket = /var/run/iceshrimp/iceshrimp.net.sock ;;ListenSocket = /var/run/iceshrimp/iceshrimp.net.sock
;;ListenSocketPerms = 660
;; Caution: changing these settings after initial setup *will* break federation ;; Caution: changing these settings after initial setup *will* break federation
WebDomain = shrimp.example.org WebDomain = shrimp.example.org