Search in sources :

Example 16 with RangerServiceDefValidator

use of org.apache.ranger.plugin.model.validation.RangerServiceDefValidator in project ranger by apache.

the class ServiceREST method deleteServiceDef.

@DELETE
@Path("/definitions/{id}")
@Produces({ "application/json", "application/xml" })
@PreAuthorize("@rangerPreAuthSecurityHandler.isAPIAccessible(\"" + RangerAPIList.DELETE_SERVICE_DEF + "\")")
public void deleteServiceDef(@PathParam("id") Long id, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.deleteServiceDef(" + id + ")");
    }
    RangerPerfTracer perf = null;
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.deleteServiceDef(serviceDefId=" + id + ")");
        }
        RangerServiceDefValidator validator = validatorFactory.getServiceDefValidator(svcStore);
        validator.validate(id, Action.DELETE);
        bizUtil.hasAdminPermissions("Service-Def");
        XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(id);
        if (xServiceDef != null) {
            bizUtil.hasKMSPermissions("Service-Def", xServiceDef.getImplclassname());
            String forceDeleteStr = request.getParameter("forceDelete");
            boolean forceDelete = false;
            if (!StringUtils.isEmpty(forceDeleteStr) && "true".equalsIgnoreCase(forceDeleteStr)) {
                forceDelete = true;
            }
            svcStore.deleteServiceDef(id, forceDelete);
        } else {
            LOG.error("Cannot retrieve service-definition:[" + id + "] for deletion");
            throw new Exception("deleteServiceDef(" + id + ") failed");
        }
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("deleteServiceDef(" + id + ") failed", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceREST.deleteServiceDef(" + id + ")");
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) VXString(org.apache.ranger.view.VXString) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with RangerServiceDefValidator

use of org.apache.ranger.plugin.model.validation.RangerServiceDefValidator in project ranger by apache.

the class ServiceREST method createServiceDef.

@POST
@Path("/definitions")
@Produces({ "application/json", "application/xml" })
@PreAuthorize("@rangerPreAuthSecurityHandler.isAPIAccessible(\"" + RangerAPIList.CREATE_SERVICE_DEF + "\")")
public RangerServiceDef createServiceDef(RangerServiceDef serviceDef) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.createServiceDef(" + serviceDef + ")");
    }
    RangerServiceDef ret = null;
    RangerPerfTracer perf = null;
    /**
     * If display name is blank (EMPTY String or NULL), use name.
     */
    if (StringUtils.isBlank(serviceDef.getDisplayName())) {
        serviceDef.setDisplayName(serviceDef.getName());
    }
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.createServiceDef(serviceDefName=" + serviceDef.getName() + ")");
        }
        RangerServiceDefValidator validator = validatorFactory.getServiceDefValidator(svcStore);
        validator.validate(serviceDef, Action.CREATE);
        bizUtil.hasAdminPermissions("Service-Def");
        bizUtil.hasKMSPermissions("Service-Def", serviceDef.getImplClass());
        bizUtil.blockAuditorRoleUser();
        ret = svcStore.createServiceDef(serviceDef);
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("createServiceDef(" + serviceDef + ") failed", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceREST.createServiceDef(" + serviceDef + "): " + ret);
    }
    return ret;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 18 with RangerServiceDefValidator

use of org.apache.ranger.plugin.model.validation.RangerServiceDefValidator in project ranger by apache.

the class PatchForNifiResourceUpdateExclude_J10011 method updateNifiServiceDef.

