Search in sources :

Example 26 with XXServiceDef

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

the class TestTagREST method test54getSecureServiceTagsIfUpdatedIsKeyAdminFalse.

@Test
public void test54getSecureServiceTagsIfUpdatedIsKeyAdminFalse() {
    boolean isAdmin = false;
    boolean isKeyAdmin = false;
    boolean isAllowed = true;
    ServiceTags oldServiceTag = new ServiceTags();
    oldServiceTag.setServiceName(serviceName);
    oldServiceTag.setTagVersion(5L);
    XXService xService = new XXService();
    xService.setId(id);
    xService.setName(serviceName);
    xService.setType(5L);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setId(id);
    xServiceDef.setVersion(5L);
    xServiceDef.setImplclassname("org.apache.ranger.services.kms.RangerServiceKMS");
    RangerService rangerService = new RangerService();
    rangerService.setId(id);
    rangerService.setName(serviceName);
    XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
    Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
    Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
    Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    try {
        Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
    } catch (Exception e) {
    }
    Mockito.when(bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download)).thenReturn(isAllowed);
    try {
        Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
    } catch (Exception e) {
    }
    ServiceTags result = tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
    Assert.assertNotNull(result.getServiceName());
    Assert.assertEquals(result.getServiceName(), oldServiceTag.getServiceName());
    Assert.assertEquals(result.getTagVersion(), oldServiceTag.getTagVersion());
    Mockito.verify(bizUtil).isAdmin();
    Mockito.verify(bizUtil).isKeyAdmin();
    Mockito.verify(daoManager).getXXService();
    Mockito.verify(xXServiceDao).findByName(serviceName);
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(xXServiceDefDao).getById(xService.getType());
    try {
        Mockito.verify(svcStore).getServiceByName(serviceName);
    } catch (Exception e) {
    }
    Mockito.verify(bizUtil).isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
    try {
        Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
    } catch (Exception e) {
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 27 with XXServiceDef

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

the class TestTagREST method test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue.

@Test
public void test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue() {
    boolean isAdmin = false;
    boolean isKeyAdmin = true;
    ServiceTags oldServiceTag = new ServiceTags();
    oldServiceTag.setServiceName(serviceName);
    oldServiceTag.setTagVersion(5L);
    XXService xService = new XXService();
    xService.setId(id);
    xService.setName(serviceName);
    xService.setType(5L);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setId(id);
    xServiceDef.setVersion(5L);
    xServiceDef.setImplclassname("org.apache.ranger.services.kms.RangerServiceKMS");
    RangerService rangerService = new RangerService();
    rangerService.setId(id);
    rangerService.setName(serviceName);
    XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
    Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
    Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
    Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    try {
        Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
    } catch (Exception e) {
    }
    try {
        Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
    } catch (Exception e) {
    }
    ServiceTags result = tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
    Assert.assertNotNull(result.getServiceName());
    Assert.assertEquals(result.getServiceName(), oldServiceTag.getServiceName());
    Assert.assertEquals(result.getTagVersion(), oldServiceTag.getTagVersion());
    Mockito.verify(bizUtil).isAdmin();
    Mockito.verify(bizUtil).isKeyAdmin();
    Mockito.verify(daoManager).getXXService();
    Mockito.verify(xXServiceDao).findByName(serviceName);
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(xXServiceDefDao).getById(xService.getType());
    try {
        Mockito.verify(svcStore).getServiceByName(serviceName);
    } catch (Exception e) {
    }
    try {
        Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
    } catch (Exception e) {
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 28 with XXServiceDef

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

the class TagREST method getSecureServiceTagsIfUpdated.

@GET
@Path(TagRESTConstants.TAGS_SECURE_DOWNLOAD + "{serviceName}")
@Produces({ "application/json", "application/xml" })
public ServiceTags getSecureServiceTagsIfUpdated(@PathParam("serviceName") String serviceName, @QueryParam(TagRESTConstants.LAST_KNOWN_TAG_VERSION_PARAM) Long lastKnownVersion, @DefaultValue("0") @QueryParam(TagRESTConstants.LAST_ACTIVATION_TIME) Long lastActivationTime, @QueryParam("pluginId") String pluginId, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TagREST.getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
    }
    ServiceTags ret = null;
    int httpCode = HttpServletResponse.SC_OK;
    String logMsg = null;
    boolean isAllowed = false;
    boolean isAdmin = bizUtil.isAdmin();
    boolean isKeyAdmin = bizUtil.isKeyAdmin();
    Long downloadedVersion = null;
    try {
        XXService xService = daoManager.getXXService().findByName(serviceName);
        if (xService == null) {
            LOG.error("Requested Service not found. serviceName=" + serviceName);
            throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, "Service:" + serviceName + " not found", false);
        }
        XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(xService.getType());
        RangerService rangerService = svcStore.getServiceByName(serviceName);
        if (StringUtils.equals(xServiceDef.getImplclassname(), EmbeddedServiceDefsUtil.KMS_IMPL_CLASS_NAME)) {
            if (isKeyAdmin) {
                isAllowed = true;
            } else {
                isAllowed = bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
            }
        } else {
            if (isAdmin) {
                isAllowed = true;
            } else {
                isAllowed = bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
            }
        }
        if (isAllowed) {
            ret = tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion);
            if (ret == null) {
                downloadedVersion = lastKnownVersion;
                httpCode = HttpServletResponse.SC_NOT_MODIFIED;
                logMsg = "No change since last update";
            } else {
                downloadedVersion = ret.getTagVersion();
                httpCode = HttpServletResponse.SC_OK;
                logMsg = "Returning " + (ret.getTags() != null ? ret.getTags().size() : 0) + " tags. Tag version=" + ret.getTagVersion();
            }
        } else {
            LOG.error("getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ") failed as User doesn't have permission to download tags");
            httpCode = HttpServletResponse.SC_UNAUTHORIZED;
            logMsg = "User doesn't have permission to download tags";
        }
    } catch (WebApplicationException webException) {
        httpCode = webException.getResponse().getStatus();
        logMsg = webException.getResponse().getEntity().toString();
    } catch (Exception excp) {
        httpCode = HttpServletResponse.SC_BAD_REQUEST;
        logMsg = excp.getMessage();
    } finally {
        assetMgr.createPluginInfo(serviceName, pluginId, request, RangerPluginInfo.ENTITY_TYPE_TAGS, downloadedVersion, lastKnownVersion, lastActivationTime, httpCode);
    }
    if (httpCode != HttpServletResponse.SC_OK) {
        boolean logError = httpCode != HttpServletResponse.SC_NOT_MODIFIED;
        throw restErrorUtil.createRESTException(httpCode, logMsg, logError);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== TagREST.getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
    }
    return ret;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) WebApplicationException(javax.ws.rs.WebApplicationException) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 29 with XXServiceDef

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

