|
| 1 | +using SectigoCertificateManager; |
| 2 | +using SectigoCertificateManager.Clients; |
| 3 | +using SectigoCertificateManager.Requests; |
| 4 | +using System; |
| 5 | +using System.Management.Automation; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +namespace SectigoCertificateManager.PowerShell; |
| 9 | + |
| 10 | +/// <summary>Renews a certificate using an order number.</summary> |
| 11 | +/// <para>Builds an API client and submits a <see cref="RenewCertificateRequest"/> identified by order number.</para> |
| 12 | +/// <list type="alertSet"> |
| 13 | +/// <item> |
| 14 | +/// <term>Network</term> |
| 15 | +/// <description>Contacts the Sectigo API and issues a new certificate for the order.</description> |
| 16 | +/// </item> |
| 17 | +/// </list> |
| 18 | +/// <example> |
| 19 | +/// <summary>Renew by order number</summary> |
| 20 | +/// <prefix>PS> </prefix> |
| 21 | +/// <code>Renew-SectigoCertificate -BaseUrl "https://api.example.com" -Username "user" -Password "pass" -CustomerUri "example" -OrderNumber 10 -Csr "CSR" -DcvMode "Email"</code> |
| 22 | +/// <para>Renews the certificate associated with order 10.</para> |
| 23 | +/// </example> |
| 24 | +/// <example> |
| 25 | +/// <summary>Specify a DCV email</summary> |
| 26 | +/// <prefix>PS> </prefix> |
| 27 | +/// <code>Renew-SectigoCertificate -BaseUrl "https://api.example.com" -Username "user" -Password "pass" -CustomerUri "example" -OrderNumber 10 -Csr "CSR" -DcvMode "Email" -DcvEmail "[email protected]"</code> |
| 28 | +/// <para>Sends the domain control validation to a specific address.</para> |
| 29 | +/// </example> |
| 30 | +/// <seealso href="https://learn.microsoft.com/powershell/scripting/developer/cmdlet/writing-a-cmdlet"/> |
| 31 | +/// <seealso href="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/SectigoCertificateManager/SectigoCertificateManager"/> |
| 32 | +[Cmdlet("Renew", "SectigoCertificate", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)] |
| 33 | +[CmdletBinding()] |
| 34 | +[OutputType(typeof(int))] |
| 35 | +public sealed class RenewSectigoCertificateCommand : PSCmdlet { |
| 36 | + /// <summary>The API base URL.</summary> |
| 37 | + [Parameter(Mandatory = true)] |
| 38 | + public string BaseUrl { get; set; } = string.Empty; |
| 39 | + |
| 40 | + /// <summary>The user name for authentication.</summary> |
| 41 | + [Parameter(Mandatory = true)] |
| 42 | + public string Username { get; set; } = string.Empty; |
| 43 | + |
| 44 | + /// <summary>The password for authentication.</summary> |
| 45 | + [Parameter(Mandatory = true)] |
| 46 | + public string Password { get; set; } = string.Empty; |
| 47 | + |
| 48 | + /// <summary>The customer URI assigned by Sectigo.</summary> |
| 49 | + [Parameter(Mandatory = true)] |
| 50 | + public string CustomerUri { get; set; } = string.Empty; |
| 51 | + |
| 52 | + /// <summary>The API version to use.</summary> |
| 53 | + [Parameter] |
| 54 | + public ApiVersion ApiVersion { get; set; } = ApiVersion.V25_6; |
| 55 | + |
| 56 | + /// <summary>The order number used to identify the certificate.</summary> |
| 57 | + [Parameter(Mandatory = true, Position = 0)] |
| 58 | + public long OrderNumber { get; set; } |
| 59 | + |
| 60 | + /// <summary>The certificate signing request.</summary> |
| 61 | + [Parameter(Mandatory = true)] |
| 62 | + public string Csr { get; set; } = string.Empty; |
| 63 | + |
| 64 | + /// <summary>The domain control validation mode.</summary> |
| 65 | + [Parameter(Mandatory = true)] |
| 66 | + public string DcvMode { get; set; } = string.Empty; |
| 67 | + |
| 68 | + /// <summary>The domain control validation email address.</summary> |
| 69 | + [Parameter] |
| 70 | + public string? DcvEmail { get; set; } |
| 71 | + |
| 72 | + /// <summary>Optional cancellation token.</summary> |
| 73 | + [Parameter] |
| 74 | + public CancellationToken CancellationToken { get; set; } |
| 75 | + |
| 76 | + /// <summary>Renews a certificate using provided parameters.</summary> |
| 77 | + /// <para>Builds an API client and submits a <see cref="RenewCertificateRequest"/>.</para> |
| 78 | + protected override void ProcessRecord() { |
| 79 | + if (OrderNumber <= 0) { |
| 80 | + var ex = new ArgumentOutOfRangeException(nameof(OrderNumber)); |
| 81 | + var record = new ErrorRecord(ex, "InvalidOrderNumber", ErrorCategory.InvalidArgument, OrderNumber); |
| 82 | + ThrowTerminatingError(record); |
| 83 | + } |
| 84 | + |
| 85 | + if (!ShouldProcess($"Order {OrderNumber}", "Renew")) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + var config = new ApiConfig(BaseUrl, Username, Password, CustomerUri, ApiVersion); |
| 90 | + ISectigoClient? client = null; |
| 91 | + try { |
| 92 | + client = TestHooks.ClientFactory?.Invoke(config) ?? new SectigoClient(config); |
| 93 | + TestHooks.CreatedClient = client; |
| 94 | + var certificates = new CertificatesClient(client); |
| 95 | + var request = new RenewCertificateRequest { |
| 96 | + Csr = Csr, |
| 97 | + DcvMode = DcvMode, |
| 98 | + DcvEmail = DcvEmail |
| 99 | + }; |
| 100 | + var newId = certificates.RenewByOrderNumberAsync(OrderNumber, request, CancellationToken) |
| 101 | + .GetAwaiter() |
| 102 | + .GetResult(); |
| 103 | + WriteObject(newId); |
| 104 | + } finally { |
| 105 | + if (client is IDisposable disposable) { |
| 106 | + disposable.Dispose(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments