Skip to content

Commit b375bde

Browse files
authored
Merge pull request #9415 from anusapan/regenerate-key
Add support to regenerate policy keys
2 parents d8c92ba + ccdddf0 commit b375bde

File tree

16 files changed

+2998
-1251
lines changed

16 files changed

+2998
-1251
lines changed

src/IotHub/IotHub.Test/ScenarioTests/IotHubTests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ function Test-AzureRmIotHubLifecycle
160160
$keys = Get-AzIotHubKey -ResourceGroupName $ResourceGroupName -Name $IotHubName
161161
Assert-True { $keys.Count -eq 6 }
162162

163+
# Get New Key
164+
$newkey = Get-AzIotHubKey -ResourceGroupName $ResourceGroupName -Name $IotHubName -KeyName iothubowner1
165+
166+
# Swap keys
167+
$swappedKey = New-AzIotHubKey -ResourceGroupName $ResourceGroupName -Name $IotHubName -KeyName iothubowner1 -RenewKey Swap
168+
Assert-True { $swappedKey.PrimaryKey -eq $newkey.SecondaryKey }
169+
Assert-True { $swappedKey.SecondaryKey -eq $newkey.PrimaryKey }
170+
171+
# Regenerate Primary Key
172+
$regeneratedKey = New-AzIotHubKey -ResourceGroupName $ResourceGroupName -Name $IotHubName -KeyName iothubowner1 -RenewKey Primary
173+
Assert-True { $regeneratedKey.PrimaryKey -ne $swappedKey.PrimaryKey }
174+
163175
# Remove Key
164176
Remove-AzIotHubKey -ResourceGroupName $ResourceGroupName -Name $IotHubName -KeyName iothubowner1
165177

src/IotHub/IotHub.Test/SessionRecords/Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubTests/TestAzureIotHubLifeCycle.json

Lines changed: 2439 additions & 1235 deletions
Large diffs are not rendered by default.

src/IotHub/IotHub/Az.IotHub.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ CmdletsToExport = 'Add-AzIotHubKey', 'Get-AzIotHubEventHubConsumerGroup',
8888
'Get-AzIotHubRoutingEndpoint', 'Add-AzIotHubRoutingEndpoint',
8989
'Remove-AzIotHubRoutingEndpoint', 'Get-AzIotHubRoute',
9090
'Add-AzIotHubRoute', 'Remove-AzIotHubRoute', 'Set-AzIotHubRoute',
91-
'Test-AzIotHubRoute'
91+
'Test-AzIotHubRoute', 'New-AzIotHubKey'
9292

9393
# Variables to export from this module
9494
# VariablesToExport = @()

src/IotHub/IotHub/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+
* Add support to regenerate authorization policy keys.
2122

2223
## Version 1.1.0
2324
* Updated cmdlets with plural nouns to singular, and deprecated plural names.

src/IotHub/IotHub/IotHub/AddAzureRmIotHubKey.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@ namespace Microsoft.Azure.Commands.Management.IotHub
2323
using Microsoft.Azure.Management.IotHub.Models;
2424
using ResourceManager.Common.ArgumentCompleters;
2525

