Ejemplo n.º 1
0
    def test_timeout_overrides(self):
        # test default timeout
        integrations = {"integrations": {}}
        rc = RequestsCommon(integrations, None)
        self.assertEqual(rc.get_timeout(), 30)

        # test global setting
        integrations_timeout = {"integrations": {"timeout": "35"}}
        rc = RequestsCommon(integrations_timeout, None)
        self.assertEqual(rc.get_timeout(), 35)

        # test timeout
        integrations_twenty = {"integrations": {"timeout": "8"}}
        rc = RequestsCommon(integrations_twenty, None)
        url = "/".join(
            (TestFunctionRequests.URL_TEST_HTTP_VERBS, "delay", "10"))

        with self.assertRaises(IntegrationError):
            rc.execute_call_v2("get", url)

        integrations_fourty = {"integrations": {"timeout": "20"}}
        rc = RequestsCommon(integrations_fourty, None)
        url = "/".join(
            (TestFunctionRequests.URL_TEST_HTTP_VERBS, "delay", "10"))
        rc.execute_call_v2("get", url)
Ejemplo n.º 2
0
def selftest_function(opts):
    """
    Placeholder for selftest function. An example use would be to test package api connectivity.
    Suggested return values are be unimplemented, success, or failure.
    """
    options = opts.get("fn_rsa_netwitness", {})
    nw_packet_server_url = options.get("nw_packet_server_url")
    nw_packet_server_user = options.get("nw_packet_server_user")
    nw_packet_server_password = options.get("nw_packet_server_password")
    nw_packet_server_verify = str_to_bool(options.get("nw_packet_server_verify"))

    nw_log_server_url = options.get("nw_log_server_url")
    nw_log_server_user = options.get("nw_log_server_user")
    nw_log_server_password = options.get("nw_log_server_password")
    nw_log_server_verify = str_to_bool(options.get("nw_log_server_verify"))

    try:
        request_common = RequestsCommon(options, opts)

        # Test PCAP server connection
        headers = get_headers(nw_packet_server_user, nw_packet_server_password)
        request_url = "{}/sdk/packets?sessions={}&render=pcap".format(nw_packet_server_url, "100")
        request_common.execute_call_v2("GET", request_url, verify=nw_packet_server_verify,\
             headers=headers).content

        # Test Logger server connection
        time1 = int(time.time()) * 1000
        time2 = int(time.time()) * 1000
        headers = get_headers(nw_log_server_user, nw_log_server_password)
        request_url = "{}/sdk/packets?time1={}&time2={}&render={}"\
            .format(nw_log_server_url, time1, time2, "logs")
        request_common.execute_call_v2("GET", request_url,\
            verify=nw_log_server_verify, headers=headers).text

        return {"state": "success"}
    except Exception as err:
        err_reason_msg = """Could not connect to NetWitness.
                    error: {0}
                    ---------
                    Current Configs in app.config file::
                    ---------
                    nw_packet_server_url: {1}
                    nw_packet_server_user: {2}
                    nw_packet_server_verify: {3}
                    nw_log_server_url: {4}
                    nw_log_server_user: {5}
                    nw_log_server_verify: {6}\n""".format(
            err,
            nw_packet_server_url,
            nw_packet_server_user,
            nw_packet_server_verify,
            nw_log_server_url,
            nw_log_server_user,
            nw_log_server_verify)

        log.error(err_reason_msg)

        return {"state": "failed"}
Ejemplo n.º 3
0
    def test_statuscode_v2(self):
        URL = TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES

        rc = RequestsCommon(None, None)

        resp = rc.execute_call_v2("get", "/".join((URL, "200")))

        with self.assertRaises(IntegrationError):
            resp = rc.execute_call_v2("get", "/".join((URL, "400")))
