simple_gssapi_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2017 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. from lib389.topologies import topology_st_gssapi, gssapi_ack
  9. from lib389.idm.user import UserAccounts
  10. from lib389.saslmap import SaslMappings
  11. from lib389._constants import DEFAULT_SUFFIX
  12. import ldap
  13. import subprocess
  14. import os
  15. import pytest
  16. pytestmark = pytest.mark.tier1
  17. @pytest.fixture(scope='module')
  18. def testuser(topology_st_gssapi):
  19. # Create a user
  20. users = UserAccounts(topology_st_gssapi.standalone, DEFAULT_SUFFIX)
  21. user = users.create(properties={
  22. 'uid': 'testuser',
  23. 'cn' : 'testuser',
  24. 'sn' : 'user',
  25. 'uidNumber' : '1000',
  26. 'gidNumber' : '2000',
  27. 'homeDirectory' : '/home/testuser'
  28. })
  29. # Give them a krb princ
  30. user.create_keytab()
  31. return user
  32. @gssapi_ack
  33. def test_gssapi_bind(topology_st_gssapi, testuser):
  34. """Test that we can bind with GSSAPI
  35. :id: 894a4c27-3d4c-4ba3-aa33-2910032e3783
  36. :setup: standalone gssapi instance
  37. :steps:
  38. 1. Bind with sasl/gssapi
  39. :expectedresults:
  40. 1. Bind succeeds
  41. """
  42. conn = testuser.bind_gssapi()
  43. assert(conn.whoami_s() == "dn: %s" % testuser.dn.lower())
  44. @gssapi_ack
  45. def test_invalid_sasl_map(topology_st_gssapi, testuser):
  46. """Test that auth fails when we can not map a user.
  47. :id: dd4218eb-9237-4611-ba2f-1781391cadd1
  48. :setup: standalone gssapi instance
  49. :steps:
  50. 1. Invalidate a sasl map
  51. 2. Attempt to bind
  52. :expectedresults:
  53. 1. The sasl map is invalid.
  54. 2. The bind fails.
  55. """
  56. saslmaps = SaslMappings(topology_st_gssapi.standalone)
  57. saslmap = saslmaps.get('suffix map')
  58. saslmap.set('nsSaslMapFilterTemplate', '(invalidattr=\\1)')
  59. with pytest.raises(ldap.INVALID_CREDENTIALS):
  60. conn = testuser.bind_gssapi()
  61. saslmap.set('nsSaslMapFilterTemplate', '(uid=\\1)')
  62. @gssapi_ack
  63. def test_missing_user(topology_st_gssapi):
  64. """Test that binding with no user does not work.
  65. :id: 109b5ab8-6556-4222-92d6-398476a50d30
  66. :setup: standalone gssapi instance
  67. :steps:
  68. 1. Create a principal with a name that is not mappable
  69. 2. Attempt to bind
  70. :expectedresults:
  71. 1. The principal is created
  72. 2. The bind fails.
  73. """
  74. # Make a principal and bind with no user.
  75. st = topology_st_gssapi.standalone
  76. st.realm.create_principal("doesnotexist")
  77. st.realm.create_keytab("doesnotexist", "/tmp/doesnotexist.keytab")
  78. # Now try to bind.
  79. subprocess.call(['/usr/bin/kdestroy', '-A'])
  80. os.environ["KRB5_CLIENT_KTNAME"] = "/tmp/doesnotexist.keytab"
  81. conn = ldap.initialize(st.toLDAPURL())
  82. sasltok = ldap.sasl.gssapi()
  83. with pytest.raises(ldap.INVALID_CREDENTIALS):
  84. conn.sasl_interactive_bind_s('', sasltok)
  85. @gssapi_ack
  86. def test_support_mech(topology_st_gssapi, testuser):
  87. """Test allowed sasl mechs works when GSSAPI is allowed
  88. :id: 6ec80aca-00c4-4141-b96b-3ae8837fc751
  89. :setup: standalone gssapi instance
  90. :steps:
  91. 1. Add GSSAPI to allowed sasl mechanisms.
  92. 2. Attempt to bind
  93. :expectedresults:
  94. 1. The allowed mechs are changed.
  95. 2. The bind succeeds.
  96. """
  97. topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'GSSAPI EXTERNAL ANONYMOUS')
  98. conn = testuser.bind_gssapi()
  99. assert(conn.whoami_s() == "dn: %s" % testuser.dn.lower())
  100. @gssapi_ack
  101. def test_rejected_mech(topology_st_gssapi, testuser):
  102. """Test allowed sasl mechs fail when GSSAPI is not allowed.
  103. :id: 7896c756-6f65-4390-a844-12e2eec19675
  104. :setup: standalone gssapi instance
  105. :steps:
  106. 1. Add GSSAPI to allowed sasl mechanisms.
  107. 2. Attempt to bind
  108. :expectedresults:
  109. 1. The allowed mechs are changed.
  110. 2. The bind fails.
  111. """
  112. topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'EXTERNAL ANONYMOUS')
  113. with pytest.raises(ldap.STRONG_AUTH_NOT_SUPPORTED):
  114. conn = testuser.bind_gssapi()
  115. topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'GSSAPI EXTERNAL ANONYMOUS')