MODULES-1119 Fixed to now have username and passwords passed in again
[puppet-modules/puppetlabs-apt.git] / lib / puppet / provider / apt_key / apt_key.rb
index 7e221dded5ae8f07bd751d4587edfa3b611fb7d5..075fb6fca5a3fa93d2141777eccc5990a8a434b6 100644 (file)
@@ -1,7 +1,16 @@
 require 'date'
 require 'open-uri'
+require 'net/ftp'
 require 'tempfile'
 
+if RUBY_VERSION == '1.8.7'
+  # Mothers cry, puppies die and Ruby 1.8.7's open-uri needs to be
+  # monkeypatched to support passing in :ftp_passive_mode.
+  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..',
+                                    'puppet_x', 'apt_key', 'patch_openuri.rb'))
+  OpenURI::Options.merge!({:ftp_active_mode => false,})
+end
+
 Puppet::Type.type(:apt_key).provide(:apt_key) do
 
   KEY_LINE = {
@@ -17,7 +26,12 @@ Puppet::Type.type(:apt_key).provide(:apt_key) do
   commands   :apt_key  => 'apt-key'
 
   def self.instances
-    key_array = apt_key('list').split("\n").collect do |line|
+    if RUBY_VERSION > '1.8.7'
+      key_output = apt_key('list').encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
+    else
+      key_output = apt_key('list')
+    end
+    key_array = key_output.split("\n").collect do |line|
       line_hash = key_line_hash(line)
       next unless line_hash
       expired = false
@@ -94,13 +108,14 @@ Puppet::Type.type(:apt_key).provide(:apt_key) do
   end
 
   def source_to_file(value)
-    if URI::parse(value).scheme.nil?
+    parsedValue = URI::parse(value)
+    if parsedValue.scheme.nil?
       fail("The file #{value} does not exist") unless File.exists?(value)
       value
     else
       begin
-        key = open(value).read
-      rescue OpenURI::HTTPError => e
+        key = parsedValue.read
+      rescue OpenURI::HTTPError, Net::FTPPermError => e
         fail("#{e.message} for #{resource[:source]}")
       rescue SocketError
         fail("could not resolve #{resource[:source]}")
@@ -144,6 +159,7 @@ Puppet::Type.type(:apt_key).provide(:apt_key) do
   end
 
   def destroy
+    #Currently del only removes the first key, we need to recursively list and ensure all with id are absent.
     apt_key('del', resource[:id])
     @property_hash.clear
   end