diff --git a/lib/puppet/provider/sqlserver.rb b/lib/puppet/provider/sqlserver.rb index dc6cc212..e196a30a 100644 --- a/lib/puppet/provider/sqlserver.rb +++ b/lib/puppet/provider/sqlserver.rb @@ -25,53 +25,6 @@ def try_execute(command, msg = nil) end end - ## - # Used by tsql provider - ## - def self.run_authenticated_sqlcmd(query, opts) - b = binding - @sql_instance_config = File.join(Puppet[:vardir], "cache/sqlserver/.#{resource[:instance]}.cfg") - if File.exists?(@sql_instance_config) - @sql_instance_config = native_path(@sql_instance_config) - else - raise Puppet::ParseError, "Config file does not exist" - end - temp = Tempfile.new(['puppet', '.sql']) - begin - temp.write(query) - temp.flush - temp.close - #input file is used in the authenticated_query.ps1.erb template - input_file = native_path(temp.path) - @instance = opts[:instance_name] - erb_template = File.join(template_path, 'authenticated_query.ps1.erb') - ps1 = ERB.new(File.new(erb_template).read, nil, '-') - temp_ps1 = Tempfile.new(['puppet', '.ps1']) - begin - temp_ps1.write(ps1.result(b)) - temp_ps1.flush - temp_ps1.close - #We want to not fail the exec but fail the overall process once we get the clean result back, otherwise we report the temp file which is meaningless - result = Puppet::Util::Execution.execute(['powershell.exe', '-noprofile', '-executionpolicy', 'unrestricted', temp_ps1.path], {:failonfail => false}) #We expect some things to fail in order to run as an only if - debug("Return result #{result}") - if opts[:failonfail] && result.match(/THROW CAUGHT/) - fail(result.gsub('THROW CAUGHT:', '')) - end - if result.match(/Msg \d+, Level 16/) - fail(result) - end - return result - ensure - temp_ps1.close - temp_ps1.unlink - end - ensure - temp.close - temp.unlink - end - return result - end - private def self.native_path(path) path.gsub(File::SEPARATOR, File::ALT_SEPARATOR) diff --git a/lib/puppet/templates/authenticated_query.ps1.erb b/lib/puppet/templates/authenticated_query.ps1.erb deleted file mode 100644 index a66b62ce..00000000 --- a/lib/puppet/templates/authenticated_query.ps1.erb +++ /dev/null @@ -1,48 +0,0 @@ -$json = cat '<%= @sql_instance_config %>' | ConvertFrom-Json -$env:SQLCMDUSER = $json.admin -$env:SQLCMDPASSWORD = $json.pass -if($json.instance -ne 'MSSQLSERVER'){ -$env:SQLCMDSERVER = "localhost\${json.instance}" -} -try { -if (!(Get-Command 'sqlcmd.exe' -ErrorAction SilentlyContinue)){ - if (Test-Path 'C:\Program Files\Microsoft SQL Server\120\Tools\Binn\sqlcmd.exe'){ - $env:Path += ";C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;" - }elseif(Test-Path 'C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd.exe'){ - $env:Path += ";C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;" - }else{ - $result = ls 'C:\Program Files\Microsoft SQL Server' -Filter sqlcmd.exe -Recurse | Select -First 1 - if ($result -eq $null) { - Write-Error -Message "Unable to find the sqlcmd.exe" -Category ResourceUnavailable - }else{ - $env:Path += ";{0}" -f $result.DirectoryName - } - } - if (!(Get-Command 'sqlcmd.exe')){ - Write-Error -Message "Unable to still find the sqlcmd.exe command" -Category ResourceUnavailable - } -} - -$result = sqlcmd.exe -i '<%= input_file %>' -h-1 -W -s ',' <% if @instance != 'MSSQLSERVER' %>-S localhost\<%= @instance %><%end%> - if($result -match "THROW CAUGHT"){ - Write-Host ($result | where {$_ -match "THROW CAUGHT"} | select -First 1) - Write-Error -Message ($result | where {$_ -match "THROW CAUGHT"} | select -First 1) - exit(10) - } - if($result -match "Incorrect syntax near "){ - Write-Host ($result | where {$_ -match "Incorrect syntax near"} | select -First 1) - Write-Error -Message ($result | where {$_ -match "Incorrect syntax"} | select -First 1) - exit(10) - } - if($result -match "Msg \d+, Level 16"){ - $msg = $result -join ' ' - Write-Host $msg - Write-Error -Message "ERROR: $msg" - exit(10) - } -} -catch{ - Write-Host $_ - exit(20) -} -exit(0) diff --git a/lib/puppet_x/sqlserver/sql_connection.rb b/lib/puppet_x/sqlserver/sql_connection.rb index 61921769..a40ce48c 100644 --- a/lib/puppet_x/sqlserver/sql_connection.rb +++ b/lib/puppet_x/sqlserver/sql_connection.rb @@ -6,12 +6,6 @@ module Sqlserver class SqlConnection attr_reader :exception_caught - - def initialize - @connection = nil - @data = nil - end - def open_and_run_command(query, config) begin open(config) @@ -34,19 +28,19 @@ def open(config) end def get_connection_string(config) - config = {'database' => 'master'}.merge(config) - # Open ADO connection to the SQL Server database - connection_string = "Provider=SQLOLEDB.1;" - connection_string << "Persist Security Info=False;" - connection_string << "User ID=#{config['admin']};" - connection_string << "password=#{config['pass']};" - connection_string << "Initial Catalog=#{config['database']};" - connection_string << "Application Name=Puppet;" + params = { + 'Provider' => 'SQLOLEDB.1', + 'User ID' => config['admin'], + 'Password' => config['pass'], + 'Initial Catalog' => config['database'] || 'master', + 'Application Name' => 'Puppet', + 'Data Source' => 'localhost' + } if config['instance'] !~ /^MSSQLSERVER$/ - connection_string << "Data Source=localhost\\#{config['instance']};" - else - connection_string << "Data Source=localhost;" + params['Data Source'] = "localhost\\#{config['instance']}" end + + params.map { |k, v| "#{k}=#{v}" }.join(';') end def command(sql) @@ -80,8 +74,6 @@ def close end def reset_instance - @data = nil - @fields = nil @exception_caught = nil end @@ -106,10 +98,6 @@ def parse_column_names(result) def win32_exception ::WIN32OLERuntimeError end - - def connection=(conn) - @connection = conn - end end class ResultOutput diff --git a/spec/unit/puppet_x/sql_connection_spec.rb b/spec/unit/puppet_x/sql_connection_spec.rb index 2e4006f6..17a4ccae 100644 --- a/spec/unit/puppet_x/sql_connection_spec.rb +++ b/spec/unit/puppet_x/sql_connection_spec.rb @@ -22,7 +22,7 @@ def stub_no_errors before :each do stub_connection @connection.stubs(:State).returns(0) - @connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;') + @connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost') end it 'should not raise an error but populate has_errors with message' do subject.stubs(:win32_exception).returns(Exception) @@ -48,11 +48,11 @@ def stub_no_errors @connection.stubs(:State).returns(0) end it 'should not add MSSQLSERVER to connection string' do - @connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;') + @connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost') subject.open_and_run_command('query', {'admin' => 'sa', 'pass' => 'Pupp3t1@', 'instance' => 'MSSQLSERVER'}) end it 'should add a non default instance to connection string' do - @connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=superuser;password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING;') + @connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=superuser;Password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING') subject.open_and_run_command('query', {'admin' => 'superuser', 'pass' => 'puppetTested', 'instance' => 'LOGGING'}) end end