Skip to content

Commit 85f2f47

Browse files
committed
Merge pull request #759 from j82w/dev
Add SQL Server Active Directory administrator Cmdlets
2 parents d2421d0 + 3f8df93 commit 85f2f47

22 files changed

+3507
-11
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/Commands.Sql.Test.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@
6767
<Reference Include="Microsoft.Azure.Gallery">
6868
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
6969
</Reference>
70+
<Reference Include="Microsoft.Azure.Graph.RBAC">
71+
<SpecificVersion>False</SpecificVersion>
72+
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
73+
</Reference>
7074
<Reference Include="Microsoft.Azure.Management.Sql">
71-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.35.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
75+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.36.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
7276
</Reference>
7377
<Reference Include="Microsoft.Azure.Management.Storage">
7478
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
@@ -188,6 +192,9 @@
188192
<Compile Include="ScenarioTests\DatabaseBackupTests.cs" />
189193
<Compile Include="ScenarioTests\DatabaseCrudTests.cs" />
190194
<Compile Include="ScenarioTests\DataMaskingTests.cs" />
195+
<None Include="ScenarioTests\ServerActiveDirectoryAdministratorTest.ps1">
196+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
197+
</None>
191198
<None Include="ScenarioTests\ServiceTierAdvisorTests.ps1">
192199
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
193200
</None>
@@ -203,12 +210,14 @@
203210
<None Include="ScenarioTests\ElasticPoolCrudTests.ps1">
204211
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
205212
</None>
213+
<Compile Include="ScenarioTests\ServerActiveDirectoryAdministratorTest.cs" />
206214
<Compile Include="ScenarioTests\ServiceTierAdvisorTests.cs" />
207215
<Compile Include="ScenarioTests\RecommendedElasticPoolTests.cs" />
208216
<Compile Include="ScenarioTests\ElasticPoolCrudTests.cs" />
209217
<Compile Include="ScenarioTests\AuditingTests.cs" />
210218
<Compile Include="ScenarioTests\ServerCrudTests.cs" />
211219
<Compile Include="ScenarioTests\ServerUpgradeTests.cs" />
220+
<Compile Include="ScenarioTests\SqlEvnSetupHelper.cs" />
212221
<Compile Include="ScenarioTests\SqlTestsBase.cs" />
213222
<Compile Include="ScenarioTests\TransparentDataEncryptionCrudTests.cs" />
214223
<Compile Include="ScenarioTests\IndexRecommendationTests.cs" />
@@ -434,6 +443,9 @@
434443
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.RecommendedElasticPoolTests\ListRecommendedElasticPools.json">
435444
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
436445
</None>
446+
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerActiveDirectoryAdministratorTest\TestServerActiveDirectoryAdministratorCRUD.json">
447+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
448+
</None>
437449
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerCrudTests\TestServerCreate.json">
438450
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
439451
</None>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
19+
namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
20+
{
21+
public class ServerActiveDirectoryAdministratorTest : SqlTestsBase
22+
{
23+
[Fact]
24+
[Trait(Category.Sql, Category.CheckIn)]
25+
public void TestServerActiveDirectoryAdministratorCRUD()
26+
{
27+
RunPowerShellTest("Test-ServerActiveDirectoryAdministrator");
28+
}
29+
}
30+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 creating a database
18+
#>
19+
function Test-ServerActiveDirectoryAdministrator ($location = "North Europe")
20+
{
21+
# Setup
22+
$rg = Create-ResourceGroupForTest
23+
$server = Create-ServerForTest $rg "12.0" $location
24+
25+
try
26+
{
27+
$activeDirectoryGroup1 = "testAADaccount"
28+
$activeDirectoryGroup1ObjectId = "41732a4a-e09e-4b18-9624-38e252d68bbf"
29+
$activeDirectoryUser1 = "Test User 2"
30+
$activeDirectoryUser1ObjectId = "e87332b2-e3ed-480a-9723-e9b3611268f8"
31+
32+
# Verify there is no Active Directory Administrator set
33+
$activeDirectoryAdmin = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName
34+
35+
Assert-Null $activeDirectoryAdmin
36+
37+
# Set an Azure SQL Server Active Directory Administrator Group
38+
$activeDirectoryAdmin1 = Set-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName `
39+
-DisplayName $activeDirectoryGroup1
40+
41+
Assert-NotNull $activeDirectoryAdmin1
42+
43+
# Verify the correct Active Directory Administrator is set
44+
Assert-AreEqual $activeDirectoryAdmin1.DisplayName $activeDirectoryGroup1
45+
Assert-AreEqual $activeDirectoryAdmin1.ObjectId $activeDirectoryGroup1ObjectId
46+
47+
# Get an Azure SQL Server Active Directory Administrator
48+
$activeDirectoryAdmin2 = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName
49+
50+
Assert-AreEqual $activeDirectoryAdmin2.DisplayName $activeDirectoryGroup1
51+
Assert-AreEqual $activeDirectoryAdmin2.ObjectId $activeDirectoryGroup1ObjectId
52+
53+
# Set an Azure SQL Server Active Directory Administrator User
54+
$activeDirectoryAdmin3 = Set-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName `
55+
-DisplayName $activeDirectoryUser1
56+
57+
Assert-AreEqual $activeDirectoryAdmin3.DisplayName $activeDirectoryUser1
58+
Assert-AreEqual $activeDirectoryAdmin3.ObjectId $activeDirectoryUser1ObjectId
59+
60+
# Set an Azure SQL Server Active Directory Administrator User
61+
$activeDirectoryAdmin4 = Remove-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -Force
62+
63+
# Verify that Azure SQL Server Active Directory Administrator was deleted
64+
$activeDirectoryAdmin5 = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName
65+
66+
Assert-Null $activeDirectoryAdmin5
67+
}
68+
finally
69+
{
70+
Remove-ResourceGroupForTest $rg
71+
}
72+
}
73+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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.Linq;
18+
using Microsoft.Azure.Common.Authentication;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.Azure.Gallery;
21+
using Microsoft.Azure.Graph.RBAC;
22+
using Microsoft.Azure.Management.Authorization;
23+
using Microsoft.Azure.Management.Resources;
24+
using Microsoft.Azure.Subscriptions;
25+
using Microsoft.Azure.Test;
26+
using Microsoft.Azure.Test.HttpRecorder;
27+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
28+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
29+
30+
namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests
31+
{
32+
public class SqlEvnSetupHelper : EnvironmentSetupHelper
33+
{
34+
/// <summary>
35+
/// This overrides the default subscription and default account. This allows the
36+
/// test to get the tenant id in the test.
37+
/// </summary>
38+
public void SetupEnvironment()
39+
{
40+
base.SetupEnvironment(AzureModule.AzureResourceManager);
41+
42+
TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
43+
44+
if (csmEnvironment.SubscriptionId != null)
45+
{
46+
//Overwrite the default subscription and default account
47+
//with ones using user ID and tenant ID from auth context
48+
var user = GetUser(csmEnvironment);
49+
var tenantId = GetTenantId(csmEnvironment);
50+
51+
// Existing test will not have a user or tenant id set
52+
if (tenantId != null && user != null)
53+
{
54+
var testSubscription = new AzureSubscription()
55+
{
56+
Id = new Guid(csmEnvironment.SubscriptionId),
57+
Name = ProfileClient.Profile.DefaultSubscription.Name,
58+
Environment = ProfileClient.Profile.DefaultSubscription.Environment,
59+
Account = user,
60+
Properties = new Dictionary<AzureSubscription.Property, string>
61+
{
62+
{
63+
AzureSubscription.Property.Default, "True"
64+
},
65+
{
66+
AzureSubscription.Property.StorageAccount,
67+
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT")
68+
},
69+
{
70+
AzureSubscription.Property.Tenants, tenantId
71+
},
72+
}
73+
};
74+
75+
var testAccount = new AzureAccount()
76+
{
77+
Id = user,
78+
Type = AzureAccount.AccountType.User,
79+
Properties = new Dictionary<AzureAccount.Property, string>
80+
{
81+
{
82+
AzureAccount.Property.Subscriptions, csmEnvironment.SubscriptionId
83+
},
84+
}
85+
};
86+
87+
ProfileClient.Profile.Accounts.Remove(ProfileClient.Profile.DefaultSubscription.Account);
88+
ProfileClient.Profile.Subscriptions[testSubscription.Id] = testSubscription;
89+
ProfileClient.Profile.Accounts[testAccount.Id] = testAccount;
90+
ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.Account);
91+
92+
ProfileClient.Profile.Save();
93+
}
94+
}
95+
}
96+
97+
/// <summary>
98+
/// Helper function to get the tenant id if it was set in the test
99+
/// </summary>
100+
/// <param name="environment">Test environment</param>
101+
/// <returns>The tenant id or null if not tenant id could be found.</returns>
102+
private string GetTenantId(TestEnvironment environment)
103+
{
104+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
105+
{
106+
HttpMockServer.Variables["TenantId"] = environment.AuthorizationContext.TenantId;
107+
return environment.AuthorizationContext.TenantId;
108+
}
109+
else
110+
{
111+
if (HttpMockServer.Variables.ContainsKey("TenantId"))
112+
{
113+
return HttpMockServer.Variables["TenantId"];
114+
}
115+
else
116+
{
117+
return null;
118+
}
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Helper function to get the user id if it was set in the test
124+
/// </summary>
125+
/// <param name="environment">Test environment</param>
126+
/// <returns>The user id or null if not tenant id could be found.</returns>
127+
private string GetUser(TestEnvironment environment)
128+
{
129+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
130+
{
131+
HttpMockServer.Variables["User"] = environment.AuthorizationContext.UserId;
132+
return environment.AuthorizationContext.UserId;
133+
}
134+
else
135+
{
136+
if (HttpMockServer.Variables.ContainsKey("User"))
137+
{
138+
return HttpMockServer.Variables["User"];
139+
}
140+
else
141+
{
142+
return null;
143+
}
144+
145+
}
146+
}
147+
148+
}
149+
}

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,26 @@
1919
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2020
using Microsoft.WindowsAzure.Management.Storage;
2121
using Microsoft.Azure.Test;
22+
using Microsoft.Azure.Graph.RBAC;
2223
using Microsoft.Azure.Common.Authentication;
2324
using Microsoft.Azure.Management.Authorization;
25+
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
26+
using System;
2427

2528
namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests
2629
{
2730
public class SqlTestsBase
2831
{
29-
protected EnvironmentSetupHelper helper;
32+
protected SqlEvnSetupHelper helper;
33+
34+
private const string TenantIdKey = "TenantId";
35+
private const string DomainKey = "Domain";
36+
37+
public string UserDomain { get; private set; }
3038

3139
protected SqlTestsBase()
3240
{
33-
helper = new EnvironmentSetupHelper();
41+
helper = new SqlEvnSetupHelper();
3442
}
3543

3644
protected virtual void SetupManagementClients()
@@ -39,7 +47,8 @@ protected virtual void SetupManagementClients()
3947
var storageClient = GetStorageClient();
4048
var resourcesClient = GetResourcesClient();
4149
var authorizationClient = GetAuthorizationManagementClient();
42-
helper.SetupSomeOfManagementClients(sqlCSMClient, storageClient, resourcesClient, authorizationClient);
50+
var graphClient = GetGraphClient();
51+
helper.SetupSomeOfManagementClients(sqlCSMClient, storageClient, resourcesClient, authorizationClient, graphClient);
4352
}
4453

4554
protected void RunPowerShellTest(params string[] scripts)
@@ -53,7 +62,7 @@ protected void RunPowerShellTest(params string[] scripts)
5362

5463
SetupManagementClients();
5564

56-
helper.SetupEnvironment(AzureModule.AzureResourceManager);
65+
helper.SetupEnvironment();
5766

5867
helper.SetupModules(AzureModule.AzureProfile, "ScenarioTests\\Common.ps1",
5968
"ScenarioTests\\" + this.GetType().Name + ".ps1");
@@ -105,5 +114,34 @@ protected AuthorizationManagementClient GetAuthorizationManagementClient()
105114
}
106115
return client;
107116
}
117+
118+
protected GraphRbacManagementClient GetGraphClient()
119+
{
120+
var testFactory = new CSMTestEnvironmentFactory();
121+
var environment = testFactory.GetTestEnvironment();
122+
string tenantId = Guid.Empty.ToString();
123+
124+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
125+
{
126+
tenantId = environment.AuthorizationContext.TenantId;
127+
UserDomain = environment.AuthorizationContext.UserDomain;
128+
129+
HttpMockServer.Variables[TenantIdKey] = tenantId;
130+
HttpMockServer.Variables[DomainKey] = UserDomain;
131+
}
132+
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
133+
{
134+
if (HttpMockServer.Variables.ContainsKey(TenantIdKey))
135+
{
136+
tenantId = HttpMockServer.Variables[TenantIdKey];
137+
}
138+
if (HttpMockServer.Variables.ContainsKey(DomainKey))
139+
{
140+
UserDomain = HttpMockServer.Variables[DomainKey];
141+
}
142+
}
143+
144+
return TestBase.GetGraphServiceClient<GraphRbacManagementClient>(testFactory, tenantId);
145+
}
108146
}
109147
}

0 commit comments

Comments
 (0)