Skip to content

Commit

Permalink
feat: add cred info to auth error details
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed Aug 29, 2024
1 parent c440807 commit e909f85
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e

{% endif %}

Expand Down Expand Up @@ -105,12 +109,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}

{% if "DeleteOperation" in api.mixin_api_methods %}
Expand Down Expand Up @@ -278,12 +286,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}
{% endif %} {# LRO #}

Expand Down Expand Up @@ -405,12 +417,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}

{% if "GetIamPolicy" in api.mixin_api_methods %}
Expand Down Expand Up @@ -528,12 +544,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}

{% if "TestIamPermissions" in api.mixin_api_methods %}
Expand Down Expand Up @@ -589,12 +609,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}
{% endif %}

Expand Down Expand Up @@ -649,12 +673,16 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}

{% if "ListLocations" in api.mixin_api_methods %}
Expand Down Expand Up @@ -705,11 +733,15 @@
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from collections import OrderedDict
{% if service.any_extended_operations_methods %}
import functools
{% endif %}
from http import HTTPStatus
import json
import os
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union, cast
Expand Down Expand Up @@ -417,6 +419,24 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ service.client_name }}._compare_universes(self.universe_domain, self.transport._credentials))
return self._is_universe_domain_valid

def _add_cred_info_for_auth_errors(self, error):
"""Adds credential info string to error details for 401/403/404 errors.

Args:
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.

Returns:
google.api_core.exceptions.GoogleAPICallError: The error with cred info added.
"""
if error.code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN, HTTPStatus.NOT_FOUND]:
cred = self._transport._credentials
cred_info = None
if hasattr(cred, "get_cred_info"):
cred_info = cred.get_cred_info()
if cred_info and hasattr(error, "_details") and error._details:
error._details.append(json.dumps(cred_info))
return error

@property
def api_endpoint(self):
"""Return the API endpoint used by the client instance.
Expand Down Expand Up @@ -706,12 +726,16 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e

def get_iam_policy(
self,
Expand Down Expand Up @@ -827,12 +851,16 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e

def test_iam_permissions(
self,
Expand Down Expand Up @@ -886,12 +914,16 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)
try:
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
e = self._add_cred_info_for_auth_errors(e)
raise e
{% endif %}

DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(gapic_version=package_version.__version__)
Expand Down

0 comments on commit e909f85

Please sign in to comment.