## What
L'enrichissement ONVIF authentifié est déplacé côté **agent**. Le pool de credentials du site (login/mdp) est exposé à l'agent via `GET /api/settings/agent/{id}` sous forme d'enveloppe AES-256-GCM (`AgentCredentialEnvelope`, clé dérivée du SHA-256 de l'API key) — déchiffrable uniquement côté agent. `NetworkCameraScanner` tente `GetDeviceInformation` avec chaque couple du pool (PasswordDigest puis PasswordText) et remonte des devices **déjà identifiés** (fabricant/modèle ONVIF + `CredentialLabel`). Le central persiste et marque le credential utilisé (`LastUsedAt`) — plus aucun appel réseau ONVIF côté central.
## Why
Le central n'est généralement **pas** sur le réseau des caméras : les appels ONVIF authentifiés qu'il faisait après réception des devices bruts échouaient. L'agent, lui, est sur le sous-réseau des caméras — c'est lui qui doit porter l'auth. Enrichissement et identification se font au moment du scan, avant la remontée.
## How
- `AgentCredentialEnvelope` (Application/Security) : AES-256-GCM, clé = SHA-256 de l'API key ; central chiffre (via l'`ApiKeyHash` stocké), agent déchiffre (via sa clé en clair).
- `GetEffectiveAgentConfigQuery` : ajoute `discovery.credentials` (pool chiffré) au dict de config.
- `SettingsController.GetEffectiveConfig` : accepte l'API key agent (vérification par site) en plus du JWT admin — corrige au passage un 403 latent sur cet endpoint.
- `NetworkCameraScanner` : probe ONVIF authentifié avec le pool (anonyme d'abord, puis chaque couple).
- `AgentCredentialStore` (mémoire, jamais sur disque) : reçoit le pool déchiffré par `ConfigReceiverService`, lu par `AgentDiscoveryService`.
- `CameraDiscoveryService` : suppression de l'enrichissement réseau ; persistance + `MarkUsed` via `CredentialLabel` ; vendor déduit du fabricant ONVIF remonté.
- `SitesController` : broadcast `ConfigReload` après add/delete credential pour que les agents re-fetch leur pool.
- Multi-agents : inchangé — upsert par (site, IP) et anti-doublon IP/MAC à la confirmation évitent les caméras en double même si un agent est éteint.
## Testing
- `dotnet build Heimdall.slnx` : 0 erreur / 0 warning.
- Application 346 verts, Agent 108 verts, Infrastructure 143 verts (hors Testcontainers).
- Web.Tests 213 verts — nouveaux bUnit : DiscoveriesPage (5) + SiteDetail credentials (4).
## Breaking Changes
Aucun changement de schéma (pas de migration). Comportement API : `GET /api/settings/agent/{id}` accepte désormais l'API key agent (accès élargi au cas d'usage prévu).
Related to #48
Co-authored-by: nonobis <nonobis@tordais.cc>
Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/49
108 lines
4.4 KiB
C#
108 lines
4.4 KiB
C#
using System.Net;
|
|
using Heimdall.Infrastructure.Services.Discovery;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Infrastructure.Tests;
|
|
|
|
public class OnvifProbeServiceTests
|
|
{
|
|
private readonly ILogger<OnvifProbeService> _logger = Substitute.For<ILogger<OnvifProbeService>>();
|
|
private readonly IHttpClientFactory _httpClientFactory = Substitute.For<IHttpClientFactory>();
|
|
|
|
[Fact]
|
|
public void Constructor_CreatesService()
|
|
{
|
|
var service = new OnvifProbeService(_httpClientFactory, _logger);
|
|
|
|
service.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProbeDeviceService_with_credentials_should_send_ws_security_header()
|
|
{
|
|
string? capturedBody = null;
|
|
var handler = new StubHttpMessageHandler(request =>
|
|
{
|
|
capturedBody = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
const string soapResponse = """
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
|
|
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
|
<soap:Body>
|
|
<tds:GetDeviceInformationResponse>
|
|
<tds:Manufacturer>Hikvision</tds:Manufacturer>
|
|
<tds:Model>DS-2CD2T47</tds:Model>
|
|
</tds:GetDeviceInformationResponse>
|
|
</soap:Body>
|
|
</soap:Envelope>
|
|
""";
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(soapResponse, System.Text.Encoding.UTF8, "application/soap+xml")
|
|
};
|
|
});
|
|
|
|
_httpClientFactory.CreateClient("Discovery").Returns(new HttpClient(handler));
|
|
|
|
var service = new OnvifProbeService(_httpClientFactory, _logger);
|
|
var result = await service.ProbeDeviceServiceAsync(
|
|
"http://192.168.1.10/onvif/device_service", "admin", "secret", CancellationToken.None);
|
|
|
|
result.ShouldNotBeNull();
|
|
result.Name.ShouldBe("Hikvision");
|
|
result.Hardware.ShouldBe("DS-2CD2T47");
|
|
capturedBody.ShouldNotBeNull();
|
|
capturedBody.ShouldContain("<Security xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">");
|
|
capturedBody.ShouldContain("<Username>admin</Username>");
|
|
capturedBody.ShouldContain("PasswordDigest");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProbeDeviceService_without_credentials_should_send_no_security_header()
|
|
{
|
|
string? capturedBody = null;
|
|
var handler = new StubHttpMessageHandler(request =>
|
|
{
|
|
capturedBody = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
const string soapResponse = """
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
|
|
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
|
|
<soap:Body>
|
|
<tds:GetDeviceInformationResponse>
|
|
<tds:Manufacturer>Axis</tds:Manufacturer>
|
|
<tds:Model>M3085</tds:Model>
|
|
</tds:GetDeviceInformationResponse>
|
|
</soap:Body>
|
|
</soap:Envelope>
|
|
""";
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(soapResponse, System.Text.Encoding.UTF8, "application/soap+xml")
|
|
};
|
|
});
|
|
|
|
_httpClientFactory.CreateClient("Discovery").Returns(new HttpClient(handler));
|
|
|
|
var service = new OnvifProbeService(_httpClientFactory, _logger);
|
|
var result = await service.ProbeDeviceServiceAsync(
|
|
"http://192.168.1.11/onvif/device_service", null, null, CancellationToken.None);
|
|
|
|
result.ShouldNotBeNull();
|
|
result.Name.ShouldBe("Axis");
|
|
capturedBody.ShouldNotBeNull();
|
|
capturedBody.ShouldNotContain("Security");
|
|
}
|
|
|
|
/// <summary>HttpMessageHandler stub for capturing request bodies.</summary>
|
|
private sealed class StubHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responder)
|
|
: HttpMessageHandler
|
|
{
|
|
protected override Task<HttpResponseMessage> SendAsync(
|
|
HttpRequestMessage request, CancellationToken ct)
|
|
=> Task.FromResult(responder(request));
|
|
}
|
|
}
|