From fa8f15329da6546ba91ae704060582e7dfc6d23d Mon Sep 17 00:00:00 2001 From: Ivan Maslov Date: Thu, 13 May 2021 18:03:27 +0300 Subject: [PATCH] Add WebAuditMessage + prototype for many servers... --- Sources/pyOpenRPA/Orchestrator/Server.py | 7 ++-- .../pyOpenRPA/Orchestrator/ServerSettings.py | 14 +++++--- .../Orchestrator/SettingsTemplate.py | 7 ++++ .../Orchestrator/__Orchestrator__.py | 34 +++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Sources/pyOpenRPA/Orchestrator/Server.py b/Sources/pyOpenRPA/Orchestrator/Server.py index 9db03c12..6b0a64d5 100644 --- a/Sources/pyOpenRPA/Orchestrator/Server.py +++ b/Sources/pyOpenRPA/Orchestrator/Server.py @@ -526,6 +526,7 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): HTTPServer.finish_request(self, request, client_address) #inGlobalDict # "JSONConfigurationDict": +import ssl class RobotDaemonServer(Thread): def __init__(self,name,inGlobalDict): Thread.__init__(self) @@ -533,8 +534,8 @@ class RobotDaemonServer(Thread): # Update the global dict ServerSettings.SettingsUpdate(inGlobalDict) def run(self): - inServerAddress=""; - inPort = gSettingsDict["ServerDict"]["ListenPort"]; + inServerAddress="" + inPort = gSettingsDict["ServerDict"]["ListenPort"] # Server settings # Choose port 8080, for port 80, which is normally used for a http server, you need root access server_address = (inServerAddress, inPort) @@ -543,5 +544,7 @@ class RobotDaemonServer(Thread): gSettingsDict["Logger"].info(f"Server init. Listen URL: {inServerAddress}, Listen port: {inPort}") #httpd.serve_forever() httpd = ThreadedHTTPServer(server_address, testHTTPServer_RequestHandler) + #httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, + # certfile='yourpemfile.pem') #print('Starting server, use to stop') httpd.serve_forever() diff --git a/Sources/pyOpenRPA/Orchestrator/ServerSettings.py b/Sources/pyOpenRPA/Orchestrator/ServerSettings.py index da236626..b3197ee8 100644 --- a/Sources/pyOpenRPA/Orchestrator/ServerSettings.py +++ b/Sources/pyOpenRPA/Orchestrator/ServerSettings.py @@ -279,7 +279,8 @@ def pyOpenRPA_Processor(inRequest, inGSettings): lActivityTypeListStr += f"{lActivityItem['Def']}; " except Exception as e: lActivityTypeListStr = "Has some error with Activity Type read" - if lL: lL.info(f"ServerSettings.pyOpenRPA_Processor. User activity from web. Domain: {inRequest.OpenRPA['Domain']}, Username: {inRequest.OpenRPA['User']}, ActivityType: {lActivityTypeListStr}") + lWebAuditMessageStr = __Orchestrator__.WebAuditMessageCreate(inRequest=inRequest,inOperationCodeStr=lActivityTypeListStr, inMessageStr="pyOpenRPA_Processor") + if lL: lL.info(lWebAuditMessageStr) # Append in list inGSettings["ProcessorDict"]["ActivityList"]+=lInput else: @@ -290,7 +291,8 @@ def pyOpenRPA_Processor(inRequest, inGSettings): lActivityTypeListStr = lInput['Def'] except Exception as e: lActivityTypeListStr = "Has some error with Activity Type read" - if lL: lL.info(f"ServerSettings.pyOpenRPA_Processor. User activity from web. Domain: {inRequest.OpenRPA['Domain']}, Username: {inRequest.OpenRPA['User']}, ActivityType: {lActivityTypeListStr}") + lWebAuditMessageStr = __Orchestrator__.WebAuditMessageCreate(inRequest=inRequest,inOperationCodeStr=lActivityTypeListStr, inMessageStr="pyOpenRPA_Processor") + if lL: lL.info(lWebAuditMessageStr) # Append in list inGSettings["ProcessorDict"]["ActivityList"].append(lInput) # Execute activity list @@ -313,7 +315,8 @@ def pyOpenRPA_ActivityListExecute(inRequest, inGSettings): lActivityTypeListStr += f"{lActivityItem['Def']}; " except Exception as e: lActivityTypeListStr = "Has some error with Activity Type read" - if lL: lL.info(f"ServerSettings.pyOpenRPA_ActivityListExecute. User activity from web. Domain: {inRequest.OpenRPA['Domain']}, Username: {inRequest.OpenRPA['User']}, ActivityType: {lActivityTypeListStr}") + lWebAuditMessageStr = __Orchestrator__.WebAuditMessageCreate(inRequest=inRequest,inOperationCodeStr=lActivityTypeListStr, inMessageStr="pyOpenRPA_ActivityListExecute") + if lL: lL.info(lWebAuditMessageStr) # Execution lResultList = Processor.ActivityListExecute(inGSettings = inGSettings, inActivityList = lInput) inRequest.OpenRPAResponseDict["Body"] = bytes(json.dumps(lResultList), "utf8") @@ -325,7 +328,10 @@ def pyOpenRPA_ActivityListExecute(inRequest, inGSettings): lActivityTypeListStr = lInput['Def'] except Exception as e: lActivityTypeListStr = "Has some error with Activity Type read" - if lL: lL.info(f"ServerSettings.pyOpenRPA_ActivityListExecute. User activity from web. Domain: {inRequest.OpenRPA['Domain']}, Username: {inRequest.OpenRPA['User']}, ActivityType: {lActivityTypeListStr}") + lWebAuditMessageStr = __Orchestrator__.WebAuditMessageCreate(inRequest=inRequest, + inOperationCodeStr=lActivityTypeListStr, + inMessageStr="pyOpenRPA_ActivityListExecute") + if lL: lL.info(lWebAuditMessageStr) # Execution lResultList = Processor.ActivityListExecute(inGSettings = inGSettings, inActivityList = [lInput]) inRequest.OpenRPAResponseDict["Body"] = bytes(json.dumps(lResultList[0]), "utf8") diff --git a/Sources/pyOpenRPA/Orchestrator/SettingsTemplate.py b/Sources/pyOpenRPA/Orchestrator/SettingsTemplate.py index 10570556..4a89ee9d 100644 --- a/Sources/pyOpenRPA/Orchestrator/SettingsTemplate.py +++ b/Sources/pyOpenRPA/Orchestrator/SettingsTemplate.py @@ -53,6 +53,13 @@ def __Create__(): "URL": "" } ], + "ListenDict": { # Prototype + "localhost":{ + "AddressStr":"", + "PortInt":80, + "CertFilePEMPathStr":"" + } + }, "AccessUsers": { # Default - all URL is blocked "FlagCredentialsAsk": True, # Turn on Authentication "RuleDomainUserDict": { diff --git a/Sources/pyOpenRPA/Orchestrator/__Orchestrator__.py b/Sources/pyOpenRPA/Orchestrator/__Orchestrator__.py index 1fcf5d9a..ff99628c 100644 --- a/Sources/pyOpenRPA/Orchestrator/__Orchestrator__.py +++ b/Sources/pyOpenRPA/Orchestrator/__Orchestrator__.py @@ -600,6 +600,40 @@ def WebCPUpdate(inGSettings, inCPKeyStr, inHTMLRenderDef=None, inJSONGeneratorDe if inJSInitGeneratorDef is not None: inGSettings["CPDict"][inCPKeyStr]["JSInitGeneratorDef"] = inJSInitGeneratorDef + +def WebAuditMessageCreate(inRequest, inOperationCodeStr="-", inMessageStr="-"): + """ + Create message string with request user details (IP, Login etc...). Very actual for IT security in big company. + + .. code-block:: python + + # USAGE + from pyOpenRPA import Orchestrator + + lWebAuditMessageStr = Orchestrator.WebAuditMessageCreate( + inRequest = lRequest, + inOperationCodeStr = "OP_CODE_1", + inMessageStr="Success"): + + # Log the WebAudit message + lLogger.info(lWebAuditMessageStr) + + :param inRequest: HTTP request handler + :param inOperationCodeStr: operation code in string format (actual for IT audit in control panels) + :param inMessageStr: additional message after + :return: format "WebAudit :: DOMAIN\\USER@101.121.123.12 :: operation code :: message" + """ + try: + lClientIPStr = inRequest.client_address[0] + lUserDict = WebUserInfoGet(inRequest=inRequest) + lDomainUpperStr = lUserDict["DomainUpperStr"] + lUserLoginStr = lUserDict["UserNameUpperStr"] + lResultStr = f"WebAudit :: {lDomainUpperStr}\\\\{lUserLoginStr}@{lClientIPStr} :: {inOperationCodeStr} :: {inMessageStr}" + except Exception as e: + print(str(e)) # Has no logger - must be dead alg branch + lResultStr = inMessageStr + return lResultStr + def WebUserInfoGet(inRequest): """ Return User info about request