Debian Codegraph @

dirsrvtests/tests/suites/ds_logs/audit_log_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2022 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8
9 import ldap
10 import logging
11 import pytest
12 import os
13 import time
14 from lib389._constants import DEFAULT_SUFFIX
15 from test389.topologies import topology_st as topo
16 from lib389.idm.user import UserAccounts
17 from lib389.idm.group import Groups
18
19 log = logging.getLogger(__name__)
20
21
22 def test_auditlog_display_attrs(topo):
23 """Test "display attributes" feature of the audit log
24
25 :id: 01beaf71-4cb5-4943-9774-3210ae5d68a2
26 :setup: Standalone Instance
27 :steps:
28 1. Test "cn" attribute is displayed
29 2. Test multiple attributes are displayed
30 3. Test modrdn updates log
31 4. Test all attributes are displayed
32 5. Test delete updates log
33 :expectedresults:
34 1. Success
35 2. Success
36 3. Success
37 4. Success
38 5. Sucecss
39 """
40
41 inst = topo.standalone
42 inst.config.replace('nsslapd-auditlog-logging-enabled', 'on')
43
44 # Test "cn" attribute
45 inst.config.replace('nsslapd-auditlog-display-attrs', 'cn')
46 users = UserAccounts(inst, DEFAULT_SUFFIX)
47 user = users.ensure_state(properties={
48 'uid': 'test_audit_log',
49 'cn': 'test',
50 'sn': 'user',
51 'uidNumber': '1000',
52 'gidNumber': '1000',
53 'homeDirectory': '/home/test',
54 'userPassword': 'pppppppp'
55 })
56 user2 = users.ensure_state(properties={
57 'uid': 'test_modrdn_delete',
58 'cn': 'modrdn_delete',
59 'sn': 'modrdn_delete',
60 'uidNumber': '1001',
61 'gidNumber': '1001',
62 'homeDirectory': '/home/modrdn_delete',
63 'userPassword': 'pppppppp'
64 })
65 time.sleep(1)
66 assert inst.ds_audit_log.match("#cn: test")
67 assert not inst.ds_audit_log.match("#uid: test_audit_log")
68
69 # Test multiple attributes
70 inst.config.replace('nsslapd-auditlog-display-attrs', 'uidNumber gidNumber, homeDirectory')
71 user.replace('sn', 'new value')
72 time.sleep(1)
73 assert inst.ds_audit_log.match("#uidNumber: 1000")
74 assert inst.ds_audit_log.match("#gidNumber: 1000")
75 assert inst.ds_audit_log.match("#homeDirectory: /home/test")
76 assert not inst.ds_audit_log.match("#uid: test_audit_log")
77 assert not inst.ds_audit_log.match("#uidNumber: 1001")
78 assert not inst.ds_audit_log.match("#sn: modrdn_delete")
79
80 # Test modrdn
81 user2.rename("uid=modrdn_delete", DEFAULT_SUFFIX)
82 time.sleep(1)
83 assert inst.ds_audit_log.match("#uidNumber: 1001")
84 assert inst.ds_audit_log.match("#gidNumber: 1001")
85
86 # Test ALL attributes
87 inst.config.replace('nsslapd-auditlog-display-attrs', '*')
88 user.replace('sn', 'new value again')
89 time.sleep(1)
90 assert inst.ds_audit_log.match("#uid: test_audit_log")
91 assert inst.ds_audit_log.match("#cn: test")
92 assert inst.ds_audit_log.match("#uidNumber: 1000")
93 assert inst.ds_audit_log.match("#objectClass: top")
94
95 # Test delete
96 user2.delete()
97 time.sleep(1)
98 assert inst.ds_audit_log.match("#sn: modrdn_delete")
99
100 def test_auditlog_bof(topo):
101 """Test that value containing 256 chars doesn't crash the server
102
103 :id: 767c0604-146d-4d07-8bf4-1093f51ce97b
104 :setup: Standalone Instance
105 :steps:
106 1. Change 'cn' attribute to contain exactly 256 chars
107 2. Test that server didn't crash
108 :expectedresults:
109 1. Success
110 2. Success
111 """
112
113 inst = topo.standalone
114 inst.config.replace('nsslapd-auditlog-logging-enabled', 'on')
115
116 inst.config.replace('nsslapd-auditlog-display-attrs', 'cn')
117 users = UserAccounts(inst, DEFAULT_SUFFIX)
118 users.ensure_state(properties={
119 'uid': 'test_auditlog_bof',
120 'cn': 'A'*256,
121 'sn': 'user',
122 'uidNumber': '1001',
123 'gidNumber': '1001',
124 'homeDirectory': '/home/auditlog_bof',
125 })
126 time.sleep(1)
127 assert inst.status() == True
128
129 def test_auditlog_buffering(topo, request):
130 """Test log buffering works as expected when on or off
131
132 :id: 08f1ccf0-c1fb-4427-9300-24585e336ae7
133 :setup: Standalone Instance
134 :steps:
135 1. Set buffering on
136 2. Make update and immediately check log (update should not be present)
137 3. Make invalid update, failed update should not be in log
138 4. Disable buffering
139 5. Make update and immediately check log (update should be present)
140 6. Make invalid update, both failed updates should be in log
141 :expectedresults:
142 1. Success
143 2. Success
144 3. Success
145 4. Success
146 5. Success
147 6. Success
148 """
149
150 # Configure instance
151 inst = topo.standalone
152 inst.config.replace('nsslapd-auditlog-logging-enabled', 'on')
153 inst.config.replace('nsslapd-auditfaillog-logging-enabled', 'on')
154 inst.config.replace('nsslapd-auditlog-logbuffering', 'on')
155 inst.deleteAuditLogs() # Start with fresh set of logs
156 original_value = inst.config.get_attr_val_utf8('nsslapd-timelimit')
157
158 # Make a good and bad update and check neither are logged
159 inst.config.replace('nsslapd-timelimit', '999')
160 with pytest.raises(ldap.UNWILLING_TO_PERFORM):
161 inst.config.replace('no_such_attr', 'blah')
162 time.sleep(1)
163 assert not inst.ds_audit_log.match("nsslapd-timelimit: 999")
164 assert not inst.ds_audit_log.match("result: 53")
165
166 # Make a good and bad update and check both are logged
167 inst.config.replace('nsslapd-auditlog-logbuffering', 'off')
168 inst.config.replace('nsslapd-timelimit', '888')
169 with pytest.raises(ldap.UNWILLING_TO_PERFORM):
170 inst.config.replace('no_such_attr', 'nope')
171 time.sleep(1)
172 assert inst.ds_audit_log.match("nsslapd-timelimit: 888")
173 # Both failed updates should be present (easiest way to check log)
174 assert len(inst.ds_audit_log.match("result: 53")) == 2
175
176 # Reset timelimit just to be safe
177 def fin():
178 inst.config.replace('nsslapd-timelimit', original_value)
179 request.addfinalizer(fin)
180
181 def test_auditlog_buffering_large_update(topo, request):
182 """Test log buffering works as expected with very large updates
183
184 :id: 8c157ccd-5cc2-4d93-bc96-16f1e098efba
185 :setup: Standalone Instance
186 :steps:
187 1. Set buffering on
188 2. Make large update
189 3. Check if the entire update was written to the log
190 :expectedresults:
191 1. Success
192 2. Success
193 3. Success
194 """
195
196 # Configure instance
197 inst = topo.standalone
198 inst.config.replace('nsslapd-auditlog-logging-enabled', 'on')
199 inst.config.replace('nsslapd-auditlog-logbuffering', 'on')
200 inst.deleteAuditLogs() # Start with fresh set of logs
201
202 groups = Groups(inst, DEFAULT_SUFFIX, rdn=None)
203 group = groups.create(properties={'cn': 'large_group'})
204
205 large_value = "L" * 5000
206 large_value += "The End"
207 group.add('description', large_value)
208
209 time.sleep(1)
210 assert inst.ds_audit_log.match(".*The End")
211
212
213 if __name__ == '__main__':
214 # Run isolated
215 # -s for DEBUG mode
216 CURRENT_FILE = os.path.realpath(__file__)
217 pytest.main(["-s", CURRENT_FILE])