Skip to content

Commit 95d922d

Browse files
author
Hovsep
committed
Merge pull request #14 from yoavrubin/master
Calculating the connection strings in the client
2 parents df0d205 + d990469 commit 95d922d

File tree

7 files changed

+133
-13
lines changed

7 files changed

+133
-13
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/Commands.Sql.Test.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</Reference>
4949
<Reference Include="Microsoft.Azure.Management.Sql, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5050
<SpecificVersion>False</SpecificVersion>
51-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.14.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
51+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.14.3-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
5252
</Reference>
5353
<Reference Include="Microsoft.Azure.Monitoring, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5454
<SpecificVersion>False</SpecificVersion>
@@ -58,7 +58,7 @@
5858
<SpecificVersion>False</SpecificVersion>
5959
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.7.1-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
6060
</Reference>
61-
<Reference Include="Microsoft.Azure.Utilities.HttpRecorder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61+
<Reference Include="Microsoft.Azure.Utilities.HttpRecorder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6262
<SpecificVersion>False</SpecificVersion>
6363
<HintPath>..\..\..\packages\Hydra.HttpRecorder.1.0.5417.13285-prerelease\lib\net45\Microsoft.Azure.Utilities.HttpRecorder.dll</HintPath>
6464
</Reference>
@@ -85,17 +85,17 @@
8585
<Reference Include="Microsoft.WindowsAzure.Common">
8686
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
8787
</Reference>
88-
<Reference Include="Microsoft.WindowsAzure.Common.NetFramework">
88+
<Reference Include="Microsoft.WindowsAzure.Common.NetFramework">
8989
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
9090
</Reference>
9191
<Reference Include="Microsoft.WindowsAzure.Management.Storage, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9292
<SpecificVersion>False</SpecificVersion>
9393
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.Storage.3.1.0\lib\net40\Microsoft.WindowsAzure.Management.Storage.dll</HintPath>
9494
</Reference>
95-
<Reference Include="Microsoft.WindowsAzure.Testing, Version=1.0.5417.13285, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
95+
<Reference Include="Microsoft.WindowsAzure.Testing, Version=1.0.5417.13285, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9696
<SpecificVersion>False</SpecificVersion>
9797
<HintPath>..\..\..\packages\Hydra.SpecTestSupport.1.0.5417.13285-prerelease\lib\net45\Microsoft.WindowsAzure.Testing.dll</HintPath>
98-
</Reference>
98+
</Reference>
9999
<Reference Include="Moq, Version=4.2.1402.2112, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
100100
<SpecificVersion>False</SpecificVersion>
101101
<HintPath>..\..\..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>

src/ResourceManager/Sql/Commands.Sql.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Hydra.SpecTestSupport" version="1.0.5406.28672-prerelease" targetFramework="net45" />
55
<package id="Microsoft.Azure.Gallery" version="2.2.1-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Management.Resources" version="2.7.1-preview" targetFramework="net45" />
7-
<package id="Microsoft.Azure.Management.Sql" version="0.14.0-prerelease" targetFramework="net45" />
7+
<package id="Microsoft.Azure.Management.Sql" version="0.14.3-prerelease" targetFramework="net45" />
88
<package id="Microsoft.Azure.Monitoring" version="2.2.1-preview" targetFramework="net45" />
99
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
1010
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<ItemGroup>
9090
<Reference Include="Microsoft.Azure.Management.Sql, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9191
<SpecificVersion>False</SpecificVersion>
92-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.14.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
92+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.14.3-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
9393
</Reference>
9494
<Reference Include="Microsoft.Azure.ResourceManager, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9595
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/Sql/Commands.Sql/Properties/Resources.Designer.cs

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Sql/Commands.Sql/Properties/Resources.resx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,27 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="EnterPassword" xml:space="preserve">
121+
<value>{your_password_here}</value>
122+
</data>
123+
<data name="EnterUserId" xml:space="preserve">
124+
<value>{your_user_id_here}</value>
125+
</data>
120126
<data name="InvalidEventTypeSet" xml:space="preserve">
121127
<value>Cannot use the '{0}' option with other event types.</value>
122128
</data>
123129
<data name="NoStorageAccountWhenConfiguringAuditingPolicy" xml:space="preserve">
124130
<value>Cannot set auditing policy without a storage account name.</value>
125131
</data>
132+
<data name="PdoTitle" xml:space="preserve">
133+
<value>PHP Data Objects(PDO) Sample Code:</value>
134+
</data>
135+
<data name="PhpConnectionError" xml:space="preserve">
136+
<value>Error connecting to SQL Server</value>
137+
</data>
138+
<data name="sqlSampleTitle" xml:space="preserve">
139+
<value>SQL Server Extension Sample Code:</value>
140+
</data>
126141
<data name="StorageAccountNotFound" xml:space="preserve">
127142
<value>Cannot find a storage account with the name '{0}'. It either does not exist, associated with a different subscription or you do not have the appropriate credentials to access it.</value>
128143
</data>

