This repository was archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTEST_security.py
1891 lines (1693 loc) · 109 KB
/
TEST_security.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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Service-Conformance-Check/blob/main/LICENSE.md
# File: rfs_check.py
# Description: Redfish service conformance check tool. This module contains implemented assertions for
# SUT.These assertions are based on operational checks on Redfish Service to verify that it conforms
# to the normative statements from the Redfish specification.
# See assertions in redfish-assertions.xlxs for assertions summary
import sys
import re
import rf_utility
# map python 2 vs 3 imports
if (sys.version_info < (3, 0)):
# Python 2
Python3 = False
from urlparse import urlparse
from StringIO import StringIO
from httplib import HTTPSConnection, HTTPConnection, HTTPResponse
import urllib
import urllib2
else:
# Python 3
Python3 = True
from urllib.parse import urlparse
from io import StringIO
from http.client import HTTPSConnection, HTTPConnection, HTTPResponse
from urllib.request import urlopen
import ssl
import re
import json
import argparse
import base64
import datetime
import types
import socket
import select
import os
from collections import OrderedDict
import time
# current spec followed for these assertions
REDFISH_SPEC_VERSION = "Version 1.0.2"
#####################################################################################################
# Name: CacheURI
# Description: Cache a few random URI's in order to speed up the tool
#####################################################################################################
cached_uri = None
cached_uri_no_member = None
def cacheURI(self):
global cached_uri
global cached_uri_no_member
cached_uri = self.uris
cached_uri_no_member = self.uris_no_members
###################################################################################################
# Name: Assertion_9_3_1(self, log) Authentication/Sessions
# Description:
# Service shall support both "Basic Authentication" (Assertion 9.3.1.4) and "Redfish Session Login
# Authentication" (This assertion)
# The response to the POST request to create a session includes: an X-Auth-Token header that
# contains a "session auth token" that the client can use an subsequent requests, and a "Location
# header that contains a link to the newly created session resource. The JSON response body that
# contains a full representation of the newly created session object? ~ i dont see it
###################################################################################################
def Assertion_9_3_1(self, log) :
log.AssertionID = '9.3.1'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
relative_uris = self.relative_uris
session_key = 'x-auth-token'
x_auth_token = None
session_location = None
#Create session
authorization = 'on'
rq_headers = self.request_headers()
session_uri = None
root_link_key = 'Sessions'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
session_uri = self.sut_toplevel_uris[root_link_key]['url']
else:
for rel_uris in self.relative_uris:
if rel_uris.endswith('Sessions'):
session_uri = self.relative_uris[rel_uris]
if session_uri:
json_payload, headers, status = self.http_cached_GET(session_uri, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif not (self.allowable_method('POST', headers)):
assertion_status = log.WARN
log.assertion_log('line', "~ the response headers for %s do not indicate support for POST" % session_uri)
else:
# request body to create session
rq_body = {'UserName': self.SUT_prop['LoginName'], 'Password': self.SUT_prop['Password']}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (session_uri, rq_body))
json_payload, headers, status = self.http_POST(session_uri, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif headers:
#session created
# get location from the response header
if 'location' in headers:
session_location = headers['location']
#get x-auth-token to request GETS using this session
session_key = 'x-auth-token'
if session_key not in headers:
assertion_status = log.FAIL
log.assertion_log('line',"Response header for POST on %s does not contain key: %s, which is required so that the client can use this session as authentication method for subsequent requests." %(session_uri, session_key))
elif not headers[session_key]:
assertion_status = log.FAIL
log.assertion_log('line', "~ Expected header %s to have a value with session auth token, which is required so that the client can use this session as authentication method for subsequent requests ~ Not found" % (session_key))
else:
x_auth_token = headers[session_key]
#try GETs on service root links with session key
rq_headers = self.request_headers()
#auth off and use session key
authorization = 'off'
rq_headers[session_key] = x_auth_token
for relative_uri in cached_uri:
json_payload, headers, status = self.http_cached_GET(relative_uris[relative_uri], rq_headers, authorization)
assertion_status_ = self.response_status_check(relative_uris[relative_uri], status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
#Terminate this session
if session_location:
authorization = 'on'
rq_headers = self.request_headers()
json_payload, headers, status = self.http_DELETE(session_location, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_location, status, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
log.assertion_log(assertion_status, None)
return (assertion_status)
#
## end Assertion 9.3.1
###################################################################################################
# Name: Assertion_9_3_1_1(self, log) Authentication/Sessions
# Description:
# The response to the POST request to create a session includes: an X-Auth-Token header that contains
# a "session auth token" that the client can use an subsequent requests
###################################################################################################
def Assertion_9_3_1_1(self, log) :
log.AssertionID = '9.3.1.1'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
#Create session
authorization = 'on'
rq_headers = self.request_headers()
session_location = None
session_uri = None
root_link_key = 'Sessions'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
session_uri = self.sut_toplevel_uris[root_link_key]['url']
else:
for rel_uris in self.relative_uris:
if rel_uris.endswith('Sessions'):
session_uri = self.relative_uris[rel_uris]
if session_uri:
json_payload, headers, status = self.http_cached_GET(session_uri, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif not (self.allowable_method('POST', headers)):
assertion_status = log.WARN
log.assertion_log('line', "~ the response headers for %s do not indicate support for POST" % session_uri)
else:
rq_body = {'UserName': self.SUT_prop['LoginName'], 'Password': self.SUT_prop['Password']}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (session_uri, rq_body))
json_payload, headers, status = self.http_POST(session_uri, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
#session created
# get location from the response header
if 'location' in headers:
session_location = headers['location']
# verify x-auth-token in header
session_key = 'x-auth-token'
if session_key not in headers:
assertion_status = log.FAIL
log.assertion_log('line', "~ Expected header %s to be in the response header of %s ~ Not found" % (session_key, session_uri))
elif not headers[session_key]:
assertion_status = log.FAIL
log.assertion_log('line', "~ Expected header %s to have a value with session auth token, which is required so that the client can use this session as authentication method for subsequent requests ~ Not found" % (session_key))
#Terminate this session
if session_location:
authorization = 'on'
rq_headers = self.request_headers()
json_payload, headers, status = self.http_DELETE(session_location, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_location, status, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
log.assertion_log(assertion_status, None)
return (assertion_status)
#
## end Assertion 9.3.1.1
###################################################################################################
# Name: Assertion_9_3_1_2(self, log) Authentication/Sessions
# Description:
# The response to the POST request to create a session includes: a "Location header that contains a
# link to the newly created session resource.
###################################################################################################
def Assertion_9_3_1_2(self, log):
log.AssertionID = '9.3.1.2'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
#Create session
authorization = 'on'
rq_headers = self.request_headers()
session_uri = None
root_link_key = 'Sessions'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
session_uri = self.sut_toplevel_uris[root_link_key]['url']
else:
for rel_uris in self.relative_uris:
if rel_uris.endswith('Sessions'):
session_uri = self.relative_uris[rel_uris]
if session_uri:
json_payload, headers, status = self.http_cached_GET(session_uri, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif not (self.allowable_method('POST', headers)):
assertion_status = log.WARN
log.assertion_log('line', "~ the response headers for %s do not indicate support for POST" % session_uri)
else:
rq_body = {'UserName': self.SUT_prop['LoginName'], 'Password': self.SUT_prop['Password']}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (session_uri, rq_body))
json_payload, headers, status = self.http_POST(session_uri, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif headers:
#session created
# verify location in the response header
location_key = 'location'
if location_key not in headers:
assertion_status = log.FAIL
log.assertion_log('line', "~ Expected header %s to be in the response header of POST %s ~ Not found" % (location_key, session_uri))
elif not headers['location']:
assertion_status = log.FAIL
log.assertion_log('line', "~ Expected header %s to have a value with a url pointing to a newly created session ~ Not found" % (location_key))
else:
session_location = headers[location_key]
authorization = 'on'
rq_headers = self.request_headers()
json_payload, headers, status = self.http_DELETE(session_location, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_location, status, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
log.assertion_log(assertion_status, None)
return (assertion_status)
#
## end Assertion 9.3.1.2
###################################################################################################
# Name: Assertion_9_3_1_3(self, log) Authentication/Sessions
# Description:
# The response to the POST request to create a session includes:
# The JSON response body that contains a full representation of the newly created session object
###################################################################################################
def Assertion_9_3_1_3(self, log):
log.AssertionID = '9.3.1.3'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
authorization = 'on'
rq_headers = self.request_headers()
# the session object keys are traced as per redfish specification. See section 9.2.4.3
session_response_keys = ['@odata.context', '@odata.id', '@odata.type', 'Id', 'Name', 'Description', 'UserName']
#Create session
authorization = 'on'
rq_headers = self.request_headers()
session_uri = None
root_link_key = 'Sessions'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
session_uri = self.sut_toplevel_uris[root_link_key]['url']
else:
for rel_uris in self.relative_uris:
if rel_uris.endswith('Sessions'):
session_uri = self.relative_uris[rel_uris]
if session_uri:
json_payload, headers, status = self.http_cached_GET(session_uri, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif not (self.allowable_method('POST', headers)):
assertion_status = log.WARN
log.assertion_log('line', "~ the response headers for %s do not indicate support for POST" % session_uri)
else:
rq_body = {'UserName': self.SUT_prop['LoginName'], 'Password': self.SUT_prop['Password']}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (session_uri, rq_body))
json_payload, headers, status = self.http_POST(session_uri, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif not (headers or json_payload):
assertion_status = log.WARN
else:
#session created
# get location from the response header
if 'location' in headers:
session_location = headers['location']
# verify JSON response body with full representation of newly created session object as per Redfish specification
if not any(key in json_payload.keys() for key in session_response_keys):
assertion_status = log.FAIL
try:
log.assertion_log('line', "~ The response body of newly created session: %s does not contain a full representation of the session object. Response body returned contains: %s" % (session_location, rf_utility.json_string(json_payload)))
except:
log.assertion_log('line', "~ The response body of newly created session does not contain a full representation of the session object. Response body returned contains: %s" % (rf_utility.json_string(json_payload)))
else:
for key in session_response_keys:
if key not in json_payload.keys():
try:
log.assertion_log('line', "~ The response body of newly created session: %s does not contain the key: %s. Requirement is that must contain full respresentation of a session object described in the Redfish specification" % (session_location, key))
except:
log.assertion_log('line', "~ The response body of newly created session does not contain the key: %s as required by the Redfish specification" % (key))
#Terminate this session
if session_location:
authorization = 'on'
rq_headers = self.request_headers()
json_payload, headers, status = self.http_DELETE(session_location, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_location, status, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
log.assertion_log(assertion_status, None)
return (assertion_status)
#
## end Assertion 9.3.1.3
###################################################################################################
# Name: Assertion_9_3_1_4(self, log) Authentication
# Description:
# Services shall not require a client to create a session when Basic Auth is used.
# Note did a test with session + basic auth, requests failed
###################################################################################################
def Assertion_9_3_1_4(self, log) :
log.AssertionID = '9.3.1.4'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
#auth on, no session
authorization = 'on'
rq_headers = self.request_headers()
relative_uris = self.relative_uris
authorization_key = 'Authorization'
for relative_uri in cached_uri:
json_payload, headers, status = self.http_cached_GET(relative_uris[relative_uri], rq_headers, authorization)
assertion_status_ = self.response_status_check(relative_uris[relative_uri], status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
log.assertion_log(assertion_status, None)
return (assertion_status)
#
## end Assertion 9.3.1.4
###################################################################################################
# Name: Assertion_9_3_2_1(self, log) HTTP Header Security
# Description:
# All write activities shall be authenticated, i.e. POST, except for The POST operation to the
# Sessions service/object needed for authentication
###################################################################################################
def Assertion_9_3_2_1(self, log) :
log.AssertionID = '9.3.2.1'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
unauth_payload = None
auth_payload = None
authorization = 'on'
log.assertion_log('line', "~note: this assertions sub-invokes Assertion 9.3.3.1")
rq_headers = self.request_headers()
session_uri = None
root_link_key = 'Sessions'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
session_uri = self.sut_toplevel_uris[root_link_key]['url']
else:
for rel_uris in self.relative_uris:
if rel_uris.endswith('Sessions'):
session_uri = self.relative_uris[rel_uris]
if session_uri:
json_payload, headers, status = self.http_cached_GET(session_uri, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif (self.allowable_method('POST', headers) != True):
assertion_status = log.FAIL
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for POST" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
rq_headers = self.request_headers()
rq_body = {'UserName': self.SUT_prop['LoginName'], 'Password': self.SUT_prop['Password']}
rq_headers['Content-Type'] = 'application/json'
'''1: Do a POST on session without auth, it should pass'''
authorization = 'off'
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s with session authorization' % (session_uri, rq_body))
json_payload, headers, status = self.http_POST(session_uri, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(session_uri, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif (self.allowable_method('DELETE', headers)):
try:
session_location = headers['location']
except:
assertion_status = log.WARN
log.assertion_log('line', "~ Expected Location in the headers of POST for %s ~ not found" %(session_uri))
log.assertion_log('line', rf_utility.json_string(headers))
else:
rq_headers = self.request_headers()
authorization = 'on'
#DELETE session here
json_payload_, headers_, status_ = self.http_DELETE(session_location, rq_headers, authorization)
assertion_status_ = self.response_status_check(session_location, status_, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
'''2: Now do a POST on an account without authorization, it should FAIL '''
rq_headers = self.request_headers()
authorization = 'on'
# get the collection of user accounts...
auth_payload = None
unauth_payload = None
if (assertion_status != log.FAIL):
rq_headers = self.request_headers()
# get the collection of user accounts...
user_name = 'testuser'
root_link_key = 'AccountService'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
json_payload, headers, status = self.http_cached_GET(self.sut_toplevel_uris[root_link_key]['url'], rq_headers, authorization)
assertion_status_ = self.response_status_check(self.sut_toplevel_uris[root_link_key]['url'], status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif not json_payload:
assertion_status = log.WARN
log.assertion_log('line', 'No response body returned for resource %s. This assertion for the resource could not be completed' % (self.sut_toplevel_uris[root_link_key]['url']))
else:
## get Accounts collection from payload
try :
key = 'Accounts'
acc_collection = (json_payload[key])['@odata.id']
except :
assertion_status = log.WARN
log.assertion_log('line', "~ \'Accounts\' not found in the payload from GET %s" % (self.sut_toplevel_uris[root_link_key]['url']))
else:
json_payload, headers, status = self.http_cached_GET(acc_collection, rq_headers, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
# check if intended method is an allowable method for resource
if (self.allowable_method('POST', headers) != True):
assertion_status = log.FAIL
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for POST" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
#check if user already exists, if it does perfcorm a delete to clean up
members = self.get_resource_members(acc_collection)
for json_payload, headers in members:
if json_payload['UserName'] == user_name:
log.assertion_log('TX_COMMENT', "~ note: the %s account pre-exists... deleting it now in prep for creation" % json_payload['UserName'])
# check if intended method is an allowable method for resource
if (self.allowable_method('DELETE', headers)):
json_payload_, headers_, status_ = self.http_DELETE(json_payload['@odata.id'], rq_headers, authorization)
assertion_status_ = self.response_status_check(json_payload['@odata.id'], status_, log, request_type='DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
break
else:
log.assertion_log('XL_COMMENT', "~ note: DELETE for %s : %s PASS. HTTP status %s:%s" % (user_name, json_payload['@odata.id'], status_, rf_utility.HTTP_status_string(status_)))
break
else:
assertion_status = log.FAIL
log.assertion_log('line', "~ The response headers for %s do not indicate support for DELETE" % json_payload['@odata.id'])
log.assertion_log('line', "~ Item already exists in %s and attempt to request DELETE failed, Try changing item configuration in the script" % json_payload['@odata.id'])
break
if (assertion_status == log.PASS) : # Ok to create the user now
rq_body = {'UserName' : 'testuser' , 'Password' : 'testpass' , 'RoleId' : 'Administrator' }
# turn auth off for POST, it should not pass
authorization = 'off'
rq_headers = self.request_headers()
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s without authorization' % (acc_collection, rq_body))
unauth_payload, headers, status = self.http_POST(acc_collection, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log, rf_utility.HTTP_UNAUTHORIZED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
#do POST with auth to get json_payload to compare if extended error has any privilages info
rq_headers = self.request_headers()
authorization = 'on'
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s with authorization' % (acc_collection, rq_body))
auth_payload, headers, status = self.http_POST(acc_collection, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
run_sub_assertion = False
if ((unauth_payload) or (auth_payload)):
run_sub_assertion = True
if (run_sub_assertion == False):
log.assertion_log('line', "~ note: Assertion failures have prevented sub-invocation of related assertions")
log.assertion_log(assertion_status, None)
return(assertion_status)
else:
# completion status for this assertion...
log.assertion_log(assertion_status, None)
#subinvoke assertion
Assertion_9_3_3_1(unauth_payload,auth_payload, log)
return (assertion_status)
#
## end Assertion 9.3.2.1
###################################################################################################
# Name: Assertion_9_3_3_1(unauth_payload,auth_payload, log) HTTP Header Security
# Description: POST
# Extended error messages shall NOT provide privileged info when authentication failures occur
###################################################################################################
def Assertion_9_3_3_1(unauth_payload,auth_payload, log) :
log.AssertionID = '9.3.3.1'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
# check to see privilegd info in unauth payload
assertion_status = checkPrivilegedinfo(unauth_payload,auth_payload, log)
log.assertion_log(assertion_status, None)
return (assertion_status)
#end Assertion 9.3.3.1
###################################################################################################
# Name: checkPrivilegedinfo (nauth_payload, auth_payload)
# Description:
# compare authorized and unauthorized response body, unauthorized payload should not provide
# privilaged information
# Assumption: any information that an authorized payload has for which authentication is required,
# is privilaged info
###################################################################################################
def checkPrivilegedinfo(unauth_load, auth_payload, log):
assertion_status = log.PASS
if (Python3 == True):
unauth_payload = b'unauth_load'
else:
unauth_payload = unauth_load
# check for any matches...
for key in auth_payload.keys():
if (Python3 == True):
bkey = b'key'
else:
bkey = key
if (bkey in unauth_payload):
if (unauth_payload[bkey] == auth_payload[bkey]):
assertion_status = log.FAIL
log.assertion_log('line', "~ Unatuhorized payload has Property \'%s\' : \'%s\' which maybe a privilaged information %s" % (key, unauth_payload[key], auth_payload[key]) )
# print the extended error for info in xl_comment TODO
#log.assertion_log('line', "~ Unatuhorized payload has Property \'%s\' : \'%s\' which maybe a privilaged information %s" % (key, check_payload[key], object_payload[key]) )
return (assertion_status)
###################################################################################################
# Name: Assertion_9_3_2_2(self, log) HTTP Header Security
# Description:
# All write activities shall be authenticated, i.e. PATCH/PUT
# TODO - should also test PATCH account with session key?
###################################################################################################
def Assertion_9_3_2_2(self, log) :
log.AssertionID = '9.3.2.2'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
authorization = 'on'
unauth_payload = ''
auth_payload = ''
rq_headers = self.request_headers()
account_url = ''
user_name = 'testuser'
root_link_key = 'AccountService'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
log.assertion_log('line', "~ note: this assertion sub-invokes Assertion 9.3.3.2")
json_payload, headers, status = self.http_cached_GET(self.sut_toplevel_uris[root_link_key]['url'], rq_headers, authorization)
assertion_status_ = self.response_status_check(self.sut_toplevel_uris[root_link_key]['url'], status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif not json_payload:
assertion_status= log.WARN
log.assertion_log('line', 'No response body returned for resource %s. This assertion for the resource could not be completed' % (self.sut_toplevel_uris[root_link_key]['url']))
else: ## get Accounts collection from payload
try :
key = 'Accounts'
acc_collection = (json_payload[key])['@odata.id']
except : # no user accounts?
assertion_status = log.WARN
log.assertion_log('line', "~ no accounts collection was returned from GET %s" % self.sut_toplevel_uris[root_link_key]['url'])
else:
deleted = False
user_exist = False
#create temp account then delete it
#check if user already exists, if it does perfcorm a delete to clean up
members = self.get_resource_members(acc_collection)
for json_payload, headers in members:
if json_payload['UserName'] == user_name:
user_exist = True
account_url = json_payload['@odata.id']
break
if user_exist == False:
json_payload, headers, status = self.http_cached_GET(acc_collection, rq_headers, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif (self.allowable_method('POST', headers) != True):
assertion_status = log.FAIL
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for POST" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
#create it
rq_body = {'UserName': 'testuser', 'Password': 'testpass', 'RoleId' : 'Administrator'}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (acc_collection, rq_body))
json_payload, headers, status = self.http_POST(acc_collection, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
log.assertion_log('line', "~ note: Item %s to test PUT/PATCH could not be created through script due to service errors stated, Try adding a user on the system through server management and reconfigure request body." % (user_name) )
else:
try:
account_url = headers['location']
except:
assertion_status = log.WARN
log.assertion_log('line', "~ note: Location header expected in response for POST %s ~ not found" % (user_name))
log.assertion_log('line', "~ note: Could not obtain url of the newly created object, cannot request a PATCH for %s in this assertion" % (user_name))
if (assertion_status == log.PASS or account_url != ''):
#got past either creating new or using existing user account
found = False
#get on account_url
json_payload, headers, status = self.http_cached_GET(account_url, rq_headers, authorization)
assertion_status_ = self.response_status_check(account_url, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
# check if required method is an allowable method
if (self.allowable_method('PATCH', headers) != True):
assertion_status = log.WARN
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for PATCH" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
etag = ''
patch_key = 'RoleId'
patch_value = 'Operator'
try:
etag = headers['etag']
except:
assertion_status = log.WARN
log.assertion_log('line', "~ note: Expected Etag in headers returned from %s ~ not found" %(json_payload['@odata.id']))
log.assertion_log('line', rf_utility.json_string(headers))
rq_body = {'UserName': 'testuser', 'Password': 'testpass' , 'RoleId' : patch_value}
#1: Turn auth = off and do a PATCH, it is not an expected behavior, hence it should FAIL
authorization = 'off' #turn auth off
rq_headers = self.request_headers()
unauth_payload, headers, status = self.http_PATCH(account_url, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(account_url, status, log, rf_utility.HTTP_UNAUTHORIZED, 'PATCH')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
#check if resource remain unchanged
if etag is not '':
rq_headers = self.request_headers()
rq_headers['If-None-Match'] = etag
authorization = 'on' #turn auth on
json_payload, headers, status = self.http_cached_GET(account_url, rq_headers, authorization)
assertion_status_ = self.response_status_check(account_url, status, log, rf_utility.HTTP_NOTMODIFIED)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
log.assertion_log('line', "~ PATCH %s : Resource might have updated unexpectedly" % (account_url) )
log.assertion_log('line', "~ Status expected %s, response status = %s" % (rf_utility.HTTP_NOTMODIFIED, status))
log.assertion_log('XL_COMMENT', ('Checked if resource is modified using If-None-Match header and etag' ))
else:
#2: Turn auth = on and do a PATCH, to get json_payload to compare if extended error has any privilages info
authorization = 'on'
rq_headers = self.request_headers()
auth_payload, headers, status = self.http_PATCH(account_url, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(account_url, status, log, request_type='PATCH')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
run_sub_assertion = False
if ((unauth_payload) or (auth_payload)):
run_sub_assertion = True
if (run_sub_assertion == False):
log.assertion_log('line', "~ note: Assertion failures have prevented sub-invocation of related assertions")
log.assertion_log(assertion_status, None)
return(assertion_status)
else:
# completion status for this assertion...
log.assertion_log(assertion_status, None)
#subinvoke assertion
Assertion_9_3_3_2(unauth_payload,auth_payload, log)
return(assertion_status)
#
## end Assertion 9.3.2.2
###################################################################################################
# Name: Assertion_9_3_3_2() HTTP Header Security
# Description: PATCH
# Extended error messages shall NOT provide privileged info when authentication failures occur
###################################################################################################
def Assertion_9_3_3_2(unauth_payload,auth_payload, log) :
log.AssertionID = '9.3.3.2'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
# check to see privielegd info in unauth payload
assertion_status = checkPrivilegedinfo(unauth_payload,auth_payload, log)
log.assertion_log(assertion_status, None)
return (assertion_status)
#end Assertion 9.3.3.2
###################################################################################################
# Name: Assertion_9_3_2_3(self, log) HTTP Header Security
# Description:
# All write activities shall be authenticated, i.e. DELETE
# TODO - should also test DELETE account with session key?
###################################################################################################
def Assertion_9_3_2_3(self, log) :
log.AssertionID = '9.3.2.3'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)
authorization = 'on'
unauth_payload = ''
auth_payload = ''
rq_headers = self.request_headers()
account_url = ''
user_name = 'testuser'
root_link_key = 'AccountService'
if root_link_key in self.sut_toplevel_uris and self.sut_toplevel_uris[root_link_key]['url']:
log.assertion_log('line', "~ note: this assertion sub-invokes Assertion 9.3.3.3")
json_payload, headers, status = self.http_cached_GET(self.sut_toplevel_uris[root_link_key]['url'], rq_headers, authorization)
assertion_status_ = self.response_status_check(self.sut_toplevel_uris[root_link_key]['url'], status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
elif not json_payload:
assertion_status = log.WARN
log.assertion_log('line', 'No response body returned for resource %s. This assertion for the resource could not be completed' % (self.sut_toplevel_uris[root_link_key]['url']))
else: ## get Accounts collection from payload
try :
key = 'Accounts'
acc_collection = (json_payload[key])['@odata.id']
except : # no user accounts?
assertion_status = log.WARN
log.assertion_log('line', "~ no accounts collection was returned from GET %s" % self.sut_toplevel_uris[root_link_key]['url'])
else:
deleted = False
user_exist = False
#create temp account then delete it
#check if user already exists, if it does perfcorm a delete to clean up
members = self.get_resource_members(acc_collection)
for json_payload, headers in members:
if json_payload['UserName'] == user_name:
user_exist = True
account_url = json_payload['@odata.id']
break
if user_exist == False:
#do a GET on acc_collection and check if POST is allowed
json_payload, headers, status = self.http_cached_GET(acc_collection, rq_headers, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
# check if intended method is an allowable method for resource
elif (self.allowable_method('POST', headers) != True):
assertion_status = log.FAIL
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for POST" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
#create it
rq_body = {'UserName': 'testuser', 'Password': 'testpass', 'RoleId' : 'Administrator'}
log.assertion_log('TX_COMMENT', 'Requesting POST on resource %s with request body %s' % (acc_collection, rq_body))
json_payload, headers, status = self.http_POST(acc_collection, rq_headers, rq_body, authorization)
assertion_status_ = self.response_status_check(acc_collection, status, log, rf_utility.HTTP_CREATED, 'POST')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
log.assertion_log('line', "~ note: Item %s to test DELETE could not be created through script due to service errors stated, Try adding a user on the system through server management and reconfigure request body." % (user_name) )
else:
try:
account_url = headers['location']
except:
assertion_status = log.WARN
log.assertion_log('line', "~ note: Location header expected in response for POST %s ~ not found" % (user_name))
log.assertion_log('line', "~ note: Could not obtain url of the newly created object, cannot request a DELETE for %s in this assertion" % (user_name))
if (assertion_status == log.PASS or account_url != ''):
#got past either creating new or using existing user account
found = False
#get on account_url
json_payload, headers, status = self.http_cached_GET(account_url, rq_headers, authorization)
assertion_status_ = self.response_status_check(account_url, status, log)
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
# check if intended method is an allowable method for resource
if (self.allowable_method('DELETE', headers) != True):
assertion_status = log.WARN
log.assertion_log('line', "~ note: the header returned from GET %s do not indicate support for DELETE" % json_payload['@odata.id'])
log.assertion_log('line', rf_utility.json_string(headers))
else:
#1.auth off
authorization = 'off' #turn auth off
rq_headers = self.request_headers()
unauth_payload, headers, status = self.http_DELETE(json_payload['@odata.id'], rq_headers, authorization)
assertion_status_ = self.response_status_check(json_payload['@odata.id'], status, log, rf_utility.HTTP_UNAUTHORIZED, 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
if assertion_status_ != log.PASS:
pass
else:
#2.auth on, to get json_payload to compare if extended error has any privilages info
authorization = 'on'
rq_headers = self.request_headers()
auth_payload, headers, status = self.http_DELETE(json_payload['@odata.id'], rq_headers, authorization)
assertion_status_ = self.response_status_check(json_payload['@odata.id'], status, log, request_type = 'DELETE')
# manage assertion status
assertion_status = log.status_fixup(assertion_status,assertion_status_)
else:
assertion_status = log.WARN
log.assertion_log('line', "~ Uri to resource: %s not found in redfish top level links: %s" % (root_link_key, self.sut_toplevel_uris) )
run_sub_assertion = False
if ((unauth_payload) or (auth_payload)):
run_sub_assertion = True
if (run_sub_assertion == False):
log.assertion_log('line', "~ note: Assertion failures have prevented sub-invocation of related assertions")
log.assertion_log(assertion_status, None)
return(assertion_status)
else:
# completion status for this assertion...
log.assertion_log(assertion_status, None)
#subinvoke assertion
Assertion_9_3_3_3(unauth_payload,auth_payload, log)
return (assertion_status)
#end Assertion 9.3.2.3
###################################################################################################
# Name: Assertion_9_3_3_3() HTTP Header Security
# Description: DELETE
# Extended error messages shall NOT provide privileged info when authentication failures occur
###################################################################################################
def Assertion_9_3_3_3(unauth_payload,auth_payload, log) :
log.AssertionID = '9.3.3.3'
assertion_status = log.PASS
log.assertion_log('BEGIN_ASSERTION', None)