Skip to content

Commit

Permalink
Merge pull request #206 from voxpupuli/escape-special-char-in-mapping-fs
Browse files Browse the repository at this point in the history
  • Loading branch information
smortex authored Nov 2, 2023
2 parents a65a7f5 + 66cecb5 commit a82bdec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
8 changes: 4 additions & 4 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,13 @@ Default value: `'present'`

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

Data type: `Pattern[/\S/]`
Data type: `Variant[String[1], Array[String[1]]]`

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,10 +792,10 @@ 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
fs => Variant[String[1], Array[String[1]]],
}]
```

Expand Down
16 changes: 11 additions & 5 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,
Pattern[/\S/] $fs,
String[1] $key,
Variant[String[1], Array[String[1]]] $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 @@ -87,12 +92,13 @@
default => "-${prelim_options}",
}
}
$formatted_fs = [$fs].flatten.map |$value| { if $value =~ /[[:blank:]"]/ { String($value, '%#p') } else { $value } }.join(' ')

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

concat::fragment { "autofs::mapping/${title}":
Expand Down
25 changes: 24 additions & 1 deletion spec/defines/mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@
mapfile: '/mnt/auto.data',
key: 'data',
options: 'rw',
fs: 'storage.host.net:/exports/data backup.host.net:/exports/data'
fs: [
'storage.host.net:/exports/data',
'backup.host.net:/exports/data'
]
}
end

Expand All @@ -165,6 +168,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
4 changes: 2 additions & 2 deletions types/fs_mapping.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# { '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
fs => Variant[String[1], Array[String[1]]],
}]

0 comments on commit a82bdec

Please sign in to comment.