def get_job_template_by_project(opts, options, filter_by_project, template_pattern):
    """
    get job templates, optionally based on project name
    :param opts: app.config file
    :param options: app.config section just for this integration
    :param filter_by_project: name of project to search for, may contain wildcards
    :param template_pattern: filter list of templates by wildcards
    :return: list of templates by project
    """
    url = "/".join((clean_url(options.get('url')), TOWER_API_BASE, LIST_URL))
    basic_auth, cafile = get_common_request_items(options)

    rc = RequestsCommon(opts, options)

    next_url = "/".join((TOWER_API_BASE, LIST_URL))
    result_templates = []
    while next_url:
        url = "/".join((clean_url(clean_url(options.get('url'))), next_url))
        results = rc.execute_call_v2("get", url, auth=basic_auth,
                                     verify=cafile)
        json_results = results.json()

        if filter_by_project:
            project_templates = project_pattern_match(json_results['results'], filter_by_project)
            result_templates.extend(template_pattern_match(project_templates, template_pattern))
        else:
            result_templates.extend(template_pattern_match(json_results['results'], template_pattern))

        # get next paged set of results
        next_url = json_results['next']

    return result_templates
def get_job_template_by_name(opts, options, filter_by_name):
    """
    get job templates, optionally based on template name
    :param opts: app.config file
    :param options: app.config section just for this integration
    :param filter_by_name: name of template to search for
    :return: found template or None
    """
    basic_auth, cafile = get_common_request_items(options)

    rc = RequestsCommon(opts, options)

    next_url = "/".join((TOWER_API_BASE, LIST_URL))
    # continue as long as paged results exist
    while next_url:
        url = "/".join((clean_url(options.get('url')), next_url))
        results = rc.execute_call_v2("get", url, auth=basic_auth,
                                     verify=cafile)
        json_results = results.json()

        if filter_by_name:
            for template in json_results['results']:
                # template names are case sensitive
                if template['name'] == filter_by_name.strip():
                    return template

        # get next paged set of results
        next_url = json_results['next']

    return None
class AuthInfo(object):
    # Singleton
    __instance = None

    def __init__(self):
        self.headers = {}
        self.qradar_auth = None
        self.qradar_token = None
        self.api_url = None
        self.cafile = True
        self.rc = None
        pass

    @staticmethod
    def get_authInfo():
        if AuthInfo.__instance is None:
            AuthInfo.__instance = AuthInfo()
        return AuthInfo.__instance

    def create(self, host, username=None, password=None, token=None, cafile=None,
               opts=None, function_opts=None):
        """
        Create headers used for REST Api calls
        :param host: qradar host
        :param username: qradar user login
        :param password: qradar password
        :param token: Use token or username/password to auth
        :param cafile:
        :param opts: app.config options
        :param function_opts: function parameters from app.config
        :return:
        """
        self.headers = {'Accept': 'application/json'}
        if username and password:
            self.qradar_auth = base64.b64encode(
                (username + ':' + password).encode('ascii'))
            self.headers['Authorization'] = b"Basic " + self.qradar_auth
        elif token:
            self.qradar_token = token
            self.headers["SEC"] = self.qradar_token
        if host.startswith("http"):
            self.api_url = "{}/api/".format(host)
        else:
            self.api_url = "https://{}/api/".format(host)
        self.cafile = cafile

        self.rc = RequestsCommon(opts, function_opts)

    def make_call(self, method, url, headers=None, data=None):
        my_headers = headers if headers else self.headers

        def make_call_callback(response):
            # 404 is not found, such as reference not found or item not found in reference set
            if response.status_code in (404,):
                return response
            else:
                response.raise_for_status()
                return response

        return self.rc.execute_call_v2(method, url, data=data, headers=my_headers, verify=self.cafile, callback=make_call_callback)
    def _isitphishing_url_function(self, event, *args, **kwargs):
        """Function: isitphishing_url
        This function takes URL string as a required parameter.  It
        queries the Vade Secure isiphishing API to analyze the
        URL to determine if it is PHISHING or SPAM.
        The results contain the result of the query in 'contents' and the
        URL input parameter to the function.
        """
        try:
            rp = ResultPayload(PACKAGE_NAME, **kwargs)

            # Get the function parameters:
            isitphishing_url = kwargs.get("isitphishing_url")  # text

            log = logging.getLogger(__name__)
            log.info("isitphishing_url: %s", isitphishing_url)

            # Form the URL for API request.
            API_URL = u"{0}/url".format(self.options["isitphishing_api_url"])

            # Get the license key to access the API endpoint.
            auth_token = get_license_key(self.options["isitphishing_name"],
                                         self.options["isitphishing_license"])

            # Build the header and the data payload.
            headers = {
                "Authorization": u'Bearer {}'.format(auth_token),
                "Content-type": "application/json",
            }

            payload = {
                "url": isitphishing_url,
                "force": False,
                "smart": True,
                "timeout": 8000
            }

            yield StatusMessage(
                "Query IsItPhishing.org endpoint for status of URL {0}.".
                format(isitphishing_url))

            # Make URL request
            rc = RequestsCommon(self.opts, self.options)
            response = rc.execute_call_v2("post",
                                          API_URL,
                                          json=payload,
                                          headers=headers,
                                          proxies=rc.get_proxies())
            if response.status_code == 200:
                success = True
            else:
                success = False

            response_json = response.json()
            results = rp.done(success, response_json)

            # Produce a FunctionResult with the results
            yield FunctionResult(results)
        except Exception as err:
            yield FunctionError(err)
