Skip to content

Commit 6eaf2db

Browse files
authored
Use gcp sdk provided flow for obtaininng application default credenti… (#430)
* Use gcp sdk provided flow for obtaininng application default credentials (#429) * Update instances.py * Add missing request builder * Fix tests * Fix tests * Fix tests * Fix tests * Fix tests * Add right error handling
1 parent b2d8237 commit 6eaf2db

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

dask_cloudprovider/gcp/instances.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import asyncio
2-
import os
32
import uuid
43
import json
54

6-
import sqlite3
75
from typing import Optional, Any, Dict
86

97
import dask
10-
from dask.utils import tmpfile
118
from dask_cloudprovider.generic.vmcluster import (
129
VMCluster,
1310
VMInterface,
@@ -628,9 +625,11 @@ def __init__(
628625
"gpu_instance": self.gpu_instance,
629626
"bootstrap": self.bootstrap,
630627
"auto_shutdown": self.auto_shutdown,
631-
"preemptible": preemptible
632-
if preemptible is not None
633-
else self.config.get("preemptible"),
628+
"preemptible": (
629+
preemptible
630+
if preemptible is not None
631+
else self.config.get("preemptible")
632+
),
634633
"instance_labels": instance_labels or self.config.get("instance_labels"),
635634
"service_account": service_account or self.config.get("service_account"),
636635
}
@@ -658,37 +657,30 @@ def __init__(self, service_account_credentials: Optional[dict[str, Any]] = None)
658657
self._compute = self.refresh_client()
659658

660659
def refresh_client(self):
661-
if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", False):
660+
if self.service_account_credentials:
662661
import google.oauth2.service_account # google-auth
663662

664-
creds = google.oauth2.service_account.Credentials.from_service_account_file(
665-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"],
666-
scopes=["https://www.googleapis.com/auth/cloud-platform"],
667-
)
668-
elif self.service_account_credentials:
669-
import google.oauth2.service_account # google-auth
670-
671-
creds = google.oauth2.service_account.Credentials.from_service_account_info(
672-
self.service_account_credentials,
673-
scopes=["https://www.googleapis.com/auth/cloud-platform"],
663+
credentials = (
664+
google.oauth2.service_account.Credentials.from_service_account_info(
665+
self.service_account_credentials,
666+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
667+
)
674668
)
675669
else:
676-
import google.auth.credentials # google-auth
670+
import google.auth
677671

678-
path = os.path.join(
679-
os.path.expanduser("~"), ".config/gcloud/credentials.db"
680-
)
681-
if not os.path.exists(path):
682-
raise GCPCredentialsError()
683-
conn = sqlite3.connect(path)
684-
creds_rows = conn.execute("select * from credentials").fetchall()
685-
with tmpfile() as f:
686-
with open(f, "w") as f_:
687-
# take first row
688-
f_.write(creds_rows[0][1])
689-
creds, _ = google.auth.load_credentials_from_file(filename=f)
672+
# Obtain Application Default Credentials (ADC)
673+
try:
674+
credentials, _ = google.auth.default()
675+
except google.auth.exceptions.DefaultCredentialsError as e:
676+
raise GCPCredentialsError() from e
677+
678+
# Use the credentials to build a service client
690679
return googleapiclient.discovery.build(
691-
"compute", "v1", credentials=creds, requestBuilder=build_request(creds)
680+
"compute",
681+
"v1",
682+
credentials=credentials,
683+
requestBuilder=build_request(credentials),
692684
)
693685

694686
def instances(self):

0 commit comments

Comments
 (0)