multiwoven-integrations 0.43.20 → 0.43.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 74ac8a7bbd6e0290102a049c08e15309ae7a6a4e9bfed62f0df7fa399a195c9e
|
|
4
|
+
data.tar.gz: 76269f1185b5dfd304224e791b4abd91a81ebeda379d9509dd5ba3fa34922f89
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6f3244338cea6958221c55e3b07adc33d15adc08f123b675b1fbf4f3546c5bbce6c61312786af398c33febeb3417572adf09a717147a83ae6e339f7c340f0235
|
|
7
|
+
data.tar.gz: f2e90fceda25e1c77eec31ddd53361520e6894eace919d800def148d57203bbf4f00ca69a2321198280ab6f477108b3e0e39d2cdf178eb5d5f388b6fec0d849a
|
|
@@ -31,13 +31,15 @@ module Multiwoven
|
|
|
31
31
|
raise ArgumentError, "Offset and limit must be non-negative" if offset.negative? || limit.negative?
|
|
32
32
|
|
|
33
33
|
# Removing any trailing semicolons
|
|
34
|
-
sql_query.chomp
|
|
34
|
+
sql_query = sql_query.to_s.chomp(";")
|
|
35
35
|
|
|
36
36
|
# Checking if the query already has a LIMIT clause
|
|
37
37
|
raise ArgumentError, "Query already contains a LIMIT clause" if sql_query.match?(/LIMIT \d+/i)
|
|
38
38
|
|
|
39
|
+
separator = sql_query.split("\n", -1).last.to_s.match?(%r{--|//|#}) ? "\n" : " "
|
|
40
|
+
|
|
39
41
|
# Appending the LIMIT and OFFSET clauses to the SQL query
|
|
40
|
-
"#{sql_query}
|
|
42
|
+
"#{sql_query}#{separator}LIMIT #{limit} OFFSET #{offset}"
|
|
41
43
|
end
|
|
42
44
|
|
|
43
45
|
def send_request(options = {})
|
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "strscan"
|
|
4
|
+
|
|
3
5
|
module Multiwoven::Integrations::Source
|
|
4
6
|
module Snowflake
|
|
5
7
|
include Multiwoven::Integrations::Core
|
|
6
8
|
class Client < SourceConnector
|
|
9
|
+
class DisallowedQueryError < StandardError; end
|
|
10
|
+
|
|
11
|
+
DISALLOWED_COMMANDS = %w[PUT GET REMOVE RM LIST LS COPY EXECUTE CALL PROCEDURE].freeze
|
|
12
|
+
DISALLOWED_COMMAND_ERROR =
|
|
13
|
+
"Query rejected: Snowflake file transfer, stage, dynamic SQL, and procedure commands " \
|
|
14
|
+
"(PUT, GET, REMOVE, LIST, COPY, EXECUTE, CALL, PROCEDURE) are not allowed"
|
|
15
|
+
ALLOWED_QUERY_STARTS = %w[SELECT WITH].freeze
|
|
16
|
+
QUERY_PREFIX_ERROR = "Query rejected: only SELECT and WITH queries are allowed"
|
|
17
|
+
STATEMENT_PREFIXES = %w[
|
|
18
|
+
BEGIN IF ELSEIF WHILE FOR REPEAT LOOP CASE WHEN DECLARE EXCEPTION LET ASYNC RETURN
|
|
19
|
+
].freeze
|
|
20
|
+
SCRIPTING_OPENERS = %w[BEGIN DECLARE].freeze
|
|
21
|
+
STATEMENT_BOUNDARIES = %w[THEN ELSE DO DEFAULT].freeze
|
|
22
|
+
FUNCTION_CALL_COMMANDS = %w[GET].freeze
|
|
23
|
+
UNTERMINATED_COMMENT_ERROR = "Query rejected: unterminated block comment"
|
|
24
|
+
UNTERMINATED_DOLLAR_QUOTE_ERROR = "Query rejected: unterminated dollar-quoted string"
|
|
25
|
+
NON_ASCII_ERROR =
|
|
26
|
+
"Query rejected: non-ASCII characters are not allowed outside quotes and comments"
|
|
27
|
+
IDENTIFIER_PATTERN = /[A-Za-z_][A-Za-z0-9_$]*/.freeze
|
|
28
|
+
LINE_COMMENT_PATTERN = %r{(?:--|//)[^\r\n]*}.freeze
|
|
29
|
+
SINGLE_QUOTED_PATTERN = /'(?:''|\\.|[^'\\])*'/m.freeze
|
|
30
|
+
DOUBLE_QUOTED_PATTERN = /"(?:""|[^"])*"/m.freeze
|
|
31
|
+
ASCII_CHAR_PATTERN = /[\x00-\x7F]/.freeze
|
|
32
|
+
|
|
7
33
|
def check_connection(connection_config)
|
|
8
34
|
connection_config = connection_config.with_indifferent_access
|
|
9
35
|
create_connection(connection_config)
|
|
@@ -55,6 +81,7 @@ module Multiwoven::Integrations::Source
|
|
|
55
81
|
private
|
|
56
82
|
|
|
57
83
|
def query(connection, query)
|
|
84
|
+
validate_query!(query)
|
|
58
85
|
records = []
|
|
59
86
|
connection.fetch(query) do |row|
|
|
60
87
|
records << RecordMessage.new(data: row, emitted_at: Time.now.to_i).to_multiwoven_message
|
|
@@ -62,6 +89,124 @@ module Multiwoven::Integrations::Source
|
|
|
62
89
|
records
|
|
63
90
|
end
|
|
64
91
|
|
|
92
|
+
def validate_query!(sql_query)
|
|
93
|
+
text = sql_query.to_s
|
|
94
|
+
return if text.strip.empty?
|
|
95
|
+
|
|
96
|
+
scanner = StringScanner.new(text)
|
|
97
|
+
ensure_allowed_query_start!(scanner)
|
|
98
|
+
at_statement_start = true
|
|
99
|
+
in_scripting = false
|
|
100
|
+
previous_identifier = nil
|
|
101
|
+
|
|
102
|
+
until scanner.eos?
|
|
103
|
+
at_statement_start, in_scripting, previous_identifier =
|
|
104
|
+
scan_next_token(scanner, at_statement_start, in_scripting, previous_identifier)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def ensure_allowed_query_start!(scanner)
|
|
109
|
+
loop do
|
|
110
|
+
nil while skip_sql_trivia(scanner)
|
|
111
|
+
break unless scanner.scan("(") || scanner.scan(";")
|
|
112
|
+
end
|
|
113
|
+
return if scanner.eos?
|
|
114
|
+
|
|
115
|
+
command = scanner.check(IDENTIFIER_PATTERN)&.upcase
|
|
116
|
+
raise DisallowedQueryError, DISALLOWED_COMMAND_ERROR if DISALLOWED_COMMANDS.include?(command)
|
|
117
|
+
raise DisallowedQueryError, QUERY_PREFIX_ERROR unless ALLOWED_QUERY_STARTS.include?(command)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def scan_next_token(scanner, at_statement_start, in_scripting, previous_identifier)
|
|
121
|
+
return [at_statement_start, in_scripting, previous_identifier] if skip_sql_trivia(scanner)
|
|
122
|
+
return [true, in_scripting, nil] if scan_statement_separator!(scanner, at_statement_start)
|
|
123
|
+
return statement_start_after_command!(scanner, in_scripting) if at_statement_start
|
|
124
|
+
return [true, in_scripting, nil] if in_scripting && scan_statement_boundary!(scanner)
|
|
125
|
+
|
|
126
|
+
[false, in_scripting, skip_quoted_or_char(scanner, previous_identifier)]
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def scan_statement_separator!(scanner, at_statement_start)
|
|
130
|
+
return true if at_statement_start && scanner.scan("(")
|
|
131
|
+
return true if scanner.scan(";")
|
|
132
|
+
return true if scanner.scan("->>")
|
|
133
|
+
return true if scanner.scan(":=")
|
|
134
|
+
|
|
135
|
+
false
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def statement_start_after_command!(scanner, in_scripting)
|
|
139
|
+
command = scanner.scan(IDENTIFIER_PATTERN)
|
|
140
|
+
return [false, in_scripting, skip_quoted_or_char(scanner, nil)] unless command
|
|
141
|
+
|
|
142
|
+
upper = command.upcase
|
|
143
|
+
if DISALLOWED_COMMANDS.include?(upper)
|
|
144
|
+
return [false, in_scripting, command] if FUNCTION_CALL_COMMANDS.include?(upper) && function_call_after_command?(scanner)
|
|
145
|
+
|
|
146
|
+
raise DisallowedQueryError, DISALLOWED_COMMAND_ERROR
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
in_scripting = true if SCRIPTING_OPENERS.include?(upper)
|
|
150
|
+
[STATEMENT_PREFIXES.include?(upper), in_scripting, command]
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def function_call_after_command?(scanner)
|
|
154
|
+
position = scanner.pos
|
|
155
|
+
nil while skip_sql_trivia(scanner)
|
|
156
|
+
function_call = scanner.check("(")
|
|
157
|
+
scanner.pos = position
|
|
158
|
+
function_call
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def scan_statement_boundary!(scanner)
|
|
162
|
+
boundary = scanner.check(IDENTIFIER_PATTERN)
|
|
163
|
+
return false unless boundary && STATEMENT_BOUNDARIES.include?(boundary.upcase)
|
|
164
|
+
|
|
165
|
+
scanner.scan(IDENTIFIER_PATTERN)
|
|
166
|
+
true
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def skip_sql_trivia(scanner)
|
|
170
|
+
return true if scanner.scan(/\s+/) || scanner.scan(LINE_COMMENT_PATTERN)
|
|
171
|
+
|
|
172
|
+
if scanner.scan(%r{/\*})
|
|
173
|
+
raise DisallowedQueryError, UNTERMINATED_COMMENT_ERROR unless scanner.skip_until(%r{\*/})
|
|
174
|
+
|
|
175
|
+
return true
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
false
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def skip_quoted_or_char(scanner, previous_identifier)
|
|
182
|
+
return nil if scanner.scan(SINGLE_QUOTED_PATTERN) || scanner.scan(DOUBLE_QUOTED_PATTERN)
|
|
183
|
+
return scan_mid_statement_identifier(scanner, previous_identifier) if scanner.check(IDENTIFIER_PATTERN)
|
|
184
|
+
return skip_dollar_quote(scanner) if scanner.check("$$")
|
|
185
|
+
|
|
186
|
+
raise DisallowedQueryError, NON_ASCII_ERROR unless scanner.check(ASCII_CHAR_PATTERN)
|
|
187
|
+
|
|
188
|
+
scanner.getch
|
|
189
|
+
nil
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def scan_mid_statement_identifier(scanner, previous_identifier)
|
|
193
|
+
identifier = scanner.scan(IDENTIFIER_PATTERN)
|
|
194
|
+
raise DisallowedQueryError, DISALLOWED_COMMAND_ERROR if procedure_after_as?(identifier, previous_identifier)
|
|
195
|
+
|
|
196
|
+
identifier
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def procedure_after_as?(identifier, previous_identifier)
|
|
200
|
+
identifier.casecmp?("PROCEDURE") && previous_identifier&.casecmp?("AS")
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def skip_dollar_quote(scanner)
|
|
204
|
+
scanner.scan("$$")
|
|
205
|
+
raise DisallowedQueryError, UNTERMINATED_DOLLAR_QUOTE_ERROR unless scanner.skip_until(/\$\$/)
|
|
206
|
+
|
|
207
|
+
nil
|
|
208
|
+
end
|
|
209
|
+
|
|
65
210
|
def create_connection(connection_config)
|
|
66
211
|
raise "Unsupported Auth type" if connection_config[:credentials][:auth_type] != "username/password"
|
|
67
212
|
|