| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2025 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 base64 |
| 10 |
import logging |
| 11 |
import pytest |
| 12 |
import os |
| 13 |
import ldap |
| 14 |
import re |
| 15 |
import time |
| 16 |
from lib389._constants import DEFAULT_SUFFIX |
| 17 |
from test389.topologies import topology_st as topo |
| 18 |
from lib389.idm.group import Groups, Group |
| 19 |
from lib389.idm.user import UserAccount, UserAccounts |
| 20 |
from lib389.dirsrv_log import DirsrvAuditJSONLog |
| 21 |
|
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
DN = "uid=audit_json,ou=people," + DEFAULT_SUFFIX |
| 25 |
MAIN_KEYS = [ |
| 26 |
"gm_time", |
| 27 |
"local_time", |
| 28 |
"target_dn", |
| 29 |
"bind_dn", |
| 30 |
"client_ip", |
| 31 |
"server_ip", |
| 32 |
"conn_id", |
| 33 |
"op_id", |
| 34 |
"result", |
| 35 |
] |
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def setup_test(topo, request): |
| 39 |
"""Configure log settings""" |
| 40 |
inst = topo.standalone |
| 41 |
inst.config.replace('nsslapd-auditlog-logbuffering', 'off') |
| 42 |
inst.config.replace('nsslapd-auditlog-logging-enabled', 'on') |
| 43 |
inst.config.replace('nsslapd-auditfaillog-logging-enabled', 'on') |
| 44 |
inst.config.replace('nsslapd-auditlog-log-format', 'json') |
| 45 |
inst.config.replace('nsslapd-auditlog-display-attrs', 'cn') |
| 46 |
inst.deleteAuditLogs() |
| 47 |
|
| 48 |
|
| 49 |
def get_log_event(inst, dn, op): |
| 50 |
"""Get a specific audit log event by target_dn and op type |
| 51 |
""" |
| 52 |
time.sleep(1) # give a little time to flush to disk |
| 53 |
|
| 54 |
audit_log = DirsrvAuditJSONLog(inst) |
| 55 |
log_lines = audit_log.readlines() |
| 56 |
for line in log_lines: |
| 57 |
if re.match(r'[ \t\n]', line): |
| 58 |
# Skip log title lines and newlines |
| 59 |
continue |
| 60 |
|
| 61 |
event = audit_log.parse_line(line) |
| 62 |
if 'header' in event: |
| 63 |
# Skip header line |
| 64 |
continue |
| 65 |
if event['target_dn'].lower() == dn.lower() and op in event: |
| 66 |
return event |
| 67 |
|
| 68 |
# Not found, must assert |
| 69 |
assert False |
| 70 |
|
| 71 |
|
| 72 |
def test_audit_json_logging(topo, setup_test): |
| 73 |
"""Test audit json logging is working |
| 74 |
|
| 75 |
:id: 837a89a3-9c5a-4f8f-90fd-27c6b32f13f6 |
| 76 |
:setup: Standalone Instance |
| 77 |
:steps: |
| 78 |
1. Add entry |
| 79 |
2. Check it was logged in json and default keys are present |
| 80 |
3. Modify entry |
| 81 |
4. Check audit log |
| 82 |
5. Modrdn of entry |
| 83 |
6. Check log |
| 84 |
7. Delete entry |
| 85 |
8. Check log |
| 86 |
9. Modify time format |
| 87 |
10. Delete of non-existent entry fails |
| 88 |
11. Check log for expected "result == 32" |
| 89 |
12. Verify new time format is applied |
| 90 |
:expectedresults: |
| 91 |
1. Success |
| 92 |
2. Success |
| 93 |
3. Success |
| 94 |
4. Success |
| 95 |
5. Success |
| 96 |
6. Success |
| 97 |
7. Success |
| 98 |
8. Success |
| 99 |
9. Success |
| 100 |
10. Success |
| 101 |
11. Success |
| 102 |
12. Success |
| 103 |
""" |
| 104 |
inst = topo.standalone |
| 105 |
|
| 106 |
# Add entry |
| 107 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 108 |
user_dn = "uid=test_audit_log,ou=people," + DEFAULT_SUFFIX |
| 109 |
user = users.ensure_state(properties={ |
| 110 |
'uid': 'test_audit_log', |
| 111 |
'cn': 'test', |
| 112 |
'sn': 'user', |
| 113 |
'uidNumber': '1000', |
| 114 |
'gidNumber': '1000', |
| 115 |
'homeDirectory': '/home/test', |
| 116 |
'userPassword': 'pppppppp' |
| 117 |
}) |
| 118 |
|
| 119 |
event = get_log_event(inst, user_dn, "add") |
| 120 |
# Check all the expected keys are present |
| 121 |
for key in MAIN_KEYS: |
| 122 |
assert key in event and event[key] != "" |
| 123 |
|
| 124 |
# Check specific values |
| 125 |
assert event['result'] == 0 |
| 126 |
assert event['add'] |
| 127 |
assert 'id_list' in event |
| 128 |
assert event['id_list'][0]['cn'] == "test" |
| 129 |
|
| 130 |
# Modify entry |
| 131 |
user.replace('sn', 'new sn') |
| 132 |
event = get_log_event(inst, user_dn, "modify") |
| 133 |
assert event['result'] == 0 |
| 134 |
assert event['modify'][0]['op'] == "replace" |
| 135 |
assert event['modify'][0]['attr'] == "sn" |
| 136 |
assert event['modify'][0]['values'][0] == "new sn" |
| 137 |
|
| 138 |
# ModRDN entry |
| 139 |
NEW_SUP = "ou=groups," + DEFAULT_SUFFIX |
| 140 |
user.rename('uid=test_modrdn', newsuperior=NEW_SUP, deloldrdn=True) |
| 141 |
event = get_log_event(inst, user_dn, "modrdn") |
| 142 |
assert event['result'] == 0 |
| 143 |
assert event['modrdn']['newrdn'] == "uid=test_modrdn" |
| 144 |
assert event['modrdn']['deleteoldrdn'] is True |
| 145 |
assert event['modrdn']['newsuperior'] == NEW_SUP |
| 146 |
|
| 147 |
# Delete entry |
| 148 |
DN = "uid=test_modrdn," + NEW_SUP |
| 149 |
user.delete() |
| 150 |
event = get_log_event(inst, DN, "delete") |
| 151 |
assert event['result'] == 0 |
| 152 |
assert event['delete']['dn'] == DN |
| 153 |
|
| 154 |
# CHange time format and check it after the next test |
| 155 |
inst.config.replace('nsslapd-auditlog-time-format', '%F %T') |
| 156 |
|
| 157 |
# Perform invalid update (result != 0) |
| 158 |
DN = "uid=does_not_exist," + DEFAULT_SUFFIX |
| 159 |
user = UserAccount(inst, DN) |
| 160 |
with pytest.raises(ldap.NO_SUCH_OBJECT): |
| 161 |
user.delete() |
| 162 |
event = get_log_event(inst, DN, "delete") |
| 163 |
assert event['result'] == 32 |
| 164 |
|
| 165 |
# Check time format |
| 166 |
assert re.match( |
| 167 |
r'[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}', |
| 168 |
event['local_time']) |
| 169 |
|
| 170 |
|
| 171 |
def test_audit_json_logging_with_binary_value(topo, setup_test): |
| 172 |
"""Test audit json logging is working with binary values |
| 173 |
|
| 174 |
:id: 79870a11-907d-490c-8d9d-3ca242902966 |
| 175 |
:setup: Standalone Instance |
| 176 |
:steps: |
| 177 |
1. Get audit log event |
| 178 |
2. Check nsState attribute is present |
| 179 |
3. Check modifiersname attribute is present |
| 180 |
4. Check nsState value is base64 encoded |
| 181 |
5. Check modifiersname value is not base64 encoded |
| 182 |
6. Add large binary value is correctly encoded |
| 183 |
:expectedresults: |
| 184 |
1. Success |
| 185 |
2. Success |
| 186 |
3. Success |
| 187 |
4. Success |
| 188 |
5. Success |
| 189 |
6. Success |
| 190 |
""" |
| 191 |
inst = topo.standalone |
| 192 |
|
| 193 |
event = get_log_event(inst, "cn=uniqueid generator,cn=config", "modify") |
| 194 |
assert event['result'] == 0 |
| 195 |
assert event['modify'][0]['op'] == "replace" |
| 196 |
assert event['modify'][0]['attr'] == "nsState" |
| 197 |
assert event['modify'][1]['attr'] == "modifiersname" |
| 198 |
|
| 199 |
# Decoding nsState should not raise an error |
| 200 |
nsState_value = event['modify'][0]['values_encoded'][0] |
| 201 |
assert base64.b64decode(nsState_value, validate=True) |
| 202 |
|
| 203 |
# Decoding modifiersname should raise an error |
| 204 |
modifersname_value = event['modify'][1]['values'][0] |
| 205 |
with pytest.raises(base64.binascii.Error): |
| 206 |
base64.b64decode(modifersname_value, validate=True) |
| 207 |
|
| 208 |
# Add large binary value and mixed value |
| 209 |
groups = Groups(inst, DEFAULT_SUFFIX, rdn=None) |
| 210 |
group_properties = { |
| 211 |
'cn': 'binary_group', |
| 212 |
'description': 'testgroup for binary value', |
| 213 |
'objectclass': ['nsValueItem', 'top', 'groupOfNames']} |
| 214 |
test_group = groups.create(properties=group_properties) |
| 215 |
|
| 216 |
# Add large binary value |
| 217 |
bin_value_large = bytes([(i % 256) for i in range(2 * 512 * 512)]) |
| 218 |
test_group.add('nsValueBin', bin_value_large) |
| 219 |
event = get_log_event(inst, test_group.dn, "modify") |
| 220 |
assert event['result'] == 0 |
| 221 |
assert event['modify'][0]['op'] == "add" |
| 222 |
assert event['modify'][0]['attr'] == "nsValueBin" |
| 223 |
bin_value = event['modify'][0]['values_encoded'][0] |
| 224 |
assert base64.b64decode(bin_value, validate=True) |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == '__main__': |
| 228 |
# Run isolated |
| 229 |
# -s for DEBUG mode |
| 230 |
CURRENT_FILE = os.path.realpath(__file__) |
| 231 |
pytest.main(["-s", CURRENT_FILE]) |