public AccessLogFile(string directoryPath, AccessLogFilenameConfig accessLogFilenameConfig, AccessLogParserConfig accessLogParserConfig, bool ssl) { DirectoryPath = directoryPath; AccessLogFilenameConfig = accessLogFilenameConfig; AccessLogParserConfig = accessLogParserConfig; SSL = ssl; }
public void Should_ReturnAccessRequests_FromALogFile() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\logs\website.com.access.2018.04.13.log", new MockFileData( string.Join(Environment.NewLine, new List <string>() { @"117.34.118.109 - - [13/Apr/2018:03:55:08 +0100] ""GET /pmamy/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:09 +0100] ""GET /pmamy2/index.php HTTP/1.1"" 301 245", @"117.34.118.109 - - [13/Apr/2018:03:55:10 +0100] ""GET /mysql/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:10 +0100] ""GET /admin/index.php HTTP/1.1"" 301 244", })) } }); var accessLogParser = new Parser(fileSystem, AccessLogParserConfig.GetDefault()); // Act var accessRequests = accessLogParser.Parse(@"c:\logs\website.com.access.2018.04.13.log"); // Assert Assert.Collection(accessRequests, item => Assert.Contains("/pmamy/index.php", item.Resource), item => Assert.Contains("/pmamy2/index.php", item.Resource), item => Assert.Contains("/mysql/index.php", item.Resource), item => Assert.Contains("/admin/index.php", item.Resource)); }
public void Should_ReturnStatusCodeMethod_FromLineOfLog() { // Arrange var accessLogParser = new Parser(new MockFileSystem(), AccessLogParserConfig.GetDefault()); var testLine = @"140.143.233.233 - - [14/May/2018:03:04:31 +0100] ""GET /admin/mysql2/index.php HTTP/1.1"" 301 251"; var accessRequest = new AccessRequest(); // Act var result = accessLogParser.Parse(testLine, out accessRequest); // Assert Assert.True(result); Assert.Equal(301, accessRequest.StatusCode); }
public void Should_ReturnADateTime_FromLineOfLog() { // Arrange var accessLogParser = new Parser(new MockFileSystem(), AccessLogParserConfig.GetDefault()); var testLine = @"140.143.233.233 - - [14/May/2018:03:04:31 +0100] ""GET /admin/mysql2/index.php HTTP/1.1"" 301 251"; var accessRequest = new AccessRequest(); // Act var result = accessLogParser.Parse(testLine, out accessRequest); // Assert Assert.True(result); Assert.Equal(new DateTimeOffset(2018, 05, 14, 03, 04, 31, new TimeSpan(1, 0, 0)), accessRequest.DateTime); }
public void GetAllUnidentifiedResourceRequestsInLogFile_ShouldNotReturnAnything_FromLogFilesWithOnlyBlacklistedResources() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\logs\website.com.access.2018.04.13.log", new MockFileData( string.Join(Environment.NewLine, new List <string>() { @"117.34.118.109 - - [13/Apr/2018:03:55:08 +0100] ""GET /pmamy/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:09 +0100] ""GET /pmamy2/index.php HTTP/1.1"" 301 245", @"117.34.118.109 - - [13/Apr/2018:03:55:10 +0100] ""GET /mysql/index.php HTTP/1.1"" 301 244", })) } }); var accessLogParser = new Parser(fileSystem, AccessLogParserConfig.GetDefault()); using (var context = _apacheLogContextFactory.NewTestContext()) { context.AddRange( new BlacklistedResource[3] { new BlacklistedResource() { FullPath = @"/pmamy/index.php" }, new BlacklistedResource() { FullPath = @"/pmamy2/index.php" }, new BlacklistedResource() { FullPath = @"/mysql/index.php" } }); context.SaveChanges(); var whitelist = new Whitelist(context); var blacklist = new Blacklist(context); var identifier = new Analyser(accessLogParser, whitelist, blacklist, fileSystem); // Act var distinctUnidentifiedResources = identifier.GetAllUnidentifiedResourceRequestsInLogFile(@"c:\logs\website.com.access.2018.04.13.log"); // Assert Assert.Empty(distinctUnidentifiedResources); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddTransient <IFileSystem, FileSystem>(); services.AddTransient <IFinder, Finder>(); services.AddTransient <IParser, Parser>(); services.AddTransient <IAnalyser, Analyser>(); services.AddTransient <IAccessLogService, AccessLogService>(); services.AddTransient <IWhitelist, Whitelist>(); services.AddTransient <IBlacklist, Blacklist>(); services.AddTransient <IBlacklistedResourceRepository, BlacklistedResourceRepository>(); services.AddTransient <IWhitelistedResourceRepository, WhitelistedResourceRepository>(); services.AddTransient <IVirtualHostRepository, VirtualHostRepository>(); services.AddDbContext <ApacheLogContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ApacheLogContext"))); var accessLogParserConfig = new AccessLogParserConfig(); Configuration.GetSection("Sites:AccessLogParserConfig").Bind(accessLogParserConfig); services.AddSingleton(accessLogParserConfig); }
public void GetAllUnidentifiedResourceRequestsInLogFile_ShouldReturnDistinctResources_FromLogFilesWithDuplicates() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\logs\website.com.access.2018.04.13.log", new MockFileData( string.Join(Environment.NewLine, new List <string>() { @"117.34.118.109 - - [13/Apr/2018:03:55:08 +0100] ""GET /pmamy/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:09 +0100] ""GET /pmamy2/index.php HTTP/1.1"" 301 245", @"117.34.118.109 - - [13/Apr/2018:03:55:10 +0100] ""GET /mysql/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:10 +0100] ""GET /admin/index.php HTTP/1.1"" 301 244", @"117.34.118.109 - - [13/Apr/2018:03:55:11 +0100] ""GET /admin/index.php HTTP/1.1"" 301 244", })) } }); var accessLogParser = new Parser(fileSystem, AccessLogParserConfig.GetDefault()); // Run the test against one instance of the context using (var context = _apacheLogContextFactory.NewTestContext()) { var whitelist = new Whitelist(context); var blacklist = new Blacklist(context); var identifier = new Analyser(accessLogParser, whitelist, blacklist, fileSystem); // Act var distinctUnidentifiedResources = identifier.GetAllUnidentifiedResourceRequestsInLogFile(@"c:\logs\website.com.access.2018.04.13.log"); // Assert Assert.Collection(distinctUnidentifiedResources, resource => Assert.Contains("/pmamy/index.php", resource), resource => Assert.Contains("/pmamy2/index.php", resource), resource => Assert.Contains("/mysql/index.php", resource), resource => Assert.Contains("/admin/index.php", resource)); } }
public Parser(IFileSystem fileSystem, AccessLogParserConfig accessLogConfig) { _fileSystem = fileSystem; _accessLogConfig = accessLogConfig; }