Skip to content

Commit

Permalink
Merge pull request #2366 from SuKSW/sandbox
Browse files Browse the repository at this point in the history
Fix sandbox endpoint invocation failure with apim
  • Loading branch information
Subhashinie Koshalya authored Nov 2, 2021
2 parents 7036ca8 + 6315582 commit e5a8f70
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 28 deletions.
3 changes: 2 additions & 1 deletion adapter/internal/eventhub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func retrieveSubscriptionDataFromChannel(response response) {
appKeyMappingList = newResponse.(*types.ApplicationKeyMappingList)
ResourceMap := make(map[string]*types.ApplicationKeyMapping)
for index, keyMapping := range appKeyMappingList.List {
ResourceMap[keyMapping.ApplicationUUID] = &appKeyMappingList.List[index]
applicationKeyMappingReference := keyMapping.ConsumerKey + ":" + keyMapping.KeyManager
ResourceMap[applicationKeyMappingReference] = &appKeyMappingList.List[index]
}
ApplicationKeyMappingMap = ResourceMap
xds.UpdateEnforcerApplicationKeyMappings(xds.MarshalKeyMappingMap(ApplicationKeyMappingMap))
Expand Down
22 changes: 12 additions & 10 deletions adapter/internal/messaging/notification_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ func handleAzureNotification() {
}
}

