Search in sources :

Example 36 with XXService

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

the class PatchForAtlasServiceDefUpdate_J10013 method updateAtlasServiceDef.

private void updateAtlasServiceDef() {
    String serviceDefName = EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME;
    XXServiceDefDao serviceDefDao = daoMgr.getXXServiceDef();
    XXServiceDef serviceDef = serviceDefDao.findByName(serviceDefName);
    // if service-def named 'atlas' does not exist then no need to process this patch further.
    if (serviceDef == null) {
        LOG.info(serviceDefName + ": service-def not found. No patching is needed");
        return;
    }
    // if older atlas service-def doesn't exist then no need to process this patch further.
    if (!checkIfHasOlderServiceDef(serviceDef)) {
        LOG.info("Older version of " + serviceDefName + " service-def not found. No patching is needed");
        return;
    }
    String suffix = null;
    for (int i = 1; true; i++) {
        suffix = ".v" + i;
        if (serviceDefDao.findByName(serviceDefName + suffix) == null) {
            break;
        }
    }
    String serviceDefNewName = serviceDefName + suffix;
    LOG.info("Renaming service-def " + serviceDefName + " as " + serviceDefNewName);
    serviceDef.setName(serviceDefNewName);
    serviceDefDao.update(serviceDef);
    LOG.info("Renamed service-def " + serviceDefName + " as " + serviceDefNewName);
    XXServiceDao serviceDao = daoMgr.getXXService();
    List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
    if (CollectionUtils.isNotEmpty(services)) {
        for (XXService service : services) {
            String serviceName = service.getName();
            String serviceNewName = serviceName + suffix;
            LOG.info("Renaming service " + serviceName + " as " + serviceNewName);
            if (serviceDao.findByName(serviceNewName) != null) {
                LOG.warn("Another service named " + serviceNewName + " already exists. Not renaming " + serviceName);
                continue;
            }
            service.setName(serviceNewName);
            serviceDao.update(service);
            LOG.info("Renamed service " + serviceName + " as " + serviceNewName);
        }
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService)

Example 37 with XXService

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

the class PatchForServiceVersionInfo_J10004 method copyVersionsFromServiceToServiceVersionInfo.

public void copyVersionsFromServiceToServiceVersionInfo() {
    List<XXService> allServices = daoManager.getXXService().getAll();
    Date now = new Date();
    for (XXService xService : allServices) {
        boolean needToCreateServiceVersionInfo = false;
        XXServiceVersionInfo serviceVersionInfoDbObj = daoManager.getXXServiceVersionInfo().findByServiceId(xService.getId());
        if (serviceVersionInfoDbObj == null) {
            needToCreateServiceVersionInfo = true;
            serviceVersionInfoDbObj = new XXServiceVersionInfo();
            serviceVersionInfoDbObj.setServiceId(xService.getId());
        }
        serviceVersionInfoDbObj.setPolicyVersion(xService.getPolicyVersion() == null ? 1L : xService.getPolicyVersion());
        serviceVersionInfoDbObj.setTagVersion(xService.getTagVersion());
        serviceVersionInfoDbObj.setPolicyUpdateTime(xService.getPolicyUpdateTime());
        serviceVersionInfoDbObj.setTagUpdateTime(xService.getTagUpdateTime());
        if (needToCreateServiceVersionInfo) {
            daoManager.getXXServiceVersionInfo().create(serviceVersionInfoDbObj);
            logger.info("Created serviceVesionInfo for serviceName [" + xService.getName() + "]");
        } else {
            daoManager.getXXServiceVersionInfo().update(serviceVersionInfoDbObj);
            logger.info("Updated serviceVesionInfo for serviceName [" + xService.getName() + "]");
        }
        // Consider this scenario:
        // 1. ranger-admin is upgraded to use versions from x_service_version_info table;
        // 2. there are updates to policies and/or tags;
        // 3. no plug-ins download service-policies;
        // 4. upgrade is rolled back, for ranger-admin to use versions from x_service table;
        // 5. Now plug-in downloads service-policies.
        // In this scenario, plug-ins will miss the policy/tag updates down in step 2. To ensure that
        // plug-ins get updated policies/tags, we increment versions in x_service table when x_service_version_info
        // table is updated in this patch. This may cause one potentially unnecessary download to plugin in case
        // step 2 above did not take place, but it is safer.
        xService.setPolicyVersion(xService.getPolicyVersion() == null ? 2L : xService.getPolicyVersion() + 1);
        xService.setTagVersion(xService.getTagVersion() + 1);
        xService.setPolicyUpdateTime(now);
        xService.setTagUpdateTime(now);
        daoManager.getXXService().update(xService);
        logger.info("Incremented policy and tag versions for serviceName [" + xService.getName() + "]");
    }
}
Also used : XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date)

