|
4 | 4 | import tempfile |
5 | 5 |
|
6 | 6 | import fabric.transfer # type: ignore |
| 7 | +import paramiko # type: ignore |
7 | 8 | from fabric import Connection # type: ignore |
8 | 9 | from paramiko.sftp import SFTPError # type: ignore |
9 | 10 | from pds_doi_service.core.actions.roundup.enumerate import get_previous_week_metadata |
|
12 | 13 | from pds_doi_service.core.util.config_parser import DOIConfigUtil |
13 | 14 |
|
14 | 15 |
|
| 16 | +class FIPSCompliantAutoAddPolicy(paramiko.MissingHostKeyPolicy): |
| 17 | + """ |
| 18 | + FIPS-compliant host key policy that accepts unknown hosts without computing MD5 fingerprints. |
| 19 | +
|
| 20 | + This policy is similar to AutoAddPolicy but avoids calling get_fingerprint() which uses MD5 |
| 21 | + and fails in FIPS mode. It's appropriate for internal SFTP servers where host key verification |
| 22 | + is not critical. |
| 23 | + """ |
| 24 | + |
| 25 | + def missing_host_key(self, client, hostname, key): |
| 26 | + """ |
| 27 | + Accept the host key without computing fingerprints. |
| 28 | +
|
| 29 | + Args: |
| 30 | + client: SSHClient instance |
| 31 | + hostname: The hostname of the server |
| 32 | + key: The server's host key |
| 33 | + """ |
| 34 | + # Add the key without logging the fingerprint (which would use MD5) |
| 35 | + client._host_keys.add(hostname, key.get_name(), key) |
| 36 | + |
| 37 | + |
15 | 38 | def ensure_target_dir(dir_path: str, conn: Connection): |
16 | 39 | transfer = fabric.transfer.Transfer(connection=conn) |
17 | 40 | sftp = transfer.sftp |
@@ -53,7 +76,20 @@ def run( |
53 | 76 | dest_filename = f'roundup-week-ending-{metadata.last_date.strftime("%Y%m%d")}.json' |
54 | 77 | dest_path = os.path.join(dest_dir_path, dest_filename) |
55 | 78 |
|
56 | | - conn = Connection(host=sftp_host, port=sftp_port, user=sftp_user, connect_kwargs={"password": sftp_password}) |
| 79 | + # Configure connection with FIPS-compliant host key policy |
| 80 | + # This automatically accepts unknown host keys without computing MD5 fingerprints |
| 81 | + conn = Connection( |
| 82 | + host=sftp_host, |
| 83 | + port=sftp_port, |
| 84 | + user=sftp_user, |
| 85 | + connect_kwargs={ |
| 86 | + "password": sftp_password, |
| 87 | + "look_for_keys": False, # Disable SSH key auth to avoid MD5 fingerprint in FIPS mode |
| 88 | + "allow_agent": False, # Disable SSH agent to avoid MD5 fingerprint in FIPS mode |
| 89 | + }, |
| 90 | + ) |
| 91 | + # Set FIPS-compliant host key policy that doesn't use MD5 |
| 92 | + conn.client.set_missing_host_key_policy(FIPSCompliantAutoAddPolicy()) |
57 | 93 | transfer = fabric.transfer.Transfer(connection=conn) |
58 | 94 |
|
59 | 95 | ensure_target_dir(dest_dir_path, conn) |
|
0 commit comments