Skip to content

Commit

Permalink
Use gcp sdk provided flow for obtaininng application default credenti…
Browse files Browse the repository at this point in the history
…als (dask#429)
  • Loading branch information
dbalabka authored Jul 10, 2024
1 parent 5babf64 commit 1eff8a9
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions dask_cloudprovider/gcp/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

try:
import googleapiclient.discovery
import google.auth
from googleapiclient.errors import HttpError
except ImportError as e:
msg = (
Expand Down Expand Up @@ -647,31 +648,11 @@ def __init__(self):
self._compute = self.refresh_client()

def refresh_client(self):
if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", False):
import google.oauth2.service_account # google-auth
# Obtain Application Default Credentials
credentials, _ = google.auth.default()

creds = google.oauth2.service_account.Credentials.from_service_account_file(
os.environ["GOOGLE_APPLICATION_CREDENTIALS"],
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
else:
import google.auth.credentials # google-auth

path = os.path.join(
os.path.expanduser("~"), ".config/gcloud/credentials.db"
)
if not os.path.exists(path):
raise GCPCredentialsError()
conn = sqlite3.connect(path)
creds_rows = conn.execute("select * from credentials").fetchall()
with tmpfile() as f:
with open(f, "w") as f_:
# take first row
f_.write(creds_rows[0][1])
creds, _ = google.auth.load_credentials_from_file(filename=f)
return googleapiclient.discovery.build(
"compute", "v1", credentials=creds, requestBuilder=build_request(creds)
)
# Use the credentials to build a service client
return googleapiclient.discovery.build('compute', 'v1', credentials=credentials)

def instances(self):
try:
Expand Down

0 comments on commit 1eff8a9

Please sign in to comment.