Search in sources :

Example 41 with SearchCriteria

use of org.apache.ranger.common.SearchCriteria in project ranger by apache.

the class XPolicyService method updatePermGroup.

public List<VXPermMap> updatePermGroup(VXResource vXResource) {
    XXResource xxResource = xaDaoMgr.getXXResource().getById(vXResource.getId());
    if (xxResource == null) {
        logger.info("Resource : " + vXResource.getPolicyName() + " Not Found, while updating PermGroup");
        throw restErrorUtil.createRESTException("Resource Not found to update PermGroup", MessageEnums.DATA_NOT_FOUND);
    }
    Long resId = vXResource.getId();
    List<VXPermMap> updatedPermMapList = new ArrayList<VXPermMap>();
    SearchCriteria searchCriteria = new SearchCriteria();
    searchCriteria.addParam("resourceId", resId);
    VXPermMapList currentPermMaps = xPermMapService.searchXPermMaps(searchCriteria);
    List<VXPermMap> currentPermMapList = currentPermMaps.getVXPermMaps();
    HashMap<String, List<String>> userPermMap = new HashMap<String, List<String>>();
    for (VXPermMap currentPermMap : currentPermMapList) {
        Long userId = currentPermMap.getUserId();
        Long groupId = currentPermMap.getGroupId();
        int permFor = currentPermMap.getPermFor();
        int permType = currentPermMap.getPermType();
        String ipAddress = currentPermMap.getIpAddress();
        String uniKey = resId + uniqueKeySeparator + permFor;
        if (permFor == AppConstants.XA_PERM_FOR_GROUP) {
            uniKey = uniKey + uniqueKeySeparator + groupId;
        } else if (permFor == AppConstants.XA_PERM_FOR_USER) {
            uniKey = uniKey + uniqueKeySeparator + userId;
        }
        List<String> permList = userPermMap.get(uniKey);
        if (permList == null) {
            permList = new ArrayList<String>();
            userPermMap.put(uniKey, permList);
        }
        permList.add("" + permType);
        if (stringUtil.isEmpty(ipAddress)) {
            permList.add(ipAddress);
        }
    }
    List<List<String>> masterKeyList = new ArrayList<List<String>>();
    List<String> proceedKeyList = new ArrayList<String>();
    for (Entry<String, List<String>> upMap : userPermMap.entrySet()) {
        if (proceedKeyList.contains(upMap.getKey())) {
            continue;
        }
        List<String> keyList = new ArrayList<String>();
        keyList.add(upMap.getKey());
        proceedKeyList.add(upMap.getKey());
        for (Entry<String, List<String>> entry : userPermMap.entrySet()) {
            if (proceedKeyList.contains(entry.getKey())) {
                continue;
            }
            boolean result = compareTwoListElements(upMap.getValue(), entry.getValue());
            if (result) {
                keyList.add(entry.getKey());
                proceedKeyList.add(entry.getKey());
            }
        }
        masterKeyList.add(keyList);
    }
    for (List<String> keyList : masterKeyList) {
        Random rand = new Random();
        String permGrp = new Date() + " : " + rand.nextInt(9999);
        for (String key : keyList) {
            SearchCriteria scPermMap = new SearchCriteria();
            String[] keyEle = StringUtils.split(key, uniqueKeySeparator);
            if (keyEle != null && keyEle.length == 3) {
                int permFor = Integer.parseInt(keyEle[1]);
                int ugId = Integer.parseInt(keyEle[2]);
                scPermMap.addParam("resourceId", resId);
                scPermMap.addParam("permFor", permFor);
                if (permFor == AppConstants.XA_PERM_FOR_GROUP) {
                    scPermMap.addParam("groupId", ugId);
                } else if (permFor == AppConstants.XA_PERM_FOR_USER) {
                    scPermMap.addParam("userId", ugId);
                }
                VXPermMapList permList = xPermMapService.searchXPermMaps(scPermMap);
                for (VXPermMap vXPerm : permList.getVXPermMaps()) {
                    vXPerm.setPermGroup(permGrp);
                    xPermMapService.updateResource(vXPerm);
                    updatedPermMapList.add(vXPerm);
                }
            } else {
                logger.info("variable : keyEle, should fulfill the checked" + " condition, but its not fulfilling required " + "condition. Ignoring appropriate permMap from" + " updating permGroup. Key : " + key + "Resource Id : " + resId);
            }
        }
    }
    return updatedPermMapList;
}
Also used : VXPermMap(org.apache.ranger.view.VXPermMap) XXResource(org.apache.ranger.entity.XXResource) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SearchCriteria(org.apache.ranger.common.SearchCriteria) Date(java.util.Date) Random(java.util.Random) VXResourceList(org.apache.ranger.view.VXResourceList) ArrayList(java.util.ArrayList) VXPolicyList(org.apache.ranger.view.VXPolicyList) VXPermMapList(org.apache.ranger.view.VXPermMapList) List(java.util.List) VXAuditMapList(org.apache.ranger.view.VXAuditMapList) VXPermMapList(org.apache.ranger.view.VXPermMapList)

Example 42 with SearchCriteria

use of org.apache.ranger.common.SearchCriteria in project ranger by apache.

the class XPolicyService method mapPublicToXAObject.

public VXResource mapPublicToXAObject(VXPolicy vXPolicy, int operationContext) {
    VXResource vXResource = new VXResource();
    vXResource = super.mapBaseAttributesToXAObject(vXPolicy, vXResource);
    vXResource.setName(vXPolicy.getResourceName());
    vXResource.setPolicyName(StringUtils.trim(vXPolicy.getPolicyName()));
    vXResource.setDescription(vXPolicy.getDescription());
    vXResource.setResourceType(getResourceType(vXPolicy));
    XXAsset xAsset = xaDaoMgr.getXXAsset().findByAssetName(vXPolicy.getRepositoryName());
    if (xAsset == null) {
        throw restErrorUtil.createRESTException("The repository for which " + "you're updating policy, doesn't exist.", MessageEnums.INVALID_INPUT_DATA);
    }
    vXResource.setAssetId(xAsset.getId());
    if (operationContext == AbstractBaseResourceService.OPERATION_UPDATE_CONTEXT) {
        XXResource xxResource = xaDaoMgr.getXXResource().getById(vXPolicy.getId());
        if (xxResource == null) {
            logger.error("No policy found with given Id : " + vXPolicy.getId());
            throw restErrorUtil.createRESTException("No Policy found with given Id : " + vXResource.getId(), MessageEnums.DATA_NOT_FOUND);
        }
        /*
			 * While updating public object we wont have createDate/updateDate,
			 * so create time, addedById, updatedById, etc. we ll have to take
			 * from existing object
			 */
        xxResource.setUpdateTime(DateUtil.getUTCDate());
        xResourceService.mapBaseAttributesToViewBean(xxResource, vXResource);
        SearchCriteria scAuditMap = new SearchCriteria();
        scAuditMap.addParam("resourceId", xxResource.getId());
        VXAuditMapList vXAuditMapList = xAuditMapService.searchXAuditMaps(scAuditMap);
        List<VXAuditMap> auditList = new ArrayList<VXAuditMap>();
        if (vXAuditMapList.getListSize() > 0 && vXPolicy.getIsAuditEnabled()) {
            auditList.addAll(vXAuditMapList.getVXAuditMaps());
        } else if (vXAuditMapList.getListSize() == 0 && vXPolicy.getIsAuditEnabled()) {
            VXAuditMap vXAuditMap = new VXAuditMap();
            vXAuditMap.setAuditType(AppConstants.XA_AUDIT_TYPE_ALL);
            auditList.add(vXAuditMap);
        }
        List<VXPermMap> permMapList = mapPermObjToPermList(vXPolicy.getPermMapList(), vXPolicy);
        vXResource.setAuditList(auditList);
        vXResource.setPermMapList(permMapList);
    } else if (operationContext == AbstractBaseResourceService.OPERATION_CREATE_CONTEXT) {
        if (vXPolicy.getIsAuditEnabled()) {
            VXAuditMap vXAuditMap = new VXAuditMap();
            vXAuditMap.setAuditType(AppConstants.XA_AUDIT_TYPE_ALL);
            List<VXAuditMap> auditList = new ArrayList<VXAuditMap>();
            auditList.add(vXAuditMap);
            vXResource.setAuditList(auditList);
        }
        if (!stringUtil.isEmpty(vXPolicy.getPermMapList())) {
            List<VXPermMap> permMapList = mapPermObjToPermList(vXPolicy.getPermMapList());
            vXResource.setPermMapList(permMapList);
        }
    }
    vXResource.setDatabases(vXPolicy.getDatabases());
    vXResource.setTables(vXPolicy.getTables());
    vXResource.setColumnFamilies(vXPolicy.getColumnFamilies());
    vXResource.setColumns(vXPolicy.getColumns());
    vXResource.setUdfs(vXPolicy.getUdfs());
    vXResource.setAssetName(vXPolicy.getRepositoryName());
    int assetType = AppConstants.getEnumFor_AssetType(vXPolicy.getRepositoryType());
    if (assetType == AppConstants.ASSET_UNKNOWN) {
        assetType = xAsset.getAssetType();
        vXPolicy.setRepositoryType(AppConstants.getLabelFor_AssetType(assetType));
    }
    vXResource.setAssetType(assetType);
    int resourceStatus = AppConstants.STATUS_ENABLED;
    if (!vXPolicy.getIsEnabled()) {
        resourceStatus = AppConstants.STATUS_DISABLED;
    }
    vXResource.setResourceStatus(resourceStatus);
    // Allowing to create policy without checking parent permission
    vXResource.setCheckParentPermission(AppConstants.BOOL_FALSE);
    vXResource.setTopologies(vXPolicy.getTopologies());
    vXResource.setServices(vXPolicy.getServices());
    /*
		 * TODO : These parameters are specific for some components. Need to
		 * take care while adding new component
		 */
    if (vXPolicy.getRepositoryType().equalsIgnoreCase(AppConstants.getLabelFor_AssetType(AppConstants.ASSET_HIVE))) {
        vXResource.setTableType(AppConstants.getEnumFor_PolicyType(vXPolicy.getTableType()));
        vXResource.setColumnType(AppConstants.getEnumFor_PolicyType(vXPolicy.getColumnType()));
    }
    if (vXPolicy.getRepositoryType().equalsIgnoreCase(AppConstants.getLabelFor_AssetType(AppConstants.ASSET_HDFS))) {
        vXResource.setIsRecursive(AppConstants.getEnumFor_BooleanValue(vXPolicy.getIsRecursive()));
    }
    return vXResource;
}
Also used : VXPermMap(org.apache.ranger.view.VXPermMap) XXResource(org.apache.ranger.entity.XXResource) VXResource(org.apache.ranger.view.VXResource) XXAsset(org.apache.ranger.entity.XXAsset) ArrayList(java.util.ArrayList) VXAuditMapList(org.apache.ranger.view.VXAuditMapList) SearchCriteria(org.apache.ranger.common.SearchCriteria) VXAuditMap(org.apache.ranger.view.VXAuditMap) VXResourceList(org.apache.ranger.view.VXResourceList) ArrayList(java.util.ArrayList) VXPolicyList(org.apache.ranger.view.VXPolicyList) VXPermMapList(org.apache.ranger.view.VXPermMapList) List(java.util.List) VXAuditMapList(org.apache.ranger.view.VXAuditMapList)

