private async Task <ApplicationConfiguration> Load(ApplicationInstance application, int basePort) { #if !USE_FILE_CONFIG // load the application configuration. ApplicationConfiguration config = await application.LoadApplicationConfiguration(true).ConfigureAwait(false); #else string root = Path.Combine("%LocalApplicationData%", "OPC"); string gdsRoot = Path.Combine(root, "GDS"); var gdsConfig = new GlobalDiscoveryServerConfiguration() { AuthoritiesStorePath = Path.Combine(gdsRoot, "authorities"), ApplicationCertificatesStorePath = Path.Combine(gdsRoot, "applications"), DefaultSubjectNameContext = "O=OPC Foundation", CertificateGroups = new CertificateGroupConfigurationCollection() { new CertificateGroupConfiguration() { Id = "Default", CertificateType = "RsaSha256ApplicationCertificateType", SubjectName = "CN=GDS Test CA, O=OPC Foundation", BaseStorePath = Path.Combine(gdsRoot, "CA", "default"), DefaultCertificateHashSize = 256, DefaultCertificateKeySize = 2048, DefaultCertificateLifetime = 12, CACertificateHashSize = 512, CACertificateKeySize = 4096, CACertificateLifetime = 60 } }, DatabaseStorePath = Path.Combine(gdsRoot, "gdsdb.json") }; // build the application configuration. ApplicationConfiguration config = await application .Build( "urn:localhost:opcfoundation.org:GlobalDiscoveryTestServer", "http://opcfoundation.org/UA/GlobalDiscoveryTestServer") .AsServer(new string[] { "opc.tcp://localhost:58810/GlobalDiscoveryTestServer" }) .AddUserTokenPolicy(UserTokenType.Anonymous) .AddUserTokenPolicy(UserTokenType.UserName) .SetDiagnosticsEnabled(true) .AddServerCapabilities("GDS") .AddServerProfile("http://opcfoundation.org/UA-Profile/Server/GlobalDiscoveryAndCertificateManagement2017") .SetShutdownDelay(0) .AddSecurityConfiguration( "CN=Global Discovery Test Server, O=OPC Foundation, DC=localhost", gdsRoot) .SetAutoAcceptUntrustedCertificates(true) .SetRejectSHA1SignedCertificates(false) .SetRejectUnknownRevocationStatus(true) .SetMinimumCertificateKeySize(1024) .AddExtension <GlobalDiscoveryServerConfiguration>(null, gdsConfig) .SetDeleteOnLoad(true) .SetOutputFilePath(Path.Combine(root, "Logs", "Opc.Ua.Gds.Tests.log.txt")) .SetTraceMasks(519) .Create().ConfigureAwait(false); #endif TestUtils.PatchBaseAddressesPorts(config, basePort); return(config); }
public async Task StartServer(bool clean, int basePort = -1) { ApplicationInstance.MessageDlg = new ApplicationMessageDlg(); ApplicationInstance application = new ApplicationInstance { ApplicationName = "Global Discovery Server", ApplicationType = ApplicationType.Server, ConfigSectionName = "Opc.Ua.GlobalDiscoveryTestServer" }; // load the application configuration. ApplicationConfiguration config = await application.LoadApplicationConfiguration(false); TestUtils.PatchBaseAddressesPorts(config, basePort); if (clean) { string thumbprint = config.SecurityConfiguration.ApplicationCertificate.Thumbprint; if (thumbprint != null) { using (var store = config.SecurityConfiguration.ApplicationCertificate.OpenStore()) { await store.Delete(thumbprint); } } // always start with clean cert store TestUtils.CleanupTrustList(config.SecurityConfiguration.TrustedIssuerCertificates.OpenStore()); TestUtils.CleanupTrustList(config.SecurityConfiguration.TrustedPeerCertificates.OpenStore()); TestUtils.CleanupTrustList(config.SecurityConfiguration.RejectedCertificateStore.OpenStore()); } if (clean) { string thumbprint = config.SecurityConfiguration.ApplicationCertificate.Thumbprint; if (thumbprint != null) { using (var store = config.SecurityConfiguration.ApplicationCertificate.OpenStore()) { await store.Delete(thumbprint); } } // always start with clean cert store TestUtils.CleanupTrustList(config.SecurityConfiguration.ApplicationCertificate.OpenStore()); TestUtils.CleanupTrustList(config.SecurityConfiguration.TrustedIssuerCertificates.OpenStore()); TestUtils.CleanupTrustList(config.SecurityConfiguration.TrustedPeerCertificates.OpenStore()); TestUtils.CleanupTrustList(config.SecurityConfiguration.RejectedCertificateStore.OpenStore()); config = await application.LoadApplicationConfiguration(false); } TestUtils.PatchBaseAddressesPorts(config, basePort); // check the application certificate. bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0); if (!haveAppCertificate) { throw new Exception("Application instance certificate invalid!"); } if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates) { config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation); } // get the DatabaseStorePath configuration parameter. GlobalDiscoveryServerConfiguration gdsConfiguration = config.ParseExtension <GlobalDiscoveryServerConfiguration>(); string databaseStorePath = Utils.ReplaceSpecialFolderNames(gdsConfiguration.DatabaseStorePath); if (clean) { // clean up database if (File.Exists(databaseStorePath)) { File.Delete(databaseStorePath); } // clean up GDS stores TestUtils.DeleteDirectory(gdsConfiguration.AuthoritiesStorePath); TestUtils.DeleteDirectory(gdsConfiguration.ApplicationCertificatesStorePath); foreach (var group in gdsConfiguration.CertificateGroups) { TestUtils.DeleteDirectory(group.BaseStorePath); } } // start the server. m_server = new GlobalDiscoverySampleServer( JsonApplicationsDatabase.Load(databaseStorePath), new CertificateGroup()); await application.Start(m_server); ServerState serverState = Server.GetStatus().State; if ((serverState = Server.GetStatus().State) != ServerState.Running) { throw new ServiceResultException("Server failed to start"); } }