Example #1
0
        public void TestSetupAuditLogEntryDataDefaultProperties()
        {
            DateTime now = DateTime.UtcNow;

            var userAccount = new UserAccount
            {
                Name = "Test User " + Guid.NewGuid()
            };

            userAccount.Save();

            AuditLogEntryData            logEntryData;
            IDictionary <string, object> parameters = new Dictionary <string, object>
            {
                { "p1", "p1Value" },
                { "p2", "p2Value" }
            };

            using (new SetUser(userAccount))
            {
                logEntryData = new AuditLogEntryData(false, "logonAuditLogEntryMetadata", parameters);
            }

            Assert.IsFalse(logEntryData.Success, "Success is invalid");
            Assert.AreEqual(userAccount.Name, logEntryData.UserName, "The UserName is invalid");
            Assert.AreEqual(RunAsDefaultTenant.DefaultTenantName, logEntryData.TenantName, "TenantName is invalid");
            Assert.GreaterOrEqual(logEntryData.CreatedDate, now, "CreatedDate is invalid");
            Assert.AreEqual(parameters.Count, logEntryData.Parameters.Count, "Parameters count is invalid");
            Assert.AreEqual(parameters["p1"], logEntryData.Parameters["p1"], "Parameter p1 is invalid");
            Assert.AreEqual(parameters["p2"], logEntryData.Parameters["p2"], "Parameter p2 is invalid");
            Assert.IsNotNullOrEmpty(logEntryData.Message, "The Message is invalid");
        }
        public void TestWriteAuditLogEntryToSyslog()
        {
            AuditLogSyslogSettings sysLogSettings = ConfigurationSettings.GetAuditLogConfigurationSection().SyslogSettings;
            bool isEnabled = sysLogSettings.IsEnabled;

            try
            {
                var mockSyslogMessageWriter = new Mock <ISyslogMessageWriter>(MockBehavior.Strict);

                // Ensure event log is enabled
                sysLogSettings.IsEnabled = true;
                var syslogWriter = new AuditLogSyslogWriter(mockSyslogMessageWriter.Object);

                IAuditLogEntryData auditLogEventData = new AuditLogEntryData(false, "logonAuditLogEntryMetadata", new Dictionary <string, object>
                {
                    { "p1", "p1Value" },
                    { "p2", "p2Value" }
                });

                mockSyslogMessageWriter.Setup(w => w.Write(It.Is <SyslogMessage>(m => ValidateMessage(m, auditLogEventData))));

                syslogWriter.Write(auditLogEventData);

                mockSyslogMessageWriter.VerifyAll();
            }
            finally
            {
                sysLogSettings.IsEnabled = isEnabled;
            }
        }
        public void TestWriteAuditLogEntryToEventLog(AuditLogSeverityEnum_Enumeration severity)
        {
            var  eventLogSettings = ConfigurationSettings.GetAuditLogConfigurationSection().EventLogSettings;
            bool isEnabled        = eventLogSettings.IsEnabled;

            try
            {
                var mockEventLog = new Mock <IEventLog>(MockBehavior.Strict);

                switch (severity)
                {
                case AuditLogSeverityEnum_Enumeration.AuditLogError:
                    mockEventLog.Setup(l => l.WriteError(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;

                case AuditLogSeverityEnum_Enumeration.AuditLogInformation:
                    mockEventLog.Setup(l => l.WriteInformation(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;

                case AuditLogSeverityEnum_Enumeration.AuditLogWarning:
                    mockEventLog.Setup(l => l.WriteWarning(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;
                }

                // Ensure event log is enabled
                eventLogSettings.IsEnabled = true;
                var eventLogWriter = new AuditLogEventLogWriter(mockEventLog.Object);

                // Override severity
                AuditLogEntryMetadata metaData = Entity.Get <AuditLogEntryMetadata>("logonAuditLogEntryMetadata", true);
                metaData.SeverityFailure_Enum = severity;
                metaData.Save();

                IAuditLogEntryData auditLogEventData = new AuditLogEntryData(false, "logonAuditLogEntryMetadata", new Dictionary <string, object>
                {
                    { "p1", "p1Value" },
                    { "p2", "p2Value" }
                });

                eventLogWriter.Write(auditLogEventData);

                mockEventLog.VerifyAll();
            }
            finally
            {
                eventLogSettings.IsEnabled = isEnabled;
            }
        }
Example #4
0
        public void TestFilterEventByMinimumSeveritySetting()
        {
            // Set minimum log level to error
            var auditLogSettings = Entity.Get <AuditLogSettings>("tenantAuditLogSettingsInstance", true);

            auditLogSettings.MinAuditLogSeverity_Enum = AuditLogSeverityEnum_Enumeration.AuditLogError;
            auditLogSettings.Save();

            var  entityModelSettings = ConfigurationSettings.GetAuditLogConfigurationSection().EntityModelSettings;
            bool isEnabled           = entityModelSettings.IsEnabled;

            try
            {
                var mockDeleter = new Mock <IAuditLogDeleter>(MockBehavior.Loose);

                // Ensure event log is enabled
                entityModelSettings.IsEnabled = true;
                var entityModelWriter = new AuditLogEntityModelWriter(mockDeleter.Object);

                Guid userName = Guid.NewGuid();

                IAuditLogEntryData auditLogEventData = new AuditLogEntryData(true, "logonAuditLogEntryMetadata", new Dictionary <string, object>
                {
                    { "loggedOnUserName", userName.ToString() }
                });

                entityModelWriter.Write(auditLogEventData);

                // Verify that logon event was not created
                IEnumerable <LogonAuditLogEntry> logonEvents = Entity.GetInstancesOfType <LogonAuditLogEntry>();
                LogonAuditLogEntry logonEvent = logonEvents.FirstOrDefault(l => l.LoggedOnUserName == userName.ToString());

                Assert.IsNull(logonEvent, "The logon event was found");
            }
            finally
            {
                entityModelSettings.IsEnabled = isEnabled;
            }
        }
Example #5
0
        public void TestWriteAuditLogEntryToEntityModel()
        {
            var  entityModelSettings = ConfigurationSettings.GetAuditLogConfigurationSection().EntityModelSettings;
            bool isEnabled           = entityModelSettings.IsEnabled;

            try
            {
                var mockDeleter = new Mock <IAuditLogDeleter>(MockBehavior.Loose);

                // Ensure event log is enabled
                entityModelSettings.IsEnabled = true;
                var entityModelWriter = new AuditLogEntityModelWriter(mockDeleter.Object);

                string userName = "******" + Guid.NewGuid();

                IAuditLogEntryData auditLogEventData = new AuditLogEntryData(false, "logonAuditLogEntryMetadata", new Dictionary <string, object>
                {
                    { "loggedOnUserName", userName }
                });

                entityModelWriter.Write(auditLogEventData);

                // Verify that logon event was created
                IEnumerable <LogonAuditLogEntry> logonEvents = Entity.GetInstancesOfType <LogonAuditLogEntry>();
                LogonAuditLogEntry logonEvent = logonEvents.FirstOrDefault(l => l.LoggedOnUserName == userName);

                Assert.IsNotNull(logonEvent, "The logon event was not found");
                Assert.AreEqual(auditLogEventData.Success, logonEvent.AuditLogEntrySuccess, "Success is invalid");
                Assert.AreEqual(auditLogEventData.UserName, logonEvent.AuditLogEntryUser, "Name is invalid");
                Assert.AreEqual(auditLogEventData.Message, logonEvent.AuditLogEntryMessage, "Message is invalid");
                Assert.AreEqual(auditLogEventData.CreatedDate, logonEvent.AuditLogEntryCreatedDate, "Created Date is invalid");
                Assert.AreEqual(auditLogEventData.SeverityEnum, logonEvent.AuditLogEntrySeverity_Enum, "Severity is invalid");
            }
            finally
            {
                entityModelSettings.IsEnabled = isEnabled;
            }
        }