Skip to content

Commit

Permalink
Hub: simplify and standardize command invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
webprofusion-chrisc committed Nov 1, 2024
1 parent fd7bf7c commit 1153096
Showing 1 changed file with 80 additions and 172 deletions.
252 changes: 80 additions & 172 deletions src/Certify.Server/Certify.Server.Api.Public/Services/ManagementAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Certify.Models;
using Certify.Models.API;
using Certify.Models.Config;
using Certify.Models.Providers;
using Certify.Models.Reporting;
using Certify.Server.Api.Public.SignalR.ManagementHub;
using Microsoft.AspNetCore.SignalR;
Expand Down Expand Up @@ -32,6 +33,52 @@ public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubCon
_backendAPIClient = backendAPIClient;
}

private async Task<InstanceCommandResult?> GetCommandResult(string instanceId, InstanceCommandRequest cmd)
{
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);

if (connectionId == null)
{
throw new Exception("Instance connection info not known, cannot send commands to instance.");
}

_mgmtStateProvider.AddAwaitedCommandRequest(cmd);

await _mgmtHubContext.Clients.Client(connectionId).SendCommandRequest(cmd);

return await _mgmtStateProvider.ConsumeAwaitedCommandResult(cmd.CommandId);
}

private async Task SendCommandWithNoResult(string instanceId, InstanceCommandRequest cmd)
{
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);

if (connectionId == null)
{
throw new Exception("Instance connection info not known, cannot send commands to instance.");
}

_mgmtStateProvider.AddAwaitedCommandRequest(cmd);

await _mgmtHubContext.Clients.Client(connectionId).SendCommandRequest(cmd);
}

private async Task<T?> PerformInstanceCommandTaskWithResult<T>(string instanceId, KeyValuePair<string, string>[] args, string commandType)
{
var cmd = new InstanceCommandRequest(commandType, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<T>(result.Value);
}
else
{
return default;
}
}

/// <summary>
/// Fetch managed cert details from the target instance
/// </summary>
Expand All @@ -43,24 +90,12 @@ public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubCon
{
// get managed cert via local api or via management hub

var args = new KeyValuePair
<string, string>[] {
var args = new KeyValuePair<string, string>[] {
new("instanceId", instanceId) ,
new("managedCertId", managedCertId)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetManagedItem, args);
var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ManagedCertificate>(result.Value);

}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ManagedCertificate?>(instanceId, args, ManagementHubCommands.GetManagedItem);
}

/// <summary>
Expand All @@ -79,21 +114,14 @@ public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubCon
new("managedCert", JsonSerializer.Serialize(managedCert))
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.UpdateManagedItem, args);
var result = await PerformInstanceCommandTaskWithResult<ManagedCertificate?>(instanceId, args, ManagementHubCommands.UpdateManagedItem);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
if (result != null)
{
var update = JsonSerializer.Deserialize<ManagedCertificate>(result.Value);

_mgmtStateProvider.UpdateCachedManagedInstanceItem(instanceId, update);
return update;
}
else
{
return null;
_mgmtStateProvider.UpdateCachedManagedInstanceItem(instanceId, result);
}

return result;
}

/// <summary>
Expand All @@ -112,49 +140,20 @@ public async Task<bool> RemoveManagedCertificate(string instanceId, string manag
new("managedCertId",managedCertId)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.DeleteManagedItem, args);

var result = await GetCommandResult(instanceId, cmd);
var deletedOK = await PerformInstanceCommandTaskWithResult<bool>(instanceId, args, ManagementHubCommands.DeleteManagedItem);

try
{
_mgmtStateProvider.DeleteCachedManagedInstanceItem(instanceId, managedCertId);
return true;
}
catch
if (deletedOK)
{
return false;
}
}

private async Task<InstanceCommandResult?> GetCommandResult(string instanceId, InstanceCommandRequest cmd)
{
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);

if (connectionId == null)
{
throw new Exception("Instance connection info not known, cannot send commands to instance.");
}

_mgmtStateProvider.AddAwaitedCommandRequest(cmd);

await _mgmtHubContext.Clients.Client(connectionId).SendCommandRequest(cmd);

return await _mgmtStateProvider.ConsumeAwaitedCommandResult(cmd.CommandId);
}

private async Task SendCommandWithNoResult(string instanceId, InstanceCommandRequest cmd)
{
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);

if (connectionId == null)
{
throw new Exception("Instance connection info not known, cannot send commands to instance.");
try
{
_mgmtStateProvider.DeleteCachedManagedInstanceItem(instanceId, managedCertId);
}
catch
{
}
}

_mgmtStateProvider.AddAwaitedCommandRequest(cmd);

await _mgmtHubContext.Clients.Client(connectionId).SendCommandRequest(cmd);
return deletedOK;
}