Example 38 with XXService

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

the class ServiceDBStore method writeExcel.

private void writeExcel(List<RangerPolicy> policies, String excelFileName, HttpServletResponse response) throws IOException {
    Workbook workbook = null;
    OutputStream outStream = null;
    try {
        workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        createHeaderRow(sheet);
        int rowCount = 0;
        if (!CollectionUtils.isEmpty(policies)) {
            for (RangerPolicy policy : policies) {
                List<RangerPolicyItem> policyItems = policy.getPolicyItems();
                List<RangerRowFilterPolicyItem> rowFilterPolicyItems = policy.getRowFilterPolicyItems();
                List<RangerDataMaskPolicyItem> dataMaskPolicyItems = policy.getDataMaskPolicyItems();
                List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
                List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
                List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
                XXService xxservice = daoMgr.getXXService().findByName(policy.getService());
                String serviceType = "";
                if (xxservice != null) {
                    Long ServiceId = xxservice.getType();
                    XXServiceDef xxservDef = daoMgr.getXXServiceDef().getById(ServiceId);
                    if (xxservDef != null) {
                        serviceType = xxservDef.getName();
                    }
                }
                if (CollectionUtils.isNotEmpty(policyItems)) {
                    for (RangerPolicyItem policyItem : policyItems) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
                    }
                } else if (CollectionUtils.isNotEmpty(dataMaskPolicyItems)) {
                    for (RangerDataMaskPolicyItem dataMaskPolicyItem : dataMaskPolicyItems) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, null, dataMaskPolicyItem, null, row, null);
                    }
                } else if (CollectionUtils.isNotEmpty(rowFilterPolicyItems)) {
                    for (RangerRowFilterPolicyItem rowFilterPolicyItem : rowFilterPolicyItems) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, null, null, rowFilterPolicyItem, row, null);
                    }
                } else if (serviceType.equalsIgnoreCase(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME)) {
                    if (CollectionUtils.isEmpty(policyItems)) {
                        Row row = sheet.createRow(++rowCount);
                        RangerPolicyItem policyItem = new RangerPolicyItem();
                        writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
                    }
                } else if (CollectionUtils.isEmpty(policyItems)) {
                    Row row = sheet.createRow(++rowCount);
                    RangerPolicyItem policyItem = new RangerPolicyItem();
                    writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
                }
                if (CollectionUtils.isNotEmpty(allowExceptions)) {
                    for (RangerPolicyItem policyItem : allowExceptions) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_EXCLUDE);
                    }
                }
                if (CollectionUtils.isNotEmpty(denyExceptions)) {
                    for (RangerPolicyItem policyItem : denyExceptions) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_DENY_EXCLUDE);
                    }
                }
                if (CollectionUtils.isNotEmpty(denyPolicyItems)) {
                    for (RangerPolicyItem policyItem : denyPolicyItems) {
                        Row row = sheet.createRow(++rowCount);
                        writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_DENY_INCLUDE);
                    }
                }
            }
        }
        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        workbook.write(outByteStream);
        byte[] outArray = outByteStream.toByteArray();
        response.setContentType("application/ms-excel");
        response.setContentLength(outArray.length);
        response.setHeader("Expires:", "0");
        response.setHeader("Content-Disposition", "attachment; filename=" + excelFileName);
        response.setStatus(HttpServletResponse.SC_OK);
        outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
    } catch (IOException ex) {
        LOG.error("Failed to create report file " + excelFileName, ex);
    } catch (Exception ex) {
        LOG.error("Error while generating report file " + excelFileName, ex);
    } finally {
        if (outStream != null) {
            outStream.close();
        }
        if (workbook != null) {
            workbook.close();
        }
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) RangerRowFilterPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem) VXString(org.apache.ranger.view.VXString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) 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) Row(org.apache.poi.ss.usermodel.Row) XXService(org.apache.ranger.entity.XXService) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 39 with XXService

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

the class ServiceDBStore method updatePolicyVersion.

private void updatePolicyVersion(RangerService service, boolean isTagVersionUpdateNeeded) throws Exception {
    if (service == null || service.getId() == null) {
        return;
    }
    boolean filterForServicePlugin = RangerConfiguration.getInstance().getBoolean(RangerTagDBRetriever.OPTION_RANGER_FILTER_TAGS_FOR_SERVICE_PLUGIN, false);
    XXServiceDao serviceDao = daoMgr.getXXService();
    XXService serviceDbObj = serviceDao.getById(service.getId());
    if (serviceDbObj == null) {
        LOG.warn("updatePolicyVersion(serviceId=" + service.getId() + "): service not found");
        return;
    }
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    XXServiceVersionInfo serviceVersionInfoDbObj = serviceVersionInfoDao.findByServiceId(service.getId());
    if (serviceVersionInfoDbObj != null) {
        serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceVersionInfoDbObj.getPolicyVersion()));
        serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
        serviceVersionInfoDao.update(serviceVersionInfoDbObj);
    } else {
        LOG.warn("updatePolicyVersion(service=" + serviceDbObj.getName() + "): serviceVersionInfo not found, creating it..");
        serviceVersionInfoDbObj = new XXServiceVersionInfo();
        serviceVersionInfoDbObj.setServiceId(serviceDbObj.getId());
        serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceDbObj.getPolicyVersion()));
        serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
        serviceVersionInfoDbObj.setTagVersion(serviceDbObj.getTagVersion());
        serviceVersionInfoDbObj.setTagUpdateTime(serviceDbObj.getTagUpdateTime());
        serviceVersionInfoDao.create(serviceVersionInfoDbObj);
    }
    // if this is a tag service, update all services that refer to this tag service
    // so that next policy-download from plugins will get updated tag policies
    boolean isTagService = serviceDbObj.getType() == EmbeddedServiceDefsUtil.instance().getTagServiceDefId();
    if (isTagService) {
        List<XXService> referringServices = serviceDao.findByTagServiceId(serviceDbObj.getId());
        if (CollectionUtils.isNotEmpty(referringServices)) {
            for (XXService referringService : referringServices) {
                serviceVersionInfoDbObj = serviceVersionInfoDao.findByServiceId(referringService.getId());
                if (serviceVersionInfoDbObj != null) {
                    serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceVersionInfoDbObj.getPolicyVersion()));
                    serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
                    if (filterForServicePlugin && isTagVersionUpdateNeeded) {
                        serviceVersionInfoDbObj.setTagVersion(getNextVersion(serviceVersionInfoDbObj.getTagVersion()));
                        serviceVersionInfoDbObj.setTagUpdateTime(new Date());
                    }
                    serviceVersionInfoDao.update(serviceVersionInfoDbObj);
                } else {
                    LOG.warn("updatePolicyVersion(service=" + referringService.getName() + "): serviceVersionInfo not found, creating it..");
                    serviceVersionInfoDbObj = new XXServiceVersionInfo();
                    serviceVersionInfoDbObj.setServiceId(referringService.getId());
                    serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(referringService.getPolicyVersion()));
                    serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
                    if (filterForServicePlugin && isTagVersionUpdateNeeded) {
                        serviceVersionInfoDbObj.setTagVersion(getNextVersion(referringService.getTagVersion()));
                        serviceVersionInfoDbObj.setTagUpdateTime(new Date());
                    } else {
                        serviceVersionInfoDbObj.setTagVersion(referringService.getTagVersion());
                        serviceVersionInfoDbObj.setTagUpdateTime(referringService.getTagUpdateTime());
                    }
                    serviceVersionInfoDao.create(serviceVersionInfoDbObj);
                }
            }
        }
    }
}
Also used : XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 40 with XXService

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

