ServiceDBStore extends AbstractServiceStore { public String getMetricByType(String type) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("==> ServiceDBStore.getMetricByType(" + type + ")"); } String ret = null; try { SearchCriteria searchCriteria = new SearchCriteria(); searchCriteria.setStartIndex(0); searchCriteria.setMaxRows(100); searchCriteria.setGetCount(true); searchCriteria.setSortType("asc"); switch (type.toLowerCase()) { case "usergroup": try { VXGroupList vxGroupList = xUserMgr.searchXGroups(searchCriteria); long groupCount = vxGroupList.getTotalCount(); ArrayList<String> userAdminRoleCount = new ArrayList<String>(); userAdminRoleCount.add(RangerConstants.ROLE_SYS_ADMIN); long userSysAdminCount = getUserCountBasedOnUserRole(userAdminRoleCount); ArrayList<String> userAdminAuditorRoleCount = new ArrayList<String>(); userAdminAuditorRoleCount.add(RangerConstants.ROLE_ADMIN_AUDITOR); long userSysAdminAuditorCount = getUserCountBasedOnUserRole(userAdminAuditorRoleCount); ArrayList<String> userRoleListKeyRoleAdmin = new ArrayList<String>(); userRoleListKeyRoleAdmin.add(RangerConstants.ROLE_KEY_ADMIN); long userKeyAdminCount = getUserCountBasedOnUserRole(userRoleListKeyRoleAdmin); ArrayList<String> userRoleListKeyadminAduitorRole = new ArrayList<String>(); userRoleListKeyadminAduitorRole.add(RangerConstants.ROLE_KEY_ADMIN_AUDITOR); long userKeyadminAuditorCount = getUserCountBasedOnUserRole(userRoleListKeyadminAduitorRole); ArrayList<String> userRoleListUser = new ArrayList<String>(); userRoleListUser.add(RangerConstants.ROLE_USER); long userRoleCount = getUserCountBasedOnUserRole(userRoleListUser); long userTotalCount = userSysAdminCount + userKeyAdminCount + userRoleCount + userKeyadminAuditorCount + userSysAdminAuditorCount; VXMetricUserGroupCount metricUserGroupCount = new VXMetricUserGroupCount(); metricUserGroupCount.setUserCountOfUserRole(userRoleCount); metricUserGroupCount.setUserCountOfKeyAdminRole(userKeyAdminCount); metricUserGroupCount.setUserCountOfSysAdminRole(userSysAdminCount); metricUserGroupCount.setUserCountOfKeyadminAuditorRole(userKeyadminAuditorCount); metricUserGroupCount.setUserCountOfSysAdminAuditorRole(userSysAdminAuditorCount); metricUserGroupCount.setUserTotalCount(userTotalCount); metricUserGroupCount.setGroupCount(groupCount); Gson gson = new GsonBuilder().create(); final String jsonUserGroupCount = gson.toJson(metricUserGroupCount); ret = jsonUserGroupCount; } catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for usergroup : " + e.getMessage()); } break; case "audits": try{ int clientTimeOffsetInMinute = RestUtil.getClientTimeOffset(); String defaultDateFormat="MM/dd/yyyy"; DateFormat formatter = new SimpleDateFormat(defaultDateFormat); VXMetricAuditDetailsCount auditObj = new VXMetricAuditDetailsCount(); DateUtil dateUtilTwoDays = new DateUtil(); Date startDateUtilTwoDays = dateUtilTwoDays.getDateFromNow(-2); Date dStart2 = restErrorUtil.parseDate(formatter.format(startDateUtilTwoDays),"Invalid value for startDate", MessageEnums.INVALID_INPUT_DATA, null, "startDate", defaultDateFormat); Date endDateTwoDays = MiscUtil.getUTCDate(); Date dEnd2 = restErrorUtil.parseDate(formatter.format(endDateTwoDays),"Invalid value for endDate", MessageEnums.INVALID_INPUT_DATA, null, "endDate", defaultDateFormat); dEnd2 = dateUtilTwoDays.getDateFromGivenDate(dEnd2, 0, 23, 59, 59); dEnd2 = dateUtilTwoDays.addTimeOffset(dEnd2, clientTimeOffsetInMinute); VXMetricServiceCount deniedCountObj = getAuditsCount(0,dStart2,dEnd2); auditObj.setDenialEventsCountTwoDays(deniedCountObj); VXMetricServiceCount allowedCountObj = getAuditsCount(1,dStart2,dEnd2); auditObj.setAccessEventsCountTwoDays(allowedCountObj); long totalAuditsCountTwoDays = deniedCountObj.getTotalCount() + allowedCountObj.getTotalCount(); auditObj.setSolrIndexCountTwoDays(totalAuditsCountTwoDays); DateUtil dateUtilWeek = new DateUtil(); Date startDateUtilWeek = dateUtilWeek.getDateFromNow(-7); Date dStart7 = restErrorUtil.parseDate(formatter.format(startDateUtilWeek),"Invalid value for startDate", MessageEnums.INVALID_INPUT_DATA, null, "startDate", defaultDateFormat); Date endDateWeek = MiscUtil.getUTCDate(); DateUtil dateUtilweek = new DateUtil(); Date dEnd7 = restErrorUtil.parseDate(formatter.format(endDateWeek),"Invalid value for endDate", MessageEnums.INVALID_INPUT_DATA, null, "endDate", defaultDateFormat); dEnd7 = dateUtilweek.getDateFromGivenDate(dEnd7,0, 23, 59, 59 ); dEnd7 = dateUtilweek.addTimeOffset(dEnd7, clientTimeOffsetInMinute); VXMetricServiceCount deniedCountObjWeek = getAuditsCount(0,dStart7,dEnd7); auditObj.setDenialEventsCountWeek(deniedCountObjWeek); VXMetricServiceCount allowedCountObjWeek = getAuditsCount(1,dStart7,dEnd7); auditObj.setAccessEventsCountWeek(allowedCountObjWeek); long totalAuditsCountWeek = deniedCountObjWeek.getTotalCount() + allowedCountObjWeek.getTotalCount(); auditObj.setSolrIndexCountWeek(totalAuditsCountWeek); Gson gson = new GsonBuilder().create(); final String jsonAudit = gson.toJson(auditObj); ret = jsonAudit; }catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for audits : "+e.getMessage()); } break; case "services" : try { SearchFilter serviceFilter = new SearchFilter(); serviceFilter.setMaxRows(200); serviceFilter.setStartIndex(0); serviceFilter.setGetCount(true); serviceFilter.setSortBy("serviceId"); serviceFilter.setSortType("asc"); VXMetricServiceCount vXMetricServiceCount = new VXMetricServiceCount(); PList<RangerService> paginatedSvcs = getPaginatedServices(serviceFilter); long totalServiceCount = paginatedSvcs.getTotalCount(); List<RangerService> rangerServiceList = paginatedSvcs.getList(); Map<String, Long> services = new HashMap<String, Long>(); for (Object rangerService : rangerServiceList) { RangerService RangerServiceObj = (RangerService) rangerService; String serviceName = RangerServiceObj.getType(); if (!(services.containsKey(serviceName))) { serviceFilter.setParam("serviceType", serviceName); PList<RangerService> paginatedSvcscount = getPaginatedServices(serviceFilter); services.put(serviceName, paginatedSvcscount.getTotalCount()); } } vXMetricServiceCount.setServiceBasedCountList(services); vXMetricServiceCount.setTotalCount(totalServiceCount); Gson gson = new GsonBuilder().create(); final String jsonServices = gson.toJson(vXMetricServiceCount); ret= jsonServices; } catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for services : " + e.getMessage()); } break; case "policies" : try { SearchFilter policyFilter = new SearchFilter(); policyFilter.setMaxRows(200); policyFilter.setStartIndex(0); policyFilter.setGetCount(true); policyFilter.setSortBy("serviceId"); policyFilter.setSortType("asc"); VXMetricPolicyWithServiceNameCount vXMetricPolicyWithServiceNameCount = new VXMetricPolicyWithServiceNameCount(); PList<RangerPolicy> paginatedSvcsList = getPaginatedPolicies(policyFilter); vXMetricPolicyWithServiceNameCount.setTotalCount(paginatedSvcsList.getTotalCount()); Map<String, VXMetricServiceNameCount> servicesWithPolicy = new HashMap<String, VXMetricServiceNameCount>(); for (int k = 2; k >= 0; k--) { String serviceType = String.valueOf(k); VXMetricServiceNameCount vXMetricServiceNameCount = getVXMetricServiceCount(serviceType); if (k == 2) { servicesWithPolicy.put("rowFilteringPolicies", vXMetricServiceNameCount); } else if (k == 1) { servicesWithPolicy.put("maskingPolicies", vXMetricServiceNameCount); } else if (k == 0) { servicesWithPolicy.put("resourcePolicy", vXMetricServiceNameCount); } } Map<String, Map<String,Long>> tagMap = new HashMap<String, Map<String,Long>>(); Map<String,Long> ServiceNameWithPolicyCount = new HashMap<String, Long>(); boolean tagFlag = false; if (tagFlag == false) { policyFilter.setParam("serviceType", "tag"); PList<RangerPolicy> policiestype = getPaginatedPolicies(policyFilter); List<RangerPolicy> policies = policiestype.getList(); for (RangerPolicy rangerPolicy : policies) { if(ServiceNameWithPolicyCount.containsKey(rangerPolicy.getService())){ Long tagServicePolicyCount = ServiceNameWithPolicyCount.get(rangerPolicy.getService()) +1l; ServiceNameWithPolicyCount.put(rangerPolicy.getService(),tagServicePolicyCount); } else if (!rangerPolicy.getName().isEmpty()){ ServiceNameWithPolicyCount.put(rangerPolicy.getService(),1l); } } tagMap.put("tag", ServiceNameWithPolicyCount); long tagCount = policiestype.getTotalCount(); VXMetricServiceNameCount vXMetricServiceNameCount = new VXMetricServiceNameCount(); vXMetricServiceNameCount.setServiceBasedCountList(tagMap); vXMetricServiceNameCount.setTotalCount(tagCount); servicesWithPolicy.put("tagBasedPolicies", vXMetricServiceNameCount); tagFlag = true; } vXMetricPolicyWithServiceNameCount.setPolicyCountList(servicesWithPolicy); Gson gson = new GsonBuilder().create(); final String jsonPolicies = gson.toJson(vXMetricPolicyWithServiceNameCount); ret= jsonPolicies; } catch (Exception e) { LOG.error("Error calculating Metric for policies : " + e.getMessage()); } break; case "database" : try { int dbFlavor = RangerBizUtil.getDBFlavor(); String dbFlavourType = "Unknow "; if (dbFlavor == AppConstants.DB_FLAVOR_MYSQL) { dbFlavourType = "MYSQL "; } else if (dbFlavor == AppConstants.DB_FLAVOR_ORACLE) { dbFlavourType = "ORACLE "; } else if (dbFlavor == AppConstants.DB_FLAVOR_POSTGRES) { dbFlavourType = "POSTGRES "; } else if (dbFlavor == AppConstants.DB_FLAVOR_SQLANYWHERE) { dbFlavourType = "SQLANYWHERE "; } else if (dbFlavor == AppConstants.DB_FLAVOR_SQLSERVER) { dbFlavourType = "SQLSERVER "; } String dbDetail = dbFlavourType + bizUtil.getDBVersion(); Gson gson = new GsonBuilder().create(); final String jsonDBDetail = gson.toJson(dbDetail); ret = jsonDBDetail; } catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for database : " + e.getMessage()); } break; case "contextenrichers": try { SearchFilter filter = new SearchFilter(); filter.setStartIndex(0); VXMetricContextEnricher serviceWithContextEnrichers = new VXMetricContextEnricher(); PList<RangerServiceDef> paginatedSvcDefs = getPaginatedServiceDefs(filter); List<RangerServiceDef> repoTypeList = paginatedSvcDefs.getList(); if (repoTypeList != null) { for (RangerServiceDef repoType : repoTypeList) { RangerServiceDef rangerServiceDefObj = (RangerServiceDef) repoType; String name = rangerServiceDefObj.getName(); List<RangerContextEnricherDef> contextEnrichers = rangerServiceDefObj.getContextEnrichers(); if (contextEnrichers != null && !contextEnrichers.isEmpty()) { serviceWithContextEnrichers.setServiceName(name); serviceWithContextEnrichers.setTotalCount(contextEnrichers.size()); } } } Gson gson = new GsonBuilder().create(); final String jsonContextEnrichers = gson.toJson(serviceWithContextEnrichers); ret = jsonContextEnrichers; } catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for contextenrichers : " + e.getMessage()); } break; case "denyconditions": try { SearchFilter policyFilter1 = new SearchFilter(); policyFilter1.setMaxRows(200); policyFilter1.setStartIndex(0); policyFilter1.setGetCount(true); policyFilter1.setSortBy("serviceId"); policyFilter1.setSortType("asc"); int denyCount = 0; Map<String, Integer> denyconditionsonMap = new HashMap<String, Integer>(); PList<RangerServiceDef> paginatedSvcDefs = getPaginatedServiceDefs(policyFilter1); if (paginatedSvcDefs != null) { List<RangerServiceDef> rangerServiceDefs = paginatedSvcDefs.getList(); if (rangerServiceDefs != null && !rangerServiceDefs.isEmpty()) { for (RangerServiceDef rangerServiceDef : rangerServiceDefs) { if (rangerServiceDef != null) { String serviceDef = rangerServiceDef.getName(); if (!StringUtils.isEmpty(serviceDef)) { policyFilter1.setParam("serviceType", serviceDef); PList<RangerPolicy> policiesList = getPaginatedPolicies(policyFilter1); if (policiesList != null && policiesList.getListSize() > 0) { int policyListCount = policiesList.getListSize(); if (policyListCount > 0 && policiesList.getList() != null) { List<RangerPolicy> policies = policiesList.getList(); for (RangerPolicy policy : policies) { if (policy != null) { List<RangerPolicyItem> policyItem = policy.getDenyPolicyItems(); if (policyItem != null && !policyItem.isEmpty()) { if (denyconditionsonMap.get(serviceDef) != null) { denyCount = denyconditionsonMap.get(serviceDef) + denyCount + policyItem.size(); } else { denyCount = denyCount + policyItem.size(); } } List<RangerPolicyItem> policyItemExclude = policy.getDenyExceptions(); if (policyItemExclude != null && !policyItemExclude.isEmpty()) { if (denyconditionsonMap.get(serviceDef) != null) { denyCount = denyconditionsonMap.get(serviceDef) + denyCount + policyItemExclude.size(); } else { denyCount = denyCount + policyItemExclude.size(); } } } } } } policyFilter1.removeParam("serviceType"); } denyconditionsonMap.put(serviceDef, denyCount); denyCount = 0; } } } } Gson gson = new GsonBuilder().create(); String jsonContextDenyCondtionOn = gson.toJson(denyconditionsonMap); ret = jsonContextDenyCondtionOn; } catch (Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric for denyconditions : " + e.getMessage()); } break; default: LOG.info("ServiceDBStore.getMetricByType(" + type + "):Please enter the valid arguments for Metric Calculation -type policies | audits | usergroup | services | database | contextenrichers | denyconditions"); break; } } catch(Exception e) { LOG.error("ServiceDBStore.getMetricByType(" + type + "): Error calculating Metric : "+e.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("== ServiceDBStore.getMetricByType(" + type + "): " + ret); } return ret; }  @Override void init(); @PostConstruct void initStore(); @Override RangerServiceDef createServiceDef(RangerServiceDef serviceDef); @Override RangerServiceDef updateServiceDef(RangerServiceDef serviceDef); void deleteServiceDef(Long serviceDefId, Boolean forceDelete); void deleteXXAccessTypeDef(XXAccessTypeDef xAccess); void deleteXXResourceDef(XXResourceDef xRes); @Override RangerServiceDef getServiceDef(Long id); @Override RangerServiceDef getServiceDefByName(String name); @Override RangerServiceDef getServiceDefByDisplayName(String displayName); @Override List<RangerServiceDef> getServiceDefs(SearchFilter filter); @Override PList<RangerServiceDef> getPaginatedServiceDefs(SearchFilter filter); @Override RangerService createService(RangerService service); @Override RangerService updateService(RangerService service, Map<String, Object> options); @Override void deleteService(Long id); @Override List<RangerPolicy> getPoliciesByResourceSignature(String serviceName, String policySignature, Boolean isPolicyEnabled); @Override RangerService getService(Long id); @Override RangerService getServiceByName(String name); @Override RangerService getServiceByDisplayName(String displayName); RangerService getServiceByNameForDP(String name); @Override List<RangerService> getServices(SearchFilter filter); PList<RangerService> getPaginatedServices(SearchFilter filter); @Override RangerPolicy createPolicy(RangerPolicy policy); void createOrMapLabels(XXPolicy xPolicy, Set<String> uniquePolicyLabels); @Override RangerPolicy updatePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy, RangerService service); @Override boolean policyExists(Long id); @Override RangerPolicy getPolicy(Long id); @Override List<RangerPolicy> getPolicies(SearchFilter filter); @Override Long getPolicyId(final Long serviceId, final String policyName, final Long zoneId); void getPoliciesInExcel(List<RangerPolicy> policies, HttpServletResponse response); void getPoliciesInCSV(List<RangerPolicy> policies,
			HttpServletResponse response); void getPoliciesInJson(List<RangerPolicy> policies,
			HttpServletResponse response); PList<RangerPolicy> getPaginatedPolicies(SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(Long serviceId, SearchFilter filter); List<RangerPolicy> noZoneFilter(List<RangerPolicy> servicePolicies); PList<RangerPolicy> getPaginatedServicePolicies(Long serviceId, SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(String serviceName, SearchFilter filter); PList<RangerPolicy> getPaginatedServicePolicies(String serviceName, SearchFilter filter); @Override ServicePolicies getServicePoliciesIfUpdated(String serviceName, Long lastKnownVersion, boolean needsBackwardCompatibility); @Override Long getServicePolicyVersion(String serviceName); @Override ServicePolicies getServicePolicyDeltasOrPolicies(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicyDeltas(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicies(String serviceName, Long lastKnownVersion); void createZoneDefaultPolicies(Collection<String> serviceNames, RangerSecurityZone zone); void deleteZonePolicies(Collection<String> serviceNames, Long zoneId); static void persistVersionChange(ServiceVersionUpdater serviceVersionUpdater); @Override Boolean getPopulateExistingBaseFields(); @Override void setPopulateExistingBaseFields(Boolean populateExistingBaseFields); RangerPolicy getPolicyFromEventTime(String eventTime, Long policyId); VXString getPolicyVersionList(Long policyId); RangerPolicy getPolicyForVersionNumber(Long policyId, int versionNo); @Override RangerSecurityZone getSecurityZone(Long id); @Override RangerSecurityZone getSecurityZone(String name); @Override long getPoliciesCount(final String serviceName); void putMetaDataInfo(RangerExportPolicyList rangerExportPolicyList); Map<String, String> getMapFromInputStream(InputStream mapStream); Map<String, RangerPolicy> setPolicyMapKeyValue(Map<String, RangerPolicy> policiesMap, RangerPolicy policy); Map<String, RangerPolicy> createPolicyMap(Map<String, String> zoneMappingMap, List<String> sourceZones,
			String destinationZoneName, Map<String, String> servicesMappingMap, List<String> sourceServices,
			List<String> destinationServices, RangerPolicy policy, Map<String, RangerPolicy> policiesMap); void getServiceUpgraded(); void resetPolicyUpdateLog(int retentionInDays, Integer policyChangeType); void resetTagUpdateLog(int retentionInDays, ServiceTags.TagsChangeType tagChangeType); List<String> getPolicyLabels(SearchFilter searchFilter); String getMetricByType(String type); boolean isServiceAdminUser(String serviceName, String userName); static boolean isSupportsPolicyDeltas(); static boolean isSupportsRolesDownloadByService(); @Override Map<String, String> getServiceConfigForPlugin(Long serviceId); static final String SERVICE_ADMIN_USERS; static final String CRYPT_ALGO; static final String ENCRYPT_KEY; static final String SALT; static final Integer ITERATION_COUNT; static boolean SUPPORTS_POLICY_DELTAS; static Integer RETENTION_PERIOD_IN_DAYS; static Integer TAG_RETENTION_PERIOD_IN_DAYS; static final String HIDDEN_PASSWORD_STR; static final String CONFIG_KEY_PASSWORD; static final String ACCESS_TYPE_DECRYPT_EEK; static final String ACCESS_TYPE_GENERATE_EEK; static final String ACCESS_TYPE_GET_METADATA; }
@Test public void test41getMetricByTypeusergroup() throws Exception{ VXGroupList vxGroupList = new VXGroupList(); vxGroupList.setTotalCount(4l); vxGroupList.setPageSize(1); String type = "usergroup"; VXUserList vXUserList = new VXUserList(); vXUserList.setTotalCount(4l); Mockito.when(xUserMgr.searchXGroups(Mockito.any(SearchCriteria.class) )).thenReturn(vxGroupList); Mockito.when(xUserMgr.searchXUsers(Mockito.any(SearchCriteria.class))).thenReturn(vXUserList); serviceDBStore.getMetricByType(type); }
@Test public void test42getMetricByTypeaudits() throws Exception{ String type = "audits"; Date date = new Date(); date.setYear(2018); Mockito.when(restErrorUtil.parseDate(Mockito.anyString(),Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString(), Mockito.anyString())).thenReturn(date); RangerServiceDefList svcDefList = new RangerServiceDefList(); svcDefList.setTotalCount(10l); Mockito.when(serviceDefService.searchRangerServiceDefs(Mockito.any(SearchFilter.class))).thenReturn(svcDefList); serviceDBStore.getMetricByType(type); }
@Test public void test43getMetricByTypeServices() throws Exception{ String type = "services"; RangerServiceList svcList = new RangerServiceList(); svcList.setTotalCount(10l); Mockito.when(svcService.searchRangerServices(Mockito.any(SearchFilter.class))).thenReturn(svcList); serviceDBStore.getMetricByType(type); }
@Test public void test44getMetricByTypePolicies() throws Exception{ String type = "policies"; RangerServiceList svcList = new RangerServiceList(); svcList.setTotalCount(10l); Mockito.when(svcService.searchRangerServices(Mockito.any(SearchFilter.class))).thenReturn(svcList); serviceDBStore.getMetricByType(type); }
@Test public void test45getMetricByTypeDatabase() throws Exception{ String type = "database"; Mockito.when(bizUtil.getDBVersion()).thenReturn("MYSQL"); serviceDBStore.getMetricByType(type); }
@Test public void test46getMetricByTypeContextenrichers() throws Exception{ String type = "contextenrichers"; RangerServiceDefList svcDefList = new RangerServiceDefList(); svcDefList.setTotalCount(10l); Mockito.when(serviceDefService.searchRangerServiceDefs(Mockito.any(SearchFilter.class))).thenReturn(svcDefList); serviceDBStore.getMetricByType(type); }
@Test public void test47getMetricByTypeDenyconditions() throws Exception{ String type = "denyconditions"; RangerServiceDefList svcDefList = new RangerServiceDefList(); svcDefList.setTotalCount(10l); Mockito.when(serviceDefService.searchRangerServiceDefs(Mockito.any(SearchFilter.class))).thenReturn(svcDefList); serviceDBStore.getMetricByType(type); }