Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL] Fix\add AzSqlServerActiveDirectoryOnlyAuthentication APIs #12782

Merged
merged 9 commits into from
Sep 3, 2020
Merged

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Accounts/Authentication/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Sql/Sql.Test/Sql.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.Azure.KeyVault.WebKey" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.Management.KeyVault" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.21.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.43.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.44.0-preview" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/Sql/Sql/Az.Sql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
'Get-AzSqlInstanceDatabaseBackupLongTermRetentionPolicy',
'Set-AzSqlInstanceDatabaseBackupLongTermRetentionPolicy',
'Disable-AzSqlServerActiveDirectoryOnlyAuthentication',
'Invoke-AzSqlInstanceFailover'
'Invoke-AzSqlInstanceFailover',
'Enable-AzSqlServerActiveDirectoryOnlyAuthentication',
'Get-AzSqlServerActiveDirectoryOnlyAuthentication'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
2 changes: 2 additions & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Additional information about change #1
-->
## Upcoming Release
* Added cmdlet `Get-AzSqlServerActiveDirectoryOnlyAuthentication`
* Added cmdlet `Enable-AzSqlServerActiveDirectoryOnlyAuthentication`

## Version 2.9.1
* Fixed potential server name case insensitive error in `New-AzSqlServer` and `Set-AzSqlServer`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,6 @@ public void RemoveServerActiveDirectoryAdministrator(string resourceGroupName, s
Communicator.Remove(resourceGroupName, serverName);
}

/// <summary>
/// Disable Azure Active Directory only authentication on a Azure SQL Server
/// </summary>
/// <param name="resourceGroup">The name of the resource group</param>
/// <param name="serverName">The name of the Azure Sql ServerActiveDirectoryAdministrator Server</param>
/// <returns>The upserted Azure SQL Server Active Directory administrator</returns>
internal AzureSqlServerActiveDirectoryAdministratorModel DisableAzureADOnlyAuthenticaion(string resourceGroup, string serverName)
{
var resp = Communicator.Disable(resourceGroup, serverName);

return CreateServerActiveDirectoryAdministratorModelFromResponse(resourceGroup, serverName, resp);
}

/// <summary>
/// Converts the response from the service to a powershell database object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ public void Remove(string resourceGroupName, string serverName)
GetCurrentSqlClient().ServerAzureADAdministrators.DeleteWithHttpMessagesAsync(resourceGroupName, serverName);
}

/// <summary>
/// Disables Azure Active Directory only authentication on a Azure SQL Server
/// </summary>
public Management.Sql.Models.ServerAzureADAdministrator Disable(string resourceGroupName, string serverName)
{
return GetCurrentSqlClient().ServerAzureADAdministrators.DisableAzureADOnlyAuthenticationAsync(resourceGroupName, serverName).Result;
}

/// <summary>
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
/// id tracing headers for the current cmdlet invocation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------
//
// 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.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.Sql.Common;
using Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Model;
using Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Services;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Cmdlet
{
public abstract class AzureSqlServerActiveDirectoryOnlyAuthenticationCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>, AzureSqlServerActiveDirectoryOnlyAuthenticationAdapter>
{
/// <summary>
/// Gets or sets the name of the Azure SQL Server that contains the Azure Active Directory administrator.
/// </summary>
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
Position = 1,
HelpMessage = "The name of the Azure SQL Server the Azure Active Directory only authentication is in.")]
[ResourceNameCompleter("Microsoft.Sql/servers", "ResourceGroupName")]
[ValidateNotNullOrEmpty]
public string ServerName { get; set; }

