Skip to content

Commit 5b66f07

Browse files
Merge branch 'main' into qa
2 parents 10550cf + e12e307 commit 5b66f07

6 files changed

Lines changed: 189 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Changelog
2+
## v1.4.0 9/23/24
3+
- Added SFTP client
4+
5+
## v1.3.2 8/1/24
6+
- Replaced info statements with debug for security purposes
7+
8+
## v1.3.1 7/31/24
9+
- Replaced log statement in Avro client with debug
10+
211
## v1.3.0 7/30/24
312
- Added SecretsManager client
413

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This package contains common Python utility classes and functions.
88
* Decrypting values with KMS
99
* Encoding and decoding records using a given Avro schema
1010
* Retrieving secrets from AWS Secrets Manager
11+
* Downloading files from a remote SSH SFTP server
1112
* Connecting to and querying a MySQL database
1213
* Connecting to and querying a PostgreSQL database
1314
* Connecting to and querying a PostgreSQL database using a connection pool
@@ -36,7 +37,7 @@ kinesis_client = KinesisClient(...)
3637
# Do not use any version below 1.0.0
3738
# All available optional dependencies can be found in pyproject.toml.
3839
# See the "Managing dependencies" section below for more details.
39-
nypl-py-utils[kinesis-client,config-helper]==1.3.0
40+
nypl-py-utils[kinesis-client,config-helper]==1.4.0
4041
```
4142

4243
## Developing locally
@@ -62,7 +63,7 @@ The optional dependency sets also give the developer the option to manually list
6263
### Using PostgreSQLClient in an AWS Lambda
6364
Because `psycopg` requires a statically linked version of the `libpq` library, the `PostgreSQLClient` cannot be installed as-is in an AWS Lambda function. Instead, it must be packaged as follows:
6465
```bash
65-
pip install --target ./package nypl-py-utils[postgresql-client]==1.1.2
66+
pip install --target ./package nypl-py-utils[postgresql-client]==1.4.0
6667

6768
pip install \
6869
--platform manylinux2014_x86_64 \

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "nypl_py_utils"
7-
version = "1.3.0"
7+
version = "1.4.0"
88
authors = [
99
{ name="Aaron Friedman", email="aaronfriedman@nypl.org" },
1010
]
@@ -60,6 +60,9 @@ secrets-manager-client = [
6060
"boto3>=1.26.5",
6161
"botocore>=1.29.5"
6262
]
63+
sftp-client = [
64+
"paramiko>=3.4.1"
65+
]
6366
config-helper = [
6467
"nypl_py_utils[kms-client]",
6568
"PyYAML>=6.0"
@@ -71,7 +74,7 @@ research-catalog-identifier-helper = [
7174
"requests>=2.28.1"
7275
]
7376
development = [
74-
"nypl_py_utils[avro-client,kinesis-client,kms-client,mysql-client,oauth2-api-client,postgresql-client,postgresql-pool-client,redshift-client,s3-client,secrets-manager-client,config-helper,obfuscation-helper,research-catalog-identifier-helper]",
77+
"nypl_py_utils[avro-client,kinesis-client,kms-client,mysql-client,oauth2-api-client,postgresql-client,postgresql-pool-client,redshift-client,s3-client,secrets-manager-client,sftp-client,config-helper,obfuscation-helper,research-catalog-identifier-helper]",
7578
"flake8>=6.0.0",
7679
"freezegun>=1.2.2",
7780
"mock>=4.0.3",

src/nypl_py_utils/classes/avro_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def decode_record(self, record):
133133
134134
Returns a dictionary where each key is a field in the schema.
135135
"""
136-
self.logger.info(
136+
self.logger.debug(
137137
"Decoding {rec} using {schema} schema".format(
138138
rec=record, schema=self.schema.name
139139
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from base64 import b64decode
2+
from io import StringIO
3+
from nypl_py_utils.functions.log_helper import create_log
4+
from paramiko import PKey, RSAKey, SSHClient
5+
from paramiko.ssh_exception import SSHException
6+
7+
8+
class SftpClient:
9+
"""Client for interacting with a remote SSH server via SFTP"""
10+
11+
def __init__(self, host, user, password=None, private_key_str=None):
12+
self.logger = create_log("sftp_client")
13+
self.host = host
14+
self.user = user
15+
self.password = password
16+
self.private_key_str = private_key_str
17+
self.ssh_client = SSHClient()
18+
19+
def add_host_key(self, key_type, public_key):
20+
try:
21+
public_key = PKey.from_type_string(key_type, b64decode(public_key))
22+
self.ssh_client.get_host_keys().add(
23+
hostname=self.host, keytype=key_type, key=public_key
24+
)
25+
except Exception as e:
26+
self.logger.warning(f"Failed to load host key: {e}")
27+
28+
def connect(self):
29+
"""Connects to a remote server using SSH"""
30+
self.logger.info("Connecting to {}".format(self.host))
31+
pkey = None
32+
try:
33+
if self.private_key_str:
34+
pkey = RSAKey.from_private_key(StringIO(self.private_key_str))
35+
self.ssh_client.connect(self.host, username=self.user,
36+
password=self.password, pkey=pkey)
37+
self.sftp_conn = self.ssh_client.open_sftp()
38+
except SSHException as e:
39+
self.logger.error(
40+
"Error connecting to {host}: {error}".format(
41+
host=self.host, error=e)
42+
)
43+
raise SftpClientError(
44+
"Error connecting to {host}: {error}".format(
45+
host=self.host, error=e)
46+
) from None
47+
48+
def download(self, remote_path, local_path):
49+
"""Downloads a file on the remote server to the local machine"""
50+
self.logger.info(
51+
"Downloading {remote} file as {local}".format(
52+
remote=remote_path, local=local_path
53+
)
54+
)
55+
try:
56+
self.sftp_conn.get(remote_path, local_path)
57+
except Exception as e:
58+
self.logger.error("Error downloading file: {}".format(e))
59+
self.close_connection()
60+
raise SftpClientError(
61+
"Error downloading file: {}".format(e)) from None
62+
63+
def close_connection(self):
64+
"""Closes the connection"""
65+
self.logger.debug("Closing connection to {}".format(self.host))
66+
self.sftp_conn.close()
67+
self.ssh_client.close()
68+
69+
70+
class SftpClientError(Exception):
71+
def __init__(self, message=None):
72+
self.message = message

tests/test_sftp_client.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import pytest
2+
3+
from nypl_py_utils.classes.sftp_client import SftpClient, SftpClientError
4+
5+
_TEST_PUBLIC_KEY = (
6+
'AAAAB3NzaC1yc2EAAAADAQABAAAAgQCHc5r1z7bCxJ+dwR4r65CK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Rr5P\n8x1L+ugdXeUfI5vH7qI9wU+A7oADke63JBEHavv0UQ==\n-----'
24+
'END RSA PRIVATE KEY-----'
25+
)
26+
27+
28+
class TestSftpClient:
29+
30+
@pytest.fixture
31+
def test_instance(self, mocker):
32+
mocker.patch('paramiko.SSHClient.connect')
33+
mocker.patch('paramiko.SSHClient.open_sftp')
34+
return SftpClient('test_host', 'test_user')
35+
36+
def test_add_host_key(self, test_instance):
37+
assert len(test_instance.ssh_client.get_host_keys().keys()) == 0
38+
39+
test_instance.add_host_key('ssh-rsa', _TEST_PUBLIC_KEY)
40+
41+
assert len(test_instance.ssh_client.get_host_keys().keys()) == 1
42+
assert test_instance.ssh_client.get_host_keys().lookup(
43+
'test_host') is not None
44+
45+
def test_connect_password(self, test_instance):
46+
test_instance.password = 'test_password'
47+
48+
test_instance.connect()
49+
50+
test_instance.ssh_client.connect.assert_called_once_with(
51+
'test_host', username='test_user', password='test_password',
52+
pkey=None)
53+
test_instance.ssh_client.open_sftp.assert_called_once()
54+
assert test_instance.sftp_conn is not None
55+
56+
def test_connect_pkey(self, test_instance, mocker):
57+
mock_rsa_key = mocker.MagicMock()
58+
mock_pkey_method = mocker.patch('paramiko.RSAKey.from_private_key',
59+
return_value=mock_rsa_key)
60+
test_instance.private_key_str = _TEST_PRIVATE_KEY
61+
62+
test_instance.connect()
63+
64+
assert mock_pkey_method.call_args[0][0].read() == _TEST_PRIVATE_KEY
65+
test_instance.ssh_client.connect.assert_called_once_with(
66+
'test_host', username='test_user', password=None,
67+
pkey=mock_rsa_key)
68+
test_instance.ssh_client.open_sftp.assert_called_once()
69+
assert test_instance.sftp_conn is not None
70+
71+
def test_download(self, test_instance, mocker):
72+
test_instance.sftp_conn = mocker.MagicMock()
73+
74+
test_instance.download('remote/path', 'local/path')
75+
76+
test_instance.sftp_conn.get.assert_called_once_with(
77+
'remote/path', 'local/path')
78+
79+
def test_download_error(self, test_instance, mocker):
80+
test_instance.ssh_client = mocker.MagicMock()
81+
test_instance.sftp_conn = mocker.MagicMock()
82+
test_instance.sftp_conn.get.side_effect = IOError('test error')
83+
84+
with pytest.raises(SftpClientError):
85+
test_instance.download('remote/path', 'local/path')
86+
87+
test_instance.sftp_conn.get.assert_called_once_with(
88+
'remote/path', 'local/path')
89+
test_instance.sftp_conn.close.assert_called_once()
90+
test_instance.ssh_client.close.assert_called_once()
91+
92+
def test_close_connection(self, test_instance, mocker):
93+
test_instance.sftp_conn = mocker.MagicMock()
94+
test_instance.ssh_client = mocker.MagicMock()
95+
96+
test_instance.close_connection()
97+
98+
test_instance.sftp_conn.close.assert_called_once()
99+
test_instance.ssh_client.close.assert_called_once()

0 commit comments

Comments
 (0)