6 if RUBY_VERSION == '1.8.7'
7 # Mothers cry, puppies die and Ruby 1.8.7's open-uri needs to be
8 # monkeypatched to support passing in :ftp_passive_mode.
9 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..',
10 'puppet_x', 'apt_key', 'patch_openuri.rb'))
11 OpenURI::Options.merge!({:ftp_active_mode => false,})
14 Puppet::Type.type(:apt_key).provide(:apt_key) do
16 confine :osfamily => :debian
17 defaultfor :osfamily => :debian
18 commands :apt_key => 'apt-key'
21 cli_args = ['adv','--list-keys', '--with-colons', '--fingerprint']
23 if RUBY_VERSION > '1.8.7'
24 key_output = apt_key(cli_args).encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
26 key_output = apt_key(cli_args)
29 pub_line, fpr_line = nil
31 key_array = key_output.split("\n").collect do |line|
32 if line.start_with?('pub')
34 elsif line.start_with?('fpr')
38 next unless (pub_line and fpr_line)
40 line_hash = key_line_hash(pub_line, fpr_line)
43 pub_line, fpr_line = nil
47 if line_hash[:key_expiry]
48 expired = Date.today > Date.parse(line_hash[:key_expiry])
52 :name => line_hash[:key_fingerprint],
53 :id => line_hash[:key_fingerprint],
54 :fingerprint => line_hash[:key_fingerprint],
55 :short => line_hash[:key_short],
56 :long => line_hash[:key_long],
59 :expiry => line_hash[:key_expiry],
60 :size => line_hash[:key_size],
61 :type => line_hash[:key_type],
62 :created => line_hash[:key_created]
68 def self.prefetch(resources)
70 resources.keys.each do |name|
72 if provider = apt_keys.find{ |key| key.fingerprint == name }
73 resources[name].provider = provider
75 elsif name.length == 16
76 if provider = apt_keys.find{ |key| key.long == name }
77 resources[name].provider = provider
79 elsif name.length == 8
80 if provider = apt_keys.find{ |key| key.short == name }
81 resources[name].provider = provider
87 def self.key_line_hash(pub_line, fpr_line)
88 pub_split = pub_line.split(':')
89 fpr_split = fpr_line.split(':')
91 fingerprint = fpr_split.last
93 :key_fingerprint => fingerprint,
94 :key_long => fingerprint[-16..-1], # last 16 characters of fingerprint
95 :key_short => fingerprint[-8..-1], # last 8 characters of fingerprint
96 :key_size => pub_split[2],
98 :key_created => pub_split[5],
99 :key_expiry => pub_split[6].empty? ? nil : pub_split[6],
102 # set key type based on types defined in /usr/share/doc/gnupg/DETAILS.gz
105 return_hash[:key_type] = :rsa
107 return_hash[:key_type] = :dsa
109 return_hash[:key_type] = :ecc
111 return_hash[:key_type] = :ecdsa
117 def source_to_file(value)
118 parsedValue = URI::parse(value)
119 if parsedValue.scheme.nil?
120 fail("The file #{value} does not exist") unless File.exists?(value)
124 key = parsedValue.read
125 rescue OpenURI::HTTPError, Net::FTPPermError => e
126 fail("#{e.message} for #{resource[:source]}")
128 fail("could not resolve #{resource[:source]}")
135 def tempfile(content)
136 file = Tempfile.new('apt_key')
143 @property_hash[:ensure] == :present
148 if resource[:source].nil? and resource[:content].nil?
149 # Breaking up the command like this is needed because it blows up
150 # if --recv-keys isn't the last argument.
151 command.push('adv', '--keyserver', resource[:server])
152 unless resource[:keyserver_options].nil?
153 command.push('--keyserver-options', resource[:keyserver_options])
155 command.push('--recv-keys', resource[:id])
156 elsif resource[:content]
157 command.push('add', tempfile(resource[:content]))
158 elsif resource[:source]
159 command.push('add', source_to_file(resource[:source]))
160 # In case we really screwed up, better safe than sorry.
162 fail("an unexpected condition occurred while trying to add the key: #{resource[:id]}")
165 @property_hash[:ensure] = :present
169 #Currently del only removes the first key, we need to recursively list and ensure all with id are absent.
170 apt_key('del', resource[:id])
175 fail('This is a read-only property.')
180 # Needed until PUP-1470 is fixed and we can drop support for Puppet versions
183 @property_hash[:expired]
186 # Alias the setters of read-only properties
187 # to the read_only function.
188 alias :created= :read_only
189 alias :expired= :read_only
190 alias :expiry= :read_only
191 alias :size= :read_only
192 alias :type= :read_only