88end
99require 'tempfile'
1010
11- Puppet ::Type . type ( :apt_key ) . provide ( :apt_key ) do
11+ Puppet ::Type . type ( :apt_key ) . provide ( :apt_key ) do # rubocop:disable Metrics/BlockLength
1212 desc 'apt-key provider for apt_key resource'
1313
1414 confine osfamily : :debian
1515 defaultfor osfamily : :debian
1616 commands apt_key : 'apt-key'
1717 commands gpg : '/usr/bin/gpg'
1818
19- def self . instances
19+ def self . instances # rubocop:disable Metrics/AbcSize
20+ key_array = [ ]
21+
2022 cli_args = [ 'adv' , '--no-tty' , '--list-keys' , '--with-colons' , '--fingerprint' , '--fixed-list-mode' ]
2123
2224 key_output = apt_key ( cli_args ) . encode ( 'UTF-8' , 'binary' , invalid : :replace , undef : :replace , replace : '' )
2325
24- pub_line , sub_line , fpr_line = nil
25-
26- key_array = key_output . split ( "\n " ) . map do |line |
27- if line . start_with? ( 'pub' )
28- pub_line = line
29- # reset fpr_line, to skip any previous subkeys which were collected
30- fpr_line = nil
31- sub_line = nil
32- elsif line . start_with? ( 'sub' )
33- sub_line = line
34- elsif line . start_with? ( 'fpr' )
35- fpr_line = line
36- end
37-
38- if sub_line && fpr_line
39- sub_line , fpr_line = nil
40- next
26+ pub_line = nil
27+ fpr_lines = [ ]
28+ sub_lines = [ ]
29+
30+ lines = key_output . split ( "\n " )
31+
32+ lines . each_index do |i |
33+ if lines [ i ] . start_with? ( 'pub' )
34+ pub_line = lines [ i ]
35+ # starting a new public key, so reset fpr_lines and sub_lines
36+ fpr_lines = [ ]
37+ sub_lines = [ ]
38+ elsif lines [ i ] . start_with? ( 'fpr' )
39+ fpr_lines << lines [ i ]
40+ elsif lines [ i ] . start_with? ( 'sub' )
41+ sub_lines << lines [ i ]
4142 end
4243
43- next unless pub_line && fpr_line
44-
45- line_hash = key_line_hash ( pub_line , fpr_line )
46-
47- # reset everything
48- pub_line , fpr_line = nil
44+ next unless ( pub_line && !fpr_lines . empty? ) && ( !lines [ i + 1 ] || lines [ i + 1 ] . start_with? ( 'pub' ) )
4945
50- expired = false
46+ line_hash = key_line_hash ( pub_line , fpr_lines )
5147
52- expired = Time . now >= line_hash [ :key_expiry ] if line_hash [ :key_expiry ]
48+ expired = line_hash [ :key_expired ] || subkeys_all_expired ( sub_lines )
5349
54- new (
50+ key_array << new (
5551 name : line_hash [ :key_fingerprint ] ,
5652 id : line_hash [ :key_long ] ,
5753 fingerprint : line_hash [ :key_fingerprint ] ,
@@ -65,7 +61,7 @@ def self.instances
6561 created : line_hash [ :key_created ] . strftime ( '%Y-%m-%d' ) ,
6662 )
6763 end
68- key_array . compact!
64+ key_array
6965 end
7066
7167 def self . prefetch ( resources )
@@ -85,9 +81,18 @@ def self.prefetch(resources)
8581 end
8682 end
8783
88- def self . key_line_hash ( pub_line , fpr_line )
84+ def self . subkeys_all_expired ( sub_lines )
85+ return false if sub_lines . empty?
86+
87+ sub_lines . each do |line |
88+ return false if line . split ( ':' ) [ 1 ] == '-'
89+ end
90+ true
91+ end
92+
93+ def self . key_line_hash ( pub_line , fpr_lines )
8994 pub_split = pub_line . split ( ':' )
90- fpr_split = fpr_line . split ( ':' )
95+ fpr_split = fpr_lines . first . split ( ':' )
9196
9297 fingerprint = fpr_split . last
9398 return_hash = {
@@ -97,6 +102,7 @@ def self.key_line_hash(pub_line, fpr_line)
97102 key_size : pub_split [ 2 ] ,
98103 key_type : nil ,
99104 key_created : Time . at ( pub_split [ 5 ] . to_i ) ,
105+ key_expired : pub_split [ 1 ] == 'e' ,
100106 key_expiry : pub_split [ 6 ] . empty? ? nil : Time . at ( pub_split [ 6 ] . to_i )
101107 }
102108
0 commit comments