Skip to content

Commit f7d8761

Browse files
authored
gdrive: use the normal file backend for saved credentials (#5554)
* gdrive: sync with pydrive's save backend changes * bump pydrive version * better temporary credentials managements for service accounts * proper temp var management
1 parent 70c445d commit f7d8761

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

dvc/fs/gdrive.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import posixpath
55
import re
6+
import tempfile
67
import threading
78
from collections import defaultdict
89
from contextlib import contextmanager
@@ -208,15 +209,25 @@ def _drive(self):
208209
from pydrive2.auth import GoogleAuth
209210
from pydrive2.drive import GoogleDrive
210211

211-
if os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA):
212-
with open(self._gdrive_user_credentials_path, "w") as cred_file:
212+
temporary_save_path = self._gdrive_user_credentials_path
213+
is_credentials_temp = os.getenv(
214+
GDriveFileSystem.GDRIVE_CREDENTIALS_DATA
215+
)
216+
if self._use_service_account:
217+
temporary_save_path = os.path.join(
218+
tempfile.gettempdir(), "google-creds.json"
219+
)
220+
221+
if is_credentials_temp:
222+
with open(temporary_save_path, "w") as cred_file:
213223
cred_file.write(
214224
os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA)
215225
)
216226

217227
auth_settings = {
218228
"client_config_backend": "settings",
219229
"save_credentials": True,
230+
"save_credentials_backend": "file",
220231
"save_credentials_file": self._gdrive_user_credentials_path,
221232
"get_refresh_token": True,
222233
"oauth_scope": [
@@ -226,13 +237,16 @@ def _drive(self):
226237
}
227238

228239
if self._use_service_account:
229-
auth_settings["save_credentials_backend"] = "json"
230240
auth_settings["service_config"] = {
231241
"client_user_email": self._service_account_user_email,
232242
"client_json_file_path": self._service_account_json_file_path,
233243
}
244+
if is_credentials_temp:
245+
auth_settings["service_config"][
246+
"client_json_file_path"
247+
] = temporary_save_path
248+
234249
else:
235-
auth_settings["save_credentials_backend"] = "file"
236250
auth_settings["client_config"] = {
237251
"client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID,
238252
"client_secret": self._client_secret
@@ -267,8 +281,8 @@ def _drive(self):
267281
except Exception as exc:
268282
raise GDriveAuthError(self.credentials_location) from exc
269283
finally:
270-
if os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA):
271-
os.remove(self._gdrive_user_credentials_path)
284+
if is_credentials_temp:
285+
os.remove(temporary_save_path)
272286

273287
return GoogleDrive(gauth)
274288

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def run(self):
9595
# Extra dependencies for remote integrations
9696

9797
gs = ["gcsfs>=0.7.2"]
98-
gdrive = ["pydrive2>=1.7.3", "six >= 1.13.0"]
98+
gdrive = ["pydrive2>=1.8.1", "six >= 1.13.0"]
9999
s3 = ["boto3>=1.9.201"]
100100
azure = ["adlfs>=0.6.3", "azure-identity>=1.4.0", "knack"]
101101
# https://github.com/Legrandin/pycryptodome/issues/465

0 commit comments

Comments
 (0)