From adb15152194982e5b4a461565588e08876d4ffb9 Mon Sep 17 00:00:00 2001 From: drf7 <83355241+drf7@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:42:27 +0200 Subject: [PATCH] fix gcs tus writer (#2392) --- nucliadb/src/nucliadb/writer/tus/gcs.py | 13 ++++++++++--- nucliadb/tests/writer/unit/tus/test_gcs.py | 8 ++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/nucliadb/src/nucliadb/writer/tus/gcs.py b/nucliadb/src/nucliadb/writer/tus/gcs.py index 52c8e02fb9..9cff2feb22 100644 --- a/nucliadb/src/nucliadb/writer/tus/gcs.py +++ b/nucliadb/src/nucliadb/writer/tus/gcs.py @@ -22,7 +22,9 @@ import asyncio import base64 import json +import os import socket +import tempfile import uuid from concurrent.futures import ThreadPoolExecutor from copy import deepcopy @@ -34,7 +36,7 @@ import google.auth.compute_engine.credentials # type: ignore import google.auth.transport.requests # type: ignore from google.auth.exceptions import DefaultCredentialsError # type: ignore -from google.oauth2 import service_account +from oauth2client.service_account import ServiceAccountCredentials # type: ignore from nucliadb.writer import logger from nucliadb.writer.tus.dm import FileDataManager @@ -123,8 +125,13 @@ async def initialize( self._credentials = None if self.json_credentials is not None and self.json_credentials.strip() != "": - self._credentials = service_account.Credentials.from_service_account_info( - base64.b64decode(self.json_credentials).decode("utf-8"), scopes=SCOPES + self.json_credentials_file = os.path.join(tempfile.mkdtemp(), "gcs_credentials.json") + with open(self.json_credentials_file, "w") as file: + file.write( + base64.b64decode(self.json_credentials).decode("utf-8") + ) + self._credentials = ServiceAccountCredentials.from_json_keyfile_name( + self.json_credentials_file, SCOPES ) else: try: diff --git a/nucliadb/tests/writer/unit/tus/test_gcs.py b/nucliadb/tests/writer/unit/tus/test_gcs.py index 6f1afa72b0..a2a8498bbe 100644 --- a/nucliadb/tests/writer/unit/tus/test_gcs.py +++ b/nucliadb/tests/writer/unit/tus/test_gcs.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from unittest.mock import MagicMock, patch +from unittest.mock import ANY, MagicMock, patch import pytest @@ -26,7 +26,7 @@ @pytest.mark.asyncio @patch("nucliadb.writer.tus.gcs.aiohttp") -@patch("nucliadb.writer.tus.gcs.service_account") +@patch("nucliadb.writer.tus.gcs.ServiceAccountCredentials") async def test_tus_gcs(mock_sa, mock_aiohttp): mock_session = MagicMock() mock_aiohttp.ClientSession.return_value = mock_session @@ -44,8 +44,8 @@ async def test_tus_gcs(mock_sa, mock_aiohttp): json_credentials="dGVzdC1jcmVk", ) - mock_sa.Credentials.from_service_account_info.assert_called_once_with( - "test-cred", scopes=["https://www.googleapis.com/auth/devstorage.read_write"] + mock_sa.from_json_keyfile_name.assert_called_once_with( + ANY, ["https://www.googleapis.com/auth/devstorage.read_write"] ) assert await gblobstore.check_exists("test-bucket")