You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@syncope.apache.org by GitBox <gi...@apache.org> on 2022/11/03 11:41:36 UTC

[GitHub] [syncope] github-code-scanning[bot] commented on a diff in pull request #387: [SYNCOPE-1696] Adding support to manage Audit entries via Elasticsearch

github-code-scanning[bot] commented on code in PR #387:
URL: https://github.com/apache/syncope/pull/387#discussion_r1012792804


##########
ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAuditConfDAO.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.syncope.core.persistence.jpa.dao;
+
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch._types.FieldSort;
+import co.elastic.clients.elasticsearch._types.SearchType;
+import co.elastic.clients.elasticsearch._types.SortOptions;
+import co.elastic.clients.elasticsearch._types.SortOrder;
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import co.elastic.clients.elasticsearch.core.CountRequest;
+import co.elastic.clients.elasticsearch.core.SearchRequest;
+import co.elastic.clients.elasticsearch.core.search.Hit;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.common.lib.audit.AuditEntry;
+import org.apache.syncope.common.lib.types.AuditElements;
+import org.apache.syncope.core.persistence.api.dao.search.OrderByClause;
+import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
+import org.apache.syncope.core.spring.security.AuthContextUtils;
+import org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils;
+import org.springframework.util.CollectionUtils;
+
+public class ElasticsearchAuditConfDAO extends JPAAuditConfDAO {
+
+    protected final ElasticsearchClient client;
+
+    protected final int indexMaxResultWindow;
+
+    public ElasticsearchAuditConfDAO(final ElasticsearchClient client, final int indexMaxResultWindow) {
+        this.client = client;
+        this.indexMaxResultWindow = indexMaxResultWindow;
+    }
+
+    protected Query getQuery(
+            final String entityKey,
+            final AuditElements.EventCategoryType type,
+            final String category,
+            final String subcategory,
+            final List<String> events,
+            final AuditElements.Result result) {
+
+        List<Query> queries = new ArrayList<>();
+
+        if (entityKey != null) {
+            queries.add(new Query.Builder().
+                    multiMatch(QueryBuilders.multiMatch().
+                            fields("message.before", "message.inputs", "message.output", "message.throwable").
+                            query(entityKey).build()).build());
+        }
+
+        if (type != null) {
+            queries.add(new Query.Builder().
+                    term(QueryBuilders.term().field("message.logger.type").value(type.name()).build()).
+                    build());
+        }
+
+        if (StringUtils.isNotBlank(category)) {
+            queries.add(new Query.Builder().
+                    term(QueryBuilders.term().field("message.logger.category").value(category).build()).
+                    build());
+        }
+
+        if (StringUtils.isNotBlank(subcategory)) {
+            queries.add(new Query.Builder().
+                    term(QueryBuilders.term().field("message.logger.subcategory").value(subcategory).build()).
+                    build());
+        }
+
+        List<Query> eventQueries = events.stream().map(event -> new Query.Builder().
+                term(QueryBuilders.term().field("message.logger.event").value(event).build()).
+                build()).
+                collect(Collectors.toList());
+        if (!eventQueries.isEmpty()) {
+            queries.add(new Query.Builder().disMax(QueryBuilders.disMax().queries(eventQueries).build()).build());
+        }
+
+        if (result != null) {
+            queries.add(new Query.Builder().
+                    term(QueryBuilders.term().field("message.logger.result").value(result.name()).build()).
+                    build());
+        }
+
+        return new Query.Builder().bool(QueryBuilders.bool().must(queries).build()).build();
+    }
+
+    @Override
+    public int countEntries(
+            final String entityKey,
+            final AuditElements.EventCategoryType type,
+            final String category,
+            final String subcategory,
+            final List<String> events,
+            final AuditElements.Result result) {
+
+        CountRequest request = new CountRequest.Builder().
+                index(ElasticsearchUtils.getAuditIndex(AuthContextUtils.getDomain())).
+                query(getQuery(entityKey, type, category, subcategory, events, result)).
+                build();
+        try {
+            return (int) client.count(request).count();
+        } catch (IOException e) {
+            LOG.error("Search error", e);
+            return 0;
+        }
+    }
+
+    protected List<SortOptions> sortBuilders(final List<OrderByClause> orderBy) {
+        return orderBy.stream().map(clause -> {
+            String sortField = clause.getField();
+            if ("EVENT_DATE".equalsIgnoreCase(sortField)) {
+                sortField = "message.date";
+            }
+
+            return new SortOptions.Builder().field(
+                    new FieldSort.Builder().
+                            field(sortField).
+                            order(clause.getDirection() == OrderByClause.Direction.ASC
+                                    ? SortOrder.Asc : SortOrder.Desc).
+                            build()).
+                    build();
+        }).collect(Collectors.toList());
+    }
+
+    @Override
+    public List<AuditEntry> searchEntries(
+            final String entityKey,
+            final int page,
+            final int itemsPerPage,
+            final AuditElements.EventCategoryType type,
+            final String category,
+            final String subcategory,
+            final List<String> events,
+            final AuditElements.Result result,
+            final List<OrderByClause> orderBy) {
+
+        SearchRequest request = new SearchRequest.Builder().
+                index(ElasticsearchUtils.getAuditIndex(AuthContextUtils.getDomain())).
+                searchType(SearchType.QueryThenFetch).
+                query(getQuery(entityKey, type, category, subcategory, events, result)).
+                from(itemsPerPage * (page <= 0 ? 0 : page - 1)).
+                size(itemsPerPage < 0 ? indexMaxResultWindow : itemsPerPage).
+                sort(sortBuilders(orderBy)).
+                build();
+
+        @SuppressWarnings("rawtypes")
+        List<Hit<Map>> esResult = null;
+        try {
+            esResult = client.search(request, Map.class).hits().hits();
+        } catch (Exception e) {
+            LOG.error("While searching in Elasticsearch", e);
+        }
+
+        return CollectionUtils.isEmpty(esResult)
+                ? List.of()
+                : esResult.stream().

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [esResult](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1194)



##########
ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java:
##########
@@ -254,4 +223,27 @@
     protected void customizeDocument(final Map<String, Object> builder, final User user, final String domain)
             throws IOException {
     }
+
+    public Map<String, Object> document(
+            final long instant,
+            final JsonNode message,
+            final String domain) throws IOException {
+
+        Map<String, Object> builder = new HashMap<>();
+
+        builder.put("instant", instant);
+        builder.put("message", message);
+
+        customizeDocument(builder, instant, message, domain);
+
+        return builder;
+    }
+
+    protected void customizeDocument(
+            final Map<String, Object> builder,

Review Comment:
   ## Useless parameter
   
   The parameter 'builder' is never used.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1190)



##########
ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java:
##########
@@ -254,4 +223,27 @@
     protected void customizeDocument(final Map<String, Object> builder, final User user, final String domain)
             throws IOException {
     }
+
+    public Map<String, Object> document(
+            final long instant,
+            final JsonNode message,
+            final String domain) throws IOException {
+
+        Map<String, Object> builder = new HashMap<>();
+
+        builder.put("instant", instant);
+        builder.put("message", message);
+
+        customizeDocument(builder, instant, message, domain);
+
+        return builder;
+    }
+
+    protected void customizeDocument(
+            final Map<String, Object> builder,
+            final long instant,
+            final JsonNode message,

Review Comment:
   ## Useless parameter
   
   The parameter 'message' is never used.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1192)



##########
ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java:
##########
@@ -254,4 +223,27 @@
     protected void customizeDocument(final Map<String, Object> builder, final User user, final String domain)
             throws IOException {
     }
+
+    public Map<String, Object> document(
+            final long instant,
+            final JsonNode message,
+            final String domain) throws IOException {
+
+        Map<String, Object> builder = new HashMap<>();
+
+        builder.put("instant", instant);
+        builder.put("message", message);
+
+        customizeDocument(builder, instant, message, domain);
+
+        return builder;
+    }
+
+    protected void customizeDocument(
+            final Map<String, Object> builder,
+            final long instant,

Review Comment:
   ## Useless parameter
   
   The parameter 'instant' is never used.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1191)



##########
ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java:
##########
@@ -254,4 +223,27 @@
     protected void customizeDocument(final Map<String, Object> builder, final User user, final String domain)
             throws IOException {
     }
+
+    public Map<String, Object> document(
+            final long instant,
+            final JsonNode message,
+            final String domain) throws IOException {
+
+        Map<String, Object> builder = new HashMap<>();
+
+        builder.put("instant", instant);
+        builder.put("message", message);
+
+        customizeDocument(builder, instant, message, domain);
+
+        return builder;
+    }
+
+    protected void customizeDocument(
+            final Map<String, Object> builder,
+            final long instant,
+            final JsonNode message,
+            final String domain)

Review Comment:
   ## Useless parameter
   
   The parameter 'domain' is never used.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1193)



##########
common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/info/NumbersInfo.java:
##########
@@ -246,16 +244,12 @@
~        return any2ByRealm;
~    }
~
~    public Map<String, Boolean> getConfCompleteness() {
         return confCompleteness;
     }
 
-    public TaskExecutorInfo getAsyncConnectorExecutor() {
-        return asyncConnectorExecutor;
-    }
-
-    public TaskExecutorInfo getPropagationTaskExecutor() {
-        return propagationTaskExecutor;
+    public Map<String, TaskExecutorInfo> getTaskExecutorInfos() {

Review Comment:
   ## Exposing internal representation
   
   getTaskExecutorInfos exposes the internal representation stored in field taskExecutorInfos. The value may be modified [after this call to getTaskExecutorInfos](1).
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1195)



-- 
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: dev-unsubscribe@syncope.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org