Skip to content

Commit 135c95e

Browse files
Merge pull request #2 from SanjaMalesevic/addPSCommandsForAadAdmin
Add ps commands for aad admin
2 parents 8de5d3f + f711aff commit 135c95e

20 files changed

+1308
-3
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 = GetGraphClient(context);
35+
Helper.SetupSomeOfManagementClients(newResourcesClient,sqlClient, networkClient, graphClient);
36+
}
37+
38+
[Fact(Skip = "Graph authentication blocks test passes")]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestManagedInstanceActiveDirectoryAdministrator()
41+
{
42+
RunPowerShellTest("Test-ManagedInstanceActiveDirectoryAdministrator");
43+
}
44+
}
45+
}
46+
47+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
$activeDirectoryGroup1 = "aadadmin"
33+
$activeDirectoryGroup1ObjectId = "52b6d571-5ff9-4b8f-92de-4a5b1bcdbbef"
34+
$activeDirectoryUser1 = "CL AAD Test User"
35+
$activeDirectoryUser1ObjectId = "034bb7d9-ca26-4c6f-abe0-4aff74fdca50"
36+
37+
# Verify there is no Active Directory Administrator set
38+
$activeDirectoryAdmin = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
39+
40+
Assert-Null $activeDirectoryAdmin
41+
42+
# Set an Active Directory Administrator Group on Managed Instance
43+
# 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
44+
# MockTokenAuthenticationFactory constructor and change SetAuthenticationFactory in EnvironmentSetupHelper.
45+
$activeDirectoryAdmin1 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryGroup1 -ObjectId $activeDirectoryGroup1ObjectId
46+
47+
Assert-NotNull $activeDirectoryAdmin1
48+
49+
# Verify the correct Active Directory Administrator is set
50+
Assert-AreEqual $activeDirectoryAdmin1.DisplayName $activeDirectoryGroup1
51+
Assert-AreEqual $activeDirectoryAdmin1.ObjectId $activeDirectoryGroup1ObjectId
52+
53+
# Get an Active Directory Administrator
54+
$activeDirectoryAdmin2 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
55+
56+
Assert-AreEqual $activeDirectoryAdmin2.DisplayName $activeDirectoryGroup1
57+
Assert-AreEqual $activeDirectoryAdmin2.ObjectId $activeDirectoryGroup1ObjectId
58+
59+
# Set an Active Directory Administrator User on Managed Instance
60+
$activeDirectoryAdmin3 = Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -DisplayName $activeDirectoryUser1 -ObjectId $activeDirectoryUser1ObjectId
61+
62+
Assert-AreEqual $activeDirectoryAdmin3.DisplayName $activeDirectoryUser1
63+
Assert-AreEqual $activeDirectoryAdmin3.ObjectId $activeDirectoryUser1ObjectId
64+
65+
# Remove an Active Directory Administrator User from Managed Instance
66+
$activeDirectoryAdmin4 = Remove-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName -Force
67+
68+
# Verify that Active Directory Administrator was deleted
69+
$activeDirectoryAdmin5 = Get-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName $rg.ResourceGroupName -InstanceName $managedInstance.ManagedInstanceName
70+
71+
Assert-Null $activeDirectoryAdmin5
72+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
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.Version1_6;
3636

