Example #1
0
    def _check_content_type(self, url, content_type):
        media_type = cgi.parse_header(content_type)[0]

        if media_type.lower() != PEM_FILE_TYPE.lower():
            raise PublicKeyRetrieverException(
                "Invalid content-type, '%s', for url '%s' ." %
                (content_type, url))
Example #2
0
 def __init__(self, base_url):
     if base_url is None or not base_url.startswith('https://'):
         raise PublicKeyRetrieverException(
             'The base url must start with https://')
     if not base_url.endswith('/'):
         base_url += '/'
     self.base_url = base_url
     self._session = self._get_session()
Example #3
0
 async def _retrieve(self, url, requests_kwargs):
     try:
         resp = await self._session.get(url,
                                        headers={'accept': PEM_FILE_TYPE},
                                        **requests_kwargs)
         resp.raise_for_status()
         self._check_content_type(url, resp.headers['content-type'])
         return await resp.text()
     except aiohttp.ClientError as e:
         status_code = getattr(e, 'code', None)
         raise PublicKeyRetrieverException(e, status_code=status_code)
 def retrieve(self, key_identifier, **requests_kwargs):
     for retriever in self._retrievers:
         try:
             return retriever.retrieve(key_identifier, **requests_kwargs)
         except RequestException as e:
             logger = logging.getLogger(__name__)
             logger.warn('Unable to retrieve public key from store',
                         extra={
                             'underlying_error': str(e),
                             'key repository': retriever.base_url
                         })
     raise PublicKeyRetrieverException(
         'Cannot load key from key repositories')
Example #5
0
    def retrieve(self, key_identifier, **requests_kwargs):
        """ returns the public key for given key_identifier. """
        if not isinstance(key_identifier, KeyIdentifier):
            key_identifier = KeyIdentifier(key_identifier)

        url = self.base_url + key_identifier.key_id
        try:
            return self._retrieve(url, requests_kwargs)
        except requests.RequestException as e:
            try:
                status_code = e.response.status_code
            except AttributeError:
                status_code = None
            raise PublicKeyRetrieverException(e, status_code=status_code)