2026-02-12 15:24:25 -05:00
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
2026-02-18 10:43:27 -05:00
|
|
|
using OTSSignsOrchestrator.Core.Models.Entities;
|
2026-02-12 15:24:25 -05:00
|
|
|
|
2026-02-18 10:43:27 -05:00
|
|
|
namespace OTSSignsOrchestrator.Core.Data;
|
2026-02-12 15:24:25 -05:00
|
|
|
|
|
|
|
|
public class XiboContext : DbContext
|
|
|
|
|
{
|
|
|
|
|
private readonly IDataProtectionProvider? _dataProtection;
|
|
|
|
|
|
|
|
|
|
public XiboContext(DbContextOptions<XiboContext> options, IDataProtectionProvider? dataProtection = null)
|
|
|
|
|
: base(options)
|
|
|
|
|
{
|
|
|
|
|
_dataProtection = dataProtection;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 10:43:27 -05:00
|
|
|
public DbSet<SshHost> SshHosts => Set<SshHost>();
|
2026-02-12 15:24:25 -05:00
|
|
|
public DbSet<OperationLog> OperationLogs => Set<OperationLog>();
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
|
2026-02-18 10:43:27 -05:00
|
|
|
// --- SshHost ---
|
|
|
|
|
modelBuilder.Entity<SshHost>(entity =>
|
2026-02-12 15:24:25 -05:00
|
|
|
{
|
2026-02-18 10:43:27 -05:00
|
|
|
entity.HasIndex(e => e.Label).IsUnique();
|
2026-02-12 15:24:25 -05:00
|
|
|
|
|
|
|
|
if (_dataProtection != null)
|
|
|
|
|
{
|
2026-02-18 10:43:27 -05:00
|
|
|
var protector = _dataProtection.CreateProtector("OTSSignsOrchestrator.SshHost");
|
|
|
|
|
var passphraseConverter = new ValueConverter<string?, string?>(
|
|
|
|
|
v => v != null ? protector.Protect(v) : null,
|
|
|
|
|
v => v != null ? protector.Unprotect(v) : null);
|
|
|
|
|
var passwordConverter = new ValueConverter<string?, string?>(
|
|
|
|
|
v => v != null ? protector.Protect(v) : null,
|
|
|
|
|
v => v != null ? protector.Unprotect(v) : null);
|
2026-02-12 15:24:25 -05:00
|
|
|
|
2026-02-18 10:43:27 -05:00
|
|
|
entity.Property(e => e.KeyPassphrase).HasConversion(passphraseConverter);
|
|
|
|
|
entity.Property(e => e.Password).HasConversion(passwordConverter);
|
2026-02-12 15:24:25 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// --- OperationLog ---
|
|
|
|
|
modelBuilder.Entity<OperationLog>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.Timestamp);
|
2026-02-19 08:27:54 -05:00
|
|
|
entity.HasIndex(e => e.StackName);
|
2026-02-12 15:24:25 -05:00
|
|
|
entity.HasIndex(e => e.Operation);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|