Skip to content

Commit

Permalink
Fix TrustListHandling
Browse files Browse the repository at this point in the history
  • Loading branch information
romanett committed Mar 5, 2024
1 parent 3aaecb9 commit cdeb6e4
Show file tree
Hide file tree
Showing 20 changed files with 648 additions and 315 deletions.
4 changes: 2 additions & 2 deletions GDSwithREST.Domain/Entities/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public Application()
ApplicationNames = new HashSet<ApplicationName>();
CertificateRequests = new HashSet<CertificateRequest>();
ServerEndpoints = new HashSet<ServerEndpoint>();
TrustLists = new HashSet<TrustList>();
}

public int Id { get; set; }
Expand All @@ -18,10 +19,9 @@ public Application()
public string ServerCapabilities { get; set; } = null!;
public byte[] Certificate { get; set; } = Array.Empty<byte>();
public byte[]? HttpsCertificate { get; set; }
public string? TrustListId { get; set; }
public string? HttpsTrustListId { get; set; }
public ICollection<ApplicationName> ApplicationNames { get; set; }
public ICollection<CertificateRequest> CertificateRequests { get; set; }
public ICollection<ServerEndpoint> ServerEndpoints { get; set; }
public ICollection<TrustList> TrustLists { get; set; }
}
}
11 changes: 11 additions & 0 deletions GDSwithREST.Domain/Entities/TrustList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GDSwithREST.Domain.Entities
{
public sealed record TrustList
{
public int Id { get; set; }
public int ApplicationId { get; set; }
public string Path { get; set; } = null!;
public string CertificateType { get; set; } = null!;
public Application Application { get; set; } = null!;
}
}
2 changes: 1 addition & 1 deletion GDSwithREST.Domain/Repositories/IApplicationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IApplicationRepository
/// <summary>
/// persists the changes made to an Application instance
/// </summary>
public void SaveChanges();
public void SaveChanges(Application application);
}
}
18 changes: 18 additions & 0 deletions GDSwithREST.Domain/Repositories/ITrustListRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using GDSwithREST.Domain.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GDSwithREST.Domain.Repositories
{
public interface ITrustListRepository
{
public Task<IEnumerable<TrustList>> GetTrustListsByApplicationId(int id);
public void RemoveTrustLists(TrustList[] trustLists);
public TrustList AddTrustList(TrustList trustList);
public void SaveChanges(TrustList trustList);
}
}
62 changes: 30 additions & 32 deletions GDSwithREST.Domain/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ record = applicationRepository.GetApplicationById(applicationId).Result;
applicationNameRepository.AddApplicationName(new ApplicationName() { ApplicationId = record.Id, Locale = applicationName.Locale, Text = applicationName.Text });
}
}
applicationRepository.SaveChanges(record);
m_lastCounterResetTime = DateTime.UtcNow;
return new NodeId(applicationId, NamespaceIndex); ;
}
Expand All @@ -122,6 +123,9 @@ public override void UnregisterApplication(NodeId applicationId)
applicationNameRepository.RemoveApplicationNames(application.ApplicationNames.ToArray());
serverEndpointRepository.RemoveServerEndpoints(application.ServerEndpoints.ToArray());
applicationRepository.RemoveApplication(application);

certificateRequestRepository.SaveChanges();
applicationRepository.SaveChanges(application);
m_lastCounterResetTime = DateTime.UtcNow;
}

Expand Down Expand Up @@ -528,7 +532,7 @@ public override bool SetApplicationCertificate(
application.Certificate = certificate;
}

applicationRepository.SaveChanges();
applicationRepository.SaveChanges(application);


return true;
Expand Down Expand Up @@ -583,27 +587,33 @@ string trustListId
{
using var scope = _serviceScopeFactory.CreateScope();
var applicationRepository = scope.ServiceProvider.GetRequiredService<IApplicationRepository>();
var trustListRepository = scope.ServiceProvider.GetRequiredService<ITrustListRepository>();

Guid id = GetNodeIdGuid(applicationId);
var application = applicationRepository.GetApplicationById(id).Result;

if (application == null || string.IsNullOrEmpty(trustListId))
if (application == null || string.IsNullOrEmpty(trustListId) || string.IsNullOrEmpty(certificateTypeId))
{
return false;
}


if(certificateTypeId == nameof(Opc.Ua.ObjectTypeIds.ApplicationCertificateType))
var trustList = trustListRepository.GetTrustListsByApplicationId(application.Id).Result
.SingleOrDefault(trustList => trustList.CertificateType == certificateTypeId);

if (trustList == null)
{
application.TrustListId = trustListId;
trustListRepository.AddTrustList(
new TrustList
{
ApplicationId = application.Id,
Application = application,
CertificateType = certificateTypeId,
Path = trustListId
});
}
if(certificateTypeId == nameof(Opc.Ua.ObjectTypeIds.HttpsCertificateType))
else
{
application.HttpsTrustListId = trustListId;
trustList.Path = trustListId;
}

applicationRepository.SaveChanges();

return true;
}

