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

mount resource: fix for Device-/Sharenames and Mountpoints including … #2257

Merged
merged 4 commits into from
Nov 1, 2017
Merged

mount resource: fix for Device-/Sharenames and Mountpoints including … #2257

merged 4 commits into from
Nov 1, 2017

Conversation

mgrobelin
Copy link
Contributor

@mgrobelin mgrobelin commented Oct 18, 2017

…whitespaces

Device-/Sharenames and Mountpoints on Linux may include whitespaces (\040), e.g. /etc/fstab entry like:

//fileserver.corp.internal/Research\040&\040Development /mnt/Research\040&\040Development cifs OTHER_OPTS

... results in a mount line like:

//fileserver.corp.internal/Research & Development on /mnt/Research & Development type cifs (OTHER_OPTS)

The Linux mount command replaces \040 with whitspace automatically, so this should be tributed.

I used a control like this:

    describe mount('/mnt/Research & Development') do
      it { should be_mounted }
      its('device') { should eq  '//fileserver.corp.internal/Research & Development' }
    end

Before:

  ×  whitespaces-1: Mount with whitespace within sharename and mountpoint. (1 failed)
     ✔  Mount /mnt/Research & Development should be mounted
     ×  Mount /mnt/Research & Development device should eq "//fileserver.corp.internal/Research & Development"

     expected: "//fileserver.corp.internal/Research & Development"
          got: "//fileserver.corp.internal/operational/Research"

     (compared using ==)

After:

  ✔  whitespaces-01: Mount with whitespace within sharename and mountpoint.
     ✔  Mount /mnt/Research & Development should be mounted
     ✔  Mount /mnt/Research & Development device should eq "//fileserver.corp.internal/Research & Development"

Signed-off-by: Markus Grobelin [email protected]

@mgrobelin mgrobelin requested a review from a team as a code owner October 18, 2017 14:40
@mgrobelin
Copy link
Contributor Author

Please note, this PR do not address the case, when the Share-/Devicename and/or Mountpoint includes words like on or type, e.g.: a /etc/fstab entry like:

/dev/sda1 /mnt/type\040on\040fire xfs [...]

Will most likely render wrong results / unexpected behavoir.

…whitespaces

Device-/Sharenames and Mountpoints on Linux may include whitespaces (\040), e.g. /etc/fstab entry like:

```//fileserver.corp.internal/Research\040&\040Development /mnt/Research\040&\040Development cifs OTHER_OPTS```

... results in a mount line like:

```//fileserver.corp.internal/Research & Development on /mnt/Research & Development type cifs (OTHER_OPTS)```

The Linux mount command replaces \040 with whitspace automatically, so this should be tributed.

I used a control like this:

```
    describe mount('/mnt/Research & Development') do
      it { should be_mounted }
      its('device') { should eq  '//fileserver.corp.internal/Research & Development' }
    end
```

Before:

```
  ×  whitespaces-1: Mount with whitespace within sharename and mountpoint. (1 failed)
     ✔  Mount /mnt/Research & Development should be mounted
     ×  Mount /mnt/Research & Development device should eq "//fileserver.corp.internal/Research & Development"

     expected: "//fileserver.corp.internal/Research & Development"
          got: "//fileserver.corp.internal/Research"

     (compared using ==)
```

After:

```
  ✔  whitespaces-01: Mount with whitespace within sharename and mountpoint.
     ✔  Mount /mnt/Research & Development should be mounted
     ✔  Mount /mnt/Research & Development device should eq "//fileserver.corp.internal/Research & Development"
```

Signed-off-by: Markus Grobelin <[email protected]>
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking pretty good! Just a couple of code-cleanup items and a suggestion on a potential way around the "type" being in device/share names.

other_opts = type_split[1]
fs, path = fs_path.match(%r{^(.+?)\son\s(/.+?)$}).captures
mount = [fs, 'on', path, 'type']
mount.concat other_opts.scan(/\S+/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to standardize on wrapping all method arguments in parentheses. Can you change this to:

mount.concat(other_opts.scan(/\S+/))

... so it's clearer? Thanks!

# Device-/Sharename or Mountpoint includes whitespaces?
def includes_whitespaces?(mount_line)
ws = mount_line.match(/^(.+)\son\s(.+)\stype\s.*$/)
ws.captures[0].include? ' ' or ws.captures[1].include? ' '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.. each of the include? method calls should wrap their arguments with parentheses, please. :)

@@ -67,7 +67,19 @@ module LinuxMountParser
# this parses the output of mount command (only tested on linux)
# this method expects only one line of the mount output
def parse_mount_options(mount_line, compatibility = false)
mount = mount_line.scan(/\S+/)
if includes_whitespaces? mount_line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the other review comments, please wrap arguments with parentheses:

if includes_whitespaces?(mount_line)

if includes_whitespaces? mount_line
# Device-/Sharenames and Mountpoints including whitespaces require special treatment:
# We use the keyword ' type ' to split up and rebuild the desired array of fields
type_split = mount_line.split(' type ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be able to use rindex to get the right-most type to avoid picking up a share name with the word type in it with spaces:

type_index = mount_line.rindex(' type ')
fs_path = mount_line[0...type_index]
other_opts_index = type_index + 6 # since ' type ' is 6 chars
other_opts = mount_line[other_opts_index..-1]
# ... etc ...

I think this might decently well, but would need to be tested. Worth playing around with a bit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely worth, but I left it out for now due to high complexity. Maybe later, but I cannot promise due to endofyear-crunch.

If one would address this edge-case, it might be clever to do the same for BsdMountParser, right? What would be your suggestion to keep it DRY?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK leaving it out for this PR and we can circle back to it if someone raises a bug or if you feel like coming back for a second iteration. :)

I would definitely DRY it up if possible as long as it doesn't make it too complicated. I'm happy to help as needed!

@adamleff adamleff added the Type: Bug Feature not working as expected label Oct 24, 2017
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgrobelin just one more set of parentheses was missed in your last commit from my initial review, and then I think we can move forward with this. Thanks!

@@ -67,7 +67,19 @@ module LinuxMountParser
# this parses the output of mount command (only tested on linux)
# this method expects only one line of the mount output
def parse_mount_options(mount_line, compatibility = false)
mount = mount_line.scan(/\S+/)
if includes_whitespaces? mount_line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed one set of parentheses here and then I think we're good!

Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this fix, @mgrobelin!

@adamleff adamleff requested a review from a team October 31, 2017 20:39
Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mgrobelin and @adamleff

@chris-rock chris-rock merged commit 221db7e into inspec:master Nov 1, 2017
@mgrobelin mgrobelin deleted the mounts_with_whitespaces branch November 1, 2017 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants