Skip to content

Commit 9513fbb

Browse files
authored
[KeyVault] Upgrade to MSGraph (Azure#16373)
* Migrate to MSGraph * Warning message and doc * remove extra space
1 parent fefb646 commit 9513fbb

34 files changed

+398
-268
lines changed

src/KeyVault/KeyVault.Test/ScenarioTests/KeyVaultManagementController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.Azure.Management.Internal.Resources;
2727
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2828
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
29+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0;
2930

3031
namespace Microsoft.Azure.Commands.KeyVault.Test
3132
{
@@ -41,7 +42,7 @@ public class KeyVaultManagementController
4142

4243
public KeyVaultManagementClient KeyVaultManagementClient { get; private set; }
4344

44-
public GraphRbacManagementClient GraphClient { get; private set; }
45+
public IMicrosoftGraphClient GraphClient { get; private set; }
4546

4647
public string UserDomain { get; private set; }
4748

@@ -142,7 +143,7 @@ private static KeyVaultManagementClient GetKeyVaultManagementClient(MockContext
142143
return context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
143144
}
144145

145-
private GraphRbacManagementClient GetGraphClient(MockContext context)
146+
private IMicrosoftGraphClient GetGraphClient(MockContext context)
146147
{
147148
var environment = TestEnvironmentFactory.GetTestEnvironment();
148149
string tenantId = null;
@@ -169,7 +170,7 @@ private GraphRbacManagementClient GetGraphClient(MockContext context)
169170
}
170171
}
171172

172-
var client = context.GetGraphServiceClient<GraphRbacManagementClient>(environment);
173+
var client = context.GetGraphServiceClient<MicrosoftGraphClient>(environment);
173174
client.TenantID = tenantId;
174175
if (AzureRmProfileProvider.Instance != null &&
175176
AzureRmProfileProvider.Instance.Profile != null &&

src/KeyVault/KeyVault.Test/ScenarioTests/KeyVaultManagementTests.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2424
using Xunit;
2525
using Microsoft.Azure.ServiceManagement.Common.Models;
26+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Users;
27+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications;
28+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications.Models;
2629

2730
namespace Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests
2831
{
@@ -248,8 +251,8 @@ public void TestSetCompoundIdAccessPolicy()
248251
[Trait(Category.AcceptanceType, Category.CheckIn)]
249252
public void TestSetRemoveAccessPolicyBySPN()
250253
{
251-
Application app = null;
252-
ServicePrincipal principal = null;
254+
MicrosoftGraphApplication app = null;
255+
MicrosoftGraphServicePrincipal principal = null;
253256

254257
KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
255258
controller.RunPsTestWorkflow(
@@ -260,9 +263,9 @@ public void TestSetRemoveAccessPolicyBySPN()
260263
app = CreateNewAdApp(controller);
261264
principal = CreateNewAdServicePrincipal(controller, app.AppId);
262265
return new[] { string.Format("{0} {1} {2} {3}", "Test-SetRemoveAccessPolicyBySPN",
263-
_data.PreCreatedVault,
264-
_data.ResourceGroupName,
265-
principal.ServicePrincipalNames.Where(s => s.StartsWith("http")).FirstOrDefault()) };
266+
_data.PreCreatedVault,
267+
_data.ResourceGroupName,
268+
principal.ServicePrincipalNames.Where(s => s.StartsWith("http")).FirstOrDefault()) };
266269
},
267270
// cleanup
268271
() =>
@@ -412,56 +415,53 @@ private string GetUserObjectId(KeyVaultManagementController controllerAdmin, str
412415
{
413416
if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Record)
414417
{
415-
var user = controllerAdmin.GraphClient.Users.Get(upn);
416-
HttpMockServer.Variables["ObjectId"] = user.ObjectId;
417-
return user.ObjectId;
418+
var user = controllerAdmin.GraphClient.Users.GetUser(upn);
419+
HttpMockServer.Variables["ObjectId"] = user.Id;
420+
return user.Id;
418421
}
419422
else
420423
{
421424
return HttpMockServer.Variables["ObjectId"];
422425
}
423426
}
424427

425-
private Application CreateNewAdApp(KeyVaultManagementController controllerAdmin)
428+
private MicrosoftGraphApplication CreateNewAdApp(KeyVaultManagementController controllerAdmin)
426429
{
427430
var appName = TestUtilities.GenerateName("adApplication");
428431
var url = string.Format("http://{0}/home", appName);
429-
var appParam = new ApplicationCreateParameters
432+
var app = new MicrosoftGraphApplication()
430433
{
431-
AvailableToOtherTenants = false,
432434
DisplayName = appName,
433-
Homepage = url,
434-
IdentifierUris = new[] { url },
435-
ReplyUrls = new[] { url }
435+
IdentifierUris = new[] { url }
436436
};
437437

438-
return controllerAdmin.GraphClient.Applications.Create(appParam);
438+
return controllerAdmin.GraphClient.Applications.CreateApplication(app);
439439
}
440440

441-
private ServicePrincipal CreateNewAdServicePrincipal(KeyVaultManagementController controllerAdmin, string appId)
441+
private MicrosoftGraphServicePrincipal CreateNewAdServicePrincipal(KeyVaultManagementController controllerAdmin, string appId)
442442
{
443-
var spParam = new ServicePrincipalCreateParameters
443+
var sp = new MicrosoftGraphServicePrincipal
444444
{
445445
AppId = appId,
446446
AccountEnabled = true
447447
};
448448

449-
return controllerAdmin.GraphClient.ServicePrincipals.Create(spParam);
449+
return controllerAdmin.GraphClient.ServicePrincipals.CreateServicePrincipal(sp);
450450
}
451451

452-
private void DeleteAdApp(KeyVaultManagementController controllerAdmin, Application app)
452+
private void DeleteAdApp(KeyVaultManagementController controllerAdmin, MicrosoftGraphApplication app)
453453
{
454454
if (app != null)
455455
{
456-
controllerAdmin.GraphClient.Applications.Delete(app.ObjectId);
456+
controllerAdmin.GraphClient.Applications.DeleteApplication(app.Id);
457457
}
458458
}
459459

460-
private void DeleteAdServicePrincipal(KeyVaultManagementController controllerAdmin, ServicePrincipal newServicePrincipal)
460+
private void DeleteAdServicePrincipal(KeyVaultManagementController controllerAdmin, MicrosoftGraphServicePrincipal newServicePrincipal)
461461
{
462462
if (newServicePrincipal != null)
463463
{
464-
controllerAdmin.GraphClient.ServicePrincipals.Delete(newServicePrincipal.ObjectId);
464+
controllerAdmin.GraphClient.ServicePrincipals.DeleteServicePrincipal(newServicePrincipal.Id);
465465
}
466466
}
467467
#endregion

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+
* Migrated AAD Graph API to MSGraph API.
2122
* Added a message to `Set-AzKeyVaultAccessPolicy` stating that for the Permissions parameters, using the 'All' option will not include the 'Purge' permission.
2223

2324
## Version 3.6.1

src/KeyVault/KeyVault/Commands/Constants.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public static class Constants
3131
public const string DefaultSoftDeleteRetentionDaysString = "90";
3232

3333
public const string KeyOpsImport = "import";
34-
35-
public const string BreakingChangeMSGraphMigration = @"This cmdlet will use Microsoft Graph in Az 7.x and later.
36-
Visit https://go.microsoft.com/fwlink/?linkid=2174792 for migration guide and breaking changes.";
3734
}
3835

3936
public static class CmdletNoun

src/KeyVault/KeyVault/Commands/GetAzureKeyVault.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414

1515
using System;
1616
using System.Collections;
17-
using System.Collections.Generic;
1817
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.KeyVault.Helpers;
1919
using Microsoft.Azure.Commands.KeyVault.Models;
2020
using Microsoft.Azure.Commands.KeyVault.Properties;
2121
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
22-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2322

2423
namespace Microsoft.Azure.Commands.KeyVault
2524
{
26-
[GenericBreakingChange(Constants.BreakingChangeMSGraphMigration)]
27-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "KeyVault",DefaultParameterSetName = GetVaultParameterSet)]
25+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "KeyVault", DefaultParameterSetName = GetVaultParameterSet)]
2826
[OutputType(typeof(PSKeyVault), typeof(PSKeyVaultIdentityItem), typeof(PSDeletedKeyVault))]
2927
public class GetAzureKeyVault : KeyVaultManagementCmdletBase
3028
{
@@ -94,12 +92,14 @@ public class GetAzureKeyVault : KeyVaultManagementCmdletBase
9492
Mandatory = false,
9593
ParameterSetName = GetVaultParameterSet,
9694
ValueFromPipelineByPropertyName = true,
97-
HelpMessage = "Specifies the key and optional value of the specified tag to filter the list of key vaults by.")]
95+
HelpMessage = "Specifies the key and optional value of the specified tag to filter the list of key vaults by.")]
9896
public Hashtable Tag { get; set; }
9997

10098
#endregion
10199
public override void ExecuteCmdlet()
102100
{
101+
MSGraphMessageHelper.WriteMessageForCmdletsSwallowException(this);
102+
103103
switch (ParameterSetName)
104104
{
105105
case GetVaultParameterSet:
@@ -110,14 +110,14 @@ public override void ExecuteCmdlet()
110110
PSKeyVault vault = KeyVaultManagementClient.GetVault(
111111
VaultName,
112112
ResourceGroupName,
113-
ActiveDirectoryClient);
113+
GraphClient);
114114
WriteObject(FilterByTag(vault, Tag));
115115
}
116116
else
117117
{
118118
WriteObject(TopLevelWildcardFilter(ResourceGroupName, VaultName, ListVaults(ResourceGroupName, Tag)), true);
119119
}
120-
120+
121121
break;
122122

123123
case GetDeletedVaultParameterSet:

src/KeyVault/KeyVault/Commands/GetAzureManagedHsm.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using Microsoft.Azure.Commands.KeyVault.Models;
1+
using Microsoft.Azure.Commands.KeyVault.Helpers;
2+
using Microsoft.Azure.Commands.KeyVault.Models;
23
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
3-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
44
using System.Collections;
55
using System.Management.Automation;
66

77
namespace Microsoft.Azure.Commands.KeyVault.Commands
88
{
9-
[GenericBreakingChange(Constants.BreakingChangeMSGraphMigration)]
109
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "KeyVaultManagedHsm")]
1110
[OutputType(typeof(PSManagedHsm), typeof(PSKeyVaultIdentityItem))]
1211
public class GetAzureManagedHsm : KeyVaultManagementCmdletBase
@@ -58,16 +57,16 @@ public override void ExecuteCmdlet()
5857
PSManagedHsm mhsm = KeyVaultManagementClient.GetManagedHsm(
5958
Name,
6059
ResourceGroupName,
61-
ActiveDirectoryClient);
60+
GraphClient);
6261
WriteObject(FilterByTag(mhsm, Tag));
6362
}
6463
else
65-
{
64+
{
6665
WriteObject(
6766
TopLevelWildcardFilter(
6867
ResourceGroupName, Name,
6968
FilterByTag(
70-
KeyVaultManagementClient.ListManagedHsms(ResourceGroupName, ActiveDirectoryClient), Tag)),
69+
KeyVaultManagementClient.ListManagedHsms(ResourceGroupName, GraphClient), Tag)),
7170
true);
7271
}
7372
}

