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

[PAYMTS-1728] Add account updater look up service #442

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import com.verygood.security.larky.modules.vgs.AccountUpdaterModule;
import com.verygood.security.larky.modules.BinasciiModule;
import com.verygood.security.larky.modules.C99MathModule;
import com.verygood.security.larky.modules.NetworkTokenModule;
Expand Down Expand Up @@ -98,6 +99,7 @@ public class ModuleSupplier {
public static final ImmutableSet<StarlarkValue> VGS_MODULES = ImmutableSet.of(
VaultModule.INSTANCE,
NetworkTokenModule.INSTANCE,
AccountUpdaterModule.INSTANCE,
CerebroModule.INSTANCE,
ChaseModule.INSTANCE,
JKSModule.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.verygood.security.larky.modules.vgs;

import com.google.common.collect.ImmutableList;
import com.verygood.security.larky.modules.vgs.aus.LarkyAccountUpdater;
import com.verygood.security.larky.modules.vgs.aus.MockAccountUpdaterService;
import com.verygood.security.larky.modules.vgs.aus.NoopAccountUpdaterService;
import com.verygood.security.larky.modules.vgs.aus.spi.AccountUpdaterService;
import java.util.List;
import java.util.ServiceLoader;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.*;

@StarlarkBuiltin(
name = "native_au",
category = "BUILTIN",
doc = "Overridable Account Updater API in Larky")
public class AccountUpdaterModule implements LarkyAccountUpdater {
public static final AccountUpdaterModule INSTANCE = new AccountUpdaterModule();

public static final String ENABLE_MOCK_PROPERTY =
"larky.modules.vgs.nts.enableMockAccountUpdater";

private final AccountUpdaterService AccountUpdaterService;

public AccountUpdaterModule() {
final ServiceLoader<AccountUpdaterService> loader =
ServiceLoader.load(AccountUpdaterService.class);
final List<AccountUpdaterService> AccountUpdaterProviders =
ImmutableList.copyOf(loader.iterator());

if (Boolean.getBoolean(ENABLE_MOCK_PROPERTY)) {
AccountUpdaterService = new MockAccountUpdaterService();
} else if (AccountUpdaterProviders.isEmpty()) {
AccountUpdaterService = new NoopAccountUpdaterService();
} else {
if (AccountUpdaterProviders.size() != 1) {
throw new IllegalArgumentException(
String.format(
"AccountUpdaterModule expecting only 1 network token provider of type AccountUpdaterService, found %d",
AccountUpdaterProviders.size()));
}
AccountUpdaterService = AccountUpdaterProviders.get(0);
}
}

@StarlarkMethod(
name = "lookup_card",
doc = "Lookup account updates for a given PAN.",
useStarlarkThread = true,
parameters = {
@Param(
name = "pan",
named = true,
doc = "Card PAN. Used to look up the corresponding card information to be returned",
allowedTypes = {@ParamType(type = String.class)}),
@Param(
name = "exp_month",
named = true,
doc =
"Card expiration month. Used to pass to the network for retrieving the corresponding card information",
allowedTypes = {@ParamType(type = StarlarkInt.class)}),
@Param(
name = "exp_year",
named = true,
doc =
"Card expiration year as two digits integer. Used to pass to the network for retrieving the corresponding card information",
allowedTypes = {@ParamType(type = StarlarkInt.class)}),
@Param(
name = "name",
named = true,
doc =
"Card owner name. Used to pass to the network for retrieving the corresponding card information",
allowedTypes = {@ParamType(type = String.class)}),
@Param(
name = "client_id",
named = true,
doc = "Client ID of service account to access calm API server",
allowedTypes = {@ParamType(type = String.class)}),
@Param(
name = "client_secret",
named = true,
doc = "Client secret of service account to access calm API server",
allowedTypes = {@ParamType(type = String.class)}),
})
@Override
public Object lookupCard(
String pan,
StarlarkInt expireMonth,
StarlarkInt expireYear,
String name,
String clientId,
String clientSecret,
StarlarkThread thread)
throws EvalException {
if (pan.trim().isEmpty()) {
throw Starlark.errorf("card number argument cannot be blank");
}
int expireMonthValue = expireYear.toInt("invalid expireYear int range");
if (expireMonthValue <= 0 || expireMonthValue >= 100) {
throw Starlark.errorf("expireMonth needs to be a positive two digits integer");
}
final AccountUpdaterService.Card card;
try {
card =
AccountUpdaterService.lookupCard(
pan,
expireMonth.toInt("invalid expireMonth int range"),
expireYear.toInt("invalid expireYear int range"),
name,
clientId,
clientSecret);
} catch (UnsupportedOperationException exception) {
throw Starlark.errorf("aus.lookup_card operation must be overridden");
}
if (card == null) {
return Starlark.NONE;
}
return Dict.<String, Object>builder()
.put("number", card.getNumber())
.put("exp_month", StarlarkInt.of(card.getExpireMonth()))
.put("exp_year", StarlarkInt.of(card.getExpireYear()))
.build(thread.mutability());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.verygood.security.larky.modules.vgs.aus;

import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

public interface LarkyAccountUpdater extends StarlarkValue {
/**
* Get updated info for the provided card.
*
* @param pan card's number
* @param expireMonth card's expiration month
* @param expireYear card's expiration year as two digits
* @param name the name on the card
* @param clientId client id of service account to access calm API
* @param clientSecret client secret of service account to access calm API
* @param thread Starlark thread object
* @return a dict contains the network token values
*/
Object lookupCard(
String pan,
StarlarkInt expireMonth,
StarlarkInt expireYear,
String name,
String clientId,
String clientSecret,
Copy link
Contributor

Choose a reason for hiding this comment

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

secret? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

please see my comment at #442 (comment) explaining the rationale

StarlarkThread thread)
throws EvalException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.verygood.security.larky.modules.vgs.aus;

import com.google.common.collect.ImmutableMap;
import com.verygood.security.larky.modules.vgs.aus.spi.AccountUpdaterService;
import java.util.Map;

public class MockAccountUpdaterService implements AccountUpdaterService {

private static final Map<String, Card> CARDS =
ImmutableMap.of(
"4111111111111111",
Card.builder()
.number("4111111111111111")
.expireMonth(10)
.expireYear(27)
.name("John Doe")
.build(),
"4242424242424242",
Card.builder()
.number("4242424242424243")
.expireMonth(12)
.expireYear(27)
.name("John Doe")
.build());

@Override
public Card lookupCard(
String pan,
Integer expireMonth,
Integer expireYear,
String name,
String clientId,
String clientSecret) {
if (!CARDS.containsKey(pan)) {
return null;
}
return CARDS.get(pan);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.verygood.security.larky.modules.vgs.aus;

import com.verygood.security.larky.modules.vgs.aus.spi.AccountUpdaterService;

public class NoopAccountUpdaterService implements AccountUpdaterService {
@Override
public Card lookupCard(
String pan,
Integer expireMonth,
Integer expireYear,
String name,
String clientId,
String clientSecret) {
throw new UnsupportedOperationException("Not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.verygood.security.larky.modules.vgs.aus.spi;

import lombok.Builder;
import lombok.Data;

public interface AccountUpdaterService {
/**
* Get updated info for the provided card
*
* @param number card's number
* @param expireMonth card's expiration month
* @param expireYear card's expiration year
* @param name the name on the card
* @param clientId client id of service account to access calm API
* @param clientSecret client secret of service account to access calm API
* @return the updated card
*/
Card lookupCard(
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure of the point of this method since it returns what it was passed in

Copy link
Contributor

Choose a reason for hiding this comment

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

Or does it return a different card? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A card could be expired, exp date changed, or stolen, so the number changed. It's still the same card, but a different number or exp date. So this service and this method is for looking up the given card number and see if there's updates for the card. The customer would like to use the new card number is there's one in case of the original one is expired.

String number,
Integer expireMonth,
Integer expireYear,
String name,
String clientId,
Copy link
Contributor

Choose a reason for hiding this comment

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

wonder why these need to be passed in and why they are env vars or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are per merchant/vault credentials for calm's service account

String clientSecret);

@Data
@Builder
class Card {
Copy link
Contributor

Choose a reason for hiding this comment

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

consider making record instead of class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to put record syntax here, but my IDE is showing red font. It seems like record is supported only in Java 14, are we using Java 14 in this larky project?

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be using JDK 17 here

private final String number;
private final Integer expireMonth;
private final Integer expireYear;
private final String name;
}
}
29 changes: 29 additions & 0 deletions larky/src/main/resources/vgs/aus.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@vgs//native_au", _au="native_au")
load("@stdlib//larky", larky="larky")

VGS_ACCOUNT_UPDATER_HEADER = "vgs-account-updater"

def lookup_card(pan, exp_month, exp_year, name, client_id, client_secret):
return _au.lookup_card(
pan=pan,
exp_month=exp_month,
exp_year=exp_year,
name=name,
client_id=client_id,
client_secret=client_secret,
)

def use_account_updater(headers):
"""Check value in the headers and determine whether is account updater looking up should be used or not

:param headers: HTTP request headers to be checked against
:return: True if the account updater looking up is enabled, otherwise False
"""
lower_case_headers = {key.lower(): value for key, value in headers.items()}
return lower_case_headers.get(VGS_ACCOUNT_UPDATER_HEADER, "").lower() == "yes"


aus = larky.struct(
lookup_card=lookup_card,
use_account_updater=use_account_updater,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.verygood.security.larky.modules.vgs.aus.MockAccountUpdaterService
66 changes: 66 additions & 0 deletions larky/src/test/resources/vgs_tests/aus/test_default_aus.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
load("@vendor//asserts", "asserts")
load("@stdlib//unittest", "unittest")
load("@vgs//aus", "aus")


def _test_lookup_card_with_exp_updates():
card = aus.lookup_card(
pan="4111111111111111",
exp_year=25,
exp_month=11,
name="Jane Doe",
client_id="AC5RA6rLA-calm-CaWsf",
client_secret="4624b52c-8906-4890-8a35-345168d7c8d7",
)
asserts.assert_that(card["number"]).is_equal_to("4111111111111111")
asserts.assert_that(card["exp_year"]).is_equal_to(27)
asserts.assert_that(card["exp_month"]).is_equal_to(10)

def _test_lookup_card_with_number_updates():
card = aus.lookup_card(
pan="4242424242424242",
exp_year=25,
exp_month=11,
name="Jane Doe",
client_id="AC5RA6rLA-calm-CaWsf",
client_secret="4624b52c-8906-4890-8a35-345168d7c8d7",
)
asserts.assert_that(card["number"]).is_equal_to("4242424242424243")
asserts.assert_that(card["exp_year"]).is_equal_to(27)
asserts.assert_that(card["exp_month"]).is_equal_to(12)


def _test_lookup_card_with_not_existing_card():
card = aus.lookup_card(
pan="999999999999",
exp_year=25,
exp_month=11,
name="Jane Doe",
client_id="AC5RA6rLA-calm-CaWsf",
client_secret="4624b52c-8906-4890-8a35-345168d7c8d7",
)
asserts.assert_that(card).is_none()


def _test_use_account_updater():
asserts.assert_that(aus.use_account_updater({})).is_false()
asserts.assert_that(aus.use_account_updater({"vgs-account-updater": ""})).is_false()
asserts.assert_that(aus.use_account_updater({"vgs-account-updater": "no"})).is_false()
asserts.assert_that(aus.use_account_updater({"vgs-account-updater": "other"})).is_false()
asserts.assert_that(aus.use_account_updater({"vgs-account-updater": "yes"})).is_true()
asserts.assert_that(aus.use_account_updater({"Vgs-Account-Updater": "yes"})).is_true()
asserts.assert_that(aus.use_account_updater({"Vgs-Account-Updater": "Yes"})).is_true()
asserts.assert_that(aus.use_account_updater({"VGS-ACCOUNT-UPDATER": "YES"})).is_true()


def _suite():
_suite = unittest.TestSuite()
_suite.addTest(unittest.FunctionTestCase(_test_lookup_card_with_exp_updates))
_suite.addTest(unittest.FunctionTestCase(_test_lookup_card_with_number_updates))
_suite.addTest(unittest.FunctionTestCase(_test_lookup_card_with_not_existing_card))
_suite.addTest(unittest.FunctionTestCase(_test_use_account_updater))
return _suite


_runner = unittest.TextTestRunner()
_runner.run(_suite())
28 changes: 28 additions & 0 deletions larky/src/test/resources/vgs_tests/aus/test_noop_aus.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Unit tests for AccountUpdateModule.java using NoopVault API"""

load("@vendor//asserts", "asserts")
load("@stdlib//unittest", "unittest")
load("@vgs//aus", "aus")


def _test_lookup_card():
asserts.assert_fails(
lambda: aus.lookup_card(
pan="4242424242424242",
exp_year=25,
exp_month=11,
name="Jane Doe",
client_id="AC5RA6rLA-calm-CaWsf",
client_secret="4624b52c-8906-4890-8a35-345168d7c8d7",
),
"aus.lookup_card operation must be overridden")


def _suite():
_suite = unittest.TestSuite()
_suite.addTest(unittest.FunctionTestCase(_test_lookup_card))
return _suite


_runner = unittest.TextTestRunner()
_runner.run(_suite())