Skip to content

Commit 0ec5b3f

Browse files
Move SQL Server and SQL Instance from AAD Client to MSGraph Client (#18944)
* Move SQL Server and SQL Instance from ActiveDirectoryClient to MicrosoftGraphClient * Update src/Sql/Sql/ChangeLog.md Co-authored-by: Beisi Zhou <[email protected]>
1 parent 404ae3b commit 0ec5b3f

File tree

4 files changed

+64
-79
lines changed

4 files changed

+64
-79
lines changed

src/Sql/Sql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020
## Upcoming Release
2121
* Removed the warning messages for MSGraph migration [#18856]
22+
* Moved SQL Server and SQL Instance from ActiveDirectoryClient to MicrosoftGraphClient
2223

2324
## Version 3.10.0
2425
* Added `GeoZone` option to `BackupStorageRedundancy` parameter to `New-AzSqlDatabase`, `Set-AzSqlDatabase`, `New-AzSqlDatabaseCopy`, `New-AzSqlDatabaseSecondary`, and `Restore-AzSqlDatabase` to enable create, update, copy, geo secondary and PITR support for GeoZone hyperscale databases

src/Sql/Sql/ManagedInstance/Services/AzureSqlManagedInstanceAdapter.cs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Linq;
20-
using System.Runtime.InteropServices;
21-
using System.Security;
22-
using System.Security.Permissions;
2320
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
2421
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2522
using Microsoft.Azure.Commands.Sql.Common;
2623
using Microsoft.WindowsAzure.Commands.Common;
2724
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2825
using Microsoft.Azure.Management.Sql.Models;
29-
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
30-
using Microsoft.Azure.Graph.RBAC.Version1_6.Models;
26+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0;
27+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Groups.Models;
28+
using Microsoft.Azure.Commands.Common.Authentication;
29+
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0.Applications.Models;
30+
3131
using Microsoft.Rest.Azure.OData;
32-
using ServicePrincipal = Microsoft.Azure.Graph.RBAC.Version1_6.Models.ServicePrincipal;
3332

3433
namespace Microsoft.Azure.Commands.Sql.ManagedInstance.Adapter
3534
{
@@ -49,30 +48,25 @@ public class AzureSqlManagedInstanceAdapter
4948
public IAzureContext Context { get; set; }
5049

5150
/// <summary>
52-
/// A private instance of ActiveDirectoryClient
51+
/// A private instance of MicrosoftGraphClient
5352
/// </summary>
54-
private ActiveDirectoryClient _activeDirectoryClient;
53+
private MicrosoftGraphClient _microsoftGraphClient;
5554

5655
/// <summary>
57-
/// Gets or sets the Azure ActiveDirectoryClient instance
56+
/// Gets or sets the Azure MicrosoftGraphClient instance
5857
/// </summary>
59-
public ActiveDirectoryClient ActiveDirectoryClient
58+
public MicrosoftGraphClient MicrosoftGraphClient
6059
{
6160
get
6261
{
63-
if (_activeDirectoryClient == null)
62+
if (_microsoftGraphClient == null)
6463
{
65-
_activeDirectoryClient = new ActiveDirectoryClient(Context);
66-
if (!Context.Environment.IsEndpointSet(AzureEnvironment.Endpoint.Graph))
67-
{
68-
throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.InvalidGraphEndpoint));
69-
}
70-
_activeDirectoryClient = new ActiveDirectoryClient(Context);
64+
_microsoftGraphClient = AzureSession.Instance.ClientFactory.CreateArmClient<MicrosoftGraphClient>(Context, AzureEnvironment.Endpoint.Graph);
65+
_microsoftGraphClient.TenantID = Context.Tenant.Id;
7166
}
72-
return this._activeDirectoryClient;
67+
return _microsoftGraphClient;
7368
}
74-
75-
set { this._activeDirectoryClient = value; }
69+
set { _microsoftGraphClient = value; }
7670
}
7771

7872
/// <summary>
@@ -317,18 +311,18 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
317311
Guid tenantId = GetTenantId();
318312

319313
// Check for a Azure Active Directory group. Recommended to always use group.
320-
IEnumerable<PSADGroup> groupList = null;
321-
PSADGroup group = null;
314+
IEnumerable<MicrosoftGraphGroup> groupList = null;
315+
MicrosoftGraphGroup group = null;
322316

323-
var filter = new ADObjectFilterOptions()
317+
var filter = new MicrosoftObjectFilterOptions()
324318
{
325319
Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
326320
SearchString = displayName,
327321
Paging = true,
328322
};
329323

330324
// Get a list of groups from Azure Active Directory
331-
groupList = ActiveDirectoryClient.FilterGroups(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));
325+
groupList = MicrosoftGraphClient.FilterGroups(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));
332326

333327
if (groupList != null && groupList.Count() > 1)
334328
{
@@ -348,19 +342,19 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
348342
}
349343

350344
// Lookup for serviceprincipals
351-
ODataQuery<ServicePrincipal> odataQueryFilter;
345+
ODataQuery<MicrosoftGraphServicePrincipal> odataQueryFilter;
352346

