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

Allow mysql resource to accept socket path #1933

Merged
merged 7 commits into from
Jun 23, 2017

Conversation

rshade
Copy link

@rshade rshade commented Jun 16, 2017

We can not use this by default against mysql installations via the mysql cookbook.

Signed-off-by: Richard Shade <rshade@rightscale.com>
Signed-off-by: Richard Shade <rshade@rightscale.com>
Signed-off-by: Richard Shade <rshade@rightscale.com>
Signed-off-by: Richard Shade <rshade@rightscale.com>
@adamleff
Copy link
Contributor

@rshade thanks for your PR!

Unfortunately, we cannot accept this change as written. The -S/--socket parameter overrides the -h/--host parameter, and we do have users that connect to MySQL instances that are not on the local machine that's being scanned (i.e. testing DB connectivity from a web server, for example).

I verified this by creating a Vagrant machine, installing MySQL, configuring it to listen on 13306 (and forwarded that port), and then tried to connect to it using both -h and -S:

mysql -uvagrant -p -S /tmp/nonexistent.sock -h localhost --port 13306
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/nonexistent.sock' (2)

One potential solution would be to set the default socket parameter to nil, and then have this kind of logic:

command = "mysql .... -u username .... "
if !socket.nil?
  command += "-S #{socket}"
else
  command += "-h #{host} ...."

That would still allow you to define a socket in your tests without breaking existing users.

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.

Unable to accept as-written, would break existing known use.

Signed-off-by: Richard Shade <rshade@rightscale.com>
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.

This looks pretty great to me, @rshade, thanks!

If you have time to level-up this change, I would break out the code the builds the CLI command string into a separate method (called mysql_command or something similar), and then that will allow us to test that logic easily and independently. If you don't have time for that, lemme know and I can follow-up with another PR to add that as well.

@adamleff adamleff added the Type: Enhancement Improves an existing feature label Jun 16, 2017
@rshade
Copy link
Author

rshade commented Jun 17, 2017

I will try and level up a little beginning of next week. Would it be cool if I open issues on a few of the items and we can work from there?

@adamleff
Copy link
Contributor

That works for me!

Signed-off-by: Richard Shade <rshade@rightscale.com>
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 @rshade for this improvement

@@ -16,10 +16,12 @@ class MysqlSession < Inspec.resource(1)
end
"

def initialize(user = nil, pass = nil, host = 'localhost')
def initialize(user = nil, pass = nil, host = 'localhost', port = nil, socket = nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future we probably want to use opts here to avoid passing nil values

@chris-rock chris-rock merged commit 1fbd4b5 into inspec:master Jun 23, 2017
@adamleff adamleff changed the title Mysql socket Allow mysql resource to accept socket path Jun 29, 2017
command += " -h #{@host}"
end
command += " --port #{@port}" unless @port.nil?
command += " #{db} -s -S #{@socket} -e \"#{escaped_query}\""
Copy link
Contributor

Choose a reason for hiding this comment

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

The -S #{@socket} argument is redundant with -h localhost and leads to MySQL error

Choose a reason for hiding this comment

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

Yep, the current version breaks for all queries without socket connections because of this

Choose a reason for hiding this comment

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

Ah, this is fixed in newer versions.

@rshade rshade deleted the mysql-socket branch February 20, 2019 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improves an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants