Search in sources :

Example 66 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method subscribeForBotDetectionAlerts.

/**
 * Subscribe for bot detection alerts
 *
 * @param body           email to be registered for the subscription
 * @param messageContext CXF Message Context
 * @return alert subscription DTO containing the uuid of the subscription and the registered email
 * @throws APIManagementException if an error occurs when subscribing for bot detection alerts
 */
@Override
public Response subscribeForBotDetectionAlerts(BotDetectionAlertSubscriptionDTO body, MessageContext messageContext) throws APIManagementException {
    String email = body.getEmail();
    if (StringUtils.isBlank(email)) {
        String propertyName = AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD;
        throw new APIManagementException(propertyName + " property value of payload cannot be blank", ExceptionCodes.from(ExceptionCodes.BLANK_PROPERTY_VALUE, propertyName));
    }
    APIAdmin apiAdmin = new APIAdminImpl();
    BotDetectionData alertSubscription = apiAdmin.getBotDetectionAlertSubscription(AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD, email);
    if (alertSubscription != null) {
        RestApiUtil.handleResourceAlreadyExistsError("Email: " + email + " has already been subscribed for bot detection alerts", log);
    }
    apiAdmin.addBotDetectionAlertSubscription(email);
    BotDetectionData newAlertSubscription = apiAdmin.getBotDetectionAlertSubscription(AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD, email);
    BotDetectionAlertSubscriptionDTO alertSubscriptionDTO = BotDetectionMappingUtil.fromAlertSubscriptionToDTO(newAlertSubscription);
    return Response.ok(alertSubscriptionDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) BotDetectionAlertSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionDTO)

Example 67 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class RegistrySearchUtilTestCase method testDevPortalUserQueryInDevPortal.

@Test
public void testDevPortalUserQueryInDevPortal() throws APIPersistenceException {
    // Normal dev portal api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("devUser", organization, null, devPortalRoles);
    String searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    String expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&name=*" + "&enableStore=(true OR null)&group=true&group.field=name&group.ngroups=true&group.sort=versionTimestamp desc" + "&lcState=(PUBLISHED OR PROTOTYPED)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&" + "description=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
    // search for provider 'pubuser'
    inputQuery = "provider:pubuser";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&" + "provider=*pubuser*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
    // search for propertyname 'test'
    inputQuery = "property_name:test";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)" + "&api_meta.property_name__display=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 68 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class RegistrySearchUtilTestCase method testAnonymousUserQueryInDevPortal.

@Test
public void testAnonymousUserQueryInDevPortal() throws APIPersistenceException {
    // Normal dev portal api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("wso2.anonymous.user", organization, null, anonymousRoles);
    String searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    String expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&name=*&enableStore=(true OR null)" + "&group=true&group.field=name&group.ngroups=true&group.sort=versionTimestamp desc&lcState=(PUBLISHED OR PROTOTYPED)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "description=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
    // search for provider 'pubuser'
    inputQuery = "provider:pubuser";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "provider=*pubuser*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
    // search for propertyname 'test'
    inputQuery = "property_name:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)" + "&api_meta.property_name__display=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 69 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class EntitlementClientUtils method loadConfigProperties.

/**
 * reads values from config property file
 *
 * @throws APIManagementException throws, if fails
 */
public static void loadConfigProperties() throws APIManagementException {
    Properties properties = new Properties();
    InputStream inputStream = null;
    try {
        File file = new File((new File(".")).getCanonicalPath() + File.separator + "resources" + File.separator + "config.properties");
        if (file.exists()) {
            inputStream = new FileInputStream(file);
        } else {
            String msg = "File does not exist : " + "config.properties";
            logger.error(msg);
        }
        try {
            if (inputStream != null) {
                properties.load(inputStream);
                configProperties = properties;
            }
        } catch (IOException e) {
            String msg = "Error loading properties from config.properties file";
            logger.error(msg, e);
            throw new APIManagementException(msg, e);
        }
    } catch (FileNotFoundException e) {
        String msg = "File can not be found : " + "config.properties";
        logger.error(msg, e);
        throw new APIManagementException(msg, e);
    } catch (IOException e) {
        String msg = "Can not create the canonical file path for given file : " + "config.properties";
        logger.error(msg, e);
        throw new APIManagementException(msg, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Properties(java.util.Properties)

Example 70 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class RestApiUtil method handleMigrationSpecificPermissionViolations.

/**
 * Handle if any cross tenant access permission violations detected. Cross tenant resources (apis/apps) can be
 * retrieved only by super tenant admin user, only while a migration process(2.6.0 to 3.0.0). APIM server has to be
 * started with the system property 'migrationMode=true' if a migration related exports are to be done.
 *
 * @param targetTenantDomain Tenant domain of which resources are requested
 * @param username           Logged in user name
 * @throws ForbiddenException
 */
public static void handleMigrationSpecificPermissionViolations(String targetTenantDomain, String username) throws ForbiddenException {
    boolean isCrossTenantAccess = !targetTenantDomain.equals(MultitenantUtils.getTenantDomain(username));
    if (!isCrossTenantAccess) {
        return;
    }
    String superAdminRole = null;
    try {
        superAdminRole = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getRealmConfiguration().getAdminRoleName();
    } catch (UserStoreException e) {
        RestApiUtil.handleInternalServerError("Error in getting super admin role name", e, log);
    }
    // check whether logged in user is a super tenant user
    String superTenantDomain = null;
    try {
        superTenantDomain = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getSuperTenantDomain();
    } catch (UserStoreException e) {
        RestApiUtil.handleInternalServerError("Error in getting the super tenant domain", e, log);
    }
    boolean isSuperTenantUser = RestApiCommonUtil.getLoggedInUserTenantDomain().equals(superTenantDomain);
    if (!isSuperTenantUser) {
        String errorMsg = "Cross Tenant resource access is not allowed for this request. User " + username + " is not allowed to access resources in " + targetTenantDomain + " as the requester is not a super " + "tenant user";
        log.error(errorMsg);
        ErrorDTO errorDTO = getErrorDTO(RestApiConstants.STATUS_FORBIDDEN_MESSAGE_DEFAULT, 403l, errorMsg);
        throw new ForbiddenException(errorDTO);
    }
    // check whether the user has super tenant admin role
    boolean isSuperAdminRoleNameExist = false;
    try {
        isSuperAdminRoleNameExist = APIUtil.isUserInRole(username, superAdminRole);
    } catch (UserStoreException | APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error in checking whether the user has admin role", e, log);
    }
    if (!isSuperAdminRoleNameExist) {
        String errorMsg = "Cross Tenant resource access is not allowed for this request. User " + username + " is not allowed to access resources in " + targetTenantDomain + " as the requester is not a " + "super tenant admin";
        log.error(errorMsg);
        ErrorDTO errorDTO = getErrorDTO(RestApiConstants.STATUS_FORBIDDEN_MESSAGE_DEFAULT, 403l, errorMsg);
        throw new ForbiddenException(errorDTO);
    }
}
Also used : ForbiddenException(org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Aggregations

HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)32 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 Resource (org.wso2.carbon.registry.core.Resource)23 Map (java.util.Map)21 Test (org.junit.Test)21 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)21 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)17 API (org.wso2.carbon.apimgt.api.model.API)16 UserStoreException (org.wso2.carbon.user.api.UserStoreException)16 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)14 JSONObject (org.json.simple.JSONObject)14 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)14 List (java.util.List)13 IOException (java.io.IOException)11 QName (javax.xml.namespace.QName)11 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)11 Properties (java.util.Properties)10 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)10