multiwoven-integrations 0.43.12 → 0.43.13

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: bb77ba90ebff752fec810abecb8454f996f5612e6e52cbbe11edbd77096bd0cf
4
- data.tar.gz: 132346fa87dd4c6c8be648edb904ff18c7dd86c4d7e42b435476f85b6c0392d3
3
+ metadata.gz: ca23138d59d109c2f07f47597bd9997adda2377d1962b127609c3cfeafc2fcd6
4
+ data.tar.gz: 5e1c92a84e6eb37e91ec3379cd7ee54213c5d32e83d7dc495d16c041961ceb29
5
5
  SHA512:
6
- metadata.gz: 75905f3a1a72c3c76873921817d365fac27337a0939b6bfd5ebb0b3702eb593d4be7cab3b742f4e1b38b6a6b39cd649d5e83c41046beb9c33f2719eede7bc88c
7
- data.tar.gz: efd2c3830f5bb62731ea630135363599fa750ef041915fe8a640ca40f409003c05dc0062352e38f09bf28d14c4ab7cd1eb862c83c1718ee2fee03e429d17bc30
6
+ metadata.gz: feda26da63773776ba19252fdcb6bd4813f50dbaf305d7c3f0a1b07dfc25b2247d120a74916655e388eed964026222bc993fdbdb31a3d50018d694702fff52a7
7
+ data.tar.gz: e93dcf64b3e22877f903e2b0b13d588f5c07d64a151da2d5a9908610c71b5c1cc9be9e8546fb40c20c1a2460faad5d315cffa76acc57f8af0fc9367396f34c60
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ### [0.43.13]
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * HTTP source reads `parse_response` with a small built-in JSONPath reader that supports only `$`, `.key`, `..key` (once per path), `['key']`, `["key"]`, `[index]`, `[start:end]`, `.*` and `[*]`, with an optional `.` before a bracket, and never calls Ruby methods. Filters, scripts, unions, step slices and a second `..` are rejected with a clear error
9
+ * HTTP source JSONPath keeps the old reader's results for a negative slice end (`$.values[1:-1]` includes the last row) and a trailing `.*` (returns the node itself). A `.*` in the middle of a path now matches direct children only, where the old reader searched every level below
10
+ * HTTP source no longer puts the response body in discover errors or in the error for a body that is not JSON
11
+ * HTTP source header and values form builds each record only from the columns its row has, and fails with a clear error above 100,000 rows
12
+ * BigQuery and Google Vertex Model sources accept only a service account key as `credentials_json` and build service account credentials from it. Other credential types and key file paths are rejected
13
+ * BigQuery and Google Vertex Model sources keep getting tokens from `https://oauth2.googleapis.com/token`, whatever `token_uri` the key sets
14
+ * BigQuery and Google Vertex Model sources reject an encrypted or non RSA `private_key` instead of waiting for a passphrase on the console
15
+ * Google Vertex Model source configures each client instance instead of the process-wide client configuration
16
+ * Google Vertex Model source accepts only lowercase letters, digits and hyphens in `project_id`, `region` and `endpoint_id`
17
+ * Iterable, HubSpot, Stripe and Zendesk destinations map stream names to a fixed list of API classes or methods and reject any other name
18
+ * Stripe, Iterable and Slack destinations send the connector's own key with each request instead of setting a process-wide key, and Stripe and Slack reject a blank key. Stripe writes only the `create` action
19
+ * MariaDB source and destination turn off `LOAD DATA LOCAL INFILE` on the client
20
+ * Salesforce Consumer Goods Cloud destination no longer turns on Restforce request logging, which wrote credentials to the log
21
+ * Iterable destination logs write errors with the right arguments
22
+ * Iterable and MariaDB destinations return a discover error as a log message instead of raising a wrong number of arguments error
23
+
3
24
  ### [0.43.12]
4
25
 
5
26
 
@@ -60,6 +60,8 @@ module Multiwoven
60
60
 
61
61
  GOOGLE_VERTEX_ENDPOINT_SERVICE_URL = "%<region>s-aiplatform.googleapis.com"
62
62
  GOOGLE_VERTEX_MODEL_NAME = "projects/%<project_id>s/locations/%<region>s/endpoints/%<endpoint_id>s"
63
+ GOOGLE_CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform"
64
+ GOOGLE_BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"
63
65
 
