dne-security-code icon indicating copy to clipboard operation
dne-security-code copied to clipboard

ISE non-pythonic code - ise_mission.py

Open chrivand opened this issue 4 years ago • 0 comments

ise_mission.py

In the function ‘post_to_ise’ the payload is created in an extremely non-pythonic way. The nice thing is that there is a function in the code that can do it in a pythonic way: ‘createPayload’ (by the way: the function name is non-PEP8)

Original code

def post_to_ise(maclist, namelist):
    #TODO: Create the URL for the PUT request to apply the ANC policy! Hint: Make sure you pass the Auth paramenters for the API call
    url = MISSION
    env_lab.print_missing_mission_warn(env_lab.get_line())
    
    for items in maclist:
        payload = "{\r\n    \"OperationAdditionalData\": {\r\n    \"additionalData\": [{\r\n    \"name\": \"macAddress\",\r\n    \"value\": \""+ items + "\"\r\n    },\r\n    {\r\n    \"name\": \"policyName\",\r\n    \"value\": \"" + namelist + '"' + "\r\n    }]\r\n  }\r\n}"
        print(json.dumps(payload,sort_keys=True,indent=3))
        response = requests.request("PUT", url, data=payload, verify=False, headers=headers)
        if(response.status_code == 204):
            print("Done!..Applied Quarantine policy to the rogue endpoint...MAC: {0} Threat is now contained....".format(items))
        else:
            print("An error has ocurred with the following code %(error)s" % {'error': response.status_code})

Proposed code: Import HTTPBasisAuth Create the payload with the available function Change the request call, added json=payload in stead of data=payload and added authentication

from requests.auth import HTTPBasicAuth
 
def post_to_ise(maclist, namelist):
    #TODO: Create the URL for the PUT request to apply the ANC policy! Hint: Make sure you pass the Auth paramenters for the API call
    url = MISSION
    env_lab.print_missing_mission_warn(env_lab.get_line())

    authentication = HTTPBasicAuth(username, password)
    for items in maclist:
        payload = createPayload(items, namelist)
        print(json.dumps(payload, sort_keys=True, indent=3))
        response = requests.put(url, json=payload, verify=False, headers=headers, auth=authentication)
        if response.status_code == 204:
            print("Done!..Applied Quarantine policy to the rogue endpoint...MAC: {0} Threat is now contained....".format(items))
        else:
            print("An error has ocurred with the following code %(error)s" % {'error': response.status_code})

chrivand avatar Jul 01 '20 16:07 chrivand