/// <summary>
        /// Load the configuration file
        /// </summary>
        public SanteDBConfiguration Load()
        {
            if (!String.IsNullOrEmpty(this.m_configPath))
            {
                using (var fs = File.OpenRead(this.m_configPath))
                {
                    return(SanteDBConfiguration.Load(fs));
                }
            }
            else
            {
                var retVal = new SanteDBConfiguration();

                // Inital data source
                DcDataConfigurationSection dataSection = new DcDataConfigurationSection()
                {
                    MainDataSourceConnectionStringName = "santeDbData",
                    MessageQueueConnectionStringName   = "santeDbData",
                    MailDataStore    = "santeDbData",
                    ConnectionString = new System.Collections.Generic.List <ConnectionString>()
                    {
                        new ConnectionString()
                        {
                            Name     = "santeDbData",
                            Value    = $"dbfile={(String.IsNullOrEmpty(this.m_dataPath) ? "SanteDB.debug.sqlite" : this.m_dataPath )}",
                            Provider = "sqlite"
                        }
                    }
                };

                JavascriptRulesConfigurationSection jsConfiguration = new JavascriptRulesConfigurationSection()
                {
                    DebugMode       = true,
                    WorkerInstances = 1
                };

                // Initial Applet configuration
                AppletConfigurationSection appletSection = new AppletConfigurationSection()
                {
                    Security = new AppletSecurityConfiguration()
                    {
                        AllowUnsignedApplets = true,
                        TrustedPublishers    = new List <string>()
                        {
                            "82C63E1E9B87578D0727E871D7613F2F0FAF683B"
                        }
                    }
                };

                // Initial applet style
                ApplicationConfigurationSection appSection = new ApplicationConfigurationSection()
                {
                    Style       = StyleSchemeType.Dark,
                    UserPrefDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SdbDebug", "userpref"),
                    Cache       = new CacheConfiguration()
                    {
                        MaxAge         = new TimeSpan(0, 5, 0).Ticks,
                        MaxSize        = 1000,
                        MaxDirtyAge    = new TimeSpan(0, 20, 0).Ticks,
                        MaxPressureAge = new TimeSpan(0, 2, 0).Ticks
                    }
                };

                // Application service section
                ApplicationServiceContextConfigurationSection appServiceSection = new ApplicationServiceContextConfigurationSection()
                {
                    ThreadPoolSize   = Environment.ProcessorCount,
                    ServiceProviders = new List <TypeReferenceConfiguration>()
                    {
                        new TypeReferenceConfiguration(typeof(SanteDB.Core.Security.DefaultPolicyDecisionService)),
                        new TypeReferenceConfiguration(typeof(SQLitePolicyInformationService)),
                        new TypeReferenceConfiguration(typeof(LocalRepositoryFactoryService)),
                        //typeof(LocalAlertService).AssemblyQualifiedName,
                        new TypeReferenceConfiguration(typeof(LocalTagPersistenceService)),
                        new TypeReferenceConfiguration(typeof(NetworkInformationService)),
                        new TypeReferenceConfiguration(typeof(BusinessRulesDaemonService)),
                        new TypeReferenceConfiguration(typeof(PersistenceEntitySource)),
                        new TypeReferenceConfiguration(typeof(SanteDB.Caching.Memory.MemoryCacheService)),
                        new TypeReferenceConfiguration(typeof(SanteDB.Core.Services.Impl.DefaultThreadPoolService)),
                        new TypeReferenceConfiguration(typeof(MemorySessionManagerService)),
                        new TypeReferenceConfiguration(typeof(AmiUpdateManager)),
                        new TypeReferenceConfiguration(typeof(AppletClinicalProtocolRepository)),
                        new TypeReferenceConfiguration(typeof(MemoryQueryPersistenceService)),
                        new TypeReferenceConfiguration(typeof(SimpleQueueFileProvider)),
                        new TypeReferenceConfiguration(typeof(SimpleCarePlanService)),
                        new TypeReferenceConfiguration(typeof(SimplePatchService)),
                        new TypeReferenceConfiguration(typeof(DebugAppletManagerService)),
                        new TypeReferenceConfiguration(typeof(SQLiteConnectionManager)),
                        new TypeReferenceConfiguration(typeof(SQLitePersistenceService)),
                        new TypeReferenceConfiguration(typeof(SQLite.Net.Platform.SqlCipher.SQLitePlatformSqlCipher))
                    }
                };

                // Security configuration
                SecurityConfigurationSection secSection = new SecurityConfigurationSection()
                {
                    DeviceName     = Environment.MachineName,
                    AuditRetention = new TimeSpan(30, 0, 0, 0, 0)
                };

                // Device key
                //var certificate = X509CertificateUtils.FindCertificate(X509FindType.FindBySubjectName, StoreLocation.LocalMachine, StoreName.My, String.Format("DN={0}.mobile.santedb.org", macAddress));
                //secSection.DeviceSecret = certificate?.Thumbprint;

                // Rest Client Configuration
                ServiceClientConfigurationSection serviceSection = new ServiceClientConfigurationSection()
                {
                    RestClientType = typeof(RestClient)
                };

                // Trace writer
                DiagnosticsConfigurationSection diagSection = new DiagnosticsConfigurationSection()
                {
                    TraceWriter = new System.Collections.Generic.List <TraceWriterConfiguration>()
                    {
                        new TraceWriterConfiguration()
                        {
                            Filter             = System.Diagnostics.Tracing.EventLevel.Error,
                            InitializationData = "SanteDB",
                            TraceWriter        = typeof(ConsoleTraceWriter)
                        },
                        new TraceWriterConfiguration()
                        {
                            Filter             = System.Diagnostics.Tracing.EventLevel.LogAlways,
                            InitializationData = "SanteDB",
                            TraceWriter        = typeof(FileTraceWriter)
                        }
                    }
                };

                retVal.Sections.Add(appServiceSection);
                retVal.Sections.Add(appletSection);
                retVal.Sections.Add(dataSection);
                retVal.Sections.Add(diagSection);
                retVal.Sections.Add(appSection);
                retVal.Sections.Add(secSection);
                retVal.Sections.Add(serviceSection);
                retVal.Sections.Add(jsConfiguration);
                retVal.Sections.Add(new SynchronizationConfigurationSection()
                {
                    PollInterval = new TimeSpan(0, 5, 0)
                });

                return(retVal);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Configure
        /// </summary>
        public bool Configure(SanteDBConfiguration configuration, IDictionary <String, Object> options)
        {
            string dataDirectory = options["DataDirectory"].ToString();

            if (!options.ContainsKey("encrypt"))
            {
                options.Add("encrypt", false);
            }

            // Connection Strings
            DcDataConfigurationSection dataSection = new DcDataConfigurationSection()
            {
                MainDataSourceConnectionStringName = "santeDbData",
                MessageQueueConnectionStringName   = "santeDbQueue",
                MailDataStore    = "santeDbMail",
                ConnectionString = new System.Collections.Generic.List <ConnectionString>()
                {
                    new ConnectionString()
                    {
                        Name     = "santeDbData",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    },
                    new ConnectionString()
                    {
                        Name     = "santeDbMail",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.mail.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    },
                    new ConnectionString()
                    {
                        Name     = "santeDbSearch",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.ftsearch.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    },
                    new ConnectionString()
                    {
                        Name     = "santeDbQueue",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.queue.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    },
                    new ConnectionString()
                    {
                        Name     = "santeDbWarehouse",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.warehouse.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    },
                    new ConnectionString()
                    {
                        Name     = "santeDbAudit",
                        Value    = $"dbfile={Path.Combine(dataDirectory, "SanteDB.audit.sqlite")};encrypt={options["encrypt"].ToString().ToLower()}",
                        Provider = "sqlite"
                    }
                }
            };

            configuration.Sections.RemoveAll(o => o is DataConfigurationSection);
            configuration.Sections.Add(dataSection);
            // Services
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Insert(0, new TypeReferenceConfiguration(typeof(SQLiteConnectionManager)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLitePersistenceService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteMailPersistenceService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteQueueManagerService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteSynchronizationLog)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteBiDataSource)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteRoleProviderService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteIdentityService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLitePolicyInformationService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteAuditRepositoryService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteDeviceIdentityProviderService)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(MdmDataManager)));
            configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration(typeof(SQLiteSecurityChallengeService)));

            // Add the audit pruning job
            configuration.GetSection <JobConfigurationSection>().Jobs.Add(new JobItemConfiguration()
            {
                Type     = typeof(SQLiteAuditPruneJob),
                Schedule = new List <JobItemSchedule>()
                {
                    new JobItemSchedule()
                    {
                        Interval = 3600,
                        Type     = JobScheduleType.Interval
                    }
                }
            });

            // SQLite provider
