diff --git a/faassupervisor/storage/providers/rucio.py b/faassupervisor/storage/providers/rucio.py index d1322d7..c309153 100644 --- a/faassupervisor/storage/providers/rucio.py +++ b/faassupervisor/storage/providers/rucio.py @@ -25,6 +25,7 @@ pass +from rucio.common.config import config_set, config_add_section, config_has_section from rucio.client.client import Client from rucio.client.uploadclient import UploadClient from rucio.client.downloadclient import DownloadClient @@ -63,14 +64,12 @@ def __init__(self, stg_auth): self.scopes = self._OIDC_SCOPE.split() self.token_temp_file = tempfile.mktemp(prefix='rucio_token_', suffix='.token') # nosec - self._create_config_file() + self._create_rucio_config() def __del__(self): try: if os.path.exists(self.token_temp_file): os.remove(self.token_temp_file) - if os.path.exists(self.cfg_temp_file): - os.remove(self.cfg_temp_file) except Exception as exc: get_logger().warning('Error removing temporary file: %s', exc) @@ -88,22 +87,23 @@ def _get_access_token(self): self.client_id) return self.oidc_token - def _create_config_file(self): - cfg_temp_file = tempfile.NamedTemporaryFile(delete=False) - cfg_temp_file.write(b'[client]\n') - cfg_temp_file.write(b'rucio_host = %s\n' % self.rucio_host.encode()) - cfg_temp_file.write(b'auth_host = %s\n' % self.auth_host.encode()) - cfg_temp_file.write(b'auth_type = oidc\n') - cfg_temp_file.write(b'account = %s\n' % self.scope.encode()) - cfg_temp_file.write(b'auth_token_file_path = %s\n' % self.token_temp_file.encode()) - cfg_temp_file.write(b'oidc_scope = %s\n' % self._OIDC_SCOPE.encode()) - self.cfg_temp_file = cfg_temp_file.name + def _create_rucio_config(self): + os.environ["RUCIO_CLIENT_MODE"] = "1" + os.environ["RUCIO_CONFIG"] = "/dev/null" + + if not config_has_section("client"): + config_add_section("client") + config_set(section="client", option="auth_token_file_path", value=self.token_temp_file) + config_set(section="client", option="oidc_scope", value=self._OIDC_SCOPE) + config_set(section="client", option="rucio_host", value=self.rucio_host) + config_set(section="client", option="auth_host", value=self.auth_host) + config_set(section="client", option="account", value=self.scope) + config_set(section="client", option="auth_type", value="oidc") def _get_rucio_client(self, client_type=None): # Create token file with open(self.token_temp_file, 'w') as f: f.write(self._get_access_token()) - os.environ['RUCIO_CONFIG'] = self.cfg_temp_file client = Client() if not client_type: return client @@ -134,7 +134,7 @@ def download_file(self, parsed_event, input_dir_path): complete_name = file["dest_file_paths"][0] basename = os.path.basename(complete_name) FileUtils.cp_file(complete_name, SysUtils.join_paths(input_dir_path, - basename)) + basename)) except Exception as e: print("An exception occurred" + e) return input_dir_path diff --git a/test/unit/faassupervisor/storage.py b/test/unit/faassupervisor/storage.py index f117d94..83d169a 100644 --- a/test/unit/faassupervisor/storage.py +++ b/test/unit/faassupervisor/storage.py @@ -31,6 +31,7 @@ from faassupervisor.events.onedata import OnedataEvent from faassupervisor.utils import StrUtils from rucio.common.exception import DataIdentifierNotFound +from rucio.common.config import config_get, config_has_section # pylint: disable=missing-docstring @@ -566,13 +567,16 @@ def test_download_file(self, mock_cp, mock_refesh, mock_download, mock_client): mock_download_client.download_dids.assert_called_once_with([{'did': 'test_account2:file1'}, {'did': 'test_account2:file2'}]) mock_rucio_client.list_files.assert_called_once_with('test_account2', 'dataset_name') - with open(rucio_provider.cfg_temp_file, 'r') as f: - content = f.read() - exp_content = RUCIO_CFG_FILE.format(rucio_provider=rucio_provider) - self.assertEqual(content, exp_content) with open(rucio_provider.token_temp_file, 'r') as f: content = f.read() self.assertEqual(content, 'new_access_token') + self.assertTrue(config_has_section('client')) + self.assertEqual(config_get('client', 'auth_token_file_path'), rucio_provider.token_temp_file) + self.assertEqual(config_get('client', 'oidc_scope'), Rucio._OIDC_SCOPE) + self.assertEqual(config_get('client', 'rucio_host'), rucio_provider.rucio_host) + self.assertEqual(config_get('client', 'auth_host'), rucio_provider.auth_host) + self.assertEqual(config_get('client', 'account'), rucio_provider.scope) + self.assertEqual(config_get('client', 'auth_type'), 'oidc') @mock.patch('faassupervisor.storage.providers.rucio.UploadClient') @mock.patch('faassupervisor.storage.providers.rucio.Client')