the class RangerDataHistService method createObjectDataHistory.

public void createObjectDataHistory(RangerBaseModelObject baseModelObj, String action) {
    if (baseModelObj == null || action == null) {
        throw restErrorUtil.createRESTException("Error while creating DataHistory. " + "Object or Action can not be null.", MessageEnums.DATA_NOT_FOUND);
    }
    Integer classType = null;
    String objectName = null;
    String content = null;
    Long objectId = baseModelObj.getId();
    String objectGuid = baseModelObj.getGuid();
    Date currentDate = DateUtil.getUTCDate();
    XXDataHist xDataHist = new XXDataHist();
    xDataHist.setObjectId(baseModelObj.getId());
    xDataHist.setObjectGuid(objectGuid);
    xDataHist.setCreateTime(currentDate);
    xDataHist.setAction(action);
    xDataHist.setVersion(baseModelObj.getVersion());
    xDataHist.setUpdateTime(currentDate);
    xDataHist.setFromTime(currentDate);
    if (baseModelObj instanceof RangerServiceDef) {
        RangerServiceDef serviceDef = (RangerServiceDef) baseModelObj;
        objectName = serviceDef.getName();
        classType = AppConstants.CLASS_TYPE_XA_SERVICE_DEF;
        content = writeObjectAsString(serviceDef);
    } else if (baseModelObj instanceof RangerService) {
        RangerService service = (RangerService) baseModelObj;
        objectName = service.getName();
        classType = AppConstants.CLASS_TYPE_XA_SERVICE;
        content = writeObjectAsString(service);
    } else if (baseModelObj instanceof RangerPolicy) {
        RangerPolicy policy = (RangerPolicy) baseModelObj;
        objectName = policy.getName();
        classType = AppConstants.CLASS_TYPE_RANGER_POLICY;
        XXService xXService = daoMgr.getXXService().findByName(policy.getService());
        XXServiceDef xxServiceDef = null;
        if (xXService != null) {
            xxServiceDef = daoMgr.getXXServiceDef().getById(xXService.getType());
        }
        if (xxServiceDef != null) {
            policy.setServiceType(xxServiceDef.getName());
        }
        content = writeObjectAsString(policy);
    }
    xDataHist.setObjectClassType(classType);
    xDataHist.setObjectName(objectName);
    xDataHist.setContent(content);
    xDataHist = daoMgr.getXXDataHist().create(xDataHist);
    if (ACTION_UPDATE.equalsIgnoreCase(action) || ACTION_DELETE.equalsIgnoreCase(action)) {
        XXDataHist prevHist = daoMgr.getXXDataHist().findLatestByObjectClassTypeAndObjectId(classType, objectId);
        if (prevHist == null) {
            throw restErrorUtil.createRESTException("Error updating DataHistory Object. ObjectName: " + objectName, MessageEnums.DATA_NOT_UPDATABLE);
        }
        prevHist.setUpdateTime(currentDate);
        prevHist.setToTime(currentDate);
        prevHist.setObjectName(objectName);
        prevHist = daoMgr.getXXDataHist().update(prevHist);
    }
}
Also used : XXDataHist(org.apache.ranger.entity.XXDataHist) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) Date(java.util.Date)

Example 30 with XXServiceDef

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

the class PatchMigration_J10002 method mapXAssetToService.

private RangerService mapXAssetToService(RangerService service, XXAsset xAsset) throws Exception {
    String type = "";
    String name = xAsset.getName();
    String description = xAsset.getDescription();
    Map<String, String> configs = null;
    int typeInt = xAsset.getAssetType();
    XXServiceDef serviceDef = daoMgr.getXXServiceDef().findByName(AppConstants.getLabelFor_AssetType(typeInt).toLowerCase());
    if (serviceDef == null) {
        throw new Exception("No ServiceDefinition found for repository: " + name);
    }
    type = serviceDef.getName();
    configs = jsonUtil.jsonToMap(xAsset.getConfig());
    List<XXServiceConfigDef> mandatoryConfigs = daoMgr.getXXServiceConfigDef().findByServiceDefName(type);
    for (XXServiceConfigDef serviceConf : mandatoryConfigs) {
        if (serviceConf.getIsMandatory()) {
            if (!stringUtil.isEmpty(configs.get(serviceConf.getName()))) {
                continue;
            }
            String dataType = serviceConf.getType();
            String defaultValue = serviceConf.getDefaultvalue();
            if (stringUtil.isEmpty(defaultValue)) {
                defaultValue = getDefaultValueForDataType(dataType);
            }
            configs.put(serviceConf.getName(), defaultValue);
        }
    }
    service.setType(type);
    service.setName(name);
    service.setDescription(description);
    service.setConfigs(configs);
    service.setCreateTime(xAsset.getCreateTime());
    service.setUpdateTime(xAsset.getUpdateTime());
    XXPortalUser createdByUser = daoMgr.getXXPortalUser().getById(xAsset.getAddedByUserId());
    XXPortalUser updByUser = daoMgr.getXXPortalUser().getById(xAsset.getUpdatedByUserId());
    if (createdByUser != null) {
        service.setCreatedBy(createdByUser.getLoginId());
    }
    if (updByUser != null) {
        service.setUpdatedBy(updByUser.getLoginId());
    }
    service.setId(xAsset.getId());
    return service;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPortalUser(org.apache.ranger.entity.XXPortalUser) XXServiceConfigDef(org.apache.ranger.entity.XXServiceConfigDef)

Aggregations

XXServiceDef (org.apache.ranger.entity.XXServiceDef)79 Test (org.junit.Test)38 XXService (org.apache.ranger.entity.XXService)34 RangerService (org.apache.ranger.plugin.model.RangerService)26 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)25 VXString (org.apache.ranger.view.VXString)22 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)21 WebApplicationException (javax.ws.rs.WebApplicationException)20 ArrayList (java.util.ArrayList)14 Date (java.util.Date)14 XXServiceDao (org.apache.ranger.db.XXServiceDao)13 Path (javax.ws.rs.Path)11 Produces (javax.ws.rs.Produces)11 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)11 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)9 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)8 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 IOException (java.io.IOException)6 XXPortalUser (org.apache.ranger.entity.XXPortalUser)6