-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Active_Directory_Query_test.py
830 lines (679 loc) · 31.5 KB
/
Active_Directory_Query_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
import demistomock as demisto
from Active_Directory_Query import main, group_dn
import socket
import ssl
from threading import Thread
import time
import os
import pytest
import json
from IAMApiModule import *
from unittest.mock import patch
BASE_TEST_PARAMS = {
'server_ip': '127.0.0.1',
'secure_connection': 'None',
'page_size': '500',
'credentials': {'identifier': 'bad', 'password': 'bad'}
}
RETURN_ERROR_TARGET = 'Active_Directory_Query.return_error'
def test_bad_host_no_ssl(mocker):
"""
Given:
- Demisto.params() with an invalid server (host), and insecure connection type (None).
When:
- Running the 'main()' function.
Then:
- Verify that the expected error message was raised.
"""
params = BASE_TEST_PARAMS
params['server_ip'] = '127.0.0.'
mocker.patch.object(demisto, 'params',
return_value=BASE_TEST_PARAMS)
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
# validate our mock of params
assert demisto.params().get('server_ip') == '127.0.0.'
main()
assert return_error_mock.call_count == 1
# call_args last call with a tuple of args list and kwargs
err_msg = return_error_mock.call_args[0][0]
assert len(err_msg) < 100
assert 'Failed to access' in err_msg
assert 'invalid server address' in err_msg
@pytest.mark.filterwarnings("ignore::ResourceWarning")
def test_bad_ssl(mocker):
"""
Given:
- Demisto.params() with an ssl connection type (SSL) and a server (host) that will cause
an SSL socket error.
When:
- Running the 'main()' function.
Then:
- Verify that the expected error message was raised.
"""
params = BASE_TEST_PARAMS.copy()
params['server_ip'] = '185.199.108.153' # disable-secrets-detection
params['secure_connection'] = 'SSL'
params['port'] = 443
mocker.patch.object(demisto, 'params',
return_value=params)
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
mocker.patch.object(demisto, "info")
# validate our mock of params
assert demisto.params().get('secure_connection') == 'SSL'
main()
assert return_error_mock.call_count == 1
# call_args last call with a tuple of args list and kwargs
err_msg = return_error_mock.call_args[0][0]
assert 'Failed to access' in err_msg
assert 'Try using: "Trust any certificate" option.' in err_msg
def ssl_bad_socket_server(port):
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
# cert and keyfile generated with
# openssl req -x509 -nodes -days 3000 -newkey rsa:2048 -keyout key.pem -out cert.pem
try:
context.load_cert_chain('cert.pem', 'key.pem')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind(('127.0.0.1', port))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
try:
conn, addr = ssock.accept()
except ssl.SSLError as err:
if 'TLSV1_ALERT_UNKNOWN_CA' in str(err):
# all is ok. client refused our cert
return
raise
conn.recv(32)
msg = b'THIS IS A TEST SERVER WHICH IGNORES PROTOCOL\n\n'
for _x in range(10):
msg += msg
conn.send(msg)
conn.shutdown(socket.SHUT_RDWR)
conn.close()
except Exception as ex:
pytest.fail(f"Failed starting ssl_bad_socket_server: {ex}")
raise
@pytest.mark.filterwarnings("ignore::ResourceWarning")
def test_faulty_server(mocker):
port = 9638
t = Thread(target=ssl_bad_socket_server, args=(port,))
t.start()
time.sleep(1) # wait for socket server to startup
params = BASE_TEST_PARAMS.copy()
params['server_ip'] = '127.0.0.1' # disable-secrets-detection
params['secure_connection'] = 'SSL'
params['unsecure'] = True
params['port'] = port
mocker.patch.object(demisto, 'params',
return_value=params)
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
# validate our mock of params
assert demisto.params().get('secure_connection') == 'SSL'
main()
t.join(5)
assert return_error_mock.call_count == 1
# call_args last call with a tuple of args list and kwargs
err_msg = return_error_mock.call_args[0][0]
assert len(err_msg) < 125
assert 'Failed to access' in err_msg
assert 'Try using: "Trust any certificate" option.' not in err_msg
def test_ssl_custom_cert(mocker, request):
ENV_KEY = 'SSL_CERT_FILE'
os.environ[ENV_KEY] = 'cert.pem'
def cleanup():
os.environ.pop(ENV_KEY)
request.addfinalizer(cleanup)
port = 9637
t = Thread(target=ssl_bad_socket_server, args=(port,))
t.start()
time.sleep(1) # wait for socket server to startup
params = BASE_TEST_PARAMS.copy()
params['server_ip'] = '127.0.0.1' # disable-secrets-detection
params['secure_connection'] = 'SSL'
params['port'] = port
mocker.patch.object(demisto, 'params',
return_value=params)
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
# validate our mock of params
assert demisto.params().get('secure_connection') == 'SSL'
main()
t.join(5)
assert return_error_mock.call_count == 1
# call_args last call with a tuple of args list and kwargs
err_msg = return_error_mock.call_args[0][0]
assert len(err_msg) < 100
assert 'Failed to access' in err_msg
def test_endpoint_entry():
"""
Given:
Custom attributes to filter the computer object entry.
When:
The function filters the computer object according to the custom attributes.
Then:
The function will return all the computer object entry because custom attributes contain '*'.
"""
from Active_Directory_Query import endpoint_entry
custom_attributes_with_asterisk = endpoint_entry({'dn': 'dn', 'name': 'name', 'memberOf': 'memberOf'}, ['*'])
assert custom_attributes_with_asterisk == {'Groups': 'memberOf', 'Hostname': 'name', 'ID': 'dn', 'Type': 'AD'}
def get_outputs_from_user_profile(user_profile):
entry_context = user_profile.to_entry()
outputs = entry_context.get('Contents')
return outputs
def mock_demisto_map_object(object, mapper_name, incident_type):
email = object.get('email')
email_prefix = email.split('@')[0]
return {
'cn': email_prefix,
'mail': email,
'sAMAccountName': email_prefix,
'userPrincipalName': email_prefix,
"ou": "OU=Americas,OU=Demisto"
}
def test_get_iam_user_profile(mocker):
from Active_Directory_Query import get_iam_user_profile
mocker.patch.object(demisto, 'mapObject', side_effect=mock_demisto_map_object)
user_profile = {"email": "test2@paloaltonetworks.com", "username": "test",
"locationregion": "Americas",
"olduserdata": {"email": "test@paloaltonetworks.com", "username": "test",
"locationregion": "Americas"}}
_, ad_user, sam_account_name = get_iam_user_profile(user_profile, 'mock_mapper_out')
assert sam_account_name == 'test'
assert ad_user
def test_update_user_iam__username_change(mocker):
"""
Given:
A valid user profile with valid mapping
When:
Running the `create_user_iam` command
Then:
The user was created successfully in AD.
"""
import Active_Directory_Query
add_args, add_kwargs = [], {}
class ConnectionMocker:
entries = []
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': '<cookie>'}}}}
def search(self, *args, **kwargs):
return
def add(self, *args, **kwargs):
nonlocal add_args, add_kwargs
add_args, add_kwargs = args, kwargs
return True
def modify(self, *args, **kwargs):
return True
def modify_dn(self, *args, **kwargs):
return True
Active_Directory_Query.connection = ConnectionMocker()
args = {"user-profile": json.dumps({"email": "test2@paloaltonetworks.com", "username": "test",
"locationregion": "Americas",
"olduserdata": {"email": "test@paloaltonetworks.com", "username": "test",
"locationregion": "Americas"}})}
mocker.patch.object(demisto, 'mapObject', side_effect=mock_demisto_map_object)
mocker.patch('Active_Directory_Query.check_if_user_exists_by_attribute', return_value=True)
mocker.patch('Active_Directory_Query.get_user_activity_by_samaccountname', return_value=True)
mocker.patch('Active_Directory_Query.user_dn', return_value='mock_dn')
user_profile = Active_Directory_Query.update_user_iam(
default_base_dn='mock_base_dn',
args=args,
create_if_not_exists=False,
mapper_out='mock_mapper_out',
disabled_users_group_cn='mock_disabled_users_group_cn'
)
outputs = get_outputs_from_user_profile(user_profile)
assert outputs.get('action') == IAMActions.UPDATE_USER
assert outputs.get('success') is True
assert outputs.get('email') == 'test2@paloaltonetworks.com'
assert outputs.get('username') == 'test2'
def test_create_user_iam(mocker):
"""
Given:
A valid user profile with valid mapping
When:
Running the `create_user_iam` command
Then:
The user was created successfully in AD.
"""
import Active_Directory_Query
add_args, add_kwargs = [], {}
class ConnectionMocker:
entries = []
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': '<cookie>'}}}}
def search(self, *args, **kwargs):
return
def add(self, *args, **kwargs):
nonlocal add_args, add_kwargs
add_args, add_kwargs = args, kwargs
return True
Active_Directory_Query.connection = ConnectionMocker()
args = {"user-profile": json.dumps({"email": "test@paloaltonetworks.com", "username": "test",
"locationregion": "Americas"})}
mocker.patch('Active_Directory_Query.check_if_user_exists_by_attribute', return_value=False)
mocker.patch.object(IAMUserProfile, 'map_object', return_value={'cn': 'test', 'mail': 'test@paloaltonetworks.com',
'sAMAccountName': 'test',
'userPrincipalName': 'test',
"ou": "OU=Americas,OU=Demisto"})
user_profile = Active_Directory_Query.create_user_iam('', args, 'mapper_out', '')
outputs = get_outputs_from_user_profile(user_profile)
assert outputs.get('action') == IAMActions.CREATE_USER
assert outputs.get('success') is True
assert outputs.get('active') is False
assert outputs.get('email') == 'test@paloaltonetworks.com'
def test_unseccsseful_create_user_iam_missing_ou(mocker):
"""
Given:
A valid user profile with missing ou in the mapping
When:
Running the `create_user_iam` command
Then:
- The user was not created in AD.
- An error message was returned.
"""
import Active_Directory_Query
add_args, add_kwargs = [], {}
class ConnectionMocker:
entries = []
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': '<cookie>'}}}}
def search(self, *args, **kwargs):
return
def add(self, *args, **kwargs):
nonlocal add_args, add_kwargs
add_args, add_kwargs = args, kwargs
return True
Active_Directory_Query.connection = ConnectionMocker()
args = {"user-profile": json.dumps({"email": "test@paloaltonetworks.com", "username": "test",
"locationregion": "Americas"})}
mocker.patch('Active_Directory_Query.check_if_user_exists_by_attribute', return_value=False)
mocker.patch.object(IAMUserProfile, 'map_object', return_value={'cn': 'test', 'mail': 'test@paloaltonetworks.com',
'sAMAccountName': 'test',
'userPrincipalName': 'test'})
user_profile = Active_Directory_Query.create_user_iam('', args, 'mapper_out', '')
outputs = get_outputs_from_user_profile(user_profile)
assert outputs.get('action') == IAMActions.CREATE_USER
assert outputs.get('success') is False
assert outputs.get('email') == 'test@paloaltonetworks.com'
assert 'User must have an Organizational Unit (OU)' in outputs.get('errorMessage')
def test_unseccsseful_create_user_iam_missing_samaccountname(mocker):
"""
Given:
A valid user profile with missing samaccountname in the mapping
When:
Running the `create_user_iam` command
Then:
- The user was not created in AD.
- An error message was returned.
"""
import Active_Directory_Query
add_args, add_kwargs = [], {}
class ConnectionMocker:
entries = []
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': '<cookie>'}}}}
def search(self, *args, **kwargs):
return
def add(self, *args, **kwargs):
nonlocal add_args, add_kwargs
add_args, add_kwargs = args, kwargs
return True
Active_Directory_Query.connection = ConnectionMocker()
args = {"user-profile": json.dumps({"email": "test@paloaltonetworks.com", "username": "test",
"locationregion": "Americas"})}
mocker.patch('Active_Directory_Query.check_if_user_exists_by_attribute', return_value=False)
mocker.patch.object(IAMUserProfile, 'map_object', return_value={'cn': 'test', 'mail': 'test@paloaltonetworks.com',
"ou": "OU=Americas,OU=Demisto",
'userPrincipalName': 'test'})
user_profile = Active_Directory_Query.create_user_iam('', args, 'mapper_out', '')
outputs = get_outputs_from_user_profile(user_profile)
assert outputs.get('action') == IAMActions.CREATE_USER
assert outputs.get('success') is False
assert outputs.get('email') == 'test@paloaltonetworks.com'
assert 'User must have a sAMAccountName' in outputs.get('errorMessage')
def test_group_entry_no_custom_attributes():
"""
Given:
Custom attributes to filter the group object entry.
When:
The function filters the group object according to the custom attributes.
Then:
The function will return all the group object entry because custom attributes contain '*'.
"""
from Active_Directory_Query import group_entry
custom_attributes_with_asterisk = group_entry({'dn': 'dn', 'name': 'name', 'memberOf': 'memberOf'}, ['*'])
assert custom_attributes_with_asterisk == {'Groups': 'memberOf', 'ID': 'dn', 'Name': 'name', 'Type': 'AD'}
def test_group_entry():
"""
Given:
Custom attributes to filter the group object entry.
When:
The function filters the group object according to the custom attributes.
Then:
The function will return all the group object entry because custom attributes contain '*'.
"""
from Active_Directory_Query import group_entry
custom_attributes_with_asterisk = group_entry({'dn': 'dn', 'name': 'name', 'memberOf': 'memberOf',
'displayName': 'display name'}, ['displayName'])
assert custom_attributes_with_asterisk == {'Groups': 'memberOf', 'ID': 'dn', 'Name': 'name', 'Type': 'AD',
'displayName': 'display name'}
def test_search_group_members(mocker):
"""
sanity test for search_group_members method
"""
import Active_Directory_Query
class EntryMocker:
def entry_to_json(self):
return '{"dn": "dn","attributes": {"memberOf": ["memberOf"], "name": ["name"]}}'
class ConnectionMocker:
entries = [EntryMocker()]
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': b'<cookie>'}}}}
def search(self, *args, **kwargs):
time.sleep(1)
expected_entry = {
'ActiveDirectory.Groups(obj.dn ==dn)': {'dn': 'dn', 'members': [{'dn': 'dn', 'category': 'group'}]},
'ActiveDirectory.Groups(obj.dn == val.dn)': [{'dn': 'dn', 'memberOf': ['memberOf'], 'name': ['name']}],
'Group': [{'Type': 'AD', 'ID': 'dn', 'Name': ['name'], 'Groups': ['memberOf']}],
'ActiveDirectory(true)': {"GroupsPageCookie": base64.b64encode(b'<cookie>').decode('utf-8')}}
expected_results = {'ContentsFormat': 'json', 'Type': 1,
'Contents': [{'dn': 'dn', 'attributes': {'memberOf': ['memberOf'], 'name': ['name']}}],
'ReadableContentsFormat': 'markdown',
'HumanReadable': '### Active Directory - Get Group Members\n|'
'dn|memberOf|name|\n|---|---|---|\n| dn | memberOf | name |\n',
'EntryContext': expected_entry}
expected_results = f'demisto results: {json.dumps(expected_results, indent=4, sort_keys=True)}'
mocker.patch.object(demisto, 'args',
return_value={'member-type': 'group', 'group-dn': 'dn', 'time_limit': '1'})
Active_Directory_Query.connection = ConnectionMocker()
with patch('logging.Logger.info') as mock:
Active_Directory_Query.search_group_members('dc', 1)
mock.assert_called_with(expected_results)
def test_group_dn_escape_characters():
"""
Given:
Group name with parentheses
When:
Running the function group_dn
Then:
The function search gets the group name after escape special characters.
"""
import Active_Directory_Query
class EntryMocker:
def entry_to_json(self):
return '{"dn": "dn","attributes": {"memberOf": ["memberOf"], "name": ["name"]}}'
class ConnectionMocker:
entries = [EntryMocker()]
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': '<cookie>'}}}}
Active_Directory_Query.connection = ConnectionMocker()
with patch('Active_Directory_Query.search', return_value=[EntryMocker()]) as mock:
group_dn('group(group)', '')
mock.assert_called_with('(&(objectClass=group)(cn=group\\28group\\29))', '')
def test_search__no_control_exist(mocker):
"""
Given:
No control key in the result
When:
Run any search query
Then:
The result return 'no entries' instead of throw exception
"""
import Active_Directory_Query
class ConnectionMocker:
entries = []
result = {}
def search(self, *args, **kwargs):
return
mocker.patch.object(demisto, 'results')
Active_Directory_Query.connection = ConnectionMocker()
Active_Directory_Query.search_users('dc=test,dc=test_1', page_size=20)
assert '**No entries.**' in demisto.results.call_args[0][0]['HumanReadable']
def test_search_attributes_to_exclude(mocker):
"""
Given:
attributes_to_exclude
When:
Run any search query
Then:
The given arguments where excluded from human_readable and context_data
"""
import Active_Directory_Query
class EntryMocker:
def entry_to_json(self):
return '{"dn": "dn"}'
class ConnectionMocker:
entries = [EntryMocker()]
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': b'<cookie>'}}}}
def search(self, *args, **kwargs):
time.sleep(1)
expected_results = {'ContentsFormat': 'json', 'Type': 1,
'Contents': [{'dn': 'dn'}],
'ReadableContentsFormat': 'markdown',
'HumanReadable': '### Active Directory - Get Users\n|dn|\n|---|\n| dn |\n',
'EntryContext': {'ActiveDirectory.Users(obj.dn == val.dn)': [{'dn': 'dn'}],
'Account(obj.ID == val.ID)':
[{'Type': 'AD', 'ID': 'dn', 'Email': None, 'Username': None,
'DisplayName': None, 'Managr': None, 'Manager': None, 'Groups': None}],
'ActiveDirectory(true)':
{"UsersPageCookie": base64.b64encode(b'<cookie>').decode('utf-8')}}}
expected_results = f'demisto results: {json.dumps(expected_results, indent=4, sort_keys=True)}'
mocker.patch.object(demisto, 'args',
return_value={'attributes-to-exclude': "memberOf,name,mail,displayName,"
"manager,sAMAccountName,userAccountControl",
'page-size': '1'})
Active_Directory_Query.connection = ConnectionMocker()
with patch('logging.Logger.info') as mock:
Active_Directory_Query.search_users('dc', 1)
mock.assert_called_with(expected_results)
def test_user_account_to_boolean_fields():
"""
Given:
a userAccountControl value
When:
parsing the userAccountControl fields
Then:
Only the relevant fields will be marked as true
"""
import Active_Directory_Query
fields = Active_Directory_Query.user_account_to_boolean_fields(0x50)
assert {k for k, v in fields.items() if v} == {'LOCKOUT', 'PASSWD_CANT_CHANGE'}
@pytest.mark.parametrize('flags', [512, 0, 544])
def test_restore_user(mocker, flags):
"""
Given:
A disabled user.
When:
Calling restore_user method.
Then:
Verify the existing flag is returned.
"""
from Active_Directory_Query import restore_user
re_val = {'flat': [{'userAccountControl': [flags]}]}
mocker.patch('Active_Directory_Query.search_with_paging', return_value=re_val)
mocker.patch.object(demisto, 'args')
assert restore_user('test_user', 0) == flags
def test_enable_user_with_restore_user_option(mocker):
"""
Given:
A disabled user.
When:
Calling enable_user method.
Then:
Verify the existing flag is returned with the disable bit off.
"""
from Active_Directory_Query import enable_user
disabled_account_with_properties = 546
enabled_account_with_properties = 544
mocker.patch('Active_Directory_Query.restore_user', return_value=disabled_account_with_properties)
mocker.patch('Active_Directory_Query.user_dn', return_value='test_dn')
modify_data = mocker.patch('Active_Directory_Query.modify_object')
mocker.patch.object(demisto, 'args')
enable_user('test_user', 0)
assert modify_data.call_args.args[1].get('userAccountControl')[0][1] == enabled_account_with_properties
def test_search_with_paging_bug(mocker):
"""
Given:
page size larger than 1.
When:
running get-group-members command.
Then:
time_limit results returned.
"""
import Active_Directory_Query
class EntryMocker:
def entry_to_json(self):
return '{"dn": "dn","attributes": {"memberOf": ["memberOf"], "name": ["name"]}}'
class ConnectionMocker:
entries = []
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': b'<cookie>'}}}}
def search(self, *args, **kwargs):
page_size = kwargs.get('paged_size')
if page_size:
self.entries = [EntryMocker() for i in range(page_size)]
time.sleep(1)
mocker.patch.object(demisto, 'results')
mocker.patch.object(demisto, 'args',
return_value={'member-type': 'group', 'group-dn': 'dn', 'time_limit': '3'})
Active_Directory_Query.connection = ConnectionMocker()
with patch('logging.Logger.info'):
Active_Directory_Query.search_group_members('dc', 1)
assert len(demisto.results.call_args[0][0]['Contents']) == 3
def test_password_not_expire_missing_username(mocker):
"""
Given:
A demisto args object with missing username and a valid value.
When:
running set_password_not_expire command.
Then:
Verify that a a missing username exception is raised.
"""
from Active_Directory_Query import set_password_not_expire
mocker.patch.object(demisto, 'args', return_value={'username': None, 'value': True})
default_base_dn = {}
with pytest.raises(Exception) as err:
set_password_not_expire(default_base_dn)
assert err.value.args[0] == 'Missing argument - You must specify a username (sAMAccountName).'
@pytest.mark.parametrize('connection_type, unsecure, expected_auto_bind_value', [
('Start TLS', True, 'TLS_BEFORE_BIND'),
('Start TLS', False, 'TLS_BEFORE_BIND'),
('TLS', False, 'TLS_BEFORE_BIND'),
('TLS', True, 'NO_TLS'),
('SSL', True, 'NO_TLS'),
('SSL', False, 'NO_TLS'),
('None', True, 'NO_TLS'),
('None', False, 'NO_TLS')
])
def test_get_auto_bind_value(connection_type, unsecure, expected_auto_bind_value):
"""
Given:
- A connection type:
1. Start TLS
2. TLS
3. SSL
4. None
When:
- Running the 'get_auto_bind_value()' function.
Then:
- Verify that the returned auto_bind value is as expected:
1. 'TLS_BEFORE_BIND' - which means that connection should upgrade it's secure level to TLS before
the bind itself (STARTTLS command is executed).
2. 'TLS_BEFORE_BIND' - for unsecure=False and 'NO_TLS' for unsecure=True
3. 'NO_TLS' - The connection is secured from the beginning,
thus STARTTLS command shouldn't be executed.
4. 'NO_TLS' - Connection is insecure (cleartext) and shouldn't be upgraded to TLS.
"""
from Active_Directory_Query import get_auto_bind_value
auto_bind_value = get_auto_bind_value(connection_type, unsecure)
assert auto_bind_value == expected_auto_bind_value
@pytest.mark.parametrize('ssl_version, expected_ssl_version', [
('TLS', 2), ('TLSv1', 3), ('TLSv1_1', 4), ('TLSv1_2', 5), ('TLS_CLIENT', 16), (None, None), ('None', None)
])
def test_get_ssl_version(ssl_version, expected_ssl_version):
"""
Given:
- An ssl protocol version:
1. TLS
2. TLSv1
3. TLSv1_1
4. TLSv1_2
5. TLS_CLIENT
6. None
7. 'None'
When:
- Running the 'get_ssl_version()' function.
Then:
- Verify that the returned ssl version value is as expected:
1. TLS - 2
2. TLSv1 - 3
3. TLSv1_1 - 4
4. TLSv1_2 - 5
5. TLS_CLIENT - 16
6. None - None
7. 'None' - None
"""
from Active_Directory_Query import get_ssl_version
ssl_version_value = get_ssl_version(ssl_version)
assert ssl_version_value == expected_ssl_version
def test_search_users_empty_userAccountControl(mocker):
"""
Given:
The 'userAccountControl' attribute was returned empty
When:
Run the 'ad-get-user' command
Then:
The result returns without raise IndexError: list index out of range
"""
import Active_Directory_Query
class EntryMocker:
def entry_to_json(self):
return '{"attributes": {"displayName": [], "mail": [], "manager": [], "memberOf": ["memberOf"], ' \
'"name": ["Guest"], "sAMAccountName": ["Guest"], "userAccountControl": []}, "dn": "test_dn"}'
class ConnectionMocker:
entries = [EntryMocker()]
result = {'controls': {'1.2.840.113556.1.4.319': {'value': {'cookie': b'<cookie>'}}}}
def search(self, *args, **kwargs):
time.sleep(1)
expected_results = {'ContentsFormat': 'json',
'Type': 1,
'Contents': [{'attributes': {'displayName': [], 'mail': [], 'manager': [],
'memberOf': ['memberOf'], 'name': ['Guest'],
'sAMAccountName': ['Guest'],
'userAccountControl': []}, 'dn': 'test_dn'}],
'ReadableContentsFormat': 'markdown',
'HumanReadable': '### Active Directory - Get Users\n|displayName|dn|mail|manager|memberOf|name'
'|sAMAccountName|userAccountControl|\n|---|---|---|---|---|---|---|---|\n| |'
' test_dn | | | memberOf | Guest | Guest | |\n',
'EntryContext': {'ActiveDirectory.Users(obj.dn == val.dn)': [{'dn': 'test_dn',
'displayName': [], 'mail': [],
'manager': [],
'memberOf': ['memberOf'],
'name': ['Guest'],
'sAMAccountName': ['Guest'],
'userAccountControl': []}],
'Account(obj.ID == val.ID)': [{'Type': 'AD', 'ID': 'test_dn', 'Email': [],
'Username': ['Guest'], 'DisplayName': [],
'Managr': [], 'Manager': [],
'Groups': ['memberOf']}],
'ActiveDirectory(true)':
{'UsersPageCookie': base64.b64encode(b'<cookie>').decode('utf-8')}}}
expected_results = f'demisto results: {json.dumps(expected_results, indent=4, sort_keys=True)}'
mocker.patch.object(demisto, 'args', return_value={'page-size': '1'})
Active_Directory_Query.connection = ConnectionMocker()
with patch('logging.Logger.info') as mock:
Active_Directory_Query.search_users('dc', 1)
mock.assert_called_with(expected_results)
def test_test_credentials_command(mocker):
"""
Given:
A demisto args object with username and password
When:
Run the 'ad-test-credentials' command
Then:
The result returns with successful connection
"""
import Active_Directory_Query
args = {'username': 'username_test_credentials', 'password': 'password_test_credentials'}
mocker.patch.object(demisto, 'args', return_value=args)
class MockConnection:
def unbind(self):
pass
def mock_create_connection(server, server_ip, username, password, ntlm_connection, auto_bind):
return MockConnection()
with patch("Active_Directory_Query.create_connection", side_effect=mock_create_connection), \
patch("Active_Directory_Query.Connection.unbind", side_effect=MockConnection.unbind):
command_results = Active_Directory_Query.test_credentials_command(BASE_TEST_PARAMS['server_ip'], ntlm_connection='true')
assert command_results.readable_output == 'Credential test with username username_test_credentials succeeded.'