src/KeyVault/KeyVault/Commands/NewAzureKeyVault.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.KeyVault.Helpers;
1516
using Microsoft.Azure.Commands.KeyVault.Models;
1617
using Microsoft.Azure.Commands.KeyVault.Properties;
1718
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
@@ -27,7 +28,6 @@ namespace Microsoft.Azure.Commands.KeyVault
2728
/// <summary>
2829
/// Create a new key vault.
2930
/// </summary>
30-
[GenericBreakingChange(Constants.BreakingChangeMSGraphMigration)]
3131
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "KeyVault", SupportsShouldProcess = true)]
3232
[OutputType(typeof(PSKeyVault))]
3333
public class NewAzureKeyVault : KeyVaultManagementCmdletBase
@@ -116,6 +116,8 @@ public class NewAzureKeyVault : KeyVaultManagementCmdletBase
116116

117117
public override void ExecuteCmdlet()
118118
{
119+
MSGraphMessageHelper.WriteMessageForCmdletsSwallowException(this);
120+
119121
if (ShouldProcess(Name, Properties.Resources.CreateKeyVault))
120122
{
121123
if (VaultExistsInCurrentSubscription(Name))
@@ -178,7 +180,7 @@ public override void ExecuteCmdlet()
178180
NetworkAcls = new NetworkRuleSet(), // New key-vault takes in default network rule set
179181
Tags = this.Tag
180182
},
181-
ActiveDirectoryClient,
183+
GraphClient,
182184
NetworkRuleSet);
183185

