def test_data_passed_through_unchanged(self, data): with mock.patch("uaclient.util.request.urlopen") as m_urlopen: util.readurl("http://some_url", data=data) assert 1 == m_urlopen.call_count req = m_urlopen.call_args[0][0] # the first positional argument assert data == req.data
def identity_doc(self) -> Dict[str, Any]: try: headers = {"Metadata-Flavor": "Google"} url_response, _headers = util.readurl(TOKEN_URL, headers=headers) except HTTPError as e: body = getattr(e, "body", None) error_desc = None if body: try: error_details = json.loads( body, cls=util.DatetimeAwareJSONDecoder) except ValueError: error_details = None if error_details: error_desc = error_details.get("error_description", None) msg = error_desc if error_desc else e.reason msg_code = None if error_desc and "service account" in error_desc.lower(): msg = messages.GCP_SERVICE_ACCT_NOT_ENABLED_ERROR.msg.format( error_msg=msg) msg_code = messages.GCP_SERVICE_ACCT_NOT_ENABLED_ERROR.name raise exceptions.GCPProAccountError(msg=msg, msg_code=msg_code, code=getattr(e, "code", 0)) return {"identityToken": url_response}
def request_url(self, path, data=None, headers=None, method=None): path = path.lstrip("/") if not headers: headers = self.headers() if headers.get("content-type") == "application/json" and data: data = json.dumps(data).encode("utf-8") url = urljoin(getattr(self.cfg, self.cfg_url_base_attr), path) try: response, headers = util.readurl(url=url, data=data, headers=headers, method=method) except error.URLError as e: if hasattr(e, "read"): try: error_details = json.loads(e.read().decode("utf-8")) except ValueError: error_details = None if error_details: raise self.api_error_cls(e, error_details) raise util.UrlError(e, code=getattr(e, "code", None), headers=headers, url=url) return response, headers
def identity_doc(self) -> "Dict[str, Any]": responses = {} for key, url in sorted(IMDS_URLS.items()): url_response, _headers = util.readurl(url, headers={"Metadata": "true"}) if key == "pkcs7": responses[key] = url_response["signature"] else: responses[key] = url_response return responses
def request_url( self, path, data=None, headers=None, method=None, query_params=None, potentially_sensitive: bool = True, ): path = path.lstrip("/") if not headers: headers = self.headers() if headers.get("content-type") == "application/json" and data: data = json.dumps(data).encode("utf-8") url = urljoin(getattr(self.cfg, self.cfg_url_base_attr), path) fake_response, fake_headers = self._get_fake_responses(url) if fake_response: return fake_response, fake_headers # URL faked by uaclient.conf if query_params: # filter out None values filtered_params = { k: v for k, v in sorted(query_params.items()) if v is not None } url += "?" + urlencode(filtered_params) try: response, headers = util.readurl( url=url, data=data, headers=headers, method=method, timeout=self.url_timeout, potentially_sensitive=potentially_sensitive, ) except error.URLError as e: body = None if hasattr(e, "body"): body = e.body # type: ignore elif hasattr(e, "read"): body = e.read().decode("utf-8") # type: ignore if body: try: error_details = json.loads( body, cls=util.DatetimeAwareJSONDecoder) except ValueError: error_details = None if error_details: raise self.api_error_cls(e, error_details) raise exceptions.UrlError(e, code=getattr(e, "code", None), headers=headers, url=url) return response, headers
def request_url(self, path, data=None, headers=None, method=None): path = path.lstrip('/') if not headers: headers = self.headers() if headers.get('content-type') == 'application/json' and data: data = json.dumps(data).encode('utf-8') url = urljoin(getattr(self.cfg, self.cfg_url_base_attr), path) try: response, headers = util.readurl(url=url, data=data, headers=headers, method=method) except error.URLError as e: code = e.errno if hasattr(e, 'read'): error_details = util.maybe_parse_json(e.read().decode('utf-8')) if error_details: raise self.api_error_cls(e, error_details) raise util.UrlError(e, code=code, headers=headers, url=url) return response, headers
def _get_imds_v2_token_headers(self, ip_address): if self._api_token == "IMDSv1": return None elif self._api_token: return {AWS_TOKEN_PUT_HEADER: self._api_token} try: response, _headers = util.readurl( IMDS_V2_TOKEN_URL.format(ip_address), method="PUT", headers={AWS_TOKEN_REQ_HEADER: AWS_TOKEN_TTL_SECONDS}, timeout=1, ) except HTTPError as e: if e.code == 404: self._api_token = "IMDSv1" return None else: raise self._api_token = response return {AWS_TOKEN_PUT_HEADER: self._api_token}
def request_url(self, path, data=None, headers=None, method=None): if path[0] != '/': path = '/' + path if not headers: headers = self.headers() if headers.get('content-type') == 'application/json' and data: data = util.encode_text(json.dumps(data)) url = getattr(self.cfg, self.cfg_url_base_attr) + path try: response, headers = util.readurl(url=url, data=data, headers=headers, method=method) except error.URLError as e: code = e.errno if hasattr(e, 'read'): error_details = util.maybe_parse_json(e.read()) if error_details: raise self.api_error_cls(e, error_details) raise util.UrlError(e, code=code, headers=headers, url=url) return response, headers
def is_pro_license_present(self, *, wait_for_change: bool) -> bool: url = LICENSES_URL if wait_for_change: url += WAIT_FOR_CHANGE if self.etag: url += LAST_ETAG.format(etag=self.etag) try: licenses, headers = util.readurl( url, headers={"Metadata-Flavor": "Google"}) except HTTPError as e: LOG.error(e) if e.code == 400: raise exceptions.CancelProLicensePolling() else: raise exceptions.DelayProLicensePolling() license_ids = [license["id"] for license in licenses] self.etag = headers.get("ETag", None) series = util.get_platform_info()["series"] return GCP_LICENSES.get(series) in license_ids
def identity_doc(self): response, _headers = util.readurl(IMDS_URL) return response
def _get_imds_url_response(self): headers = self._request_imds_v2_token_headers() return util.readurl( IMDS_URL.format(self._ip_address), headers=headers, timeout=1 )
def identity_doc(self) -> "Dict[str, Any]": response, _headers = util.readurl(IMDS_URL) return response
def test_simple_call_with_url_works(self): with mock.patch("uaclient.util.request.urlopen") as m_urlopen: util.readurl("http://some_url") assert 1 == m_urlopen.call_count
def identity_doc(self) -> "Dict[str, Any]": url_response, _headers = util.readurl( TOKEN_URL, headers={"Metadata-Flavor": "Google"}) return {"identityToken": url_response}