Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions faassupervisor/storage/providers/rucio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions test/unit/faassupervisor/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
Loading