Ejemplo n.º 8
0
    def _event_function(self, event, *args, **kwargs):
        """Function: This is a function implementation that uses the Cisco API to post a Malware event"""
        try:
            rp = ResultPayload(SECTION_NAME, **kwargs)

            url = '/'.join((self.options['url'], 'events?customerKey={}'))
            url = url.format(self.apikey)
            self.log.debug(url)

            data, cisco_dstdomain = self.createdataobject(kwargs)

            rc = RequestsCommon(self.opts, self.options)
            content, msg = rc.execute_call_v2("post",
                                              url,
                                              json=data,
                                              verify=False,
                                              headers=HEADERS,
                                              callback=callback)
            if msg:
                yield StatusMessage(msg)
            else:
                yield StatusMessage(
                    u"Add Domain for '{}' was successful".format(
                        cisco_dstdomain))

            results = rp.done(False if msg else True, None if msg else content,
                              msg)
            # backward compatibility
            results['value'] = None if msg else content

            # Produce a FunctionResult with the results
            yield FunctionResult(results)
        except Exception as err:
            self.log.error(err)
            yield FunctionError(err)
Ejemplo n.º 9
0
def selftest_function(opts):
    """
    Simple test to verify McAfee ESM connectivity.
    """
    options = opts.get("fn_mcafee_esm", {})
    try:
        # Instantiate RequestsCommon object
        rc = RequestsCommon(opts=opts, function_opts=options)
        # Check config file and change trust_cert to Boolean
        options = check_config(options)

        url = options["esm_url"] + "/rs/esm/v2/caseGetCaseList"

        headers = get_authenticated_headers(rc, options["esm_url"],
                                            options["esm_username"],
                                            options["esm_password"],
                                            options["trust_cert"])

        r = rc.execute_call_v2('post',
                               url,
                               headers=headers,
                               verify=options["trust_cert"],
                               proxies=rc.get_proxies())
        if r.status_code == 200:
            return {"state": "success", "status_code": r.status_code}
        else:
            return {"state": "failure", "status_code": r.status_code}

    except Exception as e:
        return {"state": "failure", "status_code": e}
Ejemplo n.º 10
0
    def _delete_domain_function(self, event, *args, **kwargs):
        """Function: This is a function implementation that uses the Cisco API to delete a domain from the shared customer’s domain list."""
        try:
            validate_fields(['cisco_domain'], kwargs)

            # Get the function parameters:
            cisco_domain = kwargs.get("cisco_domain")  # text

            self.log.info(u'Deleting {} from list'.format(cisco_domain))
            rp = ResultPayload(SECTION_NAME, **kwargs)

            url = '/'.join(
                (self.options['url'], 'domains', '{}?customerKey={}'))
            url = url.format(cisco_domain.strip(), self.apikey)
            self.log.debug(url)

            rc = RequestsCommon(self.opts, self.options)
            content, msg = rc.execute_call_v2("delete", url, callback=callback)
            if msg:
                yield StatusMessage(msg)
            else:
                yield StatusMessage(
                    u"Delete domain for '{}' was successful".format(
                        cisco_domain))

            # Return the results
            results = rp.done(False if msg else True, None if msg else content,
                              msg)
            # backward compatibility
            results['value'] = None if msg else content
            yield FunctionResult(results)
        except Exception as err:
            self.log.error(err)
            yield FunctionError()
    def delete_app(self, guid, *args, **kwargs):
        """
        Deletes an app.
        :param guid: String
            GUID of the app to delete.
        :return: Information about the app.
        """
        oauth_authorization_headers = {
            "accept": "application/json",
            "Content-Type": "application/json",
            "charset": "utf-8"
        }
        self._add_authentication_headers(oauth_authorization_headers)

        rc = RequestsCommon(self.opts, self.options)

        app_status = {}
        response = rc.execute_call_v2('DELETE',
                                      self.base_url + self.CF_APP.format(guid),
                                      headers=oauth_authorization_headers)
        if response.status_code == 204:  # return status code for update
            log.info("Successful deletion of {}".format(guid))
            app_status["success"] = True
        else:
            app_status["success"] = False
            app_status["details"] = "Couldn't delete."
            app_status["last_updated"] = None
            log.error("Error while deleting cf application {}.".format(guid))
            log.debug(response)
        return app_status
Ejemplo n.º 12
0
def selftest_function(opts):
    """
    Placeholder for selftest function. An example use would be to test package api connectivity.
    Suggested return values are be unimplemented, success, or failure.
    """
    options = opts.get("fn_mxtoolbox", {})

    # Setup the URL request
    url = '/'.join((options['url'], 'mx', "?argument={}&Authorization={}"))
    url = url.format('abc.com', options['api_token'])

    # Make URL request
    rc = RequestsCommon(opts, options)

    state, reason = "", ""
    try:
        response = rc.execute_call_v2("get", url, headers=HEADERS)

        # Check the results
        if response.status_code == 200:
            state = "success"
        else:
            state = "failure"
            reason = response.error
    except Exception as ex:
        state = "failure"
        reason = str(ex)

    result = {"state": state, "reason": reason}

    log.info(result)
    return result
Ejemplo n.º 13
0
def selftest_function(opts):
    """
    Test package api connectivity.
    Return success or failure based in status code
    """
    options = opts.get("fn_pulsedive", {})
    log.info("key: %s, url: %s", options["pulsedive_api_key"],
             options["pulsedive_api_url"])

    # form the url request
    api_url = "{}/search.php?".format(options["pulsedive_api_url"])
    # set generic params for indicator
    pulsedive_data = {"type": all, "risk": all}
    rc = RequestsCommon(opts, options)  # initialize

    state, reason = "", ""

    try:
        resp = rc.execute_call_v2("get", url=api_url, params=pulsedive_data)
        # Check the results
        if resp.status_code == 200:
            state = "success"
        else:
            state = "failure"
            reason = resp.error
    except Exception as err:
        state = "failure"
        reason = str(err)

    result = {"state": state, "reason": reason}

    log.info(result)
    return result
Ejemplo n.º 14
0
def selftest_function(opts):
    """
    Simple test to confirm access to Cisco AMP for endpoint API connectivity.
    """
    options = opts.get(SECTION_NAME, {})
    try:

        url = '/'.join((options['url'], 'events?customerKey={}'))
        url = url.format(options['api_token'])

        test_data = {
            "alertTime": "2013-02-08T11:14:26.0Z",
            "deviceId": "ba6a59f4-e692-4724-ba36-c28132c761de",
            "deviceVersion": "13.7a",
            "dstDomain": "internetbadguys.com",
            "dstUrl": "http://internetbadguys.com/a-bad-url",
            "eventTime": "2013-02-08T09:30:26.0Z",
            "protocolVersion": options.get('protocol_version'),
            "providerName": options.get('provider_name')
        }

        rc = RequestsCommon(opts, options)
        content, msg = rc.execute_call_v2("post",
                                          url,
                                          json=test_data,
                                          verify=False,
                                          headers=HEADERS,
                                          callback=callback)

        return {"state": "failure" if msg else "success", "reason": msg}

    except Exception as e:
        return {"state": "failure", "reason": e}
    def get_app_instances(self, guid, *args, **kwargs):
        """
        Gets instances' information for the app.
        :param guid: String
           Application to get the instances data for.
        :return: Information about all the app instances.
        """
        oauth_authorization_headers = {
            "accept": "application/json",
            "Content-Type": "application/json",
            "charset": "utf-8"
        }
        self._add_authentication_headers(oauth_authorization_headers)

        rc = RequestsCommon(self.opts, self.options)

        app_status = {}
        response = rc.execute_call_v2('GET',
                                      self.base_url +
                                      self.CF_APP_INSTANCES.format(guid),
                                      headers=oauth_authorization_headers)
        if response.status_code == 200:  # return status code for update
            log.info("Got instance information for {}".format(guid))
            app_status = response.json()
            app_status["success"] = True
        else:
            app_status["success"] = False
            app_status["details"] = "Couldn't get instances for {}.".format(
                guid)
            app_status["last_updated"] = None
            log.error(
                "Error getting instance information for {}.".format(guid))
            log.debug(response)
        return app_status
    def get_app_guids(self, application_names):
        """
        Gets the guids for the list of application names provided
        :param application_names: list
             List of names that guid need to be found for
        :return: Dict - key: application name, value: guid
        """
        oauth_authorization_headers = {
            "accept": "application/json",
            "content-type": "application/x-www-form-urlencoded",
            "charset": "utf-8"
        }
        self._add_authentication_headers(oauth_authorization_headers)

        rc = RequestsCommon(self.opts, self.options)

        result = {}

        response = rc.execute_call_v2('GET',
                                      self.base_url + self.CF_ALL_APPS,
                                      headers=oauth_authorization_headers)
        if response.status_code == 200:
            app_resources = response.json()["resources"]
            for resource in app_resources:
                res_app_name = resource["entity"]["name"]
                guid = resource["metadata"]["guid"]
                if res_app_name in application_names:
                    result[res_app_name] = guid
                    # if all the apps have been identified, no need to keep looping
                    if len(result.keys()) == len(application_names):
                        break
        else:
            raise ValueError("Error while getting CF applications metadata")
        return result
    def create_app(self, values):
        """
        Creating an app with the provided values.
        :param values: Values to be passed to the REST call.
        :return:
        """
        if values is None:
            raise ValueError("To create an app proper values are needed")
        oauth_authorization_headers = {
            "accept": "application/json",
            "Content-Type": "application/json",
            "charset": "utf-8"
        }
        self._add_authentication_headers(oauth_authorization_headers)

        rc = RequestsCommon(self.opts, self.options)

        app_status = {}
        response = rc.execute_call_v2('POST',
                                      self.base_url + self.CF_ALL_APPS,
                                      headers=oauth_authorization_headers,
                                      json=values)
        if response.status_code == 201:  # return status code for update
            log.info("Successful creation ")
            app_status = response.json()
            app_status["success"] = True
        else:
            app_status["success"] = False
            app_status["details"] = "Couldn't create."
            app_status["last_updated"] = None
            log.error("Error while creating cf application.")
            log.error(response.json())
            log.debug(response)
        return app_status
Ejemplo n.º 18
0
def selftest_function(opts):
    """
    Simple api test to confirm access
    """
    options = opts.get("fn_geocoding", {})

    url = options['url']
    payload = {
        "key": options['api_key'],
        "latlng": "42.3656119,-71.0805841"
    }

    rc = RequestsCommon(opts, options)
    response = rc.execute_call_v2("get", url, params=payload)
    if response and response.json()['status'] == "OK":
        return {
            "state": "success",
            "response": response.json()
        }
    else:
        log.error(response)
        return {
            "state": "failure",
            "reason": str(response.text)
        }
    def get_app_info(self, guid, *args, **kwargs):
        """
        Extract JSON information for the application with given guid.
        :param guid: String
            GUID for the app to extract information for
        :return:
        """
        oauth_authorization_headers = {
            "accept": "application/json",
            "content-type": "application/json",
            "charset": "utf-8"
        }
        self._add_authentication_headers(oauth_authorization_headers)

        rc = RequestsCommon(self.opts, self.options)

        response = rc.execute_call_v2('GET',
                                      self.base_url +
                                      self.CF_APP_INFO.format(guid),
                                      headers=oauth_authorization_headers)
        if response.status_code == 200:
            apps_data = response.json()
        else:
            log.error("Error while getting CF application information.")
            log.debug(response)
            apps_data = {"status": response.status_code}

        return apps_data
Ejemplo n.º 20
0
    def test_timeout_v2(self):
        URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES,
                        "200?sleep=30000"))

        rc = RequestsCommon(None, None)

        with self.assertRaises(IntegrationError):
            resp = rc.execute_call_v2("get", URL, timeout=2)
Ejemplo n.º 21
0
    def test_v2_resp_type(self):
        IPIFY = TestFunctionRequests.URL_TEST_DATA_RESULTS

        rc = RequestsCommon(None, None)

        # R E S P O N S E  Object
        response = rc.execute_call_v2("get", "{}?format=json".format(IPIFY))

        self.assertTrue(isinstance(response, requests.models.Response))
Ejemplo n.º 22
0
    def test_basicauth_v2(self):
        URL = "/".join(
            (TestFunctionRequests.URL_TEST_HTTP_VERBS, "basic-auth"))
        basicauth = ("postman", "password")

        rc = RequestsCommon(None, None)

        resp = rc.execute_call_v2("get", URL, auth=basicauth)
        self.assertTrue(resp.json().get("authenticated"))
Ejemplo n.º 23
0
    def get_token(self):
        """
        Gets authentication token from the IBM CF's UAA server.
        """

        rc = RequestsCommon(self.opts, self.options)

        response = rc.execute_call_v2('GET', self.base_url)
        if response.status_code == 200:
            # read authorization end point
            auth_url = response.json()["authorization_endpoint"]
            # prepare authorization token endpoint url
            oauth_token_url = "{}/{}".format(auth_url, "oauth/token")
            # request grant_type=password&username=apikey&password=myAPIkey
            oauth_key = {
                "grant_type": "password",
                "username": "******",
                "password": self.auth["apikey"]
            }
            oauth_headers = {
                "content-type": "application/x-www-form-urlencoded",
                "charset": "utf-8",
                "accept": "application/json",
                "authorization": "Basic Y2Y6"
            }

            response = rc.execute_call_v2('POST',
                                          oauth_token_url,
                                          headers=oauth_headers,
                                          data=oauth_key)

            if response.status_code == 200:
                self.oauth_token = response.json()
            else:
                log.error(
                    "Error getting authorization code: status code {}".format(
                        response.status_code))
                log.debug(response)
                raise ValueError(
                    "Couldn't retrieve access token from Bluemix CF")
        else:
            log.error("Error connecting to Bluemix cf api server")
            log.debug(response)
            raise ValueError("Bluemix CF server not responding")
    def _ansible_tower_list_job_templates_function(self, event, *args,
                                                   **kwargs):
        """Function: Run an ansible module outside of the job template"""
        try:
            validate_fields(("url"),
                            self.options)  # validate key app.config settings

            # Get the function parameters:
            tower_hosts = kwargs.get("tower_hosts")  # text
            tower_module = self.get_select_param(
                kwargs.get("tower_module"))  # text
            tower_arguments = kwargs.get("tower_arguments")  # text
            tower_inventory = kwargs.get("tower_inventory")  # number
            tower_credential = kwargs.get("tower_credential")  # number

            log = logging.getLogger(__name__)
            log.info("tower_hosts: %s", tower_hosts)
            log.info("tower_module: %s", tower_module)
            log.info("tower_arguments: %s", tower_arguments)
            log.info("tower_inventory: %s", tower_inventory)
            log.info("tower_credential: %s", tower_credential)

            result = ResultPayload(SECTION_HDR, **kwargs)
            rc = RequestsCommon(self.opts, self.options)

            # PUT YOUR FUNCTION IMPLEMENTATION CODE HERE
            yield StatusMessage("starting...")

            url = "/".join(
                (clean_url(self.options['url']), TOWER_API_BASE, AD_HOC_URL))
            # common
            basic_auth, cafile = get_common_request_items(self.options)

            arguments = {
                "module_name": tower_module,
                "limit": tower_hosts,
                "module_args": tower_arguments,
                "inventory": tower_inventory,
                "credential": tower_credential
            }

            rc = RequestsCommon(self.opts, self.options)
            results = rc.execute_call_v2("post",
                                         url,
                                         auth=basic_auth,
                                         json=arguments,
                                         headers=JSON_HEADERS,
                                         verify=cafile)

            result_payload = result.done(True, results.json())
            yield StatusMessage("done...")

            # Produce a FunctionResult with the results
            yield FunctionResult(result_payload)
        except Exception:
            yield FunctionError()
Ejemplo n.º 25
0
def make_rest_call(opts, options, rest_method, rest_url, headers_dict,
                   cookies_dict, rest_body, rest_verify):
    rc = RequestsCommon(opts, options)

    if CONTENT_TYPE in headers_dict and CONTENT_TYPE_JSON in headers_dict[
            CONTENT_TYPE]:
        return rc.execute_call_v2(rest_method,
                                  rest_url,
                                  headers=headers_dict,
                                  cookies=cookies_dict,
                                  json=rest_body,
                                  verify=rest_verify)

    return rc.execute_call_v2(rest_method,
                              rest_url,
                              headers=headers_dict,
                              cookies=cookies_dict,
                              data=rest_body,
                              verify=rest_verify)
Ejemplo n.º 26
0
class PhishAI(object):
    def __init__(self, opts, options):
        self.rc = RequestsCommon(opts, options)
        self.api_key = options.get("phishai_api_key")
        self.timeout_seconds = int(
            options.get("timeout_seconds", DEFAULT_TIMEOUT))
        self.headers = {'Authorization': self.api_key}
        self.cafile = options.get('cafile')
        self.bundle = os.path.expanduser(self.cafile) if self.cafile else False

    def scan_url(self, url):
        """
        scan_url
        """
        endpoint_url = u"{0}{1}".format(ENDPOINT_URL, ENDPOINT_SCAN_URL)
        response = self.rc.execute_call_v2("post",
                                           endpoint_url,
                                           data={'url': url},
                                           headers=self.headers,
                                           verify=self.bundle,
                                           proxies=self.rc.get_proxies())

        LOG.debug(u"Response: %s", response.text)
        response.raise_for_status()
        return response.json()

    def get_report(self, scan_id):
        """"
        get_report
        """

        endpoint_url = u"{0}{1}{2}".format(ENDPOINT_URL, ENDPOINT_REPORT_URL,
                                           scan_id)
        response = self.rc.execute_call_v2("get",
                                           endpoint_url,
                                           timeout=self.timeout_seconds,
                                           headers=self.headers,
                                           verify=self.bundle,
                                           proxies=self.rc.get_proxies())

        LOG.debug(u"Response: %s", response.text)
        response.raise_for_status()
        return response.json()
Ejemplo n.º 27
0
    def test_statuscode_callback_v2(self):
        URL = "/".join(
            (TestFunctionRequests.URL_TEST_HTTP_STATUS_CODES, "300"))

        def callback(resp):
            if resp.status_code != 300:
                raise ValueError(resp.status_code)

        rc = RequestsCommon(None, None)

        resp = rc.execute_call_v2("get", URL, callback=callback)
Ejemplo n.º 28
0
    def test_proxy_v2(self):
        rc = RequestsCommon()

        proxy_url = TestFunctionRequests.URL_TEST_PROXY
        proxy_result = rc.execute_call_v2("get", proxy_url)
        proxy_result_json = proxy_result.json()

        proxies = {
            'http':
            proxy_result_json['curl']
            if proxy_result_json['protocol'] == 'http' else None,
            'https':
            proxy_result_json['curl']
            if proxy_result_json['protocol'] == 'https' else None
        }

        URL = "?".join(
            (TestFunctionRequests.URL_TEST_DATA_RESULTS, "format=json"))

        # J S O N
        response = rc.execute_call_v2("get", URL, proxies=proxies)
        json_result = response.json()

        self.assertTrue(json_result.get("ip"))

        integrations = {
            "integrations": {
                'http_proxy':
                proxy_result_json['curl']
                if proxy_result_json['protocol'] == 'http' else None,
                'https_proxy':
                proxy_result_json['curl']
                if proxy_result_json['protocol'] == 'https' else None
            }
        }

        rc = RequestsCommon(opts=integrations)
        response = rc.execute_call_v2("get", URL)
        json_result = response.json()
        self.assertTrue(json_result.get("ip"))
Ejemplo n.º 29
0
def selftest_function(opts):
    """
    Placeholder for selftest function. An example use would be to test package api connectivity.
    Suggested return values are be unimplemented, success, or failure.
    """
    # Read option from app.config
    options = opts.get("urlscanio", {})

    # check that an API key and URLs are provided
    validate_fields(['urlscanio_api_key', 'urlscanio_report_url', 'urlscanio_screenshot_url'], options)

    # set API key and report URL
    urlscanio_api_key = options['urlscanio_api_key']
    urlscanio_report_url = options['urlscanio_report_url']

    # Construct the parameters to send to urlscan.io
    urlscanio_headers = {'Content-Type': 'application/json', 'API-Key': urlscanio_api_key}
    urlscanio_data = {
        "url": "https://www.ibm.com"
    }

    # Set URL to make a request to
    urlscanio_scan_url = "{}/scan/".format(urlscanio_report_url)

    # Initialize RequestsCommon
    req_common = RequestsCommon()

    # Attempt the request
    state, reason = "", ""
    try:
        resp = req_common.execute_call_v2("POST", urlscanio_scan_url,
                                          headers=urlscanio_headers,
                                          data=json.dumps(urlscanio_data))
        if resp.status_code == 200:
            state = "success"
            reason = "Connected to urlscanio API"
        else:
            state = "failure"
            reason = "Test failed with http status code {}".format(resp.status_code)

    except Exception as e:
        state = "failure"
        reason = str(e)

    result = {
        "state": state,
        "reason": reason
    }

    log.info(result)

    return result
Ejemplo n.º 30
0
    def test_headers_v2(self):
        # G E T with headers
        headers = {
            "Content-type": "application/json; charset=UTF-8",
            "my-sample-header": "my header"
        }
        URL = "/".join((TestFunctionRequests.URL_TEST_HTTP_VERBS, "headers"))

        rc = RequestsCommon()

        json_result = rc.execute_call_v2("get", URL, headers=headers)
        self.assertEqual(json_result.json()['headers'].get("my-sample-header"),
                         "my header")