Search in sources :

Example 11 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class SAMLGroupIDExtractorImplTest method getGroupingIdentifierListTestCase.

@Test
public void getGroupingIdentifierListTestCase() throws ParserConfigurationException, IOException, SAXException, UnmarshallingException, UserStoreException {
    String claim = "http://wso2.org/claims/organization";
    String organizationValue = "organization";
    SAMLGroupIDExtractorImpl samlGroupIDExtractor = new SAMLGroupIDExtractorImplWrapper();
    Mockito.when(DocumentBuilderFactory.newInstance()).thenReturn(documentBuilderFactory);
    Mockito.when(documentBuilderFactory.newDocumentBuilder()).thenReturn(documentBuilder);
    Mockito.when(documentBuilder.parse(samlGroupIDExtractor.getByteArrayInputStream("test"))).thenReturn(document);
    Mockito.when(document.getDocumentElement()).thenReturn(element);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(XMLObjectProviderRegistrySupport.class);
    Response response = Mockito.mock(Response.class);
    List<Assertion> assertion = new ArrayList();
    Subject subject = Mockito.mock(Subject.class);
    NameID nameID = Mockito.mock(NameID.class);
    Assertion assertion1 = Mockito.mock(Assertion.class);
    assertion.add(assertion1);
    Mockito.when(XMLObjectProviderRegistrySupport.getUnmarshallerFactory()).thenReturn(unmarshallerFactory);
    Mockito.when(unmarshallerFactory.getUnmarshaller(element)).thenReturn(unmarshaller);
    Mockito.when(unmarshaller.unmarshall(element)).thenReturn(response);
    Mockito.when(response.getAssertions()).thenReturn(assertion);
    Mockito.when(assertion.get(0).getSubject()).thenReturn(subject);
    Mockito.when(subject.getNameID()).thenReturn(nameID);
    Mockito.when(nameID.getValue()).thenReturn("user");
    System.setProperty(APIConstants.READ_ORGANIZATION_FROM_SAML_ASSERTION, "true");
    APIManagerConfigurationService apiManagerConfigService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigService);
    APIManagerConfiguration apiManagerConfig = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(apiManagerConfigService.getAPIManagerConfiguration()).thenReturn(apiManagerConfig);
    Mockito.when(apiManagerConfig.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI)).thenReturn("http://wso2.org/claims/organization");
    System.setProperty("carbon.home", "");
    PrivilegedCarbonContext carbonContext;
    carbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()).thenReturn(-1234);
    PowerMockito.doNothing().when(carbonContext).setTenantDomain("carbon.super", true);
    AttributeStatement mockAttributeStatement = PowerMockito.mock(AttributeStatement.class);
    List<AttributeStatement> attributeStatementList = Collections.singletonList(mockAttributeStatement);
    PowerMockito.when(assertion1.getAttributeStatements()).thenReturn(attributeStatementList);
    Attribute mockAttribute = PowerMockito.mock(Attribute.class);
    List<Attribute> attributesList = Collections.singletonList(mockAttribute);
    PowerMockito.when(mockAttributeStatement.getAttributes()).thenReturn(attributesList);
    XMLObject rawAttribute = PowerMockito.mock(XMLObject.class);
    PowerMockito.when(rawAttribute.toString()).thenReturn(organizationValue);
    List<XMLObject> mockedAttributeValues = Collections.singletonList(rawAttribute);
    AttributedStringImpl mockedAttributedStringImpl = new AttributedStringImpl("nameSpaceURI", "elementLocalName", "namespacePrefix");
    String sampleAttrValue = "MockedAuthParamSampleAttribute";
    mockedAttributedStringImpl.setValue(sampleAttrValue);
    List<XMLObject> mockedXSSAttributeValues = Collections.singletonList((XMLObject) mockedAttributedStringImpl);
    XSAnyImpl mockedXSAnyImpl = Mockito.mock(XSAnyImpl.class);
    PowerMockito.when(mockedXSAnyImpl.getTextContent()).thenReturn(sampleAttrValue);
    List<XMLObject> mockedXSAnyImplAttributeValues = Collections.singletonList((XMLObject) mockedXSAnyImpl);
    List<XMLObject> multiMockedAttributeValues = Arrays.asList(rawAttribute, PowerMockito.mock(XMLObject.class));
    AuthenticatorsConfiguration.AuthenticatorConfig mockedAuthenticatorConfig = Mockito.mock(AuthenticatorsConfiguration.AuthenticatorConfig.class);
    PowerMockito.when(mockAttribute.getAttributeValues()).thenReturn(mockedAttributeValues, multiMockedAttributeValues, mockedXSSAttributeValues, mockedXSAnyImplAttributeValues);
    PowerMockito.mockStatic(AuthenticatorsConfiguration.class);
    AuthenticatorsConfiguration mockedAuthenticatorsConfiguration = PowerMockito.mock(AuthenticatorsConfiguration.class);
    PowerMockito.when(AuthenticatorsConfiguration.getInstance()).thenReturn(mockedAuthenticatorsConfiguration);
    Map<String, String> mockedConfigParameters = new HashMap<String, String>();
    mockedConfigParameters.put(APIConstants.ORGANIZATION_CLAIM_ATTRIBUTE, claim);
    PowerMockito.when(mockedAuthenticatorConfig.getParameters()).thenReturn(mockedConfigParameters);
    PowerMockito.when(mockedAuthenticatorsConfiguration.getAuthenticatorConfig(APIConstants.SAML2_SSO_AUTHENTICATOR_NAME)).thenReturn(mockedAuthenticatorConfig);
    PowerMockito.when(mockAttribute.getName()).thenReturn(claim);
    String[] organizations = samlGroupIDExtractor.getGroupingIdentifierList("test");
    Assert.assertEquals(organizationValue, organizations[0]);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Attribute(org.opensaml.saml.saml2.core.Attribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XSAnyImpl(org.opensaml.core.xml.schema.impl.XSAnyImpl) AttributedStringImpl(org.opensaml.soap.wssecurity.impl.AttributedStringImpl) NameID(org.opensaml.saml.saml2.core.NameID) AuthenticatorsConfiguration(org.wso2.carbon.core.security.AuthenticatorsConfiguration) Assertion(org.opensaml.saml.saml2.core.Assertion) XMLObject(org.opensaml.core.xml.XMLObject) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Subject(org.opensaml.saml.saml2.core.Subject) Response(org.opensaml.saml.saml2.core.Response) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class UserAwareAPIConsumerWrapper method getAPIManagerConfiguration.

/**
 * Returns API manager configurations.
 *
 * @return APIManagerConfiguration object
 */
protected APIManagerConfiguration getAPIManagerConfiguration() {
    APIManagerConfiguration apiManagerConfiguration = new APIManagerConfiguration();
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("amConfig.xml").getFile());
    try {
        apiManagerConfiguration.load(file.getPath().toString());
    } catch (APIManagementException e) {
        log.error("Error while reading configs from file api-manager.xml", e);
    }
    return apiManagerConfiguration;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) File(java.io.File)

