Debian Codegraph @

dirsrvtests/tests/suites/gssapi/simple_gssapi_test.py

raw
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
9 from test389.topologies import topology_st_gssapi, gssapi_ack
10 from lib389.idm.user import UserAccounts
11
12 from lib389.saslmap import SaslMappings
13
14 from lib389._constants import DEFAULT_SUFFIX
15
16 import ldap
17 import subprocess
18 import os
19 import pytest
20
21 pytestmark = pytest.mark.tier1
22
23 @pytest.fixture(scope='module')
24 def testuser(topology_st_gssapi):
25 # Create a user
26 users = UserAccounts(topology_st_gssapi.standalone, DEFAULT_SUFFIX)
27 user = users.create(properties={
28 'uid': 'testuser',
29 'cn' : 'testuser',
30 'sn' : 'user',
31 'uidNumber' : '1000',
32 'gidNumber' : '2000',
33 'homeDirectory' : '/home/testuser'
34 })
35 # Give them a krb princ
36 user.create_keytab()
37 # Make krb5 config readable by everyone for the tests to work
38 os.chmod(user._instance.realm.krb5confrealm, 0o644)
39 return user
40
41 @gssapi_ack
42 def test_gssapi_bind(topology_st_gssapi, testuser):
43 """Test that we can bind with GSSAPI
44
45 :id: 894a4c27-3d4c-4ba3-aa33-2910032e3783
46
47 :setup: standalone gssapi instance
48
49 :steps:
50 1. Bind with sasl/gssapi
51 :expectedresults:
52 1. Bind succeeds
53
54 """
55 conn = testuser.bind_gssapi()
56 assert(conn.whoami_s() == "dn: %s" % testuser.dn.lower())
57
58 @gssapi_ack
59 def test_invalid_sasl_map(topology_st_gssapi, testuser):
60 """Test that auth fails when we can not map a user.
61
62 :id: dd4218eb-9237-4611-ba2f-1781391cadd1
63
64 :setup: standalone gssapi instance
65
66 :steps:
67 1. Invalidate a sasl map
68 2. Attempt to bind
69 :expectedresults:
70 1. The sasl map is invalid.
71 2. The bind fails.
72 """
73 saslmaps = SaslMappings(topology_st_gssapi.standalone)
74 saslmap = saslmaps.get('suffix map')
75 saslmap.set('nsSaslMapFilterTemplate', '(invalidattr=\\1)')
76
77 with pytest.raises(ldap.INVALID_CREDENTIALS):
78 conn = testuser.bind_gssapi()
79
80 saslmap.set('nsSaslMapFilterTemplate', '(uid=\\1)')
81
82 @gssapi_ack
83 def test_missing_user(topology_st_gssapi):
84 """Test that binding with no user does not work.
85
86 :id: 109b5ab8-6556-4222-92d6-398476a50d30
87
88 :setup: standalone gssapi instance
89
90 :steps:
91 1. Create a principal with a name that is not mappable
92 2. Attempt to bind
93 :expectedresults:
94 1. The principal is created
95 2. The bind fails.
96 """
97 # Make a principal and bind with no user.
98 st = topology_st_gssapi.standalone
99 st.realm.create_principal("doesnotexist")
100 st.realm.create_keytab("doesnotexist", "/tmp/doesnotexist.keytab")
101 # Now try to bind.
102 subprocess.call(['kdestroy', '-A'])
103 os.environ["KRB5_CLIENT_KTNAME"] = "/tmp/doesnotexist.keytab"
104
105 conn = ldap.initialize(st.toLDAPURL())
106 sasltok = ldap.sasl.gssapi()
107
108 with pytest.raises(ldap.INVALID_CREDENTIALS):
109 conn.sasl_interactive_bind_s('', sasltok)
110
111 @gssapi_ack
112 def test_support_mech(topology_st_gssapi, testuser):
113 """Test allowed sasl mechs works when GSSAPI is allowed
114
115 :id: 6ec80aca-00c4-4141-b96b-3ae8837fc751
116
117 :setup: standalone gssapi instance
118
119 :steps:
120 1. Add GSSAPI to allowed sasl mechanisms.
121 2. Attempt to bind
122 :expectedresults:
123 1. The allowed mechs are changed.
124 2. The bind succeeds.
125 """
126 topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'GSSAPI EXTERNAL ANONYMOUS')
127 conn = testuser.bind_gssapi()
128 assert(conn.whoami_s() == "dn: %s" % testuser.dn.lower())
129
130 @gssapi_ack
131 def test_rejected_mech(topology_st_gssapi, testuser):
132 """Test allowed sasl mechs fail when GSSAPI is not allowed.
133
134 :id: 7896c756-6f65-4390-a844-12e2eec19675
135
136 :setup: standalone gssapi instance
137
138 :steps:
139 1. Add GSSAPI to allowed sasl mechanisms.
140 2. Attempt to bind
141 :expectedresults:
142 1. The allowed mechs are changed.
143 2. The bind fails.
144 """
145 topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'EXTERNAL ANONYMOUS')
146 with pytest.raises(ldap.STRONG_AUTH_NOT_SUPPORTED):
147 conn = testuser.bind_gssapi()
148 topology_st_gssapi.standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'GSSAPI EXTERNAL ANONYMOUS')
149