Skip to content

Commit

Permalink
Merge pull request #145 from PeterKchen2/PasswordChangeRequired
Browse files Browse the repository at this point in the history
PasswordChangeRequired support
  • Loading branch information
mraineri committed Aug 4, 2023
2 parents 78d25a2 + 3b819e5 commit daf4cab
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/redfish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

""" Redfish restful library """

__all__ = ['rest', 'ris', 'discovery']
__version__ = "3.2.0"
__all__ = ['rest', 'ris', 'discovery', 'messages']
__version__ = "3.2.0"

from redfish.rest.v1 import redfish_client
from redfish.rest.v1 import AuthMethod
from redfish.discovery.discovery import discover_ssdp
from redfish.messages import *
import logging

def redfish_logger(file_name, log_format, log_level=logging.ERROR):
Expand Down
5 changes: 5 additions & 0 deletions src/redfish/messages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright Notice:
# Copyright 2016-2021 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link:
# https://github.com/DMTF/python-redfish-library/blob/main/LICENSE.md
from .messages import *
148 changes: 148 additions & 0 deletions src/redfish/messages/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#! /usr/bin/python
# Copyright Notice:
# Copyright 2019-2020 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Tacklebox/blob/main/LICENSE.md

"""
Messages Module
File : messages.py
Brief : This file contains the definitions and functionalities for interacting
with Messages for a given Redfish service
"""
import re

class RedfishOperationFailedError( Exception ):
"""
Raised when an operation has failed (HTTP Status >= 400)
"""
pass

class RedfishPasswordChangeRequiredError( Exception ):
"""
Raised when password change required
"""
def __str__(self):
return "\n{}\nURL: {}\n".format( str(self.args[0]), str(self.args[1]) )

def get_messages_detail( response ):
"""
Builds messages detail dict in the payload
Args:
response: The response to parser
Returns:
The dict containing messages_detail
messages_detail["status"]: http status code
messages_detail["successful"]: response successful (http status code < 400)
messages_detail["code"]: redfish message response code field
messages_detail["@Message.ExtendedInfo"]: redfish message response code field
"""

messages_detail = {}
messages_detail["status"] = response.status
messages_detail["text"] = response.text
messages_detail["successful"] = False
messages_detail["@Message.ExtendedInfo"] = []

if response.status >= 400:
messages_detail["successful"] = False
else:
messages_detail["successful"] = True

try:
message_body = response.dict
messages_detail["body"] = response.dict

if not "@Message.ExtendedInfo" in message_body:
message_body = response.dict["error"]
check_message_field = True
if "@Message.ExtendedInfo" in message_body:
messages_detail["@Message.ExtendedInfo"] = message_body["@Message.ExtendedInfo"]
for index in range(len(messages_detail["@Message.ExtendedInfo"])):
messages_item = messages_detail["@Message.ExtendedInfo"][index]
if not "MessageId" in messages_item:
messages_item["MessageId"] = ""
if not "Message" in messages_item:
messages_item["Message"] = ""
messages_detail["@Message.ExtendedInfo"][index] = messages_item
check_message_field = False

if check_message_field is True:
messages_detail["@Message.ExtendedInfo"] = []
messages_item = {}
if "code" in message_body:
messages_item["MessageId"] = message_body["code"]
else:
messages_item["MessageId"] = ""
if "message" in message_body:
messages_item["Message"] = message_body["message"]
else:
messages_item["Message"] = ""
messages_detail["@Message.ExtendedInfo"].insert(0, messages_item)
except:
messages_detail["@Message.ExtendedInfo"] = []
messages_detail["body"] = {}

return messages_detail

def search_message(response, message_registry_group, message_registry_id):
"""
search message in the payload
Args:
response: The response to parser
message_registry_group: target message_registry_group
message_registry_id: target message_registry_id
Returns:
The dict containing target message detail
"""
if isinstance(response, dict) and "@Message.ExtendedInfo" in response:
messages_detail = response
else:
messages_detail = get_messages_detail(response)

message_registry_id_search = "^" + message_registry_group + "\.[0-9]+\.[0-9]+\." + message_registry_id +"$"

for messages_item in messages_detail["@Message.ExtendedInfo"]:
if "MessageId" in messages_item:
resault = re.search(message_registry_id_search, messages_item["MessageId"])
if resault:
return messages_item
return None

def get_error_messages( response ):
"""
Builds a string based on the error messages in the payload
Args:
response: The response to print
Returns:
The string containing error messages
"""

# Pull out the error payload and the messages

out_string = ""
try:
if isinstance(response, dict) and "@Message.ExtendedInfo" in response:
messages_detail = response
else:
messages_detail = get_messages_detail(response)

if "@Message.ExtendedInfo" in messages_detail:
for message in messages_detail["@Message.ExtendedInfo"]:
if "Message" in message:
out_string = out_string + "\n" + message["Message"]
else:
out_string = out_string + "\n" + message["MessageId"]
out_string = out_string + "\n"
except:
# No response body
out_string = ""

return out_string

5 changes: 5 additions & 0 deletions src/redfish/rest/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import re
import requests
import requests_unixsocket
from redfish.messages import *

from collections import (OrderedDict)

Expand Down Expand Up @@ -995,6 +996,10 @@ def login(self, username=None, password=None, auth=AuthMethod.SESSION):
self.__session_key = resp.session_key
self.__session_location = resp.session_location

message_item = search_message(resp, "Base", "PasswordChangeRequired")
if not message_item is None:
raise RedfishPasswordChangeRequiredError("Password Change Required\n", message_item["MessageArgs"][0])

if not self.__session_key and resp.status not in [200, 201, 202, 204]:
if resp.status == 401:
# Invalid credentials supplied
Expand Down

0 comments on commit daf4cab

Please sign in to comment.