begin: PHI Column Encryption
This commit is contained in:
@@ -2,7 +2,14 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
||||
private readonly IPhiEncryptionService? _phiCrypto;
|
||||
|
||||
public AppDbContext(
|
||||
DbContextOptions<AppDbContext> options,
|
||||
IPhiEncryptionService? phiCrypto = null) : base(options)
|
||||
{
|
||||
_phiCrypto = phiCrypto;
|
||||
}
|
||||
|
||||
public DbSet<Patient> Patients => Set<Patient>();
|
||||
public DbSet<Encounter> Encounters => Set<Encounter>();
|
||||
@@ -21,9 +28,13 @@ public class AppDbContext : DbContext
|
||||
public DbSet<ExternalResourceIdentifier> ExternalResourceIdentifiers => Set<ExternalResourceIdentifier>();
|
||||
public DbSet<ClinicalUser> ClinicalUsers => Set<ClinicalUser>();
|
||||
public DbSet<ClinicalAuditLog> ClinicalAuditLogs => Set<ClinicalAuditLog>();
|
||||
public DbSet<PhiAccessLog> PhiAccessLogs => Set<PhiAccessLog>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
||||
|
||||
if (_phiCrypto is not null)
|
||||
modelBuilder.ConfigurePhiConverters(_phiCrypto);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public class PatientConfiguration : IEntityTypeConfiguration<Patient>
|
||||
builder.Property(p => p.Allergies).HasColumnName("allergies");
|
||||
builder.Property(p => p.EmergencyContactName).HasColumnName("emergency_contact_name").HasMaxLength(200);
|
||||
builder.Property(p => p.EmergencyContactPhone).HasColumnName("emergency_contact_phone").HasMaxLength(20);
|
||||
builder.Property(p => p.NameSearchToken)
|
||||
.HasColumnName("name_search_token")
|
||||
.HasMaxLength(64);
|
||||
builder.Property(p => p.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
|
||||
|
||||
// MRN uses exact-match unique index — MRN lookups are always equality checks,
|
||||
@@ -29,5 +32,6 @@ public class PatientConfiguration : IEntityTypeConfiguration<Patient>
|
||||
// equality match — so no index here; full ILIKE is intentionally unindexed
|
||||
// at this scale (pg_trgm GIN would be warranted at >500k patients).
|
||||
builder.HasIndex(p => p.Mrn).IsUnique();
|
||||
builder.HasIndex(p => p.NameSearchToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
public class PhiAccessLogConfiguration : IEntityTypeConfiguration<PhiAccessLog>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PhiAccessLog> builder)
|
||||
{
|
||||
builder.ToTable("phi_access_logs");
|
||||
builder.HasKey(p => p.Id);
|
||||
builder.Property(p => p.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
|
||||
builder.Property(p => p.AccessType).HasColumnName("access_type").HasMaxLength(20).IsRequired()
|
||||
.HasConversion(v => v.ToDbString(), v => PhiAccessTypeExtensions.FromDbString(v));
|
||||
builder.Property(p => p.PatientId).HasColumnName("patient_id");
|
||||
builder.Property(p => p.UserId).HasColumnName("user_id").IsRequired();
|
||||
builder.Property(p => p.UserDisplayName).HasColumnName("user_display_name").HasMaxLength(200).IsRequired();
|
||||
builder.Property(p => p.ResourcePath).HasColumnName("resource_path").HasMaxLength(500).IsRequired();
|
||||
builder.Property(p => p.SearchQueryHash).HasColumnName("search_query_hash").HasMaxLength(64);
|
||||
builder.Property(p => p.ResultCount).HasColumnName("result_count");
|
||||
builder.Property(p => p.IpAddress).HasColumnName("ip_address").HasMaxLength(45);
|
||||
builder.Property(p => p.CorrelationId).HasColumnName("correlation_id").HasMaxLength(100);
|
||||
builder.Property(p => p.AccessedAt).HasColumnName("accessed_at").HasDefaultValueSql("NOW()");
|
||||
|
||||
builder.HasIndex(p => p.PatientId);
|
||||
builder.HasIndex(p => p.UserId);
|
||||
builder.HasIndex(p => p.AccessedAt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public static class PatientPhiConverterConfigurator
|
||||
{
|
||||
public static void ConfigurePhiConverters(
|
||||
this ModelBuilder modelBuilder,
|
||||
IPhiEncryptionService crypto)
|
||||
{
|
||||
var entity = modelBuilder.Entity<Patient>();
|
||||
|
||||
entity.Property(p => p.FirstName)
|
||||
.HasConversion(
|
||||
v => crypto.Encrypt(v),
|
||||
v => crypto.Decrypt(v));
|
||||
|
||||
entity.Property(p => p.LastName)
|
||||
.HasConversion(
|
||||
v => crypto.Encrypt(v),
|
||||
v => crypto.Decrypt(v));
|
||||
|
||||
entity.Property(p => p.Allergies)
|
||||
.HasConversion(
|
||||
v => v == null ? null! : crypto.Encrypt(v),
|
||||
v => v == null ? null : crypto.Decrypt(v));
|
||||
|
||||
entity.Property(p => p.EmergencyContactName)
|
||||
.HasConversion(
|
||||
v => v == null ? null! : crypto.Encrypt(v),
|
||||
v => v == null ? null : crypto.Decrypt(v));
|
||||
|
||||
entity.Property(p => p.EmergencyContactPhone)
|
||||
.HasConversion(
|
||||
v => v == null ? null! : crypto.Encrypt(v),
|
||||
v => v == null ? null : crypto.Decrypt(v));
|
||||
|
||||
// DateOfBirth stored as encrypted ISO string
|
||||
entity.Property(p => p.DateOfBirth)
|
||||
.HasConversion(
|
||||
v => crypto.Encrypt(v.ToString("yyyy-MM-dd")),
|
||||
v => DateOnly.Parse(crypto.Decrypt(v)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user