public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? currentAuthContext)
Expand Down Expand Up @@ -186,18 +185,7 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("instanceId", instanceId)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetAcmeAccounts, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ICollection<Models.AccountDetails>>(result.Value);
}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ICollection<Models.AccountDetails>>(instanceId, args, ManagementHubCommands.GetAcmeAccounts);
}

public async Task<ActionResult?> AddAcmeAccount(string instanceId, ContactRegistration registration, AuthContext? currentAuthContext)
Expand All @@ -207,18 +195,15 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("registration", JsonSerializer.Serialize(registration))
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.AddAcmeAccount, args);

var result = await GetCommandResult(instanceId, cmd);
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.AddAcmeAccount);
}

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ActionResult>(result.Value);
}
else
{
return null;
}
public async Task<ICollection<ChallengeProviderDefinition>?> GetChallengeProviders(string instanceId, AuthContext? currentAuthContext)
{
var args = new KeyValuePair<string, string>[] {
new("instanceId", instanceId)
};
return await PerformInstanceCommandTaskWithResult<ICollection<ChallengeProviderDefinition>>(instanceId, args, ManagementHubCommands.GetChallengeProviders);
}

public async Task<ICollection<Models.Providers.DnsZone>?> GetDnsZones(string instanceId, string providerTypeId, string credentialsId, AuthContext? currentAuthContext)
Expand All @@ -229,18 +214,7 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("credentialsId", credentialsId)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetDnsZones, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ICollection<Models.Providers.DnsZone>>(result.Value);
}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ICollection<DnsZone>>(instanceId, args, ManagementHubCommands.GetDnsZones);
}

public async Task<ICollection<Models.Config.StoredCredential>?> GetStoredCredentials(string instanceId, AuthContext? currentAuthContext)
Expand All @@ -249,18 +223,7 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("instanceId", instanceId)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetStoredCredentials, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ICollection<Models.Config.StoredCredential>>(result.Value);
}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ICollection<StoredCredential>>(instanceId, args, ManagementHubCommands.GetStoredCredentials);
}

public async Task<ActionResult?> UpdateStoredCredential(string instanceId, StoredCredential item, AuthContext? currentAuthContext)
Expand All @@ -270,18 +233,7 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("item", JsonSerializer.Serialize(item))
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.UpdateStoredCredential, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ActionResult>(result.Value);
}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.UpdateStoredCredential);
}

public async Task<ActionResult?> DeleteStoredCredential(string instanceId, string storageKey, AuthContext authContext)
Expand All @@ -293,18 +245,7 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
new("storageKey",storageKey)
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.DeleteStoredCredential, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<ActionResult>(result.Value);
}
else
{
return null;
}
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.DeleteStoredCredential);
}

public async Task<LogItem[]> GetItemLog(string instanceId, string managedCertId, int maxLines, AuthContext? currentAuthContext)
Expand All @@ -315,18 +256,7 @@ public async Task<LogItem[]> GetItemLog(string instanceId, string managedCertId,
new("limit",maxLines.ToString())
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetManagedItemLog, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<LogItem[]>(result.Value);
}
else
{
return [];
}
return await PerformInstanceCommandTaskWithResult<LogItem[]>(instanceId, args, ManagementHubCommands.GetManagedItemLog) ?? [];
}

internal async Task<List<StatusMessage>> TestManagedCertificateConfiguration(string instanceId, ManagedCertificate managedCert, AuthContext? currentAuthContext)
Expand All @@ -336,18 +266,7 @@ internal async Task<List<StatusMessage>> TestManagedCertificateConfiguration(str
new("managedCert",JsonSerializer.Serialize(managedCert))
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.TestManagedItemConfiguration, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<List<StatusMessage>>(result.Value);
}
else
{
return [];
}
return await PerformInstanceCommandTaskWithResult<List<StatusMessage>>(instanceId, args, ManagementHubCommands.TestManagedItemConfiguration) ?? [];
}

internal async Task<List<ActionStep>> GetPreviewActions(string instanceId, ManagedCertificate managedCert, AuthContext? currentAuthContext)
Expand All @@ -357,18 +276,7 @@ internal async Task<List<ActionStep>> GetPreviewActions(string instanceId, Manag
new("managedCert",JsonSerializer.Serialize(managedCert))
};

var cmd = new InstanceCommandRequest(ManagementHubCommands.GetManagedItemRenewalPreview, args);

var result = await GetCommandResult(instanceId, cmd);

if (result?.Value != null)
{
return JsonSerializer.Deserialize<List<ActionStep>>(result.Value);
}
else
{
return [];
}
return await PerformInstanceCommandTaskWithResult<List<ActionStep>>(instanceId, args, ManagementHubCommands.GetManagedItemRenewalPreview) ?? [];
}

internal async Task PerformManagedCertificateRequest(string instanceId, string managedCertId, AuthContext? currentAuthContext)
Expand Down

0 comments on commit 1153096

Please sign in to comment.