private void updateNifiServiceDef() {
    RangerServiceDef ret = null;
    RangerServiceDef dbNifiServiceDef = null;
    try {
        dbNifiServiceDef = svcDBStore.getServiceDefByName(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_NIFI_NAME);
        if (dbNifiServiceDef != null) {
            List<RangerResourceDef> rRDefList = null;
            rRDefList = dbNifiServiceDef.getResources();
            if (CollectionUtils.isNotEmpty(rRDefList)) {
                for (RangerResourceDef rRDef : rRDefList) {
                    if (rRDef.getExcludesSupported()) {
                        rRDef.setExcludesSupported(false);
                    }
                    XXResourceDef sdf = daoMgr.getXXResourceDef().findByNameAndServiceDefId(rRDef.getName(), dbNifiServiceDef.getId());
                    long ResourceDefId = sdf.getId();
                    List<XXPolicyResource> RangerPolicyResourceList = daoMgr.getXXPolicyResource().findByResDefId(ResourceDefId);
                    if (CollectionUtils.isNotEmpty(RangerPolicyResourceList)) {
                        for (XXPolicyResource RangerPolicyResource : RangerPolicyResourceList) {
                            if (RangerPolicyResource.getIsexcludes()) {
                                RangerPolicy rPolicy = svcDBStore.getPolicy(RangerPolicyResource.getPolicyid());
                                rPolicy.setIsEnabled(false);
                                svcStore.updatePolicy(rPolicy);
                            }
                        }
                    }
                }
            }
            RangerServiceDefValidator validator = validatorFactory.getServiceDefValidator(svcStore);
            validator.validate(dbNifiServiceDef, Action.UPDATE);
            ret = svcStore.updateServiceDef(dbNifiServiceDef);
        }
        if (ret == null) {
            logger.error("Error while updating " + EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_NIFI_NAME + "service-def");
        }
    } catch (Exception e) {
        logger.error("Error while updating " + EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_NIFI_NAME + "service-def", e);
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXPolicyResource(org.apache.ranger.entity.XXPolicyResource) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)

Example 19 with RangerServiceDefValidator

use of org.apache.ranger.plugin.model.validation.RangerServiceDefValidator in project ranger by apache.

the class PatchAtlasForClassificationResource_J10047 method addResourceClassificationsInServiceDef.

private void addResourceClassificationsInServiceDef() throws Exception {
    RangerServiceDef ret = null;
    RangerServiceDef embeddedAtlasServiceDef = null;
    XXServiceDef xXServiceDefObj = null;
    RangerServiceDef dbAtlasServiceDef = null;
    embeddedAtlasServiceDef = EmbeddedServiceDefsUtil.instance().getEmbeddedServiceDef(EMBEDDED_SERVICEDEF_ATLAS_NAME);
    if (embeddedAtlasServiceDef != null) {
        xXServiceDefObj = daoMgr.getXXServiceDef().findByName(EMBEDDED_SERVICEDEF_ATLAS_NAME);
        if (xXServiceDefObj == null) {
            logger.info(" service-def for " + EMBEDDED_SERVICEDEF_ATLAS_NAME + " not found. No patching is needed");
            return;
        }
        dbAtlasServiceDef = svcDBStore.getServiceDefByName(EMBEDDED_SERVICEDEF_ATLAS_NAME);
        updateResourceInServiceDef(embeddedAtlasServiceDef, dbAtlasServiceDef);
        updateTypeResourceWithIgnoreCase(dbAtlasServiceDef.getResources());
        removeEntityResourceAccessTypeRestrictions(dbAtlasServiceDef.getResources());
        RangerServiceDefValidator validator = validatorFactory.getServiceDefValidator(svcStore);
        validator.validate(dbAtlasServiceDef, RangerValidator.Action.UPDATE);
        ret = svcStore.updateServiceDef(dbAtlasServiceDef);
        if (ret == null) {
            logger.error("Error while updating " + EMBEDDED_SERVICEDEF_ATLAS_NAME + "  service-def");
            throw new RuntimeException("Error while updating " + EMBEDDED_SERVICEDEF_ATLAS_NAME + " service-def");
        }
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator)

Example 20 with RangerServiceDefValidator

use of org.apache.ranger.plugin.model.validation.RangerServiceDefValidator in project ranger by apache.

the class PatchForTagServiceDefUpdate_J10008 method updateTagServiceDef.

private void updateTagServiceDef() {
    RangerServiceDef embeddedTagServiceDef = null;
    RangerServiceDef dbTagServiceDef = null;
    List<RangerServiceDef.RangerPolicyConditionDef> embeddedTagPolicyConditionDefs = null;
    XXServiceDef xXServiceDefObj = null;
    try {
        embeddedTagServiceDef = EmbeddedServiceDefsUtil.instance().getEmbeddedServiceDef(SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME);
        if (embeddedTagServiceDef != null) {
            embeddedTagPolicyConditionDefs = embeddedTagServiceDef.getPolicyConditions();
            if (embeddedTagPolicyConditionDefs == null) {
                logger.error("Policy Conditions are empyt in tag service def json");
                return;
            }
            if (checkScriptPolicyCondPresent(embeddedTagPolicyConditionDefs) == false) {
                logger.error(SCRIPT_POLICY_CONDITION_NAME + "policy condition not found!!");
                return;
            }
            xXServiceDefObj = daoMgr.getXXServiceDef().findByName(SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME);
            if (xXServiceDefObj == null) {
                logger.error("Service def for " + SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME + " is not found!!");
                return;
            }
            Map<String, String> serviceDefOptionsPreUpdate = null;
            String jsonStrPreUpdate = null;
            jsonStrPreUpdate = xXServiceDefObj.getDefOptions();
            if (!StringUtils.isEmpty(jsonStrPreUpdate)) {
                serviceDefOptionsPreUpdate = jsonUtil.jsonToMap(jsonStrPreUpdate);
            }
            xXServiceDefObj = null;
            dbTagServiceDef = svcDBStore.getServiceDefByName(SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME);
            if (dbTagServiceDef != null) {
                dbTagServiceDef.setPolicyConditions(embeddedTagPolicyConditionDefs);
                RangerServiceDefValidator validator = validatorFactory.getServiceDefValidator(svcStore);
                validator.validate(dbTagServiceDef, Action.UPDATE);
                svcStore.updateServiceDef(dbTagServiceDef);
                xXServiceDefObj = daoMgr.getXXServiceDef().findByName(SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME);
                if (xXServiceDefObj != null) {
                    String jsonStrPostUpdate = xXServiceDefObj.getDefOptions();
                    Map<String, String> serviceDefOptionsPostUpdate = null;
                    if (!StringUtils.isEmpty(jsonStrPostUpdate)) {
                        serviceDefOptionsPostUpdate = jsonUtil.jsonToMap(jsonStrPostUpdate);
                    }
                    if (serviceDefOptionsPostUpdate != null && serviceDefOptionsPostUpdate.containsKey(RangerServiceDef.OPTION_ENABLE_DENY_AND_EXCEPTIONS_IN_POLICIES)) {
                        if (serviceDefOptionsPreUpdate == null || !serviceDefOptionsPreUpdate.containsKey(RangerServiceDef.OPTION_ENABLE_DENY_AND_EXCEPTIONS_IN_POLICIES)) {
                            String preUpdateValue = serviceDefOptionsPreUpdate == null ? null : serviceDefOptionsPreUpdate.get(RangerServiceDef.OPTION_ENABLE_DENY_AND_EXCEPTIONS_IN_POLICIES);
                            if (preUpdateValue == null) {
                                serviceDefOptionsPostUpdate.remove(RangerServiceDef.OPTION_ENABLE_DENY_AND_EXCEPTIONS_IN_POLICIES);
                            } else {
                                serviceDefOptionsPostUpdate.put(RangerServiceDef.OPTION_ENABLE_DENY_AND_EXCEPTIONS_IN_POLICIES, preUpdateValue);
                            }
                            xXServiceDefObj.setDefOptions(mapToJsonString(serviceDefOptionsPostUpdate));
                            daoMgr.getXXServiceDef().update(xXServiceDefObj);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error while updating " + SERVICEDBSTORE_SERVICEDEFBYNAME_TAG_NAME + "service-def", e);
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator)

Aggregations

RangerServiceDefValidator (org.apache.ranger.plugin.model.validation.RangerServiceDefValidator)25 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)21 XXServiceDef (org.apache.ranger.entity.XXServiceDef)18 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)5 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 XXResourceDef (org.apache.ranger.entity.XXResourceDef)2 RangerDataMaskDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskDef)2 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)2 RangerRowFilterDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerRowFilterDef)2 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 XXAccessTypeDefDao (org.apache.ranger.db.XXAccessTypeDefDao)1