26-
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", SupportsShouldProcess = true)]
26+
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)]
2727
[OutputType(typeof(PSSharedAccessSignatureAuthorizationRule))]
2828
public class AddAzureRmIotHubKey : IotHubBaseCmdlet
2929
{
30+
private const string ResourceIdParameterSet = "ResourceIdSet";
31+
private const string ResourceParameterSet = "ResourceSet";
32+
33+
[Parameter(
34+
Position = 0,
35+
Mandatory = true,
36+
ParameterSetName = ResourceIdParameterSet,
37+
ValueFromPipelineByPropertyName = true,
38+
HelpMessage = "IotHub Resource Id")]
39+
[ValidateNotNullOrEmpty]
40+
public string HubId { get; set; }
41+
3042
[Parameter(
3143
Position = 0,
3244
Mandatory = true,
45+
ParameterSetName = ResourceParameterSet,
3346
ValueFromPipelineByPropertyName = true,
3447
HelpMessage = "Name of the Resource Group")]
3548
[ResourceGroupCompleter]
@@ -39,14 +52,21 @@ public class AddAzureRmIotHubKey : IotHubBaseCmdlet
3952
[Parameter(
4053
Position = 1,
4154
Mandatory = true,
55+
ParameterSetName = ResourceParameterSet,
4256
ValueFromPipelineByPropertyName = true,
4357
HelpMessage = "Name of the Iot Hub")]
4458
[ValidateNotNullOrEmpty]
4559
public string Name { get; set; }
4660

61+
[Parameter(
62+
Position = 1,
63+
Mandatory = true,
64+
ParameterSetName = ResourceIdParameterSet,
65+
HelpMessage = "Name of the Key")]
4766
[Parameter(
4867
Position = 2,
4968
Mandatory = true,
69+
ParameterSetName = ResourceParameterSet,
5070
HelpMessage = "Name of the Key")]
5171
[ValidateNotNullOrEmpty]
5272
public string KeyName { get; set; }
@@ -71,6 +91,12 @@ public override void ExecuteCmdlet()
7191
{
7292
if (ShouldProcess(KeyName, Properties.Resources.AddIotHubKey))
7393
{
94+
if (ParameterSetName.Equals(ResourceIdParameterSet))
95+
{
96+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.HubId);
97+
this.Name = IotHubUtils.GetIotHubName(this.HubId);
98+
}
99+
74100
var psAuthRule = new PSSharedAccessSignatureAuthorizationRule()
75101
{
76102
KeyName = this.KeyName,

src/IotHub/IotHub/IotHub/GetAzureRmIotHubKey.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@ namespace Microsoft.Azure.Commands.Management.IotHub
2323
using Microsoft.Azure.Management.IotHub.Models;
2424
using ResourceManager.Common.ArgumentCompleters;
2525

26-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey")]
26+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", DefaultParameterSetName = ResourceParameterSet)]
2727
[OutputType(typeof(PSSharedAccessSignatureAuthorizationRule))]
2828
public class GetAzureRmIotHubKey : IotHubBaseCmdlet
2929
{
30+
private const string ResourceIdParameterSet = "ResourceIdSet";
31+
private const string ResourceParameterSet = "ResourceSet";
32+
33+
[Parameter(
34+
Position = 0,
35+
Mandatory = true,
36+
ParameterSetName = ResourceIdParameterSet,
37+
ValueFromPipelineByPropertyName = true,
38+
HelpMessage = "IotHub Resource Id")]
39+
[ValidateNotNullOrEmpty]
40+
public string HubId { get; set; }
41+
3042
[Parameter(
3143
Position = 0,
3244
Mandatory = true,
45+
ParameterSetName = ResourceParameterSet,
3346
ValueFromPipelineByPropertyName = true,
3447
HelpMessage = "Name of the Resource Group")]
3548
[ResourceGroupCompleter]
@@ -39,20 +52,33 @@ public class GetAzureRmIotHubKey : IotHubBaseCmdlet
3952
[Parameter(
4053
Position = 1,
4154
Mandatory = true,
55+
ParameterSetName = ResourceParameterSet,
4256
ValueFromPipelineByPropertyName = true,
4357
HelpMessage = "Name of the Iot Hub")]
4458
[ValidateNotNullOrEmpty]
4559
public string Name { get; set; }
4660

61+
[Parameter(
62+
Position = 1,
63+
Mandatory = false,
64+
ParameterSetName = ResourceIdParameterSet,
65+
HelpMessage = "Name of the Key")]
4766
[Parameter(
4867
Position = 2,
4968
Mandatory = false,
69+
ParameterSetName = ResourceParameterSet,
5070
HelpMessage = "Name of the Key")]
5171
[ValidateNotNullOrEmpty]
5272
public string KeyName { get; set; }
5373

5474
public override void ExecuteCmdlet()
5575
{
76+
if (ParameterSetName.Equals(ResourceIdParameterSet))
77+
{
78+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.HubId);
79+
this.Name = IotHubUtils.GetIotHubName(this.HubId);
80+
}
81+
5682
if (KeyName != null)
5783
{
5884
SharedAccessSignatureAuthorizationRule authPolicy = this.IotHubClient.IotHubResource.GetKeysForKeyName(this.ResourceGroupName, this.Name, this.KeyName);
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Management.IotHub
16+
{
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Globalization;
20+
using System.Linq;
21+
using System.Management.Automation;
22+
using System.Text;
23+
using Microsoft.Azure.Commands.Management.IotHub.Common;
24+
using Microsoft.Azure.Commands.Management.IotHub.Models;
25+
using Microsoft.Azure.Management.IotHub;
26+
using Microsoft.Azure.Management.IotHub.Models;
27+
using ResourceManager.Common.ArgumentCompleters;
28+
29+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)]
30+
[OutputType(typeof(PSSharedAccessSignatureAuthorizationRule))]
31+
public class NewAzureRmIotHubKey : IotHubBaseCmdlet
32+
{
33+
private const string ResourceIdParameterSet = "ResourceIdSet";
34+
private const string ResourceParameterSet = "ResourceSet";
35+
36+
[Parameter(
37+
Position = 0,
38+
Mandatory = true,
39+
ParameterSetName = ResourceIdParameterSet,
40+
ValueFromPipelineByPropertyName = true,
41+
HelpMessage = "IotHub Resource Id")]
42+
[ValidateNotNullOrEmpty]
43+
public string HubId { get; set; }
44+
45+
[Parameter(
46+
Position = 0,
47+
Mandatory = true,
48+
ParameterSetName = ResourceParameterSet,
49+
ValueFromPipelineByPropertyName = true,
50+
HelpMessage = "Name of the Resource Group")]
51+
[ResourceGroupCompleter]
52+
[ValidateNotNullOrEmpty]
53+
public string ResourceGroupName { get; set; }
54+
55+
[Parameter(
56+
Position = 1,
57+
Mandatory = true,
58+
ParameterSetName = ResourceParameterSet,
59+
ValueFromPipelineByPropertyName = true,
60+
HelpMessage = "Name of the Iot Hub")]
61+
[ValidateNotNullOrEmpty]
62+
public string Name { get; set; }
63+
64+
[Parameter(
65+
Position = 1,
66+
Mandatory = true,
67+
ParameterSetName = ResourceIdParameterSet,
68+
HelpMessage = "Name of the Key")]
69+
[Parameter(
70+
Position = 2,
71+
Mandatory = true,
72+
ParameterSetName = ResourceParameterSet,
73+
ValueFromPipelineByPropertyName = true,
74+
HelpMessage = "Name of the Key")]
75+
[ValidateNotNullOrEmpty]
76+
public string KeyName { get; set; }
77+
78+
[Parameter(
79+
Position = 2,
80+
Mandatory = true,
81+
ParameterSetName = ResourceIdParameterSet,
82+
HelpMessage = "Regenerate Key.")]
83+
[Parameter(
84+
Position = 3,
85+
Mandatory = true,
86+
ParameterSetName = ResourceParameterSet,
87+
HelpMessage = "Regenerate Key.")]
88+
[ValidateNotNullOrEmpty]
89+
[PSArgumentCompleter(new string[] { "Primary", "Secondary", "Swap" })]
90+
public string RenewKey { get; set; }
91+
92+
public override void ExecuteCmdlet()
93+
{
94+
if (ShouldProcess(KeyName, Properties.Resources.NewIotHubKey))
95+
{
96+
if (ParameterSetName.Equals(ResourceIdParameterSet))
97+
{
98+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.HubId);
99+
this.Name = IotHubUtils.GetIotHubName(this.HubId);
100+
}
101+
102+
var regeneratedAuthRule = new PSSharedAccessSignatureAuthorizationRule();
103+
104+
IotHubDescription iothubDesc = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.Name);
105+
IList<SharedAccessSignatureAuthorizationRule> authRules = (List<SharedAccessSignatureAuthorizationRule>)this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.Name).ToList();
106+
107+
foreach (var authRule in authRules)
108+
{
109+
if (authRule.KeyName.Equals(this.KeyName, StringComparison.OrdinalIgnoreCase))
110+
{
111+
regeneratedAuthRule = IotHubUtils.ToPSSharedAccessSignatureAuthorizationRule(authRule);
112+
authRules.Remove(authRule);
113+
break;
114+
}
115+
}
116+
117+
switch (RenewKey.ToLower(CultureInfo.InvariantCulture))
118+
{
119+
case "primary":
120+
regeneratedAuthRule.PrimaryKey = this.RegenerateKey();
121+
break;
122+
case "secondary":
123+
regeneratedAuthRule.SecondaryKey = this.RegenerateKey();
124+
break;
125+
case "swap":
126+
var temp = regeneratedAuthRule.PrimaryKey;
127+
regeneratedAuthRule.PrimaryKey = regeneratedAuthRule.SecondaryKey;
128+
regeneratedAuthRule.SecondaryKey = temp;
129+
break;
130+
}
131+
132+
authRules.Add(IotHubUtils.ToSharedAccessSignatureAuthorizationRule(regeneratedAuthRule));
133+
iothubDesc.Properties.AuthorizationPolicies = authRules;
134+
135+
this.IotHubClient.IotHubResource.CreateOrUpdate(this.ResourceGroupName, this.Name, iothubDesc);
136+
IEnumerable<SharedAccessSignatureAuthorizationRule> updatedAuthRules = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.Name);
137+
138+
SharedAccessSignatureAuthorizationRule authPolicy = this.IotHubClient.IotHubResource.GetKeysForKeyName(this.ResourceGroupName, this.Name, this.KeyName);
139+
this.WriteObject(IotHubUtils.ToPSSharedAccessSignatureAuthorizationRule(authPolicy), false);
140+
}
141+
}
142+
143+
private string RegenerateKey(int byteLength = 32)
144+
{
145+
char[] charArray = new char[byteLength];
146+
for (int i = 0; i < byteLength; i++)
147+
{
148+
charArray[i] = (char)new Random().Next(1, 255);
149+
}
150+
string charCode = new string(charArray);
151+
byte[] bytes = Encoding.GetEncoding(28591).GetBytes(charCode);
152+
return System.Convert.ToBase64String(bytes);
153+
}
154+
}
155+
}

