Skip to content

Commit 19bfd30

Browse files
committed
(MODULES-4915) Remove forced TCP connection for SQL management
Previously the module tried to connect to the SQL instance using the name localhost. This had the side effect of always using the TCP based connection. If a customer used a non-default Port (common practice) then the sql_connection would not connect. This commit instead changes the Datasource to use the '.' moniker which denotes the local machine. However this instructs the OLE DB provider to use the enabled protocols enabled for the native client instead of only using TCP. This means it will prefer to use the Shared Memory protocol if enabled. Also the Shared Memory protocol is required to be enabled for the SQL Server Agent process on SQL Server 2016.
1 parent e52e9f0 commit 19bfd30

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/puppet_x/sqlserver/sql_connection.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_connection_string(config)
3232
'Provider' => 'SQLOLEDB.1',
3333
'Initial Catalog' => config[:database] || 'master',
3434
'Application Name' => 'Puppet',
35-
'Data Source' => 'localhost'
35+
'Data Source' => '.'
3636
}
3737

3838
admin_user = config[:admin_user] || ''
@@ -52,7 +52,7 @@ def get_connection_string(config)
5252
end
5353

5454
if config[:instance_name] != nil && config[:instance_name] !~ /^MSSQLSERVER$/
55-
params['Data Source'] = "localhost\\#{config[:instance_name]}"
55+
params['Data Source'] = ".\\#{config[:instance_name]}"
5656
end
5757

5858
params.map { |k, v| "#{k}=#{v}" }.join(';')

spec/unit/puppet_x/sql_connection_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def stub_connection
1717
context 'command execution' do
1818
before :each do
1919
stub_connection
20-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;User ID=sa;Password=Pupp3t1@')
20+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.;User ID=sa;Password=Pupp3t1@')
2121
end
2222
it 'should not raise an error but populate has_errors with message' do
2323
subject.stubs(:execute).raises(Exception.new("SQL Server\n error has happened"))
@@ -43,7 +43,7 @@ def stub_connection
4343

4444
context 'Use default authentication' do
4545
it 'should defaul to SQL_LOGIN if admin_login_type is not set' do
46-
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;User ID=sa;Password=Pupp3t1@')
46+
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.;User ID=sa;Password=Pupp3t1@')
4747
subject.open_and_run_command('query', {:admin_user => 'sa', :admin_pass => 'Pupp3t1@' })
4848
end
4949
end
@@ -66,11 +66,11 @@ def stub_connection
6666
end
6767

6868
it 'should not add the default instance of MSSQLSERVER to connection string' do
69-
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;User ID=sa;Password=Pupp3t1@')
69+
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.;User ID=sa;Password=Pupp3t1@')
7070
subject.open_and_run_command('query', {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'})
7171
end
7272
it 'should add a non default instance to connection string' do
73-
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\\LOGGING;User ID=sa;Password=Pupp3t1@')
73+
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.\\LOGGING;User ID=sa;Password=Pupp3t1@')
7474
subject.open_and_run_command('query', {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'LOGGING'})
7575
end
7676
end
@@ -93,12 +93,12 @@ def stub_connection
9393
end
9494

9595
it 'should add integrated security to the connection string if admin and password are empty' do
96-
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;Integrated Security=SSPI')
96+
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.;Integrated Security=SSPI')
9797
subject.open_and_run_command('query', {:admin_user => '', :admin_pass => '', :admin_login_type => 'WINDOWS_LOGIN'})
9898
end
9999

100100
it 'should add integrated security to the connection string if admin and password are not defined' do
101-
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;Integrated Security=SSPI')
101+
@connection.expects(:Open).with('Provider=SQLOLEDB.1;Initial Catalog=master;Application Name=Puppet;Data Source=.;Integrated Security=SSPI')
102102
subject.open_and_run_command('query', { :admin_login_type => 'WINDOWS_LOGIN' })
103103
end
104104
end

0 commit comments

Comments
 (0)