func processNotificationEvent (conf *config.Config, notification *msg.EventNotification) error {
func processNotificationEvent(conf *config.Config, notification *msg.EventNotification) error {
var eventType string
var decodedByte, err = base64.StdEncoding.DecodeString(notification.Event.PayloadData.Event)
if err != nil {
if _, ok := err.(base64.CorruptInputError); ok {
logger.LoggerInternalMsg.Error("\nbase64 input is corrupt, check the provided key")
}
logger.LoggerInternalMsg.Errorf("Error occurred while decoding the notification event %v. " +
logger.LoggerInternalMsg.Errorf("Error occurred while decoding the notification event %v. "+
"Hence dropping the event", err)
return err
}
Expand Down Expand Up @@ -274,19 +274,21 @@ func handleApplicationEvents(data []byte, eventType string) {
KeyManager: applicationRegistrationEvent.KeyManager, TenantID: -1, TenantDomain: applicationRegistrationEvent.TenantDomain,
TimeStamp: applicationRegistrationEvent.TimeStamp, ApplicationUUID: applicationRegistrationEvent.ApplicationUUID}

if isLaterEvent(applicationKeyMappingTimeStampMap, fmt.Sprint(applicationRegistrationEvent.ApplicationID),
applicationKeyMappingReference := applicationKeyMapping.ConsumerKey + ":" + applicationKeyMapping.KeyManager

if isLaterEvent(applicationKeyMappingTimeStampMap, fmt.Sprint(applicationKeyMappingReference),
applicationRegistrationEvent.TimeStamp) {
return
}

if strings.EqualFold(removeApplicationKeyMapping, eventType) {
delete(eh.ApplicationKeyMappingMap, applicationKeyMapping.ApplicationUUID)
logger.LoggerInternalMsg.Infof("Application Key Mapping for the applicationID %s is removed.",
applicationKeyMapping.ApplicationUUID)
delete(eh.ApplicationKeyMappingMap, applicationKeyMappingReference)
logger.LoggerInternalMsg.Infof("Application Key Mapping for the applicationKeyMappingReference %s is removed.",
applicationKeyMappingReference)
} else {
eh.ApplicationKeyMappingMap[applicationKeyMapping.ApplicationUUID] = &applicationKeyMapping
logger.LoggerInternalMsg.Infof("Application Key Mapping for the applicationID %s is added.",
applicationKeyMapping.ApplicationUUID)
eh.ApplicationKeyMappingMap[applicationKeyMappingReference] = &applicationKeyMapping
logger.LoggerInternalMsg.Infof("Application Key Mapping for the applicationKeyMappingReference %s is added.",
applicationKeyMappingReference)
}

xds.UpdateEnforcerApplicationKeyMappings(xds.MarshalKeyMappingMap(eh.ApplicationKeyMappingMap))
Expand Down Expand Up @@ -445,7 +447,7 @@ func belongsToTenant(tenantDomain string) bool {
func parseNotificationJSONEvent(data []byte, notification *msg.EventNotification) error {
unmarshalErr := json.Unmarshal(data, &notification)
if unmarshalErr != nil {
logger.LoggerInternalMsg.Errorf("Error occurred while unmarshalling " +
logger.LoggerInternalMsg.Errorf("Error occurred while unmarshalling "+
"notification event data %v. Hence dropping the event", unmarshalErr)
}
return unmarshalErr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String init(Api api) {
api.getEndpointSecurity().getProductionSecurityInfo()));
}
if (api.getEndpointSecurity().hasSandBoxSecurityInfo()) {
endpointSecurity.setProductionSecurityInfo(
endpointSecurity.setSandBoxSecurityInfo(
APIProcessUtils.convertProtoEndpointSecurity(
api.getEndpointSecurity().getSandBoxSecurityInfo()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public AuthenticationContext authenticate(RequestContext requestContext) throws
securityInfo = requestContext.getMatchedAPI().getEndpointSecurity().
getSandBoxSecurityInfo();
}
if (securityInfo.isEnabled() &&
if (securityInfo != null && securityInfo.isEnabled() &&
APIConstants.AUTHORIZATION_HEADER_BASIC.
equalsIgnoreCase(securityInfo.getSecurityType())) {
requestContext.getRemoveHeaders().remove(APIConstants.AUTHORIZATION_HEADER_DEFAULT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void initTracer() throws TracingException {

// Future tracer implementations can be initialized from here
if (StringUtils.isEmpty(exporterType)) {
logger.warn("Tracer exporter type not defined, defaulting to Azure Trace Exporter");
exporterType = TracingConstants.AZURE_TRACE_EXPORTER;
logger.warn("Tracer exporter type not defined, defaulting to Jaeger Trace Exporter");
exporterType = TracingConstants.JAEGER_TRACE_EXPORTER;
}
if (exporterType.equalsIgnoreCase(TracingConstants.AZURE_TRACE_EXPORTER)) {
this.tracer = AzureExporter.getInstance().initTracer(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ public static APIKeyDTO generateAPIKey(String applicationId, String keyType, Res

public static String generateUserAccessToken(String apimServiceURLHttps, String applicationId, User user,
RestAPIStoreImpl storeRestClient) throws CCTestException {
ApplicationKeyDTO applicationKeyDTO = StoreUtils.generateKeysForApp(applicationId, storeRestClient);
ApplicationKeyDTO applicationKeyDTO = StoreUtils.generateKeysForApp(applicationId,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION, storeRestClient);
Utils.delay(TestConstant.DEPLOYMENT_WAIT_TIME, "Interrupted while waiting for the " +
"Applications Registration event to be received by the CC");
return StoreUtils.generateUserAccessToken(apimServiceURLHttps,
applicationKeyDTO.getConsumerKey(), applicationKeyDTO.getConsumerSecret(),
new String[]{"PRODUCTION"}, user, storeRestClient);
}

public static String generateUserAccessTokenSandbox(String apimServiceURLHttps, String applicationId, User user,
RestAPIStoreImpl storeRestClient) throws CCTestException {
ApplicationKeyDTO applicationKeyDTO = StoreUtils.generateKeysForApp(applicationId,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.SANDBOX, storeRestClient);
Utils.delay(TestConstant.DEPLOYMENT_WAIT_TIME, "Interrupted while waiting for the " +
"Applications Registration event to be received by the CC");
return StoreUtils.generateUserAccessToken(apimServiceURLHttps,
applicationKeyDTO.getConsumerKey(), applicationKeyDTO.getConsumerSecret(),
new String[]{"SANDBOX"}, user, storeRestClient);
}

/**
* Generate the user access token for the grant type password.
*
Expand Down Expand Up @@ -140,7 +152,8 @@ public static String subscribeToAPI(String apiId, String applicationId, String t
public static AppWithConsumerKey createApplicationWithKeys(Application app, RestAPIStoreImpl storeRestClient)
throws CCTestException {
String applicationId = createApplication(app, storeRestClient);
ApplicationKeyDTO applicationKeyDTO = generateKeysForApp(applicationId, storeRestClient);
ApplicationKeyDTO applicationKeyDTO = generateKeysForApp(applicationId,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION, storeRestClient);
return new AppWithConsumerKey(applicationId, applicationKeyDTO.getConsumerKey(),
applicationKeyDTO.getConsumerSecret());
}
Expand Down Expand Up @@ -171,22 +184,23 @@ public static String createApplication(Application app, RestAPIStoreImpl storeRe
* @return an ApplicationKeyDTO object containing Consumer key and Secret
* @throws CCTestException if an error occurs while generating keys
*/
public static ApplicationKeyDTO generateKeysForApp(String appId, RestAPIStoreImpl storeRestClient) throws CCTestException {
public static ApplicationKeyDTO generateKeysForApp(String appId,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum keyType,
RestAPIStoreImpl storeRestClient) throws CCTestException {
ArrayList<String> grantTypes = new ArrayList<>();
grantTypes.add(APIMIntegrationConstants.GRANT_TYPE.PASSWORD);
grantTypes.add(APIMIntegrationConstants.GRANT_TYPE.CLIENT_CREDENTIAL);
ApplicationKeyDTO applicationKeyDTO;
try {
applicationKeyDTO = storeRestClient.generateKeys(appId,
TestConstant.DEFAULT_TOKEN_VALIDITY_TIME, "",
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION,
null, grantTypes);
TestConstant.DEFAULT_TOKEN_VALIDITY_TIME, "", keyType, null, grantTypes);
} catch (ApiException e) {
throw new CCTestException("Error while generating consumer keys from APIM Store", e);
}
return applicationKeyDTO;
}


public static String getSubscriptionInfoString(String apiId, String applicationId, String tier) {
return "API_Id:" + apiId + " Application_Id:" + applicationId + " Tier:" + tier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,64 @@

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.choreo.connect.mockbackend.ResponseConstants;
import org.wso2.choreo.connect.tests.apim.ApimBaseTest;
import org.wso2.choreo.connect.tests.apim.ApimResourceProcessor;
import org.wso2.choreo.connect.tests.apim.utils.StoreUtils;
import org.wso2.choreo.connect.tests.context.CCTestException;
import org.wso2.choreo.connect.tests.util.HttpResponse;
import org.wso2.choreo.connect.tests.util.HttpsClientRequest;
import org.wso2.choreo.connect.tests.util.TestConstant;
import org.wso2.choreo.connect.tests.util.Utils;

import java.util.HashMap;
import java.util.Map;

public class ExistingApiTestCase extends ApimBaseTest {
private static final String VHOST_API_ENDPOINT = "vhostApi1/1.0.0/pet/findByStatus";
private static final String API_NAME = "ExistingApi";
private static final String API_CONTEXT = "existing_api";
private static final String API_VERSION = "1.0.0";
private static final String APP_NAME = "ExistingApiApp";

@BeforeClass(alwaysRun = true, description = "initialize setup")
void setup() throws Exception {
super.initWithSuperTenant();
}

@Test
public void testExistingApiWithSubscriptions() throws CCTestException {
String applicationId = ApimResourceProcessor.applicationNameToId.get(VhostApimTestCase.APPLICATION_NAME);
public void testExistingApiWithProdKey() throws Exception {
String applicationId = ApimResourceProcessor.applicationNameToId.get(APP_NAME);
String accessToken = StoreUtils.generateUserAccessToken(apimServiceURLHttps, applicationId,
user, storeRestClient);

Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put(TestConstant.AUTHORIZATION_HEADER, "Bearer " + accessToken);
requestHeaders.put(HttpHeaderNames.HOST.toString(), "localhost");
VhostApimTestCase.testInvokeAPI(VHOST_API_ENDPOINT, requestHeaders, HttpStatus.SC_SUCCESS, ResponseConstants.RESPONSE_BODY);
Map<String, String> headers = new HashMap<>();
headers.put(TestConstant.AUTHORIZATION_HEADER, "Bearer " + accessToken);
headers.put(HttpHeaderNames.HOST.toString(), "localhost");

String endpoint = Utils.getServiceURLHttps(API_CONTEXT + "/1.0.0/pet/findByStatus");
HttpResponse response = HttpsClientRequest.retryGetRequestUntilDeployed(endpoint, headers);
Assert.assertNotNull(response, "Error occurred while invoking the endpoint " + endpoint + " HttpResponse ");
Assert.assertEquals(response.getResponseCode(), HttpStatus.SC_SUCCESS,
"Status code mismatched. Endpoint:" + endpoint + " HttpResponse ");
}

@Test
public void testExistingApiWithSandboxKey() throws Exception {
String applicationId = ApimResourceProcessor.applicationNameToId.get(APP_NAME);
String accessToken = StoreUtils.generateUserAccessTokenSandbox(apimServiceURLHttps, applicationId,
user, storeRestClient);

Map<String, String> headers = new HashMap<>();
headers.put(TestConstant.AUTHORIZATION_HEADER, "Bearer " + accessToken);
headers.put(HttpHeaderNames.HOST.toString(), "localhost");

String endpoint = Utils.getServiceURLHttps(API_CONTEXT + "/1.0.0/pet/findByStatus");
HttpResponse response = HttpsClientRequest.retryGetRequestUntilDeployed(endpoint, headers);
Assert.assertNotNull(response, "Error occurred while invoking the endpoint " + endpoint + " HttpResponse ");
Assert.assertEquals(response.getResponseCode(), HttpStatus.SC_SUCCESS,
"Status code mismatched. Endpoint:" + endpoint + " HttpResponse ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
}
]
},
{
"name": "ExistingApi",
"version": "1.0.0",
"context": "existing_api",
"operationsDTOS": [
{
"verb": "GET",
"target": "/pet/findByStatus",
"throttlingPolicy": "Unlimited"
}
]
},
{
"name": "SubscriptionValidationApi",
"version": "1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"appName": "BlockedApiApp",
"throttleTier": "Unlimited"
},
{
"appName": "ExistingApiApp",
"throttleTier": "Unlimited"
},
{
"appName": "VHostApp",
"throttleTier": "Unlimited"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"appName": "BlockedApiApp",
"tier": "Unlimited"
},
{
"apiName": "ExistingApi",
"appName": "ExistingApiApp",
"tier": "Unlimited"
},
{
"apiName": "VHostAPI1",
"appName": "VHostApp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<classes>
<class name="org.wso2.choreo.connect.tests.setup.withapim.ApimRestartExecutor"/>
<class name="org.wso2.choreo.connect.tests.testcases.withapim.BasicEventsTestCase"/>
<class name="org.wso2.choreo.connect.tests.testcases.withapim.ExistingApiTestCase"/>
<class name="org.wso2.choreo.connect.tests.testcases.withapim.APIKeyTestCase"/>
<class name="org.wso2.choreo.connect.tests.testcases.withapim.APIKeyQueryParamTestCase"/>
<class name="org.wso2.choreo.connect.tests.testcases.withapim.APIKeyAppLevelTestCase"/>
Expand Down

0 comments on commit e5a8f70

Please sign in to comment.