From 03980386f0ceef4315f3fd384f8e1b11870b51aa Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Tue, 6 Aug 2024 16:24:04 -0600 Subject: [PATCH] fixup! fix: Fix ftp failures (#5585) --- cloudinit/url_helper.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index cf74a257d552..b9b0ec88af4c 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -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 " @@ -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() @@ -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( @@ -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":