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
17 :date => '[0-9]{4}-[0-9]{2}-[0-9]{2}',
20 :key_id => '[0-9a-fA-F]+',
21 :expires => 'expire(d|s)',
24 confine :osfamily => :debian
25 defaultfor :osfamily => :debian
26 commands :apt_key => 'apt-key'
29 if RUBY_VERSION > '1.8.7'
30 key_output = apt_key('list').encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
32 key_output = apt_key('list')
34 key_array = key_output.split("\n").collect do |line|
35 line_hash = key_line_hash(line)
39 if line_hash[:key_expiry]
40 expired = Date.today > Date.parse(line_hash[:key_expiry])
44 :name => line_hash[:key_id],
45 :id => line_hash[:key_id],
48 :expiry => line_hash[:key_expiry],
49 :size => line_hash[:key_size],
50 :type => line_hash[:key_type] == 'R' ? :rsa : :dsa,
51 :created => line_hash[:key_created]
57 def self.prefetch(resources)
59 resources.keys.each do |name|
60 if provider = apt_keys.find{ |key| key.name == name }
61 resources[name].provider = provider
66 def self.key_line_hash(line)
67 line_array = line.match(key_line_regexp).to_a
68 return nil if line_array.length < 5
71 :key_id => line_array[3],
72 :key_size => line_array[1],
73 :key_type => line_array[2],
74 :key_created => line_array[4],
78 return_hash[:key_expiry] = line_array[7] if line_array.length == 8
82 def self.key_line_regexp
83 # This regexp is trying to match the following output
84 # pub 4096R/4BD6EC30 2010-07-10 [expires: 2016-07-08]
85 # pub 1024D/CD2EFD2A 2009-12-15
87 pub # match only the public key, not signatures
88 \s+ # bunch of spaces after that
89 (#{KEY_LINE[:key_size]}) # size of the key, usually a multiple of 1024
90 #{KEY_LINE[:key_type]} # type of the key, usually R or D
91 \/ # separator between key_type and key_id
92 (#{KEY_LINE[:key_id]}) # hex id of the key
93 \s+ # bunch of spaces after that
94 (#{KEY_LINE[:date]}) # date the key was added to the keyring
95 # following an optional block which indicates if the key has an expiration
96 # date and if it has expired yet
98 \s+ # again with thes paces
99 \[ # we open with a square bracket
100 #{KEY_LINE[:expires]} # expires or expired
103 (#{KEY_LINE[:date]}) # date indicating key expiry
104 \] # we close with a square bracket
105 )? # end of the optional block
110 def source_to_file(value)
111 if URI::parse(value).scheme.nil?
112 fail("The file #{value} does not exist") unless File.exists?(value)
116 key = open(value, :ftp_active_mode => false).read
117 rescue OpenURI::HTTPError, Net::FTPPermError => e
118 fail("#{e.message} for #{resource[:source]}")
120 fail("could not resolve #{resource[:source]}")
127 def tempfile(content)
128 file = Tempfile.new('apt_key')
135 @property_hash[:ensure] == :present
140 if resource[:source].nil? and resource[:content].nil?
141 # Breaking up the command like this is needed because it blows up
142 # if --recv-keys isn't the last argument.
143 command.push('adv', '--keyserver', resource[:server])
144 unless resource[:keyserver_options].nil?
145 command.push('--keyserver-options', resource[:keyserver_options])
147 command.push('--recv-keys', resource[:id])
148 elsif resource[:content]
149 command.push('add', tempfile(resource[:content]))
150 elsif resource[:source]
151 command.push('add', source_to_file(resource[:source]))
152 # In case we really screwed up, better safe than sorry.
154 fail("an unexpected condition occurred while trying to add the key: #{resource[:id]}")
157 @property_hash[:ensure] = :present
161 apt_key('del', resource[:id])
166 fail('This is a read-only property.')
171 # Needed until PUP-1470 is fixed and we can drop support for Puppet versions
174 @property_hash[:expired]
177 # Alias the setters of read-only properties
178 # to the read_only function.
179 alias :created= :read_only
180 alias :expired= :read_only
181 alias :expiry= :read_only
182 alias :size= :read_only
183 alias :type= :read_only