Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Dec 14, 2023
1 parent d13f6cd commit 3607e88
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@
package org.wso2.apk.enforcer.jwt;

import com.google.common.cache.LoadingCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.wso2.apk.enforcer.common.CacheProvider;
Expand Down Expand Up @@ -61,6 +53,12 @@
import org.wso2.apk.enforcer.subscription.SubscriptionDataHolder;
import org.wso2.apk.enforcer.subscription.SubscriptionDataStore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;

public class JWTValidatorTest {

@Before
Expand Down Expand Up @@ -579,7 +577,7 @@ public void testNoCacheExpiredJWTValidator() throws EnforcerException {
@Test
public void testTamperedPayloadJWTValidator() throws EnforcerException {

String organization = "org1";
String organization = "org2";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "sBgeoqJn0log5EZflj_G7ADvm6B3KQ9bdfFCEFVQS1U3oY9" +
Expand All @@ -589,28 +587,30 @@ public void testTamperedPayloadJWTValidator() throws EnforcerException {
"EuSe9w";
String jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UZG1aak00WkRrM05qWTBZemM1T" +
"W1abU9EZ3dNVEUzTVdZd05ERTVNV1JsWkRnNE56YzRaQT09In0" +
".eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV" +
"3YXkiLCJzdWIiOiJhZG1pbk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IiwKICAic3Vic2NyaWJlZEFQSXM" +
"iOiBbXSwKICAiY29uc3VtZXJLZXkiOiAiWGdPMzk2SUhGSzdlRll5ZHJxUWU0SEtHejFrYSIsCiAgImV4cCI6IDQxMz" +
"IzODM0NzcsCiAgImlhdCI6IDE1OTAzMzg3MTMsCiAgImp0aSI6ICJiODkzODc2OC0yM2ZkLTRkZWMtOGI3MC1iZWQ0N" +
"WViN2MzM2QiCn0=." + signature;
String tamperedJWT = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UZG1aak00WkRrM05qWTBZemM1T" +
"W1abU9EZ3dNVEUzTVdZd05ERTVNV1JsWkRnNE56YzRaQT09In0" +
".ewogICJhdWQiOiAiaHR0cDovL29yZy53c28yLmFwaW1ndC9nYXRld2F5IiwKICAic3ViIjogImFkbWluQGNhcm" +
"Jvbi5zdXBlciIsCiAgImFwcGxpY2F0aW9uIjogewogICAgIm93bmVyIjogImFkbWluIiwKICAgICJ0aWVyUXVvd" +
"GFUeXBlIjogInJlcXVlc3RDb3VudCIsCiAgICAidGllciI6ICJVbmxpbWl0ZWQiLAogICAgIm5hbWUiOiAiRGVm" +
"YXVsdEFwcGxpY2F0aW9uMiIsCiAgICAiaWQiOiAyLAogICAgInV1aWQiOiBudWxsCiAgfSwKICAic2NvcGUiOiA" +
"iYW1fYXBwbGljYXRpb25fc2NvcGUgZGVmYXVsdCIsCiAgImlzcyI6ICJodHRwczovL2xvY2FsaG9zdDo5NDQzL2" +
"9hdXRoMi90b2tlbiIsCiAgInRpZXJJbmZvIjoge30sCiAgImtleXR5cGUiOiAiUFJPRFVDVElPTiIsCiAgInN1Y" +
"nNjcmliZWRBUElzIjogW10sCiAgImNvbnN1bWVyS2V5IjogIlhnTzM5NklIRks3ZUZZeWRycVFlNEhLR3oxa2Ei" +
"LAogICJleHAiOiAxNTkwMzQyMzEzLAogICJpYXQiOiAxNTkwMzM4NzEzLAogICJqdGkiOiAiYjg5Mzg3NjgtMjN" +
"mZC00ZGVjLThiNzAtYmVkNDVlYjdjMzNkIgp9." + signature;
".ewogICJhdWQiOiAiaHR0cDovL29yZy53c28yLmFwaW1ndC9nYXRld2F5IiwKICAic3ViIjogImFkbWluQGNhcmJvbi5" +
"zdXBlciIsCiAgImFwcGxpY2F0aW9uIjogewogICAgIm93bmVyIjogImFkbWluIiwKICAgICJ0aWVyUXVvdGFUeXBlIjo" +
"gInJlcXVlc3RDb3VudCIsCiAgICAidGllciI6ICJVbmxpbWl0ZWQiLAogICAgIm5hbWUiOiAiRGVmYXVsdEFwcGxpY2F" +
"0aW9uMiIsCiAgICAiaWQiOiAyLAogICAgInV1aWQiOiBudWxsCiAgfSwKICAic2NvcGUiOiAiYW1fYXBwbGljYXRpb25" +
"fc2NvcGUgZGVmYXVsdCIsCiAgImlzcyI6ICJodHRwczovL2xvY2FsaG9zdDo5NDQzL29hdXRoMi90b2tlbiIsCiAgInR" +
"pZXJJbmZvIjoge30sCiAgImtleXR5cGUiOiAiUFJPRFVDVElPTiIsCiAgInN1YnNjcmliZWRBUElzIjogW10sCiAgImN" +
"vbnN1bWVyS2V5IjogIlhnTzM5NklIRks3ZUZZeWRycVFlNEhLR3oxa2EiLAogICJleHAiOiA0MTMyMzgzNDc3LAogICJ" +
"pYXQiOiAxNTkwMzM4NzEzLAogICJqdGkiOiAiYjg5Mzg3NjgtMjNmZC00ZGVjLThiNzAtYmVkNDVlYjdjMzNkIgp9." +
signature;

JWTValidationInfo jwtValidationInfo = new JWTValidationInfo();
jwtValidationInfo.setValid(false);
jwtValidationInfo.setExpiryTime(System.currentTimeMillis() - 100);
jwtValidationInfo.setExpiryTime(System.currentTimeMillis() + 120000);
jwtValidationInfo.setConsumerKey(UUID.randomUUID().toString());
jwtValidationInfo.setUser("user1");
jwtValidationInfo.setKeyManager("Default");
Expand Down Expand Up @@ -641,9 +641,17 @@ public void testTamperedPayloadJWTValidator() throws EnforcerException {
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
try (MockedStatic<CacheProviderUtil> cacheProviderUtilDummy = Mockito.mockStatic(CacheProviderUtil.class);
MockedStatic<KeyValidator> keyValidatorDummy = Mockito.mockStatic(KeyValidator.class)
MockedStatic<KeyValidator> keyValidatorDummy = Mockito.mockStatic(KeyValidator.class);
MockedStatic<SubscriptionDataHolder> subscriptionDataHolderMockedStatic =
Mockito.mockStatic(SubscriptionDataHolder.class);
) {
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
subscriptionDataHolderMockedStatic.when(SubscriptionDataHolder::getInstance).thenReturn(subscriptionDataHolder);
Mockito.when(subscriptionDataHolder.getSubscriptionDataStore(organization)).thenReturn(subscriptionDataStore);
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization)).
thenReturn(cacheProvider);
LoadingCache gatewayKeyCache = Mockito.mock(LoadingCache.class);
LoadingCache invalidTokenCache = Mockito.mock(LoadingCache.class);
Mockito.when(gatewayKeyCache.getIfPresent(signature)).thenReturn(jwtValidationInfo);
Expand All @@ -652,8 +660,8 @@ public void testTamperedPayloadJWTValidator() throws EnforcerException {
Mockito.when(cacheProvider.getInvalidTokenCache()).thenReturn(invalidTokenCache);
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization))
.thenReturn(cacheProvider);

JWTValidator jwtValidator = Mockito.mock(JWTValidator.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer, environment)).thenReturn(jwtValidator);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
keyValidatorDummy.when(() -> KeyValidator.validateScopes(Mockito.any())).thenReturn(true);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ spec:
checksum/config: {{ include (print $.Template.BasePath "/data-plane/gateway-components/log-conf.yaml") . | sha256sum }}
spec:
automountServiceAccountToken: false
initContainers:
- name: check-adapter
image: busybox:1.32
env:
- name: ADAPTER_HOST
value: {{ template "apk-helm.resource.prefix" . }}-adapter-service.{{ .Release.Namespace }}.svc
- name: ADAPTER_XDS_PORT
value : "18000"
command: ['sh', '-c', 'echo -e "Checking for the availability of Adapter deployment"; while ! nc -z $ADAPTER_HOST $ADAPTER_XDS_PORT; do sleep 1; printf "-"; done; echo -e " >> Adapter has started";']
- name: check-common-controller
image: busybox:1.32
env:
- name: COMMON_CONTROLLER_HOST_NAME
value: {{ template "apk-helm.resource.prefix" . }}-common-controller-service.{{ .Release.Namespace }}.svc
- name: COMMON_CONTROLLER_XDS_PORT
value: "18002"
command: ['sh', '-c', 'echo -e "Checking for the availability of common-controller deployment"; while ! nc -z $COMMON_CONTROLLER_HOST_NAME $COMMON_CONTROLLER_XDS_PORT; do sleep 1; printf "-"; done; echo -e " >> common-controller has started";']
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
readOnlyRootFilesystem: true
containers:
- name: enforcer
image: {{ .Values.wso2.apk.dp.gatewayRuntime.deployment.enforcer.image }}
Expand Down

0 comments on commit 3607e88

Please sign in to comment.