|
| 1 | +# Copyright 2022-2024 simple-syslog authors |
| 2 | +# All rights reserved. |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from io import TextIOBase |
| 16 | +from typing import Generator, List |
| 17 | + |
| 18 | +from simple_syslog.builder import DefaultBuilder |
| 19 | +from simple_syslog.data import SyslogDataSet |
| 20 | +from simple_syslog.keys import ( |
| 21 | + DefaultKeyProvider, |
| 22 | + SyslogFieldKey, |
| 23 | + SyslogFieldKeyDefaults, |
| 24 | +) |
| 25 | +from simple_syslog.parser import AbstractSyslogParser, Rfc3164SyslogParser |
| 26 | +from simple_syslog.specification import SyslogSpecification |
| 27 | + |
| 28 | +expectedMessageOne = ( |
| 29 | + "CISE_RADIUS_Accounting 0018032501 1 0 2018-09-14 10:54:09.095" |
| 30 | + " +10:00 0221114759 3002 NOTICE Radius-Accounting: RADIUS Accounting watchdog update, ConfigVersionId=73, " |
| 31 | + "Device IP Address=00.00.000.0, RequestLatency=2, NetworkDeviceName=foo, " |
| 32 | + "User-Name=ACCOUNT-01\\\\\\\\D622322, NAS-IP-Address=00.00.000.0, NAS-Port=50742, " |
| 33 | + "Framed-IP-Address=00.00.000.000, Class=CACS:0A3D720400016DBFE530A22E:lzpqrst/323409315/14578982, " |
| 34 | + "Called-Station-ID=00-CA-E5-B1-21-AA, Calling-Station-ID=54-E1-AD-A1-27-72, Acct-Status-Type=Interim-Update, " |
| 35 | + "Acct-Delay-Time=10, Acct-Input-Octets=379294, Acct-Output-Octets=1053336, Acct-Session-Id=00025EB8, " |
| 36 | + "Acct-Input-Packets=1657, Acct-Output-Packets=2018, Event-Timestamp=1536886439, NAS-Port-Type=Ethernet, " |
| 37 | + "NAS-Port-Id=GigabitEthernet7/0/42, cisco-av-pair=dc-profile-name=Microsoft-Workstation, " |
| 38 | + "cisco-av-pair=dc-device-name=MSFT 5.0, cisco-av-pair=dc-device-class-tag=Workstation:Microsoft-Workstation, " |
| 39 | + "cisco-av-pair=dc-certainty-metric=10, " |
| 40 | + "cisco-av-pair=dc-opaque=\\000\\000\\000\\002\\000\\000\\000\\001\\000\\000\\000\\000, " |
| 41 | + "cisco-av-pair=dc-protocol-map=9, " |
| 42 | + "cisco-av-pair=dhcp-option=pad=" |
| 43 | + "1b:2e:01:08:ff:2e:01:08:ff:0a:90:84:51:0a:2c:08:0a:d0:52:31:0a:d0:5a:1b:2e:01:08:ff:2e:01:08:ff:79:f9:2b:" |
| 44 | + "ff:43:17:73:6d:73:62:6f:6f:74:5c:78:38:36:5c:77:64:73:6e:62:70:2e:63:6f:6d:00:ff:6f:6d:00:ff:00:00:00:00:00:" |
| 45 | + "00:00:00:00:00:00:00:00:00:00:00:00:00:00:22:23:54:00:00, cisco-av-pair=dhcp-option=00:ff:00:00, " |
| 46 | + "cisco-av-pair=dhcp-option=dhcp-parameter-request-list=" |
| 47 | + "1\\\\, 15\\\\, 3\\\\, 6\\\\, 44\\\\, 46\\\\, 47\\\\, 31\\\\, 33\\\\, 121\\\\, 249\\\\, 43\\\\, 252," |
| 48 | + " cisco-av-pair=dhcp-option=dhcp-class-identifier=MSFT 5.0, cisco-av-pair=dhcp-option=host-name=W00000PC0R1JC3," |
| 49 | + " cisco-av-pair=dhcp-option=dhcp-client-identifier=01:54:e1:ad:a1:27:72," |
| 50 | + " cisco-av-pair=dhcp-option=dhcp-message-type=8, cisco-av-pair=audit-session-id=0A3D720400016DBFE530A22E," |
| 51 | + " cisco-av-pair=method=dot1x, AcsSessionID=lzpqrst/323409315/14579377, SelectedAccessService=PEAP_MAB," |
| 52 | + " Step=11004, Step=11017, Step=15049, Step=15008, Step=22094, Step=11005, NetworkDeviceGroups=Stage#Deployment" |
| 53 | + " Type#Secure Mode D2, NetworkDeviceGroups=Location#All Locations#Placename#500 Exhibition St" |
| 54 | + " CompanyPlace#Level 18, NetworkDeviceGroups=Device Type#All Device Types#Access Switch#Catalyst 3850," |
| 55 | + " NetworkDeviceGroups=Location Type#Location Type#Office, CPMSessionID=0A3D720400016DBFE530A22E," |
| 56 | + " Stage=Stage#Deployment Type#Secure Mode D2, Location=Location#All Locations#Placename#500 Exhibition St" |
| 57 | + " CompanyPlace#Level 18, Device Type=Device Type#All Device Types#Access Switch#Catalyst 3850, Network Device" |
| 58 | + " Profile=Cisco, Location Type=Location Type#Location Type#Office" |
| 59 | +) |
| 60 | +expectedHostNameOne = "lzpqrst-admin.in.mycompany.com.lg" |
| 61 | +expectedPriOne = "181" |
| 62 | +expectedTimestampOne = "2018-09-14T00:54:09+00:00" |
| 63 | +expectedFacilityOne = "22" |
| 64 | +expectedSeverityOne = "5" |
| 65 | + |
| 66 | + |
| 67 | +def test_parse_octet_line(octet_message_3164) -> None: |
| 68 | + """Test that we can parse octet prefixed line.""" |
| 69 | + builder = DefaultBuilder( |
| 70 | + specification=SyslogSpecification.RFC_6587_3164, |
| 71 | + key_provider=DefaultKeyProvider(), |
| 72 | + nil_policy=None, |
| 73 | + allowed_deviations=None, |
| 74 | + ) |
| 75 | + parser = Rfc3164SyslogParser(builder, SyslogSpecification.RFC_6587_3164) |
| 76 | + syslog_data: SyslogDataSet = parser.parse(octet_message_3164) |
| 77 | + assert syslog_data |
| 78 | + |
| 79 | + |
| 80 | +def test_parse_line(file_of_3164_single_ise_txt) -> None: |
| 81 | + """Test parsing regular line.""" |
| 82 | + builder = DefaultBuilder( |
| 83 | + specification=SyslogSpecification.RFC_3164, |
| 84 | + key_provider=DefaultKeyProvider(), |
| 85 | + nil_policy=None, |
| 86 | + allowed_deviations=None, |
| 87 | + ) |
| 88 | + parser = Rfc3164SyslogParser(builder) |
| 89 | + data_sets: List[SyslogDataSet] = [] |
| 90 | + with file_of_3164_single_ise_txt.open("r") as f: |
| 91 | + g = generate_from_file(f, parser) |
| 92 | + for ds in g: |
| 93 | + data_sets.append(ds) |
| 94 | + assert len(data_sets) == 1 |
| 95 | + assert ( |
| 96 | + expectedMessageOne |
| 97 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.MESSAGE]] |
| 98 | + ) |
| 99 | + assert ( |
| 100 | + expectedHostNameOne |
| 101 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.HEADER_HOSTNAME]] |
| 102 | + ) |
| 103 | + assert ( |
| 104 | + expectedPriOne |
| 105 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.HEADER_PRI]] |
| 106 | + ) |
| 107 | + assert ( |
| 108 | + expectedSeverityOne |
| 109 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.HEADER_PRI_SEVERITY]] |
| 110 | + ) |
| 111 | + assert ( |
| 112 | + expectedFacilityOne |
| 113 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.HEADER_PRI_FACILITY]] |
| 114 | + ) |
| 115 | + assert ( |
| 116 | + expectedTimestampOne |
| 117 | + == data_sets[0].data[SyslogFieldKeyDefaults[SyslogFieldKey.HEADER_TIMESTAMP]] |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def generate_from_file( |
| 122 | + f: TextIOBase, parser: AbstractSyslogParser[SyslogDataSet] |
| 123 | +) -> Generator[SyslogDataSet, None, None]: |
| 124 | + """Open a Path and return the generator.""" |
| 125 | + return parser.generate(f) |
0 commit comments