the class ServiceDBStore method updateServicesForServiceDefUpdate.

// when a service-def is updated, the updated service-def should be made available to plugins
// this is achieved by incrementing policyVersion of all services of this service-def
protected void updateServicesForServiceDefUpdate(RangerServiceDef serviceDef) throws Exception {
    if (serviceDef == null) {
        return;
    }
    boolean isTagServiceDef = StringUtils.equals(serviceDef.getName(), EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME);
    XXServiceDao serviceDao = daoMgr.getXXService();
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
    if (CollectionUtils.isNotEmpty(services)) {
        for (XXService service : services) {
            XXServiceVersionInfo serviceVersionInfo = serviceVersionInfoDao.findByServiceId(service.getId());
            if (serviceVersionInfo != null) {
                serviceVersionInfo.setPolicyVersion(getNextVersion(serviceVersionInfo.getPolicyVersion()));
                serviceVersionInfo.setPolicyUpdateTime(serviceDef.getUpdateTime());
                serviceVersionInfoDao.update(serviceVersionInfo);
            } else {
                LOG.warn("updateServicesForServiceDefUpdate(service=" + service.getName() + "): serviceVersionInfo not found, creating it..");
                serviceVersionInfo = new XXServiceVersionInfo();
                serviceVersionInfo.setServiceId(service.getId());
                serviceVersionInfo.setPolicyVersion(getNextVersion(service.getPolicyVersion()));
                serviceVersionInfo.setTagVersion(service.getTagVersion());
                serviceVersionInfo.setPolicyUpdateTime(new Date());
                serviceVersionInfo.setTagUpdateTime(service.getTagUpdateTime());
                serviceVersionInfoDao.create(serviceVersionInfo);
            }
            if (isTagServiceDef) {
                List<XXService> referrringServices = serviceDao.findByTagServiceId(service.getId());
                if (CollectionUtils.isNotEmpty(referrringServices)) {
                    for (XXService referringService : referrringServices) {
                        serviceVersionInfo = serviceVersionInfoDao.findByServiceId(referringService.getId());
                        if (serviceVersionInfo != null) {
                            serviceVersionInfo.setPolicyVersion(getNextVersion(serviceVersionInfo.getPolicyVersion()));
                            serviceVersionInfo.setPolicyUpdateTime(serviceDef.getUpdateTime());
                            serviceVersionInfoDao.update(serviceVersionInfo);
                        } else {
                            LOG.warn("updateServicesForServiceDefUpdate(service=" + referringService.getName() + "): serviceVersionInfo not found, creating it..");
                            serviceVersionInfo = new XXServiceVersionInfo();
                            serviceVersionInfo.setServiceId(referringService.getId());
                            serviceVersionInfo.setPolicyVersion(getNextVersion(referringService.getPolicyVersion()));
                            serviceVersionInfo.setTagVersion(referringService.getTagVersion());
                            serviceVersionInfo.setPolicyUpdateTime(new Date());
                            serviceVersionInfo.setTagUpdateTime(referringService.getTagUpdateTime());
                            serviceVersionInfoDao.create(serviceVersionInfo);
                        }
                    }
                }
            }
        }
    }
}
Also used : XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Aggregations

XXService (org.apache.ranger.entity.XXService)73 XXServiceDef (org.apache.ranger.entity.XXServiceDef)34 RangerService (org.apache.ranger.plugin.model.RangerService)23 Test (org.junit.Test)21 XXServiceDao (org.apache.ranger.db.XXServiceDao)19 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)19 VXString (org.apache.ranger.view.VXString)19 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)17 ArrayList (java.util.ArrayList)14 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)14 Date (java.util.Date)13 WebApplicationException (javax.ws.rs.WebApplicationException)11 IOException (java.io.IOException)9 ServiceTags (org.apache.ranger.plugin.util.ServiceTags)9 XXServiceVersionInfoDao (org.apache.ranger.db.XXServiceVersionInfoDao)7 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)7 UnknownHostException (java.net.UnknownHostException)6 XXServiceConfigMap (org.apache.ranger.entity.XXServiceConfigMap)6 JSONException (org.codehaus.jettison.json.JSONException)6 HashMap (java.util.HashMap)5