#if NOCRYPT
            appSection.ServiceTypes.Add(typeof(SQLite.Net.Platform.Generic.SQLitePlatformGeneric).AssemblyQualifiedName);
#else
            var osiService = ApplicationServiceContext.Current.GetService <IOperatingSystemInfoService>();
            switch (osiService.OperatingSystem)
            {
            case OperatingSystemID.Win32:
                if (options.ContainsKey("encrypt") && options["encrypt"].Equals(true))
                {
                    configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration("SQLite.Net.Platform.SqlCipher.SQLitePlatformSqlCipher, SQLite.Net.Platform.SqlCipher"));
                }
                else
                {
                    configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration("SQLite.Net.Platform.Generic.SQLitePlatformGeneric, SQLite.Net.Platform.Generic"));
                }
                break;

            case OperatingSystemID.MacOS:
            case OperatingSystemID.Linux:
                configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration("SQLite.Net.Platform.Generic.SQLitePlatformGeneric, SQLite.Net.Platform.Generic"));
                break;

            case OperatingSystemID.Android:
                configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders.Add(new TypeReferenceConfiguration("SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid, SQLite.Net.Platform.XamarinAndroid"));
                break;
            }
#endif

            return(true);
        }
        /// <summary>
        /// Get a bare bones configuration
        /// </summary>
        public SanteDBConfiguration GetDefaultConfiguration(String instanceName)
        {
            // TODO: Bring up initial settings dialog and utility
            var retVal = new SanteDBConfiguration();

            // Inital data source
            DcDataConfigurationSection dataSection = new DcDataConfigurationSection()
            {
                MainDataSourceConnectionStringName = "santeDbData",
                MessageQueueConnectionStringName   = "santeDbQueue"
            };

            // Initial Applet configuration
            AppletConfigurationSection appletSection = new AppletConfigurationSection()
            {
                AppletDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "santedb", "sdk", "ade", this.m_instanceName, "applets"),
                StartupAsset    = "org.santedb.uicore",
                Security        = new AppletSecurityConfiguration()
                {
                    TrustedPublishers = new List <string>()
                    {
                        "82C63E1E9B87578D0727E871D7613F2F0FAF683B"
                    }
                }
            };

            // Initial applet style
            ApplicationConfigurationSection appSection = new ApplicationConfigurationSection()
            {
                Style       = StyleSchemeType.Dark,
                UserPrefDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "santedb", "sdk", "ade", this.m_instanceName, "userpref"),
                Cache       = new CacheConfiguration()
                {
                    MaxAge         = new TimeSpan(0, 5, 0).Ticks,
                    MaxSize        = 1000,
                    MaxDirtyAge    = new TimeSpan(0, 20, 0).Ticks,
                    MaxPressureAge = new TimeSpan(0, 2, 0).Ticks
                }
            };

            var appServiceSection = new ApplicationServiceContextConfigurationSection()
            {
                ThreadPoolSize   = Environment.ProcessorCount * 16,
                ServiceProviders = new List <TypeReferenceConfiguration>()
                {
                    new TypeReferenceConfiguration(typeof(AesSymmetricCrypographicProvider)),
                    new TypeReferenceConfiguration(typeof(MemoryTickleService)),
                    new TypeReferenceConfiguration(typeof(NetworkInformationService)),
                    new TypeReferenceConfiguration(typeof(SHA256PasswordHasher)),
                    new TypeReferenceConfiguration(typeof(SanteDB.Core.Security.DefaultPolicyDecisionService)),
                    new TypeReferenceConfiguration(typeof(SanteDB.Caching.Memory.MemoryAdhocCacheService)),
                    new TypeReferenceConfiguration(typeof(AppletLocalizationService)),
                    new TypeReferenceConfiguration(typeof(BusinessRulesDaemonService)),
                    new TypeReferenceConfiguration(typeof(AgsService)),
                    new TypeReferenceConfiguration(typeof(SanteDB.Caching.Memory.MemoryCacheService)),
                    new TypeReferenceConfiguration(typeof(DefaultThreadPoolService)),
                    new TypeReferenceConfiguration(typeof(SimpleCarePlanService)),
                    new TypeReferenceConfiguration(typeof(MemorySessionManagerService)),
                    new TypeReferenceConfiguration(typeof(AmiUpdateManager)),
                    new TypeReferenceConfiguration(typeof(AppletClinicalProtocolRepository)),
                    new TypeReferenceConfiguration(typeof(MemoryQueryPersistenceService)),
                    new TypeReferenceConfiguration(typeof(SimpleQueueFileProvider)),
                    new TypeReferenceConfiguration(typeof(SimplePatchService)),
                    new TypeReferenceConfiguration(typeof(DefaultBackupService)),
                    new TypeReferenceConfiguration(typeof(AmiSecurityChallengeProvider)),
                    new TypeReferenceConfiguration(typeof(MiniAppletManagerService)),
                    new TypeReferenceConfiguration(typeof(AppletBiRepository)),
                    new TypeReferenceConfiguration(typeof(SHA256PasswordHasher)),
                    new TypeReferenceConfiguration(typeof(DataPolicyFilterService)),
                    new TypeReferenceConfiguration(typeof(DefaultOperatingSystemInfoService)),
                    new TypeReferenceConfiguration(typeof(AppletSubscriptionRepository)),
                    new TypeReferenceConfiguration(typeof(InMemoryPivotProvider)),
                    new TypeReferenceConfiguration(typeof(AuditDaemonService)),
                    new TypeReferenceConfiguration(typeof(DefaultDataSigningService)),
                    new TypeReferenceConfiguration(typeof(GenericConfigurationPushService)),
                    new TypeReferenceConfiguration(typeof(QrBarcodeGenerator)),
                    new TypeReferenceConfiguration(typeof(FileSystemDispatcherQueueService))
                },
                AppSettings = new List <AppSettingKeyValuePair>()
                {
                }
            };

            // Security configuration
            var    wlan       = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(o => o.NetworkInterfaceType == NetworkInterfaceType.Ethernet || o.Description.StartsWith("wlan"));
            String macAddress = Guid.NewGuid().ToString();

            if (wlan != null)
            {
                macAddress = wlan.GetPhysicalAddress().ToString();
            }
            //else

            SecurityConfigurationSection secSection = new SecurityConfigurationSection()
            {
                DeviceName           = String.Format("Debugee-{0}", macAddress).Replace(" ", ""),
                AuditRetention       = new TimeSpan(30, 0, 0, 0, 0),
                DomainAuthentication = DomainClientAuthentication.Inline
            };

            // Device key
            var certificate = X509CertificateUtils.FindCertificate(X509FindType.FindBySubjectName, StoreLocation.LocalMachine, StoreName.My, String.Format("DN={0}.mobile.santedb.org", macAddress));

            secSection.DeviceSecret = certificate?.Thumbprint;

            // Rest Client Configuration
            ServiceClientConfigurationSection serviceSection = new ServiceClientConfigurationSection()
            {
                RestClientType = typeof(RestClient)
            };

            // Trace writer
