Skip to content

Commit 8a6b8b7

Browse files
[KeyVault] Supported getting random number from managed HSM by Get-AzKeyVaultRandomNumber (#17778)
Checks are passed after I trigger it again. Force merge it. * Supported getting random number from managed HSM by Get-AzKeyVaultRandomNumber * rename Name to HsmName * refine codes * add description for resourceid Co-authored-by: Dingmeng Xue <[email protected]>
1 parent 6c74292 commit 8a6b8b7

9 files changed

+326
-1
lines changed

src/KeyVault/KeyVault/Az.KeyVault.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ CmdletsToExport = 'Add-AzKeyVaultCertificate', 'Update-AzKeyVaultCertificate',
100100
'New-AzKeyVaultRoleDefinition', 'Remove-AzKeyVaultRoleDefinition',
101101
'Remove-AzKeyVaultRoleAssignment', 'Remove-AzKeyVaultAccessPolicy',
102102
'Set-AzKeyVaultAccessPolicy', 'Backup-AzKeyVaultKey',
103-
'Get-AzKeyVaultKey', 'Get-AzKeyVaultSecret',
103+
'Get-AzKeyVaultKey', 'Get-AzKeyVaultSecret', 'Get-AzKeyVaultRandomNumber',
104104
'Undo-AzKeyVaultKeyRemoval', 'Undo-AzKeyVaultSecretRemoval',
105105
'Add-AzKeyVaultKey', 'Remove-AzKeyVaultKey', 'Update-AzKeyVault',
106106
'New-AzKeyVaultNetworkRuleSetObject', 'Remove-AzKeyVaultSecret',

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+
* Supported getting random number from managed HSM by `Get-AzKeyVaultRandomNumber`
2122
* Skipped subscription connection status validation for Az.KeyVault.Extension [#17712]
2223
* Enabled public network access setting
2324

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Microsoft.Azure.Commands.KeyVault.Models;
2+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
3+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Management.Automation;
8+
using System.Text;
9+
10+
namespace Microsoft.Azure.Commands.KeyVault.Commands.Key
11+
{
12+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzurePrefix + "KeyVaultRandomNumber", DefaultParameterSetName = GetByHsmNameParameterSet)]
13+
[OutputType(typeof(string), typeof(byte))]
14+
public class GetAzKeyVaultRandomNumber: KeyVaultCmdletBase
15+
{
16+
#region Parameter Set Names
17+
18+
private const string GetByHsmNameParameterSet = "GetByHsmName";
19+
private const string GetByHsmInputObjectNameParameterSet = "GetByHsmInputObject";
20+
private const string GetByHsmResourceIdParameterSet = "GetByHsmResourceId";
21+
22+
#endregion
23+
24+
#region Input Parameter Definitions
25+
26+
/// <summary>
27+
/// HSM Name
28+
/// </summary>
29+
[Parameter(Mandatory = true,
30+
Position = 0,
31+
ParameterSetName = GetByHsmNameParameterSet,
32+
HelpMessage = "HSM name. Cmdlet constructs the FQDN of a managed HSM based on the name and currently selected environment.")]
33+
[ResourceNameCompleter("Microsoft.KeyVault/managedHSMs", "FakeResourceGroupName")]
34+
[ValidateNotNullOrEmpty]
35+
public string HsmName;
36+
37+
/// <summary>
38+
/// HSM Input Object
39+
/// </summary>
40+
[Parameter(Mandatory = true,
41+
Position = 0,
42+
ValueFromPipeline = true,
43+
ParameterSetName = GetByHsmInputObjectNameParameterSet,
44+
HelpMessage = "HSM object.")]
45+
[ValidateNotNullOrEmpty]
46+
public PSManagedHsm InputObject;
47+
48+
/// <summary>
49+
/// HSM Resource Id
50+
/// </summary>
51+
[Parameter(Mandatory = true,
52+
Position = 0,
53+
ParameterSetName = GetByHsmResourceIdParameterSet,
54+
ValueFromPipelineByPropertyName = true,
55+
HelpMessage = "HSM resource id.")]
56+
[ValidateNotNullOrEmpty]
57+
public string ResourceId { get; set; }
58+
59+
[Parameter(Mandatory = true,
60+
HelpMessage = "The requested number of random bytes.")]
61+
[ValidateRange(1, 128)]
62+
public int Count;
63+
64+
[Parameter(Mandatory = false,
65+
HelpMessage = "If specified, return random number as base-64 digit. By default, this command retruns random number as byte array.")]
66+
public SwitchParameter AsBase64String;
67+
68+
#endregion
69+
70+
public override void ExecuteCmdlet()
71+
{
72+
NormalizeKeySourceParameters();
73+
var result = Track2DataClient.GetManagedHsmRandomNumber(HsmName, Count);
74+
if(AsBase64String.IsPresent)
75+
{
76+
this.WriteObject(Convert.ToBase64String(result));
77+
}
78+
else
79+
{
80+
this.WriteObject(result, true);
81+
}
82+
}
83+
84+
private void NormalizeKeySourceParameters()
85+
{
86+
if (InputObject != null)
87+
{
88+
HsmName = InputObject.Name;
89+
}
90+
else if (ResourceId != null)
91+
{
92+
var resourceIdentifier = new ResourceIdentifier(ResourceId);
93+
HsmName = resourceIdentifier.ResourceName;
94+
}
95+
}
96+
}
97+
}

