Ejemplo n.º 1
0
def test_user_attribute_sync_plugins(monkeypatch):
    monkeypatch.setattr(config, "wato_user_attrs", [{
        'add_custom_macro': False,
        'help': u'VIP attribute',
        'name': 'vip',
        'show_in_table': False,
        'title': u'VIP',
        'topic': 'ident',
        'type': 'TextAscii',
        'user_editable': True
    }])

    monkeypatch.setattr(utils, "user_attribute_registry",
                        utils.UserAttributeRegistry())
    monkeypatch.setattr(userdb, "user_attribute_registry",
                        utils.user_attribute_registry)
    monkeypatch.setattr(ldap, "ldap_attribute_plugin_registry",
                        ldap.LDAPAttributePluginRegistry())

    assert "vip" not in utils.user_attribute_registry
    assert "vip" not in ldap.ldap_attribute_plugin_registry

    userdb.update_config_based_user_attributes()

    assert "vip" in utils.user_attribute_registry
    assert "vip" in ldap.ldap_attribute_plugin_registry

    connection = ldap.LDAPUserConnector({
        "id":
        "ldp",
        "directory_type": ("ad", {
            "connect_to": ("fixed_list", {
                "server": "127.0.0.1",
            })
        })
    })

    ldap_plugin = ldap.ldap_attribute_plugin_registry["vip"]()
    assert ldap_plugin.title == "VIP"
    assert ldap_plugin.help == "VIP attribute"
    assert ldap_plugin.needed_attributes(connection,
                                         {"attr": "vip_attr"}) == ["vip_attr"]
    assert ldap_plugin.needed_attributes(connection,
                                         {"attr": "vip_attr"}) == ["vip_attr"]
    assert isinstance(ldap_plugin.parameters(connection), Dictionary)

    # Test removing previously registered ones
    monkeypatch.setattr(config, "wato_user_attrs", [])
    userdb.update_config_based_user_attributes()

    assert "vip" not in utils.user_attribute_registry
    assert "vip" not in ldap.ldap_attribute_plugin_registry
Ejemplo n.º 2
0
def test_user_attribute_sync_plugins(request_context: None,
                                     monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(
        config,
        "wato_user_attrs",
        [{
            "add_custom_macro": False,
            "help": "VIP attribute",
            "name": "vip",
            "show_in_table": False,
            "title": "VIP",
            "topic": "ident",
            "type": "TextAscii",
            "user_editable": True,
        }],
    )

    monkeypatch.setattr(utils, "user_attribute_registry",
                        utils.UserAttributeRegistry())
    monkeypatch.setattr(userdb, "user_attribute_registry",
                        utils.user_attribute_registry)
    monkeypatch.setattr(ldap, "ldap_attribute_plugin_registry",
                        ldap.LDAPAttributePluginRegistry())

    assert "vip" not in utils.user_attribute_registry
    assert "vip" not in ldap.ldap_attribute_plugin_registry

    userdb.update_config_based_user_attributes()

    assert "vip" in utils.user_attribute_registry
    assert "vip" in ldap.ldap_attribute_plugin_registry

    connection = ldap.LDAPUserConnector({
        "id":
        "ldp",
        "directory_type": (
            "ad",
            {
                "connect_to": (
                    "fixed_list",
                    {
                        "server": "127.0.0.1",
                    },
                )
            },
        ),
    })

    ldap_plugin = ldap.ldap_attribute_plugin_registry["vip"]()
    assert ldap_plugin.title == "VIP"
    assert ldap_plugin.help == "VIP attribute"
    assert ldap_plugin.needed_attributes(connection,
                                         {"attr": "vip_attr"}) == ["vip_attr"]
    assert ldap_plugin.needed_attributes(connection,
                                         {"attr": "vip_attr"}) == ["vip_attr"]
    assert isinstance(ldap_plugin.parameters(connection), Dictionary)

    # Test removing previously registered ones
    monkeypatch.setattr(config, "wato_user_attrs", [])
    userdb.update_config_based_user_attributes()

    assert "vip" not in utils.user_attribute_registry
    assert "vip" not in ldap.ldap_attribute_plugin_registry
Ejemplo n.º 3
0
def mocked_ldap(monkeypatch):
    ldap_mock = MockLdap(_ldap_tree())

    def connect(self, enforce_new=False, enforce_server=None):
        self._default_bind(self._ldap_obj)

    monkeypatch.setattr(ldap.LDAPUserConnector, "connect", connect)
    monkeypatch.setattr(ldap.LDAPUserConnector, "disconnect", lambda self: None)

    ldap_connection = ldap.LDAPUserConnector({
        "id": "default",
        "type": "ldap",
        "description": "Test connection",
        "disabled": False,
        "cache_livetime": 300,
        "suffix": "testldap",
        "active_plugins": {
            'email': {},
            'alias': {},
            'auth_expire': {}
        },
        "directory_type": ("ad", {
            "connect_to": ("fixed_list", {
                "server": "127.0.0.1"
            }),
        }),
        "bind": ("cn=sync-user,ou=users,dc=check-mk,dc=org", "sync-secret"),
        "user_id_umlauts": "keep",
        "user_scope": "sub",
        "user_dn": "ou=users,dc=check-mk,dc=org",
        "group_dn": "ou=groups,dc=check-mk,dc=org",
        "group_scope": "sub",
    })

    ldap_mock.start()
    ldap_connection._ldap_obj = ldap_mock["ldap://127.0.0.1"]

    def search_ext(self,
                   base,
                   scope,
                   filterstr='(objectclass=*)',
                   attrlist=None,
                   attrsonly=0,
                   serverctrls=None):

        # MockLdap does not exactly behave like python ldap library in terms of
        # encoding. The latter want's to have byte encoded strings and MockLdap
        # wants unicode strings :-/. Prepare the data we normally send to
        # python-ldap for MockLdap here.
        if not isinstance(base, str):
            base = base.decode("utf-8")

        if not isinstance(filterstr, str):
            filterstr = filterstr.decode("utf-8")

        return self.search(base, scope, filterstr, attrlist, attrsonly)

    LDAPObject.search_ext = search_ext

    def result_3(self, *args, **kwargs):
        unused_code, response = LDAPObject.result(self, *args, **kwargs)
        return unused_code, encode_to_byte_strings(response), None, []

    LDAPObject.result3 = result_3

    return ldap_connection