Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connector): Use ConnectorError::InvalidConnectorConfig for an invalid CoinbaseConnectorMeta #3168

Merged
merged 18 commits into from
Jan 25, 2024

Conversation

bkontur
Copy link
Contributor

@bkontur bkontur commented Dec 19, 2023

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

I've removed to_connector_meta_from_secret_with_required_field, which uses MissingRequiredField as an error (I also didn't like the unnecessary initialization of the MissingRequiredField before we knew if it would be needed). Instead, I added a TryFrom implementation for CoinbaseConnectorMeta, which uses the requested ConnectorError::InvalidConnectorConfig. Additionally, I included CoinbaseConnectorMeta::try_from in the validate_auth_and_metadata_type as suggested.

Migration

-- The migration is for adding an empty `pricing_type` attribute for `CoinbaseConnectorMeta` where it is missing.
UPDATE merchant_connector_account
SET metadata = jsonb_insert(
    metadata,
    '{pricing_type}',
    '""',
    true
)
WHERE connector_name = 'coinbase'
    AND NOT metadata ? 'pricing_type';

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

This PR addresses the bug from the issue: #2899.

How did you test it?

Run unit test:

cargo test --release  --package router --lib connector::coinbase::transformers::tests::coinbase_payments_request_try_from_works -- --exact

image

Migration script test

https://onecompiler.com/postgresql/3zz2tpkad

Test script with test data and run migration:

CREATE TABLE merchant_connector_account (
    id SERIAL PRIMARY KEY,
    connector_name VARCHAR(255) NOT NULL
);
ALTER TABLE merchant_connector_account ADD COLUMN metadata JSONB DEFAULT NULL;

-- insert test data
INSERT INTO merchant_connector_account (
        id,
        connector_name,
        metadata
    )
VALUES
  (1, 'coinbase', '{ "pricing_type": "fixed-rate" }'),
  (2, 'coinbase', '{ "whatever_attribute": "abc" }'),
  (3, 'coinbase', '{}');

-- Before
select * from merchant_connector_account;

-- Run migration:
-- The migration is for adding an empty `pricing_type` attribute for `CoinbaseConnectorMeta` where it is missing.
UPDATE merchant_connector_account
SET metadata = jsonb_insert(
    metadata,
    '{pricing_type}',
    '""',
    true
)
WHERE connector_name = 'coinbase'
    AND NOT metadata ? 'pricing_type';
    
-- After
select * from merchant_connector_account;

Output:

Output:

CREATE TABLE
ALTER TABLE
INSERT 0 3
 id | connector_name |            metadata            
----+----------------+--------------------------------
  1 | coinbase       | {"pricing_type": "fixed-rate"}
  2 | coinbase       | {"whatever_attribute": "abc"}
  3 | coinbase       | {}
(3 rows)

UPDATE 2
 id | connector_name |                     metadata                      
----+----------------+---------------------------------------------------
  1 | coinbase       | {"pricing_type": "fixed-rate"}
  2 | coinbase       | {"pricing_type": "", "whatever_attribute": "abc"}
  3 | coinbase       | {"pricing_type": ""}
(3 rows)

Run locally:

Invalid metadata:
image

Empty metadata:
image

Correct metadata:
image

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible
  • I added a CHANGELOG entry if applicable

@bkontur bkontur requested review from a team as code owners December 19, 2023 12:53
@github-actions github-actions bot added the S-conventions-not-followed Status: This PR does not follow contributing guidelines label Dec 19, 2023
@bkontur bkontur requested a review from a team as a code owner December 19, 2023 12:57
@bkontur bkontur changed the title [fix-2899] use ConnectorError::InvalidConnectorConfig for an invalid CoinbaseConnectorMeta fix(connector): Use ConnectorError::InvalidConnectorConfig for an invalid CoinbaseConnectorMeta Dec 19, 2023
@github-actions github-actions bot removed the S-conventions-not-followed Status: This PR does not follow contributing guidelines label Dec 19, 2023
@SanchithHegde SanchithHegde added A-connector-integration Area: Connector integration C-bug Category: Bug S-waiting-on-review Status: This PR has been implemented and needs to be reviewed C-refactor Category: Refactor labels Dec 19, 2023
@SanchithHegde SanchithHegde linked an issue Dec 19, 2023 that may be closed by this pull request
2 tasks
Copy link
Contributor

@srujanchikke srujanchikke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please attach screenshots of testing if possible. refer #2746 to know how to test this issue.

