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