-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #759 from j82w/dev
Add SQL Server Active Directory administrator Cmdlets
- Loading branch information
Showing
22 changed files
with
3,507 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...urceManager/Sql/Commands.Sql.Test/ScenarioTests/ServerActiveDirectoryAdministratorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Azure.Commands.ScenarioTest.SqlTests; | ||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests | ||
{ | ||
public class ServerActiveDirectoryAdministratorTest : SqlTestsBase | ||
{ | ||
[Fact] | ||
[Trait(Category.Sql, Category.CheckIn)] | ||
public void TestServerActiveDirectoryAdministratorCRUD() | ||
{ | ||
RunPowerShellTest("Test-ServerActiveDirectoryAdministrator"); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...rceManager/Sql/Commands.Sql.Test/ScenarioTests/ServerActiveDirectoryAdministratorTest.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# ---------------------------------------------------------------------------------- | ||
# | ||
# Copyright Microsoft Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ---------------------------------------------------------------------------------- | ||
|
||
<# | ||
.SYNOPSIS | ||
Tests creating a database | ||
#> | ||
function Test-ServerActiveDirectoryAdministrator ($location = "North Europe") | ||
{ | ||
# Setup | ||
$rg = Create-ResourceGroupForTest | ||
$server = Create-ServerForTest $rg "12.0" $location | ||
|
||
try | ||
{ | ||
$activeDirectoryGroup1 = "testAADaccount" | ||
$activeDirectoryGroup1ObjectId = "41732a4a-e09e-4b18-9624-38e252d68bbf" | ||
$activeDirectoryUser1 = "Test User 2" | ||
$activeDirectoryUser1ObjectId = "e87332b2-e3ed-480a-9723-e9b3611268f8" | ||
|
||
# Verify there is no Active Directory Administrator set | ||
$activeDirectoryAdmin = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName | ||
|
||
Assert-Null $activeDirectoryAdmin | ||
|
||
# Set an Azure SQL Server Active Directory Administrator Group | ||
$activeDirectoryAdmin1 = Set-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName ` | ||
-DisplayName $activeDirectoryGroup1 | ||
|
||
Assert-NotNull $activeDirectoryAdmin1 | ||
|
||
# Verify the correct Active Directory Administrator is set | ||
Assert-AreEqual $activeDirectoryAdmin1.DisplayName $activeDirectoryGroup1 | ||
Assert-AreEqual $activeDirectoryAdmin1.ObjectId $activeDirectoryGroup1ObjectId | ||
|
||
# Get an Azure SQL Server Active Directory Administrator | ||
$activeDirectoryAdmin2 = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName | ||
|
||
Assert-AreEqual $activeDirectoryAdmin2.DisplayName $activeDirectoryGroup1 | ||
Assert-AreEqual $activeDirectoryAdmin2.ObjectId $activeDirectoryGroup1ObjectId | ||
|
||
# Set an Azure SQL Server Active Directory Administrator User | ||
$activeDirectoryAdmin3 = Set-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName ` | ||
-DisplayName $activeDirectoryUser1 | ||
|
||
Assert-AreEqual $activeDirectoryAdmin3.DisplayName $activeDirectoryUser1 | ||
Assert-AreEqual $activeDirectoryAdmin3.ObjectId $activeDirectoryUser1ObjectId | ||
|
||
# Set an Azure SQL Server Active Directory Administrator User | ||
$activeDirectoryAdmin4 = Remove-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -Force | ||
|
||
# Verify that Azure SQL Server Active Directory Administrator was deleted | ||
$activeDirectoryAdmin5 = Get-AzureSqlServerActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName | ||
|
||
Assert-Null $activeDirectoryAdmin5 | ||
} | ||
finally | ||
{ | ||
Remove-ResourceGroupForTest $rg | ||
} | ||
} | ||
|
149 changes: 149 additions & 0 deletions
149
src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/SqlEvnSetupHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Azure.Common.Authentication; | ||
using Microsoft.Azure.Common.Authentication.Models; | ||
using Microsoft.Azure.Gallery; | ||
using Microsoft.Azure.Graph.RBAC; | ||
using Microsoft.Azure.Management.Authorization; | ||
using Microsoft.Azure.Management.Resources; | ||
using Microsoft.Azure.Subscriptions; | ||
using Microsoft.Azure.Test; | ||
using Microsoft.Azure.Test.HttpRecorder; | ||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
|
||
namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests | ||
{ | ||
public class SqlEvnSetupHelper : EnvironmentSetupHelper | ||
{ | ||
/// <summary> | ||
/// This overrides the default subscription and default account. This allows the | ||
/// test to get the tenant id in the test. | ||
/// </summary> | ||
public void SetupEnvironment() | ||
{ | ||
base.SetupEnvironment(AzureModule.AzureResourceManager); | ||
|
||
TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment(); | ||
|
||
if (csmEnvironment.SubscriptionId != null) | ||
{ | ||
//Overwrite the default subscription and default account | ||
//with ones using user ID and tenant ID from auth context | ||
var user = GetUser(csmEnvironment); | ||
var tenantId = GetTenantId(csmEnvironment); | ||
|
||
// Existing test will not have a user or tenant id set | ||
if (tenantId != null && user != null) | ||
{ | ||
var testSubscription = new AzureSubscription() | ||
{ | ||
Id = new Guid(csmEnvironment.SubscriptionId), | ||
Name = ProfileClient.Profile.DefaultSubscription.Name, | ||
Environment = ProfileClient.Profile.DefaultSubscription.Environment, | ||
Account = user, | ||
Properties = new Dictionary<AzureSubscription.Property, string> | ||
{ | ||
{ | ||
AzureSubscription.Property.Default, "True" | ||
}, | ||
{ | ||
AzureSubscription.Property.StorageAccount, | ||
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT") | ||
}, | ||
{ | ||
AzureSubscription.Property.Tenants, tenantId | ||
}, | ||
} | ||
}; | ||
|
||
var testAccount = new AzureAccount() | ||
{ | ||
Id = user, | ||
Type = AzureAccount.AccountType.User, | ||
Properties = new Dictionary<AzureAccount.Property, string> | ||
{ | ||
{ | ||
AzureAccount.Property.Subscriptions, csmEnvironment.SubscriptionId | ||
}, | ||
} | ||
}; | ||
|
||
ProfileClient.Profile.Accounts.Remove(ProfileClient.Profile.DefaultSubscription.Account); | ||
ProfileClient.Profile.Subscriptions[testSubscription.Id] = testSubscription; | ||
ProfileClient.Profile.Accounts[testAccount.Id] = testAccount; | ||
ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.Account); | ||
|
||
ProfileClient.Profile.Save(); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Helper function to get the tenant id if it was set in the test | ||
/// </summary> | ||
/// <param name="environment">Test environment</param> | ||
/// <returns>The tenant id or null if not tenant id could be found.</returns> | ||
private string GetTenantId(TestEnvironment environment) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
HttpMockServer.Variables["TenantId"] = environment.AuthorizationContext.TenantId; | ||
return environment.AuthorizationContext.TenantId; | ||
} | ||
else | ||
{ | ||
if (HttpMockServer.Variables.ContainsKey("TenantId")) | ||
{ | ||
return HttpMockServer.Variables["TenantId"]; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Helper function to get the user id if it was set in the test | ||
/// </summary> | ||
/// <param name="environment">Test environment</param> | ||
/// <returns>The user id or null if not tenant id could be found.</returns> | ||
private string GetUser(TestEnvironment environment) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
HttpMockServer.Variables["User"] = environment.AuthorizationContext.UserId; | ||
return environment.AuthorizationContext.UserId; | ||
} | ||
else | ||
{ | ||
if (HttpMockServer.Variables.ContainsKey("User")) | ||
{ | ||
return HttpMockServer.Variables["User"]; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.