Skip to content

Commit 1780730

Browse files
Hub: provide method to change API url for mgtm hub instance reports to
1 parent 7e88853 commit 1780730

File tree

10 files changed

+167
-19
lines changed

10 files changed

+167
-19
lines changed

src/Certify.Client/CertifyApiClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.Net.Http.Headers;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Certify.Models.Hub;
109
using Certify.Models;
1110
using Certify.Models.Config;
11+
using Certify.Models.Hub;
1212
using Certify.Models.Reporting;
1313
using Certify.Models.Utils;
1414
using Certify.Shared;
@@ -287,6 +287,12 @@ public async Task<List<ActionResult>> PerformServiceDiagnostics(AuthContext auth
287287
return JsonConvert.DeserializeObject<List<ActionResult>>(result);
288288
}
289289

290+
public async Task<ActionStep> UpdateManagementHub(string url, string joiningKey, AuthContext authContext = null)
291+
{
292+
var result = await PostAsync($"system/hub/update/", new { url, joiningKey }, authContext);
293+
return JsonConvert.DeserializeObject<ActionStep>(await result.Content.ReadAsStringAsync());
294+
}
295+
290296
public async Task<List<ActionStep>> SetDefaultDataStore(string dataStoreId, AuthContext authContext = null)
291297
{
292298
var result = await PostAsync($"system/datastores/setdefault/{dataStoreId}", null, authContext);

src/Certify.Client/ICertifyClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Threading.Tasks;
44
using Certify.Models;
55
using Certify.Models.Config;
6+
using Certify.Models.Hub;
67
using Certify.Models.Reporting;
78
using Certify.Models.Utils;
8-
using Certify.Models.Hub;
99
using Certify.Shared;
1010

1111
namespace Certify.Client
@@ -27,6 +27,8 @@ public partial interface ICertifyInternalApiClient
2727
Task<List<ActionStep>> CopyDataStore(string sourceId, string targetId, AuthContext authContext = null);
2828
Task<List<ActionStep>> UpdateDataStoreConnection(DataStoreConnection dataStoreConnection, AuthContext authContext = null);
2929
Task<List<ActionStep>> TestDataStoreConnection(DataStoreConnection dataStoreConnection, AuthContext authContext = null);
30+
31+
Task<ActionStep> UpdateManagementHub(string url, string joiningKey, AuthContext authContext = null);
3032
#endregion System
3133

3234
#region Server

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Certify.Core.Management.Access;
9-
using Certify.Models.Hub;
109
using Certify.Models;
1110
using Certify.Models.Config;
11+
using Certify.Models.Hub;
1212
using Certify.Models.Shared;
1313

1414
namespace Certify.Management
@@ -36,6 +36,12 @@ private async Task UpgradeSettings()
3636
await PerformServiceUpgrades();
3737

3838
CoreAppSettings.Current.CurrentServiceVersion = systemVersion;
39+
40+
if (Environment.GetEnvironmentVariable("CERTIFY_ENABLE_MANAGEMENT_HUB")?.Equals("true", StringComparison.InvariantCultureIgnoreCase) == true)
41+
{
42+
CoreAppSettings.Current.IsManagementHub = true;
43+
}
44+
3945
SettingsManager.SaveAppSettings();
4046

4147
var accessControl = await GetCurrentAccessControl();

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using System.Text.Json;
55
using System.Threading.Tasks;
66
using Certify.Client;
7-
using Certify.Models.Hub;
7+
using Certify.Locales;
88
using Certify.Models;
99
using Certify.Models.Config;
10+
using Certify.Models.Hub;
1011
using Certify.Shared.Core.Utils;
11-
using System.Runtime.InteropServices;
12-
using Certify.Locales;
1312

1413
namespace Certify.Management
1514
{
@@ -18,6 +17,27 @@ public partial class CertifyManager
1817
private ManagementServerClient _managementServerClient;
1918
private string _managementServerConnectionId = string.Empty;
2019

20+
public async Task<ActionStep> UpdateManagementHub(string url, string joiningKey)
21+
{
22+
23+
_serverConfig = SharedUtils.ServiceConfigManager.GetAppServiceConfig();
24+
_serverConfig.ManagementServerHubUri = url;
25+
SharedUtils.ServiceConfigManager.StoreUpdatedAppServiceConfig(_serverConfig);
26+
27+
_managementServerClient = null;
28+
29+
try
30+
{
31+
await EnsureMgmtHubConnection();
32+
}
33+
catch
34+
{
35+
return new ActionStep("Update Management Hub Failed", "A problem occurred when connecting to the management hub. Check URL.", hasError: true);
36+
}
37+
38+
return new ActionStep("Updated Management Hub", "OK", false);
39+
}
40+
2141
private async Task EnsureMgmtHubConnection()
2242
{
2343
// connect/reconnect to management hub if enabled

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using Certify.Config;
6-
using Certify.Models.Hub;
76
using Certify.Models;
87
using Certify.Models.Config;
98
using Certify.Models.Config.Migration;
9+
using Certify.Models.Hub;
1010
using Certify.Models.Providers;
1111
using Certify.Providers;
1212
using Certify.Shared;
@@ -117,5 +117,6 @@ public interface ICertifyManager
117117
Task<ActionResult> DeleteManagedChallenge(string id);
118118
Task<ActionResult> PerformManagedChallengeRequest(ManagedChallengeRequest request);
119119
Task<ActionResult> CleanupManagedChallengeRequest(ManagedChallengeRequest request);
120+
Task<ActionStep> UpdateManagementHub(string url, string joiningKey);
120121
}
121122
}

src/Certify.Models/Hub/HubInfo.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Certify.Models.Hub
2+
{
3+
public class HubInfo
4+
{
5+
public string InstanceId { get; set; }
6+
7+
public VersionInfo Version { get; set; }
8+
}
9+
}

src/Certify.Server/Certify.Server.Api.Public.Client/Certify.API.Public.cs

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public string BaseUrl
7979
partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response);
8080

