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/Controllers') 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