import os import struct import sys import pytest from conftest import get_config, test_ace from bonsai import LDAPClient from bonsai.active_directory.acl import ACE, ACL, ACLRevision from bonsai.active_directory.sid import SID from bonsai.active_directory import SecurityDescriptor @pytest.fixture def client(): """Create a client with authentication settings.""" cfg = get_config() url = f"ldap://{cfg['SERVER']['hostname']}:{cfg['SERVER']['port']}" client = LDAPClient(url) client.set_credentials( "SIMPLE", user=cfg["SIMPLEAUTH"]["user"], password=cfg["SIMPLEAUTH"]["password"] ) return client @pytest.fixture def test_sd(): return SecurityDescriptor( { "self_relative": False, "rm_control_valid": False, "sacl_protected": False, "dacl_protected": False, "sacl_auto_inherited": False, "dacl_auto_inherited": False, "sacl_computed_inheritance_required": False, "dacl_computed_inheritance_required": False, "server_security": False, "dacl_trusted": True, "sacl_defaulted": False, "sacl_present": False, "dacl_defaulted": False, "dacl_present": False, "group_defaulted": True, "owner_defaulted": True, }, SID("S-1-1-0"), SID("S-1-1-0"), None, None, ) def test_from_binary(): """Test from_binary method.""" with pytest.raises(TypeError): _ = SecurityDescriptor.from_binary(0) with pytest.raises(TypeError): _ = SecurityDescriptor.from_binary("INVALID") with pytest.raises(ValueError): _ = SecurityDescriptor.from_binary(b"\x05\nH\x00\x07\x00") curdir = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(curdir, "testenv", "sd-sample0.bin"), "rb") as data: input_data = data.read() sec_desc = SecurityDescriptor.from_binary(input_data) assert sec_desc.revision == 1 assert sec_desc.group_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" assert sec_desc.owner_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" assert sec_desc.sbz1 == 0 assert sec_desc.control["dacl_present"] assert len(sec_desc.dacl.aces) == 24 assert sec_desc.dacl.aces[0].type == 5 assert str(sec_desc.dacl.aces[0].trustee_sid) == "S-1-5-32-554" assert not sec_desc.control["sacl_present"] assert sec_desc.sacl is None with open(os.path.join(curdir, "testenv", "sd-sample1.bin"), "rb") as data: input_data = data.read() sec_desc = SecurityDescriptor.from_binary(input_data) assert sec_desc.revision == 1 assert sec_desc.group_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" assert sec_desc.owner_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" assert sec_desc.sbz1 == 0 assert sec_desc.control["dacl_present"] assert len(sec_desc.dacl.aces) == 24 assert sec_desc.dacl.aces[0].type == 5 assert sec_desc.dacl.aces[0].trustee_sid == "S-1-5-32-554" assert sec_desc.control["sacl_present"] assert len(sec_desc.sacl.aces) == 3 assert sec_desc.sacl.aces[0].type == 2 assert sec_desc.sacl.aces[0].trustee_sid == "S-1-1-0" @pytest.mark.parametrize( "file", ["sd-sample0.bin", "sd-sample1.bin"], ) def test_to_binary(file): """Test to_binary method.""" curdir = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(curdir, "testenv", file), "rb") as data: expected_data = data.read() test_sec_desc = SecurityDescriptor.from_binary(expected_data) test_data = test_sec_desc.to_binary() ( expected_rev, expected_sbz1, expected_ctrl, expected_offset_owner, expected_offset_group, expected_offset_sacl, expected_offset_dacl, ) = struct.unpack("