RakeshGuptaDev commented on code in PR #1251: URL: https://github.com/apache/ranger/pull/1251#discussion_r4092026299
########## security-admin/src/main/java/org/apache/ranger/audit/metrics/AuditMetricsHelper.java: ########## @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.audit.metrics; + +import org.apache.commons.lang3.StringUtils; +import org.apache.ranger.db.RangerDaoManager; +import org.apache.ranger.entity.XXService; +import org.apache.ranger.entity.XXServiceDef; +import org.apache.ranger.plugin.model.RangerAuditMetrics; +import org.apache.ranger.plugin.model.RangerAuditMetricsByDays; +import org.apache.ranger.plugin.model.RangerAuditMetricsByHours; +import org.apache.ranger.plugin.util.SearchFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.time.DateTimeException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeParseException; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@Component +public class AuditMetricsHelper { + private static final Logger LOGGER = LoggerFactory.getLogger(AuditMetricsHelper.class); + + public static final long MISSING_REPO_TYPE_SENTINEL = -1L; + + private final Map<String, Long> repoTypeByServiceType = new ConcurrentHashMap<>(); + + @Autowired + RangerDaoManager daoManager; + + public SearchFilter buildSearchFilter(String serviceType, String serviceName, String appId, String clusterName, String clientIP) { + SearchFilter filter = new SearchFilter(); + + filter.setParam(SearchFilter.SERVICE_TYPE, serviceType); + filter.setParam(SearchFilter.SERVICE_NAME, serviceName); + filter.setParam(SearchFilter.PLUGIN_APP_ID, appId); + filter.setParam(SearchFilter.CLUSTER_NAME, clusterName); + filter.setParam(SearchFilter.CLIENT_IP, clientIP); + + return filter; + } + + public FilterParams getFilterParams(SearchFilter filter) { + FilterParams params = new FilterParams(); + + if (filter != null) { + params.serviceType = filter.getParam(SearchFilter.SERVICE_TYPE); + params.serviceName = filter.getParam(SearchFilter.SERVICE_NAME); + params.appId = filter.getParam(SearchFilter.PLUGIN_APP_ID); + params.clusterName = filter.getParam(SearchFilter.CLUSTER_NAME); + params.clientIP = filter.getParam(SearchFilter.CLIENT_IP); + } + + return params; + } + + public RangerAuditMetrics buildAuditMetrics(String serviceType, String serviceName, String appId, String clusterName, String clientIP, long count) { + RangerAuditMetrics metric = new RangerAuditMetrics(); + + metric.setServiceName(serviceName); + metric.setServiceType(resolveServiceType(serviceType, serviceName)); + metric.setAppId(appId); + metric.setClusterName(clusterName); + metric.setClientIP(clientIP); + metric.setNumberOfAudits(count); + + Long serviceId = resolveServiceId(serviceName); + + if (serviceId != null) { + metric.setId(serviceId); + } + + return metric; + } + + public RangerAuditMetricsByDays buildAuditMetricsByDays(FilterParams params, Long auditDate, long count) { + return new RangerAuditMetricsByDays(params.serviceType, params.serviceName, params.appId, params.clusterName, params.clientIP, auditDate, count); + } + + public RangerAuditMetricsByHours buildAuditMetricsByHours(FilterParams params, int hour, long count) { + return new RangerAuditMetricsByHours(params.serviceType, params.serviceName, params.appId, params.clusterName, params.clientIP, hour, count); + } + + public Long resolveServiceId(String serviceName) { + Long ret = null; + + if (StringUtils.isNotBlank(serviceName) && daoManager != null && daoManager.getXXService() != null) { + XXService service = daoManager.getXXService().findByName(serviceName); + + if (service != null) { + ret = service.getId(); + } + } + + return ret; + } + + public String resolveServiceType(XXService service) { + String ret = null; + + if (service != null && daoManager != null && daoManager.getXXServiceDef() != null) { + XXServiceDef serviceDef = daoManager.getXXServiceDef().getById(service.getType()); + + if (serviceDef != null) { + ret = serviceDef.getName(); + } + } + + return ret; + } + + public String resolveServiceType(String serviceType, String serviceName) { + String ret = serviceType; + + if (StringUtils.isBlank(ret)) { + if (StringUtils.isNotBlank(serviceName) && daoManager != null && daoManager.getXXService() != null) { + XXService service = daoManager.getXXService().findByName(serviceName); + ret = resolveServiceType(service); + } else { + ret = null; + } + } + + return ret; + } + + public long resolveRepoType(String serviceType) { + if (StringUtils.isBlank(serviceType)) { + return MISSING_REPO_TYPE_SENTINEL; Review Comment: Thanks for the suggestion. Updated resolveRepoType() to use long ret = MISSING_REPO_TYPE_SENTINEL and return ret, consistent with the other methods in AuditMetricsHelper. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
