Skip to content

Commit

Permalink
Merge pull request #224 from Dan33l/logpathmulti
Browse files Browse the repository at this point in the history
jail logpath can handle several files
  • Loading branch information
Dan33l authored Sep 20, 2024
2 parents c1d8422 + 3dd5c7f commit 7a33d2e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
25 changes: 16 additions & 9 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

### Data types

* [`Fail2ban::Logpath`](#Fail2ban--Logpath): Describes logpath format allowed
* [`Fail2ban::Time`](#Fail2ban--Time): Describes time format allowed for bantime and findtime The time entries in fail2ban configuration (like findtime or bantime) can be provided

### Tasks
Expand Down Expand Up @@ -496,6 +497,7 @@ Handles the jails.

The following parameters are available in the `fail2ban::jail` defined type:

* [`logpath`](#-fail2ban--jail--logpath)
* [`filter_includes`](#-fail2ban--jail--filter_includes)
* [`filter_failregex`](#-fail2ban--jail--filter_failregex)
* [`filter_ignoreregex`](#-fail2ban--jail--filter_ignoreregex)
Expand All @@ -505,7 +507,6 @@ The following parameters are available in the `fail2ban::jail` defined type:
* [`enabled`](#-fail2ban--jail--enabled)
* [`action`](#-fail2ban--jail--action)
* [`filter`](#-fail2ban--jail--filter)
* [`logpath`](#-fail2ban--jail--logpath)
* [`maxretry`](#-fail2ban--jail--maxretry)
* [`findtime`](#-fail2ban--jail--findtime)
* [`bantime`](#-fail2ban--jail--bantime)
Expand All @@ -521,6 +522,14 @@ The following parameters are available in the `fail2ban::jail` defined type:
* [`config_file_notify`](#-fail2ban--jail--config_file_notify)
* [`config_file_require`](#-fail2ban--jail--config_file_require)

##### <a name="-fail2ban--jail--logpath"></a>`logpath`

Data type: `Optional[Fail2ban::Logpath]`

Filename(s) of the log files to be monitored

Default value: `undef`

##### <a name="-fail2ban--jail--filter_includes"></a>`filter_includes`

Data type: `Optional[String]`
Expand Down Expand Up @@ -593,14 +602,6 @@ Data type: `String`

Default value: `$title`

##### <a name="-fail2ban--jail--logpath"></a>`logpath`

Data type: `Optional[String[1]]`



Default value: `undef`

##### <a name="-fail2ban--jail--maxretry"></a>`maxretry`

Data type: `Integer`
Expand Down Expand Up @@ -715,6 +716,12 @@ Default value: `$fail2ban::config_file_require`

## Data types

### <a name="Fail2ban--Logpath"></a>`Fail2ban::Logpath`

Describes logpath format allowed

Alias of `Variant[String[1], Array[String[1]]]`

### <a name="Fail2ban--Time"></a>`Fail2ban::Time`

Describes time format allowed for bantime and findtime
Expand Down
4 changes: 3 additions & 1 deletion manifests/jail.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# @summary Handles the jails.
#
# @param logpath Filename(s) of the log files to be monitored
#
define fail2ban::jail (
Optional[String] $filter_includes = undef,
Optional[String] $filter_failregex = undef,
Expand All @@ -10,7 +12,7 @@
Boolean $enabled = true,
Optional[String] $action = undef,
String $filter = $title,
Optional[String[1]] $logpath = undef,
Optional[Fail2ban::Logpath] $logpath = undef,
Integer $maxretry = $fail2ban::maxretry,
Optional[Fail2ban::Time] $findtime = undef,
Fail2ban::Time $bantime = $fail2ban::bantime,
Expand Down
33 changes: 25 additions & 8 deletions spec/defines/fail2ban_jail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
describe 'fail2ban::jail' do
let(:title) { 'spec_test_jail' }
let(:pre_condition) { 'include fail2ban' }
let(:common_params) do
{
'logpath' => '/var/log/syslog',
'filter_failregex' => 'Login failed for user .* from <HOST>',
'filter_maxlines' => 10,
'filter_datepattern' => '%%Y-%%m-%%d %%H:%%M(?::%%S)?'
}
end

on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts
end

let(:params) do
{
'logpath' => '/var/log/syslog',
'filter_failregex' => 'Login failed for user .* from <HOST>',
'filter_maxlines' => 10,
'filter_datepattern' => '%%Y-%%m-%%d %%H:%%M(?::%%S)?'
}
end
let(:params) { common_params }

it do
is_expected.to compile.with_all_deps
Expand All @@ -33,6 +34,22 @@
)
end

context 'with jail using several files in logpath' do
let(:params) do
common_params.merge(
'logpath' => ['/var/log/syslog', '/var/log/syslog.1']
)
end

it do
is_expected.to contain_file('custom_jail_spec_test_jail').with(
'ensure' => 'file',
'notify' => 'Service[fail2ban]',
'content' => %r{logpath = /var/log/syslog\n /var/log/syslog\.1\n}
)
end
end

it do
is_expected.to contain_file('custom_filter_spec_test_jail').with(
'ensure' => 'file',
Expand Down
13 changes: 13 additions & 0 deletions spec/type_aliases/logpath_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Fail2ban::Logpath' do
[
'/var/log/file.log',
'/var/log/file.log[1-9]',
['/var/log/file.log', '/var/log/file.log.1'],
].each do |allowed_value|
it { is_expected.to allow_value(allowed_value) }
end
end
6 changes: 6 additions & 0 deletions templates/common/custom_jail.conf.epp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ enabled = <%= $enabled %>
action = <%= $action %>
<% } -%>
filter = <%= $filter %>
<% if $logpath =~ Array[String[1]] { -%>
logpath = <%- $logpath.each |$l| { -%>
<%= $l %>
<%- } -%>
<% } else { -%>
logpath = <%= $logpath %>
<%- } -%>
maxretry = <%= $maxretry %>
<% if $findtime { -%>
findtime = <%= $findtime %>
Expand Down
5 changes: 5 additions & 0 deletions types/logpath.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Describes logpath format allowed
type Fail2ban::Logpath = Variant[
String[1],
Array[String[1]],
]

0 comments on commit 7a33d2e

Please sign in to comment.