Example 43 with SearchCriteria

use of org.apache.ranger.common.SearchCriteria in project ranger by apache.

the class TestUserREST method test1SearchUsers.

@Test
public void test1SearchUsers() {
    SearchCriteria searchCriteria = new SearchCriteria();
    vXPUserExpList = new VXPortalUserList();
    vXPUserExpList.setPageSize(pageSize);
    List<Integer> status = new ArrayList<Integer>();
    String publicScreenName = "nrp";
    List<String> roles = new ArrayList<String>();
    Mockito.when(searchUtil.extractCommonCriterias(Matchers.eq(request), Matchers.anyListOf(SortField.class))).thenReturn(searchCriteria);
    Mockito.when(searchUtil.extractLong(request, searchCriteria, "userId", "User Id")).thenReturn(userId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "loginId", "Login Id", null)).thenReturn(loginId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "emailAddress", "Email Address", null)).thenReturn(emailId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "firstName", "First Name", StringUtil.VALIDATION_NAME)).thenReturn(firstName);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "lastName", "Last Name", StringUtil.VALIDATION_NAME)).thenReturn(lastName);
    Mockito.when(searchUtil.extractEnum(request, searchCriteria, "status", "Status", "statusList", RangerConstants.ActivationStatus_MAX)).thenReturn(status);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "publicScreenName", "Public Screen Name", StringUtil.VALIDATION_NAME)).thenReturn(publicScreenName);
    Mockito.when(searchUtil.extractStringList(request, searchCriteria, "role", "Role", "roleList", configUtil.getRoles(), StringUtil.VALIDATION_NAME)).thenReturn(roles);
    Mockito.when(userManager.searchUsers(searchCriteria)).thenReturn(vXPUserExpList);
    VXPortalUserList vXPUserListAct = userREST.searchUsers(request);
    Assert.assertNotNull(vXPUserListAct);
    Assert.assertEquals(vXPUserExpList, vXPUserListAct);
    Assert.assertEquals(vXPUserExpList.getPageSize(), vXPUserListAct.getPageSize());
    Mockito.verify(searchUtil).extractCommonCriterias(Matchers.eq(request), Matchers.anyListOf(SortField.class));
    Mockito.verify(searchUtil).extractLong(request, searchCriteria, "userId", "User Id");
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "loginId", "Login Id", null);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "emailAddress", "Email Address", null);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "firstName", "First Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "lastName", "Last Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractEnum(request, searchCriteria, "status", "Status", "statusList", RangerConstants.ActivationStatus_MAX);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "publicScreenName", "Public Screen Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractStringList(request, searchCriteria, "role", "Role", "roleList", configUtil.getRoles(), StringUtil.VALIDATION_NAME);
    Mockito.verify(userManager).searchUsers(searchCriteria);
}
Also used : VXPortalUserList(org.apache.ranger.view.VXPortalUserList) ArrayList(java.util.ArrayList) SortField(org.apache.ranger.common.SortField) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Example 44 with SearchCriteria