Example 13 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class APIManagerConfigurationTest method testEnvironmentsConfigWithAdditionalProperties.

@Test
public void testEnvironmentsConfigWithAdditionalProperties() throws XMLStreamException, APIManagementException {
    String envConfig = "<Environment type=\"hybrid\" api-console=\"true\" isDefault=\"true\">\n" + "                <Name>Default</Name>\n" + "                <DisplayName></DisplayName>\n" + "                <Description>This is a hybrid gateway that handles both production and sandbox token traffic.</Description>\n" + "                <!-- Server URL of the API gateway -->\n" + "                <ServerURL>https://localhost:9440/services/</ServerURL>\n" + "                <!-- Admin username for the API gateway. -->\n" + "                <Username>${admin.username}</Username>\n" + "                <!-- Admin password for the API gateway.-->\n" + "                <Password>${admin.password}</Password>\n" + "                <!-- Provider Vendor of the API gateway.-->\n" + "                <Provider>wso2</Provider>\n" + "                <!-- Endpoint URLs for the APIs hosted in this API gateway.-->\n" + "                <GatewayEndpoint>https://localhost:9440,http://localhost:9440</GatewayEndpoint>\n" + "                <!-- Additional properties for External Gateways -->\n" + "                <!-- Endpoint URLs of the WebSocket APIs hosted in this API Gateway -->\n" + "                <GatewayWSEndpoint>ws://localhost:9099,wss://localhost:8099</GatewayWSEndpoint>\n" + "                <!-- Endpoint URLs of the WebSub APIs hosted in this API Gateway -->\n" + "                <GatewayWebSubEndpoint>http://localhost:9021,https://localhost:8021</GatewayWebSubEndpoint>\n" + "                <Properties>\n" + "                    <Property name=\"Organization\">WSO2</Property>\n" + "                    <Property name=\"DisplayName\">Development Environment</Property>\n" + "                    <Property name=\"DevAccountName\">dev-1</Property>\n" + "                </Properties>\n" + "                <VirtualHosts>\n" + "                </VirtualHosts>\n" + "            </Environment>";
    OMElement element = AXIOMUtil.stringToOM(envConfig);
    APIManagerConfiguration config = new APIManagerConfiguration();
    config.setEnvironmentConfig(element);
    Map<String, Environment> environmentsList = config.getGatewayEnvironments();
    Assert.assertFalse(environmentsList.isEmpty());
    Environment defaultEnv = environmentsList.get("Default");
    Assert.assertFalse(defaultEnv.getAdditionalProperties().isEmpty());
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) OMElement(org.apache.axiom.om.OMElement) Test(org.junit.Test)

