Skip to content

Commit 307871b

Browse files
authored
Powershell for AAD Only Authentication on Managed Instance. (#12890)
* Powershell for AAD Only Authentication on Managed Instance. * Fix nuget version in Sql.Test * Fix links for Online Docs.
1 parent 879da76 commit 307871b

15 files changed

+982
-6
lines changed

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="3.0.0" />
2121
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.21.0-preview" />
22-
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.44.0-preview" />
22+
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.44.1-preview" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

src/Sql/Sql/Az.Sql.psd1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,13 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
262262
'Invoke-AzSqlInstanceFailover',
263263
'Enable-AzSqlServerActiveDirectoryOnlyAuthentication',
264264
'Get-AzSqlServerActiveDirectoryOnlyAuthentication',
265-
'Start-AzSqlInstanceDatabaseLogReplay',
266-
'Complete-AzSqlInstanceDatabaseLogReplay',
267-
'Stop-AzSqlInstanceDatabaseLogReplay',
268-
'Get-AzSqlInstanceDatabaseLogReplay'
265+
'Start-AzSqlInstanceDatabaseLogReplay',
266+
'Complete-AzSqlInstanceDatabaseLogReplay',
267+
'Stop-AzSqlInstanceDatabaseLogReplay',
268+
'Get-AzSqlInstanceDatabaseLogReplay',
269+
'Disable-AzSqlInstanceActiveDirectoryOnlyAuthentication',
270+
'Enable-AzSqlInstanceActiveDirectoryOnlyAuthentication',
271+
'Get-AzSqlInstanceActiveDirectoryOnlyAuthentication'
269272

270273
# Variables to export from this module
271274
# VariablesToExport = @()

src/Sql/Sql/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
- `Get-AzSqlInstanceDatabaseLogReplay`
2828
- `Complete-AzSqlInstanceDatabaseLogReplay`
2929
- `Stop-AzSqlInstanceDatabaseLogReplay`
30+
* Added cmdlet `Get-AzSqlInstanceActiveDirectoryOnlyAuthentication`
31+
* Added cmdlet `Enable-AzSqlInstanceActiveDirectoryOnlyAuthentication`
32+
* Added cmdlet `Disable-AzSqlInstanceActiveDirectoryOnlyAuthentication`
3033

3134
## Version 2.9.1
3235
* Fixed potential server name case insensitive error in `New-AzSqlServer` and `Set-AzSqlServer`
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.InstanceActiveDirectoryOnlyAuthentication.Model;
18+
using Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Services;
19+
using System.Collections.Generic;
20+
using System.Management.Automation;
21+
22+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Cmdlet
23+
{
24+
public abstract class AzureSqlInstanceActiveDirectoryOnlyAuthenticationCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>, AzureSqlInstanceActiveDirectoryOnlyAuthenticationAdapter>
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 only authentication 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 AzureSqlInstanceActiveDirectoryOnlyAuthenticationAdapter InitModelAdapter()
42+
{
43+
return new AzureSqlInstanceActiveDirectoryOnlyAuthenticationAdapter(DefaultProfile.DefaultContext);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.InstanceActiveDirectoryOnlyAuthentication.Model;
16+
using Microsoft.Rest.Azure;
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Management.Automation;
21+
22+
23+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Cmdlet
24+
{
25+
/// <summary>
26+
/// Disables the Azure Active Directory only authentication of a specific SQL Managed Instance.
27+
/// </summary>
28+
[Cmdlet("Disable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlInstanceActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
29+
[OutputType(typeof(AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel))]
30+
public class DisableAzureSqlInstanceActiveDirectoryOnlyAuthentication : AzureSqlInstanceActiveDirectoryOnlyAuthenticationCmdletBase
31+
{
32+
/// <summary>
33+
/// Get the entities from the service
34+
/// </summary>
35+
/// <returns>The list of entities</returns>
36+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> GetEntity()
37+
{
38+
List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> currentActiveDirectoryOnlyAuthentications = null;
39+
40+
try
41+
{
42+
AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel model = ModelAdapter.GetInstanceActiveDirectoryOnlyAuthentication(this.ResourceGroupName, this.InstanceName);
43+
44+
if (model != null)
45+
{
46+
currentActiveDirectoryOnlyAuthentications = new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>();
47+
currentActiveDirectoryOnlyAuthentications.Add(model);
48+
}
49+
}
50+
catch (CloudException ex)
51+
{
52+
if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
53+
{
54+
// Unexpected exception encountered
55+
throw;
56+
}
57+
}
58+
catch (Exception ex)
59+
{
60+
if ((ex.InnerException is CloudException ex1) &&
61+
ex1.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
62+
{
63+
throw ex.InnerException ?? ex;
64+
}
65+
}
66+
67+
return currentActiveDirectoryOnlyAuthentications;
68+
}
69+
70+
/// <summary>
71+
/// Create the list of models from a list of user input
72+
/// </summary>
73+
/// <param name="model">A IEnumerable of models retrieved from service</param>
74+
/// <returns>A list of models that was passed in</returns>
75+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> model)
76+
{
77+
List<Model.AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> newEntity = new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>();
78+
newEntity.Add(new AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel()
79+
{
80+
ResourceGroupName = ResourceGroupName,
81+
InstanceName = InstanceName,
82+
AzureADOnlyAuthentication = false
83+
});
84+
return newEntity;
85+
}
86+
87+
/// <summary>
88+
/// Update the Azure SQL Managed Instance Active Directory only authentication
89+
/// </summary>
90+
/// <param name="entity">A list of models to update the list</param>
91+
/// <returns>A list of the persisted entities</returns>
92+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> entity)
93+
{
94+
return new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>() {
95+
ModelAdapter.UpsertAzureADOnlyAuthenticaion(this.ResourceGroupName, this.InstanceName, entity.FirstOrDefault())
96+
};
97+
}
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.InstanceActiveDirectoryOnlyAuthentication.Model;
16+
using Microsoft.Rest.Azure;
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Management.Automation;
21+
22+
23+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Cmdlet
24+
{
25+
/// <summary>
26+
/// Disables the Azure Active Directory only authentication of a specific SQL Managed Instance.
27+
/// </summary>
28+
[Cmdlet("Enable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlInstanceActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
29+
[OutputType(typeof(AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel))]
30+
public class EnableAzureSqlInstanceActiveDirectoryOnlyAuthentication : AzureSqlInstanceActiveDirectoryOnlyAuthenticationCmdletBase
31+
{
32+
/// <summary>
33+
/// Get the entities from the service
34+
/// </summary>
35+
/// <returns>The list of entities</returns>
36+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> GetEntity()
37+
{
38+
List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> currentActiveDirectoryOnlyAuthentications = null;
39+
40+
try
41+
{
42+
AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel model = ModelAdapter.GetInstanceActiveDirectoryOnlyAuthentication(this.ResourceGroupName, this.InstanceName);
43+
44+
if (model != null)
45+
{
46+
currentActiveDirectoryOnlyAuthentications = new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>();
47+
currentActiveDirectoryOnlyAuthentications.Add(model);
48+
}
49+
}
50+
catch (CloudException ex)
51+
{
52+
if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
53+
{
54+
// Unexpected exception encountered
55+
throw;
56+
}
57+
}
58+
catch (Exception ex)
59+
{
60+
if ((ex.InnerException is CloudException ex1) &&
61+
ex1.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
62+
{
63+
throw ex.InnerException ?? ex;
64+
}
65+
}
66+
67+
return currentActiveDirectoryOnlyAuthentications;
68+
}
69+
70+
/// <summary>
71+
/// Create the list of models from a list of user input
72+
/// </summary>
73+
/// <param name="model">A IEnumerable of models retrieved from service</param>
74+
/// <returns>A list of models that was passed in</returns>
75+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> model)
76+
{
77+
List<Model.AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> newEntity = new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>();
78+
newEntity.Add(new AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel()
79+
{
80+
ResourceGroupName = ResourceGroupName,
81+
InstanceName = InstanceName,
82+
AzureADOnlyAuthentication = true
83+
});
84+
return newEntity;
85+
}
86+
87+
/// <summary>
88+
/// Update the Azure SQL Managed Instance Active Directory only authentication
89+
/// </summary>
90+
/// <param name="entity">A list of models to update the list</param>
91+
/// <returns>A list of the persisted entities</returns>
92+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> entity)
93+
{
94+
return new List<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel>() {
95+
ModelAdapter.UpsertAzureADOnlyAuthenticaion(this.ResourceGroupName, this.InstanceName, entity.FirstOrDefault())
96+
};
97+
}
98+
}
99+
}
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.InstanceActiveDirectoryOnlyAuthentication.Model;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Cmdlet
20+
{
21+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlInstanceActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
22+
[OutputType(typeof(AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel))]
23+
public class GetAzureSqlInstanceActiveDirectoryOnlyAuthentication : AzureSqlInstanceActiveDirectoryOnlyAuthenticationCmdletBase
24+
{
25+
/// <summary>
26+
/// Get the entities from the service
27+
/// </summary>
28+
/// <returns>The list of entities</returns>
29+
protected override IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> GetEntity()
30+
{
31+
ICollection<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> results;
32+
33+
results = ModelAdapter.ListInstanceActiveDirectoryOnlyAuthentications(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<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> model)
44+
{
45+
return model;
46+
}
47+
48+
/// <summary>
49+
/// No changes to persist to server
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<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel> entity)
54+
{
55+
return entity;
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
17+
namespace Microsoft.Azure.Commands.Sql.InstanceActiveDirectoryOnlyAuthentication.Model
18+
{
19+
/// <summary>
20+
/// Represents an Azure SQL Managed Instance Active Directory only authentication
21+
/// </summary>
22+
public class AzureSqlInstanceActiveDirectoryOnlyAuthenticationModel
23+
{
24+
/// <summary>
25+
/// Gets or sets the name of the resource group
26+
/// </summary>
27+
public string ResourceGroupName { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets the name of the instance
31+
/// </summary>
32+
public string InstanceName { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the value to indicate Azure AD Only authentication
36+
/// </summary>
37+
public bool AzureADOnlyAuthentication { get; set; }
38+
}
39+
}

0 commit comments

Comments
 (0)