Skip to content

Commit 6eed678

Browse files
authored
Merge pull request Azure#10057 from SanjaMalesevic/addPSCommandsForAadAdmin
Adding new Powershell cmdlets to support set/get/remove of Azure AD administrator on MI
2 parents 5960226 + 1ce0a90 commit 6eed678

21 files changed

+5747
-4
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.ScenarioTest.SqlTests;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
using Xunit.Abstractions;
19+
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
20+
21+
namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
22+
{
23+
public class ManagedInstanceActiveDirectoryAdministratorTests : SqlTestsBase
24+
{
25+
public ManagedInstanceActiveDirectoryAdministratorTests(ITestOutputHelper output) : base(output)
26+
{
27+
}
28+
29+
protected override void SetupManagementClients(RestTestFramework.MockContext context)
30+
{
31+
var newResourcesClient = GetResourcesClient(context);
32+
var sqlClient = GetSqlClient(context);
33+
var networkClient = GetNetworkClient(context);
34+
var graphClient = GetGraphClientVersion1_6(context);
35+
Helper.SetupSomeOfManagementClients(newResourcesClient,sqlClient, networkClient, graphClient);
36+
}
37+
38+
[Fact]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestManagedInstanceActiveDirectoryAdministrator()
41+
{
42+
RunPowerShellTest("Test-ManagedInstanceActiveDirectoryAdministrator");
43+
}
44+
}
45+
}
46+
47+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
<#
16+
.SYNOPSIS
17+
Tests for managing Active Directory Administrator on managed instance
18+
#>
19+
function Test-ManagedInstanceActiveDirectoryAdministrator
20+
{
21+
# Setup
22+
$rg = Create-ResourceGroupForTest
23+
$vnetName = "cl_initial"
24+
$subnetName = "Cool"
25+
26+
# Setup VNET
27+
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
28+
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id
29+
30+
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId
31+
32+
# If there is a need to re-record this test, these values must be changed to correspond to existing group and user from Azure Active Directory related to current subscription.
33+
$activeDirectoryGroup1 = "aadadmin"
34+
$activeDirectoryGroup1ObjectId = "52b6d571-5ff9-4b8f-92de-4a5b1bcdbbef"
35+
$activeDirectoryUser1 = "CL AAD Test User"
36+
$activeDirectoryUser1ObjectId = "034bb7d9-ca26-4c6f-abe0-4aff74fdca50"
37+
38+
try
39+
{
40+
# Verify there is no Active Directory Administrator set
41+
$activeDirectoryAdmin = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
42+
43+
Assert-Null $activeDirectoryAdmin
44+
45+
# Set an Active Directory Administrator Group on Managed Instance
46+
# This command uses the Graph API to check if there is a user/group for provided DisplayName and ObjectId. Graph authentication blocks test passes, so if you need to record this test again, you must provide real token in
47+
# MockTokenAuthenticationFactory constructor and change SetAuthenticationFactory in EnvironmentSetupHelper.
48+
$activeDirectoryAdmin1 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryGroup1 -ObjectId $activeDirectoryGroup1ObjectId
49+
50+
Assert-NotNull $activeDirectoryAdmin1
51+
52+
# Verify the correct Active Directory Administrator is set
53+
Assert-AreEqual $activeDirectoryAdmin1.DisplayName $activeDirectoryGroup1
54+
Assert-AreEqual $activeDirectoryAdmin1.ObjectId $activeDirectoryGroup1ObjectId
55+
56+
# Get an Active Directory Administrator
57+
$activeDirectoryAdmin2 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
58+
59+
Assert-AreEqual $activeDirectoryAdmin2.DisplayName $activeDirectoryGroup1
60+
Assert-AreEqual $activeDirectoryAdmin2.ObjectId $activeDirectoryGroup1ObjectId
61+
62+
# Set an Active Directory Administrator User on Managed Instance
63+
$activeDirectoryAdmin3 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryUser1 -ObjectId $activeDirectoryUser1ObjectId
64+
65+
Assert-AreEqual $activeDirectoryAdmin3.DisplayName $activeDirectoryUser1
66+
Assert-AreEqual $activeDirectoryAdmin3.ObjectId $activeDirectoryUser1ObjectId
67+
68+
# Remove an Active Directory Administrator User from Managed Instance
69+
$activeDirectoryAdmin4 = Remove-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -Force
70+
71+
# Verify that Active Directory Administrator was deleted
72+
$activeDirectoryAdmin5 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
73+
74+
Assert-Null $activeDirectoryAdmin5
75+
}
76+
finally
77+
{
78+
Remove-ResourceGroupForTest $rg
79+
}
80+
}

