Merge pull request #400 from voidus/master
[puppet-modules/puppetlabs-apt.git] / lib / puppet / type / apt_key.rb
1 require 'pathname'
2
3 Puppet::Type.newtype(:apt_key) do
4
5   @doc = <<-EOS
6     This type provides Puppet with the capabilities to manage GPG keys needed
7     by apt to perform package validation. Apt has it's own GPG keyring that can
8     be manipulated through the `apt-key` command.
9
10     apt_key { '4BD6EC30':
11       source => 'http://apt.puppetlabs.com/pubkey.gpg'
12     }
13
14     **Autorequires**:
15
16     If Puppet is given the location of a key file which looks like an absolute
17     path this type will autorequire that file.
18   EOS
19
20   ensurable
21
22   validate do
23     if self[:content] and self[:source]
24       fail('The properties content and source are mutually exclusive.')
25     end
26   end
27
28   newparam(:id, :namevar => true) do
29     desc 'The ID of the key you want to manage.'
30     # GPG key ID's should be either 32-bit (short) or 64-bit (long) key ID's
31     # and may start with the optional 0x
32     newvalues(/\A(0x)?[0-9a-fA-F]{8}\Z/, /\A(0x)?[0-9a-fA-F]{16}\Z/)
33     munge do |value|
34       if value.start_with?('0x')
35         id = value.partition('0x').last.upcase
36       else
37         id = value.upcase
38       end
39       id
40     end
41   end
42
43   newparam(:content) do
44     desc 'The content of, or string representing, a GPG key.'
45   end
46
47   newparam(:source) do
48     desc 'Location of a GPG key file, /path/to/file, ftp://, http:// or https://'
49     newvalues(/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/)
50   end
51
52   autorequire(:file) do
53     if self[:source] and Pathname.new(self[:source]).absolute?
54       self[:source]
55     end
56   end
57
58   newparam(:server) do
59     desc 'The key server to fetch the key from based on the ID. It can either be a domain name or url.'
60     defaultto :'keyserver.ubuntu.com'
61     
62     newvalues(/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/)
63   end
64
65   newparam(:keyserver_options) do
66     desc 'Additional options to pass to apt-key\'s --keyserver-options.'
67   end
68
69   newproperty(:expired) do
70     desc <<-EOS
71       Indicates if the key has expired.
72
73       This property is read-only.
74     EOS
75   end
76
77   newproperty(:expiry) do
78     desc <<-EOS
79       The date the key will expire, or nil if it has no expiry date.
80
81       This property is read-only.
82     EOS
83   end
84
85   newproperty(:size) do
86     desc <<-EOS
87       The key size, usually a multiple of 1024.
88
89       This property is read-only.
90     EOS
91   end
92
93   newproperty(:type) do
94     desc <<-EOS
95       The key type, either RSA or DSA.
96
97       This property is read-only.
98     EOS
99   end
100
101   newproperty(:created) do
102     desc <<-EOS
103       Date the key was created.
104
105       This property is read-only.
106     EOS
107   end
108 end