8181
/// <summary>
82-
/// Get list of Assigned Roles for a given security principle [Generated by Certify.SourceGenerators]
82+
/// Check a given security principle has permissions to perform a specific action for a specific resource type [Generated by Certify.SourceGenerators]
8383
/// </summary>
8484
/// <returns>OK</returns>
8585
/// <exception cref="ApiException">A server side error occurred.</exception>
@@ -90,7 +90,7 @@ public virtual System.Threading.Tasks.Task<bool> CheckSecurityPrincipleHasAccess
9090

9191
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
9292
/// <summary>
93-
/// Get list of Assigned Roles for a given security principle [Generated by Certify.SourceGenerators]
93+
/// Check a given security principle has permissions to perform a specific action for a specific resource type [Generated by Certify.SourceGenerators]
9494
/// </summary>
9595
/// <returns>OK</returns>
9696
/// <exception cref="ApiException">A server side error occurred.</exception>
@@ -3211,8 +3211,8 @@ public virtual async System.Threading.Tasks.Task<ManagedCertificateSummaryResult
32113211

32123212
var urlBuilder_ = new System.Text.StringBuilder();
32133213
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3214-
// Operation Path: "api/v1/hub/items"
3215-
urlBuilder_.Append("api/v1/hub/items");
3214+
// Operation Path: "internal/v1/hub/items"
3215+
urlBuilder_.Append("internal/v1/hub/items");
32163216
urlBuilder_.Append('?');
32173217
if (instanceId != null)
32183218
{
@@ -3307,8 +3307,8 @@ public virtual async System.Threading.Tasks.Task<ManagedCertificateSummaryResult
33073307

33083308
var urlBuilder_ = new System.Text.StringBuilder();
33093309
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3310-
// Operation Path: "api/v1/hub/instances"
3311-
urlBuilder_.Append("api/v1/hub/instances");
3310+
// Operation Path: "internal/v1/hub/instances"
3311+
urlBuilder_.Append("internal/v1/hub/instances");
33123312

33133313
PrepareRequest(client_, request_, urlBuilder_);
33143314

@@ -3384,8 +3384,8 @@ public virtual async System.Threading.Tasks.Task FlushHubManagedInstancesAsync(S
33843384

33853385
var urlBuilder_ = new System.Text.StringBuilder();
33863386
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3387-
// Operation Path: "api/v1/hub/flush"
3388-
urlBuilder_.Append("api/v1/hub/flush");
3387+
// Operation Path: "internal/v1/hub/flush"
3388+
urlBuilder_.Append("internal/v1/hub/flush");
33893389

33903390
PrepareRequest(client_, request_, urlBuilder_);
33913391

@@ -3434,6 +3434,84 @@ public virtual async System.Threading.Tasks.Task FlushHubManagedInstancesAsync(S
34343434
}
34353435
}
34363436

3437+
/// <returns>OK</returns>
3438+
/// <exception cref="ApiException">A server side error occurred.</exception>
3439+
public virtual System.Threading.Tasks.Task<HubInfo> GetHubInfoAsync()
3440+
{
3441+
return GetHubInfoAsync(System.Threading.CancellationToken.None);
3442+
}
3443+
3444+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
3445+
/// <returns>OK</returns>
3446+
/// <exception cref="ApiException">A server side error occurred.</exception>
3447+
public virtual async System.Threading.Tasks.Task<HubInfo> GetHubInfoAsync(System.Threading.CancellationToken cancellationToken)
3448+
{
3449+
var client_ = _httpClient;
3450+
var disposeClient_ = false;
3451+
try
3452+
{
3453+
using (var request_ = new System.Net.Http.HttpRequestMessage())
3454+
{
3455+
request_.Method = new System.Net.Http.HttpMethod("GET");
3456+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));
3457+
3458+
var urlBuilder_ = new System.Text.StringBuilder();
3459+
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3460+
// Operation Path: "internal/v1/hub/info"
3461+
urlBuilder_.Append("internal/v1/hub/info");
3462+
3463+
PrepareRequest(client_, request_, urlBuilder_);
3464+
3465+
var url_ = urlBuilder_.ToString();
3466+
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
3467+
3468+
PrepareRequest(client_, request_, url_);
3469+
3470+
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
3471+
var disposeResponse_ = true;
3472+
try
3473+
{
3474+
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
3475+
foreach (var item_ in response_.Headers)
3476+
headers_[item_.Key] = item_.Value;
3477+
if (response_.Content != null && response_.Content.Headers != null)
3478+
{
3479+
foreach (var item_ in response_.Content.Headers)
3480+
headers_[item_.Key] = item_.Value;
3481+
}
3482+
3483+
ProcessResponse(client_, response_);
3484+
3485+
var status_ = (int)response_.StatusCode;
3486+
if (status_ == 200)
3487+
{
3488+
var objectResponse_ = await ReadObjectResponseAsync<HubInfo>(response_, headers_, cancellationToken).ConfigureAwait(false);
3489+
if (objectResponse_.Object == null)
3490+
{
3491+
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
3492+
}
3493+
return objectResponse_.Object;
3494+
}
3495+
else
3496+
{
3497+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3498+
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
3499+
}
3500+
}
3501+
finally
3502+
{
3503+
if (disposeResponse_)
3504+
response_.Dispose();
3505+
}
3506+
}
3507+
}
3508+
finally
3509+
{
3510+
if (disposeClient_)
3511+
client_.Dispose();
3512+
}
3513+
}
3514+
34373515
/// <summary>
34383516
/// Request a challenge response
34393517
/// </summary>

