use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testGetResourceAuthenticationScheme.
@Test
public void testGetResourceAuthenticationScheme() {
MessageContext synCtx = Mockito.mock(Axis2MessageContext.class);
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY)).thenReturn(null);
Mockito.when(synCtx.getProperty(APIConstants.API_RESOURCE_CACHE_KEY)).thenReturn("abc");
Mockito.when(synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("abc");
Mockito.when(synCtx.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("");
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("https");
Mockito.when(((Axis2MessageContext) synCtx).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn(new API("abc", "/"));
Mockito.when(synCtx.getConfiguration()).thenReturn(synapseConfiguration);
Mockito.when(synCtx.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("https");
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), getDefaultVerbInfoDTO());
// test for ResourceNotFoundException path
try {
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
String result = apiKeyValidator.getResourceAuthenticationScheme(synCtx);
Assert.assertEquals("noMatchedAuthScheme", result);
} catch (APISecurityException e) {
e.printStackTrace();
}
APIKeyValidator apiKeyValidator1 = createAPIKeyValidator(false, getDefaultURITemplates("/menu", "GET"), getDefaultVerbInfoDTO());
Resource resource = Mockito.mock(Resource.class);
API api = new API("abc", "/");
Mockito.when(synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/menu");
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
api.addResource(resource);
Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn((api));
String result1 = null;
try {
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
Mockito.when(APIUtil.getAPIInfoDTOCacheKey("", "1.0")).thenReturn("abc");
result1 = apiKeyValidator1.getResourceAuthenticationScheme(synCtx);
} catch (APISecurityException e) {
e.printStackTrace();
}
Assert.assertEquals(StringUtils.capitalize(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN.toLowerCase()), result1);
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testFindMatchingVerb.
/*
* This method will test for findMatchingVerb()
* */
@Test
public void testFindMatchingVerb() {
MessageContext synCtx = Mockito.mock(Axis2MessageContext.class);
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY)).thenReturn(null);
Mockito.when(synCtx.getProperty(APIConstants.API_RESOURCE_CACHE_KEY)).thenReturn("abc");
Mockito.when(synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("abc");
Mockito.when(synCtx.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("");
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(((Axis2MessageContext) synCtx).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn(new API("abc", "/"));
Mockito.when(synCtx.getConfiguration()).thenReturn(synapseConfiguration);
Mockito.when(synCtx.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
VerbInfoDTO verbInfoDTO = getDefaultVerbInfoDTO();
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), verbInfoDTO);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.GATEWAY_RESOURCE_CACHE_ENABLED)).thenReturn("true");
try {
List<VerbInfoDTO> verbList = apiKeyValidator.findMatchingVerb(synCtx);
int length = verbList.toArray().length;
// Test for ResourceNotFoundexception
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
assertNotNull(verbList.get(0));
// todo Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY)).thenReturn("url");
} catch (ResourceNotFoundException e) {
assert true;
} catch (APISecurityException e) {
fail("APISecurityException is thrown " + e);
}
APIKeyValidator apiKeyValidator1 = createAPIKeyValidator(false, getDefaultURITemplates("/menu", "GET"), verbInfoDTO);
Resource resource = Mockito.mock(Resource.class);
API api = new API("abc", "/");
Mockito.when(synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/menu");
api.addResource(resource);
Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn((api));
try {
List<VerbInfoDTO> verbInfoList = new ArrayList<>();
verbInfoList.add(verbInfoDTO);
// Test for matching verb is found path
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
assertEquals("", verbInfoList, apiKeyValidator1.findMatchingVerb(synCtx));
} catch (ResourceNotFoundException e) {
fail("ResourceNotFoundException exception is thrown " + e);
} catch (APISecurityException e) {
fail("APISecurityException is thrown " + e);
}
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class APIManagerComponent method configureRecommendationEventPublisherProperties.
private void configureRecommendationEventPublisherProperties() {
OutputEventAdapterConfiguration adapterConfiguration = new OutputEventAdapterConfiguration();
adapterConfiguration.setName(APIConstants.RECOMMENDATIONS_WSO2_EVENT_PUBLISHER);
adapterConfiguration.setType(APIConstants.BLOCKING_EVENT_TYPE);
adapterConfiguration.setMessageFormat(APIConstants.BLOCKING_EVENT_FORMAT);
Map<String, String> adapterParameters = new HashMap<>();
if (ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService() != null) {
APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
if (configuration.getApiRecommendationEnvironment() != null) {
try {
String receiverPort = System.getProperty(configuration.RECEIVER_URL_PORT);
String authPort = System.getProperty(configuration.AUTH_URL_PORT);
adapterParameters.put(APIConstants.RECEIVER_URL, "tcp://localhost:" + receiverPort);
adapterParameters.put(APIConstants.AUTHENTICATOR_URL, "ssl://localhost:" + authPort);
adapterParameters.put(APIConstants.USERNAME, APIUtil.getAdminUsername());
adapterParameters.put(APIConstants.PASSWORD, APIUtil.getAdminPassword());
adapterParameters.put(APIConstants.PROTOCOL, "Binary");
adapterParameters.put(APIConstants.PUBLISHING_MODE, APIConstants.NON_BLOCKING);
adapterParameters.put(APIConstants.PUBLISHING_TIME_OUT, "0");
adapterConfiguration.setStaticProperties(adapterParameters);
ServiceReferenceHolder.getInstance().getOutputEventAdapterService().create(adapterConfiguration);
log.info("API Recommendation system for dev portal is activated");
} catch (OutputEventAdapterException e) {
log.error("Exception occurred while creating recommendationEventPublisher Adapter." + " Request Blocking may not work properly", e);
} catch (APIMgtInternalException e) {
log.error("Exception occurred while reading the admin username and password", e);
}
}
}
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class APIManagerComponent method configureNotificationEventPublisher.
/**
* Method to configure wso2event type event adapter to be used for event notification.
*/
private void configureNotificationEventPublisher() throws APIManagementException {
Map<String, String> properties = new HashMap<>();
if (ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService() != null) {
APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
if (configuration.getEventHubConfigurationDto().getEventHubPublisherConfiguration() != null && configuration.getEventHubConfigurationDto().isEnabled()) {
EventHubConfigurationDto eventHubConfigurationDto = configuration.getEventHubConfigurationDto();
EventHubConfigurationDto.EventHubPublisherConfiguration eventHubPublisherConfiguration = eventHubConfigurationDto.getEventHubPublisherConfiguration();
properties.put(APIConstants.RECEIVER_URL, eventHubPublisherConfiguration.getReceiverUrlGroup());
properties.put(APIConstants.AUTHENTICATOR_URL, eventHubPublisherConfiguration.getAuthUrlGroup());
properties.put(APIConstants.USERNAME, eventHubConfigurationDto.getUsername());
properties.put(APIConstants.PASSWORD, eventHubConfigurationDto.getPassword());
properties.put(APIConstants.PROTOCOL, eventHubPublisherConfiguration.getType());
properties.put(APIConstants.PUBLISHING_MODE, APIConstants.NON_BLOCKING);
properties.put(APIConstants.PUBLISHING_TIME_OUT, "0");
for (String key : eventHubPublisherConfiguration.getProperties().keySet()) {
properties.put(key, eventHubPublisherConfiguration.getProperties().get(key));
}
properties.put(APIConstants.IS_ENABLED, Boolean.toString(configuration.getEventHubConfigurationDto().isEnabled()));
try {
ServiceReferenceHolder.getInstance().getEventPublisherFactory().configure(properties);
} catch (EventPublisherException e) {
throw new APIManagementException(e);
}
} else {
log.info("Wso2Event Publisher not enabled.");
}
} else {
log.info("api-manager.xml not loaded. Wso2Event Publisher will not be enabled.");
}
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class KeyManagerConfigurationDataRetriever method run.
@Override
public void run() {
APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
if (apiManagerConfiguration != null) {
EventHubConfigurationDto eventHubConfigurationDto = apiManagerConfiguration.getEventHubConfigurationDto();
if (eventHubConfigurationDto != null && eventHubConfigurationDto.isEnabled()) {
try {
String url = eventHubConfigurationDto.getServiceUrl().concat(APIConstants.INTERNAL_WEB_APP_EP).concat("/keymanagers");
byte[] credentials = Base64.encodeBase64((eventHubConfigurationDto.getUsername() + ":" + eventHubConfigurationDto.getPassword()).getBytes());
HttpGet method = new HttpGet(url);
method.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
URL configUrl = new URL(url);
int port = configUrl.getPort();
String protocol = configUrl.getProtocol();
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
HttpResponse httpResponse = null;
int retryCount = 0;
boolean retry;
do {
try {
httpResponse = httpClient.execute(method);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
KeyManagerConfiguration[] keyManagerConfigurations = new Gson().fromJson(responseString, KeyManagerConfiguration[].class);
for (KeyManagerConfiguration keyManagerConfiguration : keyManagerConfigurations) {
if (keyManagerConfiguration.isEnabled()) {
try {
ServiceReferenceHolder.getInstance().getKeyManagerConfigurationService().addKeyManagerConfiguration(keyManagerConfiguration.getTenantDomain(), keyManagerConfiguration.getName(), keyManagerConfiguration.getType(), keyManagerConfiguration);
} catch (APIManagementException e) {
log.error("Error while configuring Key Manager " + keyManagerConfiguration.getName() + " in tenant " + keyManagerConfiguration.getTenantDomain(), e);
}
}
}
retry = false;
} else {
retry = true;
retryCount++;
}
} catch (IOException ex) {
retryCount++;
int maxRetries = 15;
if (retryCount < maxRetries) {
retry = true;
long retryTimeout = (long) Math.min(Math.pow(2, retryCount), 300);
log.warn("Failed retrieving Key Manager Configurations from remote " + "endpoint: " + ex.getMessage() + ". Retrying after " + retryTimeout + " seconds...");
Thread.sleep(retryTimeout * 1000);
} else {
throw ex;
}
}
} while (retry);
} catch (InterruptedException | IOException e) {
log.error("Error while retrieving key manager configurations", e);
}
}
}
}
Aggregations