PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/Sql/Commands.Sql/Secure Connection/Model/ConnectionStrings.cs

https://gitlab.com/jslee1/azure-powershell
C# | 124 lines | 74 code | 10 blank | 40 comment | 0 complexity | fb4d0dd2ae8fcd9c2ee05c9bd33cf3d9 MD5 | raw file
  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. using System;
  15. using System.Text;
  16. namespace Microsoft.Azure.Commands.Sql.SecureConnection.Model
  17. {
  18. /// <summary>
  19. /// A class representing the secure connection strings
  20. /// </summary>
  21. public class ConnectionStrings
  22. {
  23. public ConnectionStrings(string proxyDnsName, string port, string serverName, string dbName)
  24. {
  25. AdoNetConnectionString = ConstructAdoNetConnectionString(proxyDnsName, port, serverName, dbName);
  26. OdbcConnectionString = ConstructOdbcConnectionString(proxyDnsName, port, serverName, dbName);
  27. PhpConnectionString = ConstructPhpConnectionString(proxyDnsName, port, serverName, dbName);
  28. JdbcConnectionString = ConstructJdbcConnectionString(proxyDnsName, port, serverName, dbName);
  29. }
  30. /// <summary>
  31. /// Gets the Ado.Net connection string
  32. /// </summary>
  33. public string AdoNetConnectionString { get; internal set; }
  34. /// <summary>
  35. /// Gets the ODBC connection string
  36. /// </summary>
  37. public string OdbcConnectionString { get; internal set; }
  38. /// <summary>
  39. /// Gets the PhP connection string
  40. /// </summary>
  41. public string PhpConnectionString { get; internal set; }
  42. /// <summary>
  43. /// Gets the JDBC connection string
  44. /// </summary>
  45. public string JdbcConnectionString { get; internal set; }
  46. /// <summary>
  47. /// Constructs the PhP connection string
  48. /// </summary>
  49. private string ConstructPhpConnectionString(string proxyDnsName, string port, string serverName, string databaseName)
  50. {
  51. string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
  52. string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
  53. string pdoTitle = Microsoft.Azure.Commands.Sql.Properties.Resources.PdoTitle;
  54. string sqlServerSampleTitle = Microsoft.Azure.Commands.Sql.Properties.Resources.sqlSampleTitle;
  55. string connectionError = Microsoft.Azure.Commands.Sql.Properties.Resources.PhpConnectionError;
  56. StringBuilder sb = new StringBuilder();
  57. sb.Append(string.Format("Server: {0}, {1}", proxyDnsName, port)).Append(Environment.NewLine);
  58. sb.Append(string.Format("SQL Database: {0}", databaseName)).Append(Environment.NewLine);
  59. sb.Append(string.Format("User Name: {0}", enterUser)).Append(Environment.NewLine).Append(Environment.NewLine);
  60. sb.Append(pdoTitle).Append(Environment.NewLine);
  61. sb.Append("try{").Append(Environment.NewLine);
  62. sb.Append(string.Format("$conn = new PDO ( \"sqlsrv:server = tcp:{0},{1}; Database = \"{2}\", \"{3}\", \"{4}\");",
  63. proxyDnsName, port, databaseName, enterUser, enterPassword)).Append(Environment.NewLine);
  64. sb.Append("$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );").Append(Environment.NewLine);
  65. sb.Append("}").Append(Environment.NewLine);
  66. sb.Append("catch ( PDOException $e ) {").Append(Environment.NewLine);
  67. sb.Append(string.Format("print( \"{0}\" );", connectionError)).Append(Environment.NewLine);
  68. sb.Append("die(print_r($e));").Append(Environment.NewLine);
  69. sb.Append("}").Append(Environment.NewLine);
  70. sb.Append(sqlServerSampleTitle).Append(Environment.NewLine).Append(Environment.NewLine);
  71. sb.Append(string.Format("connectionInfo = array(\"UID\" => \"{0}@{1}\", \"pwd\" => \"{2}\", \"Database\" => \"{3}\", \"LoginTimeout\" => 30, \"Encrypt\" => 1);",
  72. enterUser, serverName, enterPassword, databaseName)).Append(Environment.NewLine);
  73. sb.Append(string.Format("$serverName = \"tcp:{0},{1}\";", proxyDnsName, port)).Append(Environment.NewLine);
  74. sb.Append("$conn = sqlsrv_connect($serverName, $connectionInfo);");
  75. return sb.ToString();
  76. }
  77. /// <summary>
  78. /// Constructs the ODBC connection string
  79. /// </summary>
  80. private string ConstructOdbcConnectionString(string proxyDnsName, string port, string serverName, string databaseName)
  81. {
  82. string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
  83. string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
  84. StringBuilder sb = new StringBuilder();
  85. sb.Append("Driver={SQL Server Native Client 11.0};");
  86. sb.Append(string.Format("Server=tcp:{0},{1};", proxyDnsName, port));
  87. sb.Append(string.Format("Database={0};", databaseName));
  88. sb.Append(string.Format("Uid={0}@{1};", enterUser, serverName));
  89. sb.Append(string.Format("Pwd={0};", enterPassword));
  90. sb.Append("Encrypt=yes;Connection Timeout=30;");
  91. return sb.ToString();
  92. }
  93. /// <summary>
  94. /// Constructs the JDBC connection string
  95. /// </summary>
  96. private string ConstructJdbcConnectionString(string proxyDnsName, string port, string serverName, string databaseName)
  97. {
  98. string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
  99. string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
  100. return string.Format("jdbc:sqlserver://{0}:{1};database={2};user={3}@{4};password={5};encrypt=true;hostNameInCertificate=*.database.secure.windows.net;loginTimeout=30;",
  101. proxyDnsName, port, databaseName, enterUser, serverName, enterPassword);
  102. }
  103. /// <summary>
  104. /// Constructs the ADO.NET connection string
  105. /// </summary>
  106. private string ConstructAdoNetConnectionString(string proxyDnsName, string port, string serverName, string databaseName)
  107. {
  108. string enterUser = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterUserId;
  109. string enterPassword = Microsoft.Azure.Commands.Sql.Properties.Resources.EnterPassword;
  110. return string.Format("Server=tcp:{0},{1};Database={2};User ID={3}@{4};Password={5};Trusted_Connection=False;Encrypt=True;Connection Timeout=30",
  111. proxyDnsName, port, databaseName, enterUser, serverName, enterPassword);
  112. }
  113. }
  114. }