Example 14 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class TestUtils method mockAPIMConfiguration.

public static void mockAPIMConfiguration() throws RegistryException, UserStoreException, XMLStreamException {
    ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    Map<String, Environment> apiGatewayEnvironments = new HashMap<String, Environment>();
    Environment env1 = new Environment();
    env1.setApiGatewayEndpoint("https://abc.com, http://abc.com");
    apiGatewayEnvironments.put("PROD", env1);
    // Mocking some commonly used configs
    PowerMockito.when(amConfig.getApiGatewayEnvironments()).thenReturn(apiGatewayEnvironments);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_API_DOC_VISIBILITY_LEVELS)).thenReturn("true", "false");
    ThrottleProperties throttleProperties = new ThrottleProperties();
    PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HashMap(java.util.HashMap) Environment(org.wso2.carbon.apimgt.api.model.Environment) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 15 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class GatewayUtils method isGatewayTokenCacheEnabled.

public static boolean isGatewayTokenCacheEnabled() {
    try {
        APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration();
        String cacheEnabled = config.getFirstProperty(APIConstants.GATEWAY_TOKEN_CACHE_ENABLED);
        return Boolean.parseBoolean(cacheEnabled);
    } catch (Exception e) {
        log.error("Did not found valid API Validation Information cache configuration. " + "Use default configuration.", e);
    }
    return true;
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) JOSEException(com.nimbusds.jose.JOSEException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) BadJWTException(com.nimbusds.jwt.proc.BadJWTException) IOException(java.io.IOException) APISecurityException(org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)122 Test (org.junit.Test)76 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)67 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)51 HashMap (java.util.HashMap)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)36 RealmService (org.wso2.carbon.user.core.service.RealmService)33 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)31 ArrayList (java.util.ArrayList)26 API (org.wso2.carbon.apimgt.api.model.API)25 Before (org.junit.Before)24 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)24 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)24 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)23 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)20 Cache (javax.cache.Cache)19 Map (java.util.Map)18