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

Prevent multiple spaces in sysctl value #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
$_sysctl_d_file = "${title}${suffix}"
}

# Some sysctl values can contain spaces. However, the output of
# `sysctl -n <key>` may contain tabs instead. Therefore, we replace
# all whitespace characters with a single space to avoid mismatches.
$_value = is_string($value) ? {
true => strip(regsubst($value, '\\s+', ' ', 'G')),
default => $value
}

# Some sysctl keys contain a slash, which is not valid in a filename.
# Most common at those on VLANs: net.ipv4.conf.eth0/1.arp_accept = 0
$sysctl_d_file = regsubst($_sysctl_d_file, '[/ ]', '_', 'G')
Expand Down Expand Up @@ -75,7 +83,7 @@

# For the few original values from the main file
exec { "update-sysctl.conf-${title}":
command => "sed -i -e 's#^${title} *=.*#${title} = ${value}#' /etc/sysctl.conf",
command => "sed -i -e 's#^${title} *=.*#${title} = ${_value}#' /etc/sysctl.conf",
path => [ '/usr/sbin', '/sbin', '/usr/bin', '/bin' ],
refreshonly => true,
onlyif => "grep -E '^${title} *=' /etc/sysctl.conf",
Expand All @@ -87,10 +95,10 @@
# Value may contain '|' and others, we need to quote to be safe
# Convert any numerical to expected string, 0 instead of '0' would fail
# lint:ignore:only_variable_string Convert numerical to string
$qvalue = shellquote("${value}")
$qvalue = shellquote("${_value}")
# lint:endignore
exec { "enforce-sysctl-value-${qtitle}":
unless => "/usr/bin/test \"$(/sbin/sysctl -n ${qtitle})\" = ${qvalue}",
unless => "/usr/bin/test \"$(/sbin/sysctl -n ${qtitle} | sed 's/\\s\{1,\}/ /g')\" = ${qvalue}",
command => "/sbin/sysctl -w ${qtitle}=${qvalue}",
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/sysctl.d-file.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# <%= line.strip %>
<% end -%>
<% end -%>
<%= @title %> = <%= @value %>
<%= @title %> = <%= @_value %>