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

Modify checksum logic to use system binaries #251

Merged
merged 11 commits into from
Aug 10, 2018

Conversation

jerryaldrichiii
Copy link
Contributor

@jerryaldrichiii jerryaldrichiii commented Feb 6, 2018

This modifies md5sum/sha256sum to use binaries on the target system vs Ruby on the machine running InSpec.

This fixes #235
This closes #236

Many thanks to @tarcinil for the initial work on this! It is greatly appreciated!

@jerryaldrichiii jerryaldrichiii requested a review from a team February 6, 2018 04:29
@jerryaldrichiii jerryaldrichiii force-pushed the ja/235-enable-remote-checksum branch 12 times, most recently from 6d61432 to 7dc6fa1 Compare February 6, 2018 23:56
@jerryaldrichiii jerryaldrichiii changed the title WIP DO NOT MERGE: Modify checksum logic to use system binaries Modify checksum logic to use system binaries Feb 7, 2018
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.

I like the improvements,since it saves a lot of time to transfer data, but we need to think about worst cases, where those commands are not available.

'digest -a md5'
else
'md5sum'
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to think about the case where those commands are not available. In those case I would like to fall back to the old methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, added in last commit. Let me know what you think @chris-rock.

@jerryaldrichiii jerryaldrichiii force-pushed the ja/235-enable-remote-checksum branch from e8024d3 to 37b6108 Compare April 2, 2018 05:10
@chris-rock
Copy link
Contributor

We are getting closer @jerryaldrichiii !!! I am going to request one other round for improvement. At this point we are detecting platforms and available commands again and again. Why not keep our decision for one connection. Therefore we have many implementations of the hash generation eg.:

  • md5
  • md5sum
  • rubymd5
    ...
    Once we call the function the first time, we detect the right implementation and stick for to that decision while the connection stays available. Does this makes any sense?

@jerryaldrichiii
Copy link
Contributor Author

Sure does @chris-rock. Let me know if the recent commits achieve what you're looking for.

@jerryaldrichiii jerryaldrichiii force-pushed the ja/235-enable-remote-checksum branch from 8547322 to 0dcac56 Compare June 15, 2018 17:24
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.

Awesome work @jerryaldrichiii I added some style comments, nothing major. 👍


private

def perform_checksum(method)
Copy link
Contributor

Choose a reason for hiding this comment

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

lets call this perform_checksum_cmd

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically you can call this method without setting the command before. I think we should either move the command detection into this method or assume it as an argument to minimize function side-effects.

# This pulls the content of the file to the machine running Train and uses
# Digest to perform the checksum. This is less efficient than using remote
# system binaries and can lead to incorrect results due to encoding.
def perform_ruby_checksum(method)
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be called perform_checksum_ruby to keep names aligned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I think that is fine. I've done a search and replace.

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

perform_ruby_checksum(method)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would return an error for that method instead of calling perform_ruby_checksum here

res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

perform_ruby_checksum(method)
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, I would not perform the ruby checksum here, but return an error

end

def sha256sum
return perform_ruby_checksum(method) if defined?(@use_ruby_checksum)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think method needs to be defined here

@@ -147,5 +129,85 @@ def mounted?

!mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.empty?
end

def md5sum
return perform_ruby_checksum(method) if defined?(@use_ruby_checksum)
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be perform_checksum_ruby(:md5)?


return perform_checksum(:md5) unless @backend.os.family == 'windows'

perform_checksum_windows(:md5)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be a bit more simple, something like:

method = :md5
if @hash_ruby_fallback
  hash, err = perform_checksum_windows(method) if os.family == 'windows'
  hash, err  = perform_checksum_cmd(method) if os.family != 'windows'
  if err == nil
    return hash
  end
end
@hash_ruby_fallback = true
return perform_checksum_ruby(method)

@jerryaldrichiii
Copy link
Contributor Author

I took a different approach @chris-rock but the feedback was super valuable. Let me know if it is too divergent from what you had in mind.

@jerryaldrichiii
Copy link
Contributor Author

I fixed the merge conflicts via the GUI. No sure how I feel, but hey we squash any way.

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.

Great improvement @jerryaldrichiii

'sha256sum'
end

perform_checksum_linux(@sha256_command)
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be perform_checksum_unix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't see why not. I've fixed it.

tarcinil and others added 11 commits July 31, 2018 10:42
This removes the tests related to using `md5sum` or `sha356sum` on a
FIFO file.

These tests were not working previously and I was unable to make them
work after my changes. I suspect that it is not possible since a FIFO
file must be open at both ends simultaneously before you can do any I/O
operations on it.

See: https://linux.die.net/man/3/mkfifo

Signed-off-by: Jerry Aldrich <[email protected]>
Signed-off-by: Jerry Aldrich <[email protected]>
@jerryaldrichiii jerryaldrichiii force-pushed the ja/235-enable-remote-checksum branch from ec2620f to c5987b2 Compare July 31, 2018 17:42
Copy link
Contributor

@jquick jquick left a comment

Choose a reason for hiding this comment

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

@jquick jquick merged commit 335cd3a into master Aug 10, 2018
@jquick jquick deleted the ja/235-enable-remote-checksum branch August 10, 2018 12:37
@tas50
Copy link
Contributor

tas50 commented Aug 15, 2018

This is great. Thanks!

@clintoncwolfe clintoncwolfe added Type: New Feature Adds new functionality and removed Type: New Feature labels Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: New Feature Adds new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sha256sum and md5sum to execute on the remote machine
7 participants