def connect(self, params={}): self.logger.info("Connect: Configuring REST details") base_url = params.get(Input.BASE_URL) default_headers = params.get(Input.DEFAULT_HEADERS, {}) self.authentication_type = params.get(Input.AUTHENTICATION_TYPE) username = None password = None secret_key = None if self.authentication_type: if self.authentication_type == "Basic Auth" or self.authentication_type == "Digest Auth": username = params.get(Input.BASIC_AUTH_CREDENTIALS).get("username") password = params.get(Input.BASIC_AUTH_CREDENTIALS).get('password') if not username or not password: raise PluginException( cause="Basic Auth authentication selected without providing username and password.", assistance="The authentication type requires a username and password." " Please complete the connection with a username and password or change the authentication type." ) else: secret_key = params.get(Input.SECRET).get("secretKey") if not secret_key: raise PluginException( cause="An authentication type was selected that requires a secret key.", assistance="Please complete the connection with a secret key or change the authentication type." ) if self.authentication_type == "Basic Auth": self.auth = HTTPBasicAuth(username, password) elif self.authentication_type == "Digest Auth": self.auth = HTTPDigestAuth(username, password) elif self.authentication_type == "Bearer Token": default_headers = Common.merge_dicts(default_headers, {'Authorization': f'Bearer {secret_key}'}) elif self.authentication_type == "Rapid7 Insight": default_headers = Common.merge_dicts(default_headers, {'X-Api-Key': secret_key}) elif self.authentication_type == "OpsGenie": default_headers = Common.merge_dicts(default_headers, {'Authorization': f'GenieKey {secret_key}'}) elif self.authentication_type == "Pendo": default_headers = Common.merge_dicts( default_headers, { "content-type": "application/json", "x-pendo-integration-key": secret_key } ) elif self.authentication_type == "Custom": new_headers = {} for key, value in default_headers.items(): if value == self.CUSTOM_SECRET_INPUT: new_headers[key] = secret_key else: new_headers[key] = value default_headers = new_headers self.base_url = base_url self.default_headers = default_headers self.ssl_verify = params.get("ssl_verify") self.logger.info("Connect: Connecting..")
def run(self, params={}): route = params.get("route") headers = params.get("headers", {}) body = params.get("body", {}) req_headers = Common.merge_dicts(self.connection.default_headers, headers) url = requests.compat.urljoin(self.connection.base_url, route) response = requests.patch(url, headers=req_headers, data=body, verify=self.connection.ssl_verify) body_object = {} try: body_object = response.json() except ValueError: """ Nothing? We don't care if it fails, that could be normal """ resp_headers = Common.copy_dict(response.headers) return { 'body_object': body_object, 'body_string': response.text, 'status': response.status_code, 'headers': resp_headers, }
def run(self, params={}): route = params.get("route") headers = params.get("headers", {}) body = params.get("body", {}) req_headers = Common.merge_dicts(self.connection.default_headers, headers) url = parse.urljoin(self.connection.base_url, route) response = requests.post(url, headers=req_headers, json=body, verify=self.connection.ssl_verify) body_object = {} try: body_object = response.json() except ValueError: """ Nothing? We don't care if it fails, that could be normal """ # It's possible to have a successful call with no body # https://stackoverflow.com/questions/32319845/python-requests-gives-none-response-where-json-data-is-expected if body_object == None: body_object = {} resp_headers = Common.copy_dict(response.headers) return { 'body_object': body_object, 'body_string': response.text, 'status': response.status_code, 'headers': resp_headers, }
def connect(self, params): self.logger.info("Connect: Configuring REST details") base_url = params.get("base_url") default_headers = params.get("default_headers", {}) username = params.get('basic_auth_credentials', {}).get("username") password = params.get("basic_auth_credentials", {}).get('password') assert base_url is not None and len(base_url) > 0, \ "Connect: Property 'base_url' was None or 0 length. Make sure it is marked required." if username and password: credentials = '{}:{}'.format(username, password).encode() auth = 'Basic {}'.format( base64.encodebytes(credentials).decode().replace('\n', '')) default_headers = Common.merge_dicts(default_headers, {'Authorization': auth}) self.base_url = base_url self.default_headers = default_headers self.ssl_verify = params.get("ssl_verify") self.logger.info("Connect: Connecting..")