353347
if ((objectId != null && objectId != Guid.Empty))
354348
{
355349
var applicationIdString = objectId.ToString();
356-
odataQueryFilter = new Rest.Azure.OData.ODataQuery<ServicePrincipal>(a => a.AppId == applicationIdString);
350+
odataQueryFilter = new Rest.Azure.OData.ODataQuery<MicrosoftGraphServicePrincipal>(a => a.AppId == applicationIdString);
357351
}
358352
else
359353
{
360-
odataQueryFilter = new Rest.Azure.OData.ODataQuery<ServicePrincipal>(a => a.DisplayName == displayName);
354+
odataQueryFilter = new Rest.Azure.OData.ODataQuery<MicrosoftGraphServicePrincipal>(a => a.DisplayName == displayName);
361355
}
362356

363-
var servicePrincipalList = ActiveDirectoryClient.FilterServicePrincipals(odataQueryFilter);
357+
var servicePrincipalList = MicrosoftGraphClient.FilterServicePrincipals(odataQueryFilter);
364358

365359
if (servicePrincipalList != null && servicePrincipalList.Count() > 1)
366360
{
@@ -370,7 +364,7 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
370364
else if (servicePrincipalList != null && servicePrincipalList.Count() == 1)
371365
{
372366
// Only one user was found. Get the user display name and object id
373-
PSADServicePrincipal app = servicePrincipalList.First();
367+
MicrosoftGraphServicePrincipal app = servicePrincipalList.First();
374368

375369
if (displayName != null && string.CompareOrdinal(displayName, app.DisplayName) != 0)
376370
{
@@ -385,7 +379,7 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
385379
return new ManagedInstanceExternalAdministrator()
386380
{
387381
Login = displayName,
388-
Sid = app.ApplicationId,
382+
Sid = new Guid(app.AppId),
389383
TenantId = tenantId,
390384
PrincipalType = "Application",
391385
AzureADOnlyAuthentication = adOnlyAuth
@@ -397,50 +391,50 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
397391
return new ManagedInstanceExternalAdministrator()
398392
{
399393
Login = group.DisplayName,
400-
Sid = group.Id,
394+
Sid = new Guid(group.Id),
401395
TenantId = tenantId,
402396
PrincipalType = "Group",
403397
AzureADOnlyAuthentication = adOnlyAuth
404398
};
405399
}
406400

407401
// No group or service principal was found. Check for a user
408-
filter = new ADObjectFilterOptions()
402+
filter = new MicrosoftObjectFilterOptions()
409403
{
410404
Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
411405
SearchString = displayName,
412406
Paging = true,
413407
};
414408

415409
// Get a list of user from Azure Active Directory
416-
var userList = ActiveDirectoryClient.FilterUsers(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));
410+
var userList = MicrosoftGraphClient.FilterUsers(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));
417411

418412
// No user was found. Check if the display name is a UPN
419413
if (userList == null || userList.Count() == 0)
420414
{
421415
// Check if the display name is the UPN
422-
filter = new ADObjectFilterOptions()
416+
filter = new MicrosoftObjectFilterOptions()
423417
{
424418
Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
425419
UPN = displayName,
426420
Paging = true,
427421
};
428422

429-
userList = ActiveDirectoryClient.FilterUsers(filter).Where(gr => string.Equals(gr.UserPrincipalName, displayName, StringComparison.OrdinalIgnoreCase));
423+
userList = MicrosoftGraphClient.FilterUsers(filter).Where(gr => string.Equals(gr.UserPrincipalName, displayName, StringComparison.OrdinalIgnoreCase));
430424
}
431425

432426
// No user was found. Check if the display name is a guest user.
433427
if (userList == null || userList.Count() == 0)
434428
{
435429
// Check if the display name is the UPN
436-
filter = new ADObjectFilterOptions()
430+
filter = new MicrosoftObjectFilterOptions()
437431
{
438432
Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
439433
Mail = displayName,
440434
Paging = true,
441435
};
442436

443-
userList = ActiveDirectoryClient.FilterUsers(filter);
437+
userList = MicrosoftGraphClient.FilterUsers(filter);
444438
}
445439

446440
// No user was found
@@ -461,7 +455,7 @@ protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(Man
461455
return new ManagedInstanceExternalAdministrator()
462456
{
463457
Login = displayName,
464-
Sid = obj.Id,
458+
Sid = new Guid(obj.Id),
465459
TenantId = tenantId,
466460
PrincipalType = "User",
467461
AzureADOnlyAuthentication = adOnlyAuth

src/Sql/Sql/ManagedInstance/Services/AzureSqlManagedInstanceCommunicator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17-
using Microsoft.Azure.Commands.Common.Authentication.Models;
18-
using Microsoft.Azure.Commands.Sql.Common;
1917
using Microsoft.Azure.Management.Internal.Resources;
2018
using Microsoft.Azure.Management.Sql;
21-
using Microsoft.Azure.Management.Sql.Models;
22-
using System;
2319
using System.Collections.Generic;
2420
using System.Linq;
2521

0 commit comments

Comments
 (0)