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

Quote and escape special chars in mapping keys #205

Merged
merged 1 commit into from
Nov 2, 2023
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
4 changes: 2 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ the remote filesystem to mount

##### <a name="-autofs--mapping--key"></a>`key`

Data type: `Pattern[/\A\S+\z/]`
Data type: `String[1]`

the autofs key for this mapping. For indirect maps it is the
basename of the mountpoint directory for $fs (not to be confused with
Expand Down Expand Up @@ -792,7 +792,7 @@ Alias of

```puppet
Struct[{
key => Pattern[/\A\S+\z/],
key => String[1],
options => Optional[Autofs::Options],
order => Optional[Integer],
fs => Pattern[/\S/] # contains at least one non-whitespace character
Expand Down
13 changes: 9 additions & 4 deletions manifests/mapping.pp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@
#
define autofs::mapping (
Stdlib::Absolutepath $mapfile,
Pattern[/\A\S+\z/] $key,
String[1] $key,
Pattern[/\S/] $fs,
Enum['present', 'absent'] $ensure = 'present',
Optional[Autofs::Options] $options = undef,
Integer $order = 10,
) {
unless $ensure == 'absent' {
$formatted_key = if $key =~ /[[:blank:]"]/ {
String($key, '%#p')
} else {
$key
}
# Format the options string, relying to some extent on the
# $options parameter, if specified, to indeed match the
# Autofs::Options data type
Expand All @@ -89,10 +94,10 @@
}

# Declare an appropriate fragment of the target map file
if $key == '+' {
$content = "${key}${fs}\n"
if $formatted_key == '+' {
$content = "${formatted_key}${fs}\n"
} else {
$content = "${key} ${formatted_options} ${fs}\n"
$content = "${formatted_key}\t${formatted_options}\t${fs}\n"
}

concat::fragment { "autofs::mapping/${title}":
Expand Down
20 changes: 20 additions & 0 deletions spec/defines/mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@
with(target: '/mnt/auto.data', content: "data -rw storage.host.net:/exports/data backup.host.net:/exports/data\n")
end
end

context 'with spaces in path' do
let(:params) do
{
mapfile: '/mnt/auto.data',
key: %(/scary/don't fear "quotes" and spaces),
options: 'rw',
fs: %("storage.host.net:/exports/data/don't fear \\"quotes\\" and spaces")
}
end

it do
expect(subject).to compile
expect(subject).not_to contain_class('autofs')
expect(subject).to have_concat_resource_count(0)
expect(subject).to have_concat__fragment_resource_count(1)
expect(subject).to contain_concat__fragment('autofs::mapping/data').
with(target: '/mnt/auto.data', content: %("/scary/don't fear \\"quotes\\" and spaces"\t-rw\t"storage.host.net:/exports/data/don't fear \\"quotes\\" and spaces"\n))
end
end
end
end
end
2 changes: 1 addition & 1 deletion types/fs_mapping.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# { 'key' => 'other', 'options' => [ 'ro', 'noexec' ], 'fs' => 'external.net:/the/exported/fs' }
#
type Autofs::Fs_mapping = Struct[{
key => Pattern[/\A\S+\z/],
key => String[1],
options => Optional[Autofs::Options],
order => Optional[Integer],
fs => Pattern[/\S/] # contains at least one non-whitespace character
Expand Down