feat: enable self signed jwt for service account credentials (#1553)
Enable self signed jwt if google-auth service account credentials are used.
Tested with Storage, Compute and PubSub APIs:
```
import googleapiclient.discovery
project = "<project>"
zone = "us-west1-a"
compute = googleapiclient.discovery.build('compute', 'v1')
result = compute.instances().list(project=project, zone=zone).execute()
print(result)
storage = googleapiclient.discovery.build('storage', 'v1')
result = storage.buckets().list(project=project).execute()
print(result)
topic = "<topic>"
pubsub = googleapiclient.discovery.build('pubsub', 'v1')
result = pubsub.projects().topics().get(topic=f"projects/{project}/topics/{topic}").execute()
print(result)
```
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 9500fbf..2f3f028 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -685,6 +685,33 @@
"credentials.json", scopes=None, quota_project_id=None
)
+ def test_self_signed_jwt_enabled(self):
+ service_account_file_path = os.path.join(DATA_DIR, "service_account.json")
+ creds = google.oauth2.service_account.Credentials.from_service_account_file(service_account_file_path)
+
+ discovery = read_datafile("logging.json")
+
+ with mock.patch("google.oauth2.service_account.Credentials._create_self_signed_jwt") as _create_self_signed_jwt:
+ build_from_document(
+ discovery,
+ credentials=creds,
+ )
+ _create_self_signed_jwt.assert_called_with("https://logging.googleapis.com/")
+
+ def test_self_signed_jwt_disabled(self):
+ service_account_file_path = os.path.join(DATA_DIR, "service_account.json")
+ creds = google.oauth2.service_account.Credentials.from_service_account_file(service_account_file_path)
+
+ discovery = read_datafile("logging.json")
+
+ with mock.patch("google.oauth2.service_account.Credentials._create_self_signed_jwt") as _create_self_signed_jwt:
+ build_from_document(
+ discovery,
+ credentials=creds,
+ always_use_jwt_access=False,
+ )
+ _create_self_signed_jwt.assert_not_called()
+
REGULAR_ENDPOINT = "https://www.googleapis.com/plus/v1/"
MTLS_ENDPOINT = "https://www.mtls.googleapis.com/plus/v1/"