forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionConfig.cs
386 lines (347 loc) · 15.1 KB
/
CollectionConfig.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using DBADashService;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SqlServer.TransactSql.ScriptDom;
using static DBADash.DBADashConnection;
namespace DBADash
{
public class CollectionConfig : BasicConfig
{
public Int32 ServiceThreads = -1;
private string _secretKey;
private bool wasEncryptionPerformed;
private readonly string myString = "g&hAs2&mVOLwE6DqO!I5";
public SchemaSnapshotDBOptions SchemaSnapshotOptions = null;
public bool ScanForAzureDBs { get; set; } = true;
public Int32 ScanForAzureDBsInterval { get; set; } = 3600;
public string ServiceName { get; set; } = "DBADashService";
public bool AutoUpdateDatabase { get; set; } = true;
public bool LogInternalPerformanceCounters = false;
public int? IdentityCollectionThreshold = null;
public CollectionSchedules CollectionSchedules;
public Dictionary<string, CustomCollection> CustomCollections { get; set; } = new();
public string SummaryRefreshCron { get; set; }
public int? ImportCommandTimeout { get; set; }
public int? PurgeDataCommandTimeout { get; set; }
public int? AddPartitionsCommandTimeout { get; set; }
public bool EnableMessaging { get; set; }
public bool AllowPlanForcing { get; set; }
public string ServiceSQSQueueUrl { get; set; }
public CollectionSchedules GetSchedules()
{
if (CollectionSchedules == null)
{
return CollectionSchedules.DefaultSchedules;
}
else
{
return CollectionSchedules.CombineWithDefault();
}
}
public List<DBADashSource> SourceConnections = new();
public static CollectionConfig Deserialize(string json, string password = null)
{
return BasicConfig.Deserialize<CollectionConfig>(json, password);
}
public string AWSProfile { get; set; }
public string AccessKey { get; set; }
public string SecretKey
{
get => _secretKey;
set
{
if (value == "")
{
_secretKey = null;
}
else if (value == null || value.StartsWith("¬=!"))
{
_secretKey = value;
}
else
{
_secretKey = "¬=!" + value.EncryptString(myString);
wasEncryptionPerformed = true;
}
}
}
public string GetSecretKey()
{
if (_secretKey != null && _secretKey.StartsWith("¬=!"))
{
return _secretKey[3..].DecryptString(myString);
}
else
{
return _secretKey;
}
}
private const string defaultMaintenanceCron = "0 0 0/12 1/1 * ? *";
public string MaintenanceScheduleCron { get; set; }
public string GetMaintenanceCron()
{
return MaintenanceScheduleCron is null or "" ? defaultMaintenanceCron : MaintenanceScheduleCron;
}
[JsonIgnore]
public List<DBADashConnection> SecondaryDestinationConnections { get; set; } = new();
// Required for serialization
public string[] SecondaryDestinations
{
get
{
var encryptedStrings = new List<string>();
foreach (var d in SecondaryDestinationConnections)
{
encryptedStrings.Add(d.EncryptedConnectionString);
}
return encryptedStrings.ToArray();
}
set
{
SecondaryDestinationConnections = new List<DBADashConnection>();
foreach (var s in value.Distinct().ToArray())
{
SecondaryDestinationConnections.Add(new DBADashConnection(s));
}
}
}
[JsonIgnore]
public List<DBADashConnection> AllDestinations
{
get
{
var all = new List<DBADashConnection>();
if (!string.IsNullOrEmpty(Destination))
{
all.Add(DestinationConnection);
}
all.AddRange(SecondaryDestinationConnections);
return all;
}
}
public bool WasEncrypted()
{
if (wasEncryptionPerformed)
{
return wasEncryptionPerformed;
}
if (DestinationConnection.WasEncrypted)
{
wasEncryptionPerformed = true;
}
else
{
foreach (var c in SourceConnections)
{
if (c.SourceConnection.WasEncrypted)
{
wasEncryptionPerformed = true;
}
}
}
return wasEncryptionPerformed;
}
public int? MessageThreads { get; set; }
public void ValidateDestination()
{
ValidateDestination(DestinationConnection);
}
public static void ValidateDestination(DBADashConnection destination)
{
if (destination.Type == ConnectionType.Invalid)
{
throw new Exception("Invalid connection string");
}
else if (destination.Type == ConnectionType.SQL)
{
if (string.IsNullOrEmpty(destination.InitialCatalog()))
{
throw new Exception("Provide a name for the DBADash repository database through the Initial Catalog property of the connection string");
}
// VersionStatus check will throw an error if there is an issue connecting to the DB or the DB isn't valid.
var status = DBValidations.VersionStatus(destination.ConnectionString);
if (status.VersionStatus == DBValidations.DBVersionStatusEnum.CreateDB)
{
ValidateDestinationVersion(destination);
}
Log.Information("DB Version Status: {status}", status.VersionStatus);
}
else
{
destination.Validate();
}
}
private static void ValidateDestinationVersion(DBADashConnection destination)
{
var master = destination.MasterConnection();
var cInfo = master.ConnectionInfo;
if (string.IsNullOrEmpty(cInfo.ServerName))
{
Log.Warning("@@SERVERNAME returned NULL for {connection}. Consider fixing with sp_addserver", destination.ConnectionForPrint);
}
if (cInfo.MajorVersion < 13 && cInfo.EngineEdition != Microsoft.SqlServer.Management.Common.DatabaseEngineEdition.SqlDatabase && cInfo.EngineEdition != Microsoft.SqlServer.Management.Common.DatabaseEngineEdition.SqlManagedInstance) // 13=2016, 12 might be Azure DB which is OK
{
throw new Exception("DBA Dash repository database requires SQL 2016 SP1 or later");
}
else if (cInfo.MajorVersion == 13 && (new Version(cInfo.ProductVersion)).CompareTo(new Version("13.0.4001.0")) < 0 && cInfo.EngineEdition != Microsoft.SqlServer.Management.Common.DatabaseEngineEdition.Enterprise)
{ // Check if we are running SP1 for SQL 2016
throw new Exception("DBA Dash repository database requires SQL 2016 SP1 or later. Please upgrade to SP1 or later.");
}
}
public DBADashSource GetSourceFromConnectionString(string connectionString, bool? isAzure = null)
{
var findConnection = new DBADashConnection(connectionString);
foreach (var s in SourceConnections.Where(s => s.SourceConnection.Type == findConnection.Type))
{
if (s.SourceConnection.Type == ConnectionType.SQL && string.Equals(s.SourceConnection.DataSource(), findConnection.DataSource(), StringComparison.CurrentCultureIgnoreCase) && s.SourceConnection.ApplicationIntent() == findConnection.ApplicationIntent())
{
// normally we can treat as same connection if we just vary by initial catalog. For AzureDB, a different DB is a different instance
if (string.Equals(s.SourceConnection.InitialCatalog(), findConnection.InitialCatalog(), StringComparison.CurrentCultureIgnoreCase))
{
return s;
}
else if ((new[] { "master", string.Empty }).Contains(s.SourceConnection.InitialCatalog().ToLower())
&& (new[] { "master", string.Empty }).Contains(findConnection.InitialCatalog().ToLower()))
{
return s;
}
else
{
isAzure ??= findConnection.IsAzureDB();
if (isAzure == false)
{
return s;
}
}
}
else if (s.SourceConnection.ConnectionString == findConnection.ConnectionString)
{
return s;
}
}
return null;
}
public bool SourceExists(string connectionString, bool? isAzure = null)
{
return GetSourceFromConnectionString(connectionString, isAzure) != null;
}
public void AddConnections(List<DBADashSource> connections)
{
SourceConnections.AddRange(connections);
}
public List<DBADashSource> AddAzureDBs()
{
var newConnections = GetNewAzureDBConnections();
lock (SourceConnections)
{
SourceConnections.AddRange(newConnections);
}
return newConnections;
}
public List<DBADashSource> AddAzureDBs(DBADashSource masterConnection)
{
var newConnections = GetNewAzureDBConnections(masterConnection);
SourceConnections.AddRange(newConnections);
return newConnections;
}
public List<DBADashSource> GetNewAzureDBConnections()
{
var newConnections = new List<DBADashSource>();
foreach (var cfg in SourceConnections.Where(src => src.SourceConnection.Type == ConnectionType.SQL))
{
try
{
if ((new[] { "master", String.Empty }).Contains(cfg.SourceConnection.InitialCatalog()) && cfg.SourceConnection.ConnectionInfo.IsAzureMasterDB)
{
newConnections.AddRange(GetNewAzureDBConnections(cfg));
}
}
catch (Exception ex)
{
Log.Error(ex, "Error getting Azure DB connections from master {connection}", cfg.SourceConnection.ConnectionForPrint);
}
}
return newConnections;
}
public List<DBADashSource> GetNewAzureDBConnections(DBADashSource masterConnection)
{
var newConnections = new List<DBADashSource>();
using (var cn = new SqlConnection(masterConnection.SourceConnection.ConnectionString))
using (var cmd = new SqlCommand("SELECT name from sys.databases WHERE name <> 'master'", cn))
{
cn.Open();
using var rdr = cmd.ExecuteReader();
var builder = new SqlConnectionStringBuilder(masterConnection.SourceConnection.ConnectionString);
while (rdr.Read())
{
builder.InitialCatalog = rdr.GetString(0);
var dbCn = masterConnection.DeepCopy();
dbCn.SourceConnection.ConnectionString = builder.ConnectionString;
dbCn.ConnectionID = string.Empty;
if (!SourceExists(dbCn.SourceConnection.ConnectionString, true))
{
newConnections.Add(dbCn);
Log.Information("Add Azure DB connection {DataSource}|{DB}", builder.DataSource, builder.InitialCatalog);
}
}
}
return newConnections;
}
public override bool ContainsSensitive()
{
if (!string.IsNullOrEmpty(SecretKey))
{
return true;
}
foreach (var src in SourceConnections)
{
if (src.SourceConnection.Type == ConnectionType.SQL && new SqlConnectionStringBuilder(src.SourceConnection.ConnectionString).Password.Length > 0)
{
return true;
}
}
return base.ContainsSensitive();
}
public DBADashConnection GetDestination(string hash)
{
return DestinationConnection.Hash == hash ? DestinationConnection : SecondaryDestinationConnections.FirstOrDefault(c => c.Hash == hash);
}
public DBADashSource GetSourceConnection(string connectionID)
{
var src = SourceConnections.FirstOrDefault(s => string.Equals(s.ConnectionID, connectionID, StringComparison.InvariantCultureIgnoreCase));
if (src != null) // We have a match on ConnectionID
{
return src;
}
else if (connectionID.Contains('|') && ScanForAzureDBs) // We don't have a match but ConnectionID looks like an AzureDB connection.
{
// Try to find the master connection for this AzureDB
var masterInstanceName = connectionID.Split('|')[0] + "|master";
var masterSrc = SourceConnections.FirstOrDefault(s => string.Equals(s.ConnectionID, masterInstanceName, StringComparison.InvariantCultureIgnoreCase));
if (masterSrc != null)
{
// Master connection found. Create a copy with the correct database name
src = masterSrc.DeepCopy();
src.ConnectionID = null;
var builder = new SqlConnectionStringBuilder(masterSrc.SourceConnection.ConnectionString)
{
InitialCatalog = connectionID.Split('|')[1]
};
src.SourceConnection.ConnectionString = builder.ToString();
var collector = new DBCollector(src, ServiceName);
src.ConnectionID = collector.ConnectionID;
// Double check that the generated ConnectionID matches the one we're looking for & return the connection
if (string.Equals(src.ConnectionID, connectionID, StringComparison.InvariantCultureIgnoreCase))
{
return src;
}
}
}
throw new ArgumentException($"Unable to find instance with ConnectionID {connectionID}");
}
}
}