Beispiel #1
0
    def test_07_ServiceAuthFromSavedCredentialsJsonFile(self):
        # Have an initial auth so that credentials/7.dat gets saved
        ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml"))
        ga.ServiceAuth()
        self.assertTrue(os.path.exists(ga.settings["save_credentials_file"]))

        # Secondary auth should be made only using the previously saved
        # login info
        ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml"))
        ga.ServiceAuth()

        self.assertEqual(ga.access_token_expired, False)
        time.sleep(1)
Beispiel #2
0
    def test_10_Files_Download_Service(self):
        """
        Tests that a fresh GoogleDrive object can correctly authenticate
        and download from a file ID.
        """
        drive = GoogleDrive(self.ga)
        file1 = drive.CreateFile()
        filename = self.getTempFile("prepatchtestfile")
        content = "hello world!"

        file1["title"] = filename
        file1.SetContentString(content)
        pydrive_retry(file1.Upload)  # Files.insert
        self.assertEqual(file1.metadata["title"], filename)
        fileOut1 = self.getTempFile()
        pydrive_retry(file1.GetContentFile, fileOut1)

        # fresh download-only instance
        auth = GoogleAuth(
            settings_file_path("default.yaml", os.path.join(self.tmpdir, "")))
        auth.ServiceAuth()
        drive2 = GoogleDrive(auth)
        file2 = drive2.CreateFile({"id": file1["id"]})
        fileOut2 = self.getTempFile()
        pydrive_retry(file2.GetContentFile, fileOut2)
        self.assertEqual(filecmp.cmp(fileOut1, fileOut2), True)

        self.DeleteUploadedFiles(drive, [file1["id"]])
Beispiel #3
0
    def _drive(self):
        from pydrive2.auth import RefreshError
        from pydrive2.auth import GoogleAuth
        from pydrive2.drive import GoogleDrive

        if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA):
            with open(self._gdrive_user_credentials_path,
                      "w") as credentials_file:
                credentials_file.write(
                    os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA))

        GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings"
        if self._use_service_account:
            GoogleAuth.DEFAULT_SETTINGS["service_config"] = {
                "client_service_email": self._service_account_email,
                "client_user_email": self._service_account_user_email,
                "client_pkcs12_file_path": self._service_account_p12_file_path,
            }
        else:
            GoogleAuth.DEFAULT_SETTINGS["client_config"] = {
                "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID,
                "client_secret": self._client_secret
                or self.DEFAULT_GDRIVE_CLIENT_SECRET,
                "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                "token_uri": "https://oauth2.googleapis.com/token",
                "revoke_uri": "https://oauth2.googleapis.com/revoke",
                "redirect_uri": "",
            }
        GoogleAuth.DEFAULT_SETTINGS["save_credentials"] = True
        GoogleAuth.DEFAULT_SETTINGS["save_credentials_backend"] = "file"
        GoogleAuth.DEFAULT_SETTINGS[
            "save_credentials_file"] = self._gdrive_user_credentials_path
        GoogleAuth.DEFAULT_SETTINGS["get_refresh_token"] = True
        GoogleAuth.DEFAULT_SETTINGS["oauth_scope"] = [
            "https://www.googleapis.com/auth/drive",
            "https://www.googleapis.com/auth/drive.appdata",
        ]

        # Pass non existent settings path to force DEFAULT_SETTINGS loading
        gauth = GoogleAuth(settings_file="")

        try:
            if self._use_service_account:
                gauth.ServiceAuth()
            else:
                gauth.CommandLineAuth()
        except RefreshError as exc:
            raise GDriveAccessTokenRefreshError from exc
        except KeyError as exc:
            raise GDriveMissedCredentialKeyError(
                self._gdrive_user_credentials_path) from exc
        # Handle pydrive2.auth.AuthenticationError and other auth failures
        except Exception as exc:
            raise DvcException("Google Drive authentication failed") from exc
        finally:
            if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA):
                os.remove(self._gdrive_user_credentials_path)

        return GoogleDrive(gauth)
