def withLdapConfig(self, config: LdapConfig): """ Validates the config before continuing """ validateLdapConfig(config) self.endpoint = buildLdapEndpoint(config) self.tlsEnabled = config.isTLSEnabled() self.client_cert = config.getClientCert() self.client_key = config.getClientKey() self.ca_cert = config.getCACert() return self
def validateLdapConfig(ldap_config: LdapConfig = LdapConfig()): """ Simple validation of the Ldap Config for the purposes of the testing. If TLS is enabled, check that the client cert, key and ca.crt exist. :raises: FileNotFoundError when the filepath does not exist :raises: FileNotFoundError when the filepath points to a location that is not a file :raises: ValueError when the file is empty """ if ldap_config.isTLSEnabled(): _validate_file(ldap_config.getCACert()) _validate_file(ldap_config.getClientCert()) _validate_file(ldap_config.getClientKey())
def buildLdapEndpoint(ldap_config: LdapConfig = LdapConfig()): """ Builds the endpoint (i.e. ldaps://some.url:689) for the LdapService from the LdapConfig If isTLSEnabled then it will prefix with 'ldaps' instead of 'ldap'. :returns: URL starting with either 'ldap' or 'ldaps' """ endpoint = "ldap" if ldap_config.isTLSEnabled(): endpoint += "s" endpoint += "://" endpoint += ldap_config.getHostname() endpoint += ":" endpoint += ldap_config.getPort() return endpoint
def test_with_default_ldap_config(self): connection_factory = LdapConnectionFactory\ .builder()\ .withLdapConfig(LdapConfig())\ .create() self.assertEqual(connection_factory.getEndpoint(), 'ldap://localhost:389') self.assertFalse(connection_factory.isTlsEnabled()) self.assertEqual(connection_factory.getClientCert(), '') self.assertEqual(connection_factory.getClientKey(), '') self.assertEqual(connection_factory.getCACert(), '')
def test_tls_false_should_use_ldap(self): os.environ[ldapEnableTLSKey] = str(False) endpoint = buildLdapEndpoint(LdapConfig()) self.assertEqual(ldapEndpoint, endpoint)
def test_environment_var_override_ca_cert(self): os.environ[ldapCACertKey] = ldapCACert self.assertEqual(LdapConfig().getCACert(), ldapCACert)
def test_tls_true_should_use_ldaps(self): endpoint = buildLdapEndpoint(LdapConfig()) self.assertEqual(ldapsEndpoint, endpoint)
def test_environment_var_override_client_key(self): os.environ[ldapClientPrivateKey] = ldapClientPrivate self.assertEqual(LdapConfig().getClientKey(), ldapClientPrivate)
def test_default_ca_cert_config(self): self.assertEqual(LdapConfig().getCACert(), "")
def test_environment_var_overrides_enable_tls_flag(self): os.environ[ldapEnableTLSKey] = str(ldapEnableTLS) self.assertTrue(type(LdapConfig().isTLSEnabled()) is bool) self.assertTrue(LdapConfig().isTLSEnabled())
def test_default_client_key_config(self): self.assertEqual(LdapConfig().getClientKey(), "")
def test_default_enable_tls_flag(self): self.assertTrue(type(LdapConfig().isTLSEnabled()) is bool) self.assertFalse(LdapConfig().isTLSEnabled())
def test_environment_var_overrides_port_config(self): os.environ[ldapPortKey] = ldapPort self.assertEqual(LdapConfig().getPort(), ldapPort)
def test_default_port_config(self): self.assertEqual(LdapConfig().getPort(), defaultPort)
def test_environment_var_overrides_hostname_config(self): os.environ[ldapHostnameKey] = ldapHostname self.assertEqual(LdapConfig().getHostname(), ldapHostname)
def test_default_hostname_config(self): self.assertEqual(LdapConfig().getHostname(), defaultHostname)