Skip to content

Commit 0c32cf9

Browse files
API: per instance deployment task edit/execute, refactor
1 parent 9460e10 commit 0c32cf9

File tree

9 files changed

+302
-101
lines changed

9 files changed

+302
-101
lines changed

src/Certify.Client/CertifyApiClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ public async Task<List<CertificateAuthority>> GetCertificateAuthorities(AuthCont
620620
return JsonConvert.DeserializeObject<List<CertificateAuthority>>(result);
621621
}
622622

623+
public async Task<ActionResult> UpdateCertificateAuthority(CertificateAuthority ca, AuthContext authContext = null)
624+
{
625+
var result = await PostAsync("accounts/authorities", ca, authContext);
626+
return JsonConvert.DeserializeObject<ActionResult>(await result.Content.ReadAsStringAsync());
627+
}
628+
623629
public async Task<ActionResult> DeleteCertificateAuthority(string id, AuthContext authContext = null)
624630
{
625631
var result = await DeleteAsync("accounts/authorities/" + id, authContext);

src/Certify.Client/ICertifyClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public partial interface ICertifyInternalApiClient
116116

117117
#region Accounts
118118
Task<List<CertificateAuthority>> GetCertificateAuthorities(AuthContext authContext = null);
119+
120+
Task<ActionResult> UpdateCertificateAuthority(CertificateAuthority ca, AuthContext authContext = null);
119121
Task<ActionResult> DeleteCertificateAuthority(string id, AuthContext authContext = null);
120122
Task<List<AccountDetails>> GetAccounts(AuthContext authContext = null);
121123
Task<ActionResult> AddAccount(ContactRegistration contact, AuthContext authContext = null);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ private async Task<InstanceCommandResult> _managementServerClient_OnGetCommandRe
175175

176176
val = await UpdateCertificateAuthority(item);
177177
}
178+
else if (arg.CommandType == ManagementHubCommands.RemoveCertificateAuthority)
179+
{
180+
var args = JsonSerializer.Deserialize<KeyValuePair<string, string>[]>(arg.Value);
181+
var itemArg = args.FirstOrDefault(a => a.Key == "id");
182+
val = await RemoveCertificateAuthority(itemArg.Value);
183+
}
178184
else if (arg.CommandType == ManagementHubCommands.GetAcmeAccounts)
179185
{
180186
val = await GetAccountRegistrations();
@@ -187,6 +193,13 @@ private async Task<InstanceCommandResult> _managementServerClient_OnGetCommandRe
187193

188194
val = await AddAccount(registration);
189195
}
196+
else if (arg.CommandType == ManagementHubCommands.RemoveAcmeAccount)
197+
{
198+
var args = JsonSerializer.Deserialize<KeyValuePair<string, string>[]>(arg.Value);
199+
var itemArg = args.FirstOrDefault(a => a.Key == "storageKey");
200+
var deactivateArg = args.FirstOrDefault(a => a.Key == "deactivate");
201+
val = await RemoveAccount(itemArg.Value, bool.Parse(deactivateArg.Value));
202+
}
190203
else if (arg.CommandType == ManagementHubCommands.GetStoredCredentials)
191204
{
192205
val = await _credentialsManager.GetCredentials();
@@ -209,6 +222,7 @@ private async Task<InstanceCommandResult> _managementServerClient_OnGetCommandRe
209222
{
210223
val = await Core.Management.Challenges.ChallengeProviders.GetChallengeAPIProviders();
211224
}
225+
212226
else if (arg.CommandType == ManagementHubCommands.GetDnsZones)
213227
{
214228
var args = JsonSerializer.Deserialize<KeyValuePair<string, string>[]>(arg.Value);
@@ -217,6 +231,19 @@ private async Task<InstanceCommandResult> _managementServerClient_OnGetCommandRe
217231

218232
val = await GetDnsProviderZones(providerTypeArg.Value, credentialIdArg.Value);
219233
}
234+
else if (arg.CommandType == ManagementHubCommands.GetDeploymentProviders)
235+
{
236+
val = await GetDeploymentProviders();
237+
}
238+
else if (arg.CommandType == ManagementHubCommands.ExecuteDeploymentTask)
239+
{
240+
var args = JsonSerializer.Deserialize<KeyValuePair<string, string>[]>(arg.Value);
241+
242+
var managedCertificateIdArg = args.FirstOrDefault(a => a.Key == "managedCertificateId");
243+
var taskIdArg = args.FirstOrDefault(a => a.Key == "taskId");
244+
245+
val = await PerformDeploymentTask(null, managedCertificateIdArg.Value, taskIdArg.Value, isPreviewOnly: false, skipDeferredTasks: false, forceTaskExecution: false);
246+
}
220247
else if (arg.CommandType == ManagementHubCommands.Reconnect)
221248
{
222249
await _managementServerClient.Disconnect();

src/Certify.Models/API/Management/ManagementHubMessages.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ManagementHubCommands
3030

3131
public const string GetAcmeAccounts = "GetAcmeAccounts";
3232
public const string AddAcmeAccount = "AddAcmeAccount";
33+
public const string RemoveAcmeAccount = "RemoveAcmeAccount";
3334

3435
public const string GetStoredCredentials = "GetStoredCredentials";
3536
public const string UpdateStoredCredential = "UpdateStoredCredential";
@@ -38,6 +39,9 @@ public class ManagementHubCommands
3839
public const string GetChallengeProviders = "GetChallengeProviders";
3940
public const string GetDnsZones = "GetDnsZones";
4041

42+
public const string GetDeploymentProviders = "GetDeploymentProviders";
43+
public const string ExecuteDeploymentTask = "ExecuteDeploymentTask";
44+
4145
public const string Reconnect = "Reconnect";
4246
}
4347

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

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,23 +2895,26 @@ public virtual async System.Threading.Tasks.Task<ActionResult> RemoveAcmeAccount
28952895
}
28962896