#if DEBUG
            DiagnosticsConfigurationSection diagSection = new DiagnosticsConfigurationSection()
            {
                TraceWriter = new System.Collections.Generic.List <TraceWriterConfiguration>()
                {
                    new TraceWriterConfiguration()
                    {
                        Filter             = System.Diagnostics.Tracing.EventLevel.LogAlways,
                        InitializationData = "SanteDB",
                        TraceWriter        = typeof(LogTraceWriter)
                    },
                    new TraceWriterConfiguration()
                    {
                        Filter             = System.Diagnostics.Tracing.EventLevel.LogAlways,
                        InitializationData = "SanteDB",
                        TraceWriter        = typeof(FileTraceWriter)
                    },
                    new TraceWriterConfiguration()
                    {
                        Filter             = System.Diagnostics.Tracing.EventLevel.LogAlways,
                        InitializationData = "SanteDB",
                        TraceWriter        = typeof(ConsoleTraceWriter)
                    }
                }
            };
#else
            DiagnosticsConfigurationSection diagSection = new DiagnosticsConfigurationSection()
            {
                TraceWriter = new List <TraceWriterConfiguration>()
                {
                    new TraceWriterConfiguration()
                    {
                        Filter             = System.Diagnostics.Tracing.EventLevel.Informational,
                        InitializationData = "SanteDB",
                        TraceWriter        = typeof(FileTraceWriter)
                    },
                    new TraceWriterConfiguration()
                    {
                        Filter             = System.Diagnostics.Tracing.EventLevel.Informational,
                        InitializationData = "SanteDB",
                        TraceWriter        = typeof(ConsoleTraceWriter)
                    }
                }
            };
