diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Commands.SqlDatabase.csproj b/src/ServiceManagement/Sql/Commands.SqlDatabase/Commands.SqlDatabase.csproj index b42fc5c279aa..4f31cc80d2da 100644 --- a/src/ServiceManagement/Sql/Commands.SqlDatabase/Commands.SqlDatabase.csproj +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Commands.SqlDatabase.csproj @@ -190,12 +190,13 @@ - + + diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/StartAzureSqlDatabaseExport.cs b/src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/StartAzureSqlDatabaseExport.cs index f9fec0f80808..7bb031178809 100644 --- a/src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/StartAzureSqlDatabaseExport.cs +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/StartAzureSqlDatabaseExport.cs @@ -56,7 +56,7 @@ public class StartAzureSqlDatabaseExport : SqlDatabaseCmdletBase [Parameter(Mandatory = true, Position = 0, HelpMessage = "The context for connecting to the server")] [ValidateNotNullOrEmpty] - public ServerDataServiceSqlAuth SqlConnectionContext { get; set; } + public ISqlCredentialsDataServiceContext SqlConnectionContext { get; set; } /// /// Gets or sets the destination storage container for the blob diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/DataServiceBasicCredentials.cs b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/SqlAuthenticationCredentials.cs similarity index 78% rename from src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/DataServiceBasicCredentials.cs rename to src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/SqlAuthenticationCredentials.cs index 4bd788f111a5..148e213a0cd3 100644 --- a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/DataServiceBasicCredentials.cs +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Common/SqlAuthenticationCredentials.cs @@ -26,7 +26,33 @@ public class SqlAuthenticationCredentials { private string userName; private SecureString password; - + + /// + /// Initializes a new instance of the class. + /// + /// The user name. + /// The password + public SqlAuthenticationCredentials(string userName, string password) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("userName"); + } + + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentNullException("password"); + } + + SecureString encryptedPassword = new SecureString(); + foreach (char letter in password) + { + encryptedPassword.AppendChar(letter); + } + + Initialize(userName, encryptedPassword); + } + /// /// Initializes a new instance of the class. /// @@ -44,6 +70,12 @@ public SqlAuthenticationCredentials(string userName, SecureString password) throw new ArgumentNullException("password"); } + Initialize(userName, password); + } + + + private void Initialize(string userName, SecureString password) + { this.userName = userName; this.password = password.Copy(); this.password.MakeReadOnly(); diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ISqlCredentialsDataServiceContext.cs b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ISqlCredentialsDataServiceContext.cs new file mode 100644 index 000000000000..c95a7d38a61d --- /dev/null +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ISqlCredentialsDataServiceContext.cs @@ -0,0 +1,32 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server +{ + using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Common; + using DatabaseCopyModel = Model.DatabaseCopy; + + /// + /// Common interface for all server based operations. + /// + public interface ISqlCredentialsDataServiceContext : IServerDataServiceContext + { + /// + /// Gets the Credentials for this context + /// + SqlAuthenticationCredentials SqlCredentials { get; } + } +} diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ServerDataServiceSqlAuth.cs b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ServerDataServiceSqlAuth.cs index 255fc38fb6b7..68f9f65e7553 100644 --- a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ServerDataServiceSqlAuth.cs +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ServerDataServiceSqlAuth.cs @@ -31,7 +31,7 @@ namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server /// /// Implementation of the with Sql Authentication. /// - public partial class ServerDataServiceSqlAuth : ServerDataServiceContext, IServerDataServiceContext + public partial class ServerDataServiceSqlAuth : ServerDataServiceContext, ISqlCredentialsDataServiceContext { #region Constants diff --git a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/TSqlConnectionContext.cs b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/TSqlConnectionContext.cs index 8b99c07f4cc0..b3764ed6e408 100644 --- a/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/TSqlConnectionContext.cs +++ b/src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/TSqlConnectionContext.cs @@ -19,10 +19,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Common; namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server { - public class TSqlConnectionContext : IServerDataServiceContext + public class TSqlConnectionContext : ISqlCredentialsDataServiceContext { /// /// Timeout duration for commands @@ -122,6 +123,17 @@ public string ServerName } } + /// + /// Gets the sql credentials used to connect to the sever + /// + public SqlAuthenticationCredentials SqlCredentials + { + get + { + return this.sqlCredentials; + } + } + /// /// Contains the connection string necessary to connect to the server /// @@ -142,6 +154,11 @@ public string ServerName /// private string serverName; + /// + /// SQL Credentials for the context + /// + private SqlAuthenticationCredentials sqlCredentials; + /// /// Helper function to generate the SqlConnectionStringBuilder /// @@ -180,6 +197,19 @@ private DbConnection CreateConnection() } } + /// + /// Creates an instance of a SQLAuth to TSql class + /// + /// + /// The sql credentials to use. + public TSqlConnectionContext(Guid sessionActivityId, string fullyQualifiedServerName, SqlAuthenticationCredentials sqlCredentials) + { + this.sessionActivityId = sessionActivityId; + this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); + this.sqlCredentials = sqlCredentials; + builder = GenerateSqlConnectionBuilder(fullyQualifiedServerName, sqlCredentials.UserName, sqlCredentials.Password); + } + /// /// Creates an instance of a SQLAuth to TSql class /// @@ -187,10 +217,8 @@ private DbConnection CreateConnection() /// /// public TSqlConnectionContext(Guid sessionActivityId, string fullyQualifiedServerName, string username, string password) + : this(sessionActivityId, fullyQualifiedServerName, new SqlAuthenticationCredentials(username, password)) { - this.sessionActivityId = sessionActivityId; - this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); - builder = GenerateSqlConnectionBuilder(fullyQualifiedServerName, username, password); } ///