diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-01-29 19:37:48 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-01-29 19:37:48 -0600 |
| commit | 74ca21dc0566b081f9fa372d6895b12bc0aaa569 (patch) | |
| tree | 7a77340c5d35e905594ce725b644fa02ab3e0dce /src | |
| parent | 501c0415d3091d92b79569353b98c96d05a78909 (diff) | |
| download | slskd-74ca21dc0566b081f9fa372d6895b12bc0aaa569.tar.gz | |
tweak endpoints, create backup before updating
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/API/Controllers/OptionsController.cs | 45 |
1 files changed, 19 insertions, 26 deletions
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<Options> OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator<State> StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext<OptionsController>(); + private ILogger Log { get; } = Serilog.Log.ForContext<OptionsController>(); /// <summary> /// Gets the current application options. @@ -73,11 +72,17 @@ namespace slskd.Core.API /// </summary> /// <param name="overlay">The overlay to apply.</param> /// <returns></returns> - [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}"); } } @@ -116,19 +120,6 @@ namespace slskd.Core.API } /// <summary> - /// Gets the current application option overlay, if one has been applied. - /// </summary> - /// <returns></returns> - [HttpGet] - [Route("overlay")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(OptionsOverlay), 200)] - public IActionResult GetOverlay() - { - return Ok(Program.ConfigurationOverlay?.Redact()); - } - - /// <summary> /// Gets the debug view of the current application options. /// </summary> /// <returns></returns> @@ -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; |