src/ResourceManager/Sql/Commands.Sql/Security/Services/SqlClient.cs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System;
1919
using System.Collections.Generic;
2020
using System.Linq;
21+
using System.Text;
2122

2223
namespace Microsoft.Azure.Commands.Sql.Security.Services
2324
{
@@ -65,6 +66,7 @@ public AuditingPolicy GetDatabaseAuditingPolicy(string resourceGroup, string ser
6566
wrapper.ResourceGroupName = resourceGroup;
6667
wrapper.ServerName = serverName;
6768
wrapper.DatabaseName = databaseName;
69+
AddConnectionStringsToWrapperFromPolicy(wrapper, policy.Properties);
6870
return wrapper;
6971
}
7072

@@ -86,7 +88,6 @@ private AuditingPolicy WrapPolicy(DatabaseSecurityPolicy policy)
8688
wrapper.DirectAccessEnabled = !properties.IsBlockDirectAccessEnabled;
8789
addStorageInfoToWrapperFromPolicy(wrapper, properties);
8890
AddEventTypesToWrapperFromPolicy(wrapper, properties);
89-
AddConnectionStringsToWrapperFromPolicy(wrapper, properties);
9091
this.FetchedProperties = properties;
9192
return wrapper;
9293
}
@@ -102,10 +103,69 @@ private void addStorageInfoToWrapperFromPolicy(AuditingPolicy wrapper, DatabaseS
102103

103104
private void AddConnectionStringsToWrapperFromPolicy(AuditingPolicy wrapper, DatabaseSecurityPolicyProperties properties)
104105
{
105-
wrapper.ConnectionStrings.AdoNetConnectionString = properties.AdoNetConnectionString;
106-
wrapper.ConnectionStrings.OdbcConnectionString = properties.OdbcConnectionString;
107-
wrapper.ConnectionStrings.JdbcConnectionString = properties.JdbcConnectionString;
108-
wrapper.ConnectionStrings.PhpConnectionString = properties.PhpConnectionString;
106+
wrapper.ConnectionStrings.AdoNetConnectionString = ConstructAdoNetConnectionString(wrapper, properties);
107+
wrapper.ConnectionStrings.OdbcConnectionString = ConstructOdbcConnectionString(wrapper, properties);
108+
wrapper.ConnectionStrings.JdbcConnectionString = ConstructJdbcConnectionString(wrapper, properties);
109+
wrapper.ConnectionStrings.PhpConnectionString = ConstructPhpConnectionString(wrapper, properties);
110+
}
111+
112+
private string ConstructPhpConnectionString(AuditingPolicy wrapper, DatabaseSecurityPolicyProperties properties)
113+
{
114+
string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
115+
string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
116+
string pdoTitle = Microsoft.Azure.Commands.Sql.Properties.Resources.PdoTitle;
117+
string sqlServerSampleTitle = Microsoft.Azure.Commands.Sql.Properties.Resources.sqlSampleTitle;
118+
string connectionError = Microsoft.Azure.Commands.Sql.Properties.Resources.PhpConnectionError;
119+
StringBuilder sb = new StringBuilder();
120+
sb.Append(string.Format("Server: {0}, {1}", properties.ProxyDnsName, properties.ProxyPort)).Append(Environment.NewLine);
121+
sb.Append(string.Format("SQL Database: {0}", wrapper.DatabaseName)).Append(Environment.NewLine);
122+
sb.Append(string.Format("User Name: {0}", enterUser)).Append(Environment.NewLine).Append(Environment.NewLine);
123+
sb.Append(pdoTitle).Append(Environment.NewLine);
124+
sb.Append("try{").Append(Environment.NewLine);
125+
sb.Append(string.Format("$conn = new PDO ( \"sqlsrv:server = tcp:{0},{1}; Database = \"{2}\", \"{3}\", \"{4}\");",
126+
properties.ProxyDnsName, properties.ProxyPort, wrapper.DatabaseName, enterUser, enterPassword)).Append(Environment.NewLine);
127+
sb.Append("$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );").Append(Environment.NewLine);
128+
sb.Append("}").Append(Environment.NewLine);
129+
sb.Append("catch ( PDOException $e ) {").Append(Environment.NewLine);
130+
sb.Append(string.Format("print( \"{0}\" );", connectionError)).Append(Environment.NewLine);
131+
sb.Append("die(print_r($e));").Append(Environment.NewLine);
132+
sb.Append("}").Append(Environment.NewLine);
133+
sb.Append(sqlServerSampleTitle).Append(Environment.NewLine).Append(Environment.NewLine);
134+
sb.Append(string.Format("connectionInfo = array(\"UID\" => \"{0}@{1}\", \"pwd\" => \"{2}\", \"Database\" => \"{3}\", \"LoginTimeout\" => 30, \"Encrypt\" => 1);",
135+
enterUser, wrapper.ServerName, enterPassword, wrapper.DatabaseName)).Append(Environment.NewLine);
136+
sb.Append(string.Format("$serverName = \"tcp:{0},{1}\";", properties.ProxyDnsName, properties.ProxyPort)).Append(Environment.NewLine);
137+
sb.Append("$conn = sqlsrv_connect($serverName, $connectionInfo);");
138+
return sb.ToString();
139+
}
140+
141+
private string ConstructOdbcConnectionString(AuditingPolicy wrapper, DatabaseSecurityPolicyProperties properties)
142+
{
143+
string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
144+
string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
145+
StringBuilder sb = new StringBuilder();
146+
sb.Append("Driver={SQL Server Native Client 11.0};");
147+
sb.Append(string.Format("Server=tcp:{0},{1};", properties.ProxyDnsName, properties.ProxyPort));
148+
sb.Append(string.Format("Database={0};", wrapper.DatabaseName));
149+
sb.Append(string.Format("Uid={0}@{1};", enterUser, wrapper.ServerName));
150+
sb.Append(string.Format("Pwd={0};", enterPassword));
151+
sb.Append("Encrypt=yes;Connection Timeout=30;");
152+
return sb.ToString();
153+
}
154+
155+
private string ConstructJdbcConnectionString(AuditingPolicy wrapper, DatabaseSecurityPolicyProperties properties)
156+
{
157+
string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
158+
string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
159+
return string.Format("jdbc:sqlserver://{0}:{1};database={2};user={3}@{4};password={5};encrypt=true;hostNameInCertificate=*.database.secure.windows.net;loginTimeout=30;",
160+
properties.ProxyDnsName, properties.ProxyPort, wrapper.DatabaseName, enterUser, wrapper.ServerName, enterPassword);
161+
}
162+
163+
private string ConstructAdoNetConnectionString(AuditingPolicy wrapper,DatabaseSecurityPolicyProperties properties)
164+
{
165+
string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
166+
string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
167+
return string.Format("Server=tcp:{0},{1};Database={2};User ID={3}@{4};Password={5};Trusted_Connection=False;Encrypt=True;Connection Timeout=30",
168+
properties.ProxyDnsName, properties.ProxyPort, wrapper.DatabaseName, enterUser, wrapper.ServerName, enterPassword);
109169
}
110170

111171
private void AddEventTypesToWrapperFromPolicy(AuditingPolicy wrapper, DatabaseSecurityPolicyProperties properties)

src/ResourceManager/Sql/Commands.Sql/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Azure.Management.Resources" version="2.7.0-preview" targetFramework="net45" />
4-
<package id="Microsoft.Azure.Management.Sql" version="0.14.0-prerelease" targetFramework="net45" />
4+
<package id="Microsoft.Azure.Management.Sql" version="0.14.3-prerelease" targetFramework="net45" />
55
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
66
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
77
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />

0 commit comments

Comments
 (0)