## 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
306 lines
13 KiB
Plaintext
306 lines
13 KiB
Plaintext
@page "/sites/{Id:guid}"
|
|
@attribute [Authorize]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject ISnackbar Snackbar
|
|
@inject NavigationManager Nav
|
|
@inject IJSRuntime Js
|
|
|
|
<MudText Typo="Typo.h1" Class="mb-6">
|
|
<i class="fas fa-video mr-2"></i>
|
|
Site : @_site?.Name
|
|
</MudText>
|
|
|
|
@if (_loading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" />
|
|
}
|
|
else if (_site is null)
|
|
{
|
|
<MudAlert Severity="Severity.Warning">Site introuvable</MudAlert>
|
|
}
|
|
else
|
|
{
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h6" Class="mb-4">Informations</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Nom</MudText>
|
|
<MudText Typo="Typo.body1">@_site.Name</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Adresse</MudText>
|
|
<MudText Typo="Typo.body1">@(_site.Address ?? "-")</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Fuseau horaire</MudText>
|
|
<MudText Typo="Typo.body1">@_site.Timezone</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Statut</MudText>
|
|
<MudText Color="@(_site.Enabled ? Color.Success : Color.Default)">
|
|
@(_site.Enabled ? "● Actif" : "○ Inactif")
|
|
</MudText>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
@* Token d'enregistrement *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h6" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.Key" Class="mr-1" />
|
|
Token d'enregistrement agent
|
|
</MudText>
|
|
|
|
@if (!string.IsNullOrEmpty(_generatedToken))
|
|
{
|
|
<MudAlert Severity="Severity.Warning" Class="mb-4">
|
|
<strong>Token d'enregistrement :</strong><br />
|
|
<code class="hmd-code-block mt-2">
|
|
@_generatedToken
|
|
</code>
|
|
<MudText Typo="Typo.body2" Color="Color.Warning" Class="mt-2">
|
|
⚠ Copiez ce token maintenant. Il ne sera plus jamais affiché.
|
|
</MudText>
|
|
<MudText Typo="Typo.body2" Class="mt-1">
|
|
Expire le : @_tokenExpiresAt.ToString("g")
|
|
</MudText>
|
|
</MudAlert>
|
|
}
|
|
|
|
<MudButton Variant="Variant.Filled" Color="Color.Warning" OnClick="@GenerateToken" Disabled="@_generating">
|
|
@if (_generating) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" />
|
|
@(!string.IsNullOrEmpty(_generatedToken) ? "Régénérer" : "Générer un token")
|
|
</MudButton>
|
|
|
|
@if (!string.IsNullOrEmpty(_generatedToken))
|
|
{
|
|
<SiteInstallPanel SiteName="@_site.Name" SiteToken="@_generatedToken"
|
|
CentralUrl="@CentralUrl" />
|
|
}
|
|
</MudPaper>
|
|
|
|
@* Identifiants caméras (découverte automatique) *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<div class="d-flex justify-space-between align-center mb-4">
|
|
<MudText Typo="Typo.h6">
|
|
<MudIcon Icon="@Icons.Material.Filled.Lock" Class="mr-1" />
|
|
Identifiants caméras
|
|
</MudText>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small"
|
|
OnClick="@(() => _showCredentialDialog = true)">
|
|
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Ajouter
|
|
</MudButton>
|
|
</div>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
|
|
Déclarez les couples login/mot de passe possibles des caméras de ce site.
|
|
Les agents les utilisent pour authentifier et identifier automatiquement
|
|
chaque caméra détectée sur le réseau.
|
|
</MudText>
|
|
|
|
@if (_credentialsLoading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" Size="Size.Small" />
|
|
}
|
|
else if (_credentials.Count == 0)
|
|
{
|
|
<MudAlert Severity="Severity.Info" Variant="Variant.Outlined">
|
|
Aucun identifiant déclaré. La détection fonctionnera, mais les caméras
|
|
protégées ne seront pas identifiées automatiquement.
|
|
</MudAlert>
|
|
}
|
|
else
|
|
{
|
|
<MudTable Items="@_credentials" Hover="true" Dense="true">
|
|
<HeaderContent>
|
|
<MudTh>Libellé</MudTh>
|
|
<MudTh>Utilisateur</MudTh>
|
|
<MudTh>Dernière utilisation</MudTh>
|
|
<MudTh></MudTh>
|
|
</HeaderContent>
|
|
<RowTemplate>
|
|
<MudTd DataLabel="Libellé">@context.Label</MudTd>
|
|
<MudTd DataLabel="Utilisateur"><code>@context.Username</code></MudTd>
|
|
<MudTd DataLabel="Dernière utilisation">
|
|
@(context.LastUsedAt?.ToString("g") ?? "jamais")
|
|
</MudTd>
|
|
<MudTd DataLabel="Actions">
|
|
<MudTooltip Text="Supprimer cet identifiant">
|
|
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small"
|
|
Color="Color.Error" aria-label="Supprimer l'identifiant"
|
|
OnClick="@(() => DeleteCredential(context))" />
|
|
</MudTooltip>
|
|
</MudTd>
|
|
</RowTemplate>
|
|
</MudTable>
|
|
}
|
|
</MudPaper>
|
|
|
|
@* Navigation rapide *@
|
|
<MudText Typo="Typo.h6" Class="mb-4">Gestion</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="4">
|
|
<MudPaper Elevation="3" Class="pa-4 text-center" Style="border-radius: 12px; cursor: pointer;"
|
|
@onclick="@(() => Nav.NavigateTo($"/sites/{Id}/cameras"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Videocam" Size="Size.Large" Color="Color.Primary" />
|
|
<MudText Typo="Typo.h6" Class="mt-2">Caméras</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Gérer les caméras</MudText>
|
|
</MudPaper>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="4">
|
|
<MudPaper Elevation="3" Class="pa-4 text-center" Style="border-radius: 12px; cursor: pointer;"
|
|
@onclick="@(() => Nav.NavigateTo($"/sites/{Id}/agents"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Dns" Size="Size.Large" Color="Color.Secondary" />
|
|
<MudText Typo="Typo.h6" Class="mt-2">Agents</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Voir les agents</MudText>
|
|
</MudPaper>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="4">
|
|
<MudPaper Elevation="3" Class="pa-4 text-center" Style="border-radius: 12px; cursor: pointer;"
|
|
@onclick="@(() => Nav.NavigateTo($"/sites/{Id}/discoveries"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Radar" Size="Size.Large" Color="Color.Warning" />
|
|
<MudText Typo="Typo.h6" Class="mt-2">Détection</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Caméras détectées</MudText>
|
|
</MudPaper>
|
|
</MudItem>
|
|
</MudGrid>
|
|
|
|
<MudButton Variant="Variant.Outlined" Color="Color.Default" Class="mt-4"
|
|
OnClick="@(() => Nav.NavigateTo("/sites"))">
|
|
← Retour aux sites
|
|
</MudButton>
|
|
}
|
|
|
|
@* ─── Ajout d'identifiant caméra ─────────────────────────────────── *@
|
|
<MudOverlay Visible="@_showCredentialDialog" OnClick="@(() => _showCredentialDialog = false)" />
|
|
<MudDialog @bind-Visible="_showCredentialDialog">
|
|
<TitleContent><MudText Typo="Typo.h6">Ajouter un identifiant caméra</MudText></TitleContent>
|
|
<DialogContent>
|
|
<MudTextField @bind-Value="_newCredLabel" Label="Libellé *" Variant="Variant.Outlined"
|
|
Class="mb-4" Immediate="true"
|
|
HelperText="Ex : admin principal, compte NVR, ..." />
|
|
<MudTextField @bind-Value="_newCredUsername" Label="Utilisateur *" Variant="Variant.Outlined"
|
|
Class="mb-4" Immediate="true" />
|
|
<MudTextField @bind-Value="_newCredPassword" Label="Mot de passe *" Variant="Variant.Outlined"
|
|
InputType="InputType.Password" Class="mb-4" Immediate="true" />
|
|
@if (!string.IsNullOrEmpty(_credentialError))
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4">@_credentialError</MudAlert>
|
|
}
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton Variant="Variant.Text" OnClick="@(() => _showCredentialDialog = false)">Annuler</MudButton>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@AddCredential"
|
|
Disabled="@(_savingCredential || string.IsNullOrWhiteSpace(_newCredLabel) || string.IsNullOrWhiteSpace(_newCredUsername) || string.IsNullOrWhiteSpace(_newCredPassword))">
|
|
@if (_savingCredential) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
Ajouter
|
|
</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
|
|
@code {
|
|
[Parameter] public Guid Id { get; set; }
|
|
|
|
private SiteItem? _site;
|
|
private bool _loading = true;
|
|
private bool _generating;
|
|
private string? _generatedToken;
|
|
private DateTime _tokenExpiresAt;
|
|
private string _dockerComposeSnippet = "";
|
|
|
|
// ── Identifiants caméras ──────────────────────────────────────
|
|
private List<SiteCredentialItem> _credentials = [];
|
|
private bool _credentialsLoading = true;
|
|
private bool _showCredentialDialog;
|
|
private bool _savingCredential;
|
|
private string _newCredLabel = "";
|
|
private string _newCredUsername = "";
|
|
private string _newCredPassword = "";
|
|
private string _credentialError = "";
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_site = await Api.GetSiteAsync(Id);
|
|
}
|
|
catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
|
|
finally { _loading = false; }
|
|
|
|
await LoadCredentials();
|
|
}
|
|
|
|
private async Task LoadCredentials()
|
|
{
|
|
_credentialsLoading = true;
|
|
try
|
|
{
|
|
_credentials = await Api.GetSiteCredentialsAsync(Id);
|
|
}
|
|
catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
|
|
finally { _credentialsLoading = false; }
|
|
}
|
|
|
|
private async Task AddCredential()
|
|
{
|
|
_savingCredential = true;
|
|
_credentialError = "";
|
|
try
|
|
{
|
|
var result = await Api.AddSiteCredentialAsync(Id, _newCredLabel, _newCredUsername, _newCredPassword);
|
|
if (result is null)
|
|
{
|
|
_credentialError = "Erreur lors de l'ajout (libellé ou utilisateur déjà utilisé ?)";
|
|
return;
|
|
}
|
|
Snackbar.Add("Identifiant ajouté", Severity.Success);
|
|
_showCredentialDialog = false;
|
|
_newCredLabel = "";
|
|
_newCredUsername = "";
|
|
_newCredPassword = "";
|
|
await LoadCredentials();
|
|
}
|
|
catch { _credentialError = UiErrors.OperationFailed(); }
|
|
finally { _savingCredential = false; }
|
|
}
|
|
|
|
private async Task DeleteCredential(SiteCredentialItem credential)
|
|
{
|
|
if (await Api.DeleteSiteCredentialAsync(Id, credential.Id))
|
|
{
|
|
Snackbar.Add($"Identifiant \"{credential.Label}\" supprimé", Severity.Success);
|
|
await LoadCredentials();
|
|
}
|
|
else
|
|
{
|
|
Snackbar.Add("Erreur lors de la suppression", Severity.Error);
|
|
}
|
|
}
|
|
|
|
private async Task GenerateToken()
|
|
{
|
|
_generating = true;
|
|
try
|
|
{
|
|
var result = await Api.GenerateSiteTokenAsync(Id);
|
|
if (result is null)
|
|
{
|
|
Snackbar.Add("Erreur lors de la génération du token", Severity.Error);
|
|
return;
|
|
}
|
|
_generatedToken = result.Token;
|
|
_tokenExpiresAt = result.ExpiresAt;
|
|
_dockerComposeSnippet = GenerateDockerComposeSnippet(_generatedToken);
|
|
Snackbar.Add("Token généré ! Copiez le docker-compose ci-dessous.", Severity.Warning);
|
|
}
|
|
catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
|
|
finally { _generating = false; }
|
|
}
|
|
|
|
private string GenerateDockerComposeSnippet(string token) => "";
|
|
|
|
/// <summary>URL publique du serveur Heimdall (utilisée dans le compose généré).</summary>
|
|
private string CentralUrl => "https://heimdall.tordais.cc";
|
|
}
|