ca88f867eab26918b43ad729dd29a7b53ec754e2
[puppet-modules/puppetlabs-apt.git] / lib / puppet / type / apt_key.rb
1 require 'pathname'
2 require 'puppet/parameter/boolean'
3
4 Puppet::Type.newtype(:apt_key) do
5   @doc = <<-MANIFEST
6     @summary 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     @example Basic usage
11       apt_key { '6F6B15509CF8E59E6E469F327F438280EF8D349F':
12         source => 'http://apt.puppetlabs.com/pubkey.gpg'
13       }
14
15     **Autorequires**
16
17     If Puppet is given the location of a key file which looks like an absolute
18     path this type will autorequire that file.
19
20     @api private
21   MANIFEST
22
23   ensurable
24
25   validate do
26     if self[:refresh] == true && self[:ensure] == :absent
27       raise(_('ensure => absent and refresh => true are mutually exclusive'))
28     end
29     if self[:content] && self[:source]
30       raise(_('The properties content and source are mutually exclusive.'))
31     end
32     if self[:id].length < 40
33       warning(_('The id should be a full fingerprint (40 characters), see README.'))
34     end
35   end
36
37   newparam(:id, namevar: true) do
38     desc 'The ID of the key you want to manage.'
39     # GPG key ID's should be either 32-bit (short) or 64-bit (long) key ID's
40     # and may start with the optional 0x, or they can be 40-digit key fingerprints
41     newvalues(%r{\A(0x)?[0-9a-fA-F]{8}\Z}, %r{\A(0x)?[0-9a-fA-F]{16}\Z}, %r{\A(0x)?[0-9a-fA-F]{40}\Z})
42     munge do |value|
43       id = if value.start_with?('0x')
44              value.partition('0x').last.upcase
45            else
46              value.upcase
47            end
48       id
49     end
50   end
51
52   newparam(:content) do
53     desc 'The content of, or string representing, a GPG key.'
54   end
55
56   newparam(:source) do
57     desc 'Location of a GPG key file, /path/to/file, ftp://, http:// or https://'
58     newvalues(%r{\Ahttps?://}, %r{\Aftp://}, %r{\A/\w+})
59   end
60
61   autorequire(:file) do
62     if self[:source] && Pathname.new(self[:source]).absolute?
63       self[:source]
64     end
65   end
66
67   autorequire(:package) do
68     'dirmngr'
69   end
70
71   newparam(:server) do
72     desc 'The key server to fetch the key from based on the ID. It can either be a domain name or url.'
73     defaultto :'keyserver.ubuntu.com'
74
75     newvalues(%r{\A((hkp|hkps|http|https)://)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$})
76   end
77
78   newparam(:options) do
79     desc 'Additional options to pass to apt-key\'s --keyserver-options.'
80   end
81
82   newparam(:refresh, boolean: true, parent: Puppet::Parameter::Boolean) do
83     desc 'When true, recreate an existing expired key'
84     defaultto false
85   end
86
87   newproperty(:fingerprint) do
88     desc <<-MANIFEST
89       The 40-digit hexadecimal fingerprint of the specified GPG key.
90
91       This property is read-only.
92     MANIFEST
93   end
94
95   newproperty(:long) do
96     desc <<-MANIFEST
97       The 16-digit hexadecimal id of the specified GPG key.
98
99       This property is read-only.
100     MANIFEST
101   end
102
103   newproperty(:short) do
104     desc <<-MANIFEST
105       The 8-digit hexadecimal id of the specified GPG key.
106
107       This property is read-only.
108     MANIFEST
109   end
110
111   newproperty(:expired) do
112     desc <<-MANIFEST
113       Indicates if the key has expired.
114
115       This property is read-only.
116     MANIFEST
117   end
118
119   newproperty(:expiry) do
120     desc <<-MANIFEST
121       The date the key will expire, or nil if it has no expiry date.
122
123       This property is read-only.
124     MANIFEST
125   end
126
127   newproperty(:size) do
128     desc <<-MANIFEST
129       The key size, usually a multiple of 1024.
130
131       This property is read-only.
132     MANIFEST
133   end
134
135   newproperty(:type) do
136     desc <<-MANIFEST
137       The key type, one of: rsa, dsa, ecc, ecdsa
138
139       This property is read-only.
140     MANIFEST
141   end
142
143   newproperty(:created) do
144     desc <<-MANIFEST
145       Date the key was created.
146
147       This property is read-only.
148     MANIFEST
149   end
150 end