#endif
            retVal.Sections.Add(new FileSystemDispatcherQueueConfigurationSection()
            {
                QueuePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "santedb", "sdk", "ade", this.m_instanceName, "queue"),
            });
            retVal.Sections.Add(appServiceSection);
            retVal.Sections.Add(appletSection);
            retVal.Sections.Add(dataSection);
            retVal.Sections.Add(diagSection);
            retVal.Sections.Add(appSection);
            retVal.Sections.Add(secSection);
            retVal.Sections.Add(serviceSection);
            retVal.Sections.Add(new AuditAccountabilityConfigurationSection()
            {
                AuditFilters = new List <AuditFilterConfiguration>()
                {
                    // Audit any failure - No matter which event
                    new AuditFilterConfiguration(null, null, SanteDB.Core.Auditing.OutcomeIndicator.EpicFail | SanteDB.Core.Auditing.OutcomeIndicator.MinorFail | SanteDB.Core.Auditing.OutcomeIndicator.SeriousFail, true, true),
                    // Audit anything that creates, reads, or updates data
                    new AuditFilterConfiguration(SanteDB.Core.Auditing.ActionType.Create | SanteDB.Core.Auditing.ActionType.Read | SanteDB.Core.Auditing.ActionType.Update | SanteDB.Core.Auditing.ActionType.Delete, null, null, true, true)
                }
            });
            retVal.Sections.Add(AgsService.GetDefaultConfiguration());
            retVal.Sections.Add(new SynchronizationConfigurationSection()
            {
                PollInterval      = new TimeSpan(0, 15, 0),
                ForbiddenResouces = new List <SynchronizationForbidConfiguration>()
                {
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "DeviceEntity"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "ApplicationEntity"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "Concept"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "ConceptSet"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "Place"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "ReferenceTerm"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.All, "AssigningAuthority"),
                    new SynchronizationForbidConfiguration(SynchronizationOperationType.Obsolete, "UserEntity")
                }
            });

            var initConfig = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).SelectMany(a => a.ExportedTypes).Where(t => typeof(IInitialConfigurationProvider).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
            foreach (var t in initConfig.Distinct())
            {
                retVal = (Activator.CreateInstance(t) as IInitialConfigurationProvider).Provide(retVal);
            }

            return(retVal);
        }