Skip to content

Commit

Permalink
Merge pull request #1143 from uc-cdis/feat/sftp_backoff
Browse files Browse the repository at this point in the history
 backoff.exception for sftp connection
  • Loading branch information
k-burt-uch authored Apr 23, 2024
2 parents 37a78a0 + dfb546d commit a47e104
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions fence/sync/sync_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import backoff
import glob
import jwt
import os
Expand Down Expand Up @@ -42,7 +43,7 @@
from fence.resources.google.access_utils import GoogleUpdateException
from fence.sync import utils
from fence.sync.passport_sync.ras_sync import RASVisa
from fence.utils import get_SQLAlchemyDriver
from fence.utils import get_SQLAlchemyDriver, DEFAULT_BACKOFF_SETTINGS


def _format_policy_id(path, privilege):
Expand Down Expand Up @@ -429,13 +430,17 @@ def _get_from_sftp_with_proxy(self, server, path):
parameters.get("port", "unknown"),
)
)
client.connect(**parameters)
self._connect_with_ssh(ssh_client=client, parameters=parameters)
with client.open_sftp() as sftp:
download_dir(sftp, "./", path)

if proxy:
proxy.close()

@backoff.on_exception(backoff.expo, Exception, **DEFAULT_BACKOFF_SETTINGS)
def _connect_with_ssh(self, ssh_client, parameters):
ssh_client.connect(**parameters)

def _get_from_ftp_with_proxy(self, server, path):
"""
Download data from ftp sever to a local dir
Expand Down
16 changes: 16 additions & 0 deletions tests/dbgap_sync/test_user_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from fence.resources.google.access_utils import GoogleUpdateException
from fence.config import config
from fence.job.visa_update_cronjob import Visa_Token_Update
from fence.utils import DEFAULT_BACKOFF_SETTINGS

from tests.dbgap_sync.conftest import (
get_test_encoded_decoded_visa_and_exp,
Expand Down Expand Up @@ -497,6 +498,21 @@ def test_sync_with_google_errors(syncer, monkeypatch):
syncer._update_arborist.assert_called()
syncer._update_authz_in_arborist.assert_called()

@patch("fence.sync.sync_users.paramiko.SSHClient")
@patch("os.makedirs")
@patch("os.path.exists", return_value=False)
@pytest.mark.parametrize("syncer", ["google", "cleversafe"], indirect=True)
def test_sync_with_sftp_connection_errors(mock_path, mock_makedir, mock_ssh_client, syncer, monkeypatch):
"""
Verifies that when there is an sftp connection error connection, that the connection is retried the max amount of
tries as configured by DEFAULT_BACKOFF_SETTINGS
"""
monkeypatch.setattr(syncer, "is_sync_from_dbgap_server", True)
mock_ssh_client.return_value.__enter__.return_value.connect.side_effect = Exception("Authentication timed out")
# usersync System Exits if any exception is raised during download.
with pytest.raises(SystemExit):
syncer.sync()
assert mock_ssh_client.return_value.__enter__.return_value.connect.call_count == DEFAULT_BACKOFF_SETTINGS['max_tries']

@pytest.mark.parametrize("syncer", ["google", "cleversafe"], indirect=True)
def test_sync_from_files(syncer, db_session, storage_client):
Expand Down

0 comments on commit a47e104

Please sign in to comment.