Merge pull request #282 from laurenrother/security
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
1 # Class: apt
2 #
3 # This module manages the initial configuration of apt.
4 #
5 # Parameters:
6 #   The parameters listed here are not required in general and were
7 #     added for use cases related to development environments.
8 #   disable_keys - disables the requirement for all packages to be signed
9 #   always_apt_update - rather apt should be updated on every run (intended
10 #     for development environments where package updates are frequent)
11 #   purge_sources_list - Accepts true or false. Defaults to false If set to
12 #     true, Puppet will purge all unmanaged entries from sources.list
13 #   purge_sources_list_d - Accepts true or false. Defaults to false. If set
14 #     to true, Puppet will purge all unmanaged entries from sources.list.d
15 #   update_timeout - Overrides the exec timeout in seconds for apt-get update.
16 #     If not set defaults to Exec's default (300)
17 #   update_tries - Number of times that `apt-get update` will be tried. Use this
18 #     to work around transient DNS and HTTP errors. By default, the command
19 #     will only be run once.
20 #
21 # Actions:
22 #
23 # Requires:
24 #   puppetlabs/stdlib
25 # Sample Usage:
26 #  class { 'apt': }
27
28 class apt(
29   $always_apt_update    = false,
30   $disable_keys         = undef,
31   $proxy_host           = undef,
32   $proxy_port           = '8080',
33   $purge_sources_list   = false,
34   $purge_sources_list_d = false,
35   $purge_preferences    = false,
36   $purge_preferences_d  = false,
37   $update_timeout       = undef,
38   $update_tries         = undef,
39   $sources              = undef
40 ) {
41
42   if $::osfamily != 'Debian' {
43     fail('This module only works on Debian or derivatives like Ubuntu')
44   }
45
46   include apt::params
47   include apt::update
48
49   validate_bool($purge_sources_list, $purge_sources_list_d,
50                 $purge_preferences, $purge_preferences_d)
51
52   $sources_list_content = $purge_sources_list ? {
53     false => undef,
54     true  => "# Repos managed by puppet.\n",
55   }
56
57   $preferences_content = $purge_preferences ? {
58     false => undef,
59     true  => "Explanation: Preferences managed by Puppet\n
60 Explanation: We need a bogus package line because of Debian Bug #732746\n
61 Package: bogus-package\n",
62   }
63
64   if $always_apt_update == true {
65     Exec <| title=='apt_update' |> {
66       refreshonly => false,
67     }
68   }
69
70   $root           = $apt::params::root
71   $apt_conf_d     = $apt::params::apt_conf_d
72   $sources_list_d = $apt::params::sources_list_d
73   $preferences_d  = $apt::params::preferences_d
74   $provider       = $apt::params::provider
75
76   file { 'sources.list':
77     ensure  => present,
78     path    => "${root}/sources.list",
79     owner   => root,
80     group   => root,
81     mode    => '0644',
82     content => $sources_list_content,
83     notify  => Exec['apt_update'],
84   }
85
86   file { 'sources.list.d':
87     ensure  => directory,
88     path    => $sources_list_d,
89     owner   => root,
90     group   => root,
91     purge   => $purge_sources_list_d,
92     recurse => $purge_sources_list_d,
93     notify  => Exec['apt_update'],
94   }
95
96   file { 'apt-preferences':
97     ensure  => present,
98     path    => "${root}/preferences",
99     owner   => root,
100     group   => root,
101     mode    => '0644',
102     content => $preferences_content,
103   }
104
105   file { 'preferences.d':
106     ensure  => directory,
107     path    => $preferences_d,
108     owner   => root,
109     group   => root,
110     purge   => $purge_preferences_d,
111     recurse => $purge_preferences_d,
112   }
113
114   case $disable_keys {
115     true: {
116       file { '99unauth':
117         ensure  => present,
118         content => "APT::Get::AllowUnauthenticated 1;\n",
119         path    => "${apt_conf_d}/99unauth",
120       }
121     }
122     false: {
123       file { '99unauth':
124         ensure => absent,
125         path   => "${apt_conf_d}/99unauth",
126       }
127     }
128     undef:   { } # do nothing
129     default: { fail('Valid values for disable_keys are true or false') }
130   }
131
132   $proxy_set = $proxy_host ? {
133     undef   => absent,
134     default => present
135   }
136
137   file { 'configure-apt-proxy':
138     ensure  => $proxy_set,
139     path    => "${apt_conf_d}/proxy",
140     content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
141     notify  => Exec['apt_update'],
142     mode    => '0644',
143     owner   => root,
144     group   => root,
145   }
146
147   # Need anchor to provide containment for dependencies.
148   anchor { 'apt::update':
149     require => Class['apt::update'],
150   }
151
152   # manage sources if present
153   if $sources != undef {
154     validate_hash($sources)
155     create_resources('apt::source', $sources)
156   }
157 }