Skip to content

Commit c2cdf88

Browse files
notyashhhYash Patil
andauthored
[KeyVault] Handle Nullable Parameters for Certificate Auto-Renewal in Set-AzKeyVaultCertificatePolicy (#25844)
* Added manual validation for piped attributes * Updated ChangeLog * Validate using a helper function * Added back ValueFromPipeline Testing * Removed ValueFromPipelineProperty * Added Suggested Changes * Dummy Commit * Added Pester Testing * Added CI error suppression * Updated Help Docs for cmdlet --------- Co-authored-by: Yash Patil <[email protected]>
1 parent 72b1db9 commit c2cdf88

File tree

7 files changed

+130
-9
lines changed

7 files changed

+130
-9
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# CertificatePolicy.Tests.ps1
2+
3+
BeforeAll {
4+
$vaultName = 'yash-kv'
5+
. "$PSScriptRoot\..\Scripts\Common.ps1" # Common setup script
6+
7+
$psd1Path = Join-Path $PSScriptRoot "../../../../artifacts/Debug/" -Resolve
8+
$keyVaultPsd1 = Join-Path $psd1Path "./Az.KeyVault/Az.KeyVault.psd1" -Resolve
9+
Import-Module $keyVaultPsd1 -Force
10+
}
11+
12+
Describe "Set-AzKeyVaultCertificatePolicy Null Handling" {
13+
Context "When setting null for RenewAtNumberOfDaysBeforeExpiry and RenewAtPercentageLifetime" {
14+
15+
It "Should not throw an error when setting null values" {
16+
17+
# Arrange: Generate a random certificate name
18+
$certName = Get-CertificateName -suffix (Get-Random)
19+
20+
# Retrieve Key Vault & Certificate
21+
$KV = Get-AzKeyVault -VaultName $vaultName
22+
$cert = $KV | Get-AzKeyVaultCertificate -Name $certName
23+
24+
if ($cert -eq $null) {
25+
# Create a certificate if it doesn't exist
26+
$policy = New-AzKeyVaultCertificatePolicy `
27+
-SubjectName "CN=$certName" `
28+
-IssuerName "Self" `
29+
-ValidityInMonths 12
30+
31+
$cert = Add-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -CertificatePolicy $policy
32+
}
33+
34+
# Retrieve Cert & Certificate Policy
35+
$cert = $KV | Get-AzKeyVaultCertificate -Name $certName
36+
$policy = $cert | Get-AzKeyVaultCertificatePolicy
37+
38+
# Act: Set null for RenewAtPercentageLifetime and some value for RenewAtNumberOfDaysBeforeExpiry
39+
$policy.RenewAtNumberOfDaysBeforeExpiry = 25
40+
$policy.RenewAtPercentageLifetime = $null
41+
42+
# Apply policy and verify no errors
43+
$policy | Set-AzKeyVaultCertificatePolicy -VaultName $vaultName -Name $certName
44+
45+
# Retrieve updated policy
46+
$updatedCert = $KV | Get-AzKeyVaultCertificate -Name $certName
47+
$updatedPolicy = $updatedCert | Get-AzKeyVaultCertificatePolicy
48+
49+
# Clean up the created resources
50+
Remove-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -Force
51+
52+
# Assert: Check if the properties have been set to null
53+
$updatedPolicy.RenewAtNumberOfDaysBeforeExpiry | Should -Be 25
54+
$updatedPolicy.RenewAtPercentageLifetime | Should -Be $null
55+
}
56+
}
57+
}
58+

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed a parameter validation issue in Set-AzureKeyVaultCertificatePolicy. [#25649]
2122

2223
## Version 6.1.0
2324
* Fixed secrets exposure in example documentation.

src/KeyVault/KeyVault/Commands/Certificate/Policy/SetAzureKeyVaultCertificatePolicy.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Management.Automation;
2121
using System.Security.Cryptography;
2222
using System.Security.Cryptography.X509Certificates;
23+
using Microsoft.Azure.Commands.Common.Exceptions;
2324
using PSKeyVaultProperties = Microsoft.Azure.Commands.KeyVault.Properties;
2425

2526
namespace Microsoft.Azure.Commands.KeyVault
@@ -81,7 +82,6 @@ public class SetAzureKeyVaultCertificatePolicy : KeyVaultCmdletBase
8182
[Parameter(Mandatory = true,
8283
ParameterSetName = ExpandedRenewNumberParameterSet,
8384
HelpMessage = "Specifies the number of days before expiration when automatic renewal should start.")]
84-
[ValidateRange(1, int.MaxValue)]
8585
public int? RenewAtNumberOfDaysBeforeExpiry { get; set; }
8686

8787
/// <summary>
@@ -90,7 +90,6 @@ public class SetAzureKeyVaultCertificatePolicy : KeyVaultCmdletBase
9090
[Parameter(Mandatory = false,
9191
ParameterSetName = ExpandedRenewPercentageParameterSet,
9292
HelpMessage = "Specifies the percentage of the lifetime after which the automatic process for the certificate renewal begins.")]
93-
[ValidateRange(0, 99)]
9493
public int? RenewAtPercentageLifetime { get; set; }
9594

9695
/// <summary>
@@ -231,7 +230,6 @@ public class SetAzureKeyVaultCertificatePolicy : KeyVaultCmdletBase
231230
/// Key size
232231
/// </summary>
233232
[Parameter(Mandatory = false,
234-
ValueFromPipelineByPropertyName = true,
235233
HelpMessage = "Specifies the key size of the certificate. Default is 2048.")]
236234
[ValidateSet("2048", "3072", "4096", "256", "384", "521")]
237235
public int KeySize { get; set; }
@@ -247,6 +245,9 @@ public class SetAzureKeyVaultCertificatePolicy : KeyVaultCmdletBase
247245
HelpMessage = "Specifies whether the key is not exportable.")]
248246
public SwitchParameter KeyNotExportable { get; set; }
249247

248+
/// <summary>
249+
/// CertificateTransparency
250+
/// </summary>
250251
[Parameter(ValueFromPipelineByPropertyName = false,
251252
HelpMessage = "Indicates whether certificate transparency is enabled for this certificate/issuer; if not specified, the default is 'true'")]
252253
public bool? CertificateTransparency { get; set; }
@@ -261,7 +262,6 @@ public class SetAzureKeyVaultCertificatePolicy : KeyVaultCmdletBase
261262
/// Elliptic Curve Name of the key
262263
/// </summary>
263264
[Parameter(Mandatory = false,
264-
ValueFromPipelineByPropertyName = true,
265265
HelpMessage = "Specifies the elliptic curve name of the key of the ECC certificate.")]
266266
[ValidateSet(Constants.P256, Constants.P384, Constants.P521, Constants.P256K, Constants.SECP256K1)]
267267
public string Curve { get; set; }
@@ -271,6 +271,8 @@ public override void ExecuteCmdlet()
271271
{
272272
if (ShouldProcess(Name, Properties.Resources.SetCertificatePolicy))
273273
{
274+
275+
ValidateArguments();
274276
PSKeyVaultCertificatePolicy policy = new PSKeyVaultCertificatePolicy();
275277

276278
switch (ParameterSetName)
@@ -313,5 +315,21 @@ public override void ExecuteCmdlet()
313315
}
314316
}
315317
}
318+
319+
320+
private void ValidateArguments()
321+
{
322+
// Manually Validate `RenewAtNumberOfDaysBeforeExpiry` and `RenewAtPercentageLifetime`
323+
if (RenewAtNumberOfDaysBeforeExpiry.HasValue && (RenewAtNumberOfDaysBeforeExpiry < 1 || RenewAtNumberOfDaysBeforeExpiry > int.MaxValue))
324+
{
325+
throw new AzPSArgumentOutOfRangeException(Properties.Resources.InvalidRangeDaysBeforeExpiry, nameof(RenewAtNumberOfDaysBeforeExpiry));
326+
}
327+
328+
if (RenewAtPercentageLifetime.HasValue && (RenewAtPercentageLifetime < 0 || RenewAtPercentageLifetime > 99))
329+
{
330+
throw new AzPSArgumentOutOfRangeException(Properties.Resources.InvalidRangePercentageLifetime, nameof(RenewAtPercentageLifetime));
331+
}
332+
}
316333
}
334+
317335
}

src/KeyVault/KeyVault/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/KeyVault/KeyVault/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,4 +630,10 @@ You can find the object ID using Azure Active Directory Module for Windows Power
630630
<data name="ProcessingCertError" xml:space="preserve">
631631
<value>Error happens when processing certificate '{0}'. See detailed error: {1}</value>
632632
</data>
633+
<data name="InvalidRangeDaysBeforeExpiry" xml:space="preserve">
634+
<value>Value must be between 1 and int.MaxValue.</value>
635+
</data>
636+
<data name="InvalidRangePercentageLifetime" xml:space="preserve">
637+
<value>"Value must be between 0 and 99."</value>
638+
</data>
633639
</root>

src/KeyVault/KeyVault/help/Set-AzKeyVaultCertificatePolicy.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Set-AzKeyVaultCertificatePolicy [-VaultName] <String> [-Name] <String> [-RenewAt
2222
[-Ekus <System.Collections.Generic.List`1[System.String]>] [-ValidityInMonths <Int32>] [-IssuerName <String>]
2323
[-CertificateType <String>] [-EmailAtNumberOfDaysBeforeExpiry <Int32>] [-EmailAtPercentageLifetime <Int32>]
2424
[-KeyType <String>] [-KeySize <Int32>] [-KeyNotExportable] [-CertificateTransparency <Boolean>] [-PassThru]
25-
[-Curve <String>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
25+
[-Curve <String>] [-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>] [-WhatIf]
2626
[-Confirm] [<CommonParameters>]
2727
```
2828

@@ -32,7 +32,7 @@ Set-AzKeyVaultCertificatePolicy [-VaultName] <String> [-Name] <String>
3232
[-InputObject] <PSKeyVaultCertificatePolicy> [-EmailAtNumberOfDaysBeforeExpiry <Int32>]
3333
[-EmailAtPercentageLifetime <Int32>] [-KeyType <String>] [-KeySize <Int32>]
3434
[-CertificateTransparency <Boolean>] [-PassThru] [-Curve <String>] [-DefaultProfile <IAzureContextContainer>]
35-
[-WhatIf] [-Confirm] [<CommonParameters>]
35+
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
3636
```
3737

3838
### ExpandedRenewNumber
@@ -44,7 +44,7 @@ Set-AzKeyVaultCertificatePolicy [-VaultName] <String> [-Name] <String> -RenewAtN
4444
[-Ekus <System.Collections.Generic.List`1[System.String]>] [-ValidityInMonths <Int32>] [-IssuerName <String>]
4545
[-CertificateType <String>] [-EmailAtNumberOfDaysBeforeExpiry <Int32>] [-EmailAtPercentageLifetime <Int32>]
4646
[-KeyType <String>] [-KeySize <Int32>] [-KeyNotExportable] [-CertificateTransparency <Boolean>] [-PassThru]
47-
[-Curve <String>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
47+
[-Curve <String>] [-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>] [-WhatIf]
4848
[-Confirm] [<CommonParameters>]
4949
```
5050

@@ -135,7 +135,7 @@ Accepted values: P-256, P-384, P-521, P-256K, SECP256K1
135135
Required: False
136136
Position: Named
137137
Default value: None
138-
Accept pipeline input: True (ByPropertyName)
138+
Accept pipeline input: False
139139
Accept wildcard characters: False
140140
```
141141
@@ -293,7 +293,7 @@ Accepted values: 2048, 3072, 4096, 256, 384, 521
293293
Required: False
294294
Position: Named
295295
Default value: 2048
296-
Accept pipeline input: True (ByPropertyName)
296+
Accept pipeline input: False
297297
Accept wildcard characters: False
298298
```
299299
@@ -365,6 +365,21 @@ Accept pipeline input: False
365365
Accept wildcard characters: False
366366
```
367367
368+
### -ProgressAction
369+
{{ Fill ProgressAction Description }}
370+
371+
```yaml
372+
Type: System.Management.Automation.ActionPreference
373+
Parameter Sets: (All)
374+
Aliases: proga
375+
376+
Required: False
377+
Position: Named
378+
Default value: None
379+
Accept pipeline input: False
380+
Accept wildcard characters: False
381+
```
382+
368383
### -RenewAtNumberOfDaysBeforeExpiry
369384
Specifies the number of days before expiry after which the automatic process for certificate renewal begins.
370385
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"Module","ClassName","Target","Severity","ProblemId","Description","Remediation"
2+
"Az.KeyVault","Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultCertificatePolicy","Set-AzKeyVaultCertificatePolicy","0","1050","The parameter set '__AllParameterSets' for cmdlet 'Set-AzKeyVaultCertificatePolicy' has been removed.","Add parameter set '__AllParameterSets' back to cmdlet 'Set-AzKeyVaultCertificatePolicy'."
3+
"Az.KeyVault","Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultCertificatePolicy","Set-AzKeyVaultCertificatePolicy","0","1050","The parameter set 'ByValue' for cmdlet 'Set-AzKeyVaultCertificatePolicy' has been removed.","Add parameter set 'ByValue' back to cmdlet 'Set-AzKeyVaultCertificatePolicy'."
4+
"Az.KeyVault","Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultCertificatePolicy","Set-AzKeyVaultCertificatePolicy","0","1050","The parameter set 'ExpandedRenewNumber' for cmdlet 'Set-AzKeyVaultCertificatePolicy' has been removed.","Add parameter set 'ExpandedRenewNumber' back to cmdlet 'Set-AzKeyVaultCertificatePolicy'."
5+
"Az.KeyVault","Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultCertificatePolicy","Set-AzKeyVaultCertificatePolicy","0","1050","The parameter set 'ExpandedRenewPercentage' for cmdlet 'Set-AzKeyVaultCertificatePolicy' has been removed.","Add parameter set 'ExpandedRenewPercentage' back to cmdlet 'Set-AzKeyVaultCertificatePolicy'."

0 commit comments

Comments
 (0)