src/Certify.Server/Certify.Server.Api.Public/Controllers/internal/HubController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Certify.Server.Api.Public.Controllers
1212
/// Provides managed certificate related operations
1313
/// </summary>
1414
[ApiController]
15-
[Route("api/v1/[controller]")]
15+
[Route("internal/v1/[controller]")]
1616
public partial class HubController : ApiControllerBase
1717
{
1818

@@ -120,5 +120,23 @@ public async Task<IActionResult> FlushHubManagedInstances()
120120
await _mgmtHubContext.Clients.All.SendCommandRequest(new InstanceCommandRequest(ManagementHubCommands.Reconnect));
121121
return new OkResult();
122122
}
123+
124+
[HttpGet]
125+
[Route("info")]
126+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(HubInfo))]
127+
public async Task<IActionResult> GetHubInfo()
128+
{
129+
var hubInfo = new HubInfo();
130+
131+
var hubprefs = await _client.GetPreferences();
132+
133+
hubInfo.InstanceId = hubprefs.InstanceId;
134+
135+
var versionInfo = await _client.GetAppVersion();
136+
137+
hubInfo.Version = new Models.Hub.VersionInfo { Version = versionInfo, Product = "Certify Management Hub" };
138+
139+
return new OkObjectResult(hubInfo);
140+
}
123141
}
124142
}

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
@@ -96,5 +96,8 @@ public async Task<List<ActionStep>> TestDataStore(DataStoreConnection dataStore)
9696

9797
[HttpPost, Route("datastores/delete")]
9898
public async Task<List<ActionStep>> RemoveDataStore(string dataStoreId) => await _certifyManager.RemoveDataStoreConnection(dataStoreId);
99+
100+
[HttpPost, Route("hub/update")]
101+
public async Task<ActionStep> UpdateManagementHub(string url, string joiningKey) => await _certifyManager.UpdateManagementHub(url, joiningKey);
99102
}
100103
}

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Properties/launchSettings.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development",
1818
"ASPNETCORE_URLS": "http://127.0.0.2:9695",
19-
"CERTIFY_MANAGEMENT_HUB": "https://localhost:44361/api/internal/managementhub"
19+
"CERTIFY_MANAGEMENT_HUB": "https://localhost:44361/api/internal/managementhub",
20+
"CERTIFY_ENABLE_MANAGEMENT_HUB": "true"
2021
}
2122
},
2223
"IIS Express": {
@@ -35,7 +36,8 @@
3536
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/docs",
3637
"environmentVariables": {
3738
"ASPNETCORE_ENVIRONMENT": "Development",
38-
"ASPNETCORE_URLS": "http://0.0.0.0:9695"
39+
"ASPNETCORE_URLS": "http://0.0.0.0:9695",
40+
"CERTIFY_ENABLE_MANAGEMENT_HUB": "true"
3941
},
4042
"publishAllPorts": true,
4143
"httpPort": 9695
@@ -45,7 +47,10 @@
4547
"launchBrowser": false,
4648
"launchUrl": "http://localhost:9695/docs",
4749
"environmentVariables": {
48-
"ASPNETCORE_ENVIRONMENT": "Development"
50+
"ASPNETCORE_ENVIRONMENT": "Development",
51+
"ASPNETCORE_URLS": "http://0.0.0.0:9695",
52+
"CERTIFY_ENABLE_MANAGEMENT_HUB": "true",
53+
"CERTIFY_MANAGEMENT_HUB": "https://localhost:44361/api/internal/managementhub"
4954
}
5055
}
5156
}

0 commit comments

Comments
 (0)