crates/router/src/configs/settings.rs Outdated Show resolved Hide resolved
crates/router/src/connector/coinbase/transformers.rs Outdated Show resolved Hide resolved
crates/router/src/connector/utils.rs Outdated Show resolved Hide resolved
crates/router/src/connector/worldline/transformers.rs Outdated Show resolved Hide resolved
crates/router/src/core/fraud_check.rs Outdated Show resolved Hide resolved
crates/router/src/core/payment_methods/vault.rs Outdated Show resolved Hide resolved
@@ -250,6 +252,17 @@ pub struct CoinbaseConnectorMeta {
pub pricing_type: String,
}

impl TryFrom<&Option<pii::SecretSerdeValue>> for CoinbaseConnectorMeta {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes will impact previous merchants who are using coinbase, This PR needs data migrations. Could you please refer #2746 description for more info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue#2899 does not mention any data migrations.
I've checked that #2746, and in that case, the migration involved replacing terminalId with terminal_id due to the removal of #[serde(rename_all = "camelCase")] here.

However, my issue is different. If I understand correctly, this issue is about empty JSON for CoinbaseConnectorMeta or the absence of the pricing_type attribute in the JSON, and the goal is to add validations to prevent storing invalid CoinbaseConnectorMeta.

So, I am not sure about the migration here. If empty JSON or JSON without pricing_type is stored, how should the migration to the correct CoinbaseConnectorMeta be handled? What value should be set for pricing_type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, We will take this up as these are data migrations. FYI, I was talking about existing merchants, we need to add pricing_type in metadata and set it to empty string. In that case, when merchant tries to make payment connector throws error and they are expected to update the metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srujanchikke aha, ok, thank you, make senses now, so I prepared migration script and simple test demonstration that it works, please see PR description.

@srujanchikke srujanchikke added S-waiting-on-author Status: This PR is incomplete or needs to address review comments and removed S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Jan 4, 2024
@bkontur
Copy link
Contributor Author

bkontur commented Jan 6, 2024

Please attach screenshots of testing if possible. refer #2746 to know how to test this issue.

Ok, I will try the next week

Copy link
Contributor

@srujanchikke srujanchikke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to make changes where ever CoinbaseConnectorMeta is used

let connector_meta: CoinbaseConnectorMeta =

let metadata = item.router_data.get_connector_meta()?; let connector_meta: CoinbaseConnectorMeta = metadata.parse_value(“CoinbaseConnectorMeta”) .change_context(errors::ConnectorError::InvalidConnectorConfig { config: “Merchant connector account metadata”, })?;

crates/router/src/connector/coinbase/transformers.rs Outdated Show resolved Hide resolved
@@ -250,6 +252,17 @@ pub struct CoinbaseConnectorMeta {
pub pricing_type: String,
}

impl TryFrom<&Option<pii::SecretSerdeValue>> for CoinbaseConnectorMeta {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, We will take this up as these are data migrations. FYI, I was talking about existing merchants, we need to add pricing_type in metadata and set it to empty string. In that case, when merchant tries to make payment connector throws error and they are expected to update the metadata.

@bkontur
Copy link
Contributor Author

bkontur commented Jan 11, 2024

Please attach screenshots of testing if possible. refer #2746 to know how to test this issue.

Ok, I will try the next week

@srujanchikke testing done with local docker compose --file docker-compose-development.yml up -d.
I like the prepared postman stuff :), please, see the description with attached screenshots.

@bkontur
Copy link
Contributor Author

bkontur commented Jan 17, 2024

@srujanchikke I think ,I've addressed all your review comments, added migration script (with simple test for sql) and added screenshots from postman testing, anything else?

Copy link
Contributor

@srujanchikke srujanchikke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bkontur ,
Apologies for the delay, Given some minor comments, Please resolve it.

crates/router/src/core/admin.rs Outdated Show resolved Hide resolved
crates/router/src/connector/coinbase/transformers.rs Outdated Show resolved Hide resolved
crates/router/tests/connectors/coinbase.rs Outdated Show resolved Hide resolved
@Gnanasundari24 Gnanasundari24 added this pull request to the merge queue Jan 25, 2024
Merged via the queue into juspay:main with commit d827c9a Jan 25, 2024
12 checks passed
@SanchithHegde SanchithHegde removed the S-waiting-on-author Status: This PR is incomplete or needs to address review comments label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-connector-integration Area: Connector integration C-bug Category: Bug C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: MCA metadata deserialization failures should be 4xx
5 participants