def _WriteHostKeyToGuestAttributes(self, key_type, key_value): """Write a host key to guest attributes, ignoring errors.""" headers = {'Metadata-Flavor': 'Google'} url = '%s/%s/%s' % (GUEST_ATTRIBUTES_URL, HOSTKEY_NAMESPACE, key_type) req = PutRequest(url, key_value, headers) try: response = urlrequest.urlopen(req) self.logger.debug(response) self.logger.info('Wrote %s host key to guest attributes.', key_type) except urlerror.HTTPError: self.logger.info('Unable to write %s host key to guest attributes.', key_type)
def _DownloadAuthUrl(self, url, dest_dir): """Download a Google Storage URL using an authentication token. If the token cannot be fetched, fallback to unauthenticated download. Args: url: string, the URL to download. dest_dir: string, the path to a directory for storing metadata scripts. Returns: string, the path to the file storing the metadata script. """ dest_file = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False) dest_file.close() dest = dest_file.name self.logger.info( 'Downloading url from %s to %s using authentication token.', url, dest) if not self.token: response = self.watcher.GetMetadata(self.token_metadata_key, recursive=False, retry=False) if not response: self.logger.info( 'Authentication token not found. Attempting unauthenticated ' 'download.') return self._DownloadUrl(url, dest_dir) self.token = '%s %s' % (response.get( 'token_type', ''), response.get('access_token', '')) try: request = urlrequest.Request(url) request.add_unredirected_header('Metadata-Flavor', 'Google') request.add_unredirected_header('Authorization', self.token) content = urlrequest.urlopen(request).read() except (httpclient.HTTPException, socket.error, urlerror.URLError) as e: self.logger.warning('Could not download %s. %s.', url, str(e)) return None with open(dest, 'w') as f: f.write(content) return dest
def _DownloadAuthUrl(self, url, dest_dir): """Download a Google Storage URL using an authentication token. If the token cannot be fetched, fallback to unauthenticated download. Args: url: string, the URL to download. dest_dir: string, the path to a directory for storing metadata scripts. Returns: string, the path to the file storing the metadata script. """ dest_file = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False) dest_file.close() dest = dest_file.name self.logger.info( 'Downloading url from %s to %s using authentication token.', url, dest) if not self.token: response = self.watcher.GetMetadata( self.token_metadata_key, recursive=False, retry=False) if not response: self.logger.info( 'Authentication token not found. Attempting unauthenticated ' 'download.') return self._DownloadUrl(url, dest_dir) self.token = '%s %s' % ( response.get('token_type', ''), response.get('access_token', '')) try: request = urlrequest.Request(url) request.add_unredirected_header('Metadata-Flavor', 'Google') request.add_unredirected_header('Authorization', self.token) content = urlrequest.urlopen(request).read().decode('utf-8') except (httpclient.HTTPException, socket.error, urlerror.URLError) as e: self.logger.warning('Could not download %s. %s.', url, str(e)) return None with open(dest, 'wb') as f: f.write(content) return dest
def _GetMetadataRequest(self, metadata_url, params=None): """Performs a GET request with the metadata headers. Args: metadata_url: string, the URL to perform a GET request on. params: dictionary, the query parameters in the GET request. Returns: HTTP response from the GET request. Raises: urlerror.HTTPError: raises when the GET request fails. """ headers = {'Metadata-Flavor': 'Google'} params = urlparse.urlencode(params or {}) url = '%s?%s' % (metadata_url, params) request = urlrequest.Request(url, headers=headers) return urlrequest.urlopen(request, timeout=self.timeout * 1.1)
def _GetMetadataRequest(self, metadata_url, params=None): """Performs a GET request with the metadata headers. Args: metadata_url: string, the URL to perform a GET request on. params: dictionary, the query parameters in the GET request. Returns: HTTP response from the GET request. Raises: urlerror.HTTPError: raises when the GET request fails. """ headers = {'Metadata-Flavor': 'Google'} params = urlparse.urlencode(params or {}) url = '%s?%s' % (metadata_url, params) request = urlrequest.Request(url, headers=headers) return urlrequest.urlopen(request, timeout=self.timeout*1.1)
def _UrlOpenWithRetry(request): """Call urlopen with retry.""" return urlrequest.urlopen(request)