# Path: ndr/syslog.py
# class SyslogEntry():
#     def __init__(self):
#         self.timestamp = None
#         self.program = None
#         self.priority = None
#         self.pid = None
#         self.message = None
#         self.facility = None
#         self.host = None
# 
#     @classmethod
#     def from_dict(cls, syslog_dict):
#         '''Attempts to convert a YAML dict tree into a SyslogEntry.
# 
#         Because messages can be malformed or otherwise bad, this returns a KeyError
#         if the values in the dict are missing. A special case is made for Message
#         being empty. This represents a newline in the syslog, and we simply return
#         None and discard it'''
# 
#         message = SyslogEntry()
#         message.program = syslog_dict['program']
#         message.priority = SyslogPriorities(syslog_dict['priority'])
# 
#         # PID will not always be present, set to none if its MIA
#         if "pid" in syslog_dict:
#             message.pid = int(syslog_dict["pid"])
#         else:
#             message.pid = None
# 
#         # Message can be blank, representing a newline. Toss it.
#         if "message" not in syslog_dict:
#             return None
# 
#         message.message = syslog_dict['message']
#         message.timestamp = syslog_dict['timestamp']
# 
#         message.facility = SyslogFacilities(syslog_dict['facility'])
#         message.host = syslog_dict['host']
#         return message
# 
#     def to_dict(self):
#         '''Returns a YAML structure of the entry as per the standardized YAML specification
#            used by NDR'''
# 
#         syslog_dict = {}
#         syslog_dict['program'] = self.program
#         syslog_dict['priority'] = self.priority.value
#         if self.pid:
#             syslog_dict['pid'] = self.pid
#         syslog_dict['message'] = self.message
# 
#         syslog_dict['timestamp'] = self.timestamp
#         syslog_dict['facility'] = self.facility.value
#         syslog_dict['host'] = self.host
# 
#         return syslog_dict
# 
# class SyslogUploadMessage(IngestMessage, Syslog):
#     '''Uploads logs from syslog formatted in a JSON manner.
# 
#     This class depends on syslog-ng's output to be formatted in JSON-formatted with the following
#     fields present
#      - program
#      - priority
#      - pid
#      - message
#      - facility
#      - date
# 
#     syslog-ng formats one JSON object per line, these must be deserialized on a line-by-line
#     basis'''
# 
#     def __init__(self, config=None):
#         IngestMessage.__init__(
#             self, config, IngestMessageTypes.SYSLOG_UPLOAD)
#         Syslog.__init__(self)
# 
#     def from_message(self, ingest_msg: IngestMessage):
#         '''Converts an ingest message to a syslog message'''
#         super().from_message(ingest_msg)
# 
#         # Now we need to deserialize the payload
#         for log in self.headers['payload']:
#             for log_entry in log['log']:
#                 self.syslog_entries.append(SyslogEntry.from_dict(log_entry))
#         return self
# 
#     def create_report(self):
#         syslog_dicts = []
#         for log_entry in self.syslog_entries:
#             syslog_dicts.append(log_entry.to_dict())
# 
#         self.add_header('payload', [{'log': syslog_dicts}])
#         super().create_report()


import unittest
import os
import ndr
import yaml
from ndr import SyslogUploadMessage, SyslogEntry
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NDR.  If not, see <http://www.gnu.org/licenses/>.




# Testing data from a live system running syslog-ng in JSON reporting mode
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_SYSLOG_DATA = THIS_DIR + '/data/test_log.json'
NDR_CONFIG = ndr.Config(THIS_DIR + '/data/test_config.yml')

class SyslogTest(unittest.TestCase):
    def test_syslog_loading(self):
        '''Tests in-depth behavior of syslog loading as we do it with syslog_upload'''

        # For NDR, syslogs are in JSON-like (its complicated, see sysload_uploader,
        # for the gorey details) that we want to read and load an entry for. This
        # function runs through a sample log and makes sure we get realistic RL
        # results

        log_upload = SyslogUploadMessage(NDR_CONFIG)

        entries = 0
        with open(TEST_SYSLOG_DATA, 'r') as f:
            for line in f:
                yaml_line = yaml.safe_load(line)

