Debian Codegraph @

dirsrvtests/tests/suites/replication/encryption_cl5_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 import logging
10 import pytest
11 import pdb
12 from lib389.utils import ensure_bytes, ds_supports_new_changelog
13 from lib389.replica import ReplicationManager
14 from lib389.dseldif import DSEldif
15 from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
16 from test389.topologies import topology_m2
17 from lib389._constants import *
18
19 pytestmark = pytest.mark.tier1
20
21 ATTRIBUTE = 'unhashed#user#password'
22
23 DEBUGGING = os.getenv("DEBUGGING", default=False)
24 if DEBUGGING:
25 logging.getLogger(__name__).setLevel(logging.DEBUG)
26 else:
27 logging.getLogger(__name__).setLevel(logging.INFO)
28 log = logging.getLogger(__name__)
29
30
31 @pytest.fixture(scope="module")
32 def topology_with_tls(topology_m2):
33 """Enable TLS on all suppliers"""
34
35 [i.enable_tls() for i in topology_m2]
36
37 repl = ReplicationManager(DEFAULT_SUFFIX)
38 repl.test_replication(topology_m2.ms['supplier1'], topology_m2.ms['supplier2'])
39
40 return topology_m2
41
42
43 def _enable_changelog_encryption(inst, encrypt_algorithm):
44 """Configure changelog encryption for supplier"""
45
46 dse_ldif = DSEldif(inst)
47 log.info('Configuring changelog encryption:{} for: {}'.format(inst.serverid, encrypt_algorithm))
48 inst.stop()
49 if ds_supports_new_changelog():
50 changelog = 'cn=changelog,{}'.format(DN_USERROOT_LDBM)
51 else:
52 changelog = DN_CHANGELOG
53
54 dse_ldif.replace(changelog, 'nsslapd-encryptionalgorithm', encrypt_algorithm)
55 if dse_ldif.get(changelog, 'nsSymmetricKey'):
56 dse_ldif.delete(changelog, 'nsSymmetricKey')
57 inst.start()
58
59
60 def _check_unhashed_userpw_encrypted(inst, change_type, user_dn, user_pw, is_encrypted):
61 """Check if unhashed#user#password attribute value is encrypted or not"""
62
63 if ds_supports_new_changelog():
64 log.info('Running dbscan -f to check {} attr'.format(ATTRIBUTE))
65 dbscanOut = inst.dbscan(DEFAULT_BENAME, 'replication_changelog')
66 else:
67 changelog_dbdir = os.path.join(os.path.dirname(inst.dbdir), DEFAULT_CHANGELOG_DB)
68 for changelog_dbfile in glob.glob(f'{changelog_dbdir}*/*.db*'):
69 log.info('Changelog dbfile file exist: {}'.format(changelog_dbfile))
70 log.info('Running dbscan -f to check {} attr'.format(ATTRIBUTE))
71 dbscanOut = inst.dbscan(DEFAULT_CHANGELOG_DB, changelog_dbfile)
72
73 count = 0
74 for entry in dbscanOut.split(b'dbid: '):
75 if ensure_bytes('operation: {}'.format(change_type)) in entry and\
76 ensure_bytes(ATTRIBUTE) in entry and ensure_bytes(user_dn.lower()) in entry.lower():
77 count += 1
78 user_pw_attr = ensure_bytes('{}: {}'.format(ATTRIBUTE, user_pw))
79 if is_encrypted:
80 assert user_pw_attr not in entry, 'Changelog entry contains clear text password'
81 else:
82 assert user_pw_attr in entry, 'Changelog entry does not contain clear text password'
83 assert count, 'Operation type and DN of the entry not matched in changelog'
84
85 #unstable or unstatus tests, skipped for now
86 @pytest.mark.flaky(max_runs=2, min_passes=1)
87 def test_algorithm_unhashed(topology_with_tls):
88 """Check encryption algorithm AES
89 And check unhashed#user#password attribute for encryption.
90
91 :id: b7a37bf8-4b2e-4dbd-9891-70117d67558c
92 :parametrized: yes
93 :setup: Replication with two suppliers and SSL configured.
94 :steps: 1. Enable changelog encrytion on supplier1
95 2. Add a user to supplier1/supplier2
96 3. Run dbscan -f on m1 to check unhashed#user#password
97 attribute is encrypted.
98 4. Run dbscan -f on m2 to check unhashed#user#password
99 attribute is in cleartext.
100 5. Modify password in supplier2/supplier1
101 6. Run dbscan -f on m1 to check unhashed#user#password
102 attribute is encrypted.
103 7. Run dbscan -f on m2 to check unhashed#user#password
104 attribute is in cleartext.
105 :expectedresults:
106 1. It should pass
107 2. It should pass
108 3. It should pass
109 4. It should pass
110 5. It should pass
111 6. It should pass
112 7. It should pass
113 """
114 encryption = 'AES'
115 m1 = topology_with_tls.ms['supplier1']
116 m2 = topology_with_tls.ms['supplier2']
117 m1.config.set('nsslapd-unhashed-pw-switch', 'on')
118 m2.config.set('nsslapd-unhashed-pw-switch', 'on')
119 test_passw = 'm2Test199'
120
121 _enable_changelog_encryption(m1, encryption)
122
123 for inst1, inst2 in ((m1, m2), (m2, m1)):
124 # need to create a user specific to the encryption
125 # else the two runs will hit the same user
126 user_props={
127 'uid': 'testuser_%s' % encryption,
128 'cn' : 'testuser_%s' % encryption,
129 'sn' : 'user',
130 'uidNumber' : '1000',
131 'gidNumber' : '1000',
132 'homeDirectory' : '/home/testuser_%s' % encryption
133 }
134 user_props["userPassword"] = PASSWORD
135 users = UserAccounts(inst1, DEFAULT_SUFFIX)
136 tuser = users.create(properties=user_props)
137
138 _check_unhashed_userpw_encrypted(m1, 'add', tuser.dn, PASSWORD, True)
139 _check_unhashed_userpw_encrypted(m2, 'add', tuser.dn, PASSWORD, False)
140
141 users = UserAccounts(inst2, DEFAULT_SUFFIX)
142 tuser = users.get(tuser.rdn)
143 tuser.set('userPassword', test_passw)
144 _check_unhashed_userpw_encrypted(m1, 'modify', tuser.dn, test_passw, True)
145 _check_unhashed_userpw_encrypted(m2, 'modify', tuser.dn, test_passw, False)
146 tuser.delete()
147
148
149 if __name__ == '__main__':
150 # Run isolated
151 # -s for DEBUG mode
152 CURRENT_FILE = os.path.realpath(__file__)
153 pytest.main("-s {}".format(CURRENT_FILE))