184186
this.WriteObject(newVault);

src/KeyVault/KeyVault/Commands/NewAzureManagedHsm.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Microsoft.Azure.Commands.KeyVault.Models;
1+
using Microsoft.Azure.Commands.KeyVault.Helpers;
2+
using Microsoft.Azure.Commands.KeyVault.Models;
23
using Microsoft.Azure.Commands.KeyVault.Properties;
34
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
4-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
55
using System;
66
using System.Collections;
77
using System.Management.Automation;
@@ -11,7 +11,6 @@ namespace Microsoft.Azure.Commands.KeyVault.Commands
1111
/// <summary>
1212
/// Create a new managed HSM.
1313
/// </summary>
14-
[GenericBreakingChange(Constants.BreakingChangeMSGraphMigration)]
1514
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "KeyVaultManagedHsm", SupportsShouldProcess = true)]
1615
[OutputType(typeof(PSManagedHsm))]
1716
public class NewAzureManagedHsm : KeyVaultManagementCmdletBase
@@ -97,7 +96,7 @@ public override void ExecuteCmdlet()
9796
SkuFamilyName = DefaultManagedHsmSkuFamily
9897
};
9998

100-
this.WriteObject(KeyVaultManagementClient.CreateNewManagedHsm(vaultCreationParameter, ActiveDirectoryClient));
99+
this.WriteObject(KeyVaultManagementClient.CreateNewManagedHsm(vaultCreationParameter, GraphClient));
101100
}
102101
}
103102

src/KeyVault/KeyVault/Commands/RBAC/GetAzureManagedHsmRoleAssignment.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications;
16+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications.Models;
17+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Users;
18+
using Microsoft.Azure.Commands.KeyVault.Helpers;
1519
using Microsoft.Azure.Commands.KeyVault.Models;
1620
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17-
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
18-
using Microsoft.Azure.Graph.RBAC.Version1_6.Models;
1921
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2022
using System;
2123
using System.Linq;
@@ -74,6 +76,8 @@ public class GetAzureManagedHsmRoleAssignment : RbacCmdletBase
7476

7577
public override void ExecuteCmdlet()
7678
{
79+
MSGraphMessageHelper.WriteMessageForCmdletsSwallowException(this);
80+
7781
switch (ParameterSetName)
7882
{
7983
case ListParameterSet:
@@ -111,15 +115,14 @@ private PSKeyVaultRoleAssignment[] FilterAssignments(PSKeyVaultRoleAssignment[]
111115
}
112116
if (!string.IsNullOrEmpty(SignInName))
113117
{
114-
var filter = new ADObjectFilterOptions() { UPN = SignInName };
115-
var user = ActiveDirectoryClient.FilterUsers(filter).FirstOrDefault();
116-
ObjectId = user?.Id.ToString();
118+
var user = GraphClient.Users.GetUser(SignInName);
119+
ObjectId = user?.Id;
117120
}
118121
if (!string.IsNullOrEmpty(ApplicationId))
119122
{
120-
var odataQuery = new Rest.Azure.OData.ODataQuery<Application>(s => string.Equals(s.AppId, ApplicationId, StringComparison.OrdinalIgnoreCase));
121-
var app = ActiveDirectoryClient.GetApplicationWithFilters(odataQuery).FirstOrDefault();
122-
ObjectId = app?.ObjectId.ToString();
123+
var filter = ODataHelper.FormatFilterString<MicrosoftGraphServicePrincipal>(sp => sp.AppId == ApplicationId);
124+
var servicePrincipal = GraphClient.ServicePrincipals.ListServicePrincipal(filter: filter).Value.FirstOrDefault();
125+
ObjectId = servicePrincipal?.Id;
123126
}
124127
if (!string.IsNullOrEmpty(RoleDefinitionId))
125128
{

0 commit comments

Comments
 (0)