From ea68ea521b8ea0ef460ee639759e76e22a620a7e Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 10 Aug 2022 14:26:16 +0200 Subject: [PATCH] Deal with net-ftp being unavailable In Ruby 3.0 net-ftp changed from a bundled gem to a default gem. This means it may not be available, such as when running unit tests. Since ftp is becoming less and less common, this changes net-ftp to be an optional dependency. Users who do need ftp support should ensure the gem is installed. --- lib/puppet/provider/apt_key/apt_key.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/puppet/provider/apt_key/apt_key.rb b/lib/puppet/provider/apt_key/apt_key.rb index 56f9a072e5..115ed316de 100644 --- a/lib/puppet/provider/apt_key/apt_key.rb +++ b/lib/puppet/provider/apt_key/apt_key.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true require 'open-uri' -require 'net/ftp' +begin + require 'net/ftp' +rescue LoadError + # Ruby 3.0 changed net-ftp to a default gem +end require 'tempfile' Puppet::Type.type(:apt_key).provide(:apt_key) do @@ -124,6 +128,9 @@ def source_to_file(value) f.close f else + exceptions = [OpenURI::HTTPError] + exceptions << Net::FTPPermError if defined?(Net::FTPPermError) + begin # Only send basic auth if URL contains userinfo # Some webservers (e.g. Amazon S3) return code 400 if empty basic auth is sent @@ -138,7 +145,7 @@ def source_to_file(value) parsed_value.userinfo = '' key = open(parsed_value, http_basic_authentication: user_pass).read end - rescue OpenURI::HTTPError, Net::FTPPermError => e + rescue *exceptions => e raise(_('%{_e} for %{_resource}') % { _e: e.message, _resource: resource[:source] }) rescue SocketError raise(_('could not resolve %{_resource}') % { _resource: resource[:source] })