Search in sources :

Example 11 with XXPolicy

use of org.apache.ranger.entity.XXPolicy in project ranger by apache.

the class PatchTagModulePermission_J10005 method trimPolicyName.

private void trimPolicyName() {
    List<XXPolicy> policies = daoManager.getXXPolicy().getAll();
    if (!CollectionUtils.isEmpty(policies)) {
        String policyName = null;
        for (XXPolicy xXPolicy : policies) {
            try {
                if (xXPolicy != null) {
                    policyName = xXPolicy.getName();
                    if (!StringUtils.isEmpty(policyName)) {
                        if (policyName.startsWith(" ") || policyName.endsWith(" ")) {
                            xXPolicy.setName(StringUtils.trim(policyName));
                            daoManager.getXXPolicy().update(xXPolicy);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Error during policy update:" + xXPolicy.toString());
                logger.error(ex);
            }
        }
    }
}
Also used : XXPolicy(org.apache.ranger.entity.XXPolicy)

Example 12 with XXPolicy

use of org.apache.ranger.entity.XXPolicy in project ranger by apache.

the class PublicAPIs method updatePolicy.

@PUT
@Path("/api/policy/{id}")
@Produces({ "application/json", "application/xml" })
public VXPolicy updatePolicy(VXPolicy vXPolicy, @PathParam("id") Long id) {
    if (logger.isDebugEnabled()) {
        logger.debug("==> PublicAPIs.updatePolicy(): " + vXPolicy);
    }
    XXPolicy existing = daoMgr.getXXPolicy().getById(id);
    if (existing == null) {
        throw restErrorUtil.createRESTException("Policy not found for Id: " + id, MessageEnums.DATA_NOT_FOUND);
    }
    vXPolicy.setId(id);
    RangerService service = serviceREST.getServiceByName(vXPolicy.getRepositoryName());
    RangerPolicy policy = serviceUtil.toRangerPolicy(vXPolicy, service);
    VXPolicy ret = null;
    if (policy != null) {
        policy.setVersion(existing.getVersion());
        RangerPolicy updatedPolicy = serviceREST.updatePolicy(policy);
        ret = serviceUtil.toVXPolicy(updatedPolicy, service);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("<== PublicAPIs.updatePolicy(" + policy + "): " + ret);
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) XXPolicy(org.apache.ranger.entity.XXPolicy)

Example 13 with XXPolicy

use of org.apache.ranger.entity.XXPolicy in project ranger by apache.

the class ServiceDBStore method getPoliciesByResourceSignature.

@Override
public List<RangerPolicy> getPoliciesByResourceSignature(String serviceName, String policySignature, Boolean isPolicyEnabled) throws Exception {
    List<XXPolicy> xxPolicies = daoMgr.getXXPolicy().findByResourceSignatureByPolicyStatus(serviceName, policySignature, isPolicyEnabled);
    List<RangerPolicy> policies = new ArrayList<RangerPolicy>(xxPolicies.size());
    for (XXPolicy xxPolicy : xxPolicies) {
        RangerPolicy policy = policyService.getPopulatedViewObject(xxPolicy);
        policies.add(policy);
    }
    return policies;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ArrayList(java.util.ArrayList) XXPolicy(org.apache.ranger.entity.XXPolicy)

Example 14 with XXPolicy

use of org.apache.ranger.entity.XXPolicy in project ranger by apache.

the class ServiceDBStore method deleteService.

@Override
public void deleteService(Long id) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deleteService(" + id + ")");
    }
    RangerService service = getService(id);
    if (service == null) {
        throw new Exception("no service exists with ID=" + id);
    }
    List<XXPolicy> policies = daoMgr.getXXPolicy().findByServiceId(service.getId());
    RangerPolicy rangerPolicy = null;
    for (XXPolicy policy : policies) {
        LOG.info("Deleting Policy, policyName: " + policy.getName());
        rangerPolicy = getPolicy(policy.getId());
        deletePolicy(rangerPolicy);
    }
    XXServiceConfigMapDao configDao = daoMgr.getXXServiceConfigMap();
    List<XXServiceConfigMap> configs = configDao.findByServiceId(service.getId());
    for (XXServiceConfigMap configMap : configs) {
        configDao.remove(configMap);
    }
    Long version = service.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    service.setVersion(version);
    svcService.delete(service);
    dataHistService.createObjectDataHistory(service, RangerDataHistService.ACTION_DELETE);
    List<XXTrxLog> trxLogList = svcService.getTransactionLog(service, RangerServiceService.OPERATION_DELETE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
}
Also used : XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 15 with XXPolicy

use of org.apache.ranger.entity.XXPolicy in project ranger by apache.

the class ServiceDBStore method updatePolicy.

@Override
public RangerPolicy updatePolicy(RangerPolicy policy) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.updatePolicy(" + policy + ")");
    }
    XXPolicy xxExisting = daoMgr.getXXPolicy().getById(policy.getId());
    RangerPolicy existing = policyService.getPopulatedViewObject(xxExisting);
    if (existing == null) {
        throw new Exception("no policy exists with ID=" + policy.getId());
    }
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name=" + policy.getService());
    }
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType());
    if (xServiceDef == null) {
        throw new Exception("service-def does not exist - name=" + service.getType());
    }
    if (!StringUtils.equalsIgnoreCase(existing.getService(), policy.getService())) {
        throw new Exception("policy id=" + policy.getId() + " already exists in service " + existing.getService() + ". It can not be moved to service " + policy.getService());
    }
    boolean renamed = !StringUtils.equalsIgnoreCase(policy.getName(), existing.getName());
    if (renamed) {
        XXPolicy newNamePolicy = daoMgr.getXXPolicy().findByNameAndServiceId(policy.getName(), service.getId());
        if (newNamePolicy != null) {
            throw new Exception("another policy already exists with name '" + policy.getName() + "'. ID=" + newNamePolicy.getId());
        }
    }
    Map<String, RangerPolicyResource> newResources = policy.getResources();
    List<RangerPolicyItem> policyItems = policy.getPolicyItems();
    List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
    List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
    List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
    List<RangerDataMaskPolicyItem> dataMaskPolicyItems = policy.getDataMaskPolicyItems();
    List<RangerRowFilterPolicyItem> rowFilterItems = policy.getRowFilterPolicyItems();
    List<String> policyLabels = policy.getPolicyLabels();
    policy.setCreateTime(xxExisting.getCreateTime());
    policy.setGuid(xxExisting.getGuid());
    policy.setVersion(xxExisting.getVersion());
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, xxExisting, RangerPolicyService.OPERATION_UPDATE_CONTEXT);
    updatePolicySignature(policy);
    boolean isTagVersionUpdateNeeded = false;
    if (EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME.equals(service.getType())) {
        isTagVersionUpdateNeeded = existing.getIsEnabled() ? !policy.getIsEnabled() : policy.getIsEnabled();
        isTagVersionUpdateNeeded = isTagVersionUpdateNeeded || !StringUtils.equals(existing.getResourceSignature(), policy.getResourceSignature());
    }
    policy = policyService.update(policy);
    XXPolicy newUpdPolicy = daoMgr.getXXPolicy().getById(policy.getId());
    deleteExistingPolicyResources(policy);
    deleteExistingPolicyItems(policy);
    deleteExistingPolicyLabel(policy);
    createNewResourcesForPolicy(policy, newUpdPolicy, newResources);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, policyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, denyPolicyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, allowExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW_EXCEPTIONS);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, denyExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY_EXCEPTIONS);
    createNewDataMaskPolicyItemsForPolicy(policy, newUpdPolicy, dataMaskPolicyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DATAMASK);
    createNewRowFilterPolicyItemsForPolicy(policy, newUpdPolicy, rowFilterItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ROWFILTER);
    createNewLabelsForPolicy(newUpdPolicy, policyLabels);
    handlePolicyUpdate(service, isTagVersionUpdateNeeded);
    RangerPolicy updPolicy = policyService.getPopulatedViewObject(newUpdPolicy);
    dataHistService.createObjectDataHistory(updPolicy, RangerDataHistService.ACTION_UPDATE);
    bizUtil.createTrxLog(trxLogList);
    return updPolicy;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) RangerRowFilterPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerDataMaskPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem) RangerService(org.apache.ranger.plugin.model.RangerService)

Aggregations

XXPolicy (org.apache.ranger.entity.XXPolicy)20 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)14 Test (org.junit.Test)7 RangerService (org.apache.ranger.plugin.model.RangerService)6 XXPolicyDao (org.apache.ranger.db.XXPolicyDao)5 XXTrxLog (org.apache.ranger.entity.XXTrxLog)5 ArrayList (java.util.ArrayList)4 XXAuditMapDao (org.apache.ranger.db.XXAuditMapDao)4 XXGroupUserDao (org.apache.ranger.db.XXGroupUserDao)4 XXPermMapDao (org.apache.ranger.db.XXPermMapDao)4 RangerDataMaskPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem)4 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)4 RangerRowFilterPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem)4 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Date (java.util.Date)3 XXServiceDao (org.apache.ranger.db.XXServiceDao)3 XXUserDao (org.apache.ranger.db.XXUserDao)3 XXUser (org.apache.ranger.entity.XXUser)3 SearchCriteria (org.apache.ranger.common.SearchCriteria)2