src/Sql/Sql.Test/ScenarioTests/SqlTestsBase.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
using Microsoft.Azure.Management.OperationalInsights;
3232
using SDKMonitor = Microsoft.Azure.Management.Monitor;
3333
using CommonMonitor = Microsoft.Azure.Management.Monitor.Version2018_09_01;
34-
using Microsoft.Azure.Graph.RBAC;
3534
using Microsoft.Azure.Management.KeyVault;
35+
using Microsoft.Azure.Graph.RBAC;
36+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
3637

3738
namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests
3839
{
3940
public class SqlTestsBase : RMTestBase
4041
{
4142
protected EnvironmentSetupHelper Helper;
4243
protected string[] resourceTypesToIgnoreApiVersion;
44+
private const string TenantIdKey = "TenantId";
4345

4446
protected SqlTestsBase(ITestOutputHelper output)
4547
{
@@ -84,8 +86,8 @@ protected void RunPowerShellTest(params string[] scripts)
8486
// Enable undo functionality as well as mock recording
8587
using (var context = MockContext.Start(callingClassType, mockName))
8688
{
87-
SetupManagementClients(context);
8889
Helper.SetupEnvironment(AzureModule.AzureResourceManager);
90+
SetupManagementClients(context);
8991
Helper.SetupModules(AzureModule.AzureResourceManager,
9092
"ScenarioTests\\Common.ps1",
9193
"ScenarioTests\\" + GetType().Name + ".ps1",
@@ -140,6 +142,35 @@ protected GraphRbacManagementClient GetGraphClient(MockContext context)
140142
return graphClient;
141143
}
142144

145+
protected Microsoft.Azure.Graph.RBAC.Version1_6.GraphRbacManagementClient GetGraphClientVersion1_6(MockContext context)
146+
{
147+
Microsoft.Azure.Graph.RBAC.Version1_6.GraphRbacManagementClient graphClient = context.GetServiceClient<Microsoft.Azure.Graph.RBAC.Version1_6.GraphRbacManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
148+
graphClient.BaseUri = TestEnvironmentFactory.GetTestEnvironment().Endpoints.GraphUri;
149+
string tenantId = null;
150+
151+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
152+
{
153+
tenantId = TestEnvironmentFactory.GetTestEnvironment().Tenant;
154+
HttpMockServer.Variables[TenantIdKey] = tenantId;
155+
}
156+
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
157+
{
158+
if (HttpMockServer.Variables.ContainsKey(TenantIdKey))
159+
{
160+
tenantId = HttpMockServer.Variables[TenantIdKey];
161+
}
162+
}
163+
graphClient.TenantID = tenantId;
164+
if (AzureRmProfileProvider.Instance != null &&
165+
AzureRmProfileProvider.Instance.Profile != null &&
166+
AzureRmProfileProvider.Instance.Profile.DefaultContext != null &&
167+
AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant != null)
168+
{
169+
AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id = tenantId;
170+
}
171+
return graphClient;
172+
}
173+
143174
protected KeyVaultManagementClient GetKeyVaultClient(MockContext context)
144175
{
145176
return context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());

0 commit comments

Comments
 (0)