From 294be9893b024815126a8f847b53be9bfc2be7ab Mon Sep 17 00:00:00 2001 From: Stoica Alex Date: Tue, 18 Jul 2023 21:19:00 +0100 Subject: [PATCH 1/2] Fix race condition in Snowflake package The race condition in the Snowflake code allows the overwriting of the error message. The fix removes the shared variable and thus avoid cases where `err.Error()` changes partway through execution. --- Gemfile | 4 ++ Gemfile.lock | 4 +- .../snowflakedb/gosnowflake/errors.go | 7 ++- spec/snowflake/client_spec.rb | 43 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index d828534..e4a3abd 100644 --- a/Gemfile +++ b/Gemfile @@ -10,3 +10,7 @@ gem "rake" gem "rspec" gem "pry" gem "dotenv" + +group :development do + gem "parallel" +end diff --git a/Gemfile.lock b/Gemfile.lock index 63619f7..d33e1af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ruby_snowflake_client (1.1.0) + ruby_snowflake_client (1.2.1) GEM remote: https://rubygems.org/ @@ -10,6 +10,7 @@ GEM diff-lcs (1.5.0) dotenv (2.8.1) method_source (1.0.0) + parallel (1.23.0) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -34,6 +35,7 @@ PLATFORMS DEPENDENCIES bundler dotenv + parallel pry rake rspec diff --git a/ext/vendor/github.com/snowflakedb/gosnowflake/errors.go b/ext/vendor/github.com/snowflakedb/gosnowflake/errors.go index c49fc92..3b50503 100644 --- a/ext/vendor/github.com/snowflakedb/gosnowflake/errors.go +++ b/ext/vendor/github.com/snowflakedb/gosnowflake/errors.go @@ -82,7 +82,12 @@ func (se *SnowflakeError) exceptionTelemetry(sc *snowflakeConn) *SnowflakeError // return populated error fields replacing the default response func populateErrorFields(code int, data *execResponse) *SnowflakeError { - err := ErrUnknownError + err := &SnowflakeError{ + Number: -1, + SQLState: "-1", + Message: "an unknown server side error occurred", + QueryID: "-1", + } if code != -1 { err.Number = code } diff --git a/spec/snowflake/client_spec.rb b/spec/snowflake/client_spec.rb index 8ad3524..8abee58 100644 --- a/spec/snowflake/client_spec.rb +++ b/spec/snowflake/client_spec.rb @@ -62,7 +62,50 @@ sql: query ) end + end + + context "for unauthorized database" do + before do + client.connect( + account: ENV["SNOWFLAKE_ACCOUNT"], + warehouse: ENV["SNOWFLAKE_WAREHOUSE"], + user: ENV["SNOWFLAKE_USER"], + password: ENV["SNOWFLAKE_PASSWORD"], + ) + end + let(:query) { "SELECT * FROM TEST_DATABASE.RINSED_WEB_APP.EMAILS LIMIT 1;" } + it "should raise an exception" do + expect { result }.to raise_error do |error| + expect(error).to be_a Snowflake::Error + expect(error.message).to include "TEST_DATABASE" + expect(error.sentry_context).to include( + sql: query + ) + end + end + it "should raise the correct exception for threaded work" do + require "parallel" + + Parallel.map((1..3).collect { _1 }, in_threads: 2) do |idx| + c = described_class.new + c.connect( + account: ENV["SNOWFLAKE_ACCOUNT"], + warehouse: ENV["SNOWFLAKE_WAREHOUSE"], + user: ENV["SNOWFLAKE_USER"], + password: ENV["SNOWFLAKE_PASSWORD"], + ) + query = "SELECT * FROM TEST_DATABASE#{idx}.RINSED_WEB_APP.EMAILS LIMIT 1;" + + expect { c.fetch(query) }.to raise_error do |error| + expect(error).to be_a Snowflake::Error + expect(error.sentry_context).to include( + sql: query + ) + expect(error.message).to include "TEST_DATABASE#{idx}" + end + end + end end end From f288f5799567794f7fadeae640e79af55e7028df Mon Sep 17 00:00:00 2001 From: Stoica Alex Date: Thu, 20 Jul 2023 19:10:11 +0100 Subject: [PATCH 2/2] Update VERSION --- Gemfile.lock | 2 +- lib/ruby_snowflake_client/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d33e1af..afc6246 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ruby_snowflake_client (1.2.1) + ruby_snowflake_client (1.3.0) GEM remote: https://rubygems.org/ diff --git a/lib/ruby_snowflake_client/version.rb b/lib/ruby_snowflake_client/version.rb index 6d195c0..1af8b18 100644 --- a/lib/ruby_snowflake_client/version.rb +++ b/lib/ruby_snowflake_client/version.rb @@ -1,3 +1,3 @@ module RubySnowflakeClient - VERSION = '1.2.1' + VERSION = '1.3.0' end