Merge pull request #209 from pabl0/proxy
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
1 # == Define: apt::key
2 #
3 # The apt::key defined type allows for keys to be added to apt's keyring
4 # which is used for package validation. This defined type uses the apt_key
5 # native type to manage keys. This is a simple wrapper around apt_key with
6 # a few safeguards in place.
7 #
8 # === Parameters
9 #
10 # [*key*]
11 #   _default_: +$title+, the title/name of the resource
12 #
13 #   Is a GPG key ID. This key ID is validated with a regex enforcing it
14 #   to only contain valid hexadecimal characters, be precisely 8 or 16
15 #   characters long and optionally prefixed with 0x.
16 #
17 # [*ensure*]
18 #   _default_: +present+
19 #
20 #   The state we want this key in, may be either one of:
21 #   * +present+
22 #   * +absent+
23 #
24 # [*key_content*]
25 #   _default_: +undef+
26 #
27 #   This parameter can be used to pass in a GPG key as a
28 #   string in case it cannot be fetched from a remote location
29 #   and using a file resource is for other reasons inconvenient.
30 #
31 # [*key_source*]
32 #   _default_: +undef+
33 #
34 #   This parameter can be used to pass in the location of a GPG
35 #   key. This URI can take the form of a:
36 #   * +URL+: ftp, http or https
37 #   * +path+: absolute path to a file on the target system.
38 #
39 # [*key_server*]
40 #   _default_: +undef+
41 #
42 #   The keyserver from where to fetch our GPG key. It defaults to
43 #   undef which results in apt_key's default keyserver being used,
44 #   currently +keyserver.ubuntu.com+.
45 #
46 # [*key_options*]
47 #   _default_: +undef+
48 #
49 #   Additional options to pass on to `apt-key adv --keyserver-options`.
50 define apt::key (
51   $key         = $title,
52   $ensure      = present,
53   $key_content = undef,
54   $key_source  = undef,
55   $key_server  = undef,
56   $key_options = undef,
57 ) {
58
59   validate_re($key, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z'])
60   validate_re($ensure, ['\Aabsent|present\Z',])
61
62   if $key_content {
63     validate_string($key_content)
64   }
65
66   if $key_source {
67     validate_re($key_source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
68   }
69
70   if $key_server {
71     if !is_domain_name($key_server) {
72       fail('$key_server must be a valid domain name')
73     }
74   }
75
76   if $key_options {
77     validate_string($key_options)
78   }
79
80   case $ensure {
81     present: {
82       if defined(Anchor["apt_key ${key} absent"]){
83         fail("key with id ${key} already ensured as absent")
84       }
85
86       if !defined(Anchor["apt_key ${key} present"]) {
87         apt_key { $title:
88           ensure            => $ensure,
89           id                => $key,
90           source            => $key_source,
91           content           => $key_content,
92           server            => $key_server,
93           keyserver_options => $key_options,
94         } ->
95         anchor { "apt_key ${key} present": }
96       }
97     }
98
99     absent: {
100       if defined(Anchor["apt_key ${key} present"]){
101         fail("key with id ${key} already ensured as present")
102       }
103
104       if !defined(Anchor["apt_key ${key} absent"]){
105         apt_key { $title:
106           ensure            => $ensure,
107           id                => $key,
108           source            => $key_source,
109           content           => $key_content,
110           server            => $key_server,
111           keyserver_options => $key_options,
112         } ->
113         anchor { "apt_key ${key} absent": }
114       }
115     }
116
117     default: {
118       fail "Invalid 'ensure' value '${ensure}' for apt::key"
119     }
120   }
121 }