28972897
/// <summary>
2898-
/// Get List of supported deployment tasks
2898+
/// Get Deployment Task Providers [Generated by Certify.SourceGenerators]
28992899
/// </summary>
29002900
/// <returns>OK</returns>
29012901
/// <exception cref="ApiException">A server side error occurred.</exception>
2902-
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DeploymentProviderDefinition>> GetDeploymentProvidersAsync()
2902+
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DeploymentProviderDefinition>> GetDeploymentProvidersAsync(string instanceId)
29032903
{
2904-
return GetDeploymentProvidersAsync(System.Threading.CancellationToken.None);
2904+
return GetDeploymentProvidersAsync(instanceId, System.Threading.CancellationToken.None);
29052905
}
29062906

29072907
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
29082908
/// <summary>
2909-
/// Get List of supported deployment tasks
2909+
/// Get Deployment Task Providers [Generated by Certify.SourceGenerators]
29102910
/// </summary>
29112911
/// <returns>OK</returns>
29122912
/// <exception cref="ApiException">A server side error occurred.</exception>
2913-
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DeploymentProviderDefinition>> GetDeploymentProvidersAsync(System.Threading.CancellationToken cancellationToken)
2913+
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DeploymentProviderDefinition>> GetDeploymentProvidersAsync(string instanceId, System.Threading.CancellationToken cancellationToken)
29142914
{
2915+
if (instanceId == null)
2916+
throw new System.ArgumentNullException("instanceId");
2917+
29152918
var client_ = _httpClient;
29162919
var disposeClient_ = false;
29172920
try
@@ -2923,8 +2926,9 @@ public virtual async System.Threading.Tasks.Task<ActionResult> RemoveAcmeAccount
29232926

29242927
var urlBuilder_ = new System.Text.StringBuilder();
29252928
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
2926-
// Operation Path: "internal/v1/deploymenttask/providers"
2927-
urlBuilder_.Append("internal/v1/deploymenttask/providers");
2929+
// Operation Path: "internal/v1/deploymenttask/{instanceId}"
2930+
urlBuilder_.Append("internal/v1/deploymenttask/");
2931+
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(instanceId, System.Globalization.CultureInfo.InvariantCulture)));
29282932

29292933
PrepareRequest(client_, request_, urlBuilder_);
29302934

@@ -2978,6 +2982,104 @@ public virtual async System.Threading.Tasks.Task<ActionResult> RemoveAcmeAccount
29782982
}
29792983
}
29802984

