nYWQgwsd

2021-09-13 15:14

			
import requests
import json
import traceback


TESTING = False


class HAWKAPI(object):

    def __init__(self):
        if TESTING:
            self.base_url = 'https://qa.hawkdefense.com:8080/API/1.1'
            self.username = ''
            self.password = ''
        else:
            self.base_url = 'https://portal.hawk.io:8080/API/1.1'
            self.username = ''
            self.password = ''

        self.client = requests.Session()
        self.client.verify = False

    def login(self):

        url = self.base_url + '/login'

        data = {'username': self.username, 'password': self.password}

        try:
            res = self.client.post(url, data=data)
            res.raise_for_status()

        except Exception as e:
            print(e)

        return True

    def get_resources(self):

        url = self.base_url + '/search/resource'

        data = {'column[]': 'resource_name'}

        try:
            res = self.client.post(url, data=data)
            res.raise_for_status()

        except Exception as e:
            print(e)

        return res.json()['results']

    def search_resources_ip(self, ip_address):

        url = self.base_url + '/search/resource'

        data = {
            'column[]': 'resource_name',
            'where[]': 'resource_address="{}"'.format(ip_address)
            }

        try:
            res = self.client.post(url, data=data)
            res.raise_for_status()

        except Exception as e:
            print(e)

        return res.json()['results']

    def search_resources_crit(self, criticality):

        url = self.base_url + '/search/resource'
        res = None

        data = {
            'column[]': 'resource_name',
            'where[]': 'resource_asset_criticality="{}"'.format(criticality)
            }

        try:
            res = self.client.post(url, data=data)
            res.raise_for_status()

        except Exception as e:
            print(e, traceback.format_exc())

        if res:
            return res.json()['results']
        else:
            return None

    def eanble_resource_monitoring(self):
        '''Enable resource monitoring where criticality equals value'''

        url = self.base_url + '/resource/{}'

        result = {}
        result['count'] = 0

        criticality = 1
        if isinstance(criticality, int):
            resources = self.search_resources_crit(criticality)
            if resources:
                for resource in resources:
                    print(json.dumps(resource, indent=4, sort_keys=True))
                    if (resource['resource_asset_criticality'] == 1
                            and not resource['monitor']):
                        # and resource['resource_group'] == "test"):

                        # Update just the values that are needed to be changed
                        # and send the whole record back.
                        resource['monitor'] = 'true'
                        resource['monitor_hours'] = 0.25

                        try:
                            res = self.client.post(
                                url.format(resource['resource_id']),
                                data=resource
                                )
                            res.raise_for_status()

                        except Exception as e:
                            print(e)

                        print(res.json())
                        result['count'] += 1
        return result

    def run(self):

        self.login()

        '''
        resources = self.get_resources()

        for resource in resources:
            print(json.dumps(resource, indent=4, sort_keys=True))
        '''

        result = self.eanble_resource_monitoring()

        print("{} Resources have been updated...".format(result['count']))

        '''
        criticality = 1
        if isinstance(criticality, int):
            resources = self.search_resources_crit(criticality)

            for resource in resources:
                # print(json.dumps(resource, indent=4, sort_keys=True))
                for k, v in resource.items():
                    print("Key: {}, Value: {}, Value_Type: {}".format(k, v, type(v)))
        '''


if __name__ == '__main__':
    API = HAWKAPI()
    API.run()