2026-07-22 19:48:16 +00:00
|
|
|
using Heimdall.Application.Common;
|
2026-08-11 16:15:41 +02:00
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Application.Security;
|
2026-07-22 19:48:16 +00:00
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Settings.Queries.GetEffectiveAgentConfig;
|
|
|
|
|
|
|
|
|
|
public record GetEffectiveAgentConfigQuery(Guid AgentId) : IRequest<ServiceResult<Dictionary<string, string>>>;
|
|
|
|
|
|
|
|
|
|
public class GetEffectiveAgentConfigQueryHandler
|
|
|
|
|
: IRequestHandler<GetEffectiveAgentConfigQuery, ServiceResult<Dictionary<string, string>>>
|
|
|
|
|
{
|
|
|
|
|
private readonly IApplicationDbContext _db;
|
2026-08-11 16:15:41 +02:00
|
|
|
private readonly ICredentialProtector _protector;
|
2026-07-22 19:48:16 +00:00
|
|
|
|
2026-08-11 16:15:41 +02:00
|
|
|
public GetEffectiveAgentConfigQueryHandler(IApplicationDbContext db, ICredentialProtector protector)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
_protector = protector;
|
|
|
|
|
}
|
2026-07-22 19:48:16 +00:00
|
|
|
|
|
|
|
|
public async Task<ServiceResult<Dictionary<string, string>>> Handle(
|
|
|
|
|
GetEffectiveAgentConfigQuery request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
var appSettings = await _db.AppSettings.ToListAsync(ct).ConfigureAwait(false);
|
2026-07-22 19:48:16 +00:00
|
|
|
var config = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
|
|
|
|
|
|
|
|
foreach (var s in appSettings)
|
|
|
|
|
config[s.Key] = s.Value;
|
|
|
|
|
|
|
|
|
|
var agentSettings = await _db.AgentSettings
|
|
|
|
|
.Where(s => s.AgentId == request.AgentId)
|
|
|
|
|
.ToListAsync(ct);
|
|
|
|
|
|
|
|
|
|
foreach (var s in agentSettings)
|
|
|
|
|
config[s.Key] = s.Value;
|
|
|
|
|
|
2026-08-11 16:15:41 +02:00
|
|
|
// ── Camera credential pool (encrypted, decryptable only by the agent) ──
|
|
|
|
|
// The agent lives on the camera network, so it must own the authenticated
|
|
|
|
|
// ONVIF enrichment. The Central ships the site's credential pairs inside
|
|
|
|
|
// an AES-GCM envelope keyed by the agent's API key hash. The agent can
|
|
|
|
|
// decrypt it with its plaintext API key; nobody else can.
|
|
|
|
|
var agent = await _db.Agents
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefaultAsync(a => a.Id == request.AgentId, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (agent is not null && !string.IsNullOrEmpty(agent.ApiKeyHash))
|
|
|
|
|
{
|
|
|
|
|
var credentials = await _db.SiteCredentials
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(c => c.SiteId == agent.SiteId)
|
|
|
|
|
.ToListAsync(ct)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (credentials.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var pairs = credentials
|
|
|
|
|
.Select(c => new AgentCameraCredential(
|
|
|
|
|
c.Label,
|
|
|
|
|
c.Username,
|
|
|
|
|
_protector.Decrypt(c.Password)))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var key = AgentCredentialEnvelope.DeriveKeyFromApiKeyHash(agent.ApiKeyHash);
|
|
|
|
|
config["discovery.credentials"] = AgentCredentialEnvelope.Encrypt(pairs, key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 19:48:16 +00:00
|
|
|
return ServiceResult<Dictionary<string, string>>.Success(config);
|
|
|
|
|
}
|
|
|
|
|
}
|