/
Settings.cs
298 lines (236 loc) · 11 KB
/
Settings.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
namespace ServiceControl.Audit.Infrastructure.Settings
{
using System;
using System.Configuration;
using Configuration;
using NLog.Common;
using NServiceBus.Logging;
using NServiceBus.Transport;
using Transports;
public class Settings
{
// Service name is what the user chose when installing the instance or is passing on the command line.
// We use this as the default endpoint name.
public static Settings FromConfiguration(string serviceName) =>
new(
SettingsReader.Read(SettingsRootNamespace, "InternalQueueName", serviceName) // endpoint name can also be overriden via config
);
public Settings(string serviceName, string transportType = null, string persisterType = null)
{
ServiceName = serviceName;
TransportType = transportType ?? SettingsReader.Read<string>(SettingsRootNamespace, "TransportType");
PersistenceType = persisterType ?? SettingsReader.Read<string>(SettingsRootNamespace, "PersistenceType");
TransportConnectionString = GetConnectionString();
LoadAuditQueueInformation();
TryLoadLicenseFromConfig();
ForwardAuditMessages = GetForwardAuditMessages();
AuditRetentionPeriod = GetAuditRetentionPeriod();
Port = SettingsReader.Read(SettingsRootNamespace, "Port", 44444);
MaximumConcurrencyLevel = SettingsReader.Read(SettingsRootNamespace, "MaximumConcurrencyLevel", 32);
DataSpaceRemainingThreshold = GetDataSpaceRemainingThreshold();
ServiceControlQueueAddress = SettingsReader.Read<string>(SettingsRootNamespace, "ServiceControlQueueAddress");
TimeToRestartAuditIngestionAfterFailure = GetTimeToRestartAuditIngestionAfterFailure();
EnableFullTextSearchOnBodies = SettingsReader.Read(SettingsRootNamespace, "EnableFullTextSearchOnBodies", true);
}
void LoadAuditQueueInformation()
{
var serviceBusRootNamespace = new SettingsRootNamespace("ServiceBus");
AuditQueue = SettingsReader.Read(serviceBusRootNamespace, "AuditQueue", "audit");
if (string.IsNullOrEmpty(AuditQueue))
{
throw new Exception("ServiceBus/AuditQueue value is required to start the instance");
}
IngestAuditMessages = SettingsReader.Read(new SettingsRootNamespace("ServiceControl"), "IngestAuditMessages", true);
if (IngestAuditMessages == false)
{
logger.Info("Audit ingestion disabled.");
}
AuditLogQueue = SettingsReader.Read<string>(serviceBusRootNamespace, "AuditLogQueue", null);
if (AuditLogQueue == null)
{
logger.Info("No settings found for audit log queue to import, default name will be used");
AuditLogQueue = Subscope(AuditQueue);
}
}
//HINT: acceptance tests only
public Func<MessageContext, bool> MessageFilter { get; set; }
public bool ValidateConfiguration => SettingsReader.Read(SettingsRootNamespace, "ValidateConfig", true);
public bool SkipQueueCreation { get; set; }
public string RootUrl
{
get
{
var suffix = string.Empty;
if (!string.IsNullOrEmpty(VirtualDirectory))
{
suffix = $"{VirtualDirectory}/";
}
return $"http://{Hostname}:{Port}/{suffix}";
}
}
public string ApiUrl => $"{RootUrl}api";
public int Port { get; set; }
public bool PrintMetrics => SettingsReader.Read<bool>(SettingsRootNamespace, "PrintMetrics");
public string Hostname => SettingsReader.Read(SettingsRootNamespace, "Hostname", "localhost");
public string VirtualDirectory => SettingsReader.Read(SettingsRootNamespace, "VirtualDirectory", string.Empty);
public string TransportType { get; private set; }
public string PersistenceType { get; private set; }
public string AuditQueue { get; set; }
public bool ForwardAuditMessages { get; set; }
public bool IngestAuditMessages { get; set; } = true;
public string AuditLogQueue { get; set; }
public string LicenseFileText { get; set; }
public TimeSpan AuditRetentionPeriod { get; }
public int MaxBodySizeToStore
{
get
{
if (maxBodySizeToStore <= 0)
{
logger.Error($"MaxBodySizeToStore settings is invalid, {1} is the minimum value. Defaulting to {MaxBodySizeToStoreDefault}");
return MaxBodySizeToStoreDefault;
}
return maxBodySizeToStore;
}
set => maxBodySizeToStore = value;
}
public string ServiceName { get; }
public string TransportConnectionString { get; set; }
public int MaximumConcurrencyLevel { get; set; }
public int DataSpaceRemainingThreshold { get; set; }
public string ServiceControlQueueAddress { get; set; }
public TimeSpan TimeToRestartAuditIngestionAfterFailure { get; set; }
public bool EnableFullTextSearchOnBodies { get; set; }
public ITransportCustomization LoadTransportCustomization()
{
try
{
TransportType = TransportManifestLibrary.Find(TransportType);
var customizationType = Type.GetType(TransportType, true);
return (ITransportCustomization)Activator.CreateInstance(customizationType);
}
catch (Exception e)
{
throw new Exception($"Could not load transport customization type {TransportType}.", e);
}
}
TimeSpan GetTimeToRestartAuditIngestionAfterFailure()
{
string message;
var valueRead = SettingsReader.Read<string>(SettingsRootNamespace, "TimeToRestartAuditIngestionAfterFailure");
if (valueRead == null)
{
return TimeSpan.FromSeconds(60);
}
if (TimeSpan.TryParse(valueRead, out var result))
{
if (ValidateConfiguration && result < TimeSpan.FromSeconds(5))
{
message = $"{nameof(TimeToRestartAuditIngestionAfterFailure)} setting is invalid, value should be minimum 5 seconds.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
if (ValidateConfiguration && result > TimeSpan.FromHours(1))
{
message = $"{nameof(TimeToRestartAuditIngestionAfterFailure)} setting is invalid, value should be maximum 1 hour.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
}
else
{
message = $"{nameof(TimeToRestartAuditIngestionAfterFailure)} setting is invalid, please make sure it is a TimeSpan.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
return result;
}
static bool GetForwardAuditMessages()
{
var forwardAuditMessages = SettingsReader.Read<bool?>(SettingsRootNamespace, "ForwardAuditMessages");
if (forwardAuditMessages.HasValue)
{
return forwardAuditMessages.Value;
}
return false;
}
static string GetConnectionString()
{
var settingsValue = SettingsReader.Read<string>(SettingsRootNamespace, "ConnectionString");
if (settingsValue != null)
{
return settingsValue;
}
var connectionStringSettings = ConfigurationManager.ConnectionStrings["NServiceBus/Transport"];
return connectionStringSettings?.ConnectionString;
}
TimeSpan GetAuditRetentionPeriod()
{
string message;
var valueRead = SettingsReader.Read<string>(SettingsRootNamespace, "AuditRetentionPeriod");
if (valueRead == null)
{
//same default as SCMU
return TimeSpan.FromDays(30);
}
if (TimeSpan.TryParse(valueRead, out var result))
{
if (ValidateConfiguration && result < TimeSpan.FromHours(1))
{
message = "AuditRetentionPeriod settings is invalid, value should be minimum 1 hour.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
if (ValidateConfiguration && result > TimeSpan.FromDays(365))
{
message = "AuditRetentionPeriod settings is invalid, value should be maximum 365 days.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
}
else
{
message = "AuditRetentionPeriod settings is invalid, please make sure it is a TimeSpan.";
InternalLogger.Fatal(message);
throw new Exception(message);
}
return result;
}
static string Subscope(string address)
{
var atIndex = address.IndexOf("@", StringComparison.InvariantCulture);
if (atIndex <= -1)
{
return $"{address}.log";
}
var queue = address.Substring(0, atIndex);
var machine = address.Substring(atIndex + 1);
return $"{queue}.log@{machine}";
}
int GetDataSpaceRemainingThreshold()
{
string message;
var threshold = SettingsReader.Read(SettingsRootNamespace, "DataSpaceRemainingThreshold", DataSpaceRemainingThresholdDefault);
if (threshold < 0)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, minimum value is 0.";
logger.Fatal(message);
throw new Exception(message);
}
if (threshold > 100)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, maximum value is 100.";
logger.Fatal(message);
throw new Exception(message);
}
return threshold;
}
void TryLoadLicenseFromConfig() => LicenseFileText = SettingsReader.Read<string>(SettingsRootNamespace, "LicenseText");
ILog logger = LogManager.GetLogger(typeof(Settings));
int maxBodySizeToStore = SettingsReader.Read(SettingsRootNamespace, "MaxBodySizeToStore", MaxBodySizeToStoreDefault);
public const string DEFAULT_SERVICE_NAME = "Particular.ServiceControl.Audit";
public static readonly SettingsRootNamespace SettingsRootNamespace = new("ServiceControl.Audit");
const int MaxBodySizeToStoreDefault = 102400; //100 kb
const int DataSpaceRemainingThresholdDefault = 20;
}
}