3737
namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests
3838
{

src/Sql/Sql.Test/Sql.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="Microsoft.Azure.KeyVault.WebKey" Version="3.0.1" />
2020
<PackageReference Include="Microsoft.Azure.Management.KeyVault" Version="2.4.2" />
2121
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.19.0-preview" />
22-
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.33.0-preview" />
22+
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.34.0-preview" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

src/Sql/Sql/Az.Sql.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
230230
'Set-AzSqlInstanceTransparentDataEncryptionProtector',
231231
'Get-AzSqlServerAudit', 'Get-AzSqlDatabaseAudit',
232232
'Set-AzSqlServerAudit', 'Set-AzSqlDatabaseAudit',
233+
'Get-AzSqlInstanceActiveDirectoryAdministrator',
234+
'Remove-AzSqlInstanceActiveDirectoryAdministrator',
235+
'Set-AzSqlInstanceActiveDirectoryAdministrator',
233236
'Remove-AzSqlServerAudit', 'Remove-AzSqlDatabaseAudit',
234237
'Get-AzSqlInstancePool', 'Set-AzSqlInstancePool',
235238
'New-AzSqlInstancePool', 'Remove-AzSqlInstancePool',

src/Sql/Sql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Remove the validation of EmailAddresses and the check that EmailAdmins is not false in case EmailAddresses is empty in Set-AzSqlServerAdvancedThreatProtectionPolicy and Set-AzSqlDatabaseAdvancedThreatProtectionPolicy
2424
* Enabled removal of server/database auditing settings when multiple diagnostic settings that enable audit category exist.
2525
* Fix email addresses validation in multiple Sql Vulnerability Assessment cmdlets (Update-AzSqlDatabaseVulnerabilityAssessmentSetting, Update-AzSqlServerVulnerabilityAssessmentSetting, Update-AzSqlInstanceDatabaseVulnerabilityAssessmentSetting and Update-AzSqlInstanceVulnerabilityAssessmentSetting).
26+
* Add support for setting Active Directory Administrator on Managed Instance
2627

2728
## Version 1.14.1
2829
* Update documentation of old Auditing cmdlets.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.ResourceManager.Common.ArgumentCompleters;
16+
using Microsoft.Azure.Commands.Sql.Common;
17+
using Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryAdministrator.Model;
18+
using Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryAdministrator.Services;
19+
using System.Collections.Generic;
20+
using System.Management.Automation;
21+
22+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryAdministrator.Cmdlet
23+
{
24+
public abstract class AzureSqlInstanceActiveDirectoryAdministratorCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel>, AzureSqlInstanceActiveDirectoryAdministratorAdapter>
25+
{
26+
/// <summary>
27+
/// Gets or sets the name of the Azure SQL Managed Instance that contains the Azure Active Directory administrator.
28+
/// </summary>
29+
[Parameter(Mandatory = true,
30+
ValueFromPipelineByPropertyName = true,
31+
Position = 1,
32+
HelpMessage = "The name of the Azure SQL Managed Instance the Azure Active Directory administrator is in.")]
33+
[ResourceNameCompleter("Microsoft.Sql/managedInstances", "ResourceGroupName")]
34+
[ValidateNotNullOrEmpty]
35+
public string InstanceName { get; set; }
36+
37+
/// <summary>
38+
/// Initializes the adapter
39+
/// </summary>
40+
/// <returns></returns>
41+
protected override AzureSqlInstanceActiveDirectoryAdministratorAdapter InitModelAdapter()
42+
{
43+
return new AzureSqlInstanceActiveDirectoryAdministratorAdapter(DefaultProfile.DefaultContext);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.Sql.InstanceActiveDirectoryAdministrator.Model;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryAdministrator.Cmdlet
20+
{
21+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlInstanceActiveDirectoryAdministrator")]
22+
[OutputType(typeof(AzureSqlInstanceActiveDirectoryAdministratorModel))]
23+
public class GetAzureSqlInstanceActiveDirectoryAdministrator : AzureSqlInstanceActiveDirectoryAdministratorCmdletBase
24+
{
25+
/// <summary>
26+
/// Get the entities from the service
27+
/// </summary>
28+
/// <returns>The list of entities</returns>
29+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> GetEntity()
30+
{
31+
ICollection<AzureSqlInstanceActiveDirectoryAdministratorModel> results;
32+
33+
results = ModelAdapter.ListInstanceActiveDirectoryAdministrators(this.ResourceGroupName, this.InstanceName);
34+
35+
return results;
36+
}
37+
38+
/// <summary>
39+
/// No user input to apply to model
40+
/// </summary>
41+
/// <param name="model">Model retrieved from service</param>
42+
/// <returns>The model that was passed in</returns>
43+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> ApplyUserInputToModel(IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> model)
44+
{
45+
return model;
46+
}
47+
48+
/// <summary>
49+
/// No changes to persist to mi
50+
/// </summary>
51+
/// <param name="entity">The output of apply user input to model</param>
52+
/// <returns>The input entity</returns>
53+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> PersistChanges(IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> entity)
54+
{
55+
return entity;
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Sql.InstanceActiveDirectoryAdministrator.Model;
16+
using System.Collections.Generic;
17+
using System.Globalization;
18+
using System.Management.Automation;
19+
20+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryAdministrator.Cmdlet
21+
{
22+
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlInstanceActiveDirectoryAdministrator", SupportsShouldProcess = true), OutputType(typeof(AzureSqlInstanceActiveDirectoryAdministratorModel))]
23+
public class RemoveAzureSqlInstanceActiveDirectoryAdministrator : AzureSqlInstanceActiveDirectoryAdministratorCmdletBase
24+
{
25+
/// <summary>
26+
/// Defines whether it is ok to skip the requesting of rule removal confirmation
27+
/// </summary>
28+
[Parameter(HelpMessage = "Skip confirmation message for performing the action")]
29+
public SwitchParameter Force { get; set; }
30+
31+
/// <summary>
32+
/// Get the entities from the service
33+
/// </summary>
34+
/// <returns>The list of entities</returns>
35+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> GetEntity()
36+
{
37+
return new List<AzureSqlInstanceActiveDirectoryAdministratorModel>() {
38+
ModelAdapter.GetInstanceActiveDirectoryAdministrator(this.ResourceGroupName, this.InstanceName)
39+
};
40+
}
41+
42+
/// <summary>
43+
/// No user input to apply to model
44+
/// </summary>
45+
/// <param name="model">Model retrieved from service</param>
46+
/// <returns>The model that was passed in</returns>
47+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> ApplyUserInputToModel(IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> model)
48+
{
49+
return model;
50+
}
51+
52+
/// <summary>
53+
/// No changes to persist to managed instance
54+
/// </summary>
55+
/// <param name="entity">The output of apply user input to model</param>
56+
/// <returns>The input entity</returns>
57+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> PersistChanges(IEnumerable<AzureSqlInstanceActiveDirectoryAdministratorModel> entity)
58+
{
59+
ModelAdapter.RemoveInstanceActiveDirectoryAdministrator(this.ResourceGroupName, this.InstanceName);
60+
return entity;
61+
}
62+
63+
/// <summary>
64+
/// Entry point for the cmdlet
65+
/// </summary>
66+
public override void ExecuteCmdlet()
67+
{
68+
if (!Force.IsPresent && !ShouldProcess(
69+
string.Format(CultureInfo.InvariantCulture, Properties.Resources.RemoveAzureSqlInstanceActiveDirectoryAdministratorDescription, this.InstanceName),
70+
string.Format(CultureInfo.InvariantCulture, Properties.Resources.RemoveAzureSqlInstanceActiveDirectoryAdministratorWarning, this.InstanceName),
71+
Properties.Resources.ShouldProcessCaption))
72+
{
73+
return;
74+
}
75+
76+
base.ExecuteCmdlet();
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)