64
66
  WATSONX_HEALTH_DEPLOYMENT_URL = "https://%<region>s.ml.cloud.ibm.com/ml/v4/deployments?version=%<version>s"
65
67
  WATSONX_PREDICTION_DEPLOYMENT_URL = "https://%<region>s.ml.cloud.ibm.com/ml/v4/deployments/%<deployment_id>s/predictions?version=%<version>s"
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ module GoogleServiceAccountCredentials
6
+ class InvalidCredentialsError < ArgumentError; end
7
+
8
+ INVALID_CREDENTIALS_MESSAGE = "Google credentials must be a service account JSON key"
9
+ INVALID_PRIVATE_KEY_MESSAGE = "Google service account private_key must be an unencrypted RSA private key"
10
+ SERVICE_ACCOUNT_TYPE = "service_account"
11
+ TOKEN_URI = "https://oauth2.googleapis.com/token"
12
+ NO_PASSPHRASE = ""
13
+
14
+ module_function
15
+
16
+ def build(credentials_json, scope:)
17
+ Google::Auth::ServiceAccountCredentials.make_creds(
18
+ json_key_io: StringIO.new(service_account_key(credentials_json).to_json),
19
+ scope: scope,
20
+ token_credential_uri: TOKEN_URI,
21
+ audience: TOKEN_URI
22
+ )
23
+ end
24
+
25
+ def service_account_key(credentials_json)
26
+ raise InvalidCredentialsError, INVALID_CREDENTIALS_MESSAGE unless credentials_json.is_a?(Hash)
27
+
28
+ key = credentials_json.to_h.transform_keys(&:to_s)
29
+ raise InvalidCredentialsError, INVALID_CREDENTIALS_MESSAGE unless key["type"] == SERVICE_ACCOUNT_TYPE && key["private_key"].is_a?(String)
30
+
31
+ key.merge("private_key" => unencrypted_private_key_pem(key["private_key"].gsub("\\n", "\n")))
32
+ end
33
+
34
+ def unencrypted_private_key_pem(private_key_pem)
35
+ private_key = OpenSSL::PKey.read(private_key_pem, NO_PASSPHRASE)
36
+ raise InvalidCredentialsError, INVALID_PRIVATE_KEY_MESSAGE unless private_key.is_a?(OpenSSL::PKey::RSA) && private_key.private?
37
+
38
+ private_key.to_pem
39
+ rescue OpenSSL::PKey::PKeyError
40
+ raise InvalidCredentialsError, INVALID_PRIVATE_KEY_MESSAGE
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strscan"
4
+
5
+ module Multiwoven
6
+ module Integrations::Core
7
+ module SafeJsonPath
8
+ class UnsupportedPathError < ArgumentError; end
9
+
10
+ UNSUPPORTED_PATH_MESSAGE = "Unsupported JSONPath expression. Use only $, .key, ..key (once per path), ['key'], [index], [start:end], .* and [*]"
11
+ # Each extra recursive descent multiplies the matches, so a crafted response could exhaust memory.
12
+ MAX_DESCENDANT_STEPS = 1
13
+ ROOT = /\$/.freeze
14
+ DESCENDANT_KEY = /\.\.([^.\[\]()'"?*\s]+)/.freeze
15
+ DOT_KEY = /\.([^.\[\]()'"?*\s]+)/.freeze
16
+ SINGLE_QUOTED_KEY = /\['([^'\\]*)'\]/.freeze
17
+ DOUBLE_QUOTED_KEY = /\["([^"\\]*)"\]/.freeze
18
+ INDEX = /\[(-?\d+)\]/.freeze
19
+ SLICE = /\[(-?\d*):(-?\d*)\]/.freeze
20
+ WILDCARD = /\[\*\]|\.\*/.freeze
21
+ TRAILING_DOT_WILDCARD = /\.\*\z/.freeze
22
+ DOT_BEFORE_BRACKET = /\.(?=\[)/.freeze
23
+
24
+ module_function
25
+
26
+ def on(document, path)
27
+ steps(path).reduce([document]) do |nodes, step|
28
+ nodes.flat_map { |node| step.call(node) }
29
+ end
30
+ end
31
+
32
+ def steps(path)
33
+ raise UnsupportedPathError, UNSUPPORTED_PATH_MESSAGE unless path.is_a?(String)
34
+
35
+ scanner = StringScanner.new(rooted(path.strip))
36
+ scanner.skip(ROOT)
37
+ path_steps = []
38
+ descendant_step_count = 0
39
+ until scanner.eos?
40
+ next if scanner.skip(TRAILING_DOT_WILDCARD)
41
+
42
+ descendant_step_count += 1 if scanner.check(DESCENDANT_KEY)
43
+ path_steps << next_step(scanner)
44
+ end
45
+ raise UnsupportedPathError, UNSUPPORTED_PATH_MESSAGE if descendant_step_count > MAX_DESCENDANT_STEPS
46
+
47
+ path_steps
48
+ end
49
+
50
+ def rooted(path)
51
+ return path if path.empty? || path.start_with?("$", ".", "[")
52
+
53
+ ".#{path}"
54
+ end
55
+
56
+ def next_step(scanner)
57
+ scanner.skip(DOT_BEFORE_BRACKET)
58
+ if scanner.scan(DESCENDANT_KEY)
59
+ descendant_key_step(scanner[1])
60
+ elsif scan_key(scanner)
61
+ key_step(scanner[1])
62
+ elsif scanner.scan(INDEX)
63
+ index_step(Integer(scanner[1], 10))
64
+ elsif scanner.scan(SLICE)
65
+ slice_step(slice_bound(scanner[1]), slice_bound(scanner[2]))
66
+ elsif scanner.skip(WILDCARD)
67
+ wildcard_step
68
+ else
69
+ raise UnsupportedPathError, UNSUPPORTED_PATH_MESSAGE
70
+ end
71
+ end
72
+
73
+ def scan_key(scanner)
74
+ scanner.scan(DOT_KEY) || scanner.scan(SINGLE_QUOTED_KEY) || scanner.scan(DOUBLE_QUOTED_KEY)
75
+ end
76
+
77
+ def key_step(key)
78
+ ->(node) { node.is_a?(Hash) && node.key?(key) ? [node[key]] : [] }
79
+ end
80
+
81
+ def descendant_key_step(key)
82
+ lookup = key_step(key)
83
+ ->(node) { containers_within(node).flat_map { |container| lookup.call(container) } }
84
+ end
85
+
86
+ def containers_within(node)
87
+ return [] unless node.is_a?(Array) || node.is_a?(Hash)
88
+
89
+ [node] + children_of(node).flat_map { |child| containers_within(child) }
90
+ end
91
+
92
+ def index_step(index)
93
+ ->(node) { node.is_a?(Array) && index.between?(-node.size, node.size - 1) ? [node[index]] : [] }
94
+ end
95
+
96
+ def slice_step(first, last)
97
+ lambda do |node|
98
+ next [] unless node.is_a?(Array)
99
+
100
+ start = slice_position(first || 0, node.size)
101
+ finish = slice_end_position(last, node.size)
102
+ node[start...finish]
103
+ end
104
+ end
105
+
106
+ def wildcard_step
107
+ ->(node) { children_of(node) }
108
+ end
109
+
110
+ def children_of(node)
111
+ case node
112
+ when Array then node
113
+ when Hash then node.values
114
+ else []
115
+ end
116
+ end
117
+
118
+ def slice_bound(bound)
119
+ bound.empty? ? nil : Integer(bound, 10)
120
+ end
121
+
122
+ def slice_position(bound, size)
123
+ (bound.negative? ? bound + size : bound).clamp(0, size)
124
+ end
125
+
126
+ def slice_end_position(bound, size)
127
+ return size if bound.nil?
128
+
129
+ (bound.negative? ? bound + size + 1 : bound).clamp(0, size)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -79,12 +79,21 @@ module Multiwoven
79
79
 
80
80
  def send_data_to_hubspot(stream_name, record = {})
81
81
  args = build_args(@action, stream_name, record)
82
- hubspot_stream = @client.crm.send(stream_name)
83
82
  hubspot_data = { simple_public_object_input_for_create: args }
84
- response = hubspot_stream.basic_api.send(@action, hubspot_data)
83
+ response = crm_object_api(stream_name).basic_api.send(@action, hubspot_data)
85
84
  [args, response]
86
85
  end
87
86
 
87
+ def crm_object_api(stream_name)
88
+ case stream_name
89
+ when "contacts" then @client.crm.contacts
90
+ when "companies" then @client.crm.companies
91
+ when "deals" then @client.crm.deals
92
+ when "line_items" then @client.crm.line_items
93
+ else raise ArgumentError, "Unsupported HubSpot stream"
94
+ end
95
+ end
96
+
88
97
  def build_args(action, stream_name, record)
89
98
  case action
90
99
  when :upsert
@@ -7,10 +7,15 @@ module Multiwoven
7
7
  include Multiwoven::Integrations::Core
8
8
  class Client < DestinationConnector
9
9
  MAX_CHUNK_SIZE = 10
10
+ STREAM_CLASSES = {
11
+ "Catalogs" => ::Iterable::Catalogs,
12
+ "CatalogItems" => ::Iterable::CatalogItems
13
+ }.freeze
14
+
10
15
  def check_connection(connection_config)
11
16
  connection_config = connection_config.with_indifferent_access
12
17
  initialize_client(connection_config)
13
- channels = ::Iterable::Channels.new
18
+ channels = ::Iterable::Channels.new(@iterable_config)
14
19
  response = channels.all
15
20
  if response.success?
16
21
  success_status
@@ -29,11 +34,10 @@ module Multiwoven
29
34
  catalog = build_catalog(load_catalog)
30
35
  catalog.to_multiwoven_message
31
36
  rescue StandardError => e
32
- handle_exception(
33
- "ITERABLE:DISCOVER:EXCEPTION",
34
- "error",
35
- e
36
- )
37
+ handle_exception(e, {
38
+ context: "ITERABLE:DISCOVER:EXCEPTION",
39
+ type: "error"
40
+ })
37
41
  end
38
42
 
39
43
  def write(sync_config, records, action = "create", _identifier_key = nil)
@@ -42,15 +46,13 @@ module Multiwoven
42
46
  initialize_client(connection_config)
43
47
  process_records(records, sync_config.stream)
44
48
  rescue StandardError => e
45
- handle_exception("ITERABLE:WRITE:EXCEPTION", "error", e)
49
+ handle_exception(e, { context: "ITERABLE:WRITE:EXCEPTION", type: "error" })
46
50
  end
47
51
 
48
52
  private
49
53
 
50
54
  def initialize_client(connection_config)
51
- ::Iterable.configure do |config|
52
- config.token = connection_config[:api_key]
53
- end
55
+ @iterable_config = ::Iterable::Config.new(token: connection_config[:api_key])
54
56
  end
55
57
 
56
58
  def process_records(records, stream)
@@ -67,7 +69,7 @@ module Multiwoven
67
69
  end
68
70
  log_message_array << log_request_response("info", request, response.body)
69
71
  rescue StandardError => e
70
- handle_exception("ITERABLE:WRITE:EXCEPTION", "error", e)
72
+ handle_exception(e, { context: "ITERABLE:WRITE:EXCEPTION", type: "error" })
71
73
  write_failure += 1
72
74
  log_message_array << log_request_response("error", request, e.message)
73
75
  end
@@ -75,7 +77,7 @@ module Multiwoven
75
77
  end
76
78
 
77
79
  def process_stream(record, stream)
78
- klass = ::Iterable.const_get(stream.name).new(*initialize_params(stream, record))
80
+ klass = stream_class(stream.name).new(*initialize_params(stream, record), @iterable_config)
79
81
  item_attrs = initialize_attribute(stream, record)
80
82
  response = if stream.name == "CatalogItems"
81
83
  klass.send("create", item_attrs)
@@ -85,6 +87,10 @@ module Multiwoven
85
87
  [item_attrs, response]
86
88
  end
87
89
 
90
+ def stream_class(stream_name)
91
+ STREAM_CLASSES.fetch(stream_name) { raise ArgumentError, "Unsupported Iterable stream" }
92
+ end
93
+
88
94
  def initialize_params(stream, record)
89
95
  if stream.name == "CatalogItems"
90
96
  [record[:catalog_name], record[:item_id]]
@@ -28,11 +28,10 @@ module Multiwoven::Integrations::Destination
28
28
  catalog = Catalog.new(streams: create_streams(records))
29
29
  catalog.to_multiwoven_message
30
30
  rescue StandardError => e
31
- handle_exception(
32
- "MARIA:DB:DISCOVER:EXCEPTION",
33
- "error",
34
- e
35
- )
31
+ handle_exception(e, {
32
+ context: "MARIA:DB:DISCOVER:EXCEPTION",
33
+ type: "error"
34
+ })
36
35
  end
37
36
 
38
37
  def write(sync_config, records, action = "destination_insert", _identifier_key = nil)
@@ -83,7 +82,8 @@ module Multiwoven::Integrations::Destination
83
82
  port: connection_config[:port],
84
83
  user: connection_config[:username],
85
84
  password: connection_config[:password],
86
- database: connection_config[:database]
85
+ database: connection_config[:database],
86
+ local_infile: false
87
87
  )
88
88
  end
89
89
 
@@ -21,7 +21,6 @@ module Multiwoven
21
21
  Restforce.configure do |config|
22
22
  config.logger = @logger
23
23
  end
24
- Restforce.log = true
25
24
  end
26
25
 
27
26
  def check_connection(connection_config)
@@ -11,9 +11,7 @@ module Multiwoven
11
11
  attr_accessor :channel_id
12
12
 
13
13
  def check_connection(connection_config)
14
- configure_slack(connection_config[:api_token])
15
- client = ::Slack::Web::Client.new
16
- client.auth_test
14
+ slack_client(connection_config[:api_token]).auth_test
17
15
  success_status
18
16
  rescue StandardError => e
19
17
  failure_status(e)
@@ -35,8 +33,7 @@ module Multiwoven
35
33
  @sync_config = sync_config
36
34
  @action = sync_config.stream.action || action
37
35
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
38
- configure_slack(connection_config[:api_token])
39
- @client = ::Slack::Web::Client.new
36
+ @client = slack_client(connection_config[:api_token])
40
37
  @channel_id = connection_config[:channel_id]
41
38
  process_records(records, sync_config.stream)
42
39
  rescue StandardError => e
@@ -50,10 +47,10 @@ module Multiwoven
50
47
 
51
48
  private
52
49
 
53
- def configure_slack(api_token)
54
- ::Slack.configure do |config|
55
- config.token = api_token
56
- end
50
+ def slack_client(api_token)
51
+ raise ArgumentError, "Slack api_token is required" if api_token.blank?
52
+
53
+ ::Slack::Web::Client.new(token: api_token)
57
54
  end
58
55
 
59
56
  def process_records(records, stream)
@@ -10,6 +10,12 @@ module Multiwoven
10
10
 
11
11
  class Client < DestinationConnector
12
12
  prepend Multiwoven::Integrations::Core::RateLimiter
13
+ STREAM_CLASSES = {
14
+ "Customer" => ::Stripe::Customer,
15
+ "Product" => ::Stripe::Product
16
+ }.freeze
17
+ CREATE_ACTION = "create"
18
+
13
19
  def check_connection(connection_config)
14
20
  connection_config = connection_config.with_indifferent_access
15
21
  initialize_client(connection_config)
@@ -47,8 +53,9 @@ module Multiwoven
47
53
 
48
54
  def initialize_client(config)
49
55
  config = config.with_indifferent_access
50
- ::Stripe.api_key = config[:api_key]
51
- @client = ::Stripe
56
+ raise ArgumentError, "Stripe api_key is required" if config[:api_key].blank?
57
+
58
+ @request_options = { api_key: config[:api_key] }
52
59
  end
53
60
 
54
61
  def process_records(records, stream)
@@ -59,8 +66,7 @@ module Multiwoven
59
66
  records.each do |record_object|
60
67
  record = extract_data(record_object, properties)
61
68
  args = [stream.name, "Id", record]
62
- klass = @client.const_get(stream.name)
63
- response = klass.send(@action, record)
69
+ response = create_record(stream.name, record)
64
70
  write_success += 1
65
71
  log_message_array << log_request_response("info", args, response)
66
72
  rescue StandardError => e
@@ -76,8 +82,18 @@ module Multiwoven
76
82
  tracking_message(write_success, write_failure, log_message_array)
77
83
  end
78
84
 
85
+ def create_record(stream_name, record)
86
+ raise ArgumentError, "Unsupported Stripe action" unless @action == CREATE_ACTION
87
+
88
+ stream_class(stream_name).create(record, @request_options)
89
+ end
90
+
91
+ def stream_class(stream_name)
92
+ STREAM_CLASSES.fetch(stream_name) { raise ArgumentError, "Unsupported Stripe stream" }
93
+ end
94
+
79
95
  def authenticate_client
80
- @client::Customer.list
96
+ ::Stripe::Customer.list({}, @request_options)
81
97
  end
82
98
 
83
99
  def load_catalog
@@ -76,9 +76,9 @@ module Multiwoven
76
76
  args = [plural_stream_name, @action, zendesk_data]
77
77
 
78
78
  if @action == "create"
79
- response = @client.send(plural_stream_name).create!(zendesk_data)
79
+ response = zendesk_resources(plural_stream_name).create!(zendesk_data)
80
80
  else
81
- existing_record = @client.send(plural_stream_name).find(id: record[:id])
81
+ existing_record = zendesk_resources(plural_stream_name).find(id: record[:id])
82
82
  response = existing_record.update!(zendesk_data)
83
83
  end
84
84
 
@@ -101,6 +101,14 @@ module Multiwoven
101
101
  { "ticket" => "tickets", "user" => "users" }.fetch(name, name)
102
102
  end
103
103
 
104
+ def zendesk_resources(plural_stream_name)
105
+ case plural_stream_name
106
+ when "tickets" then @client.tickets
107
+ when "users" then @client.users
108
+ else raise ArgumentError, "Unsupported Zendesk stream"
109
+ end
110
+ end
111
+
104
112
  def prepare_record_data(record, type)
105
113
  case type
106
114
  when "Tickets"
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Multiwoven
4
4
  module Integrations
5
- VERSION = "0.43.12"
5
+ VERSION = "0.43.13"
6
6
 
7
7
  ENABLED_SOURCES = %w[
8
8
  Snowflake
@@ -73,13 +73,11 @@ module Multiwoven::Integrations::Source
73
73
  end
74
74
 
75
75
  def create_connection(connection_config)
76
- credentials = connection_config["credentials_json"]
77
-
78
- credentials["private_key"] = credentials["private_key"].gsub("\\n", "\n") if credentials["private_key"].include?("\\n")
79
-
80
76
  Google::Cloud::Bigquery.new(
81
77
  project: connection_config["project_id"],
82
- credentials: connection_config["credentials_json"]
78
+ credentials: Multiwoven::Integrations::Core::GoogleServiceAccountCredentials.build(
79
+ connection_config["credentials_json"], scope: GOOGLE_BIGQUERY_SCOPE
80
+ )
83
81
  )
84
82
  end
85
83
 
@@ -4,6 +4,10 @@ module Multiwoven::Integrations::Source
4
4
  module VertexModel
5
5
  include Multiwoven::Integrations::Core
6
6
  class Client < SourceConnector
7
+ RESOURCE_ID_FIELDS = %w[project_id region endpoint_id].freeze
8
+ RESOURCE_ID_FORMAT = /\A[a-z0-9-]+\z/.freeze
9
+ INVALID_RESOURCE_ID_MESSAGE = "%<field>s must contain only lowercase letters, digits and hyphens"
10
+
7
11
  def check_connection(connection_config)
8
12
  connection_config = connection_config.with_indifferent_access
9
13
  create_connection(connection_config)
@@ -42,17 +46,28 @@ module Multiwoven::Integrations::Source
42
46
  private
43
47
 
44
48
  def create_connection(connection_config)
45
- Multiwoven::Integrations::Core::NetworkEgressGuard.ensure_allowed_url!(build_url(GOOGLE_VERTEX_ENDPOINT_SERVICE_URL, connection_config))
46
- Google::Cloud::AIPlatform::V1::EndpointService::Client.configure do |config|
47
- config.endpoint = build_url(GOOGLE_VERTEX_ENDPOINT_SERVICE_URL, connection_config)
48
- config.credentials = connection_config["credentials_json"]
49
+ validate_resource_ids(connection_config)
50
+ endpoint = build_url(GOOGLE_VERTEX_ENDPOINT_SERVICE_URL, connection_config)
51
+ Multiwoven::Integrations::Core::NetworkEgressGuard.ensure_allowed_url!(endpoint)
52
+ credentials = Multiwoven::Integrations::Core::GoogleServiceAccountCredentials.build(
53
+ connection_config["credentials_json"], scope: GOOGLE_CLOUD_PLATFORM_SCOPE
54
+ )
55
+ @client = Google::Cloud::AIPlatform::V1::EndpointService::Client.new do |config|
56
+ config.endpoint = endpoint
57
+ config.credentials = credentials
58
+ end
59
+ @endpoint = Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
60
+ config.endpoint = endpoint
61
+ config.credentials = credentials
49
62
  end
50
- Google::Cloud::AIPlatform::V1::PredictionService::Client.configure do |config|
51
- config.endpoint = build_url(GOOGLE_VERTEX_ENDPOINT_SERVICE_URL, connection_config)
52
- config.credentials = connection_config["credentials_json"]
63
+ end
64
+
65
+ def validate_resource_ids(connection_config)
66
+ RESOURCE_ID_FIELDS.each do |field|
67
+ next if connection_config[field].to_s.match?(RESOURCE_ID_FORMAT)
68
+
69
+ raise ArgumentError, format(INVALID_RESOURCE_ID_MESSAGE, field: field)
53
70
  end
54
- @client = Google::Cloud::AIPlatform::V1::EndpointService::Client.new
55
- @endpoint = Google::Cloud::AIPlatform::V1::PredictionService::Client.new
56
71
  end
57
72
 
58
73
  def run_model(connection_config, payload)
@@ -5,6 +5,9 @@ module Multiwoven::Integrations::Source
5
5
  include Multiwoven::Integrations::Core
6
6
  class Client < SourceConnector
7
7
  include Multiwoven::Integrations::Core::OauthClientCredentials
8
+ MAX_RECORDS = 100_000
9
+ TOO_MANY_RECORDS_MESSAGE = "HTTP response has more than #{MAX_RECORDS} rows. Use pagination or a narrower parse_response path"
10
+ INVALID_JSON_RESPONSE_MESSAGE = "HTTP response body is not valid JSON"
8
11
 
9
12
  def check_connection(connection_config)
10
13
  connection_config = prepare_config(connection_config)
@@ -46,9 +49,9 @@ module Multiwoven::Integrations::Source
46
49
  config: connection_config[:config],
47
50
  params: connection_config[:params]
48
51
  )
49
- raise StandardError, "Response code: #{response.code}, Body: #{response.body}" unless success?(response)
52
+ raise StandardError, query_failure_message(response) unless success?(response)
50
53
 
51
- catalog = Catalog.new(streams: create_streams(JSON.parse(response.body)))
54
+ catalog = Catalog.new(streams: create_streams(parse_json_body(response.body)))
52
55
  catalog.to_multiwoven_message
53
56
  rescue StandardError => e
54
57
  handle_exception(e, { context: "HTTP:DISCOVER:EXCEPTION", type: "error" })
@@ -140,7 +143,7 @@ module Multiwoven::Integrations::Source
140
143
  params: connection_config[:params] || {}
141
144
  )
142
145
  if success?(response)
143
- response_body = JSON.parse(response.body)
146
+ response_body = parse_json_body(response.body)
144
147
  parse_response = get_parse_response(connection_config[:parse_response])
145
148
  parse_response(response_body, parse_response)
146
149
  else
@@ -155,6 +158,12 @@ module Multiwoven::Integrations::Source
155
158
  "Failed to fetch data: HTTP #{code}"
156
159
  end
157
160
 
161
+ def parse_json_body(body)
162
+ JSON.parse(body)
163
+ rescue JSON::ParserError
164
+ raise StandardError, INVALID_JSON_RESPONSE_MESSAGE, cause: nil
165
+ end
166
+
158
167
  def create_streams(response_body)
159
168
  group_by_table(response_body).map do |r|
160
169
  Multiwoven::Integrations::Protocol::Stream.new(name: r["name"], action: StreamAction["fetch"], json_schema: r["schema"])
@@ -162,22 +171,33 @@ module Multiwoven::Integrations::Source
162
171
  end
163
172
 
164
173
  def get_parse_response(parse_response)
165
- parse_response = JSON.parse(parse_response) if parse_response.is_a?(String) && parse_response.start_with?("[")
174
+ return parse_response unless parse_response.is_a?(String) && parse_response.start_with?("[")
175
+
176
+ path_list = JSON.parse(parse_response)
177
+ path_list?(path_list) ? path_list : parse_response
178
+ rescue JSON::ParserError
166
179
  parse_response
167
180
  end
168
181
 
182
+ # A rootless bracket path such as ['data'][0] also starts with "[", so only a JSON array of path strings (header and values paths) is parsed as one.
183
+ def path_list?(value)
184
+ value.is_a?(Array) && value.size >= 2 && value.all?(String)
185
+ end
186
+
169
187
  def parse_response(response_body, parse_response)
170
188
  case parse_response
171
189
  when Array
172
- headers = JsonPath.on(response_body, parse_response[0]).flatten(1)
173
- values = JsonPath.on(response_body, parse_response[1])
190
+ headers = Multiwoven::Integrations::Core::SafeJsonPath.on(response_body, parse_response[0]).flatten(1)
191
+ values = Multiwoven::Integrations::Core::SafeJsonPath.on(response_body, parse_response[1])
174
192
  rows = values.first.is_a?(Array) ? values : values.each_slice(headers.size).to_a
193
+ raise StandardError, TOO_MANY_RECORDS_MESSAGE if rows.size > MAX_RECORDS
194
+
175
195
  rows.map do |row_values|
176
- data = Hash[headers.zip(row_values)]
196
+ data = Hash[headers.first(row_values.size).zip(row_values)]
177
197
  RecordMessage.new(data: data, emitted_at: Time.now.to_i).to_multiwoven_message
178
198
  end
179
199
  else
180
- records = JsonPath.on(response_body, parse_response)
200
+ records = Multiwoven::Integrations::Core::SafeJsonPath.on(response_body, parse_response)
181
201
  records.map do |data|
182
202
  RecordMessage.new(data: data, emitted_at: Time.now.to_i).to_multiwoven_message
183
203
  end
@@ -51,7 +51,8 @@ module Multiwoven::Integrations::Source
51
51
  port: connection_config[:port],
52
52
  user: connection_config[:username],
53
53
  password: connection_config[:password],
54
- database: connection_config[:database]
54
+ database: connection_config[:database],
55
+ local_infile: false
55
56
  )
