Skip to content

Commit ab303a0

Browse files
Hub: implement hub instance registration on startup
1 parent b40789d commit ab303a0

File tree

14 files changed

+245
-41
lines changed

14 files changed

+245
-41
lines changed

src/Certify.Client/CertifyApiClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,12 @@ public async Task<List<SecurityPrinciple>> GetAccessSecurityPrinciples(AuthConte
767767
return JsonConvert.DeserializeObject<ActionResult>(await result.Content.ReadAsStringAsync());
768768
}
769769

770+
public async Task<HubInfo> GetHubInfo(AuthContext authContext = null)
771+
{
772+
var result = await FetchAsync("system/hub/info", authContext);
773+
return JsonToObject<HubInfo>(result);
774+
}
775+
770776
#endregion
771777
private T JsonToObject<T>(string json)
772778
{

src/Certify.Client/ICertifyClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Certify.Models;
@@ -31,6 +31,7 @@ public partial interface ICertifyInternalApiClient
3131
Task<ActionResult> JoinManagementHub(HubJoiningClientSecret hubJoiningClientSecret, AuthContext authContext = null);
3232
Task<ActionResult> CheckManagementHubCredentials(HubJoiningClientSecret hubJoiningClientSecret, AuthContext authContext = null);
3333
Task<ActionResult> CheckManagementHubConnectionStatus(AuthContext authContext = null);
34+
Task<HubInfo> GetHubInfo(AuthContext authContext = null);
3435
Task<Certify.Models.Config.ActionResult> CheckApiTokenHasAccess(AccessToken token, AccessCheck check, AuthContext authContext = null);
3536
#endregion System
3637

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
@@ -24,6 +24,16 @@ private async Task UpgradeSettings()
2424
var systemVersion = Util.GetAppVersion().ToString();
2525
var previousVersion = CoreAppSettings.Current.CurrentServiceVersion;
2626

27+
if (
28+
Environment.GetEnvironmentVariable("CERTIFY_ENABLE_MANAGEMENT_HUB")?.Equals("true", StringComparison.InvariantCultureIgnoreCase) == true
29+
|| CoreAppSettings.Current.IsManagementHubService
30+
|| _isDirectMgmtHubBackend
31+
)
32+
{
33+
// instance is running as a management hub backend (remote or direct)
34+
_isMgtmHubBackend = true;
35+
}
36+
2737
if (CoreAppSettings.Current.CurrentServiceVersion != systemVersion || Environment.GetEnvironmentVariable("CERTIFY_UPGRADE_SETTINGS") == "true")
2838
{
2939
_tc?.TrackEvent("ServiceUpgrade", new Dictionary<string, string> {
@@ -34,22 +44,55 @@ private async Task UpgradeSettings()
3444
// service has been updated, run any required migrations
3545
await PerformServiceUpgrades();
3646

47+
// update the system version
3748
CoreAppSettings.Current.CurrentServiceVersion = systemVersion;
3849

39-
if (Environment.GetEnvironmentVariable("CERTIFY_ENABLE_MANAGEMENT_HUB")?.Equals("true", StringComparison.InvariantCultureIgnoreCase) == true)
50+
if (_isMgtmHubBackend)
4051
{
4152
CoreAppSettings.Current.IsManagementHubService = true;
4253
}
4354

4455
SettingsManager.SaveAppSettings();
4556

46-
var accessControl = await GetCurrentAccessControl();
47-
57+
// if we are a management hub backend, register with the hub if not already registered
4858
if (CoreAppSettings.Current.IsManagementHubService)
4959
{
60+
var accessControl = await GetCurrentAccessControl();
5061
await AccessControlConfig.ConfigureStandardUsersAndRoles(accessControl);
5162
}
5263
}
64+
65+
if (_isMgtmHubBackend)
66+
{
67+
// we are the hub backend instance directly connected, if we are not already registered, register now
68+
69+
var hubInstance = string.IsNullOrEmpty(_serverConfig.HubAssignedInstanceId) ? null : await GetHubManagedInstance(_serverConfig.HubAssignedInstanceId);
70+
71+
if (hubInstance == null)
72+
{
73+
var newInstance = GetManagedInstanceInfo();
74+
75+
newInstance.Description = "Primary Certify Manager Instance (Hub)";
76+
77+
var newHubInstanceResult = await AddHubManagedInstance(newInstance);
78+
79+
_serverConfig.HubAssignedInstanceId = newHubInstanceResult.Result.InstanceId;
80+
SharedUtils.ServiceConfigManager.StoreUpdatedAppServiceConfig(_serverConfig);
81+
}
82+
else
83+
{
84+
// update instance details
85+
var updatedInstance = GetManagedInstanceInfo();
86+
87+
hubInstance.OS = updatedInstance.OS;
88+
hubInstance.OSVersion = updatedInstance.OSVersion;
89+
hubInstance.Title = updatedInstance.Title;
90+
hubInstance.ClientName = updatedInstance.ClientName;
91+
hubInstance.ClientVersion = updatedInstance.ClientVersion;
92+
93+
await UpdateHubManagedInstance(hubInstance);
94+
}
95+
}
5396
}
5497

5598
/// <summary>
@@ -264,7 +307,7 @@ private async Task PerformCertificateStatusChecks(CancellationToken cancelToken,
264307
#if NET9_0_OR_GREATER
265308
var x509Cert2 = System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12FromFile(item.CertificatePath, await GetPfxPassword(item));
266309
#else
267-
var x509Cert2 = new System.Security.Cryptography.X509Certificates.X509Certificate2(File.ReadAllBytes(item.CertificatePath), await GetPfxPassword(item));
310+
var x509Cert2 = new System.Security.Cryptography.X509Certificates.X509Certificate2(File.ReadAllBytes(item.CertificatePath), await GetPfxPassword(item));
268311
#endif
269312
var ariCertId = item.ARICertificateId ?? Certify.Shared.Core.Utils.PKI.CertUtils.GetARICertIdBase64(x509Cert2);
270313
var info = await provider.GetRenewalInfo(ariCertId);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@ public async Task<ActionResult<ManagedInstanceInfo>> AddHubManagedInstance(Manag
1818

1919
public async Task<ActionResult> UpdateHubManagedInstance(ManagedInstanceInfo item)
2020
{
21-
await _configStore.Update(nameof(ManagedInstanceInfo), item);
21+
var existing = await _configStore.Get<ManagedInstanceInfo>(nameof(ManagedInstanceInfo), item.Id);
22+
23+
existing.OS = item.OS;
24+
existing.OSVersion = item.OSVersion;
25+
26+
existing.ClientName = item.ClientName;
27+
existing.ClientVersion = item.ClientVersion;
28+
29+
existing.Title = item.Title;
30+
existing.Description = item.Description;
31+
32+
existing.DateLastReported = item.DateLastReported;
33+
34+
existing.Tags = item.Tags;
35+
36+
await _configStore.Update(nameof(ManagedInstanceInfo), existing);
37+
2238
return new ActionResult("Updated", true);
2339
}
2440

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.Json;
@@ -18,7 +18,8 @@ namespace Certify.Management
1818
public partial class CertifyManager
1919
{
2020
private IManagementServerClient _managementServerClient;
21-
private bool _isDirectMgmtHubClient = false;
21+
private bool _isDirectMgmtHubBackend = false;
22+
private bool _isMgtmHubBackend = false;
2223
private bool _isHubConnectionErrorLogged = false;
2324
private ClientSecret _mgmtHubJoiningSecret;
2425
private const string _mgmtHubJoiningCredId = "_ManagementHubJoiningKey";
@@ -141,16 +142,45 @@ public async Task<ActionResult<HubJoiningInfo>> CheckManagementHubCredentials(st
141142
}
142143
}
143144

145+
public void EnableManagementHubBackend(bool isDirectHubBackend)
146+
{
147+
_isDirectMgmtHubBackend = isDirectHubBackend;
148+
149+
}
150+
144151
public void SetDirectManagementClient(IManagementServerClient client)
145152
{
146153
_managementServerClient = client;
147-
_isDirectMgmtHubClient = true;
154+
}
155+
156+
public async Task<HubInfo> GetHubInfo()
157+
{
158+
if (_isMgtmHubBackend)
159+
{
160+
var hubInfo = new HubInfo();
161+
162+
hubInfo.InstanceId = _serverConfig.HubAssignedInstanceId;
163+
164+
var versionInfo = Util.GetAppVersion().ToString();
165+
166+
hubInfo.Version = new Models.Hub.VersionInfo
167+
{
168+
Version = versionInfo,
169+
Product = "Certify Management Hub"
170+
};
171+
172+
return hubInfo;
173+
}
174+
else
175+
{
176+
return null;
177+
}
148178
}
149179

150180
private JsonWebTokenHandler _joiningTokenHandler = new JsonWebTokenHandler();
151181
private async Task EnsureMgmtHubConnection()
152182
{
153-
if (!_isDirectMgmtHubClient)
183+
if (!_isDirectMgmtHubBackend)
154184
{
155185
// check we have a current non-expired joining token
156186
if (!string.IsNullOrWhiteSpace(_mgmtHubJoiningToken))
@@ -181,7 +211,7 @@ private async Task EnsureMgmtHubConnection()
181211
var endpoint = string.Empty;
182212
var defaultEnpoint = "api/internal/managementhub";
183213

184-
if (!_isDirectMgmtHubClient)
214+
if (!_isDirectMgmtHubBackend)
185215
{
186216
// construct hub api url and status hub api endpoint
187217
if (Environment.GetEnvironmentVariable("CERTIFY_MANAGEMENT_HUB") != null)

src/Certify.Models/Providers/ICertifyManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public interface ICertifyManager
9292

9393
Task<InstanceCommandResult> PerformHubCommandWithResult(InstanceCommandRequest arg);
9494
void SetDirectManagementClient(IManagementServerClient client);
95+
void EnableManagementHubBackend(bool isDirectHubBackend);
9596
ManagedInstanceInfo GetManagedInstanceInfo();
9697
Task<ActionResult> CheckManagementHubConnectionStatus();
9798

@@ -105,5 +106,7 @@ public interface ICertifyManager
105106
Task<Certify.Models.Config.ActionResult> RemoveHubItemTags(ICollection<string> tagsIds);
106107
Task<ICollection<ItemTag>> GetAllHubItemTags();
107108
Task<ICollection<ItemTag>> GetHubItemTags(string itemId, string itemTypeId);
109+
110+
Task<HubInfo> GetHubInfo();
108111
}
109112
}

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Controllers/ManagedInstanceController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Certify.Management;
22
using Certify.Models.Hub;
3-
using Microsoft.AspNetCore.DataProtection;
43
using Microsoft.AspNetCore.Mvc;
54

65
namespace Certify.Service.Controllers
@@ -10,7 +9,6 @@ namespace Certify.Service.Controllers
109
public class ManagedInstanceController : ControllerBase
1110
{
1211
private ICertifyManager _certifyManager;
13-
private IDataProtectionProvider _dataProtectionProvider;
1412

1513
public ManagedInstanceController(ICertifyManager certifyManager)
1614
{
@@ -24,10 +22,10 @@ public ManagedInstanceController(ICertifyManager certifyManager)
2422
/// <param name="id">Hub assigned instance id</param>
2523
/// <returns></returns>
2624
[HttpGet]
25+
[Route("{id}")]
2726
public async Task<ManagedInstanceInfo> Get(string id)
2827
{
2928
return await _certifyManager.GetHubManagedInstance(id);
30-
3129
}
3230

3331
[HttpPost]
@@ -44,6 +42,7 @@ public async Task<ManagedInstanceInfo> Get(string id)
4442
}
4543

4644
[HttpGet]
45+
[Route("list")]
4746
public async Task<ICollection<ManagedInstanceInfo>> List()
4847
{
4948
return await _certifyManager.GetHubManagedInstances();

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Controllers/SystemController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,8 @@ public async Task<List<ActionStep>> TestDataStore(DataStoreConnection dataStore)
106106

107107
[HttpGet, Route("hub/status")]
108108
public async Task<Models.Config.ActionResult> CheckManagementHubConnectionStatus() => await _certifyManager.CheckManagementHubConnectionStatus();
109+
110+
[HttpGet, Route("hub/info")]
111+
public async Task<HubInfo> GetHubInfo() => await _certifyManager.GetHubInfo();
109112
}
110113
}

0 commit comments

Comments
 (0)