225 lines
10 KiB
C#
225 lines
10 KiB
C#
using Heimdall.Application.Common;
|
|||
|
|
using Heimdall.Application.DTOs;
|
||
|
|
using Heimdall.Application.Services;
|
||
|
|
using Heimdall.Api.Hubs;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/settings")]
|
||
|
|
[Authorize]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class SettingsController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ISettingService _settingService;
|
||
|
|
private readonly IConfigBroadcastService _broadcast;
|
||
|
|
private readonly ILogger<SettingsController> _logger;
|
||
|
|
|
||
|
|
public SettingsController(
|
||
|
|
ISettingService settingService,
|
||
|
|
IConfigBroadcastService broadcast,
|
||
|
|
ILogger<SettingsController> logger)
|
||
|
|
{
|
||
|
|
_settingService = settingService;
|
||
|
|
_broadcast = broadcast;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>List all global settings (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(List<AppSettingDto>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public async Task<IActionResult> ListAll(CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.GetAllSettingsAsync(ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
// Redact sensitive values before returning to client
|
||
|
|
var redacted = result.Data!.Select(s => s with
|
||
|
|
{
|
||
|
|
Value = RedactValue(s.ValueType, s.Value, s.Key)
|
||
|
|
}).ToList();
|
||
|
|
|
||
|
|
return Ok(redacted);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get a global setting by key (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpGet("{key}")]
|
||
|
|
[ProducesResponseType(typeof(AppSettingDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetByKey(string key, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.GetSettingByKeyAsync(key, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return NotFound(new { error = result.Error });
|
||
|
|
|
||
|
|
// Redact sensitive values before returning to client
|
||
|
|
var redacted = result.Data! with
|
||
|
|
{
|
||
|
|
Value = RedactValue(result.Data.ValueType, result.Data.Value, result.Data.Key)
|
||
|
|
};
|
||
|
|
|
||
|
|
return Ok(redacted);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Create a new global setting (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(AppSettingDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public async Task<IActionResult> Create(
|
||
|
|
[FromBody] CreateAppSettingRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.CreateSettingAsync(request, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
await _broadcast.NotifyGlobalSettingChanged(result.Data!.Key, result.Data.Value, ct);
|
||
|
|
_logger.LogInformation("Created global setting {Key} = {Value}", result.Data.Key, RedactValue(result.Data.ValueType, result.Data.Value));
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Update a global setting (Admin only). Broadcasts the change to all agents.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpPut("{key}")]
|
||
|
|
[ProducesResponseType(typeof(AppSettingDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Update(
|
||
|
|
string key, [FromBody] UpdateAppSettingRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.UpdateSettingAsync(key, request, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
await _broadcast.NotifyGlobalSettingChanged(result.Data!.Key, result.Data.Value, ct);
|
||
|
|
_logger.LogInformation("Updated global setting {Key} = {Value}", result.Data.Key, RedactValue(result.Data.ValueType, result.Data.Value));
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Delete a global setting (Admin only). Broadcasts the change to all agents.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpDelete("{key}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Delete(string key, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.DeleteSettingAsync(key, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
await _broadcast.NotifyAllAgentsReload(ct);
|
||
|
|
_logger.LogInformation("Deleted global setting {Key}", key);
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Get the effective configuration for an agent: global defaults merged with
|
||
|
|
/// per-agent overrides (Agent or Admin only).
|
||
|
|
/// </summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpGet("agent/{agentId:guid}")]
|
||
|
|
[ProducesResponseType(typeof(Dictionary<string, string>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetEffectiveConfig(Guid agentId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.GetEffectiveAgentConfigAsync(agentId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: NotFound(new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>List all setting overrides for a specific agent (Admin only).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpGet("agent/{agentId:guid}/overrides")]
|
||
|
|
[ProducesResponseType(typeof(List<AgentSettingDto>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> ListAgentOverrides(Guid agentId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.GetAgentSettingsAsync(agentId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: NotFound(new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Set (create or update) a per-agent setting override (Admin only).
|
||
|
|
/// Broadcasts the change to the affected agent in real-time.
|
||
|
|
/// </summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpPut("agent/{agentId:guid}/overrides")]
|
||
|
|
[ProducesResponseType(typeof(AgentSettingDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> SetAgentOverride(
|
||
|
|
Guid agentId, [FromBody] SetAgentSettingRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.SetAgentSettingAsync(agentId, request, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
await _broadcast.NotifyAgentSettingChanged(agentId, result.Data!.Key, result.Data.Value, ct);
|
||
|
|
_logger.LogInformation("Set agent override Agent={AgentId}, {Key} = {Value}",
|
||
|
|
agentId, result.Data.Key, RedactValue(null, result.Data.Value, result.Data.Key));
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Remove a per-agent setting override (Admin only).
|
||
|
|
/// Broadcasts a reload notification to the affected agent.
|
||
|
|
/// </summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpDelete("agent/{agentId:guid}/overrides/{key}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> RemoveAgentOverride(Guid agentId, string key, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _settingService.RemoveAgentSettingAsync(agentId, key, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
|
||
|
|
await _broadcast.NotifyAgentSettingChanged(agentId, key, string.Empty, ct);
|
||
|
|
_logger.LogInformation("Removed agent override Agent={AgentId}, {Key}", agentId, key);
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Broadcast a "ConfigReload" signal to all connected agents so they
|
||
|
|
/// re-fetch their full configuration (Admin only).
|
||
|
|
/// </summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)]
|
||
|
|
[HttpPost("reload")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status202Accepted)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public async Task<IActionResult> ReloadAllAgents(CancellationToken ct)
|
||
|
|
{
|
||
|
|
await _broadcast.NotifyAllAgentsReload(ct);
|
||
|
|
_logger.LogInformation("Broadcast ConfigReload to all agents");
|
||
|
|
return Accepted(new { message = "Config reload broadcast to all connected agents" });
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string RedactValue(string? valueType, string value, string? key = null)
|
||
|
|
{
|
||
|
|
if (valueType is not null && valueType.Contains("password", StringComparison.OrdinalIgnoreCase))
|
||
|
|
return "***REDACTED***";
|
||
|
|
if (valueType is not null && valueType.Contains("token", StringComparison.OrdinalIgnoreCase))
|
||
|
|
return "***REDACTED***";
|
||
|
|
if (key is not null && key.Contains("password", StringComparison.OrdinalIgnoreCase))
|
||
|
|
return "***REDACTED***";
|
||
|
|
if (key is not null && key.Contains("token", StringComparison.OrdinalIgnoreCase))
|
||
|
|
return "***REDACTED***";
|
||
|
|
if (key is not null && key.Contains("secret", StringComparison.OrdinalIgnoreCase))
|
||
|
|
return "***REDACTED***";
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
}
|