Merge pull request #215 from lotherk/proxy_val_no_boolean
[puppet-modules/puppetlabs-apt.git] / lib / puppet / provider / apt_key / apt_key.rb
1 require 'date'
2 require 'open-uri'
3 require 'net/ftp'
4 require 'tempfile'
5
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 'puppet_x/apt_key/patch_openuri'
10   OpenURI::Options.merge!({:ftp_active_mode => false,})
11 end
12
13 Puppet::Type.type(:apt_key).provide(:apt_key) do
14
15   KEY_LINE = {
16     :date     => '[0-9]{4}-[0-9]{2}-[0-9]{2}',
17     :key_type => '(R|D)',
18     :key_size => '\d{4}',
19     :key_id   => '[0-9a-fA-F]+',
20     :expires  => 'expire(d|s)',
21   }
22
23   confine    :osfamily => :debian
24   defaultfor :osfamily => :debian
25   commands   :apt_key  => 'apt-key'
26
27   def self.instances
28     key_array = apt_key('list').split("\n").collect do |line|
29       line_hash = key_line_hash(line)
30       next unless line_hash
31       expired = false
32
33       if line_hash[:key_expiry]
34         expired = Date.today > Date.parse(line_hash[:key_expiry])
35       end
36
37       new(
38         :name    => line_hash[:key_id],
39         :id      => line_hash[:key_id],
40         :ensure  => :present,
41         :expired => expired,
42         :expiry  => line_hash[:key_expiry],
43         :size    => line_hash[:key_size],
44         :type    => line_hash[:key_type] == 'R' ? :rsa : :dsa,
45         :created => line_hash[:key_created]
46       )
47     end
48     key_array.compact!
49   end
50
51   def self.prefetch(resources)
52     apt_keys = instances
53     resources.keys.each do |name|
54       if provider = apt_keys.find{ |key| key.name == name }
55         resources[name].provider = provider
56       end
57     end
58   end
59
60   def self.key_line_hash(line)
61     line_array = line.match(key_line_regexp).to_a
62     return nil if line_array.length < 5
63
64     return_hash = {
65       :key_id      => line_array[3],
66       :key_size    => line_array[1],
67       :key_type    => line_array[2],
68       :key_created => line_array[4],
69       :key_expiry  => nil,
70     }
71
72     return_hash[:key_expiry] = line_array[7] if line_array.length == 8
73     return return_hash
74   end
75
76   def self.key_line_regexp
77     # This regexp is trying to match the following output
78     # pub   4096R/4BD6EC30 2010-07-10 [expires: 2016-07-08]
79     # pub   1024D/CD2EFD2A 2009-12-15
80     regexp = /\A
81       pub  # match only the public key, not signatures
82       \s+  # bunch of spaces after that
83       (#{KEY_LINE[:key_size]})  # size of the key, usually a multiple of 1024
84       #{KEY_LINE[:key_type]}  # type of the key, usually R or D
85       \/  # separator between key_type and key_id
86       (#{KEY_LINE[:key_id]})  # hex id of the key
87       \s+  # bunch of spaces after that
88       (#{KEY_LINE[:date]})  # date the key was added to the keyring
89       # following an optional block which indicates if the key has an expiration
90       # date and if it has expired yet
91       (
92         \s+  # again with thes paces
93         \[  # we open with a square bracket
94         #{KEY_LINE[:expires]}  # expires or expired
95         \:  # a colon
96         \s+  # more spaces
97         (#{KEY_LINE[:date]})  # date indicating key expiry
98         \]  # we close with a square bracket
99       )?  # end of the optional block
100       \Z/x
101       regexp
102   end
103
104   def source_to_file(value)
105     if URI::parse(value).scheme.nil?
106       fail("The file #{value} does not exist") unless File.exists?(value)
107       value
108     else
109       begin
110         key = open(value, :ftp_active_mode => false).read
111       rescue OpenURI::HTTPError, Net::FTPPermError => e
112         fail("#{e.message} for #{resource[:source]}")
113       rescue SocketError
114         fail("could not resolve #{resource[:source]}")
115       else
116         tempfile(key)
117       end
118     end
119   end
120
121   def tempfile(content)
122     file = Tempfile.new('apt_key')
123     file.write content
124     file.close
125     file.path
126   end
127
128   def exists?
129     @property_hash[:ensure] == :present
130   end
131
132   def create
133     command = []
134     if resource[:source].nil? and resource[:content].nil?
135       # Breaking up the command like this is needed because it blows up
136       # if --recv-keys isn't the last argument.
137       command.push('adv', '--keyserver', resource[:server])
138       unless resource[:keyserver_options].nil?
139         command.push('--keyserver-options', resource[:keyserver_options])
140       end
141       command.push('--recv-keys', resource[:id])
142     elsif resource[:content]
143       command.push('add', tempfile(resource[:content]))
144     elsif resource[:source]
145       command.push('add', source_to_file(resource[:source]))
146     # In case we really screwed up, better safe than sorry.
147     else
148       fail("an unexpected condition occurred while trying to add the key: #{resource[:id]}")
149     end
150     apt_key(command)
151     @property_hash[:ensure] = :present
152   end
153
154   def destroy
155     apt_key('del', resource[:id])
156     @property_hash.clear
157   end
158
159   def read_only(value)
160     fail('This is a read-only property.')
161   end
162
163   mk_resource_methods
164
165   # Needed until PUP-1470 is fixed and we can drop support for Puppet versions
166   # before that.
167   def expired
168     @property_hash[:expired]
169   end
170
171   # Alias the setters of read-only properties
172   # to the read_only function.
173   alias :created= :read_only
174   alias :expired= :read_only
175   alias :expiry=  :read_only
176   alias :size=    :read_only
177   alias :type=    :read_only
178 end