apt_key.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. require 'date'
  2. require 'open-uri'
  3. require 'net/ftp'
  4. require 'tempfile'
  5. if RUBY_VERSION == '1.8.7'
  6. # Mothers cry, puppies die and Ruby 1.8.7's open-uri needs to be
  7. # monkeypatched to support passing in :ftp_passive_mode.
  8. require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..',
  9. 'puppet_x', 'apt_key', 'patch_openuri.rb'))
  10. OpenURI::Options.merge!({:ftp_active_mode => false,})
  11. end
  12. Puppet::Type.type(:apt_key).provide(:apt_key) do
  13. confine :osfamily => :debian
  14. defaultfor :osfamily => :debian
  15. commands :apt_key => 'apt-key'
  16. def self.instances
  17. cli_args = ['adv','--list-keys', '--with-colons', '--fingerprint']
  18. if RUBY_VERSION > '1.8.7'
  19. key_output = apt_key(cli_args).encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
  20. else
  21. key_output = apt_key(cli_args)
  22. end
  23. pub_line, fpr_line = nil
  24. key_array = key_output.split("\n").collect do |line|
  25. if line.start_with?('pub')
  26. pub_line = line
  27. elsif line.start_with?('fpr')
  28. fpr_line = line
  29. end
  30. next unless (pub_line and fpr_line)
  31. line_hash = key_line_hash(pub_line, fpr_line)
  32. # reset everything
  33. pub_line, fpr_line = nil
  34. expired = false
  35. if line_hash[:key_expiry]
  36. expired = Date.today > Date.parse(line_hash[:key_expiry])
  37. end
  38. new(
  39. :name => line_hash[:key_fingerprint],
  40. :id => line_hash[:key_fingerprint],
  41. :fingerprint => line_hash[:key_fingerprint],
  42. :short => line_hash[:key_short],
  43. :long => line_hash[:key_long],
  44. :ensure => :present,
  45. :expired => expired,
  46. :expiry => line_hash[:key_expiry],
  47. :size => line_hash[:key_size],
  48. :type => line_hash[:key_type],
  49. :created => line_hash[:key_created]
  50. )
  51. end
  52. key_array.compact!
  53. end
  54. def self.prefetch(resources)
  55. apt_keys = instances
  56. resources.keys.each do |name|
  57. if name.length == 40
  58. if provider = apt_keys.find{ |key| key.fingerprint == name }
  59. resources[name].provider = provider
  60. end
  61. elsif name.length == 16
  62. if provider = apt_keys.find{ |key| key.long == name }
  63. resources[name].provider = provider
  64. end
  65. elsif name.length == 8
  66. if provider = apt_keys.find{ |key| key.short == name }
  67. resources[name].provider = provider
  68. end
  69. end
  70. end
  71. end
  72. def self.key_line_hash(pub_line, fpr_line)
  73. pub_split = pub_line.split(':')
  74. fpr_split = fpr_line.split(':')
  75. fingerprint = fpr_split.last
  76. return_hash = {
  77. :key_fingerprint => fingerprint,
  78. :key_long => fingerprint[-16..-1], # last 16 characters of fingerprint
  79. :key_short => fingerprint[-8..-1], # last 8 characters of fingerprint
  80. :key_size => pub_split[2],
  81. :key_type => nil,
  82. :key_created => pub_split[5],
  83. :key_expiry => pub_split[6].empty? ? nil : pub_split[6],
  84. }
  85. # set key type based on types defined in /usr/share/doc/gnupg/DETAILS.gz
  86. case pub_split[3]
  87. when "1"
  88. return_hash[:key_type] = :rsa
  89. when "17"
  90. return_hash[:key_type] = :dsa
  91. when "18"
  92. return_hash[:key_type] = :ecc
  93. when "19"
  94. return_hash[:key_type] = :ecdsa
  95. end
  96. return return_hash
  97. end
  98. def source_to_file(value)
  99. parsedValue = URI::parse(value)
  100. if parsedValue.scheme.nil?
  101. fail("The file #{value} does not exist") unless File.exists?(value)
  102. value
  103. else
  104. begin
  105. key = parsedValue.read
  106. rescue OpenURI::HTTPError, Net::FTPPermError => e
  107. fail("#{e.message} for #{resource[:source]}")
  108. rescue SocketError
  109. fail("could not resolve #{resource[:source]}")
  110. else
  111. tempfile(key)
  112. end
  113. end
  114. end
  115. def tempfile(content)
  116. file = Tempfile.new('apt_key')
  117. file.write content
  118. file.close
  119. file.path
  120. end
  121. def exists?
  122. @property_hash[:ensure] == :present
  123. end
  124. def create
  125. command = []
  126. if resource[:source].nil? and resource[:content].nil?
  127. # Breaking up the command like this is needed because it blows up
  128. # if --recv-keys isn't the last argument.
  129. command.push('adv', '--keyserver', resource[:server])
  130. unless resource[:keyserver_options].nil?
  131. command.push('--keyserver-options', resource[:keyserver_options])
  132. end
  133. command.push('--recv-keys', resource[:id])
  134. elsif resource[:content]
  135. command.push('add', tempfile(resource[:content]))
  136. elsif resource[:source]
  137. command.push('add', source_to_file(resource[:source]))
  138. # In case we really screwed up, better safe than sorry.
  139. else
  140. fail("an unexpected condition occurred while trying to add the key: #{resource[:id]}")
  141. end
  142. apt_key(command)
  143. @property_hash[:ensure] = :present
  144. end
  145. def destroy
  146. #Currently del only removes the first key, we need to recursively list and ensure all with id are absent.
  147. apt_key('del', resource[:id])
  148. @property_hash.clear
  149. end
  150. def read_only(value)
  151. fail('This is a read-only property.')
  152. end
  153. mk_resource_methods
  154. # Needed until PUP-1470 is fixed and we can drop support for Puppet versions
  155. # before that.
  156. def expired
  157. @property_hash[:expired]
  158. end
  159. # Alias the setters of read-only properties
  160. # to the read_only function.
  161. alias :created= :read_only
  162. alias :expired= :read_only
  163. alias :expiry= :read_only
  164. alias :size= :read_only
  165. alias :type= :read_only
  166. end