-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
DBADashConnection.cs
328 lines (293 loc) · 9.93 KB
/
DBADashConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using DBADashService;
using Newtonsoft.Json;
using System;
using Microsoft.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;
using Serilog;
using System.Collections.Generic;
namespace DBADash
{
public class DBADashConnection
{
private List<int> supportedEngineEditions = new List<int> { 1, 2, 3, 4, 5, 8 }; // Personal, Standard, Enterprise, Express, Azure DB, Azure MI
private List<int> supportedProductVersions = new List<int> { 9, 10, 11, 12, 13, 14, 15, 16 }; // SQL 2005 to 2022 & Azure
public enum ConnectionType
{
SQL,
Directory,
AWSS3,
Invalid
}
private readonly string myString = "g&hAs2&mVOLwE6DqO!I5";
private bool wasEncryptionPerformed = false;
private bool isEncrypted = false;
private string encryptedConnectionString = "";
private string connectionString = "";
private ConnectionType connectionType;
public bool WasEncrypted
{
get
{
return wasEncryptionPerformed;
}
}
public bool IsEncrypted
{
get
{
return isEncrypted;
}
}
public DBADashConnection(string connectionString)
{
setConnectionString(connectionString);
}
public DBADashConnection()
{
}
private void setConnectionString(string value)
{
if (getConnectionType(value) == ConnectionType.SQL){
var builder = new SqlConnectionStringBuilder(value)
{
ApplicationName = "DBADash"
};
value = builder.ToString();
}
encryptedConnectionString = getConnectionStringWithEncryptedPassword(value);
connectionString = getDecryptedConnectionString(value);
connectionType = getConnectionType(value);
}
[JsonIgnore]
public string ConnectionString
{
get
{
return connectionString;
}
set
{
setConnectionString(value);
}
}
[JsonIgnore]
public string MasterConnectionString
{
get
{
var builder = new SqlConnectionStringBuilder(ConnectionString);
builder.InitialCatalog = "master";
return builder.ToString();
}
}
public DBADashConnection MasterConnection()
{
return new DBADashConnection(MasterConnectionString);
}
public string EncryptedConnectionString
{
get
{
return encryptedConnectionString;
}
set
{
setConnectionString(value);
}
}
private ConnectionInfo _connectionInfo;
[JsonIgnore]
public ConnectionInfo ConnectionInfo
{
get
{
if (_connectionInfo == null && connectionType == ConnectionType.SQL)
{
_connectionInfo = ConnectionInfo.GetConnectionInfo(connectionString);
}
return _connectionInfo;
}
}
public void Validate()
{
if (connectionType == ConnectionType.Directory)
{
if (System.IO.Directory.Exists(connectionString) == false)
{
throw new Exception("Directory does not exist");
}
}
else if (connectionType == ConnectionType.SQL)
{
validateSQLConnection(); // Open a connection to the DB
}
}
public bool IsXESupported()
{
if (connectionType == ConnectionType.SQL)
{
return ConnectionInfo.IsXESupported;
}
else
{
return false;
}
}
public static bool IsXESupported(string productVersion)
{
return ConnectionInfo.GetXESupported(productVersion);
}
public bool IsAzureDB()
{
return ConnectionInfo.IsAzureDB;
}
private void validateSQLConnection()
{
if (!supportedProductVersions.Contains(ConnectionInfo.MajorVersion)){
throw new Exception(string.Format("SQL Server Version {0} isn't supported by DBA Dash. For testing purposes, it's possible to skip this validation check.",ConnectionInfo.MajorVersion));
}
if(!supportedEngineEditions.Contains(ConnectionInfo.EngineEditionValue)){
throw new Exception(string.Format("SQL Server Engine Edition {0} isn't supported by DBA Dash. For testing purposes, it's possible to skip this validation check.", ConnectionInfo.EngineEditionValue));
}
}
public ConnectionType Type
{
get
{
return connectionType;
}
}
public string InitialCatalog()
{
if (connectionType == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
return builder.InitialCatalog;
}
else
{
return "";
}
}
public string DataSource()
{
if (connectionType == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
return builder.DataSource;
}
else
{
return "";
}
}
private ConnectionType getConnectionType(string connectionString)
{
if (connectionString == null || connectionString.Length < 3)
{
return ConnectionType.Invalid;
}
else if (connectionString.StartsWith("s3://") || connectionString.StartsWith("https://"))
{
return ConnectionType.AWSS3;
}
else if (connectionString.StartsWith("\\\\") || connectionString.StartsWith("//") || connectionString.Substring(1, 2) == ":\\")
{
return ConnectionType.Directory;
}
else
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
return ConnectionType.SQL;
}
catch
{
return ConnectionType.Invalid;
}
}
}
private string getConnectionStringWithEncryptedPassword(string connectionString)
{
if (getConnectionType(connectionString) == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
if (builder.Password.StartsWith("¬=!"))
{
isEncrypted = true;
return connectionString;
}
else if (builder.Password.Length > 0)
{
builder.Password = "¬=!" + EncryptText.EncryptString(builder.Password, myString);
wasEncryptionPerformed = true;
isEncrypted = true;
return builder.ConnectionString;
}
else
{
return connectionString; ;
}
}
else
{
return connectionString;
}
}
private string getDecryptedConnectionString(string connectionString)
{
if (getConnectionType(connectionString) == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
if (builder.ApplicationName == ".Net SqlClient Data Provider")
{
builder.ApplicationName = "DBADash";
}
if (builder.Password.StartsWith("¬=!"))
{
builder.Password = EncryptText.DecryptString(builder.Password.Substring(3), myString);
}
return builder.ConnectionString;
}
else
{
return connectionString;
}
}
public string ConnectionForFileName
{
get
{
if (Type == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
string connection = builder.DataSource + (builder.InitialCatalog == "" ? "" : "_" + builder.InitialCatalog);
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
return r.Replace(connection, "");
}
else
{
throw new Exception("Invalid connection type for filename generation");
}
}
}
public string ConnectionForPrint
{
get
{
if (Type == ConnectionType.SQL)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
return builder.DataSource + (builder.InitialCatalog == "" ? "" : "|" + builder.InitialCatalog);
}
else
{
return connectionString;
}
}
}
}
}