def setUp(self): schema = SchemaInfo.from_json(edir_8_8_8_schema) info = DsaInfo.from_json(edir_8_8_8_dsa_info, schema) server = Server.from_definition('MockSyncServer', info, schema) self.connection = Connection(server, user='******', password='******', client_strategy=MOCK_SYNC)
def setUpClass(cls): cls.schema = SchemaInfo.from_json(edir_8_8_8_schema) info = DsaInfo.from_json(edir_8_8_8_dsa_info, cls.schema) cls.server = Server.from_definition('MockSyncServer', info, cls.schema) cls.connection = Connection(cls.server, user='******', password='******', client_strategy=MOCK_SYNC) # create fixtures cls.connection.strategy.add_entry('cn=user1,ou=test', {'userPassword': '******', 'revision': 1}) cls.connection.strategy.add_entry('cn=user2,ou=test', {'userPassword': '******', 'revision': 2}) cls.connection.strategy.add_entry('cn=user3,ou=test', {'userPassword': '******', 'revision': 3})
def __init__(self, isSearch=False): self.MOCK_DATA_PATH = os.getcwd() + "/tests/mock_data/" if isSearch: self.MOCK_DATA_PATH = os.getcwd( ) + "/tests/mock_data/search_specific/" # Create a fake LDAP server from the info and schema json files self.FAKE_SERVER = Server.from_definition( 'my_fake_server', self.MOCK_DATA_PATH + 'mock_server_info.json', self.MOCK_DATA_PATH + 'mock_server_schema.json')
def test_json_definition(self): if not self.connection.strategy.pooled: if not self.connection.server.info: self.connection.refresh_server_info() json_info = self.connection.server.info.to_json() json_schema = self.connection.server.schema.to_json() info = DsaInfo.from_json(json_info) schema = SchemaInfo.from_json(json_schema) server1 = Server.from_definition(test_server, info, schema) json_info1 = server1.info.to_json() json_schema1 = server1.schema.to_json() self.assertEqual(json_info, json_info1) self.assertEqual(json_schema, json_schema1)
def setUp(self): server = Server.from_definition('my_fake_server', 'mokup/info.json', 'mokup/schema.json') model.ldap_conn = Connection(server, user='******', password='******', client_strategy=MOCK_SYNC) model.ldap_conn.strategy.entries_from_json('mokup/entries.json') model.ldap_conn.strategy.add_entry('cn=admin,dc=example,dc=org', { 'userPassword': '******', 'sn': 'admin' }) model.ldap_conn.bind() model.base_dn = 'dc=example,dc=org'
def setUpClass(cls): cls.schema = SchemaInfo.from_json(edir_9_1_4_schema) info = DsaInfo.from_json(edir_9_1_4_dsa_info, cls.schema) cls.server = Server.from_definition('MockSyncServer', info, cls.schema) cls.connection = Connection(cls.server, user='******', password='******', client_strategy=MOCK_SYNC) # create fixtures cls.connection.strategy.add_entry('cn=user1,ou=test', { 'userPassword': '******', 'revision': 1 }) cls.connection.strategy.add_entry('cn=user2,ou=test', { 'userPassword': '******', 'revision': 2 }) cls.connection.strategy.add_entry('cn=user3,ou=test', { 'userPassword': '******', 'revision': 3 })
def build_mock_sds_connection(): url = config.get_config(LDAP_MOCK_DATA_URL_CONFIG_KEY) parsed_url = urlparse(url) _MOCK_DATA_LOADERS[parsed_url.scheme](parsed_url) fake_spine_url = config.get_config(FAKE_SPINE_URL_CONFIG_KEY, default=None) if fake_spine_url: _modify_spine_url(fake_spine_url) else: logger.info( "Skipping spine url modification as config key '%s' is not present", FAKE_SPINE_URL_CONFIG_KEY) fake_server = Server.from_definition('fake_server', _MOCK_DATA_SERVER_INFO_FILE_PATH, _MOCK_DATA_SERVER_SCHEMA_FILE_PATH) fake_connection = Connection(fake_server, client_strategy=MOCK_ASYNC) fake_connection.strategy.entries_from_json( _MOCK_DATA_SERVER_ENTRIES_FILE_PATH) fake_connection.bind() return fake_connection
import os import logging LOG = logging.getLogger(__name__) PACKAGE_NAME = "fn_ldap_utilities" FUNCTION_NAME = "ldap_utilities_search" MOCK_DATA_PATH = os.getcwd() + "/tests/mock_data/search_specific/" # Read the default configuration-data section from the package config_data = get_mock_config_data() # Provide a simulation of the Resilient REST API (uncomment to connect to a real appliance) resilient_mock = "pytest_resilient_circuits.BasicResilientMock" # Create a fake LDAP server from the info and schema json files fake_server = Server.from_definition('my_fake_server', MOCK_DATA_PATH + 'mock_server_info.json', MOCK_DATA_PATH + 'mock_server_schema.json') def mocked_server(): """Mock ldap3 server. :return: Return mocked server object """ server = Mock(return_value=fake_server) return server def mocked_connection(): """Mock ldap3 connection. :return: Return mocked connection object """ # Create a MockSyncStrategy connection to the fake server mocked_connection = Connection(fake_server, user='******', password='******',
def get_connection(bind=None, use_ssl=None, check_names=None, lazy_connection=None, authentication=None, sasl_mechanism=None, sasl_credentials=None, ntlm_credentials=(None, None), get_info=None, usage=None, fast_decoder=None, simple_credentials=(None, None), receive_timeout=None): if bind is None: bind = True if check_names is None: check_names = test_check_names if lazy_connection is None: lazy_connection = test_lazy_connection if authentication is None: authentication = test_authentication if get_info is None: get_info = test_get_info if usage is None: usage = test_usage if fast_decoder is None: fast_decoder = test_fast_decoder if receive_timeout is None: receive_timeout = test_receive_timeout if test_server_type == 'AD' and use_ssl is None: use_ssl = True # Active directory forbids Add operations in cleartext if test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: # define real server if isinstance(test_server, (list, tuple)): server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) for host in test_server: server.add(Server(host=host, use_ssl=use_ssl, port=test_port_ssl if use_ssl else test_port, allowed_referral_hosts=('*', True), get_info=get_info, mode=test_server_mode)) else: server = Server(host=test_server, use_ssl=use_ssl, port=test_port_ssl if use_ssl else test_port, allowed_referral_hosts=('*', True), get_info=get_info, mode=test_server_mode) else: if test_server_type == 'EDIR': schema = SchemaInfo.from_json(edir_8_8_8_schema) info = DsaInfo.from_json(edir_8_8_8_dsa_info, schema) server = Server.from_definition('MockSyncServer', info, schema) elif test_server_type == 'AD': schema = SchemaInfo.from_json(ad_2012_r2_schema) info = DsaInfo.from_json(ad_2012_r2_dsa_info, schema) server = Server.from_definition('MockSyncServer', info, schema) elif test_server_type == 'SLAPD': schema = SchemaInfo.from_json(slapd_2_4_schema) info = DsaInfo.from_json(slapd_2_4_dsa_info, schema) server = Server.from_definition('MockSyncServer', info, schema) if authentication == SASL: connection = Connection(server, auto_bind=bind, version=3, client_strategy=test_strategy, authentication=SASL, sasl_mechanism=sasl_mechanism, sasl_credentials=sasl_credentials, lazy=lazy_connection, pool_name='pool1', check_names=check_names, collect_usage=usage, fast_decoder=fast_decoder, receive_timeout=receive_timeout) elif authentication == NTLM: connection = Connection(server, auto_bind=bind, version=3, client_strategy=test_strategy, user=ntlm_credentials[0], password=ntlm_credentials[1], authentication=NTLM, lazy=lazy_connection, pool_name='pool1', check_names=check_names, collect_usage=usage, fast_decoder=fast_decoder, receive_timeout=receive_timeout) elif authentication == ANONYMOUS: connection = Connection(server, auto_bind=bind, version=3, client_strategy=test_strategy, user=None, password=None, authentication=ANONYMOUS, lazy=lazy_connection, pool_name='pool1', check_names=check_names, collect_usage=usage, fast_decoder=fast_decoder, receive_timeout=receive_timeout) else: connection = Connection(server, auto_bind=bind, version=3, client_strategy=test_strategy, user=simple_credentials[0] or test_user, password=simple_credentials[1] or test_password, authentication=authentication, lazy=lazy_connection, pool_name='pool1', check_names=check_names, collect_usage=usage, fast_decoder=fast_decoder, receive_timeout=receive_timeout) if test_strategy in [MOCK_SYNC, MOCK_ASYNC]: # create authentication identities for testing mock strategies connection.strategy.add_entry(test_user, {'objectClass': 'inetOrgPerson', 'userPassword': test_password}) connection.strategy.add_entry(test_secondary_user, {'objectClass': 'inetOrgPerson', 'userPassword': test_secondary_password}) connection.strategy.add_entry(test_sasl_user_dn, {'objectClass': 'inetOrgPerson', 'userPassword': test_sasl_password}) connection.strategy.add_entry(test_sasl_secondary_user_dn, {'objectClass': 'inetOrgPerson', 'userPassword': test_sasl_secondary_password}) # connection.strategy.add_entry(test_ntlm_user, {'objectClass': 'inetOrgPerson', 'userPassword': test_ntlm_password}) if bind: connection.bind() return connection
from resilient_circuits.util import get_config_data, get_function_definition from resilient_circuits import SubmitTestFunction, FunctionResult from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, MOCK_SYNC import logging LOG = logging.getLogger(__name__) PACKAGE_NAME = "fn_ldap_search" FUNCTION_NAME = "ldap_search" # Read the default configuration-data section from the package config_data = get_config_data(PACKAGE_NAME) # Provide a simulation of the Resilient REST API (uncomment to connect to a real appliance) resilient_mock = "pytest_resilient_circuits.BasicResilientMock" # Create a fake LDAP server from the info and schema json files fake_server = Server.from_definition('my_fake_server', 'mock_server_info.json', 'mock_server_schema.json') def mocked_server(): """Mock ldap3 server. :return: Return mocked server object """ server = Mock(return_value=fake_server) return server def mocked_connection(): """Mock ldap3 connection. :return: Return mocked connection object """ # Create a MockSyncStrategy connection to the fake server
"entries": [ i for i in entries["entries"] if "posixAccount" not in i["attributes"]["objectClass"] # don't add the accounts ] } with open(ENTRIES_OUTPUT, "w") as f: json.dump(filtered_entries, f, indent=2) else: raise RuntimeError("ldap search failed!") # Close the connection to the real server connection.unbind() # Create a fake server from the info and schema json files fake_server = Server.from_definition("csua_mock", INFO_OUTPUT, SCHEMA_OUTPUT) # Create a MockSyncStrategy connection to the fake server fake_connection = Connection(fake_server, client_strategy=MOCK_SYNC) # Populate the DIT of the fake server fake_connection.strategy.entries_from_json(ENTRIES_OUTPUT) # Add a fake user for Simple binding fake_connection.strategy.add_entry( "cn=django_test_user,ou=People,dc=csua,dc=berkeley,dc=edu", { "uid": "django_test_user", "userPassword": "******" }, )