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

Added separate executor list to handle CDS unauthenticated endpoints #19

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.wso2.openbanking.accelerator.gateway.executor.core.OpenBankingGatewayExecutor;
import com.wso2.openbanking.accelerator.gateway.executor.model.OBAPIRequestContext;
import com.wso2.openbanking.accelerator.gateway.executor.model.OBAPIResponseContext;
import org.wso2.openbanking.cds.gateway.utils.GatewayConstants;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -46,6 +47,16 @@ public List<OpenBankingGatewayExecutor> getExecutorsForRequest(OBAPIRequestConte
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_NON_REGULATORY);
return EMPTY_LIST;
} else if (GatewayConstants.UNAUTHENTICATED_ENDPOINTS.contains(requestContext.getMsgInfo()
.getElectedResource())) {
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_CDS_UNAUTHENTICATED);
return this.getExecutorMap().get(RequestRouterConstants.CDS_UNAUTHENTICATED);
} else if (GatewayConstants.COMMON_ENDPOINTS.contains(requestContext.getMsgInfo()
.getElectedResource())) {
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_COMMON);
return this.getExecutorMap().get(RequestRouterConstants.CDS_COMMON);
} else if (RequestRouterConstants.API_TYPE_CONSENT
.equals(requestContext.getOpenAPI().getExtensions().get(RequestRouterConstants.API_TYPE_CUSTOM_PROP))) {
// Add support for consent management portal APIs
Expand All @@ -61,10 +72,6 @@ public List<OpenBankingGatewayExecutor> getExecutorsForRequest(OBAPIRequestConte
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_CDS);
return this.getExecutorMap().get(RequestRouterConstants.CDS);
} else if (RequestRouterConstants.COMMON_API_NAME.equals(requestContext.getOpenAPI().getInfo().getTitle())) {
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_COMMON);
return this.getExecutorMap().get(RequestRouterConstants.CDS_COMMON);
} else if (RequestRouterConstants.ADMIN_API_NAME.equals(requestContext.getOpenAPI().getInfo().getTitle())) {
requestContext.addContextProperty(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_ADMIN);
Expand Down Expand Up @@ -105,6 +112,9 @@ public List<OpenBankingGatewayExecutor> getExecutorsForResponse(OBAPIResponseCon
case RequestRouterConstants.API_TYPE_CDS:
executorList = this.getExecutorMap().get(RequestRouterConstants.CDS);
break;
case RequestRouterConstants.API_TYPE_CDS_UNAUTHENTICATED:
executorList = this.getExecutorMap().get(RequestRouterConstants.CDS_UNAUTHENTICATED);
break;
case RequestRouterConstants.API_TYPE_COMMON:
executorList = this.getExecutorMap().get(RequestRouterConstants.CDS_COMMON);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RequestRouterConstants {
public static final String DEFAULT = "Default";
public static final String DCR = "DCR";
public static final String CDS = "CDS";
public static final String CDS_UNAUTHENTICATED = "CDSUnauthenticated";
public static final String CDS_COMMON = "CDSCommon";
public static final String CONSENT = "Consent";
public static final String ADMIN = "Admin";
Expand All @@ -38,6 +39,7 @@ public class RequestRouterConstants {
public static final String API_TYPE_NON_REGULATORY = "non-regulatory";
public static final String API_TYPE_DCR = "dcr";
public static final String API_TYPE_CDS = "cds";
public static final String API_TYPE_CDS_UNAUTHENTICATED = "cds-unauthenticated";
public static final String API_TYPE_COMMON = "common";
public static final String API_TYPE_ARRANGEMENT = "arrangement";
public static final String API_TYPE_ADMIN = "admin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,22 @@ private GatewayConstants() {
public static final String CDR_ARRANGEMENT_ENDPOINT = "/{cdrArrangementId}";
public static final String DISCOVERY_OUTAGES_ENDPOINT = "/discovery/outages";
public static final String DISCOVERY_STATUS_ENDPOINT = "/discovery/status";
public static final String PRODUCTS_ENDPOINT = "/banking/products";
public static final String PRODUCT_DETAILS_ENDPOINT = "/banking/products/{productId}";
public static final String COMMON_CUSTOMER_ENDPOINT = "/common/customer";
public static final String COMMON_CUSTOMER_DETAIL_ENDPOINT = "/common/customer/detail";

public static final List<String> INFOSEC_ENDPOINTS = Collections.unmodifiableList(Arrays.asList(AUTHORIZE_ENDPOINT,
TOKEN_ENDPOINT, USERINFO_ENDPOINT, PAR_ENDPOINT, INTROSPECTION_ENDPOINT, JWKS_ENDPOINT, REVOKE_ENDPOINT,
REGISTER_ENDPOINT, REGISTER_CLIENT_ID_ENDPOINT, WELL_KNOWN_ENDPOINT, CDR_ARRANGEMENT_ENDPOINT,
DISCOVERY_OUTAGES_ENDPOINT, DISCOVERY_STATUS_ENDPOINT));

public static final List<String> UNAUTHENTICATED_ENDPOINTS = Collections.unmodifiableList(Arrays.asList(
PRODUCTS_ENDPOINT, PRODUCT_DETAILS_ENDPOINT, DISCOVERY_STATUS_ENDPOINT, DISCOVERY_OUTAGES_ENDPOINT));

public static final List<String> COMMON_ENDPOINTS = Collections.unmodifiableList(Arrays.asList(
COMMON_CUSTOMER_ENDPOINT, COMMON_CUSTOMER_DETAIL_ENDPOINT));

public static final String UNKNOWN = "Unknown";
public static final String CLIENT_USER_AGENT = "User-Agent";
public static final String USER_NAME = "api.ut.userName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.apimgt.common.gateway.dto.MsgInfoDTO;
import org.wso2.openbanking.cds.gateway.test.util.TestUtil;
import org.wso2.openbanking.cds.gateway.utils.GatewayConstants;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -60,6 +62,10 @@ public void testDCRRequestsForRouter() {
contextProps.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP, RequestRouterConstants.API_TYPE_DCR);
openAPI.setExtensions(extensions);
Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI);

MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO);

Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps);
Mockito.when(apiInfo.getTitle()).thenReturn(RequestRouterConstants.DCR_API_NAME);
Assert.assertNotNull(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext));
Expand All @@ -80,6 +86,10 @@ public void testCDSRequestsForRouter() {
contextProps.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP, RequestRouterConstants.API_TYPE_CDS);
openAPI.setExtensions(extensions);
Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI);

MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO);

Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps);
Mockito.when(apiInfo.getTitle()).thenReturn(RequestRouterConstants.CDS_API_NAME);
Assert.assertNotNull(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext));
Expand All @@ -98,6 +108,10 @@ public void testOtherRequestsForRouter() {
Map<String, String> contextProps = new HashMap<>();
openAPI.setExtensions(extensions);
Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI);

MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO);

Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps);
Mockito.when(apiInfo.getTitle()).thenReturn(RequestRouterConstants.CDS_API_NAME);
Assert.assertNotNull(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext));
Expand All @@ -119,4 +133,50 @@ public void testNonRegulatoryAPIcall() {
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext).size(), 0);
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForResponse(obapiResponseContext).size(), 0);
}

@Test(priority = 3)
public void testUnauthenticatedAPIcall() {

OBAPIRequestContext obapiRequestContext = Mockito.mock(OBAPIRequestContext.class);
OBAPIResponseContext obapiResponseContext = Mockito.mock(OBAPIResponseContext.class);
Map<String, Object> extensions = new HashMap<>();
Map<String, String> contextProps = new HashMap<>();
extensions.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_CDS_UNAUTHENTICATED);
contextProps.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_CDS_UNAUTHENTICATED);
openAPI.setExtensions(extensions);
Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI);

MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
msgInfoDTO.setElectedResource(GatewayConstants.PRODUCT_DETAILS_ENDPOINT);
Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO);

Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps);
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext).size(), 1);
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForResponse(obapiResponseContext).size(), 1);
}

@Test(priority = 3)
public void testCommonAPIcall() {

OBAPIRequestContext obapiRequestContext = Mockito.mock(OBAPIRequestContext.class);
OBAPIResponseContext obapiResponseContext = Mockito.mock(OBAPIResponseContext.class);
Map<String, Object> extensions = new HashMap<>();
Map<String, String> contextProps = new HashMap<>();
extensions.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_COMMON);
contextProps.put(RequestRouterConstants.API_TYPE_CUSTOM_PROP,
RequestRouterConstants.API_TYPE_COMMON);
openAPI.setExtensions(extensions);
Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI);

MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
msgInfoDTO.setElectedResource(GatewayConstants.COMMON_CUSTOMER_ENDPOINT);
Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO);

Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps);
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForRequest(obapiRequestContext).size(), 1);
Assert.assertEquals(cdsApiRequestRouter.getExecutorsForResponse(obapiResponseContext).size(), 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class TestConstants {
public static final Map<String, Map<Integer, String>> FULL_VALIDATOR_MAP = Stream.of(
new AbstractMap.SimpleImmutableEntry<>("Default", VALID_EXECUTOR_MAP),
new AbstractMap.SimpleImmutableEntry<>("DCR", VALID_EXECUTOR_MAP),
new AbstractMap.SimpleImmutableEntry<>("CDS", VALID_EXECUTOR_MAP))
new AbstractMap.SimpleImmutableEntry<>("CDS", VALID_EXECUTOR_MAP),
new AbstractMap.SimpleImmutableEntry<>("CDSCommon", VALID_EXECUTOR_MAP),
new AbstractMap.SimpleImmutableEntry<>("CDSUnauthenticated", VALID_EXECUTOR_MAP))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,24 @@ priority = 8
name = "org.wso2.openbanking.cds.gateway.executors.error.handler.CDSErrorHandler"
priority = 1000

[[open_banking.gateway.openbanking_gateway_executors.type]]
imesh94 marked this conversation as resolved.
Show resolved Hide resolved
name = "CDSUnauthenticated"
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
name = "org.wso2.openbanking.cds.gateway.executors.idpermanence.IDPermanenceExecutor"
priority = 1
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
name = "org.wso2.openbanking.cds.gateway.executors.header.validation.CDSHeaderValidationExecutor"
priority = 2
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
name = "com.wso2.openbanking.accelerator.gateway.executor.impl.common.reporting.data.executor.CommonReportingDataExecutor"
priority = 3
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
name = "org.wso2.openbanking.cds.gateway.executors.reporting.CDSCommonDataReportingExecutor"
priority = 4
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
name = "org.wso2.openbanking.cds.gateway.executors.error.handler.CDSErrorHandler"
priority = 1000

[[open_banking.gateway.openbanking_gateway_executors.type]]
name = "CDSCommon"
[[open_banking.gateway.openbanking_gateway_executors.type.executors]]
Expand Down
Loading