faa430be4263132488fd3448f27733b0990b8a2a
[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     raise(_('ensure => absent and refresh => true are mutually exclusive')) if self[:refresh] == true && self[:ensure] == :absent
29     raise(_('The properties content and source are mutually exclusive.')) if self[:content] && self[:source]
30
31     warning(_('The id should be a full fingerprint (40 characters), see README.')) if self[:id].length < 40
32   end
33
34   newparam(:id, namevar: true) do
35     desc 'The ID of the key you want to manage.'
36     # GPG key ID's should be either 32-bit (short) or 64-bit (long) key ID's
37     # and may start with the optional 0x, or they can be 40-digit key fingerprints
38     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})
39     munge do |value|
40       id = if value.start_with?('0x')
41              value.partition('0x').last.upcase
42            else
43              value.upcase
44            end
45       id
46     end
47   end
48
49   newparam(:content) do
50     desc 'The content of, or string representing, a GPG key.'
51   end
52
53   newparam(:source) do
54     desc 'Location of a GPG key file, /path/to/file, ftp://, http:// or https://'
55     newvalues(%r{\Ahttps?://}, %r{\Aftp://}, %r{\A/\w+})
56   end
57
58   autorequire(:file) do
59     self[:source] if self[:source] && Pathname.new(self[:source]).absolute?
60   end
61
62   newparam(:server) do
63     desc 'The key server to fetch the key from based on the ID. It can either be a domain name or url.'
64     defaultto :'keyserver.ubuntu.com'
65
66     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\-_.]+)*/?$})
67   end
68
69   newparam(:options) do
70     desc 'Additional options to pass to apt-key\'s --keyserver-options.'
71   end
72
73   newparam(:refresh, boolean: true, parent: Puppet::Parameter::Boolean) do
74     desc 'When true, recreate an existing expired key'
75     defaultto false
76   end
77
78   newparam(:weak_ssl, boolean: true, parent: Puppet::Parameter::Boolean) do
79     desc 'When true and source uses https, accepts download of keys without SSL verification'
80     defaultto false
81   end
82
83   newproperty(:fingerprint) do
84     desc <<-MANIFEST
85       The 40-digit hexadecimal fingerprint of the specified GPG key.
86
87       This property is read-only.
88     MANIFEST
89   end
90
91   newproperty(:long) do
92     desc <<-MANIFEST
93       The 16-digit hexadecimal id of the specified GPG key.
94
95       This property is read-only.
96     MANIFEST
97   end
98
99   newproperty(:short) do
100     desc <<-MANIFEST
101       The 8-digit hexadecimal id of the specified GPG key.
102
103       This property is read-only.
104     MANIFEST
105   end
106
107   newproperty(:expired) do
108     desc <<-MANIFEST
109       Indicates if the key has expired.
110
111       This property is read-only.
112     MANIFEST
113   end
114
115   newproperty(:expiry) do
116     desc <<-MANIFEST
117       The date the key will expire, or nil if it has no expiry date.
118
119       This property is read-only.
120     MANIFEST
121   end
122
123   newproperty(:size) do
124     desc <<-MANIFEST
125       The key size, usually a multiple of 1024.
126
127       This property is read-only.
128     MANIFEST
129   end
130
131   newproperty(:type) do
132     desc <<-MANIFEST
133       The key type, one of: rsa, dsa, ecc, ecdsa
134
135       This property is read-only.
136     MANIFEST
137   end
138
139   newproperty(:created) do
140     desc <<-MANIFEST
141       Date the key was created.
142
143       This property is read-only.
144     MANIFEST
145   end
146 end