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