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