Expand All @@ -616,36 +626,24 @@ out string trustListId
trustListId = null!;
using var scope = _serviceScopeFactory.CreateScope();
var applicationRepository = scope.ServiceProvider.GetRequiredService<IApplicationRepository>();
var trustListRepository = scope.ServiceProvider.GetRequiredService<ITrustListRepository>();

Guid id = GetNodeIdGuid(applicationId);
var application = applicationRepository.GetApplicationById(id).Result;

if (application == null)
if (application == null || string.IsNullOrEmpty(certificateTypeId))
{
return false;
}

if (certificateTypeId == nameof(Opc.Ua.ObjectTypeIds.ApplicationCertificateType))
{
if(string.IsNullOrEmpty(application.TrustListId))
{
return false;
}
trustListId = application.TrustListId;
return true;
}
if (certificateTypeId == nameof(Opc.Ua.ObjectTypeIds.HttpsCertificateType))
var trustList = trustListRepository.GetTrustListsByApplicationId(application.Id).Result
.SingleOrDefault(trustList => trustList.CertificateType == certificateTypeId);
//var trustList = application.TrustLists.SingleOrDefault(trustList => trustList.CertificateType == certificateTypeId);

if (trustList == null)
{
if (string.IsNullOrEmpty(application.HttpsTrustListId))
{
return false;
}
trustListId = application.HttpsTrustListId;
return true;
return false;
}

applicationRepository.SaveChanges();

trustListId = trustList.Path;
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions GDSwithREST.Domain/Services/CertificateRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public NodeId StartSigningRequest(
application.CertificateRequests.Add(request);
}

applicationRepository.SaveChanges();
applicationRepository.SaveChanges(application);

return new NodeId(request.RequestId, NamespaceIndex);

Expand Down Expand Up @@ -120,7 +120,7 @@ public NodeId StartNewKeyPairRequest(
application.CertificateRequests.Add(request);
}

applicationRepository.SaveChanges();
applicationRepository.SaveChanges(application);

return new NodeId(request.RequestId, NamespaceIndex);

Expand Down
8 changes: 1 addition & 7 deletions GDSwithREST.Domain/Services/GdsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ public async Task StartServer(CancellationToken stoppingToken)
//start GDS
await _applicationInstance.Start(gdsServer);

////trust GDS CA
//var defaultCertificateGroup = _certificateGroups.CertificateGroups.SingleOrDefault(cg => cg.Id.Identifier is (uint)CertificateGroupType.DefaultApplicationGroup);
//if (defaultCertificateGroup is null)
// throw new Exception("Failed to initialze GDS CA Certifcate");

//await _applicationInstance.AddOwnCertificateToTrustedStoreAsync(defaultCertificateGroup.Certificate, stoppingToken);


var endpoints = _applicationInstance.Server.GetEndpoints().Select(e => e.EndpointUrl).Distinct();

foreach (var endpoint in endpoints)
Expand Down
27 changes: 23 additions & 4 deletions GDSwithREST.Infrastructure/GdsDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public GdsDbContext(DbContextOptions<GdsDbContext> options)
public virtual DbSet<Application> Applications { get; set; }
public virtual DbSet<CertificateRequest> CertificateRequests { get; set; }
public virtual DbSet<ServerEndpoint> ServerEndpoints { get; set; }
public virtual DbSet<TrustList> TrustLists { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down Expand Up @@ -48,13 +49,31 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(d => d.ApplicationId)
.HasConstraintName("FK_ApplicationNames_ApplicationId");
});
modelBuilder.Entity<TrustList>(entity => {
entity.HasIndex(e => e.ApplicationId)
.HasDatabaseName("IX_FK_TrustLists_ApplicationId");
modelBuilder.Entity<Application>(entity =>
{
entity.Property(e => e.HttpsTrustListId).IsRequired(false);
entity.HasIndex(e => new { e.CertificateType, e.ApplicationId })
.IsUnique();
entity.Property(e => e.Id).HasColumnName("ID");
entity.Property(e => e.CertificateType)
.HasMaxLength(200)
.IsRequired(true);
entity.Property(e => e.Path)
.IsRequired()
.HasMaxLength(1000);
entity.Property(e => e.TrustListId).IsRequired(false);
entity.HasOne(d => d.Application)
.WithMany(p => p.TrustLists)
.HasForeignKey(d => d.ApplicationId)
.HasConstraintName("FK_TrustLists_ApplicationId");
});

modelBuilder.Entity<Application>(entity =>
{
entity.Property(e => e.Id).HasColumnName("ID");
entity.Property(e => e.ApplicationName)
Expand Down
Loading

0 comments on commit cdeb6e4

Please sign in to comment.