File: test_mariadb_audit_parser.py

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (148 lines) | stat: -rw-r--r-- 6,498 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
#############################################################################
# Copyright (c) 2022 One Identity
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################
import json

import pytest


test_parameters = [
    (
        "<190>Apr 13 14:43:13 mysql-server_auditing: columnstore-1 eff8a68bcd7f,user1,172.18.0.1,32,394,QUERY,syslog_ng,'SELECT * FROM test WHERE 0=1',1146",
        {
            "PRI": "190",
            "DATE": "Apr 13 14:43:13",
            "_mariadb": {
                "username": "user1",
                "syslog_info": "columnstore-1",
                "serverhost": "eff8a68bcd7f",
                "retcode": "1146",
                "queryid": "394",
                "operation": "QUERY",
                "object": "SELECT * FROM test WHERE 0=1",
                "host": "172.18.0.1",
                "database": "syslog_ng",
                "connectionid": "32",
            },
            "MSG": r"columnstore-1 eff8a68bcd7f,user1,172.18.0.1,32,394,QUERY,syslog_ng,'SELECT * FROM test WHERE 0=1',1146",
        },
    ),
    (
        "<190>Sep 14 17:46:51 centos mysql-server_auditing: columnstore-1 centos,root,localhost,11,117,QUERY,loans,'SELECT grade, AVG(loan_amnt) avg,FROM loanstats GROUP BY grade ORDER BY grade',0",
        {
            "PRI": "190",
            "DATE": "Sep 14 17:46:51",
            "_mariadb": {
                "username": "root",
                "syslog_info": "columnstore-1",
                "serverhost": "centos",
                "retcode": "0",
                "queryid": "117",
                "operation": "QUERY",
                "object": "SELECT grade, AVG(loan_amnt) avg,FROM loanstats GROUP BY grade ORDER BY grade",
                "host": "localhost",
                "database": "loans",
                "connectionid": "11",
            },
            "MSG": r"columnstore-1 centos,root,localhost,11,117,QUERY,loans,'SELECT grade, AVG(loan_amnt) avg,FROM loanstats GROUP BY grade ORDER BY grade',0",
        },
    ),
    (
        "<190>Apr 29 13:56:36 mysql-server_auditing: docker eff8a68bcd7f,root,172.18.0.3,13,0,CONNECT,,,0",
        {
            "PRI": "190",
            "DATE": "Apr 29 13:56:36",
            "_mariadb": {
                "username": "root",
                "syslog_info": "docker",
                "serverhost": "eff8a68bcd7f",
                "retcode": "0",
                "queryid": "0",
                "operation": "CONNECT",
                "object": "",
                "host": "172.18.0.3",
                "database": "",
                "connectionid": "13",
            },
            "MSG": r"docker eff8a68bcd7f,root,172.18.0.3,13,0,CONNECT,,,0",
        },
    ),
    (
        "<190>Apr 29 13:56:16 mysql-server_auditing: docker eff8a68bcd7f,user1,172.18.0.3,11,58,QUERY,syslog_ng,'insert into test (date, host, program, message) VALUES(Apr 29 15:55:24, locohost, test-program, foo fighters bar baz)',0",
        {
            "PRI": "190",
            "DATE": "Apr 29 13:56:16",
            "_mariadb": {
                "username": "user1",
                "syslog_info": "docker",
                "serverhost": "eff8a68bcd7f",
                "retcode": "0",
                "queryid": "58",
                "operation": "QUERY",
                "object": "insert into test (date, host, program, message) VALUES(Apr 29 15:55:24, locohost, test-program, foo fighters bar baz)",
                "host": "172.18.0.3",
                "database": "syslog_ng",
                "connectionid": "11",
            },
            "MSG": r"docker eff8a68bcd7f,user1,172.18.0.3,11,58,QUERY,syslog_ng,'insert into test (date, host, program, message) VALUES(Apr 29 15:55:24, locohost, test-program, foo fighters bar baz)',0",
        },
    ),
]


@pytest.mark.parametrize(
    "input_message, expected_kv_pairs",
    test_parameters,
    ids=range(len(test_parameters)),
)
def test_mariadb_audit_parser(config, port_allocator, syslog_ng, input_message, expected_kv_pairs):
    config.add_include("scl.conf")

    network_source = config.create_network_source(ip="localhost", port=port_allocator())
    mariadb_audit_parser = config.create_mariadb_audit_parser()
    file_destination = config.create_file_destination(
        file_name="output.log",
        template=config.stringify("$(format-json --scope everything)" + "\n"),
    )
    config.create_logpath(
        statements=[network_source, mariadb_audit_parser, file_destination],
    )

    syslog_ng.start(config)
    network_source.write_log(input_message)

    output_json = json.loads(file_destination.read_log())

    assert output_json["PRI"] == expected_kv_pairs["PRI"]
    assert output_json["DATE"] == expected_kv_pairs["DATE"]

    assert output_json["_mariadb"]["syslog_info"] == expected_kv_pairs["_mariadb"]["syslog_info"]
    assert output_json["_mariadb"]["serverhost"] == expected_kv_pairs["_mariadb"]["serverhost"]
    assert output_json["_mariadb"]["retcode"] == expected_kv_pairs["_mariadb"]["retcode"]
    assert output_json["_mariadb"]["queryid"] == expected_kv_pairs["_mariadb"]["queryid"]
    assert output_json["_mariadb"]["operation"] == expected_kv_pairs["_mariadb"]["operation"]
    assert output_json["_mariadb"]["object"] == expected_kv_pairs["_mariadb"]["object"]
    assert output_json["_mariadb"]["host"] == expected_kv_pairs["_mariadb"]["host"]
    assert output_json["_mariadb"]["database"] == expected_kv_pairs["_mariadb"]["database"]
    assert output_json["_mariadb"]["connectionid"] == expected_kv_pairs["_mariadb"]["connectionid"]

    assert output_json["MSG"] == expected_kv_pairs["MSG"]