# Path: ndr/syslog.py
# class SyslogEntry():
#     def __init__(self):
#         self.timestamp = None
#         self.program = None
#         self.priority = None
#         self.pid = None
#         self.message = None
#         self.facility = None
#         self.host = None
# 
#     @classmethod
#     def from_dict(cls, syslog_dict):
#         '''Attempts to convert a YAML dict tree into a SyslogEntry.
# 
#         Because messages can be malformed or otherwise bad, this returns a KeyError
#         if the values in the dict are missing. A special case is made for Message
#         being empty. This represents a newline in the syslog, and we simply return
#         None and discard it'''
# 
#         message = SyslogEntry()
#         message.program = syslog_dict['program']
#         message.priority = SyslogPriorities(syslog_dict['priority'])
# 
#         # PID will not always be present, set to none if its MIA
#         if "pid" in syslog_dict:
#             message.pid = int(syslog_dict["pid"])
#         else:
#             message.pid = None
# 
#         # Message can be blank, representing a newline. Toss it.
#         if "message" not in syslog_dict:
#             return None
# 
#         message.message = syslog_dict['message']
#         message.timestamp = syslog_dict['timestamp']
# 
#         message.facility = SyslogFacilities(syslog_dict['facility'])
#         message.host = syslog_dict['host']
#         return message
# 
#     def to_dict(self):
#         '''Returns a YAML structure of the entry as per the standardized YAML specification
#            used by NDR'''
# 
#         syslog_dict = {}
#         syslog_dict['program'] = self.program
#         syslog_dict['priority'] = self.priority.value
#         if self.pid:
#             syslog_dict['pid'] = self.pid
#         syslog_dict['message'] = self.message
# 
#         syslog_dict['timestamp'] = self.timestamp
#         syslog_dict['facility'] = self.facility.value
#         syslog_dict['host'] = self.host
# 
#         return syslog_dict
# 
# class SyslogUploadMessage(IngestMessage, Syslog):
#     '''Uploads logs from syslog formatted in a JSON manner.
# 
#     This class depends on syslog-ng's output to be formatted in JSON-formatted with the following
#     fields present
#      - program
#      - priority
#      - pid
#      - message
#      - facility
#      - date
# 
#     syslog-ng formats one JSON object per line, these must be deserialized on a line-by-line
#     basis'''
# 
#     def __init__(self, config=None):
#         IngestMessage.__init__(
#             self, config, IngestMessageTypes.SYSLOG_UPLOAD)
#         Syslog.__init__(self)
# 
#     def from_message(self, ingest_msg: IngestMessage):
#         '''Converts an ingest message to a syslog message'''
#         super().from_message(ingest_msg)
# 
#         # Now we need to deserialize the payload
#         for log in self.headers['payload']:
#             for log_entry in log['log']:
#                 self.syslog_entries.append(SyslogEntry.from_dict(log_entry))
#         return self
# 
#     def create_report(self):
#         syslog_dicts = []
#         for log_entry in self.syslog_entries:
#             syslog_dicts.append(log_entry.to_dict())
# 
#         self.add_header('payload', [{'log': syslog_dicts}])
#         super().create_report()


# Path: tests/test_syslog.py
import unittest
import os
import ndr
import yaml
from ndr import SyslogUploadMessage, SyslogEntry

# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NDR.  If not, see <http://www.gnu.org/licenses/>.




# Testing data from a live system running syslog-ng in JSON reporting mode
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_SYSLOG_DATA = THIS_DIR + '/data/test_log.json'
NDR_CONFIG = ndr.Config(THIS_DIR + '/data/test_config.yml')

class SyslogTest(unittest.TestCase):
    def test_syslog_loading(self):
        '''Tests in-depth behavior of syslog loading as we do it with syslog_upload'''

        # For NDR, syslogs are in JSON-like (its complicated, see sysload_uploader,
        # for the gorey details) that we want to read and load an entry for. This
        # function runs through a sample log and makes sure we get realistic RL
        # results

        log_upload = SyslogUploadMessage(NDR_CONFIG)

        entries = 0
        with open(TEST_SYSLOG_DATA, 'r') as f:
            for line in f:
                yaml_line = yaml.safe_load(line)

                entry = SyslogEntry.from_dict(yaml_line)