Skip to content

Commit

Permalink
Bug fix: Fixed incorrect logic to determine if an index should be que…
Browse files Browse the repository at this point in the history
…ried. This was causing some jobs due to fail by submitting an empty query to an index that should not have been queried.
  • Loading branch information
davemoore- committed Sep 9, 2019
1 parent 52d157a commit 431e2d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Once you have installed Elasticsearch, you can install zentity from a remote URL

Example:

`elasticsearch-plugin install https://zentity.io/releases/zentity-1.4.1-elasticsearch-7.3.1.zip`
`elasticsearch-plugin install https://zentity.io/releases/zentity-1.4.2-beta1-elasticsearch-7.3.1.zip`

Read the [installation](https://zentity.io/docs/installation) docs for more details.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<zentity.author>Dave Moore</zentity.author>
<zentity.classname>org.elasticsearch.plugin.zentity.ZentityPlugin</zentity.classname>
<zentity.website>https://zentity.io</zentity.website>
<zentity.version>1.4.1</zentity.version>
<zentity.version>1.4.2-beta1</zentity.version>
<!-- dependency versions -->
<elasticsearch.version>7.3.1</elasticsearch.version>
<jackson.core.version>2.9.9</jackson.core.version>
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/io/zentity/resolution/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,16 @@ public Job(NodeClient client) {
this.client = client;
}

public static String serializeElasticsearchException(ElasticsearchException e) throws IOException {
String cause = Strings.toString(e.toXContent(jsonBuilder().startObject(), ToXContent.EMPTY_PARAMS).endObject());
return "{\"error\":{\"root_cause\":[" + cause + "],\"type\":\"" + ElasticsearchException.getExceptionName(e) + "\",\"reason\":\"" + e.getMessage() + "\"},\"status\":" + e.status().getStatus() + "}";
public static String serializeException(Exception e) throws IOException {
String serialized;
if (e instanceof ElasticsearchException) {
ElasticsearchException ee = (ElasticsearchException) e;
String cause = Strings.toString(ee.toXContent(jsonBuilder().startObject(), ToXContent.EMPTY_PARAMS).endObject());
serialized = "{\"error\":{\"root_cause\":[" + cause + "],\"type\":\"" + ElasticsearchException.getExceptionName(ee) + "\",\"reason\":\"" + e.getMessage() + "\"},\"status\":" + ee.status().getStatus() + "}";
} else {
serialized = "{\"error\":{\"type\":\"" + e.getClass() + "\",\"reason\":\"" + e.getMessage() + "\"}}";
}
return serialized;
}

public static String serializeLoggedQuery(Input input, int _hop, int _query, String indexName, String request, String response, List<String> resolvers, TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped, List<String> termResolvers, TreeMap<String, TreeMap> termResolversFilterTree) throws JsonProcessingException {
Expand Down Expand Up @@ -645,8 +652,6 @@ private void traverse() throws IOException, ValidationException {
if (!this.docIds.containsKey(indexName))
this.docIds.put(indexName, new TreeSet<>());

boolean filterIds = this.hop == 0 && this.input().ids().containsKey(indexName) && !this.input().ids().get(indexName).isEmpty();

// "_explanation" uses named queries, and each value of the "_name" fields must be unique.
// Use a counter to prepend a unique and deterministic identifier for each "_name" field in the query.
AtomicInteger _nameIdCounter = new AtomicInteger();
Expand All @@ -656,7 +661,12 @@ private void traverse() throws IOException, ValidationException {
for (String resolverName : this.input.model().resolvers().keySet())
if (canQuery(this.input.model(), indexName, resolverName, this.attributes))
resolvers.add(resolverName);
if (resolvers.size() == 0 && !filterIds && (this.hop == 0 && this.input.terms().isEmpty()))

// Determine if we can query this index.
boolean canQueryIds = this.hop == 0 && this.input().ids().containsKey(indexName) && !this.input().ids().get(indexName).isEmpty();
boolean canQueryTerms = this.hop == 0 && !this.input.terms().isEmpty();
boolean canQueryAttributes = resolvers.size() > 0;
if (!canQueryAttributes && !canQueryIds && !canQueryTerms)
continue;

// Construct query for this index.
Expand Down Expand Up @@ -702,7 +712,7 @@ else if (size == 1)

// Construct the "ids" clause if this is the first hop and if any ids are specified for this index.
String idsClause = "";
if (filterIds) {
if (canQueryIds) {
Set<String> ids = this.input().ids().get(indexName);
idsClause = "{\"bool\":{\"filter\":[{\"ids\":{\"values\":[" + String.join(",", ids) + "]}}]}}";
}
Expand Down Expand Up @@ -782,7 +792,7 @@ else if (parentResolverClauses.size() == 1)
// unlike structured attribute search where the attributes are assumed be known.
List<String> termResolvers = new ArrayList<>();
TreeMap<String, TreeMap> termResolversFilterTree = new TreeMap<>();
if (this.hop == 0 && !this.input.terms().isEmpty()) {
if (canQueryTerms) {
String termResolversClause = "";

// Get the names of each attribute of each in-scope resolver.
Expand Down Expand Up @@ -986,15 +996,15 @@ else if (!resolversClause.isEmpty())

// Submit query to Elasticsearch.
SearchResponse response = null;
ElasticsearchException responseError = null;
Exception responseError = null;
boolean fatalError = false;
try {
response = this.search(indexName, query);
} catch (IndexNotFoundException e) {
// Don't fail the job if an index was missing.
this.missingIndices.add(e.getIndex().getName());
responseError = e;
} catch (ElasticsearchException e) {
} catch (Exception e) {
// Fail the job for any other error.
fatalError = true;
responseError = e;
Expand All @@ -1018,7 +1028,7 @@ else if (!resolversClause.isEmpty())
}
responseString = responseDataCopyObj.toString();
} else {
responseString = serializeElasticsearchException(responseError);
responseString = serializeException(responseError);
}
String logged = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, responseString, resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
this.queries.add(logged);
Expand All @@ -1027,7 +1037,7 @@ else if (!resolversClause.isEmpty())
// Stop traversing if there was an error not due to a missing index.
// Include the logged query in the response.
if (fatalError) {
this.error = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, serializeElasticsearchException(responseError), resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
this.error = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, serializeException(responseError), resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
return;
}

Expand Down

0 comments on commit 431e2d8

Please sign in to comment.