use of org.apache.ranger.common.SearchCriteria in project ranger by apache.

the class TestXUserREST method test80searchXModuleDef.

@SuppressWarnings("unchecked")
@Test
public void test80searchXModuleDef() {
    VXModuleDefList testVXModuleDefList = new VXModuleDefList();
    VXModuleDef vXModuleDef = createVXModuleDef();
    List<VXModuleDef> VXModuleDefs = new ArrayList<VXModuleDef>();
    VXModuleDefs.add(vXModuleDef);
    testVXModuleDefList.setvXModuleDef(VXModuleDefs);
    testVXModuleDefList.setTotalCount(1);
    testVXModuleDefList.setStartIndex(1);
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    SearchCriteria testSearchCriteria = createsearchCriteria();
    Mockito.when(searchUtil.extractCommonCriterias((HttpServletRequest) Mockito.any(), (List<SortField>) Mockito.any())).thenReturn(testSearchCriteria);
    Mockito.when(searchUtil.extractString(request, testSearchCriteria, "module", "modulename", null)).thenReturn("");
    Mockito.when(searchUtil.extractString(request, testSearchCriteria, "moduleDefList", "id", null)).thenReturn("");
    Mockito.when(searchUtil.extractString(request, testSearchCriteria, "userName", "userName", null)).thenReturn("");
    Mockito.when(searchUtil.extractString(request, testSearchCriteria, "groupName", "groupName", null)).thenReturn("");
    Mockito.when(xUserMgr.searchXModuleDef(testSearchCriteria)).thenReturn(testVXModuleDefList);
    VXModuleDefList outputVXModuleDefList = xUserRest.searchXModuleDef(request);
    assertNotNull(outputVXModuleDefList);
    assertEquals(outputVXModuleDefList.getTotalCount(), testVXModuleDefList.getTotalCount());
    assertEquals(outputVXModuleDefList.getStartIndex(), testVXModuleDefList.getStartIndex());
    Mockito.verify(xUserMgr).searchXModuleDef(testSearchCriteria);
    Mockito.verify(searchUtil).extractCommonCriterias((HttpServletRequest) Mockito.any(), (List<SortField>) Mockito.any());
    Mockito.verify(searchUtil).extractString(request, testSearchCriteria, "module", "modulename", null);
    Mockito.verify(searchUtil).extractString(request, testSearchCriteria, "moduleDefList", "id", null);
    Mockito.verify(searchUtil).extractString(request, testSearchCriteria, "userName", "userName", null);
    Mockito.verify(searchUtil).extractString(request, testSearchCriteria, "groupName", "groupName", null);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) VXModuleDefList(org.apache.ranger.view.VXModuleDefList) VXModuleDef(org.apache.ranger.view.VXModuleDef) ArrayList(java.util.ArrayList) SortField(org.apache.ranger.common.SortField) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Example 45 with SearchCriteria

