Skip to content

Commit fd55e5e

Browse files
Fix propagating headers across different class instances
There was a bug in login function of RestClientBase which incorrectly propagated headers across all instances of the class. Fix it. The empty dictionary was created only once in the parameter list, and then reused for all following instances. The bug appears e.g. when trying to change the authorization method in the same service using redfish library: 1) creating redfish_client with basic authorization adds a header to a global headers dictionary ('Authorization') 2) creating a totally separate instance of redfish_client with session fails, because we get unexpected 'Authorization' header which was created in previous instance and propagated incorrectly Simplified example to explain the incorrect use of dictionary parameter: ``` class Example: def bad_param(self, optional_dict={}): if not optional_dict: print('Optional dict is empty, adding something') optional_dict['some'] = 'thing' else: print(f'Optional dict is NOT empty: {optional_dict}') example1 = Example() example1.bad_param() example2 = Example() example2.bad_param() ``` Signed-off-by: Michał Michalik <michal.michalik.priv@gmail.com>
1 parent 79a008d commit fd55e5e

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/redfish/rest/v1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ def _rest_request(self, path, method='GET', args=None, body=None,
986986
else:
987987
raise RetriesExhaustedError() from cause_exception
988988

989-
def login(self, username=None, password=None, auth=AuthMethod.SESSION, headers={}):
989+
def login(self, username=None, password=None, auth=AuthMethod.SESSION, headers=None):
990990
"""Login and start a REST session. Remember to call logout() when"""
991991
""" you are done.
992992
@@ -1006,6 +1006,8 @@ def login(self, username=None, password=None, auth=AuthMethod.SESSION, headers={
10061006
self.__username = username if username else self.__username
10071007
self.__password = password if password else self.__password
10081008

1009+
headers = headers if headers else {}
1010+
10091011
if auth == AuthMethod.BASIC:
10101012
auth_key = base64.b64encode(('%s:%s' % (self.__username,
10111013
self.__password)).encode('utf-8')).decode('utf-8')

0 commit comments

Comments
 (0)