Support managing login configurations in /etc/apt/auth.conf
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
1 # == Define: apt::key
2 define apt::key (
3     String $id                           = $title,
4     Enum['present', 'absent'] $ensure    = present,
5     Optional[String] $content            = undef,
6     Optional[String] $source             = undef,
7     String $server                       = $::apt::keyserver,
8     Optional[String] $options            = undef,
9     ) {
10
11   assert_type(
12     Pattern[
13       /\A(0x)?[0-9a-fA-F]{8}\Z/,
14       /\A(0x)?[0-9a-fA-F]{16}\Z/,
15       /\A(0x)?[0-9a-fA-F]{40}\Z/,
16     ], $id)
17
18   if $source {
19     assert_type(Pattern[/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/], $source)
20   }
21
22   if $server {
23     assert_type(Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/], $server)
24   }
25
26   case $ensure {
27     present: {
28       if defined(Anchor["apt_key ${id} absent"]){
29         fail("key with id ${id} already ensured as absent")
30       }
31
32       if !defined(Anchor["apt_key ${id} present"]) {
33         apt_key { $title:
34           ensure  => $ensure,
35           id      => $id,
36           source  => $source,
37           content => $content,
38           server  => $server,
39           options => $options,
40         } -> anchor { "apt_key ${id} present": }
41
42         case $facts['os']['name'] {
43           'Debian': {
44             if versioncmp($facts['os']['release']['major'], '9') >= 0 {
45               ensure_packages(['dirmngr'])
46               Apt::Key<| title == $title |>
47             }
48           }
49           'Ubuntu': {
50             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
51               ensure_packages(['dirmngr'])
52               Apt::Key<| title == $title |>
53             }
54           }
55           default: { }
56         }
57       }
58     }
59
60     absent: {
61       if defined(Anchor["apt_key ${id} present"]){
62         fail("key with id ${id} already ensured as present")
63       }
64
65       if !defined(Anchor["apt_key ${id} absent"]){
66         apt_key { $title:
67           ensure  => $ensure,
68           id      => $id,
69           source  => $source,
70           content => $content,
71           server  => $server,
72           options => $options,
73         } -> anchor { "apt_key ${id} absent": }
74       }
75     }
76
77     default: {
78       fail "Invalid 'ensure' value '${ensure}' for apt::key"
79     }
80   }
81 }