Skip to content

Commit f5a8866

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/revert-proxy-change
2 parents ca36673 + ef576f1 commit f5a8866

File tree

15 files changed

+487
-34
lines changed

15 files changed

+487
-34
lines changed

components/org.wso2.openbanking.cds.common/src/main/java/org/wso2/openbanking/cds/common/config/OpenBankingCDSConfigParser.java

+22
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,26 @@ public int performConfigIntegerValueCheck(String key, int defaultValue) {
812812
}
813813
return defaultValue;
814814
}
815+
816+
/**
817+
* Get external traffic header name.
818+
* This header should be set by the load balancer to identify the external traffic.
819+
*
820+
* @return String
821+
*/
822+
public String getExternalTrafficHeaderName() {
823+
824+
return ((String) getConfigElementFromKey(CommonConstants.EXTERNAL_TRAFFIC_HEADER_NAME)).trim();
825+
}
826+
827+
/**
828+
* Get external traffic expected header value.
829+
* If this value is set in the header identified by the header name, the traffic is considered as external.
830+
*
831+
* @return String
832+
*/
833+
public String getExternalTrafficExpectedValue() {
834+
835+
return ((String) getConfigElementFromKey(CommonConstants.EXTERNAL_TRAFFIC_EXPECTED_VALUE)).trim();
836+
}
815837
}