src/KeyVault/KeyVault/Models/IKeyVaultDataServiceClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public interface IKeyVaultDataServiceClient
106106

107107
PSKeyVaultKey RestoreManagedHsmKey(string managedHsmName, string inputBlobPath);
108108

109+
byte[] GetManagedHsmRandomNumber(string managedHsmName, int count);
110+
109111
#region Key rotation
110112
PSKeyVaultKey RotateManagedHsmKey(string managedHsmName, string keyName);
111113

src/KeyVault/KeyVault/Models/KeyVaultDataServiceClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,11 @@ public PSKeyOperationResult ManagedHsmWrapKey(string managedHsmName, string keyN
22042204
throw new NotImplementedException("Wrapping keys on managed HSM is only possible in track 2 SDK.");
22052205
}
22062206

2207+
public byte[] GetManagedHsmRandomNumber(string managedHsmName, int count)
2208+
{
2209+
throw new NotImplementedException("Getting random number on managed HSM is only possible in track 2 SDK.");
2210+
}
2211+
22072212
#region Key rotation
22082213
public PSKeyVaultKey RotateManagedHsmKey(string managedHsmName, string keyName)
22092214
{

src/KeyVault/KeyVault/Track2Models/Track2HsmClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,16 @@ internal void PurgeKey(string managedHsmName, string keyName)
491491
throw GetInnerException(ex);
492492
}
493493
}
494+
495+
public byte[] GetRandomNumberBytes(string managedHsmName, int count)
496+
{
497+
if (string.IsNullOrEmpty(managedHsmName))
498+
throw new ArgumentNullException(nameof(managedHsmName));
499+
500+
var client = CreateKeyClient(managedHsmName);
501+
return client.GetRandomBytes(count);
502+
}
503+
494504
#endregion
495505

496506
#region Key rotation

src/KeyVault/KeyVault/Track2Models/Track2KeyVaultDataServiceClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public PSKeyVaultKey RestoreKey(string vaultName, string inputBlobPath)
116116
throw new NotImplementedException();
117117
}
118118