2985+
/// <summary>
2986+
/// Execute Deployment Task [Generated by Certify.SourceGenerators]
2987+
/// </summary>
2988+
/// <returns>OK</returns>
2989+
/// <exception cref="ApiException">A server side error occurred.</exception>
2990+
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<ActionStep>> ExecuteDeploymentTaskAsync(string instanceId, string managedCertificateId, string taskId)
2991+
{
2992+
return ExecuteDeploymentTaskAsync(instanceId, managedCertificateId, taskId, System.Threading.CancellationToken.None);
2993+
}
2994+
2995+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
2996+
/// <summary>
2997+
/// Execute Deployment Task [Generated by Certify.SourceGenerators]
2998+
/// </summary>
2999+
/// <returns>OK</returns>
3000+
/// <exception cref="ApiException">A server side error occurred.</exception>
3001+
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<ActionStep>> ExecuteDeploymentTaskAsync(string instanceId, string managedCertificateId, string taskId, System.Threading.CancellationToken cancellationToken)
3002+
{
3003+
if (instanceId == null)
3004+
throw new System.ArgumentNullException("instanceId");
3005+
3006+
if (managedCertificateId == null)
3007+
throw new System.ArgumentNullException("managedCertificateId");
3008+
3009+
if (taskId == null)
3010+
throw new System.ArgumentNullException("taskId");
3011+
3012+
var client_ = _httpClient;
3013+
var disposeClient_ = false;
3014+
try
3015+
{
3016+
using (var request_ = new System.Net.Http.HttpRequestMessage())
3017+
{
3018+
request_.Method = new System.Net.Http.HttpMethod("GET");
3019+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));
3020+
3021+
var urlBuilder_ = new System.Text.StringBuilder();
3022+
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3023+
// Operation Path: "internal/v1/deploymenttask/{instanceId}/execute/{managedCertificateId}/{taskId}"
3024+
urlBuilder_.Append("internal/v1/deploymenttask/");
3025+
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(instanceId, System.Globalization.CultureInfo.InvariantCulture)));
3026+
urlBuilder_.Append("/execute/");
3027+
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(managedCertificateId, System.Globalization.CultureInfo.InvariantCulture)));
3028+
urlBuilder_.Append('/');
3029+
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(taskId, System.Globalization.CultureInfo.InvariantCulture)));
3030+
3031+
PrepareRequest(client_, request_, urlBuilder_);
3032+
3033+
var url_ = urlBuilder_.ToString();
3034+
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
3035+
3036+
PrepareRequest(client_, request_, url_);
3037+
3038+
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
3039+
var disposeResponse_ = true;
3040+
try
3041+
{
3042+
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
3043+
foreach (var item_ in response_.Headers)
3044+
headers_[item_.Key] = item_.Value;
3045+
if (response_.Content != null && response_.Content.Headers != null)
3046+
{
3047+
foreach (var item_ in response_.Content.Headers)
3048+
headers_[item_.Key] = item_.Value;
3049+
}
3050+
3051+
ProcessResponse(client_, response_);
3052+
3053+
var status_ = (int)response_.StatusCode;
3054+
if (status_ == 200)
3055+
{
3056+
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<ActionStep>>(response_, headers_, cancellationToken).ConfigureAwait(false);
3057+
if (objectResponse_.Object == null)
3058+
{
3059+
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
3060+
}
3061+
return objectResponse_.Object;
3062+
}
3063+
else
3064+
{
3065+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3066+
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
3067+
}
3068+
}
3069+
finally
3070+
{
3071+
if (disposeResponse_)
3072+
response_.Dispose();
3073+
}
3074+
}
3075+
}
3076+
finally
3077+
{
3078+
if (disposeClient_)
3079+
client_.Dispose();
3080+
}
3081+
}
3082+
29813083
/// <summary>
29823084
/// Get all managed certificates matching criteria
29833085
/// </summary>
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Certify.Client;
2+
using Certify.Server.Api.Public.Services;
23
using Microsoft.AspNetCore.Authentication.JwtBearer;
34
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
@@ -16,31 +17,18 @@ public partial class DeploymentTaskController : ApiControllerBase
1617
private readonly ILogger<DeploymentTaskController> _logger;
1718

1819
private readonly ICertifyInternalApiClient _client;
20+
private readonly ManagementAPI _mgmtAPI;
1921

