Puppet Class: crowdsec

Inherits:
crowdsec::params
Defined in:
manifests/init.pp

Summary

Install and manage crowdsec

Overview

The whole config part that should go into config.yaml.local. $config[‘server’] is overwritten in case $local_api_puppet_certname is set and == $trusted

Setup apt sources from the crowdsec repositories. Defaults to true.

The local api url crowdsec should connect to. Defaults to 127.0.0.1:8080

The login/user used to authenticate against the local api server.

The password used to login on the local api server.

Use a hash over fqdn and password instead of the puppet certname. This sounds weird, but it makes sure that we update user/password in case the password changes. There is not way to verify an existing password unfortunately. Don’t disable if you plan to connect to the central API.

If this option is set and matches $trusted, enable the local api and collect host registrations exported for that certname.

Nobody reads the documentation. If you actually did so, raise this number: 0 Pull requests for it are fine!

Set this to true if you really want to run the local api server without TLS. Absolutely not recommended.

Register machine automatically if $local_api_url and $local_api_puppet_certname is configured properly.

Configure crowdsec to run as LAPI server

Defaults to true, when false we configure a user/group for crowdsec.

Update packages from the crowdsec hub automatically. Defaults to true.

Base directory for all crowdsec config files.

Name of the service used to control the crowdsec daemon.

Remove modules/configs that are not installed by puppet. Keep in mind that this will break collections - you will have to list everything contained by a collection manually.

Either the name of the module or an array, containing the module name and all the params to pass to crowdsec::module to install the module.

See parsers

See parsers

See parsers

See parsers

See parsers

See parsers

Examples:

include crowdsec