/// <summary>
/// Initializes the adapter
/// </summary>
/// <returns></returns>
protected override AzureSqlServerActiveDirectoryOnlyAuthenticationAdapter InitModelAdapter()
{
return new AzureSqlServerActiveDirectoryOnlyAuthenticationAdapter(DefaultProfile.DefaultContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,39 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Sql.ServerActiveDirectoryAdministrator.Model;
using Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Model;
using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Sql.ServerActiveDirectoryAdministrator.Cmdlet

namespace Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Cmdlet
{
/// <summary>
/// Disables the Azure Active Directory only authentication of a specific SQL server.
/// </summary>
[Cmdlet("Disable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlServerActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
[OutputType(typeof(AzureSqlServerActiveDirectoryAdministratorModel))]
public class DisableAzureSqlServerActiveDirectoryOnlyAuthentication : AzureSqlServerActiveDirectoryAdministratorCmdletBase
[OutputType(typeof(AzureSqlServerActiveDirectoryOnlyAuthenticationModel))]
public class DisableAzureSqlServerActiveDirectoryOnlyAuthentication : AzureSqlServerActiveDirectoryOnlyAuthenticationCmdletBase
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryAdministratorModel> GetEntity()
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> GetEntity()
{
List<AzureSqlServerActiveDirectoryAdministratorModel> currentActiveDirectoryAdmins = null;
List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> currentActiveDirectoryOnlyAuthentications = null;

try
{
AzureSqlServerActiveDirectoryAdministratorModel model = ModelAdapter.GetServerActiveDirectoryAdministrator(this.ResourceGroupName, this.ServerName);
AzureSqlServerActiveDirectoryOnlyAuthenticationModel model = ModelAdapter.GetServerActiveDirectoryOnlyAuthentication(this.ResourceGroupName, this.ServerName);

if (model != null)
{
currentActiveDirectoryAdmins = new List<AzureSqlServerActiveDirectoryAdministratorModel>();
currentActiveDirectoryAdmins.Add(model);
currentActiveDirectoryOnlyAuthentications = new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>();
currentActiveDirectoryOnlyAuthentications.Add(model);
}
}
catch (CloudException ex)
Expand All @@ -63,18 +64,35 @@ protected override IEnumerable<AzureSqlServerActiveDirectoryAdministratorModel>
}
}

return currentActiveDirectoryAdmins;
return currentActiveDirectoryOnlyAuthentications;
}

/// <summary>
/// Create the list of models from a list of user input
/// </summary>
/// <param name="model">A IEnumerable of models retrieved from service</param>
/// <returns>A list of models that was passed in</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> model)
{
List<Model.AzureSqlServerActiveDirectoryOnlyAuthenticationModel> newEntity = new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>();
newEntity.Add(new AzureSqlServerActiveDirectoryOnlyAuthenticationModel()
{
ResourceGroupName = ResourceGroupName,
ServerName = ServerName,
AzureADOnlyAuthentication = false
});
return newEntity;
}

/// <summary>
/// Update the Azure SQL Server Active Directory administrator
/// Update the Azure SQL Server Active Directory only authentication
/// </summary>
/// <param name="entity">A list of models to update the list</param>
/// <returns>A list of the persisted entities</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryAdministratorModel> PersistChanges(IEnumerable<AzureSqlServerActiveDirectoryAdministratorModel> entity)
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> entity)
{
return new List<AzureSqlServerActiveDirectoryAdministratorModel>() {
ModelAdapter.DisableAzureADOnlyAuthenticaion(this.ResourceGroupName, this.ServerName)
return new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>() {
ModelAdapter.UpsertAzureADOnlyAuthenticaion(this.ResourceGroupName, this.ServerName, entity.FirstOrDefault())
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// ----------------------------------------------------------------------------------
//
// 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.Sql.ServerActiveDirectoryOnlyAuthentication.Model;
using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;


namespace Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Cmdlet
{
/// <summary>
/// Disables the Azure Active Directory only authentication of a specific SQL server.
/// </summary>
[Cmdlet("Enable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlServerActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
[OutputType(typeof(AzureSqlServerActiveDirectoryOnlyAuthenticationModel))]
public class EnableAzureSqlServerActiveDirectoryOnlyAuthentication : AzureSqlServerActiveDirectoryOnlyAuthenticationCmdletBase
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> GetEntity()
{
List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> currentActiveDirectoryOnlyAuthentications = null;

try
{
AzureSqlServerActiveDirectoryOnlyAuthenticationModel model = ModelAdapter.GetServerActiveDirectoryOnlyAuthentication(this.ResourceGroupName, this.ServerName);

if (model != null)
{
currentActiveDirectoryOnlyAuthentications = new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>();
currentActiveDirectoryOnlyAuthentications.Add(model);
}
}
catch (CloudException ex)
{
if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
{
// Unexpected exception encountered
throw;
}
}
catch (Exception ex)
{
if ((ex.InnerException is CloudException ex1) &&
ex1.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
{
throw ex.InnerException ?? ex;
}
}

return currentActiveDirectoryOnlyAuthentications;
}

/// <summary>
/// Create the list of models from a list of user input
/// </summary>
/// <param name="model">A IEnumerable of models retrieved from service</param>
/// <returns>A list of models that was passed in</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> model)
{
List<Model.AzureSqlServerActiveDirectoryOnlyAuthenticationModel> newEntity = new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>();
newEntity.Add(new AzureSqlServerActiveDirectoryOnlyAuthenticationModel()
{
ResourceGroupName = ResourceGroupName,
ServerName = ServerName,
AzureADOnlyAuthentication = true
});
return newEntity;
}

/// <summary>
/// Update the Azure SQL Server Active Directory only authentication
/// </summary>
/// <param name="entity">A list of models to update the list</param>
/// <returns>A list of the persisted entities</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> entity)
{
return new List<AzureSqlServerActiveDirectoryOnlyAuthenticationModel>() {
ModelAdapter.UpsertAzureADOnlyAuthenticaion(this.ResourceGroupName, this.ServerName, entity.FirstOrDefault())
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// ----------------------------------------------------------------------------------
//
// 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.Sql.ServerActiveDirectoryOnlyAuthentication.Model;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Sql.ServerActiveDirectoryOnlyAuthentication.Cmdlet
{
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlServerActiveDirectoryOnlyAuthentication", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)]
[OutputType(typeof(AzureSqlServerActiveDirectoryOnlyAuthenticationModel))]
public class GetAzureSqlServerActiveDirectoryOnlyAuthentication : AzureSqlServerActiveDirectoryOnlyAuthenticationCmdletBase
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> GetEntity()
{
ICollection<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> results;

results = ModelAdapter.ListServerActiveDirectoryOnlyAuthentications(this.ResourceGroupName, this.ServerName);

return results;
}

/// <summary>
/// No user input to apply to model
/// </summary>
/// <param name="model">Model retrieved from service</param>
/// <returns>The model that was passed in</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> ApplyUserInputToModel(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> model)
{
return model;
}

/// <summary>
/// No changes to persist to server
/// </summary>
/// <param name="entity">The output of apply user input to model</param>
/// <returns>The input entity</returns>
protected override IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> PersistChanges(IEnumerable<AzureSqlServerActiveDirectoryOnlyAuthenticationModel> entity)
{
return entity;
}
}
}
Loading