示例#1
0
 def refresh(self, request):
     try:
         client = UserSecretsClient()
         if self.target == GcpTarget.BIGQUERY:
             self.token, self.expiry = client.get_bigquery_access_token()
         elif self.target == GcpTarget.GCS:
             self.token, self.expiry = client._get_gcs_access_token()
         elif self.target == GcpTarget.CLOUDAI:
             self.token, self.expiry = client._get_cloudai_access_token()
     except ConnectionError as e:
         Log.error(f"Connection error trying to refresh access token: {e}")
         print(
             "There was a connection error trying to fetch the access token. "
             f"Please ensure internet is on in order to use the {self.target.service} Integration."
         )
         raise RefreshError(
             'Unable to refresh access token due to connection error.'
         ) from e
     except Exception as e:
         Log.error(f"Error trying to refresh access token: {e}")
         if (not get_integrations().has_integration(self.target)):
             Log.error(f"No {self.target.service} integration found.")
             print(
                 f"Please ensure you have selected a {self.target.service} account in the Notebook Add-ons menu."
             )
         raise RefreshError('Unable to refresh access token.') from e
示例#2
0
  def testCreateHttpHeader_refreshFails(self):
    self.mock_credentials_instance.refresh.side_effect = RefreshError(
        'Invalid response 400')

    with mock.patch('google.auth.transport.requests.Request', self.mock_req):
      self.assertRaises(RefreshError, self.sa_client.CreateHttpHeader)
      self.assertFalse(self.mock_credentials_instance.apply.called)
 def refresh(self, request):
     print("Calling Kaggle.UserSecrets to refresh token.")
     try:
         client = UserSecretsClient()
         fresh_token = client.get_bigquery_access_token()
     except Exception as e:
         raise RefreshError('Unable to refresh access token.') from e
示例#4
0
 def refresh(self, request):
     try:
         client = UserSecretsClient()
         self.token, self.expiry = client.get_bigquery_access_token()
     except ConnectionError as e:
         Log.error(f"Connection error trying to refresh access token: {e}")
         print("There was a connection error trying to fetch the access token. "
               "Please ensure internet is on in order to use the BigQuery Integration.")
         raise RefreshError('Unable to refresh access token due to connection error.') from e
     except Exception as e:
         Log.error(f"Error trying to refresh access token: {e}")
         if (not get_integrations().has_bigquery()):
             Log.error(f"No bigquery integration found.")
             print(
                 'Please ensure you have selected a BigQuery account in the Kernels Settings sidebar.')
         raise RefreshError('Unable to refresh access token.') from e
示例#5
0
 def test_refresh_reraises_on_error(self, mock_google_auth):
     mock_auth = MagicMock()
     mock_auth.valid = False
     mock_auth.refresh.side_effect = RefreshError('Unable to refresh token')
     mock_google_auth.default = MagicMock(return_value=(mock_auth,
                                                        TEST_PROJECT))
     with self.assertRaises(RefreshError):
         handlers.AuthProvider.get()
示例#6
0
    def test_upload_failed_due_to_authentication_errors(self):
        """Test Authenticating related error occurs."""
        events = [copy.deepcopy(_event_test_conversion)]
        self.mock_service.mutate.side_effect = RefreshError(
            'invalid_client: Unauthorized')
        blb = blob.Blob(events=events, location='')

        blb = self.test_hook.send_events(blb)

        self.assertEqual(len(blb.failed_events), 1)
示例#7
0
 def refresh(self, request):
     try:
         client = UserSecretsClient()
         self.token, self.expiry = client.get_bigquery_access_token()
     except Exception as e:
         if (not get_integrations().has_bigquery()):
             print(
                 'Please ensure you have selected a BigQuery account in the Kernels Settings sidebar.'
             )
         raise RefreshError('Unable to refresh access token.') from e
 def refresh(self, request):
     try:
         client = UserSecretsClient()
         if self.target == GcpTarget.BIGQUERY:
             self.token, self.expiry = client.get_bigquery_access_token()
         elif self.target == GcpTarget.GCS:
             self.token, self.expiry = client._get_gcs_access_token()
         elif self.target == GcpTarget.AUTOML:
             self.token, self.expiry = client._get_automl_access_token()
         elif self.target == GcpTarget.TRANSLATION:
             self.token, self.expiry = client._get_translation_access_token(
             )
         elif self.target == GcpTarget.NATURAL_LANGUAGE:
             self.token, self.expiry = client._get_natural_language_access_token(
             )
         elif self.target == GcpTarget.VIDEO_INTELLIGENCE:
             self.token, self.expiry = client._get_video_intelligence_access_token(
             )
         elif self.target == GcpTarget.VISION:
             self.token, self.expiry = client._get_vision_access_token()
     except ConnectionError as e:
         Log.error(f"Connection error trying to refresh access token: {e}")
         print(
             "There was a connection error trying to fetch the access token. "
             f"Please ensure internet is on in order to use the {self.target.service} Integration."
         )
         raise RefreshError(
             'Unable to refresh access token due to connection error.'
         ) from e
     except Exception as e:
         Log.error(f"Error trying to refresh access token: {e}")
         if (not get_integrations().has_integration(self.target)):
             Log.error(f"No {self.target.service} integration found.")
             print(
                 f"Please ensure you have selected a {self.target.service} account in the Notebook Add-ons menu."
             )
         raise RefreshError('Unable to refresh access token.') from e
示例#9
0
 def refresh(self, request):
     """Refreshes the access token.
     Args:
         request (google.auth.transport.Request): The object used to make
             HTTP requests.
     Raises:
         google.auth.exceptions.RefreshError: If the credentials could
             not be refreshed.
     """
     usa = self.usa
     try:
         usa.refresh_token(load_strategy())
     except requests_errors.HTTPError as e:
         raise RefreshError(e)
     data = usa.extra_data
     self.token = data['access_token']
     self._refresh_token = None
     self.expiry = self._parse_expiry(data)
示例#10
0
 def _obtain_accounts(service) -> List[str]:
     """Get a list of all Google Analytics accounts for this user
     Args:
         service: A service that is connected to the specified API.
     Returns:
         a list of all account_ids obtained
     """
     try:
         query = service.management().accounts().list()
         response = query.execute()
     except TypeError as error:
         # Handle errors in constructing a query.
         logging.error(
             f"There was an error in constructing your query : {error}")
         raise TypeError(error)
     except HttpError as error:
         # Handle API errors.
         logging.error("Arg, there was an API error : {} : {}".format(
             error.resp.status, error._get_reason()))
         raise HttpError(error)
     except RefreshError:
         # Handle Auth errors.
         message = (
             "The credentials have been revoked or expired, please re-run "
             "the application to re-authorize")
         logging.error(message)
         raise RefreshError(message)
     except Exception as e:
         # Handle all other errors.
         raise Exception(e)
     else:
         if response.get("items"):
             # Get all Google Analytics account.
             return [i["id"] for i in response["items"]]
         logging.warning("no Google Analytics accounts detected\n" * 5)
         return []
 def query_raiser(self, query, **kwargs):
     self.number_of_calls += 1
     if self.number_of_calls < self.raise_after:
         return self.response
     else:
         raise RefreshError()
示例#12
0
 def refresh(self, request):
     try:
         client = UserSecretsClient()
         self.token, self.expiry = client.get_bigquery_access_token()
     except Exception as e:
         raise RefreshError('Unable to refresh access token.') from e