Skip to content

Commit cbe6b06

Browse files
BethanyZhouVeryEarlySandidoisra-fel
authored
[KeyVault] Removed redundant Microsoft Graph API calls for access policy in Get-AzKeyVault (#23062)
* reduce MSGraph API call for access policy * Add change log * Update src/KeyVault/KeyVault/Models/ModelExtensions.cs * Update ChangeLog.md * polish code * record test cases for Set-AzKeyVaultAccessPolicy * Record test case for TestConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuestManual (#23534) * test trying * clean * merge from main * test new record --------- Co-authored-by: Adam Sandor <[email protected]> * Update src/KeyVault/KeyVault/ChangeLog.md Co-authored-by: Yeming Liu <[email protected]> * polish code --------- Co-authored-by: Yabo Hu <[email protected]> Co-authored-by: Adam Sandor <[email protected]> Co-authored-by: Yeming Liu <[email protected]>
1 parent f0263ee commit cbe6b06

File tree

12 files changed

+1693
-1836
lines changed

12 files changed

+1693
-1836
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6232,14 +6232,14 @@ function Test-ManualConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuest
62326232
# Setup
62336233
#$rgname = Get-ComputeTestResourceName;
62346234
$loc = "northeurope";
6235-
$rgname = "adsandwiki53";
6235+
$rgname = "adsandwiki57";
62366236

62376237
try
62386238
{
62396239
<#
62406240
The below script runs assuming that these below steps were manually run beforehand.
62416241
This script uses Data Plane Operations, which our test framework does not support.
6242-
$rgname = "adsandwiki53";
6242+
$rgname = "adsandwiki57";
62436243
$loc = 'northeurope';
62446244
New-AzResourceGroup -Name $rgname -Location $loc -Force;
62456245
@@ -6255,8 +6255,8 @@ function Test-ManualConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuest
62556255
$secureEncryptGuestState = 'DiskWithVMGuestState';
62566256
$vmSecurityType = "ConfidentialVM";
62576257
$user = "admin01";
6258-
#$password = Get-PasswordForVM;
6259-
$securePassword = "Testing1234567" | ConvertTo-SecureString -AsPlainText -Force;
6258+
$password = "Testing1234567";
6259+
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
62606260
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
62616261
62626262
$kvname = "kv" + $rgname;
@@ -6300,12 +6300,12 @@ function Test-ManualConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuest
63006300
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
63016301
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
63026302

6303-
$kvname = "kvadsandwiki53";
6304-
$keyname = "kadsandwiki53";
6305-
$desName= "desadsandwiki53";
6303+
$kvname = "kv" + $rgname;
6304+
$keyname = "k" + $rgname;
6305+
$desName= "des" + $rgname;
63066306

6307-
$encryptionKeyVaultId = "/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/adsandwiki53/providers/Microsoft.KeyVault/vaults/kvadsandwiki53";
6308-
$encryptionKeyURL = "https://kvadsandwiki53.vault.azure.net/keys/kadsandwiki53/c3d6f9e802ac4a90962cf43b9718cc94";
6307+
$encryptionKeyVaultId = "/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/adsandwiki57/providers/Microsoft.KeyVault/vaults/kvadsandwiki57";
6308+
$encryptionKeyURL = "https://kvadsandwiki57.vault.azure.net/keys/kadsandwiki57/3bacce5d72d147a785ecf79d4b1dc9b0";
63096309

63106310
# Create new DES Config and DES
63116311
$diskEncryptionType = "ConfidentialVmEncryptedWithCustomerKey";
@@ -6327,7 +6327,7 @@ function Test-ManualConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuest
63276327
$vnet = New-AzVirtualNetwork -Force -Name ($vnetPrefix + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
63286328
$vnet = Get-AzVirtualNetwork -Name ($vnetPrefix + $rgname) -ResourceGroupName $rgname;
63296329
$subnetId = $vnet.Subnets[0].Id;
6330-
$pubip = New-AzPublicIpAddress -Force -Name ($pubIpPrefix + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel $domainNameLabel2;
6330+
$pubip = New-AzPublicIpAddress -Force -Name ($pubIpPrefix + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Static -DomainNameLabel $domainNameLabel2;
63316331
$pubip = Get-AzPublicIpAddress -Name ($pubIpPrefix + $rgname) -ResourceGroupName $rgname;
63326332
$pubipId = $pubip.Id;
63336333
$nic = New-AzNetworkInterface -Force -Name ($nicPrefix + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
@@ -6468,8 +6468,8 @@ function Test-ConfVMSetAzDiskSecurityProfile
64686468
$keyname = "key" + $rgname;
64696469
$desName= "des" + $rgname;
64706470
$KeySize = 3072;
6471-
$keyVaultId = "/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/adsanddes2/providers/Microsoft.KeyVault/vaults/valadsanddes2";
6472-
$keyUrl = "https://valadsanddes2.vault.azure.net/keys/keyadsanddes2/929ebcca47fd4540a1ce06fbb35c821e";
6471+
$keyVaultId = "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/adsanddes2/providers/Microsoft.KeyVault/vaults/valadsanddes2";
6472+
$keyUrl = "https://valadsanddes2.vault.azure.net/keys/keyadsanddes2/186432bab3ba4f829f483670988a2996";
64736473

64746474
# Create new DES Config and DES
64756475
$diskEncryptionType = "ConfidentialVmEncryptedWithCustomerKey";

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestConfVMSetAzDiskSecurityProfile.json

Lines changed: 404 additions & 525 deletions
Large diffs are not rendered by default.

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuestManual.json

Lines changed: 915 additions & 1290 deletions
Large diffs are not rendered by default.

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Removed redundant Microsoft Graph API calls for access policy in `Get-AzKeyVault`.
2122

2223
## Version 5.0.0
2324
* Removed non-core types creation in PowerShell scripts to be compatible in constrained language mode.
@@ -250,4 +251,4 @@
250251

251252
## Version 1.0.0
252253
* General availability of `Az.KeyVault` module
253-
* Remove deprecated PurgeDisabled property from PS models
254+
* Remove deprecated PurgeDisabled property from PS models
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications.Models;
16+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Groups.Models;
17+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Users.Models;
18+
using Newtonsoft.Json;
19+
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Linq;
23+
using System.Text;
24+
25+
namespace Microsoft.Azure.Commands.KeyVault.Models.ADObject
26+
{
27+
internal static class MSGraphClientExtension
28+
{
29+
30+
public static bool IsUser(this Common.MSGraph.Version1_0.DirectoryObjects.Models.MicrosoftGraphDirectoryObject obj)
31+
{
32+
return string.Equals(obj.Odatatype, "#microsoft.graph.user", StringComparison.OrdinalIgnoreCase);
33+
}
34+
public static bool IsServicePrincipal(this Common.MSGraph.Version1_0.DirectoryObjects.Models.MicrosoftGraphDirectoryObject obj)
35+
{
36+
return string.Equals(obj.Odatatype, "#microsoft.graph.servicePrincipal", StringComparison.OrdinalIgnoreCase);
37+
}
38+
public static bool IsGroup(this Common.MSGraph.Version1_0.DirectoryObjects.Models.MicrosoftGraphDirectoryObject obj)
39+
{
40+
return string.Equals(obj.Odatatype, "#microsoft.graph.group", StringComparison.OrdinalIgnoreCase);
41+
}
42+
43+
public static PSADUser ToPSADUser(this MicrosoftGraphUser user)
44+
{
45+
return new PSADUser()
46+
{
47+
DisplayName = user.DisplayName,
48+
Id = user.Id,
49+
UserPrincipalName = user.UserPrincipalName,
50+
Type = user.UserType ?? "User",
51+
UsageLocation = user.UsageLocation,
52+
GivenName = user.GivenName,
53+
Surname = user.Surname,
54+
AccountEnabled = user.AccountEnabled,
55+
MailNickname = user.MailNickname,
56+
Mail = user.Mail,
57+
ImmutableId = user.OnPremisesImmutableId,
58+
AdditionalProperties = user.AdditionalProperties
59+
};
60+
}
61+
62+
public static PSADGroup ToPSADGroup(this MicrosoftGraphGroup group)
63+
{
64+
return new PSADGroup()
65+
{
66+
DisplayName = group.DisplayName,
67+
Id = group.Id,
68+
Type = "Group",
69+
SecurityEnabled = group.SecurityEnabled,
70+
MailNickname = !string.IsNullOrEmpty(group.Mail) ? group.Mail : group.AdditionalProperties.ContainsKey("mailNickname") ? group.AdditionalProperties["mailNickname"]?.ToString() : null,
71+
Description = group.AdditionalProperties.ContainsKey("description") ? group.AdditionalProperties["description"]?.ToString() : null,
72+
MailEnabled = group.MailEnabled,
73+
AdditionalProperties = group.AdditionalProperties
74+
};
75+
}
76+
77+
public static PSADServicePrincipal ToPSADServicePrincipal(this MicrosoftGraphServicePrincipal servicePrincipal)
78+
{
79+
return new PSADServicePrincipal()
80+
{
81+
DisplayName = servicePrincipal.DisplayName,
82+
Id = servicePrincipal.Id,
83+
ApplicationId = Guid.Parse(servicePrincipal.AppId),
84+
Type = "ServicePrincipal",
85+
ServicePrincipalNames = servicePrincipal.ServicePrincipalNames.ToArray(),
86+
AdditionalProperties = servicePrincipal.AdditionalProperties
87+
};
88+
}
89+
90+
91+
public static PSADObject ToPSADObject(this Common.MSGraph.Version1_0.DirectoryObjects.Models.MicrosoftGraphDirectoryObject obj)
92+
{
93+
if (obj == null) throw new ArgumentNullException();
94+
95+
if (obj.IsUser())
96+
{
97+
return JsonConvert.DeserializeObject<MicrosoftGraphUser>(JsonConvert.SerializeObject(obj)).ToPSADUser();
98+
}
99+
if (obj.IsServicePrincipal())
100+
{
101+
return JsonConvert.DeserializeObject<MicrosoftGraphServicePrincipal>(JsonConvert.SerializeObject(obj)).ToPSADServicePrincipal();
102+
}
103+
if (obj.IsGroup())
104+
{
105+
return JsonConvert.DeserializeObject<MicrosoftGraphGroup>(JsonConvert.SerializeObject(obj)).ToPSADGroup();
106+
}
107+
108+
return new PSADObject()
109+
{
110+
Id = obj.Id,
111+
};
112+
}
113+
114+
}
115+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
18+
19+
namespace Microsoft.Azure.Commands.KeyVault.Models.ADObject
20+
{
21+
internal class PSADGroup : PSADObject
22+
{
23+
public bool? SecurityEnabled { get; set; }
24+
25+
public bool? MailEnabled { get; set; }
26+
27+
public string MailNickname { get; set; }
28+
29+
public string ObjectType => "Group";
30+
31+
public string Description { get; set; }
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
18+
19+
namespace Microsoft.Azure.Commands.KeyVault.Models.ADObject
20+
{
21+
internal class PSADObject
22+
{
23+
public string DisplayName { get; set; }
24+
25+
public string Id { get; set; }
26+
27+
public string Type { get; set; }
28+
29+
public IDictionary<string, object> AdditionalProperties { get; set; }
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
18+
19+
namespace Microsoft.Azure.Commands.KeyVault.Models.ADObject
20+
{
21+
internal class PSADServicePrincipal : PSADObject
22+
{
23+
public string[] ServicePrincipalNames { get; set; }
24+
25+
public Guid ApplicationId { get; set; }
26+
27+
public string ObjectType => "ServicePrincipal";
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
18+
19+
namespace Microsoft.Azure.Commands.KeyVault.Models.ADObject
20+
{
21+
internal class PSADUser : PSADObject
22+
{
23+
public string UserPrincipalName { get; set; }
24+
25+
public string ObjectType => "User";
26+
27+
public string UsageLocation { get; set; }
28+
29+
public string GivenName { get; set; }
30+
31+
public string Surname { get; set; }
32+
33+
public bool? AccountEnabled { get; set; }
34+
35+
public string MailNickname { get; set; }
36+
37+
public string Mail { get; set; }
38+
39+
public string ImmutableId { get; set; }
40+
}
41+
}

0 commit comments

Comments
 (0)