| 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 |
|
| 10 |
import pytest |
| 11 |
import ldap |
| 12 |
|
| 13 |
from test389.topologies import topology_st |
| 14 |
# This pulls in logging I think |
| 15 |
from lib389.utils import * |
| 16 |
from lib389._constants import DEFAULT_SUFFIX, DEFAULT_SECURE_PORT |
| 17 |
from lib389.sasl import PlainSASL |
| 18 |
from lib389.idm.services import ServiceAccounts, ServiceAccount |
| 19 |
|
| 20 |
pytestmark = pytest.mark.tier1 |
| 21 |
|
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
def test_basic_feature(topology_st): |
| 26 |
"""Check basic SASL functionality for PLAIN mechanism |
| 27 |
|
| 28 |
:id: 75ddc6fa-aa5a-4025-9c71-1abad20c91fc |
| 29 |
:setup: Standalone instance |
| 30 |
:steps: |
| 31 |
1. Stop the instance |
| 32 |
2. Clean up confdir from previous cert and key files |
| 33 |
3. Create RSA files: CA, key and cert |
| 34 |
4. Start the instance |
| 35 |
5. Create RSA entry |
| 36 |
6. Set nsslapd-secureport to 636 and nsslapd-security to 'on' |
| 37 |
7. Restart the instance |
| 38 |
8. Create a user |
| 39 |
9. Check we can bind |
| 40 |
10. Check that PLAIN is listed in supported mechs |
| 41 |
11. Set up Plain SASL credentials |
| 42 |
12. Try to open a connection without TLS |
| 43 |
13. Try to open a connection with TLS |
| 44 |
14. Try to open a connection with a wrong password |
| 45 |
:expectedresults: |
| 46 |
1. The instance should stop |
| 47 |
2. Confdir should be clean |
| 48 |
3. RSA files should be created |
| 49 |
4. The instance should start |
| 50 |
5. RSA entry should be created |
| 51 |
6. nsslapd-secureport and nsslapd-security should be set successfully |
| 52 |
7. The instance should be restarted |
| 53 |
8. User should be created |
| 54 |
9. Bind should be successful |
| 55 |
10. PLAIN should be listed in supported mechs |
| 56 |
11. Plain SASL should be successfully set |
| 57 |
12. AUTH_UNKNOWN exception should be raised |
| 58 |
13. The connection should open |
| 59 |
14. INVALID_CREDENTIALS exception should be raised |
| 60 |
""" |
| 61 |
|
| 62 |
standalone = topology_st.standalone |
| 63 |
standalone.enable_tls() |
| 64 |
|
| 65 |
# Create a user |
| 66 |
sas = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 67 |
sas._basedn = DEFAULT_SUFFIX |
| 68 |
sa = sas.create(properties={'cn': 'testaccount', 'userPassword': 'password'}) |
| 69 |
# Check we can bind. This will raise exceptions if it fails. |
| 70 |
sa.bind('password') |
| 71 |
|
| 72 |
# Check that PLAIN is listed in supported mechns. |
| 73 |
assert(standalone.rootdse.supports_sasl_plain()) |
| 74 |
|
| 75 |
# The sasl parameters don't change, so set them up now. |
| 76 |
# Do we need the sasl map dn:? |
| 77 |
auth_tokens = PlainSASL("dn:%s" % sa.dn, 'password') |
| 78 |
|
| 79 |
# Check that it fails without TLS |
| 80 |
with pytest.raises(ldap.AUTH_UNKNOWN): |
| 81 |
conn = sa.sasl_bind(uri=standalone.get_ldap_uri(), saslmethod='PLAIN', sasltoken=auth_tokens, connOnly=True) |
| 82 |
|
| 83 |
# We *have* to use REQCERT NEVER here because python ldap fails cert verification for .... some reason that even |
| 84 |
# I can not solve. I think it's leaking state across connections in start_tls_s? |
| 85 |
|
| 86 |
# Check that it works with TLS |
| 87 |
conn = sa.sasl_bind(uri=standalone.get_ldaps_uri(), saslmethod='PLAIN', sasltoken=auth_tokens, connOnly=True) |
| 88 |
conn.close() |
| 89 |
|
| 90 |
# Check that it correct fails our bind if we don't have the password. |
| 91 |
auth_tokens = PlainSASL("dn:%s" % sa.dn, 'password-wrong') |
| 92 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 93 |
conn = sa.sasl_bind(uri=standalone.get_ldaps_uri(), saslmethod='PLAIN', sasltoken=auth_tokens, connOnly=True) |
| 94 |
|
| 95 |
# Done! |