Java Code Examples for org.wso2.carbon.apimgt.api.model.API#setEndpointSecured()

The following examples show how to use org.wso2.carbon.apimgt.api.model.API#setEndpointSecured() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIMappingUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Method to set Endpoint Security From APIDTO To API Model
 *
 * @param dto DTO model of the API
 * @param api API
 */
private static void setEndpointSecurityFromApiDTOToModel(APIDTO dto, API api) {
    APIEndpointSecurityDTO securityDTO = dto.getEndpointSecurity();
    if (dto.getEndpointSecurity() != null && securityDTO.getType() != null) {
        api.setEndpointSecured(true);
        api.setEndpointUTUsername(securityDTO.getUsername());
        api.setEndpointUTPassword(securityDTO.getPassword());
        if (APIEndpointSecurityDTO.TypeEnum.digest.equals(securityDTO.getType())) {
            api.setEndpointAuthDigest(true);
        }
    }
}
 
Example 2
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test public void testPublishingExistingRESTAPIToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {

    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(apiData);
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);

    //Test deleting existing API from production environment, if matching producntion endpoint configuration is
    // not found in API's endpoint config
    api.setEndpointConfig(sandBoxEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating 'INLINE' type REST API to gateway
    api.setImplementation("INLINE");
    api.setEndpointConfig(prodEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating 'ENDPOINT' type REST API to gateway
    api.setImplementation("ENDPOINT");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating default version of the API
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying API, if secure vault is enabled
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 3
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecurityConfigContext() throws Exception {

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointUTUsername("admin");
    api.setEndpointUTPassword("admin123");
    api.setEndpointSecured(true);
    api.setEndpointAuthDigest(true);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    for (Map.Entry<String, EndpointSecurityModel> endpointSecurityModelEntry : endpointSecurityModelMap
            .entrySet()) {
        Assert.assertTrue("Property isEndpointSecured cannot be false.",
                endpointSecurityModelEntry.getValue().isEnabled());
        Assert.assertTrue("Property isEndpointAuthDigest cannot be false.",
                endpointSecurityModelEntry.getValue().getType().contains("digest"));
        Assert.assertTrue("Property username does not match.",
                "admin".equals(endpointSecurityModelEntry.getValue().getUsername()));
        Assert.assertTrue("Property base64unpw does not match. ",
                new String(Base64.encodeBase64("admin:admin123".getBytes()))
                        .equalsIgnoreCase(endpointSecurityModelEntry.getValue().getBase64EncodedPassword()));
        Assert.assertTrue("Property securevault_alias does not match.",
                "admin--TestAPI1.0.0".equalsIgnoreCase(endpointSecurityModelEntry.getValue().getAlias()));
    }
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
}
 
Example 4
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test public void testPublishingNewAPIToGateway()
        throws Exception {

    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setAsDefaultVersion(true);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);

    //Test when environments are not defined for API
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test adding LocalEntry
    api.setEnvironments(environments);
    api.setEndpointConfig(sandBoxEndpointConfig);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);

    //Test when API's environment endpoint configuration is not available
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying 'INLINE' type REST API to gateway
    api.setImplementation("INLINE");
    api.setEndpointConfig(prodEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying 'ENDPOINT' type REST API to gateway
    api.setImplementation("ENDPOINT");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying default version of the API and updating the existing default API
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying API, if secure vault is enabled
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test publishing WebSocket API
    api.setType("WS");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

}
 
Example 5
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test public void testFailureWhilePublishingNewAPIToGateway() throws Exception {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setInSequence(inSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    OMElement inSequence = AXIOMUtil.stringToOM(testSequenceDefinition);
    OMElement faultSequence = AXIOMUtil.stringToOM(testSequenceDefinition);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);
    PowerMockito.when(APIUtil.getCustomSequence(inSequenceName, tenantID, "in", api.getId()))
            .thenReturn(inSequence);
    PowerMockito.when(APIUtil.isProductionEndpointsExists(Mockito.anyString())).thenReturn(true);
    PowerMockito.when(APIUtil.isSequenceDefined(Mockito.anyString())).thenReturn(true);

    Map<String, String> failedEnvironmentsMap = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap.keySet().contains(prodEnvironmentName));

    //Test failure to deploy API when setting secure vault property failed
    Mockito.doThrow(new APIManagementException("Failed to set secure vault property for the tenant"))
            .when(apiGatewayAdminClient).setSecureVaultProperty(api, tenantDomain);
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Mockito.when(config.getFirstProperty(APIConstants.ENABLE_MTLS_FOR_APIS)).thenReturn("true");
    Map<String, String> failedEnvironmentsMap2 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap2.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap2.keySet().contains(prodEnvironmentName ));

    //Test failure to deploy API when fault sequence deployment failed
    api.setFaultSequence(faultSequenceName);
    PowerMockito.when(APIUtil.getCustomSequence(faultSequenceName, tenantID, "fault", api.getId()))
            .thenReturn(faultSequence);
    Map<String, String> failedEnvironmentsMap3 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap3.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap3.keySet().contains(prodEnvironmentName));

    //Test throwing AxisFault when Gateway client initialisation failed
    Mockito.doThrow(new AxisFault("Error occurred while deploying sequence")).when(apiGatewayAdminClient)
            .getApi(tenantDomain, apiIdentifier);
    Map<String, String> failedEnvironmentsMap4 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap4.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap4.keySet().contains(prodEnvironmentName));
}
 
Example 6
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecurityConfigContextIgnoringEndpointConfig() throws Exception {

    String json = "{\"endpoint_security\":{\n" +
            "  \"sandbox\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"DIGEST\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123#QA\"\n" +
            "  }\n" +
            "  }\n" +
            "}";

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    api.setEndpointUTUsername("admin");
    api.setEndpointUTPassword("admin123");
    api.setEndpointSecured(true);
    api.setEndpointAuthDigest(true);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    for (Map.Entry<String, EndpointSecurityModel> endpointSecurityModelEntry : endpointSecurityModelMap
            .entrySet()) {
        Assert.assertTrue("Property isEndpointSecured cannot be false.",
                endpointSecurityModelEntry.getValue().isEnabled());
        Assert.assertTrue("Property isEndpointAuthDigest cannot be false.",
                endpointSecurityModelEntry.getValue().getType().contains("digest"));
        Assert.assertTrue("Property username does not match.",
                "admin".equals(endpointSecurityModelEntry.getValue().getUsername()));
        Assert.assertTrue("Property base64unpw does not match. ",
                new String(Base64.encodeBase64("admin:admin123".getBytes()))
                        .equalsIgnoreCase(endpointSecurityModelEntry.getValue().getBase64EncodedPassword()));
        Assert.assertTrue("Property securevault_alias does not match.",
                "admin--TestAPI1.0.0".equalsIgnoreCase(endpointSecurityModelEntry.getValue().getAlias()));
    }
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
}