Skip to content

ISE non-pythonic code - ise_mission.py  #45

@chrivand

Description

@chrivand

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})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions