Debian Codegraph @

dirsrvtests/tests/suites/attr_encryption/attr_encryption_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 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 logging
11 import pytest
12 from lib389.tasks import *
13 from test389.topologies import topology_st as topo
14 from lib389.utils import *
15 from lib389._constants import DEFAULT_SUFFIX
16 from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
17 from lib389.backend import Backends
18 from lib389.idm.domain import Domain
19 from lib389.encrypted_attributes import EncryptedAttrs
20
21 pytestmark = pytest.mark.tier1
22
23 USER_DN = 'uid=test_user,%s' % DEFAULT_SUFFIX
24
25 logging.getLogger(__name__).setLevel(logging.INFO)
26 log = logging.getLogger(__name__)
27
28
29 @pytest.fixture(scope="module")
30 def enable_user_attr_encryption(topo, request):
31 """ Enables attribute encryption for various attributes
32 Adds a test user with encrypted attributes
33 """
34
35 log.info("Enable TLS for attribute encryption")
36 topo.standalone.enable_tls()
37
38 log.info("Enables attribute encryption")
39 backends = Backends(topo.standalone)
40 backend = backends.list()[0]
41 encrypt_attrs = EncryptedAttrs(topo.standalone, basedn='cn=encrypted attributes,{}'.format(backend.dn))
42 log.info("Enables attribute encryption for employeeNumber and telephoneNumber")
43 emp_num_encrypt = encrypt_attrs.create(properties={'cn': 'employeeNumber', 'nsEncryptionAlgorithm': 'AES'})
44 telephone_encrypt = encrypt_attrs.create(properties={'cn': 'telephoneNumber', 'nsEncryptionAlgorithm': '3DES'})
45
46 log.info("Add a test user with encrypted attributes")
47 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
48 test_user = users.create(properties=TEST_USER_PROPERTIES)
49 test_user.replace('employeeNumber', '1000')
50 test_user.replace('telephoneNumber', '1234567890')
51
52 def fin():
53 log.info("Remove attribute encryption for various attributes")
54 emp_num_encrypt.delete()
55 telephone_encrypt.delete()
56
57 request.addfinalizer(fin)
58 return test_user
59
60
61 def test_basic(topo, enable_user_attr_encryption):
62 """Tests encrypted attributes with a test user entry
63
64 :id: d767d5c8-b934-4b14-9774-bd13480d81b3
65 :setup: Standalone instance
66 Enable AES encryption config on employeenumber
67 Enable 3DES encryption config on telephoneNumber
68 Add a test user with with encrypted attributes
69 :steps:
70 1. Restart the server
71 2. Check employeenumber encryption enabled
72 3. Check telephoneNumber encryption enabled
73 4. Check that encrypted attribute is present for user i.e. telephoneNumber
74 :expectedresults:
75 1. This should be successful
76 2. This should be successful
77 3. This should be successful
78 4. This should be successful
79 """
80
81 log.info("Restart the server")
82 topo.standalone.restart()
83 backends = Backends(topo.standalone)
84 backend = backends.list()[0]
85 encrypt_attrs = backend.get_encrypted_attrs()
86
87 log.info("Extracting values of cn from the list of objects in encrypt_attrs")
88 log.info("And appending the cn values in a list")
89 enc_attrs_cns = []
90 for enc_attr in encrypt_attrs:
91 enc_attrs_cns.append(enc_attr.rdn)
92
93 log.info("Check employeenumber encryption is enabled")
94 assert "employeeNumber" in enc_attrs_cns
95
96 log.info("Check telephoneNumber encryption is enabled")
97 assert "telephoneNumber" in enc_attrs_cns
98
99 log.info("Check that encrypted attribute is present for user i.e. telephoneNumber")
100 assert enable_user_attr_encryption.present('telephoneNumber')
101
102
103 def test_export_import_ciphertext(topo, enable_user_attr_encryption):
104 """Configure attribute encryption, store some data, check that we can export the ciphertext
105
106 :id: b433e215-2926-48a5-818f-c21abc40fc2d
107 :setup: Standalone instance
108 Enable AES encryption config on employeenumber
109 Enable 3DES encryption config on telephoneNumber
110 Add a test user with encrypted attributes
111 :steps:
112 1. Export data as ciphertext
113 2. Check that the attribute is present in the exported file
114 3. Check that the encrypted value of attribute is not present in the exported file
115 4. Delete the test user entry with encrypted data
116 5. Import the previously exported data as ciphertext
117 6. Check attribute telephoneNumber should be imported
118 :expectedresults:
119 1. This should be successful
120 2. This should be successful
121 3. This should be successful
122 4. This should be successful
123 5. This should be successful
124 6. This should be successful
125 """
126
127 log.info("Export data as ciphertext")
128 export_ldif = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_ciphertext.ldif")
129
130 # Offline export
131 topo.standalone.stop()
132 if not topo.standalone.db2ldif(bename=DEFAULT_BENAME, suffixes=(DEFAULT_SUFFIX,),
133 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_ldif):
134 log.fatal('Failed to run offline db2ldif')
135 assert False
136 topo.standalone.start()
137
138 log.info("Check that the attribute is present in the exported file")
139 log.info("Check that the encrypted value of attribute is not present in the exported file")
140 with open(export_ldif, 'r') as ldif_file:
141 ldif = ldif_file.read()
142 assert 'telephoneNumber' in ldif
143 assert 'telephoneNumber: 1234567890' not in ldif
144
145 log.info("Delete the test user entry with encrypted data")
146 enable_user_attr_encryption.delete()
147
148 log.info("Import data as ciphertext, which was exported previously")
149 import_ldif = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_ciphertext.ldif")
150
151 # Offline export
152 topo.standalone.stop()
153 if not topo.standalone.ldif2db(bename=DEFAULT_BENAME, suffixes=(DEFAULT_SUFFIX,),
154 excludeSuffixes=None, encrypt=False, import_file=import_ldif):
155 log.fatal('Failed to run offline ldif2db')
156 assert False
157 topo.standalone.start()
158
159 log.info("Check that the data with encrypted attribute is imported properly")
160 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
161 user = users.get('testuser')
162 assert user.present("telephoneNumber")
163
164
165 def test_export_import_plaintext(topo, enable_user_attr_encryption):
166 """Configure attribute encryption, store some data, check that we can export the plain text
167
168 :id: b171e215-0456-48a5-245f-c21abc40fc2d
169 :setup: Standalone instance
170 Enable AES encryption config on employeenumber
171 Enable 3DES encryption config on telephoneNumber
172 Add a test user with encrypted attributes
173 :steps:
174 1. Export data as plain text
175 2. Check that the attribute is present in the exported file
176 3. Check that the encrypted value of attribute is also present in the exported file
177 4. Delete the test user entry with encrypted data
178 5. Import data as plaintext
179 6. Check attribute value of telephoneNumber
180 :expectedresults:
181 1. This should be successful
182 2. This should be successful
183 3. This should be successful
184 4. This should be successful
185 5. This should be successful
186 6. This should be successful
187 """
188
189 log.info("Export data as plain text")
190 export_ldif = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_plaintext.ldif")
191
192 # Offline export
193 topo.standalone.stop()
194 if not topo.standalone.db2ldif(bename=DEFAULT_BENAME, suffixes=(DEFAULT_SUFFIX,),
195 excludeSuffixes=None, encrypt=True, repl_data=None, outputfile=export_ldif):
196 log.fatal('Failed to run offline db2ldif')
197 assert False
198 topo.standalone.start()
199
200 log.info("Check that the attribute is present in the exported file")
201 log.info("Check that the plain text value of the encrypted attribute is present in the exported file")
202 with open(export_ldif, 'r') as ldif_file:
203 assert 'telephoneNumber: 1234567890' in ldif_file.read()
204
205 log.info("Delete the test user entry with encrypted data")
206 enable_user_attr_encryption.delete()
207
208 log.info("Import data as plain text, which was exported previously")
209 import_ldif = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_plaintext.ldif")
210
211 # Offline export
212 topo.standalone.stop()
213 if not topo.standalone.ldif2db(bename=DEFAULT_BENAME, suffixes=(DEFAULT_SUFFIX,),
214 excludeSuffixes=None, encrypt=True, import_file=import_ldif):
215 log.fatal('Failed to run offline ldif2db')
216 assert False
217 topo.standalone.start()
218
219 log.info("Check that the attribute is imported properly")
220 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
221 user = users.get('testuser')
222 assert user.present("telephoneNumber")
223
224
225 def test_attr_encryption_unindexed(topo, enable_user_attr_encryption):
226 """Configure attribute encryption for an un-indexed attribute, check that we can export encrypted data
227
228 :id: d3ef38e1-bb5a-44d8-a3a4-4a25a57e3454
229 :setup: Standalone instance
230 Enable AES encryption config on employeenumber
231 Enable 3DES encryption config on telephoneNumber
232 Add a test user with encrypted attributes
233 :steps:
234 1. Export data as cipher text
235 2. Check that the unindexed attribute employeenumber is present in exported ldif file
236 3. Check that the unindexed attribute employeenumber value is not present in exported ldif file
237 :expectedresults:
238 1. This should be successful
239 2. This should be successful
240 3. This should be successful
241 """
242 log.info("Export data as cipher text")
243 export_ldif = os.path.join(topo.standalone.ds_paths.ldif_dir, "emp_num_ciphertext.ldif")
244
245 # Offline export
246 topo.standalone.stop()
247 if not topo.standalone.db2ldif(bename=DEFAULT_BENAME, suffixes=(DEFAULT_SUFFIX,),
248 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_ldif):
249 log.fatal('Failed to run offline db2ldif')
250 assert False
251 topo.standalone.start()
252
253 log.info("Check that the attribute is present in the exported file")
254 log.info("Check that the encrypted value of attribute is not present in the exported file")
255 with open(export_ldif, 'r') as ldif_file:
256 ldif = ldif_file.read()
257 assert 'employeeNumber' in ldif
258 assert 'employeeNumber: 1000' not in ldif
259
260
261 def test_attr_encryption_multiple_backends(topo, enable_user_attr_encryption):
262 """Tests Configuration of attribute encryption for multiple backends
263 Where both the backends have attribute encryption
264
265 :id: 9ece3e6c-96b7-4dd5-b092-d76dda23472d
266 :setup: Standalone instance
267 SSL Enabled
268 :steps:
269 1. Add two test backends
270 2. Configure attribute encryption for telephoneNumber in one test backend
271 3. Configure attribute encryption for employeenumber in another test backend
272 4. Add a test user in both backends with encrypted attributes
273 5. Export data as ciphertext from both backends
274 6. Check that telephoneNumber is encrypted in the ldif file of db1
275 7. Check that employeeNumber is encrypted in the ldif file of db2
276 8. Delete both test backends
277 :expectedresults:
278 1. This should be successful
279 2. This should be successful
280 3. This should be successful
281 4. This should be successful
282 5. This should be successful
283 6. This should be successful
284 7. This should be successful
285 8. This should be successful
286 """
287 log.info("Add two test backends")
288 test_suffix1 = 'dc=test1,dc=com'
289 test_db1 = 'test_db1'
290 test_suffix2 = 'dc=test2,dc=com'
291 test_db2 = 'test_db2'
292
293 # Create backends
294 backends = Backends(topo.standalone)
295 backend = backends.list()[0]
296 test_backend1 = backends.create(properties={'cn': test_db1,
297 'nsslapd-suffix': test_suffix1})
298 test_backend2 = backends.create(properties={'cn': test_db2,
299 'nsslapd-suffix': test_suffix2})
300
301 # Create the top of the tree
302 suffix1 = Domain(topo.standalone, test_suffix1)
303 test1 = suffix1.create(properties={'dc': 'test1'})
304 suffix2 = Domain(topo.standalone, test_suffix2)
305 test2 = suffix2.create(properties={'dc': 'test2'})
306
307 log.info("Enables attribute encryption for telephoneNumber in test_backend1")
308 backend1_encrypt_attrs = EncryptedAttrs(topo.standalone, basedn='cn=encrypted attributes,{}'.format(test_backend1.dn))
309 b1_encrypt = backend1_encrypt_attrs.create(properties={'cn': 'telephoneNumber',
310 'nsEncryptionAlgorithm': 'AES'})
311
312 log.info("Enables attribute encryption for employeeNumber in test_backend2")
313 backend2_encrypt_attrs = EncryptedAttrs(topo.standalone, basedn='cn=encrypted attributes,{}'.format(test_backend2.dn))
314 b2_encrypt = backend2_encrypt_attrs.create(properties={'cn': 'employeeNumber',
315 'nsEncryptionAlgorithm': 'AES'})
316
317 log.info("Add a test user with encrypted attributes in both backends")
318 users = UserAccounts(topo.standalone, test1.dn, None)
319 test_user = users.create(properties=TEST_USER_PROPERTIES)
320 test_user.replace('telephoneNumber', '1234567890')
321
322 users = UserAccounts(topo.standalone, test2.dn, None)
323 test_user = users.create(properties=TEST_USER_PROPERTIES)
324 test_user.replace('employeeNumber', '1000')
325
326 log.info("Export data as ciphertext from both backends")
327 export_db1 = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_db1.ldif")
328 export_db2 = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_db2.ldif")
329
330 # Offline export
331 topo.standalone.stop()
332 if not topo.standalone.db2ldif(bename=test_db1, suffixes=(test_suffix1,),
333 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_db1):
334 log.fatal('Failed to run offline db2ldif')
335 assert False
336
337 if not topo.standalone.db2ldif(bename=test_db2, suffixes=(test_suffix2,),
338 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_db2):
339 log.fatal('Failed to run offline db2ldif')
340 assert False
341 topo.standalone.start()
342
343 log.info("Check that the attribute is present in the exported file in db1")
344 log.info("Check that the encrypted value of attribute is not present in the exported file in db1")
345 with open(export_db1, 'r') as ldif_file:
346 ldif = ldif_file.read()
347 assert 'telephoneNumber' in ldif
348 assert 'telephoneNumber: 1234567890' not in ldif
349
350 log.info("Check that the attribute is present in the exported file in db2")
351 log.info("Check that the encrypted value of attribute is not present in the exported file in db2")
352 with open(export_db2, 'r') as ldif_file:
353 ldif = ldif_file.read()
354 assert 'employeeNumber' in ldif
355 assert 'employeeNumber: 1000' not in ldif
356
357 log.info("Delete test backends")
358 test_backend1.delete()
359 test_backend2.delete()
360
361
362 def test_attr_encryption_backends(topo, enable_user_attr_encryption):
363 """Tests Configuration of attribute encryption for single backend
364 where more backends are present
365
366 :id: f3ef40e1-17d6-44d8-a3a4-4a25a57e9064
367 :setup: Standalone instance
368 SSL Enabled
369 :steps:
370 1. Add two test backends
371 2. Configure attribute encryption for telephoneNumber in one test backend
372 3. Add a test user in both backends with telephoneNumber
373 4. Export ldif from both test backends
374 5. Check that telephoneNumber is encrypted in the ldif file of db1
375 6. Check that telephoneNumber is not encrypted in the ldif file of db2
376 7. Delete both test backends
377 :expectedresults:
378 1. This should be successful
379 2. This should be successful
380 3. This should be successful
381 4. This should be successful
382 5. This should be successful
383 6. This should be successful
384 7. This should be successful
385 """
386 log.info("Add two test backends")
387 test_suffix1 = 'dc=test1,dc=com'
388 test_db1 = 'test_db1'
389 test_suffix2 = 'dc=test2,dc=com'
390 test_db2 = 'test_db2'
391
392 # Create backends
393 backends = Backends(topo.standalone)
394 test_backend1 = backends.create(properties={'cn': test_db1,
395 'nsslapd-suffix': test_suffix1})
396 test_backend2 = backends.create(properties={'cn': test_db2,
397 'nsslapd-suffix': test_suffix2})
398
399 # Create the top of the tree
400 suffix1 = Domain(topo.standalone, test_suffix1)
401 test1 = suffix1.create(properties={'dc': 'test1'})
402 suffix2 = Domain(topo.standalone, test_suffix2)
403 test2 = suffix2.create(properties={'dc': 'test2'})
404
405 log.info("Enables attribute encryption for telephoneNumber in test_backend1")
406 backend1_encrypt_attrs = EncryptedAttrs(topo.standalone, basedn='cn=encrypted attributes,{}'.format(test_backend1.dn))
407 b1_encrypt = backend1_encrypt_attrs.create(properties={'cn': 'telephoneNumber',
408 'nsEncryptionAlgorithm': 'AES'})
409
410 log.info("Add a test user with telephoneNumber in both backends")
411 users = UserAccounts(topo.standalone, test1.dn, None)
412 test_user = users.create(properties=TEST_USER_PROPERTIES)
413 test_user.replace('telephoneNumber', '1234567890')
414
415 users = UserAccounts(topo.standalone, test2.dn, None)
416 test_user = users.create(properties=TEST_USER_PROPERTIES)
417 test_user.replace('telephoneNumber', '1234567890')
418
419 log.info("Export data as ciphertext from both backends")
420 export_db1 = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_db1.ldif")
421 export_db2 = os.path.join(topo.standalone.ds_paths.ldif_dir, "export_db2.ldif")
422
423 # Offline export
424 topo.standalone.stop()
425 if not topo.standalone.db2ldif(bename=test_db1, suffixes=(test_suffix1,),
426 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_db1):
427 log.fatal('Failed to run offline db2ldif')
428 assert False
429
430 if not topo.standalone.db2ldif(bename=test_db2, suffixes=(test_suffix2,),
431 excludeSuffixes=None, encrypt=False, repl_data=None, outputfile=export_db2):
432 log.fatal('Failed to run offline db2ldif')
433 assert False
434 topo.standalone.start()
435
436 log.info("Check that the attribute is present in the exported file in db1")
437 log.info("Check that the encrypted value of attribute is not present in the exported file in db1")
438 with open(export_db1, 'r') as ldif_file:
439 ldif = ldif_file.read()
440 assert 'telephoneNumber' in ldif
441 assert 'telephoneNumber: 1234567890' not in ldif
442
443 log.info("Check that the attribute is present in the exported file in db2")
444 log.info("Check that the value of attribute is also present in the exported file in db2")
445 with open(export_db2, 'r') as ldif_file:
446 ldif = ldif_file.read()
447 assert 'telephoneNumber' in ldif
448 assert 'telephoneNumber: 1234567890' in ldif
449
450 log.info("Delete test backends")
451 test_backend1.delete()
452 test_backend2.delete()
453
454
455 if __name__ == '__main__':
456 # Run isolated
457 # -s for DEBUG mode
458 CURRENT_FILE = os.path.realpath(__file__)
459 pytest.main("-s %s" % CURRENT_FILE)