diff options
author | 2017-07-20 13:35:06 -0400 | |
---|---|---|
committer | 2017-07-20 13:36:12 -0400 | |
commit | 629c4dc6e90840f164af0091eb00c4bcf4033f83 (patch) | |
tree | 5ebf43f889dd95c0fc7eb7d91552f4790bec8b75 /src/main/java/org/openecomp | |
parent | 895cd72a962de1868151288d4df1510db5280fab (diff) |
Reject doc create requests if index does not exist
[AAI-62] Search Data Service should not implicitly create indexes on
document write...
Search Data Service will now explicitly block a document create or
update if the associated index does not already exist in the document
store.
Change-Id: Ie96364da754aa6a2cb554b06f62a7a647181bcce
Signed-off-by: gfraboni <gino.fraboni@amdocs.com>
Diffstat (limited to 'src/main/java/org/openecomp')
3 files changed, 78 insertions, 10 deletions
diff --git a/src/main/java/org/openecomp/sa/rest/DocumentApi.java b/src/main/java/org/openecomp/sa/rest/DocumentApi.java index e3c15a5..7e34d86 100644 --- a/src/main/java/org/openecomp/sa/rest/DocumentApi.java +++ b/src/main/java/org/openecomp/sa/rest/DocumentApi.java @@ -49,7 +49,8 @@ import javax.ws.rs.core.Response.Status; public class DocumentApi { private static final String REQUEST_HEADER_RESOURCE_VERSION = "If-Match"; private static final String RESPONSE_HEADER_RESOURCE_VERSION = "ETag"; - + private static final String REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION = "X-CreateIndex"; + protected SearchServiceApi searchService = null; private Logger logger = LoggerFactory.getInstance().getLogger(DocumentApi.class.getName()); @@ -92,7 +93,7 @@ public class DocumentApi { DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl(); document.setContent(content); - DocumentOperationResult result = documentStore.createDocument(index, document); + DocumentOperationResult result = documentStore.createDocument(index, document, implicitlyCreateIndex(headers)); String output = null; if (result.getResultCode() >= 200 && result.getResultCode() <= 299) { output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getDocument()); @@ -157,9 +158,9 @@ public class DocumentApi { DocumentOperationResult result = null; if (resourceVersion == null) { - result = documentStore.createDocument(index, document); + result = documentStore.createDocument(index, document, implicitlyCreateIndex(headers)); } else { - result = documentStore.updateDocument(index, document); + result = documentStore.updateDocument(index, document, implicitlyCreateIndex(headers)); } String output = null; @@ -463,6 +464,31 @@ public class DocumentApi { } } + + /** + * Checks the supplied HTTP headers to see if we should allow the underlying document + * store to implicitly create the index referenced in a document PUT or POST if it + * does not already exist in the data store. + * + * @param headers - The HTTP headers to examine. + * + * @return - true if the headers indicate that missing indices should be implicitly created, + * false otherwise. + */ + private boolean implicitlyCreateIndex(HttpHeaders headers) { + + boolean createIndexIfNotPresent = false; + String implicitIndexCreationHeader = + headers.getRequestHeaders().getFirst(REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION); + + if( (implicitIndexCreationHeader != null) && (implicitIndexCreationHeader.equals("true")) ) { + createIndexIfNotPresent = true; + } + + return createIndexIfNotPresent; + } + + private String prepareOutput(ObjectMapper mapper, SearchOperationResult result) throws JsonProcessingException { StringBuffer output = new StringBuffer(); diff --git a/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java b/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java index a396516..e8dc384 100644 --- a/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java +++ b/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java @@ -39,11 +39,13 @@ public interface DocumentStoreInterface { public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException; - public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document) - throws DocumentStoreOperationException; + public DocumentOperationResult createDocument(String indexName, + DocumentStoreDataEntity document, + boolean allowImplicitIndexCreation) throws DocumentStoreOperationException; - public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document) - throws DocumentStoreOperationException; + public DocumentOperationResult updateDocument(String indexName, + DocumentStoreDataEntity document, + boolean allowImplicitIndexCreation) throws DocumentStoreOperationException; public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document) throws DocumentStoreOperationException; diff --git a/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java b/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java index 0e9ff8b..371a483 100644 --- a/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java +++ b/src/main/java/org/openecomp/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java @@ -365,8 +365,28 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } @Override - public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document) + public DocumentOperationResult createDocument(String indexName, + DocumentStoreDataEntity document, + boolean allowImplicitIndexCreation) throws DocumentStoreOperationException { + + if(!allowImplicitIndexCreation) { + + // Before we do anything, make sure that the specified index actually exists in the + // document store - we don't want to rely on ElasticSearch to fail the document + // create because it could be configured to implicitly create a non-existent index, + // which can lead to hard-to-debug behaviour with queries down the road. + OperationResult indexExistsResult = checkIndexExistence(indexName); + if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) { + + DocumentOperationResult opResult = new DocumentOperationResult(); + opResult.setResultCode(Status.NOT_FOUND.getStatusCode()); + opResult.setResult("Document Index '" + indexName + "' does not exist."); + opResult.setFailureCause("Document Index '" + indexName + "' does not exist."); + return opResult; + } + } + if (document.getId() == null || document.getId().isEmpty()) { return createDocumentWithoutId(indexName, document); } else { @@ -531,8 +551,28 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } @Override - public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document) + public DocumentOperationResult updateDocument(String indexName, + DocumentStoreDataEntity document, + boolean allowImplicitIndexCreation) throws DocumentStoreOperationException { + + if(!allowImplicitIndexCreation) { + + // Before we do anything, make sure that the specified index actually exists in the + // document store - we don't want to rely on ElasticSearch to fail the document + // create because it could be configured to implicitly create a non-existent index, + // which can lead to hard-to-debug behaviour with queries down the road. + OperationResult indexExistsResult = checkIndexExistence(indexName); + if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) { + + DocumentOperationResult opResult = new DocumentOperationResult(); + opResult.setResultCode(Status.NOT_FOUND.getStatusCode()); + opResult.setResult("Document Index '" + indexName + "' does not exist."); + opResult.setFailureCause("Document Index '" + indexName + "' does not exist."); + return opResult; + } + } + DocumentOperationResult opResult = new DocumentOperationResult(); // Initialize operation result with a failure codes / fault string |