-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
465 lines (382 loc) · 18.9 KB
/
Program.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using LogAnalytics.Client;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace fwlogs2loganalytics
{
public enum GuardianLogType
{
API_Log = 1,
Unknown_Log = 2
}
class Program
{
private static Dictionary<string, string> environment = new Dictionary<string, string>()
{
{"tableName", "apifirewall_log_1"},
{"tags", string.Empty},
{"tickInterval", "10"},
};
private static int inTimer = 0;
private static uint tickInterval = 10;
private static uint currentTick = 0;
private static bool trace = false;
private static LogAnalyticsClient laclient = null;
private static List<string> guardianStateFiles = new List<string> { };
private static List<string> seenFiles = new List<string> { };
private static ILogger logger;
static async void WriteEvent(GuardianEntity entity)
{
await laclient.SendLogEntry(entity, environment["tableName"]);
}
static void ProcessEvironment()
{
// Read all the evironment variables
environment["workspaceId"] = Environment.GetEnvironmentVariable("FW2LA_WORKSPACE_ID");
environment["workspaceKey"] = Environment.GetEnvironmentVariable("FW2LA_WORKSPACE_KEY");
if (string.IsNullOrEmpty(environment["workspaceKey"]))
throw new ArgumentException("No workspace key provided, unable to proceed");
if (string.IsNullOrEmpty(environment["workspaceId"]))
throw new ArgumentException("No workspace ID provided, unable to proceed");
environment["tableName"] = Environment.GetEnvironmentVariable("FW2LA_TABLENAME");
environment["tableName"] ??= "apifirewall_log_1";
environment["logs_folder"] = Environment.GetEnvironmentVariable("FW2LA_LOGS_FOLDER");
environment["logs_folder"] ??= Directory.GetCurrentDirectory();
environment["state_folder"] = Environment.GetEnvironmentVariable("FW2LA_STATE_FOLDER");
environment["state_folder"] ??= Path.Join(Directory.GetCurrentDirectory(), ".state");
environment["tickInterval"] = Environment.GetEnvironmentVariable("FW2LA_TICK_INTERVAL");
environment["tickInterval"] ??= "10";
tickInterval = uint.Parse(environment["tickInterval"]);
foreach (KeyValuePair<string, string> item in environment)
{
if (item.Key.Contains("KEY", StringComparison.CurrentCultureIgnoreCase)) {
Console.WriteLine("{0} -> {1}", item.Key, GetMaskedToken(item.Value));
}
else {
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
}
}
static dynamic ExamineFolder(string folder)
{
string apiLogFilePath = null;
string unknownLogFilePath = null;
string api_id = null;
if (! seenFiles.Contains(folder))
{
seenFiles.Add(folder);
}
var allFiles = Directory.EnumerateFiles(folder);
foreach (var thisFilePath in allFiles)
{
Console.WriteLine($" File = {thisFilePath}");
string thisFile = Path.GetFileName(thisFilePath);
Match match = Regex.Match(thisFile, "api-([\\w]{8}-[\\w]{4}-[\\w]{4}-[\\w]{4}-[\\w]{12}).transaction.log");
if (match.Success)
{
var md5sum = CalculateMD5Sum(thisFilePath);
apiLogFilePath = thisFilePath;
api_id = match.Groups[1].ToString();
}
if ("api-unknown.transaction.log".Equals(thisFile))
{
unknownLogFilePath = thisFilePath;
}
}
// If we're missing either file here we need to quit out
if (null == apiLogFilePath)
return null;
if (null == unknownLogFilePath)
return null;
return new { ApiID = api_id, ApiLogFilePath = apiLogFilePath, UnknownLogFilePath = unknownLogFilePath };
}
static void ProcessInitialState()
{
// Pull this out into a separate method
List<string> guardianInstanceFolders = null;
// Check the logs folder exists and exit if not
if (!Directory.Exists(environment["logs_folder"]))
{
throw new DirectoryNotFoundException("Logs source directory does not exist or is not accessible");
}
else
{
guardianInstanceFolders = new List<string>(Directory.EnumerateDirectories(environment["logs_folder"]));
}
// Check the state folder exists, create it if not
if (!Directory.Exists(environment["state_folder"]))
{
Directory.CreateDirectory(environment["state_folder"]);
}
// Create the empty state files
foreach (var thisFolder in guardianInstanceFolders)
{
// TODO : We should check the 'seenFiles' list and skip the folder if we've already seen it ?
Console.WriteLine($"Folder = {thisFolder}");
var instance_name = Path.GetFileName(thisFolder);
var logFiles = ExamineFolder(thisFolder);
if (null == logFiles)
{
Console.WriteLine($"Skipping folder {thisFolder} due to no logs found");
continue;
}
// Get the new state based on the log files
var newGs = new GuardianState();
var api_gfs = newGs;
newGs.API_ID = logFiles.ApiID;
newGs.Instance_Name = instance_name;
newGs.API_Log_File = GetFileState(logFiles.ApiLogFilePath);
newGs.Unknown_Log_File = GetFileState(logFiles.UnknownLogFilePath);
// Get the existing state file if it exists
string existingStateFilePath = Path.ChangeExtension(Path.Combine(environment["state_folder"], instance_name), ".json");
if (!File.Exists(existingStateFilePath))
{
Console.WriteLine("State file doesn't exist so writing it now ...");
// Here we need to set the lines written to zero or we'll never send the file
newGs.API_Log_File.LastLineSent = 0;
newGs.Unknown_Log_File.LastLineSent = 0;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(newGs, Formatting.Indented);
File.WriteAllText(existingStateFilePath, json);
}
else
{
Console.WriteLine("State file exists so reading it now ...");
// TODO : Why do we read this if we don't use it ?
var json = File.ReadAllText(existingStateFilePath);
GuardianState response = JsonConvert.DeserializeObject<GuardianState>(json);
}
guardianStateFiles.Add(existingStateFilePath);
}
}
private static void ProcessTimeEvent(Object o)
{
if (Interlocked.Exchange(ref inTimer, 1) == 0)
{
// Display the date/time when this method got called.
logger.LogDebug("42logs2sentinel OK at tick " + currentTick);
// This is designed to rollover - ignore warnings
currentTick = (ushort)(currentTick + 1);
ProcessAllLogFiles(guardianStateFiles);
// Force a garbage collection to occur for this demo.
//GC.Collect();
Interlocked.Exchange(ref inTimer, 0);
}
}
private static void ProcessLogFile(string stateFile, GuardianLogType logType = GuardianLogType.API_Log)
{
// Get the current state file
if (!File.Exists(stateFile))
throw new FileNotFoundException($"State file not found -> {stateFile}");
GuardianState savedState = JsonConvert.DeserializeObject<GuardianState>(File.ReadAllText(stateFile));
string logname = logType == GuardianLogType.API_Log ? savedState.API_Log_File.FileName : savedState.Unknown_Log_File.FileName;
GuardianFileState gfs = logType == GuardianLogType.API_Log ? savedState.API_Log_File : savedState.Unknown_Log_File;
// Process the log file
string thisLogFile = Path.Combine(environment["logs_folder"], savedState.Instance_Name, logname);
if (!File.Exists(thisLogFile))
throw new FileNotFoundException($"Log file not found -> {thisLogFile}");
// Process the file if the size has changed or the lines sent is zero
if (gfs.FileSize != ((new System.IO.FileInfo(thisLogFile)).Length) || gfs.LastLineSent == 0)
{
// Read the full file
logger.LogDebug($"Processiong log file -> {thisLogFile}");
var logLines = File.ReadAllLines(thisLogFile);
// The weakness here is we have to read the entire file in one go - might have to change this
for (int i = gfs.LastLineSent; i < logLines.Count(); i++)
{
logger.LogDebug($" process {i}");
var log = JObject.Parse(logLines[i]);
// Now parse out all the data and post it
GuardianEntity ge = new GuardianEntity(
logType,
log["uuid"].Value<string>(),
log["pod"]["instance_name"].Value<string>(),
null,
log["date_epoch"].Value<long>(),
log["api"].Value<string>(),
log["api_name"].Value<string>(),
log["non_blocking_mode"].Value<bool>(),
log["source_ip"].Value<string>(),
log["source_port"].Value<uint>(),
log["destination_ip"].Value<string>(),
log["destination_port"].Value<uint>(),
log["protocol"].Value<string>(),
log["hostname"].Value<string>(),
log["uri_path"].Value<string>(),
log["method"].Value<string>(),
log["status"].Value<uint>(),
log["query"].Value<string>(),
log["params"]["request_header"].ToString(),
log["params"]["response_header"].ToString(),
log["errors"].ToString(),
null);
logger.LogDebug(ge.ToString());
// Send the event now
WriteEvent(ge);
}
// Save the new state
var newFs = GetFileState(thisLogFile);
if (logType == GuardianLogType.API_Log)
savedState.API_Log_File = newFs;
else
savedState.Unknown_Log_File = newFs;
File.WriteAllText(stateFile, Newtonsoft.Json.JsonConvert.SerializeObject(savedState, Formatting.Indented));
}
}
private static void ShowUsage()
{
Console.WriteLine("\n------------------------------------------------------------------------------------------------------------");
Console.WriteLine("\n42Crunch Logs Analytics publisher - Ingests 42Crunch APIFirewall log files and pushes to Azure Log Analytics");
Console.WriteLine("\nUsage via Docker:");
Console.WriteLine(" docker run -d --rm -v ?:? guardian2LA");
Console.WriteLine("\nEnvironment variables:");
Console.WriteLine(" FW2LA_WORKSPACE_ID : Auzre Log Analytics workspace ID");
Console.WriteLine(" FW2LA_WORKSPACE_KEY : Auzre Log Analytics primary/secondary key");
Console.WriteLine(" FW2LA_LOGS_FOLDER : Guardian log file location (via a -v mount)");
Console.WriteLine(" FW2LA_STATE_FOLDER : State file location (defaults to ./.state)");
Console.WriteLine(" FW2LA_TABLENAME : Azure Log Analytics table name (defaults to guardian_log)");
Console.WriteLine(" FW2LA_TICK_INTERVAL : File processing interval in seconds (defaults to 10 seconds)");
Console.WriteLine();
}
private static void ProcessAllLogFiles(List<string> stateFiles)
{
foreach (var stateFile in stateFiles)
{
logger.LogDebug($"Processing state file -> {stateFile}");
ProcessLogFile(stateFile, GuardianLogType.API_Log);
ProcessLogFile(stateFile, GuardianLogType.Unknown_Log);
}
}
static async Task<int> Main(string[] args)
{
var logLevel = LogLevel.Warning;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("FW2LA_TRACE")))
{
logLevel = LogLevel.Debug;
trace = true;
System.Console.WriteLine("Enabled tracing mode");
}
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter(l => l >= logLevel)
.AddFilter("Microsoft", logLevel)
.AddFilter("System", logLevel)
.AddFilter("NonHostConsoleApp.Program", logLevel)
.AddConsole();
}) ;
logger = loggerFactory.CreateLogger<Program>();
logger.LogTrace("Enabled tracing mode");
logger.LogDebug("42logs2sentinel starting");
if (args.Contains("--help"))
{
ShowUsage();
return 1;
}
// Get all the environment variables - this may throw an exception
ProcessEvironment();
// Allocate a client
laclient = new LogAnalyticsClient(
workspaceId: environment["workspaceId"],
sharedKey: environment["workspaceKey"]);
// Hook the shutdown events so we can shutdown gracefully
var tcs = new TaskCompletionSource<bool>();
var sigintReceived = false;
AppDomain.CurrentDomain.ProcessExit += (_, ea) =>
{
if (!sigintReceived)
{
logger.LogWarning("Received SIGTERM");
tcs.SetResult(true);
}
else
{
logger.LogWarning("Received SIGTERM, ignoring it because already processed SIGINT");
}
};
Console.CancelKeyPress += (_, ea) =>
{
// Tell .NET to not terminate the process
ea.Cancel = true;
logger.LogWarning("Received SIGINT (Ctrl+C)");
sigintReceived = true;
tcs.SetResult(true);
};
ProcessInitialState();
// Now we can start the time now
Timer timer = new Timer(ProcessTimeEvent, null, 0, tickInterval * 1000);
// Wait here forever or until the process is shut down
await tcs.Task;
timer.Dispose();
logger.LogInformation("42logs2sentinel stopping");
return 0;
}
private static string GetMaskedToken(string inputToken, uint showCount = 8, char maskValue = '*')
{
StringBuilder opStr = new StringBuilder();
foreach (var c in inputToken.ToCharArray().Select((value, index) => new { value, index }))
{
if (c.index < (inputToken.Length - showCount) && c.value != '-')
{
opStr.Append(maskValue);
}
else
{
opStr.Append(c.value);
}
}
return opStr.ToString();
}
private static string CalculateMD5Sum(string inputFile)
{
// Use the method found here:
// https://stackoverflow.com/questions/16380477/net-md5-doesnt-equal-md5sum-exe-on-unix
byte[] hash;
var inputText = File.ReadAllText(inputFile);
using (MD5 md5 = MD5.Create())
{
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(inputText));
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
private static GuardianFileState GetFileState(string logFile, bool onlyFileSize = false)
{
var gfs = new GuardianFileState();
gfs.FileName = Path.GetFileName(logFile);
gfs.FileSize = ((new System.IO.FileInfo(logFile)).Length);
if (onlyFileSize == false)
{
gfs.MD5Sum = CalculateMD5Sum(logFile);
gfs.LastLineSent = File.ReadLines(logFile).Count();
}
return gfs;
}
private class GuardianState
{
public string API_ID { get; set; }
public string Instance_Name { get; set; }
public GuardianFileState API_Log_File { get; set; }
public GuardianFileState Unknown_Log_File { get; set; }
}
private class GuardianFileState
{
public string FileName { get; set; }
public long FileSize { get; set; }
public string MD5Sum { get; set; }
public int LastLineSent { get; set; }
}
}
}