Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service filename bugfix #266

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 120 additions & 7 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

**Classes**

* [`firewalld`](#firewalld): == Class: firewalld Manage the firewalld service See the README.md for usage instructions for the firewalld_zone and firewalld_rich_rule ty
* [`firewalld`](#firewalld): Manage the firewalld service
* [`firewalld::reload`](#firewalldreload): A common point for triggering an intermediary firewalld reload using firewall-cmd
* [`firewalld::reload::complete`](#firewalldreloadcomplete): A common point for triggering an intermediary firewalld full reload using firewall-cmd

**Defined types**

Expand All @@ -23,13 +25,13 @@
* [`firewalld_service`](#firewalld_service): Assigns a service to a specific firewalld zone.
* [`firewalld_zone`](#firewalld_zone): Creates and manages firewalld zones.

## Classes
**Functions**

### firewalld
* [`firewalld::safe_filename`](#firewalldsafe_filename): Returns a string that is safe for firewalld filenames

== Class: firewalld
## Classes

Manage the firewalld service
### firewalld

See the README.md for usage instructions for the firewalld_zone and
firewalld_rich_rule types
Expand All @@ -48,8 +50,6 @@ firewalld_rich_rule types
install_gui => true,
}



=== Authors

Craig Dunn <craig@craigdunn.org>
Expand Down Expand Up @@ -294,6 +294,14 @@ Data type: `Optional[String]`

Default value: `undef`

### firewalld::reload

A common point for triggering an intermediary firewalld reload using firewall-cmd

### firewalld::reload::complete

A common point for triggering an intermediary firewalld full reload using firewall-cmd

## Defined types

### firewalld::custom_service
Expand Down Expand Up @@ -982,3 +990,108 @@ Description of the zone to add

Short description of the zone to add

## Functions

### firewalld::safe_filename

Type: Puppet Language

Returns a string that is safe for firewalld filenames

#### Examples

##### Regular Filename

```puppet
$filename = 'B@d Characters!'
firewalld::safe_filename($orig_string)

Result => 'B_d_Characters_'
```

##### Filename with Options

```puppet
$filename = 'B@d Characters!.txt'
firewalld::safe_filename(
$filename,
{
'replacement_string' => '--',
'file_extension' => '.txt'
}
)

Result => 'B--d--Characters--.txt'
```

#### `firewalld::safe_filename(String[1] $filename, Struct[
{
'replacement_string' => Pattern[/[\w-]/],
'file_extension' => Optional[String[1]]
}
] $options = { 'replacement_string' => '_'})`

The firewalld::safe_filename function.

Returns: `String` Processed string

##### Examples

###### Regular Filename

```puppet
$filename = 'B@d Characters!'
firewalld::safe_filename($orig_string)

Result => 'B_d_Characters_'
```

###### Filename with Options

```puppet
$filename = 'B@d Characters!.txt'
firewalld::safe_filename(
$filename,
{
'replacement_string' => '--',
'file_extension' => '.txt'
}
)

Result => 'B--d--Characters--.txt'
```

##### `filename`

Data type: `String[1]`

The String to process

##### `options`

Data type: `Struct[
{
'replacement_string' => Pattern[/[\w-]/],
'file_extension' => Optional[String[1]]
}
]`

Various processing options

Options:

* **file_extension** `String[1]`: This will be stripped from the end of the string prior to processing and
re-added afterwards

##### `options`

Data type: `String[1]`

replacement_string
The String to use when replacing invalid characters

Options:

* **file_extension** `String[1]`: This will be stripped from the end of the string prior to processing and
re-added afterwards

72 changes: 72 additions & 0 deletions functions/safe_filename.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# @summary Returns a string that is safe for firewalld filenames
trevor-vaughan marked this conversation as resolved.
Show resolved Hide resolved
#
# @example Regular Filename
# $filename = 'B@d Characters!'
# firewalld::safe_filename($orig_string)
#
# Result => 'B_d_Characters_'
#
# @example Filename with Options
# $filename = 'B@d Characters!.txt'
# firewalld::safe_filename(
# $filename,
# {
# 'replacement_string' => '--',
# 'file_extension' => '.txt'
trevor-vaughan marked this conversation as resolved.
Show resolved Hide resolved
# }
# )
#
# Result => 'B--d--Characters--.txt'
#
# @param filename
# The String to process
#
# @param options
# Various processing options
#
# @param options [String[1]] replacement_string
# The String to use when replacing invalid characters
#
# @option options [String[1]] file_extension
# This will be stripped from the end of the string prior to processing and
# re-added afterwards
#
# @return [String]
# Processed string
#
function firewalld::safe_filename(
String[1] $filename,
Struct[
{
'replacement_string' => Pattern[/[\w-]/],
'file_extension' => Optional[String[1]]
}
] $options = { 'replacement_string' => '_'}
) {

$_badchar_regex = '[^\w-]'

# If we have an extension defined
if $options['file_extension'] {

# See if the string ends with the extension
$_extension_length = length($options['file_extension'])
if $filename[-($_extension_length), -1] == $options['file_extension'] {

# And extract the base filename
$_basename = $filename[0, -($_extension_length) - 1]
}
}

# If we extraced a base filename substitute on that and re-add the file extension
if defined('$_basename') {
sprintf('%s%s',
regsubst($_basename, $_badchar_regex, $options['replacement_string'], 'G'),
$options['file_extension']
)
}
# Otherwise, just substitute on the original filename
else {
regsubst($filename, $_badchar_regex, $options['replacement_string'], 'G')
}
}
4 changes: 4 additions & 0 deletions lib/puppet/provider/firewalld_zone/firewall_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,8 @@ def short
def short=(new_short)
execute_firewall_cmd(['--set-short', new_short], @resource[:name], true, false)
end

def flush
reload_firewall
end
end
9 changes: 9 additions & 0 deletions lib/puppet/type/firewalld_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def generate

newparam(:name) do
desc 'Name of the rule resource in Puppet'
isnamevar
end

newparam(:zone) do
Expand Down Expand Up @@ -165,6 +166,14 @@ def retrieve
end
end

validate do
[:zone, :name].each do |attr|
if self[attr] && (self[attr]).to_s.length > 17
raise(Puppet::Error, "Zone identifier '#{attr}' must be less than 18 characters long")
end
end
end

autorequire(:service) do
['firewalld']
end
Expand Down
32 changes: 21 additions & 11 deletions manifests/custom_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,28 @@
Enum['present','absent'] $ensure = 'present',
) {

file{"${config_dir}/${filename}.xml":
include firewalld::reload

# Service files may only contain alphanumeric characters and underscores.
# This is not documented, but has been experimentally confirmed.
$_safe_filename = firewalld::safe_filename($filename)

$_content = epp(
"${module_name}/service.xml.epp",
'short' => $short,
'description' => $description,
'port' => $port,
'module' => $module,
'destination' => $destination,
'filename' => $filename,
'config_dir' => $config_dir,
'ensure' => $ensure
)

file{ "${config_dir}/${_safe_filename}.xml":
ensure => $ensure,
content => template('firewalld/service.xml.erb'),
content => $_content,
mode => '0644',
notify => Exec["firewalld::custom_service::reload-${name}"],
notify => Class['firewalld::reload'],
}

exec{ "firewalld::custom_service::reload-${name}":
path =>'/usr/bin:/bin',
command => 'firewall-cmd --reload',
onlyif => 'firewall-cmd --state',
refreshonly => true,
}

}
Loading