Commit b714fe00 by apoorvnaik Committed by Madhan Neethiraj

ATLAS-2025: Validation of classification/type in basic search query

parent cd49be50
...@@ -36,7 +36,7 @@ public enum AtlasErrorCode { ...@@ -36,7 +36,7 @@ public enum AtlasErrorCode {
CONSTRAINT_MISSING_PARAMS(400, "ATLAS-400-00-007", "{0}.{1} : invalid constraint. missing parameter {2} in {3}. params={4}"), CONSTRAINT_MISSING_PARAMS(400, "ATLAS-400-00-007", "{0}.{1} : invalid constraint. missing parameter {2} in {3}. params={4}"),
CONSTRAINT_NOT_EXIST(400, "ATLAS-400-00-008", "{0}.{1} : invalid constraint. {2} {3}.{4} does not exist"), CONSTRAINT_NOT_EXIST(400, "ATLAS-400-00-008", "{0}.{1} : invalid constraint. {2} {3}.{4} does not exist"),
CONSTRAINT_NOT_MATCHED(400, "ATLAS-400-00-009", "{0}.{1} : invalid constraint. Data type of {2} {3}.{4} should be {5}, but found {6}"), CONSTRAINT_NOT_MATCHED(400, "ATLAS-400-00-009", "{0}.{1} : invalid constraint. Data type of {2} {3}.{4} should be {5}, but found {6}"),
UNKNOWN_TYPENAME(400, "ATLAS-400-00-00A", "{0}: Unknown typename"), UNKNOWN_TYPENAME(400, "ATLAS-400-00-00A", "{0}: Unknown/invalid typename"),
CONSTRAINT_NOT_SUPPORTED_ON_MAP_TYPE(400, "ATLAS-400-00-00B", "{0}.{1} : constraints not supported on map type {2}"), CONSTRAINT_NOT_SUPPORTED_ON_MAP_TYPE(400, "ATLAS-400-00-00B", "{0}.{1} : constraints not supported on map type {2}"),
CANNOT_ADD_MANDATORY_ATTRIBUTE(400, "ATLAS-400-00-00C", "{0}.{1} : can not add mandatory attribute"), CANNOT_ADD_MANDATORY_ATTRIBUTE(400, "ATLAS-400-00-00C", "{0}.{1} : can not add mandatory attribute"),
ATTRIBUTE_DELETION_NOT_SUPPORTED(400, "ATLAS-400-00-00D", "{0}.{1} : attribute delete not supported"), ATTRIBUTE_DELETION_NOT_SUPPORTED(400, "ATLAS-400-00-00D", "{0}.{1} : attribute delete not supported"),
...@@ -90,6 +90,8 @@ public enum AtlasErrorCode { ...@@ -90,6 +90,8 @@ public enum AtlasErrorCode {
RELATIONSHIPDEF_NOT_DEFINED(400, "ATLAS-400-00-043", "No relationshipDef defined between {0} and {1} on attribute: {2}"), RELATIONSHIPDEF_NOT_DEFINED(400, "ATLAS-400-00-043", "No relationshipDef defined between {0} and {1} on attribute: {2}"),
RELATIONSHIPDEF_INVALID(400, "ATLAS-400-00-044", "Invalid relationshipDef: {0}"), RELATIONSHIPDEF_INVALID(400, "ATLAS-400-00-044", "Invalid relationshipDef: {0}"),
RELATIONSHIP_INVALID_ENDTYPE(400, "ATLAS-400-00-045", "Invalid entity-type for relationship attribute ‘{0}’: entity specified (guid={1}) is of type ‘{2}’, but expected type is ‘{3}’"), RELATIONSHIP_INVALID_ENDTYPE(400, "ATLAS-400-00-045", "Invalid entity-type for relationship attribute ‘{0}’: entity specified (guid={1}) is of type ‘{2}’, but expected type is ‘{3}’"),
UNKNOWN_CLASSIFICATION(400, "ATLAS-400-00-046", "{0}: Unknown/invalid classification"),
INVALID_SEARCH_PARAMS(400, "ATLAS-400-00-047", "No search parameter was found. One of the following MUST be specified in the request; typeName, classification or queryText"),
// All Not found enums go here // All Not found enums go here
TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-001", "Given typename {0} was invalid"), TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-001", "Given typename {0} was invalid"),
TYPE_GUID_NOT_FOUND(404, "ATLAS-404-00-002", "Given type guid {0} was invalid"), TYPE_GUID_NOT_FOUND(404, "ATLAS-404-00-002", "Given type guid {0} was invalid"),
...@@ -104,6 +106,7 @@ public enum AtlasErrorCode { ...@@ -104,6 +106,7 @@ public enum AtlasErrorCode {
RELATIONSHIP_GUID_NOT_FOUND(404, "ATLAS-404-00-00C", "Given relationship guid {0} is invalid/not found"), RELATIONSHIP_GUID_NOT_FOUND(404, "ATLAS-404-00-00C", "Given relationship guid {0} is invalid/not found"),
RELATIONSHIP_CRUD_INVALID_PARAMS(404, "ATLAS-404-00-00D", "Invalid relationship creation/updation parameters passed : {0}"), RELATIONSHIP_CRUD_INVALID_PARAMS(404, "ATLAS-404-00-00D", "Invalid relationship creation/updation parameters passed : {0}"),
RELATIONSHIPDEF_END_TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-00E", "RelationshipDef {0} endDef typename {0} cannot be found"), RELATIONSHIPDEF_END_TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-00E", "RelationshipDef {0} endDef typename {0} cannot be found"),
// All data conflict errors go here // All data conflict errors go here
TYPE_ALREADY_EXISTS(409, "ATLAS-409-00-001", "Given type {0} already exists"), TYPE_ALREADY_EXISTS(409, "ATLAS-409-00-001", "Given type {0} already exists"),
TYPE_HAS_REFERENCES(409, "ATLAS-409-00-002", "Given type {0} has references"), TYPE_HAS_REFERENCES(409, "ATLAS-409-00-002", "Given type {0} has references"),
......
...@@ -427,8 +427,10 @@ public class EntityDiscoveryService implements AtlasDiscoveryService { ...@@ -427,8 +427,10 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
resultAttributes.addAll(context.getEntityAttributes()); resultAttributes.addAll(context.getEntityAttributes());
} }
AtlasEntityType entityType = context.getEntityType();
if (entityType != null) {
for (String resultAttribute : resultAttributes) { for (String resultAttribute : resultAttributes) {
AtlasAttribute attribute = context.getEntityType().getAttribute(resultAttribute); AtlasAttribute attribute = entityType.getAttribute(resultAttribute);
if (attribute != null) { if (attribute != null) {
AtlasType attributeType = attribute.getAttributeType(); AtlasType attributeType = attribute.getAttributeType();
...@@ -442,6 +444,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService { ...@@ -442,6 +444,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
} }
} }
} }
}
for (AtlasVertex atlasVertex : resultList) { for (AtlasVertex atlasVertex : resultList) {
AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(atlasVertex, resultAttributes); AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(atlasVertex, resultAttributes);
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
package org.apache.atlas.discovery; package org.apache.atlas.discovery;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.discovery.SearchParameters; import org.apache.atlas.model.discovery.SearchParameters;
import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria; import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
...@@ -30,7 +32,11 @@ import org.apache.commons.lang3.StringUtils; ...@@ -30,7 +32,11 @@ import org.apache.commons.lang3.StringUtils;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
/*
* Search context captures elements required for performing a basic search
* For every search request the search context will determine the execution sequence of the search processor(s) and the
* possible chaining of processor(s)
*/
public class SearchContext { public class SearchContext {
private final SearchParameters searchParameters; private final SearchParameters searchParameters;
private final AtlasTypeRegistry typeRegistry; private final AtlasTypeRegistry typeRegistry;
...@@ -42,16 +48,26 @@ public class SearchContext { ...@@ -42,16 +48,26 @@ public class SearchContext {
private SearchProcessor searchProcessor; private SearchProcessor searchProcessor;
private boolean terminateSearch = false; private boolean terminateSearch = false;
public SearchContext(SearchParameters searchParameters, AtlasTypeRegistry typeRegistry, AtlasGraph graph, Set<String> indexedKeys) { public SearchContext(SearchParameters searchParameters, AtlasTypeRegistry typeRegistry, AtlasGraph graph, Set<String> indexedKeys) throws AtlasBaseException {
this.searchParameters = searchParameters; this.searchParameters = searchParameters;
this.typeRegistry = typeRegistry; this.typeRegistry = typeRegistry;
this.graph = graph; this.graph = graph;
this.indexedKeys = indexedKeys; this.indexedKeys = indexedKeys;
this.entityAttributes = new HashSet<>(); entityAttributes = new HashSet<>();
this.entityType = typeRegistry.getEntityTypeByName(searchParameters.getTypeName()); entityType = typeRegistry.getEntityTypeByName(searchParameters.getTypeName());
this.classificationType = typeRegistry.getClassificationTypeByName(searchParameters.getClassification()); classificationType = typeRegistry.getClassificationTypeByName(searchParameters.getClassification());
// Validate if the type name exists
if (StringUtils.isNotEmpty(searchParameters.getTypeName()) && entityType == null) {
throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_TYPENAME, searchParameters.getTypeName());
}
// Validate if the classification exists
if (StringUtils.isNotEmpty(searchParameters.getClassification()) && classificationType == null) {
throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_CLASSIFICATION, searchParameters.getClassification());
}
if (needFullTextrocessor()) { if (needFullTextProcessor()) {
addProcessor(new FullTextSearchProcessor(this)); addProcessor(new FullTextSearchProcessor(this));
} }
...@@ -80,7 +96,7 @@ public class SearchContext { ...@@ -80,7 +96,7 @@ public class SearchContext {
public SearchProcessor getSearchProcessor() { return searchProcessor; } public SearchProcessor getSearchProcessor() { return searchProcessor; }
public boolean terminateSearch() { return this.terminateSearch; } public boolean terminateSearch() { return terminateSearch; }
public void terminateSearch(boolean terminateSearch) { this.terminateSearch = terminateSearch; } public void terminateSearch(boolean terminateSearch) { this.terminateSearch = terminateSearch; }
...@@ -103,7 +119,7 @@ public class SearchContext { ...@@ -103,7 +119,7 @@ public class SearchContext {
return toString(new StringBuilder()).toString(); return toString(new StringBuilder()).toString();
} }
boolean needFullTextrocessor() { boolean needFullTextProcessor() {
return StringUtils.isNotEmpty(searchParameters.getQuery()); return StringUtils.isNotEmpty(searchParameters.getQuery());
} }
...@@ -121,10 +137,10 @@ public class SearchContext { ...@@ -121,10 +137,10 @@ public class SearchContext {
} }
private void addProcessor(SearchProcessor processor) { private void addProcessor(SearchProcessor processor) {
if (this.searchProcessor == null) { if (searchProcessor == null) {
this.searchProcessor = processor; searchProcessor = processor;
} else { } else {
this.searchProcessor.addProcessor(processor); searchProcessor.addProcessor(processor);
} }
} }
} }
...@@ -213,7 +213,7 @@ public class DiscoveryREST { ...@@ -213,7 +213,7 @@ public class DiscoveryREST {
if (StringUtils.isEmpty(attrName) && StringUtils.isEmpty(attrValuePrefix)) { if (StringUtils.isEmpty(attrName) && StringUtils.isEmpty(attrValuePrefix)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS,
String.format("attrName : {0}, attrValue: {1} for attribute search.", attrName, attrValuePrefix)); String.format("attrName : %s, attrValue: %s for attribute search.", attrName, attrValuePrefix));
} }
return atlasDiscoveryService.searchUsingBasicQuery(null, typeName, null, attrName, attrValuePrefix, true, limit, offset); return atlasDiscoveryService.searchUsingBasicQuery(null, typeName, null, attrName, attrValuePrefix, true, limit, offset);
...@@ -256,6 +256,10 @@ public class DiscoveryREST { ...@@ -256,6 +256,10 @@ public class DiscoveryREST {
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "TagFilters specified without tag name"); throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "TagFilters specified without tag name");
} }
if (StringUtils.isEmpty(parameters.getTypeName()) && StringUtils.isEmpty(parameters.getClassification()) && StringUtils.isEmpty(parameters.getQuery())) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_SEARCH_PARAMS);
}
return atlasDiscoveryService.searchWithParameters(parameters); return atlasDiscoveryService.searchWithParameters(parameters);
} finally { } finally {
AtlasPerfTracer.log(perf); AtlasPerfTracer.log(perf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment