Skip to content

Commit

Permalink
fixup! fix: Fix ftp failures (#5585)
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Aug 6, 2024
1 parent 132b476 commit 0398038
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cloudinit/url_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
)
LOG.debug("Creating a secure connection")
ftp_tls.prot_p()
LOG.debug("Reading file: %s", url_parts.path)
ftp_tls.retrbinary(
f"RETR {url_parts.path}", callback=buffer.write
)

response = FtpResponse(buffer.getvalue(), url)
LOG.debug("Closing connection")
except ftplib.error_perm as e:
LOG.warning(
"Attempted to connect to an insecure ftp server but used "
Expand All @@ -165,9 +158,27 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
headers=None,
url=url,
) from e
try:
LOG.debug("Reading file: %s", url_parts.path)
ftp_tls.retrbinary(
f"RETR {url_parts.path}", callback=buffer.write
)

return FtpResponse(buffer.getvalue(), url)
except ftplib.all_errors as e:
code = ftp_get_return_code_from_exception(e)
raise UrlError(
cause=(
"Reading file from ftp server"
f" failed for url {url} [{code}]"
),
code=code,
headers=None,
url=url,
) from e
finally:
LOG.debug("Closing connection")
ftp_tls.close()
return response
else:
try:
ftp = ftplib.FTP()
Expand All @@ -186,7 +197,7 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
)
LOG.debug("Reading file: %s", url_parts.path)
ftp.retrbinary(f"RETR {url_parts.path}", callback=buffer.write)
response = FtpResponse(buffer.getvalue(), url)
return FtpResponse(buffer.getvalue(), url)
except ftplib.all_errors as e:
code = ftp_get_return_code_from_exception(e)
raise UrlError(
Expand All @@ -201,7 +212,6 @@ def read_ftps(url: str, timeout: float = 5.0, **kwargs: dict) -> "FtpResponse":
finally:
LOG.debug("Closing connection")
ftp.close()
return response


def _read_file(path: str, **kwargs) -> "FileResponse":
Expand Down

0 comments on commit 0398038

Please sign in to comment.