2022
/// <summary>
2123
/// Constructor
2224
/// </summary>
2325
/// <param name="logger"></param>
2426
/// <param name="client"></param>
25-
public DeploymentTaskController(ILogger<DeploymentTaskController> logger, ICertifyInternalApiClient client)
27+
public DeploymentTaskController(ILogger<DeploymentTaskController> logger, ICertifyInternalApiClient client, ManagementAPI mgmtAPI)
2628
{
2729
_logger = logger;
2830
_client = client;
29-
}
30-
31-
/// <summary>
32-
/// Get List of supported deployment tasks
33-
/// </summary>
34-
/// <returns></returns>
35-
[HttpGet]
36-
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
37-
38-
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<Models.Config.DeploymentProviderDefinition>))]
39-
[Route("providers")]
40-
public async Task<IActionResult> GetDeploymentProviders()
41-
{
42-
var list = await _client.GetDeploymentProviderList();
43-
return new OkObjectResult(list);
31+
_mgmtAPI = mgmtAPI;
4432
}
4533
}
4634
}

src/Certify.Server/Certify.Server.Api.Public/Services/ManagementAPI.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
197197
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.UpdateCertificateAuthority);
198198
}
199199

200+
public async Task<ActionResult?> RemoveCertificateAuthority(string instanceId, string id, AuthContext? currentAuthContext)
201+
{
202+
var args = new KeyValuePair<string, string>[] {
203+
new("instanceId", instanceId),
204+
new("id", id)
205+
};
206+
207+
return await PerformInstanceCommandTaskWithResult<ActionResult?>(id, args, ManagementHubCommands.RemoveCertificateAuthority);
208+
}
209+
200210
public async Task<ICollection<Models.AccountDetails>?> GetAcmeAccounts(string instanceId, AuthContext? currentAuthContext)
201211
{
202212
var args = new KeyValuePair<string, string>[] {
@@ -215,14 +225,41 @@ public async Task<StatusSummary> GetManagedCertificateSummary(AuthContext? curre
215225

216226
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.AddAcmeAccount);
217227
}
228+
public async Task<ActionResult?> RemoveAcmeAccount(string instanceId, string storageKey, bool deactivate, AuthContext? currentAuthContext)
229+
{
230+
var args = new KeyValuePair<string, string>[] {
231+
new("instanceId", instanceId),
232+
new("storageKey", storageKey),
233+
new("deactivate", deactivate.ToString())
234+
};
218235

236+
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.RemoveAcmeAccount);
237+
}
219238
public async Task<ICollection<ChallengeProviderDefinition>?> GetChallengeProviders(string instanceId, AuthContext? currentAuthContext)
220239
{
221240
var args = new KeyValuePair<string, string>[] {
222241
new("instanceId", instanceId)
223242
};
224243
return await PerformInstanceCommandTaskWithResult<ICollection<ChallengeProviderDefinition>>(instanceId, args, ManagementHubCommands.GetChallengeProviders);
225244
}
245+
public async Task<ICollection<DeploymentProviderDefinition>?> GetDeploymentProviders(string instanceId, AuthContext? currentAuthContext)
246+
{
247+
var args = new KeyValuePair<string, string>[] {
248+
new("instanceId", instanceId)
249+
};
250+
return await PerformInstanceCommandTaskWithResult<ICollection<DeploymentProviderDefinition>>(instanceId, args, ManagementHubCommands.GetDeploymentProviders);
251+
}
252+
253+
public async Task<ICollection<ActionStep>?> ExecuteDeploymentTask(string instanceId, string managedCertificateId, string taskId, AuthContext? currentAuthContext)
254+
{
255+
var args = new KeyValuePair<string, string>[] {
256+
new("instanceId", instanceId),
257+
new("managedCertificateId", managedCertificateId),
258+
new("taskId", taskId)
259+
};
260+
261+
return await PerformInstanceCommandTaskWithResult<ICollection<ActionStep>>(instanceId, args, ManagementHubCommands.ExecuteDeploymentTask);
262+
}
226263

227264
public async Task<ICollection<Models.Providers.DnsZone>?> GetDnsZones(string instanceId, string providerTypeId, string credentialId, AuthContext? currentAuthContext)
228265
{

0 commit comments

Comments
 (0)