56
57
  end
57
58
 
@@ -45,7 +45,6 @@ require "xmlrpc/client"
45
45
  require "googleauth"
46
46
  require "google/apis/drive_v3"
47
47
  require "aws-sdk-textract"
48
- require "jsonpath"
49
48
  require "pdf/reader"
50
49
 
51
50
  # Service
@@ -57,6 +56,8 @@ require_relative "integrations/service"
57
56
  require_relative "integrations/core/constants"
58
57
  require_relative "integrations/core/utils"
59
58
  require_relative "integrations/core/duckdb_lockdown"
59
+ require_relative "integrations/core/safe_json_path"
60
+ require_relative "integrations/core/google_service_account_credentials"
60
61
  require_relative "integrations/core/network_egress_guard"
61
62
  require_relative "integrations/core/odbc_connection_string"
62
63
  require_relative "integrations/core/rate_limiter"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiwoven-integrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.43.12
4
+ version: 0.43.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Subin T P
@@ -636,6 +636,7 @@ files:
636
636
  - lib/multiwoven/integrations/core/destination_connector.rb
637
637
  - lib/multiwoven/integrations/core/duckdb_lockdown.rb
638
638
  - lib/multiwoven/integrations/core/fullrefresher.rb
639
+ - lib/multiwoven/integrations/core/google_service_account_credentials.rb
639
640
  - lib/multiwoven/integrations/core/http_client.rb
640
641
  - lib/multiwoven/integrations/core/http_helper.rb
641
642
  - lib/multiwoven/integrations/core/network_egress_guard.rb
@@ -644,6 +645,7 @@ files:
644
645
  - lib/multiwoven/integrations/core/open_router_catalog.rb
645
646
  - lib/multiwoven/integrations/core/query_builder.rb
646
647
  - lib/multiwoven/integrations/core/rate_limiter.rb
648
+ - lib/multiwoven/integrations/core/safe_json_path.rb
647
649
  - lib/multiwoven/integrations/core/source_connector.rb
648
650
  - lib/multiwoven/integrations/core/streaming_http_client.rb
649
651
  - lib/multiwoven/integrations/core/unstructured_source_connector.rb