Connection ssue with mssql_session

I am having some issues connecting to an instance of MSSQL on a Windows server while using mssql_session. Here is the code that I am using for the test:

Snippet

sql = mssql_session(user: 'user', password: 'Password', host: 'ADDRESS', instance: 'MSSQL')

I am getting the following error:
/opt/inspec/embedded/lib/ruby/gems/2.6.0/gems/inspec-4.18.39/lib/inspec/resources/mssql_session.rb:77:in `query': Could not execute the sql query (Inspec::Exceptions::ResourceFailed)
SQLCMD.EXE : Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: No connection could be made because the target machine actively refused it.
+ CategoryInfo : NotSpecified: (Sqlcmd: Error: ...ely refused it.:String) , RemoteException
+ FullyQualifiedErrorId : NativeCommandError
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

I am able to connect to that instance via SQLCMD manually. I took a quick look at the source code, and it appears as though the mssql_session wraps all the parameters in a single quote when calling sqlcmd.

i.e. sqlcmd -S 'server\instance' -U 'user' -P 'Password' If I call sqlcmd manually if this same manner, I get the same error that InSpec is returning.

However if I do the following:
sqlcmd -S "server\instance" -U "user" -P "Password" (using double quotes), I am able to connect to the instance fine.

Has anyone run into this before or have any ideas? Is there something obvious that I have missed?

Thanks!

The issue it turns out is due to the way the host is being passed in. If you do not specify a port, it defaults to port 1433. This is how it would appear if you called it directly via SQLCMD:

sqlcmd - S 'server\instance,1433'

If you do not have IPAll set to that port number for your instance in SQL Server Configuration Manager, make sure to pass in the proper port number when calling mssql_session. Example:

sql = mssql_session(user: 'user', password: 'Password', host: 'ADDRESS,PORT_NUMBER', instance: 'MSSQL')

Hopefully this might help someone out in the future.

1 Like