From 385b54334209d06e2761a0c0f3c0b782065ce79c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:46:35 -0600 Subject: add patch endpoint for options --- .../Core/API/Controllers/OptionsController.cs | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 25e9095a..f14acc39 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -22,6 +22,7 @@ namespace slskd.Core.API using System; using System.IO; using System.Reflection; + using System.Text.RegularExpressions; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -41,19 +42,22 @@ namespace slskd.Core.API public class OptionsController : ControllerBase { public OptionsController( + IApplication application, OptionsAtStartup optionsAtStartup, IOptionsSnapshot optionsSnapshot, IStateMutator stateMutator) { + Application = application; OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; StateMutator = stateMutator; } + private IApplication Application { get; } private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); + private ILogger Logger { get; } = Log.ForContext(); /// /// Gets the current application options. @@ -67,6 +71,41 @@ namespace slskd.Core.API return Ok(OptionsSnapshot.Value.Redact()); } + /// + /// Patch the application options with new values. + /// + /// The patch to apply. + /// + [HttpPatch] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Options), 200)] + public IActionResult Patch([FromBody] OptionsPatch patch) + { + if (patch is null) + { + return NoContent(); + } + + if (!patch.TryValidate(out var result)) + { + var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); + Logger.Warning("Options patch validation failed: {Message}", error); + return BadRequest(error); + } + + try + { + Application.ApplyOptionsPatchAsync(patch); + } + catch (Exception ex) + { + Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); + return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); + } + + return Ok(OptionsSnapshot.Value.Redact()); + } + /// /// Gets the application options provided at startup. /// -- cgit v1.2.3 From 8859b9a6c82b1490349f7ffb4f6699b502384f8a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:48:25 -0600 Subject: call it an overlay --- src/slskd/Common/Validation/Extensions.cs | 6 +- .../Core/API/Controllers/OptionsController.cs | 4 +- src/slskd/Core/OptionsOverlay.cs | 66 +++++++++++++++++++++ src/slskd/Core/OptionsPatch.cs | 69 ---------------------- 4 files changed, 71 insertions(+), 74 deletions(-) create mode 100644 src/slskd/Core/OptionsOverlay.cs delete mode 100644 src/slskd/Core/OptionsPatch.cs (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Common/Validation/Extensions.cs b/src/slskd/Common/Validation/Extensions.cs index 6233d53b..87d83965 100644 --- a/src/slskd/Common/Validation/Extensions.cs +++ b/src/slskd/Common/Validation/Extensions.cs @@ -48,15 +48,15 @@ namespace slskd.Validation /// /// Validates options. /// - /// The options patch instance to validate. + /// The options overlay instance to validate. /// The result of the validation, if invalid. /// A value indicating whether the instance is valid. - public static bool TryValidate(this OptionsPatch patch, out CompositeValidationResult result) + public static bool TryValidate(this OptionsOverlay overlay, out CompositeValidationResult result) { result = null; var results = new List(); - if (!Validator.TryValidateObject(patch, new ValidationContext(patch), results, true)) + if (!Validator.TryValidateObject(overlay, new ValidationContext(overlay), results, true)) { result = new CompositeValidationResult("Invalid configuration", results); return false; diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index f14acc39..291ae561 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -79,7 +79,7 @@ namespace slskd.Core.API [HttpPatch] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] - public IActionResult Patch([FromBody] OptionsPatch patch) + public IActionResult Patch([FromBody] OptionsOverlay patch) { if (patch is null) { @@ -95,7 +95,7 @@ namespace slskd.Core.API try { - Application.ApplyOptionsPatchAsync(patch); + Program.ApplyConfigurationOverlay(patch); } catch (Exception ex) { diff --git a/src/slskd/Core/OptionsOverlay.cs b/src/slskd/Core/OptionsOverlay.cs new file mode 100644 index 00000000..faea4ea7 --- /dev/null +++ b/src/slskd/Core/OptionsOverlay.cs @@ -0,0 +1,66 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// +namespace slskd +{ + using System; + using System.ComponentModel.DataAnnotations; + using slskd.Validation; + + /// + /// Volatile run-time overlay for application . + /// + /// + /// + /// The values specified in this overlay are applied at run-time only, are volatile (lost when the application restarts), + /// and take precedence over all other options, regardless of which method was used to define them. + /// + /// + /// Only options that can be applied while the application is running can be overlaid, given the nature of how + /// an overlay is applied. + /// + /// + /// Every property in this class must be nullable, and must have a null default value; the application + /// selectively applies the patch overlay only information explicitly supplied. + /// + /// + public class OptionsOverlay + { + /// + /// Gets options for the Soulseek client. + /// + [Validate] + public SoulseekOptionsPatch Soulseek { get; init; } = null; + + /// + /// Soulseek client options. + /// + public class SoulseekOptionsPatch + { + /// + /// Gets the local IP address on which to listen for incoming connections. + /// + [IPAddress] + public string ListenIpAddress { get; init; } = null; + + /// + /// Gets the port on which to listen for incoming connections. + /// + [Range(1024, 65535)] + public int? ListenPort { get; init; } = null; + } + } +} \ No newline at end of file diff --git a/src/slskd/Core/OptionsPatch.cs b/src/slskd/Core/OptionsPatch.cs deleted file mode 100644 index 6eee75e3..00000000 --- a/src/slskd/Core/OptionsPatch.cs +++ /dev/null @@ -1,69 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// -namespace slskd -{ - using System; - using System.ComponentModel.DataAnnotations; - using slskd.Validation; - - /// - /// Run-time patch for application . - /// - /// - /// - /// The values specified in this patch are applied at run-time only, are volatile (lost when the application restarts), - /// and take precedence over all other options, regardless of which method was used to define them. - /// - /// - /// Only options that can be applied while the application is running can be patched, given the nature of how - /// a patch is applied. - /// - /// - /// Every property in this class must be nullable, and must have a null default value; the application - /// selectively applies the patch using only information explicitly supplied. - /// - /// - public class OptionsPatch - { - public static OptionsPatch Current { get; private set; } = new(); - - [Validate] - public SoulseekOptionsPatch Soulseek { get; init; } = null; - - public static void SetCurrent(OptionsPatch patch) => Current = patch; - - /// - /// Soulseek client options. - /// - public class SoulseekOptionsPatch - { - /// - /// Gets the local IP address on which to listen for incoming connections. - /// - [IPAddress] -#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. - public string? ListenIpAddress { get; init; } = null; -#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. - - /// - /// Gets the port on which to listen for incoming connections. - /// - [Range(1024, 65535)] - public int? ListenPort { get; init; } = null; - } - } -} \ No newline at end of file -- cgit v1.2.3 From a631b5fbeaaf4e21c0a0a3c266d94d710eb4ffd7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 18:31:09 -0600 Subject: use /overlay endpoints --- .../Core/API/Controllers/OptionsController.cs | 33 ++++++++++++++-------- src/slskd/Program.cs | 5 ++++ 2 files changed, 26 insertions(+), 12 deletions(-) (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 291ae561..bfbb2dd7 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -42,18 +42,15 @@ namespace slskd.Core.API public class OptionsController : ControllerBase { public OptionsController( - IApplication application, OptionsAtStartup optionsAtStartup, IOptionsSnapshot optionsSnapshot, IStateMutator stateMutator) { - Application = application; OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; StateMutator = stateMutator; } - private IApplication Application { get; } private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } @@ -72,21 +69,21 @@ namespace slskd.Core.API } /// - /// Patch the application options with new values. + /// Overlay the application options with new values. /// - /// The patch to apply. + /// The overlay to apply. /// - [HttpPatch] + [HttpPut("overlay")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] - public IActionResult Patch([FromBody] OptionsOverlay patch) + public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { - if (patch is null) + if (overlay is null) { return NoContent(); } - if (!patch.TryValidate(out var result)) + if (!overlay.TryValidate(out var result)) { var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); Logger.Warning("Options patch validation failed: {Message}", error); @@ -95,15 +92,14 @@ namespace slskd.Core.API try { - Program.ApplyConfigurationOverlay(patch); + Program.ApplyConfigurationOverlay(overlay); + return Ok(OptionsSnapshot.Value.Redact()); } catch (Exception ex) { Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); } - - return Ok(OptionsSnapshot.Value.Redact()); } /// @@ -119,6 +115,19 @@ namespace slskd.Core.API return Ok(OptionsAtStartup.Redact()); } + /// + /// Gets the current application option overlay, if one has been applied. + /// + /// + [HttpGet] + [Route("overlay")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(OptionsOverlay), 200)] + public IActionResult GetOverlay() + { + return Ok(Program.ConfigurationOverlay?.Redact()); + } + /// /// Gets the debug view of the current application options. /// diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 143cd460..3b4cdb73 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -189,6 +189,11 @@ namespace slskd [EnvironmentVariable("CONFIG")] public static string ConfigurationFile { get; private set; } = null; + /// + /// Gets the current configuration overlay, if one has been applied. + /// + public static OptionsOverlay ConfigurationOverlay { get; private set; } = VolatileOverlayConfigurationSource?.CurrentValue; + /// /// Gets the path where persistent data is saved. /// -- cgit v1.2.3 From 74ca21dc0566b081f9fa372d6895b12bc0aaa569 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 19:37:48 -0600 Subject: tweak endpoints, create backup before updating --- .../Core/API/Controllers/OptionsController.cs | 45 +++++++++------------- 1 file changed, 19 insertions(+), 26 deletions(-) (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index bfbb2dd7..e6222791 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -22,7 +22,6 @@ namespace slskd.Core.API using System; using System.IO; using System.Reflection; - using System.Text.RegularExpressions; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -54,7 +53,7 @@ namespace slskd.Core.API private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext(); + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Gets the current application options. @@ -73,11 +72,17 @@ namespace slskd.Core.API /// /// The overlay to apply. /// - [HttpPut("overlay")] - [Authorize(Policy = AuthPolicy.Any)] + [HttpPatch] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] [ProducesResponseType(typeof(Options), 200)] public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { + if (!OptionsSnapshot.Value.RemoteConfiguration) + { + Log.Warning("Blocked options overlay application; remote configuration is disabled"); + return Forbid(); + } + if (overlay is null) { return NoContent(); @@ -85,19 +90,18 @@ namespace slskd.Core.API if (!overlay.TryValidate(out var result)) { - var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); - Logger.Warning("Options patch validation failed: {Message}", error); - return BadRequest(error); + Log.Warning("Options patch validation failed: {Message}", result.GetResultString()); + return BadRequest(result.GetResultString()); } try { Program.ApplyConfigurationOverlay(overlay); - return Ok(OptionsSnapshot.Value.Redact()); + return Ok(Program.ConfigurationOverlay?.Redact()); } catch (Exception ex) { - Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); + Log.Error(ex, "Failed to apply options patch: {Message}", ex.Message); return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); } } @@ -115,19 +119,6 @@ namespace slskd.Core.API return Ok(OptionsAtStartup.Redact()); } - /// - /// Gets the current application option overlay, if one has been applied. - /// - /// - [HttpGet] - [Route("overlay")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(OptionsOverlay), 200)] - public IActionResult GetOverlay() - { - return Ok(Program.ConfigurationOverlay?.Redact()); - } - /// /// Gets the debug view of the current application options. /// @@ -178,7 +169,7 @@ namespace slskd.Core.API return Ok(yaml); } - [HttpPost] + [HttpPut] [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)] [Route("yaml")] public IActionResult UpdateYamlFile([FromBody] string yaml) @@ -190,17 +181,19 @@ namespace slskd.Core.API if (!TryValidateYaml(yaml, out var error)) { - Logger.Error(error, "Failed to validate YAML configuration"); + Log.Error(error, "Failed to validate YAML configuration"); return BadRequest(error); } try { + IOFile.Copy(Program.ConfigurationFile, $"{Program.ConfigurationFile}.bak", overwrite: true); + IOFile.WriteAllText(Program.ConfigurationFile, yaml); if (OptionsSnapshot.Value.Flags.NoConfigWatch) { - Logger.Information("Configuration watch is disabled; restart required for changes to take effect"); + Log.Information("Configuration watch is disabled; restart required for changes to take effect"); StateMutator.SetValue(state => state with { PendingRestart = true }); } @@ -251,7 +244,7 @@ namespace slskd.Core.API } catch (Exception ex) { - Logger.Warning(ex, "Configuration validation failed"); + Log.Warning(ex, "Configuration validation failed"); error = ex.Message; -- cgit v1.2.3 From 5c30a775b4f4850e21f54a53b490857b379aa070 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:44:54 -0600 Subject: Update src/slskd/Core/API/Controllers/OptionsController.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Core/API/Controllers/OptionsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index e6222791..f5c1a5b6 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -74,7 +74,7 @@ namespace slskd.Core.API /// [HttpPatch] [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] - [ProducesResponseType(typeof(Options), 200)] + [ProducesResponseType(typeof(OptionsOverlay), 200)] public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { if (!OptionsSnapshot.Value.RemoteConfiguration) -- cgit v1.2.3