Skip to content

Commit 231aab8

Browse files
Add core service status events and cleanup/refactor
1 parent 9290720 commit 231aab8

File tree

16 files changed

+767
-130
lines changed

16 files changed

+767
-130
lines changed

src/Certify.Core/Management/CertifyManager/CertifyManager.Account.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Certify.Models;
77
using Certify.Models.Config;
88
using Certify.Models.Providers;
9+
using Certify.Models.Reporting;
910
using Certify.Providers.ACME.Anvil;
1011
using Microsoft.Extensions.Logging;
1112
using Newtonsoft.Json;
@@ -692,11 +693,26 @@ private void LoadCertificateAuthorities()
692693
{
693694
_certificateAuthorities.TryAdd(ca.Id, ca);
694695
}
696+
697+
AddSystemStatusItem(
698+
SystemStatusCategories.SERVICE_CORE,
699+
SystemStatusKeys.SERVICE_CORE_CA_CUSTOM_LOAD,
700+
title: "Core Service Load Custom CAs",
701+
description: $"Loaded custom Certificate Authority config"
702+
);
695703
}
696704
catch (Exception exp)
697705
{
698706
// failed to load custom CAs
699707
_serviceLog?.Error(exp.Message);
708+
709+
AddSystemStatusItem(
710+
SystemStatusCategories.SERVICE_CORE,
711+
SystemStatusKeys.SERVICE_CORE_CA_CUSTOM_LOAD,
712+
title: "Core Service Load Custom CAs",
713+
description: $"Failed to load one or more custom CA configurations",
714+
hasError: true
715+
);
700716
}
701717
}
702718
}

src/Certify.Core/Management/CertifyManager/CertifyManager.DataStores.cs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
6+
using Certify.Core.Management.Access;
7+
using Certify.Datastore.SQLite;
58
using Certify.Models;
69
using Certify.Models.Config;
710
using Certify.Providers;
@@ -13,6 +16,89 @@ public partial class CertifyManager
1316
{
1417
private object _dataStoreLocker = new object();
1518

19+
20+
private async Task InitDataStore()
21+
{
22+
var enableExtendedDataStores = true;
23+
24+
try
25+
{
26+
if (enableExtendedDataStores)
27+
{
28+
29+
var defaultStoreId = CoreAppSettings.Current.ConfigDataStoreConnectionId;
30+
var dataStoreInfo = await GetDataStore(defaultStoreId);
31+
32+
if (string.IsNullOrEmpty(defaultStoreId) || defaultStoreId == "(default)")
33+
{
34+
// default sqlite storage
35+
_itemManager = new SQLiteManagedItemStore("", _serviceLog);
36+
_credentialsManager = new SQLiteCredentialStore("", _serviceLog);
37+
38+
// config store is a generic store for settings etc
39+
_configStore = new SQLiteConfigurationStore("", _serviceLog);
40+
_accessControl = new AccessControl(_serviceLog, _configStore);
41+
}
42+
else
43+
{
44+
// select data store based on current default selection
45+
var managedItemStoreOK = await SelectManagedItemStore(defaultStoreId);
46+
if (!managedItemStoreOK)
47+
{
48+
var msg = $"FATAL: Managed Item Store {defaultStoreId} could not connect or load. Service will not start.";
49+
_serviceLog.Error(msg);
50+
throw new Exception(msg);
51+
}
52+
53+
var credentialStoreOK = await SelectCredentialsStore(defaultStoreId);
54+
55+
if (!credentialStoreOK)
56+
{
57+
var msg = $"FATAL: Credential Store {defaultStoreId} could not connect or load. Service will not start.";
58+
_serviceLog.Error(msg);
59+
throw new Exception(msg);
60+
}
61+
62+
_serviceLog.Information($"Certify Manager is connected to data store {dataStoreInfo.Id} '{dataStoreInfo.Title}' [{dataStoreInfo.TypeId}]");
63+
}
64+
}
65+
else
66+
{
67+
_itemManager = new SQLiteManagedItemStore("", _serviceLog);
68+
_credentialsManager = new SQLiteCredentialStore("", _serviceLog);
69+
70+
_configStore = new SQLiteConfigurationStore("", _serviceLog);
71+
_accessControl = new AccessControl(_serviceLog, _configStore);
72+
}
73+
74+
// attempt to create and delete a test item
75+
try
76+
{
77+
var item = new ManagedCertificate { Id = $"writecheck_{Guid.NewGuid()}" };
78+
79+
await _itemManager.Update(item);
80+
81+
await _itemManager.Delete(item);
82+
}
83+
catch (Exception ex)
84+
{
85+
_serviceLog?.Error(ex, $"Data store write failed. Check connection and data integrity. Ensure file based databases are not subject to locks via AV scanning etc as this can cause data corruption. {ex}", ex.Message);
86+
throw;
87+
}
88+
89+
if (!_itemManager.IsInitialised().Result)
90+
{
91+
_serviceLog?.Error($"Item Manager failed to initialise properly. Check service logs for more information.");
92+
}
93+
}
94+
catch (Exception exp)
95+
{
96+
var msg = $"Failed to open or upgrade the managed items data store. :: {exp}";
97+
_serviceLog?.Error(msg);
98+
throw new Exception(msg);
99+
}
100+
}
101+
16102
private async Task<IManagedItemStore> GetManagedItemStoreProvider(DataStoreConnection dataStore)
17103
{
18104

0 commit comments

Comments
 (0)