Skip to content

Commit

Permalink
Refactor order poller, log masked SF key (#507)
Browse files Browse the repository at this point in the history
* Refactoring order poller to be more modular

Makes debugging in prod easier

* Log masked api key for easier debugging
  • Loading branch information
mbianco-stripe authored Jun 16, 2022
1 parent 35b0ebf commit 29080aa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
9 changes: 8 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Application < Rails::Application
SALESFORCE_ACCOUNT_ID_HEADER,
SALESFORCE_PACKAGE_NAMESPACE_HEADER,
SALESFORCE_INSTANCE_TYPE_HEADER,
SALESFORCE_PACKAGE_ID_HEADER,
]

config.lograge.enabled = true
Expand All @@ -63,7 +64,13 @@ class Application < Rails::Application
SALESFORCE_HEADERS.each {|h| custom_params[h] = event.payload[:request].headers[h] }

# include a boolean to indicate if the key was specified
custom_params[SALESFORCE_KEY_HEADER] = event.payload[:request].headers[SALESFORCE_KEY_HEADER].present?
sf_key = event.payload[:request].headers[SALESFORCE_KEY_HEADER]
custom_params[SALESFORCE_KEY_HEADER] = if sf_key.present?
# https://stackoverflow.com/questions/42938422/how-to-mask-all-but-last-four-characters-in-a-string
sf_key.gsub(/.(?=.{4})/, '*')
else
'false'
end

custom_params.reject {|_, v| v.blank? }
end
Expand Down
1 change: 1 addition & 0 deletions lib/stripe-force/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class OrderStatusOptions < T::Enum
SALESFORCE_KEY_HEADER = 'Salesforce-Key'
SALESFORCE_PACKAGE_NAMESPACE_HEADER = "Salesforce-Package-Namespace"
SALESFORCE_INSTANCE_TYPE_HEADER = 'Salesforce-Type'
SALESFORCE_PACKAGE_ID_HEADER = 'Salesforce-Package-Id'

CONNECTOR_SETTING_SALESFORCE_NAMESPACE = "salesforce_namespace"
CONNECTOR_SETTING_SALESFORCE_INSTANCE_TYPE = 'salesforce_instance_type'
Expand Down
27 changes: 15 additions & 12 deletions lib/stripe-force/jobs/order_poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class StripeForce::OrderPoller < StripeForce::PollerBase
def perform
# TODO lock on job in a separate wrapper job
# TODO lock on job in a separate wrapper job in the future
locker.lock_on_poll_job(self.class)

execution_time = Time.now.utc
Expand All @@ -18,17 +18,7 @@ def perform
log.info 'initiating poll', from: poll_record.last_polled_at, to: execution_time

# note that SF fields never contain empty strings, they are reported null instead
updated_orders = sf.query(
<<~EOL
SELECT Id
FROM #{SF_ORDER}
WHERE
#{prefixed_stripe_field(GENERIC_STRIPE_ID)} = null AND
LastModifiedDate >= #{poll_record.last_polled_at.iso8601} AND
LastModifiedDate < #{execution_time.iso8601}
#{user_specified_where_clause_for_object}
EOL
)
updated_orders = @user.sf_client.query(generate_soql(poll_record.last_polled_at, execution_time))

# important to use `size` here and not count because of the paging issue described below
log.info 'order query complete', size: updated_orders.size
Expand Down Expand Up @@ -60,6 +50,19 @@ def perform
updated_order_ids
end

private def generate_soql(time_start, time_end)
# LastModifiedDate has second, but not millisecond, granularity
<<~EOL
SELECT Id
FROM #{poll_type}
WHERE
#{prefixed_stripe_field(GENERIC_STRIPE_ID)} = null AND
LastModifiedDate >= #{time_start.iso8601} AND
LastModifiedDate < #{time_end.iso8601}
#{user_specified_where_clause_for_object}
EOL
end

private def user_specified_where_clause_for_object
custom_query = @user.connector_settings.dig("filters", poll_type)

Expand Down
4 changes: 0 additions & 4 deletions lib/stripe-force/jobs/poller_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ def poll_timestamp
)
end

def sf
@user.sf_client
end

sig { abstract.returns(String) }
def poll_type; end

Expand Down

0 comments on commit 29080aa

Please sign in to comment.