test_ldap.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import salt.auth.ldap
  2. from tests.support.mixins import LoaderModuleMockMixin
  3. from tests.support.mock import patch
  4. from tests.support.unit import TestCase, skipIf
  5. class Bind:
  6. """
  7. fake search_s return
  8. """
  9. @staticmethod
  10. def search_s(*args, **kwargs):
  11. return [
  12. (
  13. "cn=saltusers,cn=groups,cn=compat,dc=saltstack,dc=com",
  14. {"memberUid": [b"saltuser"], "cn": [b"saltusers"]},
  15. ),
  16. ]
  17. @skipIf(not salt.auth.ldap.HAS_LDAP, "Install python-ldap for this test")
  18. class LDAPAuthTestCase(TestCase, LoaderModuleMockMixin):
  19. """
  20. Unit tests for salt.auth.ldap
  21. """
  22. @classmethod
  23. def setUpClass(cls):
  24. cls.config = {
  25. "auth.ldap.binddn": "uid={{username}},cn=users,cn=compat,dc=saltstack,dc=com",
  26. "auth.ldap.port": 389,
  27. "auth.ldap.tls": False,
  28. "auth.ldap.server": "172.18.0.2",
  29. "auth.ldap.accountattributename": "memberUid",
  30. "auth.ldap.groupattribute": "memberOf",
  31. "auth.ldap.group_basedn": "cn=groups,cn=compat,dc=saltstack,dc=com",
  32. "auth.ldap.basedn": "dc=saltstack,dc=com",
  33. "auth.ldap.group_filter": "(&(memberUid={{ username }})(objectClass=posixgroup))",
  34. }
  35. @classmethod
  36. def tearDownClass(cls):
  37. cls.config = cls.opts = None
  38. def setUp(self):
  39. self.opts = self.config.copy()
  40. def tearDown(self):
  41. self.opts = None
  42. def setup_loader_modules(self):
  43. return {salt.auth.ldap: {"__opts__": self.config.copy()}}
  44. def test_config(self):
  45. """
  46. Test that the _config function works correctly
  47. """
  48. self.assertEqual(salt.auth.ldap._config("basedn"), "dc=saltstack,dc=com")
  49. self.assertEqual(
  50. salt.auth.ldap._config("group_filter"),
  51. "(&(memberUid={{ username }})(objectClass=posixgroup))",
  52. )
  53. self.assertEqual(salt.auth.ldap._config("accountattributename"), "memberUid")
  54. self.assertEqual(salt.auth.ldap._config("groupattribute"), "memberOf")
  55. def test_groups_freeipa(self):
  56. """
  57. test groups in freeipa
  58. """
  59. self.opts["auth.ldap.freeipa"] = True
  60. with patch.dict(salt.auth.ldap.__opts__, self.opts):
  61. with patch("salt.auth.ldap._bind", return_value=Bind):
  62. self.assertIn(
  63. "saltusers", salt.auth.ldap.groups("saltuser", password="password")
  64. )
  65. def test_groups(self):
  66. """
  67. test groups in ldap
  68. """
  69. with patch("salt.auth.ldap._bind", return_value=Bind):
  70. self.assertIn(
  71. "saltusers", salt.auth.ldap.groups("saltuser", password="password")
  72. )
  73. def test_groups_activedirectory(self):
  74. """
  75. test groups in activedirectory
  76. """
  77. self.opts["auth.ldap.activedirectory"] = True
  78. with patch.dict(salt.auth.ldap.__opts__, self.opts):
  79. with patch("salt.auth.ldap._bind", return_value=Bind):
  80. self.assertIn(
  81. "saltusers", salt.auth.ldap.groups("saltuser", password="password")
  82. )
  83. def test_auth_nopass(self):
  84. opts = self.opts.copy()
  85. opts["auth.ldap.bindpw"] = "p@ssw0rd!"
  86. with patch.dict(salt.auth.ldap.__opts__, opts):
  87. with patch("salt.auth.ldap._bind_for_search", return_value=Bind):
  88. self.assertFalse(salt.auth.ldap.auth("foo", None))
  89. def test_auth_nouser(self):
  90. opts = self.opts.copy()
  91. opts["auth.ldap.bindpw"] = "p@ssw0rd!"
  92. with patch.dict(salt.auth.ldap.__opts__, opts):
  93. with patch("salt.auth.ldap._bind_for_search", return_value=Bind):
  94. self.assertFalse(salt.auth.ldap.auth(None, "foo"))
  95. def test_auth_nouserandpass(self):
  96. opts = self.opts.copy()
  97. opts["auth.ldap.bindpw"] = "p@ssw0rd!"
  98. with patch.dict(salt.auth.ldap.__opts__, opts):
  99. with patch("salt.auth.ldap._bind_for_search", return_value=Bind):
  100. self.assertFalse(salt.auth.ldap.auth(None, None))