use of org.apache.ranger.common.SearchCriteria in project ranger by apache.

the class TestXUserREST method test73getXGroupUsers.

@SuppressWarnings("unchecked")
@Test
public void test73getXGroupUsers() {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    SearchCriteria testSearchCriteria = createsearchCriteria();
    testSearchCriteria.addParam("xGroupId", id);
    Mockito.when(searchUtil.extractCommonCriterias((HttpServletRequest) Mockito.any(), (List<SortField>) Mockito.any())).thenReturn(testSearchCriteria);
    VXUser testVXUser = createVXUser();
    VXUserList testVXUserList = new VXUserList();
    List<VXUser> testVXUsers = new ArrayList<VXUser>();
    testVXUsers.add(testVXUser);
    testVXUserList.setVXUsers(testVXUsers);
    testVXUserList.setStartIndex(1);
    testVXUserList.setTotalCount(1);
    Mockito.when(xUserMgr.getXGroupUsers(testSearchCriteria)).thenReturn(testVXUserList);
    VXUserList retVxGroupList = xUserRest.getXGroupUsers(request, id);
    assertNotNull(retVxGroupList);
    assertEquals(testVXUserList.getTotalCount(), retVxGroupList.getTotalCount());
    assertEquals(testVXUserList.getStartIndex(), retVxGroupList.getStartIndex());
    Mockito.verify(xUserMgr).getXGroupUsers(testSearchCriteria);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) SortField(org.apache.ranger.common.SortField) VXUser(org.apache.ranger.view.VXUser) VXUserList(org.apache.ranger.view.VXUserList) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Aggregations

SearchCriteria (org.apache.ranger.common.SearchCriteria)61 Test (org.junit.Test)29 SortField (org.apache.ranger.common.SortField)27 ArrayList (java.util.ArrayList)22 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 GET (javax.ws.rs.GET)13 Path (javax.ws.rs.Path)12 Produces (javax.ws.rs.Produces)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 VXLong (org.apache.ranger.view.VXLong)10 Date (java.util.Date)9 DateUtil (org.apache.ranger.common.DateUtil)6 HashMap (java.util.HashMap)4 XXPortalUser (org.apache.ranger.entity.XXPortalUser)4 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)4 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)4 VXPermMap (org.apache.ranger.view.VXPermMap)4 VXUserList (org.apache.ranger.view.VXUserList)4 List (java.util.List)3 XXServiceDef (org.apache.ranger.entity.XXServiceDef)3