components/org.wso2.openbanking.cds.common/src/main/java/org/wso2/openbanking/cds/common/utils/CommonConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class CommonConstants {
4545
public static final String VALIDATE_ACCOUNTS_ON_RETRIEVAL = "BNR.ValidateAccountsOnRetrieval";
4646
public static final String ENABLE_CONSENT_REVOCATION = "BNR.EnableConsentRevocation";
4747
public static final String CUSTOMER_TYPE_SELECTION_METHOD = "BNR.CustomerTypeSelectionMethod";
48+
public static final String EXTERNAL_TRAFFIC_HEADER_NAME = "ExternalTraffic.HeaderName";
49+
public static final String EXTERNAL_TRAFFIC_EXPECTED_VALUE = "ExternalTraffic.ExpectedValue";
4850

4951
// Http related constants
5052
public static final String POST_METHOD = "POST";

components/org.wso2.openbanking.cds.common/src/test/java/org/wso2/openbanking/cds/common/OpenBankingCDSConfigParserTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ public void testGetBNRCustomerTypeSelectionMethod() {
289289
Assert.assertEquals(openBankingCDSConfigParser.getBNRCustomerTypeSelectionMethod(), "profile_selection");
290290
}
291291

292+
@Test(priority = 8)
293+
public void testGetExternalTrafficHeaderName() {
294+
String dummyConfigFile = absolutePathForTestResources + "/open-banking-cds.xml";
295+
OpenBankingCDSConfigParser openBankingCDSConfigParser = OpenBankingCDSConfigParser.getInstance(dummyConfigFile);
296+
Assert.assertEquals(openBankingCDSConfigParser.getExternalTrafficHeaderName(), "X-External-Traffic");
297+
}
298+
299+
@Test(priority = 8)
300+
public void testGetExternalTrafficExpectedValue() {
301+
String dummyConfigFile = absolutePathForTestResources + "/open-banking-cds.xml";
302+
OpenBankingCDSConfigParser openBankingCDSConfigParser = OpenBankingCDSConfigParser.getInstance(dummyConfigFile);
303+
Assert.assertEquals(openBankingCDSConfigParser.getExternalTrafficExpectedValue(), "true");
304+
}
305+
292306
private void injectEnvironmentVariable(String key, String value)
293307
throws ReflectiveOperationException {
294308

components/org.wso2.openbanking.cds.common/src/test/resources/open-banking-cds.xml

+4
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@
9494
<DisclosureOptionsManagement>
9595
<Enable>true</Enable>
9696
</DisclosureOptionsManagement>
97+
<ExternalTraffic>
98+
<HeaderName>X-External-Traffic</HeaderName>
99+
<ExpectedValue>true</ExpectedValue>
100+
</ExternalTraffic>
97101
</Server>

components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class CDSConsentEventExecutor implements OBEventExecutor {
9292
private static final String USER_ID_KEY = "userId";
9393
private static final String STATUS_KEY = "status";
9494
private static final String EXPIRY_TIME_KEY = "expiryTime";
95-
private static final ConcurrentLinkedDeque<String> publishedRequestUriKeyQueue = new ConcurrentLinkedDeque<>();
95+
private static final ConcurrentLinkedDeque<String> publishedEventIdentifierQueue = new ConcurrentLinkedDeque<>();
9696

9797
@Override
9898
public void processEvent(OBEvent obEvent) {
@@ -160,12 +160,13 @@ public void processEvent(OBEvent obEvent) {
160160

161161
log.debug("Publishing consent data for authorisation metrics.");
162162
String consentId = (String) eventData.get(CONSENT_ID);
163-
ConsentStatusEnum consentStatus = getConsentStatusForEventType(obEvent.getEventType());
164-
AuthorisationFlowTypeEnum authFlowType = getAuthFlowTypeForEventType(obEvent.getEventType());
165-
String requestUriKey = getRequestUriKeyFromConsentResource(consentResource, detailedConsentResource);
166-
if (requestUriKey != null && publishedRequestUriKeyQueue.contains(requestUriKey)) {
163+
String eventType = obEvent.getEventType();
164+
ConsentStatusEnum consentStatus = getConsentStatusForEventType(eventType);
165+
AuthorisationFlowTypeEnum authFlowType = getAuthFlowTypeForEventType(eventType);
166+
String eventIdentifier = getEventIdentifier(consentResource, detailedConsentResource, eventType);
167+
if (eventIdentifier != null && publishedEventIdentifierQueue.contains(eventIdentifier)) {
167168
if (log.isDebugEnabled()) {
168-
log.debug("Skipping authorisation data publishing for requestUriKey: " + requestUriKey +
169+
log.debug("Skipping authorisation data publishing for event identifier: " + eventIdentifier +
169170
" as it has already been published.");
170171
}
171172
return;
@@ -190,7 +191,7 @@ public void processEvent(OBEvent obEvent) {
190191
consentStatus, authFlowType, customerProfile, consentDurationType);
191192

192193
dataPublishingService.publishAuthorisationData(authorisationData);
193-
addToPublishedRequestUriKeyQueue(requestUriKey);
194+
addToPublishedEventIdentifierQueue(eventIdentifier);
194195
}
195196

196197
}
@@ -390,32 +391,32 @@ private AuthorisationFlowTypeEnum getAuthFlowTypeForEventType(String eventType)
390391
}
391392

392393
/**
393-
* Add the request uri key to the published data queue.
394+
* Add the event identifier to the published data queue.
394395
* If the queue is full, oldest key is removed.
395396
* 20 keys are maintained in the queue to handle simultaneous consent state change events.
396397
*
397-
* @param requestUriKey request uri key coming as a consent attribute
398+
* @param eventIdentifier request uri key coming as a consent attribute + event type to identify a unique event.
398399
*/
399-
private void addToPublishedRequestUriKeyQueue(String requestUriKey) {
400+
private void addToPublishedEventIdentifierQueue(String eventIdentifier) {
400401

401-
if (StringUtils.isBlank(requestUriKey)) {
402+
if (StringUtils.isBlank(eventIdentifier)) {
402403
return;
403404
}
404-
if (publishedRequestUriKeyQueue.size() >= 20) {
405-
publishedRequestUriKeyQueue.pollFirst();
405+
if (publishedEventIdentifierQueue.size() >= 20) {
406+
publishedEventIdentifierQueue.pollFirst();
406407
}
407-
publishedRequestUriKeyQueue.addLast(requestUriKey);
408+
publishedEventIdentifierQueue.addLast(eventIdentifier);
408409
}
409410

410-
private String getRequestUriKeyFromConsentResource(ConsentResource consentResource,
411-
DetailedConsentResource detailedConsentResource) {
411+
private String getEventIdentifier(ConsentResource consentResource, DetailedConsentResource detailedConsentResource,
412+
String eventType) {
412413

413414
Map<String, String> consentAttributes = null;
414415
if (consentResource != null) {
415416
consentAttributes = consentResource.getConsentAttributes();
416417
} else if (detailedConsentResource != null) {
417418
consentAttributes = detailedConsentResource.getConsentAttributes();
418419
}
419-
return consentAttributes != null ? consentAttributes.get(REQUEST_URI_KEY) : null;
420+
return consentAttributes != null ? (consentAttributes.get(REQUEST_URI_KEY) + ":" + eventType) : null;
420421
}
421422
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
3+
* <p>
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
package org.wso2.openbanking.cds.identity.filter;
19+
20+
import org.wso2.openbanking.cds.identity.utils.CDSIdentityConstants;
21+
22+
import javax.servlet.ServletRequest;
23+
24+
/**
25+
* Authorize Data Publishing Filter.
26+
* Implements custom logic related to publishing /authorize request data.
27+
*/
28+
public class AuthorizeDataPublishingFilter extends InfoSecDataPublishingFilter {
29+
30+
@Override
31+
public boolean shouldPublishCurrentRequestData(ServletRequest request) {
32+
33+
// If the sessionDataKey query parameter is present, it is an internal redirect and should not be published.
34+
return request.getParameter(CDSIdentityConstants.SESSION_DATA_KEY_PARAMETER) == null &&
35+
super.shouldPublishCurrentRequestData(request);
36+
}
37+
}

components/org.wso2.openbanking.cds.identity/src/main/java/org/wso2/openbanking/cds/identity/filter/InfoSecDataPublishingFilter.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
3-
*
3+
* <p>
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
66
* in compliance with the License.
77
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
1111
* Unless required by applicable law or agreed to in writing,
1212
* software distributed under the License is distributed on an
1313
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -26,7 +26,9 @@
2626
import org.apache.commons.lang.StringUtils;
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
29+
import org.wso2.openbanking.cds.common.config.OpenBankingCDSConfigParser;
2930
import org.wso2.openbanking.cds.common.data.publisher.CDSDataPublishingService;
31+
import org.wso2.openbanking.cds.common.utils.CommonConstants;
3032
import org.wso2.openbanking.cds.identity.filter.constants.CDSFilterConstants;
3133

3234
import java.io.IOException;
@@ -54,6 +56,11 @@
5456
public class InfoSecDataPublishingFilter implements Filter {
5557

5658
private static final Log LOG = LogFactory.getLog(InfoSecDataPublishingFilter.class);
59+
private final Map<String, Object> configMap = OpenBankingCDSConfigParser.getInstance().getConfiguration();
60+
private final String externalTrafficHeaderName = (String) configMap.get(CommonConstants
61+
.EXTERNAL_TRAFFIC_HEADER_NAME);
62+
private final String expectedExternalTrafficHeaderValue = (String) configMap.get(CommonConstants
63+
.EXTERNAL_TRAFFIC_EXPECTED_VALUE);
5764

5865
@Override
5966
public void init(FilterConfig filterConfig) {
@@ -83,7 +90,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
8390
public void publishReportingData(HttpServletRequest request, HttpServletResponse response) {
8491

8592
if (Boolean.parseBoolean((String) OpenBankingConfigParser.getInstance().getConfiguration()
86-
.get(DataPublishingConstants.DATA_PUBLISHING_ENABLED))) {
93+
.get(DataPublishingConstants.DATA_PUBLISHING_ENABLED)) && shouldPublishCurrentRequestData(request)) {
8794

8895
String messageId = UUID.randomUUID().toString();
8996

@@ -94,6 +101,9 @@ public void publishReportingData(HttpServletRequest request, HttpServletResponse
94101
// publish api endpoint latency data
95102
Map<String, Object> latencyData = generateLatencyDataMap(request, messageId);
96103
CDSDataPublishingService.getCDSDataPublishingService().publishApiLatencyData(latencyData);
104+
} else {
105+
LOG.debug("Data publishing is disabled or the request is not an external request. Infosec data " +
106+
"publishing skipped.");
97107
}
98108
}
99109

@@ -245,4 +255,15 @@ private String extractClientId(HttpServletRequest request) {
245255
public void destroy() {
246256
}
247257

258+
/**
259+
* Check whether data should be published for the current request.
260+
*
261+
* @return boolean
262+
*/
263+
public boolean shouldPublishCurrentRequestData(ServletRequest request) {
264+
265+
// If the request is internal traffic, no need to publish data
266+
return expectedExternalTrafficHeaderValue.equalsIgnoreCase(
267+
((HttpServletRequest) request).getHeader(externalTrafficHeaderName));
268+
}
248269
}

components/org.wso2.openbanking.cds.identity/src/main/java/org/wso2/openbanking/cds/identity/utils/CDSIdentityConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ public class CDSIdentityConstants {
4141
public static final String CODE_RESPONSE_TYPE = "code";
4242
public static final String JWT_RESPONSE_MODE = "jwt";
4343
public static final String UNSUPPORTED_RESPONSE_TYPE_ERROR = "unsupported_response_type";
44+
public static final String SESSION_DATA_KEY_PARAMETER = "sessionDataKey";
4445

4546
}

0 commit comments

Comments
 (0)