## 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
152 lines
5.1 KiB
C#
152 lines
5.1 KiB
C#
using Bunit;
|
|
using Heimdall.Web.Components.Pages;
|
|
using Heimdall.Web.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MudBlazor;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
public class DiscoveriesPageTests : PageTestBase
|
|
{
|
|
private static readonly Guid SiteId = Guid.NewGuid();
|
|
private static readonly Guid DiscoveryId = Guid.NewGuid();
|
|
|
|
private static PendingDiscoveryItem Discovery(
|
|
string ip = "192.168.1.10",
|
|
string? vendor = "Hikvision",
|
|
string? hardware = "DS-2CD2T47",
|
|
string[]? urls = null) => new()
|
|
{
|
|
Id = DiscoveryId,
|
|
SiteId = SiteId,
|
|
IpAddress = ip,
|
|
MacAddress = "3C:EF:8C:12:34:56",
|
|
Vendor = vendor,
|
|
OnvifHardware = hardware,
|
|
IsReachable = true,
|
|
DetectedType = "Standard",
|
|
DiscoveryMethod = "OnvifWsDiscovery",
|
|
OpenPorts = [80, 554, 3702],
|
|
OnvifDeviceServiceUrl = $"http://{ip}/onvif/device_service",
|
|
OnvifName = vendor,
|
|
OnvifPort = 3702,
|
|
ProbedAt = DateTime.UtcNow.AddMinutes(-2),
|
|
StreamUrls = urls ?? [$"rtsp://{ip}:554/stream1"],
|
|
};
|
|
|
|
private static AgentItem Agent() => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Agent 1",
|
|
Hostname = "host1",
|
|
Status = "Online",
|
|
};
|
|
|
|
private void RouteDiscoveries(List<PendingDiscoveryItem> discoveries)
|
|
{
|
|
Handler.OnGet("api/discovery/sites/", discoveries);
|
|
Handler.OnGet("api/agents/by-site/", new PagedData<AgentItem>([Agent()], 1, 1, 50));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AfficheListeDesDecouvertes()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
cut.Markup.ShouldContain("Caméras détectées");
|
|
cut.Markup.ShouldContain("192.168.1.10");
|
|
cut.Markup.ShouldContain("Hikvision");
|
|
cut.Markup.ShouldContain("DS-2CD2T47");
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AucuneDecouverte_AfficheMessage()
|
|
{
|
|
RouteDiscoveries([]);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucune caméra détectée"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_ScanNow_NotifieAgents()
|
|
{
|
|
RouteDiscoveries([]);
|
|
Handler.On(HttpMethod.Post, "scan-now", new ScanNowResult(2));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Scanner maintenant"));
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Scanner maintenant")).Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("2 agent", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_Confirmer_AjouteCamera()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
Handler.On(HttpMethod.Post, "confirm", new CameraItem
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Hikvision (192.168.1.10)",
|
|
Status = "Online",
|
|
});
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Confirmer la caméra']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Ajouter la caméra 192.168.1.10"));
|
|
|
|
// Le nom par défaut est pré-rempli et l'URL de flux sélectionnée → bouton actif.
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Ajouter la caméra") && b.TextContent.Contains("Ajouter")).Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ajoutée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_Rejeter_IgnoreDetection()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
Handler.On(HttpMethod.Post, $"api/discovery/{DiscoveryId}/reject", System.Net.HttpStatusCode.OK);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Ignorer la détection']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ignorée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
}
|