Beispiel #4
0
def fs(tmpdir, base_remote_dir):
    setup_credentials()
    auth = GoogleAuth(settings_file_path("default.yaml", tmpdir / ""))
    auth.ServiceAuth()

    bucket, base = base_remote_dir.split("/", 1)
    fs = GDriveFileSystem(base_remote_dir, auth)
    fs._gdrive_create_dir("root", base)

    return fs
Beispiel #5
0
    def _drive(self):
        from pydrive2.auth import GoogleAuth
        from pydrive2.drive import GoogleDrive

        if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA):
            with open(self._gdrive_user_credentials_path, "w") as cred_file:
                cred_file.write(os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA))

        auth_settings = {
            "client_config_backend": "settings",
            "save_credentials": True,
            "save_credentials_backend": "file",
            "save_credentials_file": self._gdrive_user_credentials_path,
            "get_refresh_token": True,
            "oauth_scope": [
                "https://www.googleapis.com/auth/drive",
                "https://www.googleapis.com/auth/drive.appdata",
            ],
        }

        if self._use_service_account:
            auth_settings["service_config"] = {
                "client_service_email": self._service_account_email,
                "client_user_email": self._service_account_user_email,
                "client_pkcs12_file_path": self._service_account_p12_file_path,
            }
        else:
            auth_settings["client_config"] = {
                "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID,
                "client_secret": self._client_secret
                or self.DEFAULT_GDRIVE_CLIENT_SECRET,
                "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                "token_uri": "https://oauth2.googleapis.com/token",
                "revoke_uri": "https://oauth2.googleapis.com/revoke",
                "redirect_uri": "",
            }

        GoogleAuth.DEFAULT_SETTINGS.update(auth_settings)

        # Pass non existent settings path to force DEFAULT_SETTINGS loadings
        gauth = GoogleAuth(settings_file="")

        try:
            logger.debug(
                "GDrive remote auth with config '{}'.".format(
                    GoogleAuth.DEFAULT_SETTINGS
                )
            )
            if self._use_service_account:
                gauth.ServiceAuth()
            else:
                gauth.CommandLineAuth()
                GDriveTree._validate_credentials(gauth, auth_settings)

        # Handle AuthenticationError, RefreshError and other auth failures
        # It's hard to come up with a narrow exception, since PyDrive throws
        # a lot of different errors - broken credentials file, refresh token
        # expired, flow failed, etc.
        except Exception as exc:
            raise GDriveAuthError(self.credentials_location) from exc
        finally:
            if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA):
                os.remove(self._gdrive_user_credentials_path)

        return GoogleDrive(gauth)
Beispiel #6
0
 def test_06_ServiceAuthFromSavedCredentialsFile(self):
   setup_credentials("credentials/6.dat")
   ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_06.yaml')
   ga.ServiceAuth()
   self.assertEqual(ga.access_token_expired, False)
   time.sleep(1)
Beispiel #7
0
 def test_06_ServiceAuthFromSavedCredentialsP12File(self):
     setup_credentials("credentials/6.dat")
     ga = GoogleAuth(settings_file_path("test_oauth_test_06.yaml"))
     ga.ServiceAuth()
     self.assertEqual(ga.access_token_expired, False)
     time.sleep(1)
Beispiel #8
0
 def setUp(self):
     ga = GoogleAuth('pydrive2/test/settings/default.yaml')
     ga.ServiceAuth()
     self.drive = GoogleDrive(ga)
     self.file1 = self.drive.CreateFile()
     pydrive_retry(self.file1.Upload)
 def setUp(self):
     ga = GoogleAuth(settings_file_path("default.yaml"))
     ga.ServiceAuth()
     self.drive = GoogleDrive(ga)
     self.file1 = self.drive.CreateFile()
     pydrive_retry(self.file1.Upload)