119+
public byte[] GetRandomNumber()
120+
{
121+
throw new NotImplementedException();
122+
}
123+
119124
#region Key rotation
120125
public PSKeyVaultKey RotateKey(string vaultName, string keyName)
121126
{
@@ -576,6 +581,12 @@ public PSKeyVaultKey RestoreManagedHsmKey(string managedHsmName, string inputBlo
576581
{
577582
return HsmClient.RestoreKey(managedHsmName, inputBlobPath);
578583
}
584+
585+
public byte[] GetManagedHsmRandomNumber(string managedHsmName, int count)
586+
{
587+
return HsmClient.GetRandomNumberBytes(managedHsmName, count);
588+
}
589+
579590
#endregion
580591

581592
#region Key rotation
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
external help file: Microsoft.Azure.PowerShell.Cmdlets.KeyVault.dll-Help.xml
3+
Module Name: Az.KeyVault
4+
online version: https://docs.microsoft.com/powershell/module/az.keyvault/get-azkeyvaultrandomnumber
5+
schema: 2.0.0
6+
---
7+
8+
# Get-AzKeyVaultRandomNumber
9+
10+
## SYNOPSIS
11+
Get the requested number of bytes containing random values from a managed HSM.
12+
13+
## SYNTAX
14+
15+
### GetByHsmName (Default)
16+
```
17+
Get-AzKeyVaultRandomNumber [-DefaultProfile <IAzureContextContainer>] [-HsmName] <String> -Count <Int32>
18+
[-AsBase64String] [<CommonParameters>]
19+
```
20+
21+
### GetByHsmResourceId
22+
```
23+
Get-AzKeyVaultRandomNumber [-ResourceId] <String> [-DefaultProfile <IAzureContextContainer>] -Count <Int32>
24+
[-AsBase64String] [<CommonParameters>]
25+
```
26+
27+
### GetByHsmInputObject
28+
```
29+
Get-AzKeyVaultRandomNumber [-DefaultProfile <IAzureContextContainer>] [-InputObject] <PSManagedHsm>
30+
-Count <Int32> [-AsBase64String] [<CommonParameters>]
31+
```
32+
33+
## DESCRIPTION
34+
Get the requested number of bytes containing random values from a managed HSM.
35+
36+
## EXAMPLES
37+
38+
### Example 1: Get requested number of random bytes by managed HSM name
39+
```powershell
40+
Get-AzKeyVaultRandomNumber -HsmName testmhsm -Count 10
41+
```
42+
43+
```output
44+
158
45+
171
46+
96
47+
142
48+
109
49+
28
50+
1
51+
85
52+
178
53+
201
54+
```
55+
56+
This command gets 10 random bytes from managed HSM "testmhsm"
57+
58+
### Example 2: Get random number as base64 string by piping
59+
```powershell
60+
Get-AzKeyVaultManagedHsm -HsmName bezmhsm2022 | Get-AzKeyVaultRandomNumber -Count 10 -AsBase64String
61+
```
62+
63+
```output
64+
G1CsEqa9yUp/EA==
65+
```
66+
67+
This command gets 10 random bytes as base-64 string from managed HSM "testmhsm"
68+
69+
### Example 3: Get random number by resource id
70+
```powershell
71+
Get-AzKeyVaultRandomNumber -ResourceId /subscriptions/0b1fxxxx-xxxx-xxxx-aec3-xxxx72f09590/resourceGroups/test-rg/provders/Microsoft.KeyVault/managedHSMs/testhsm -Count 10
72+
```
73+
74+
```output
75+
158
76+
171
77+
96
78+
142
79+
109
80+
28
81+
1
82+
85
83+
178
84+
201
85+
```
86+
87+
This command gets 10 random bytes from managed HSM with specified resource id
88+
89+
## PARAMETERS
90+
91+
### -AsBase64String
92+
If specified, return random number as base-64 digit.
93+
By default, this command retruns random number as byte array.
94+
95+
```yaml
96+
Type: System.Management.Automation.SwitchParameter
97+
Parameter Sets: (All)
98+
Aliases:
99+
100+
Required: False
101+
Position: Named
102+
Default value: None
103+
Accept pipeline input: False
104+
Accept wildcard characters: False
105+
```
106+
107+
### -Count
108+
The requested number of random bytes.
109+
110+
```yaml
111+
Type: System.Int32
112+
Parameter Sets: (All)
113+
Aliases:
114+
115+
Required: True
116+
Position: Named
117+
Default value: None
118+
Accept pipeline input: False
119+
Accept wildcard characters: False
120+
```
121+
122+
### -DefaultProfile
123+
The credentials, account, tenant, and subscription used for communication with Azure.
124+
125+
```yaml
126+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
127+
Parameter Sets: (All)
128+
Aliases: AzContext, AzureRmContext, AzureCredential
129+
130+
Required: False
131+
Position: Named
132+
Default value: None
133+
Accept pipeline input: False
134+
Accept wildcard characters: False
135+
```
136+
137+
### -HsmName
138+
HSM name. Cmdlet constructs the FQDN of a managed HSM based on the name and currently selected environment.
139+
140+
```yaml
141+
Type: System.String
142+
Parameter Sets: GetByHsmName
143+
Aliases:
144+
145+
Required: True
146+
Position: 0
147+
Default value: None
148+
Accept pipeline input: False
149+
Accept wildcard characters: False
150+
```
151+
152+
### -InputObject
153+
HSM object.
154+
155+
```yaml
156+
Type: Microsoft.Azure.Commands.KeyVault.Models.PSManagedHsm
157+
Parameter Sets: GetByHsmInputObject
158+
Aliases:
159+
160+
Required: True
161+
Position: 0
162+
Default value: None
163+
Accept pipeline input: True (ByValue)
164+
Accept wildcard characters: False
165+
```
166+
167+
### -ResourceId
168+
HSM resource id.
169+
170+
```yaml
171+
Type: System.String
172+
Parameter Sets: GetByHsmResourceId
173+
Aliases:
174+
175+
Required: True
176+
Position: 0
177+
Default value: None
178+
Accept pipeline input: True (ByPropertyName)
179+
Accept wildcard characters: False
180+
```
181+
182+
### CommonParameters
183+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
184+
185+
## INPUTS
186+
187+
### System.String
188+
189+
### Microsoft.Azure.Commands.KeyVault.Models.PSManagedHsm
190+
191+
## OUTPUTS
192+
193+
### System.String
194+
195+
### System.Byte
196+
197+
## NOTES
198+
199+
## RELATED LINKS

0 commit comments

Comments
 (0)