src/IotHub/IotHub/IotHub/RemoveAzureRmIotHubKey.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,26 @@ namespace Microsoft.Azure.Commands.Management.IotHub
2626
using PSIotHubProperties = Microsoft.Azure.Commands.Management.IotHub.Properties;
2727
using ResourceManager.Common.ArgumentCompleters;
2828

29-
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", SupportsShouldProcess = true)]
29+
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubKey", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)]
3030
[OutputType(typeof(PSSharedAccessSignatureAuthorizationRule))]
3131
public class RemoveAzureRmIotHubKey : IotHubBaseCmdlet
3232
{
33+
private const string ResourceIdParameterSet = "ResourceIdSet";
34+
private const string ResourceParameterSet = "ResourceSet";
3335

3436
[Parameter(
3537
Position = 0,
3638
Mandatory = true,
39+
ParameterSetName = ResourceIdParameterSet,
40+
ValueFromPipelineByPropertyName = true,
41+
HelpMessage = "IotHub Resource Id")]
42+
[ValidateNotNullOrEmpty]
43+
public string HubId { get; set; }
44+
45+
[Parameter(
46+
Position = 0,
47+
Mandatory = true,
48+
ParameterSetName = ResourceParameterSet,
3749
ValueFromPipelineByPropertyName = true,
3850
HelpMessage = "Name of the Resource Group")]
3951
[ResourceGroupCompleter]
@@ -43,14 +55,22 @@ public class RemoveAzureRmIotHubKey : IotHubBaseCmdlet
4355
[Parameter(
4456
Position = 1,
4557
Mandatory = true,
58+
ParameterSetName = ResourceParameterSet,
4659
ValueFromPipelineByPropertyName = true,
4760
HelpMessage = "Name of the Iot Hub")]
4861
[ValidateNotNullOrEmpty]
4962
public string Name { get; set; }
5063

64+
65+
[Parameter(
66+
Position = 1,
67+
Mandatory = true,
68+
ParameterSetName = ResourceIdParameterSet,
69+
HelpMessage = "Name of the Key")]
5170
[Parameter(
5271
Position = 2,
5372
Mandatory = true,
73+
ParameterSetName = ResourceParameterSet,
5474
ValueFromPipelineByPropertyName = true,
5575
HelpMessage = "Name of the Key")]
5676
[ValidateNotNullOrEmpty]
@@ -60,6 +80,12 @@ public override void ExecuteCmdlet()
6080
{
6181
if (ShouldProcess(KeyName, Properties.Resources.RemoveIotHubKey))
6282
{
83+
if (ParameterSetName.Equals(ResourceIdParameterSet))
84+
{
85+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.HubId);
86+
this.Name = IotHubUtils.GetIotHubName(this.HubId);
87+
}
88+
6389
IotHubDescription iothubDesc = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.Name);
6490
IList<SharedAccessSignatureAuthorizationRule> authRules = (List<SharedAccessSignatureAuthorizationRule>)this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.Name).ToList();
6591

0 commit comments

Comments
 (0)