Parameters:

  • config (Hash) (defaults to: {})
  • manage_sources (Boolean) (defaults to: true)
  • local_api_url (Stdlib::HTTPUrl) (defaults to: 'http://127.0.0.1:8080')
  • local_api_login (String) (defaults to: if $use_anonymous_api_logins { sha256("${trusted['certname']} ${local_api_password}") } else { $trusted['certname'])
  • local_api_password (Sensitive[String]) (defaults to: Sensitive( fqdn_rand_string( 32, undef, $facts['networking']['mac'], ) ))
  • use_anonymous_api_logins (Boolean) (defaults to: true)
  • local_api_puppet_certname (Optional[Stdlib::Fqdn]) (defaults to: undef)
  • force_local_api_no_tls (Boolean) (defaults to: false)
  • register_machine (Boolean) (defaults to: ($local_api_url != 'http://127.0.0.1:8080') and $local_api_puppet_certname)
  • enable_local_api (Boolean) (defaults to: $local_api_puppet_certname and $local_api_puppet_certname == $trusted['certname'])
  • run_as_root (Boolean) (defaults to: !$enable_local_api)
  • automatic_hub_updates (Boolean) (defaults to: true)
  • config_basedir (Stdlib::Absolutepath) (defaults to: $crowdsec::params::config_basedir)
  • service_name (String) (defaults to: $crowdsec::params::service_name)
  • manage_modules (Boolean) (defaults to: false)
  • parsers (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • postoverflows (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • scenarios (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • contexts (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • appsec_configs (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • appsec_rules (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [])
  • collections (Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0]) (defaults to: [ 'crowdsecurity/linux', 'crowdsecurity/sshd', ])


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'manifests/init.pp', line 89

class crowdsec (
  Hash $config = {},
  Boolean $manage_sources = true,
  Stdlib::HTTPUrl $local_api_url = 'http://127.0.0.1:8080',
  Boolean $use_anonymous_api_logins = true,
  Optional[Stdlib::Fqdn] $local_api_puppet_certname = undef,
  Sensitive[String] $local_api_password = Sensitive(
    fqdn_rand_string(
      32,
      undef,
      $facts['networking']['mac'],
    )
  ),
  String $local_api_login = if $use_anonymous_api_logins {
    sha256("${trusted['certname']} ${local_api_password}")
  } else {
    $trusted['certname']
  },
  Boolean $force_local_api_no_tls = false,
  Boolean $register_machine = ($local_api_url != 'http://127.0.0.1:8080') and $local_api_puppet_certname,
  Boolean $enable_local_api = $local_api_puppet_certname and $local_api_puppet_certname == $trusted['certname'],
  Boolean $run_as_root = !$enable_local_api,
  Boolean $automatic_hub_updates = true,
  Stdlib::Absolutepath $config_basedir = $crowdsec::params::config_basedir,
  String $service_name = $crowdsec::params::service_name,
  Boolean $manage_modules = false,
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $appsec_configs = [],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $appsec_rules = [],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $collections = [
    'crowdsecurity/linux',
    'crowdsecurity/sshd',
  ],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $contexts = [],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $parsers = [],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $postoverflows = [],
  Tuple[Variant[Crowdsec::Module_name, Tuple[Crowdsec::Module_name, Hash, 2, 2]], 0] $scenarios = [],
) inherits crowdsec::params {
  if $run_as_root {
    $user = 'root'
    $group = 'root'
  } else {
    $user = 'crowdsec'
    $group = 'crowdsec'

    group { $group:
      system => true,
    }
    user { $user:
      system => true,
      home   => '/var/lib/crowdsec',
      gid    => $group,
    }

    systemd::manage_dropin { 'crowdsec_as_non_root.conf':
      unit          => $service_name,
      notify        => Service[$service_name],
      service_entry => {
        'User'                => $user,
        'Group'               => $group,
        'AmbientCapabilities' => 'CAP_NET_BIND_SERVICE',
      },
    }
  }

  file { [$config_basedir, '/var/log/crowdsec', '/var/lib/crowdsec']:
    ensure  => directory,
    owner   => $user,
    group   => $group,
    recurse => true,
  }

  if $manage_sources {
    include crowdsec::sources
    Class['crowdsec::sources'] -> Package['crowdsec']
  }

  $default_config = {
    'common' => {
      'log_dir' => '/var/log/crowdsec',
    },
  }
  $local_api_config = {
    'api' => {
      'server' => {
        'enable' => $enable_local_api,
      },
    },
  }

  $local_config = $default_config + $local_api_config + $config
  if !$force_local_api_no_tls and $enable_local_api {
    $tls_cert = $local_config.dig('api', 'server', 'tls', 'cert_file')
    $tls_key = $local_config.dig('api', 'server', 'tls', 'key_file')
    if !($tls_cert and $tls_key) {
      fail('Please configure TLS for the crodsec local API (or set $force_local_api_no_tls to true).')
    }
  }

  package { 'crowdsec':
    # this is necessary to make sure modules don't show up as tainted just because
    # the engine is too old.
    ensure => latest,
  }

  service { $service_name:
    ensure  => 'running',
    enable  => 'true',
    require => Package['crowdsec'],
  }

  file { "${config_basedir}/config.yaml.local":
    ensure  => file,
    owner   => $user,
    group   => $group,
    mode    => '0640',
    content => to_yaml($local_config),
    require => Package['crowdsec'],
    notify  => Service['crowdsec.service'],
  }

  if $enable_local_api {
    include crowdsec::local_api
  }

  @@crowdsec::local_api::register { $local_api_login :
    password => $local_api_password.unwrap,
    tag      => $local_api_puppet_certname,
  }

  file { "${config_basedir}/local_api_credentials.yaml":
    ensure  => file,
    owner   => $user,
    group   => $group,
    mode    => '0600',
    content => to_yaml(
      {
        'url'      => $local_api_url,
        'login'    => $local_api_login,
        'password' => $local_api_password.unwrap,
      }
    ),
    require => Package['crowdsec'],
    notify  => Service['crowdsec.service'],
  }

  [
    'parsers',
    'postoverflows',
    'scenarios',
    'contexts',
    'appsec-configs',
    'appsec-rules',
    'collections',
  ].each |$module_type| {
    $_varname = regsubst($module_type, /-/, '_', 'G')
    getvar($_varname).each|$module| {
      if $module =~ Array {
        crowdsec::module { $module[0]:
          * => { 'module_type' => $module_type } + $module[1],
        }
      } else {
        crowdsec::module { "${module_type}-${module}":
          module_type => $module_type,
          module      => $module,
        }
      }
    }
    if $manage_modules {
      $_modules = getvar($_varname).map|$module| {
        if $module =~ Array {
          $module[0]
        } else {
          $module
        }
      }
      $_uninstall_modules = pick_default($facts.dig('crowdsec', $module_type), []).filter|$_m| {
        $_m['status'] =~ /enabled/
      }.map|$_m| {
        $_m['name']
      }.filter|$_m| {
        !($_m in $_modules)
      }.each|$_m| {
        crowdsec::module { "${module_type}-${_m}":
          ensure      => absent,
          module_type => $module_type,
          module_name => $_m,
        }
      }
    }
  }
}