boscodurai commented on a change in pull request #42: DINF-1390 Added start of implementation for ElasticSearch write/searc… URL: https://github.com/apache/ranger/pull/42#discussion_r342402735
########## File path: security-admin/src/main/java/org/apache/ranger/elasticsearch/ElasticSearchAccessAuditsService.java ########## @@ -0,0 +1,390 @@ +/* + * 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.elasticsearch; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.apache.ranger.common.*; +import org.apache.ranger.common.SearchField.DATA_TYPE; +import org.apache.ranger.common.SearchField.SEARCH_TYPE; +import org.apache.ranger.common.SortField.SORT_ORDER; +import org.apache.ranger.db.RangerDaoManager; +import org.apache.ranger.entity.XXServiceDef; +import org.apache.ranger.plugin.store.EmbeddedServiceDefsUtil; +import org.apache.ranger.view.VXAccessAudit; +import org.apache.ranger.view.VXAccessAuditList; +import org.apache.ranger.view.VXLong; +//import org.apache.solr.client.solrj.SolrClient; +//import org.apache.solr.client.solrj.response.QueryResponse; +//import org.apache.solr.common.SolrDocument; +//import org.apache.solr.common.SolrDocumentList; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.*; + +@Service +@Scope("singleton") +public class ElasticSearchAccessAuditsService { + private static final Logger LOGGER = Logger.getLogger(ElasticSearchAccessAuditsService.class); + + @Autowired + ElasticSearchMgr elasticSearchMgr; + + @Autowired + ElasticSearchUtil elasticSearchUtil; + + @Autowired + RESTErrorUtil restErrorUtil; + + @Autowired + StringUtil stringUtil; + + @Autowired + RangerDaoManager daoManager; + + private List<SortField> sortFields = new ArrayList<SortField>(); + private List<SearchField> searchFields = new ArrayList<SearchField>(); + + public ElasticSearchAccessAuditsService() { + + searchFields.add(new SearchField("id", "id", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("accessType", "access", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("aclEnforcer", "enforcer", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("agentId", "agent", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("repoName", "repo", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("sessionId", "sess", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("requestUser", "reqUser", + DATA_TYPE.STR_LIST, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("excludeUser", "exlUser", + DATA_TYPE.STR_LIST, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("requestData", "reqData", DATA_TYPE.STRING, + SEARCH_TYPE.PARTIAL)); + searchFields.add(new SearchField("resourcePath", "resource", DATA_TYPE.STRING, + SEARCH_TYPE.PARTIAL)); + searchFields.add(new SearchField("clientIP", "cliIP", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + + searchFields.add(new SearchField("auditType", "logType", + DATA_TYPE.INTEGER, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("accessResult", "result", + DATA_TYPE.INTEGER, SEARCH_TYPE.FULL)); + // searchFields.add(new SearchField("assetId", "obj.assetId", + // SearchField.DATA_TYPE.INTEGER, SearchField.SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("policyId", "policy", + DATA_TYPE.INTEGER, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("repoType", "repoType", + DATA_TYPE.INTEGER, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("-repoType", "-repoType", + DATA_TYPE.INTEGER, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("-requestUser", "-reqUser", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("resourceType", "resType", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("reason", "reason", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("action", "action", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + + searchFields.add(new SearchField("startDate", "evtTime", + DATA_TYPE.DATE, SEARCH_TYPE.GREATER_EQUAL_THAN)); + searchFields.add(new SearchField("endDate", "evtTime", DATA_TYPE.DATE, + SEARCH_TYPE.LESS_EQUAL_THAN)); + + searchFields.add(new SearchField("tags", "tags", DATA_TYPE.STRING, SEARCH_TYPE.PARTIAL)); + searchFields.add(new SearchField("cluster", "cluster", + DATA_TYPE.STRING, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("zoneName", "zoneName", + DATA_TYPE.STR_LIST, SEARCH_TYPE.FULL)); + searchFields.add(new SearchField("agentHost", "agentHost", + DATA_TYPE.STRING, SEARCH_TYPE.PARTIAL)); + + sortFields.add(new SortField("eventTime", "evtTime", true, + SORT_ORDER.DESC)); + } + + + public VXAccessAuditList searchXAccessAudits(SearchCriteria searchCriteria) { + + RestHighLevelClient client = elasticSearchMgr.getClient(); + final boolean hiveQueryVisibility = PropertiesUtil.getBooleanProperty("ranger.audit.hive.query.visibility", true); + if (client == null) { + LOGGER.warn("ElasticSearch client is null, so not running the query."); + throw restErrorUtil.createRESTException( + "Error connecting to search engine", + MessageEnums.ERROR_SYSTEM); + } + List<VXAccessAudit> xAccessAuditList = new ArrayList<VXAccessAudit>(); + Map<String, Object> paramList = searchCriteria.getParamList(); + updateUserExclusion(paramList); + + SearchResponse response; + try { + response = elasticSearchUtil.searchResources(searchCriteria, searchFields, sortFields, client); Review comment: Seems except for this, everything seems to be common between Solr and Elastic. Should consider abstracting a class with common methods? E.g. updateUserExclusion(), getExcludeUsersList(), getServiceUserList(), etc. This will be easy to maintain going forward. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
