91dbe9ae11
- Nuovo progetto MachineGuard: libreria che verifica se la macchina corrente è autorizzata all'esecuzione tramite DPAPI (Data Protection API di Windows) - Nuovo progetto MachineGuardSetup: tool di configurazione da eseguire come Amministratore per registrare la macchina autorizzata - Data_Coupler.sln: aggiunti entrambi i nuovi progetti alla soluzione - Data_Coupler.csproj: aggiunto riferimento al progetto MachineGuard - Program.cs: integrazione MachineGuard all'avvio dell'applicazione; se la macchina non è autorizzata l'app viene arrestata immediatamente con log critico e scrittura nel Windows Event Log
76 lines
2.9 KiB
C#
76 lines
2.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace MachineGuard;
|
|
|
|
/// <summary>
|
|
/// Metodi di estensione per la registrazione di MachineGuard nel container DI.
|
|
/// </summary>
|
|
public static class MachineGuardExtensions
|
|
{
|
|
/// <summary>
|
|
/// Registra il servizio <see cref="IMachineGuard"/> nel container DI.
|
|
/// <para>
|
|
/// Se <c>MachineGuard:Enabled</c> è <c>false</c> in appsettings, viene registrato
|
|
/// un guard no-op che approva sempre la verifica (utile per sviluppo/CI).
|
|
/// Su piattaforme non-Windows, DPAPI non è disponibile e il guard viene disabilitato automaticamente.
|
|
/// </para>
|
|
/// </summary>
|
|
public static IServiceCollection AddMachineGuard(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<MachineGuardOptions>(
|
|
configuration.GetSection(MachineGuardOptions.SectionName));
|
|
|
|
services.AddSingleton<IMachineGuard>(sp =>
|
|
{
|
|
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
|
|
var logger = loggerFactory.CreateLogger("MachineGuard.Startup");
|
|
|
|
#if DEBUG
|
|
// In build Debug la protezione è sempre disabilitata — nessuna configurazione richiesta.
|
|
logger.LogInformation(
|
|
"MachineGuard: build DEBUG — protezione machine-binding disabilitata automaticamente.");
|
|
return new NullMachineGuard();
|
|
#else
|
|
// In build Release la protezione è sempre attiva.
|
|
// Può essere disabilitata esplicitamente via MachineGuard:Enabled = false
|
|
// (utile per ambienti Linux/Docker o casi eccezionali).
|
|
var options = sp.GetRequiredService<IOptions<MachineGuardOptions>>();
|
|
|
|
if (!options.Value.Enabled)
|
|
{
|
|
logger.LogWarning(
|
|
"MachineGuard: protezione machine-binding DISABILITATA via configurazione. " +
|
|
"Impostare MachineGuard:Enabled = true in produzione.");
|
|
return new NullMachineGuard();
|
|
}
|
|
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
logger.LogWarning(
|
|
"MachineGuard: DPAPI non è disponibile su piattaforme non-Windows. " +
|
|
"La protezione machine-binding è bypassata automaticamente.");
|
|
return new NullMachineGuard();
|
|
}
|
|
|
|
return CreateWindowsGuard(sp, options);
|
|
#endif
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
|
|
private static IMachineGuard CreateWindowsGuard(
|
|
IServiceProvider sp,
|
|
IOptions<MachineGuardOptions> options)
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<DpapiMachineGuard>>();
|
|
return new DpapiMachineGuard(options, logger);
|
|
}
|
|
}
|