Fixed auth bug
This commit is contained in:
		| @@ -54,3 +54,4 @@ prd_instance_id = 1 | |||||||
| product_guid = "asdf" | product_guid = "asdf" | ||||||
| product_name = "EFT" | product_name = "EFT" | ||||||
| product_version ="8.1.0.9" | product_version ="8.1.0.9" | ||||||
|  | prd_ext_tenant_id = "e71851c2-593f-4f49-9c07-91727b1be94b" | ||||||
							
								
								
									
										6
									
								
								inex.py
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								inex.py
									
									
									
									
									
								
							| @@ -44,6 +44,7 @@ class Inex: | |||||||
|                 self.logPath = self.config["logging"]["logPath"] |                 self.logPath = self.config["logging"]["logPath"] | ||||||
|                 self.logLevel = self.config["logging"]["logLevel"] |                 self.logLevel = self.config["logging"]["logLevel"] | ||||||
|                 self.prdInstanceID = self.config["immutables"]["prd_instance_id"] |                 self.prdInstanceID = self.config["immutables"]["prd_instance_id"] | ||||||
|  |                 self.prd_ext_tenant_id = self.config["immutables"]["prd_ext_tenant_id"] | ||||||
|                 self.productGUID = self.config["immutables"]["product_guid"] |                 self.productGUID = self.config["immutables"]["product_guid"] | ||||||
|                 self.productName = self.config["immutables"]["product_name"] |                 self.productName = self.config["immutables"]["product_name"] | ||||||
|                 self.productVersion = self.config["immutables"]["product_version"] |                 self.productVersion = self.config["immutables"]["product_version"] | ||||||
| @@ -74,10 +75,11 @@ class Inex: | |||||||
|         self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.sq.sqlQuerymodel.queryData(self.queryOverride,self.dbQuery, self.queryDaystopull)) |         self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.sq.sqlQuerymodel.queryData(self.queryOverride,self.dbQuery, self.queryDaystopull)) | ||||||
|  |  | ||||||
|         self.modifiedData = processData(self.data, dataTemplate, prd_instance_id=self.prdInstanceID,\ |         self.modifiedData = processData(self.data, dataTemplate, prd_instance_id=self.prdInstanceID,\ | ||||||
|                                          product_guid=self.productGUID,product_name=self.productName,product_version=self.productVersion) |                                          product_guid=self.productGUID,product_name=self.productName,\ | ||||||
|  |                                          product_version=self.productVersion,prd_ext_tenant_id=self.prd_ext_tenant_id) | ||||||
|  |  | ||||||
|         if self.pushToplatform: |         if self.pushToplatform: | ||||||
|             inexConnect.fortraEFC.pushPayload(self) |             inexConnect.fortraEFC.__init__(self) | ||||||
|  |  | ||||||
|         # TODO: move this to its own function |         # TODO: move this to its own function | ||||||
|         if self.useLog: |         if self.useLog: | ||||||
|   | |||||||
| @@ -43,33 +43,46 @@ class inexSql: | |||||||
|         return r |         return r | ||||||
|  |  | ||||||
| class fortraEFC: | class fortraEFC: | ||||||
|     def getToken(self): |     def __init__(self): | ||||||
|         self.tokenData = self.r.post(self.platformConfig["idp"], data={"grant_type":"client_credentials",\ |         # Check if .token file is present | ||||||
|                                                                               "client_id": self.platformConfig["client_id"],\ |         if fortraEFC.readToken(self) == 1: | ||||||
|                                                                               "client_secret": self.platformConfig["secret"],}) |             # Get fresh token. First run. | ||||||
|     def writeToken(self): |  | ||||||
|             fortraEFC.getToken(self) |             fortraEFC.getToken(self) | ||||||
|         with open(self.tokenFilepath, "w") as f: |             fortraEFC.writeToken(self) | ||||||
|             self.j.dump(self.tokenData.json(), f, indent = 2) |         # Push data with token | ||||||
|  |         self.pushPayloadresponse = fortraEFC.pushPayload(self) | ||||||
|  |         if self.pushPayloadresponse == 401: | ||||||
|  |             fortraEFC.getToken(self) | ||||||
|  |             fortraEFC.writeToken(self) | ||||||
|  |             fortraEFC.pushPayload(self) | ||||||
|  |  | ||||||
|     def readToken(self): |     def readToken(self): | ||||||
|         if self.os.path.exists(self.tokenFilepath): |         if self.os.path.exists(self.tokenFilepath): | ||||||
|             with open(self.tokenFilepath, 'rb') as t: |             with open(self.tokenFilepath, 'rb') as t: | ||||||
|                 self.tokenData = self.j.load(t) |                 self.tokenData = self.j.load(t) | ||||||
|                 # print(self.tokenData["access_token"]) |                 self.il.debug(f'readToken {self.tokenData["access_token"]}') | ||||||
|  |             return 0 | ||||||
|         else: |         else: | ||||||
|             fortraEFC.writeToken(self) |             return 1 | ||||||
|  |      | ||||||
|  |     def getToken(self): | ||||||
|  |         self.tokenData = self.r.post(self.platformConfig["idp"], data={"grant_type":"client_credentials",\ | ||||||
|  |                                                                               "client_id": self.platformConfig["client_id"],\ | ||||||
|  |                                                                               "client_secret": self.platformConfig["secret"],}) | ||||||
|  |         self.tokenData = self.tokenData.json() | ||||||
|  |         self.il.debug(f'getToken {self.tokenData["access_token"]}') | ||||||
|  |  | ||||||
|  |     def writeToken(self): | ||||||
|  |         fortraEFC.getToken(self) | ||||||
|  |         with open(self.tokenFilepath, "w") as f: | ||||||
|  |             self.j.dump(self.tokenData, f, indent = 2) | ||||||
|  |             self.il.debug(f'writeToken {self.tokenData["access_token"]}') | ||||||
|  |  | ||||||
|     def pushPayload(self): |     def pushPayload(self): | ||||||
|         fortraEFC.readToken(self) |         self.il.debug(f'pushPayload {self.tokenData["access_token"]}') | ||||||
|         print(self.tokenData) |  | ||||||
|         try: |  | ||||||
|         url = f'{self.platformConfig["efc_url"]}/api/v1/unity/data/{self.platformConfig["tenant_id"]}/machine_event' |         url = f'{self.platformConfig["efc_url"]}/api/v1/unity/data/{self.platformConfig["tenant_id"]}/machine_event' | ||||||
|             pushPayloadResponse = self.r.post(url, headers={'Authorization': f'bearer {self.tokenData["access_token"]}'},\ |         pushPayloadResponse = self.r.post(url, headers={'Authorization': f'Bearer {self.tokenData["access_token"]}'},\ | ||||||
|                                                json=self.j.dumps(self.modifiedData,indent = 2, cls=self.e)) |                                            data=self.j.dumps(self.modifiedData, cls=self.e)) | ||||||
|  |         self.il.debug(pushPayloadResponse.status_code) | ||||||
|  |         self.il.debug(pushPayloadResponse.text) | ||||||
|         return pushPayloadResponse.status_code |         return pushPayloadResponse.status_code | ||||||
|         except self.r.exceptions.HTTPError as errh: |  | ||||||
|             print ("Http Error:",errh) |  | ||||||
|             if "401" in errh: |  | ||||||
|                 fortraEFC.writeToken(self) |  | ||||||
|                 fortraEFC.pushPayload(self) |  | ||||||
| @@ -14,7 +14,7 @@ def processData(data, template, **kwargs): | |||||||
|             continue |             continue | ||||||
|         try: |         try: | ||||||
|             processedData.append(template(identifyUtype(row.get('Command')),\ |             processedData.append(template(identifyUtype(row.get('Command')),\ | ||||||
|                 prd_ext_tenant_id='',\ |                 prd_ext_tenant_id=kwargs.get('prd_ext_tenant_id'),\ | ||||||
|                 status_code=row.get('ResultID'),\ |                 status_code=row.get('ResultID'),\ | ||||||
|                 file_size=row.get('FileSize'),\ |                 file_size=row.get('FileSize'),\ | ||||||
|                 file_path=row.get('PhysicalFolderName'),\ |                 file_path=row.get('PhysicalFolderName'),\ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user