Merge "Junits for ONAP-PDP module"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / elk / client / ElkConnectorImpl.java
index 88f9e2c..d87e75f 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -101,6 +101,9 @@ public class ElkConnectorImpl implements ElkConnector{
                        throw e;
                }
        }
+       private boolean isAlphaNumeric(String query){
+               return query.matches("[a-zA-Z_0-9]+");
+       }
 
        @Override
        public JestResult search(PolicyIndexType type, String text) throws IllegalStateException, IllegalArgumentException {
@@ -112,6 +115,10 @@ public class ElkConnectorImpl implements ElkConnector{
                        throw new IllegalArgumentException("No search string provided");
                }
 
+               if(!isAlphaNumeric(text)){
+                       throw new IllegalArgumentException("Search must be alpha numeric");
+               }
+
                QueryStringQueryBuilder mQ = QueryBuilders.queryStringQuery("*"+text+"*");
                SearchSourceBuilder searchSourceBuilder = 
                                new SearchSourceBuilder().query(mQ);
@@ -159,7 +166,7 @@ public class ElkConnectorImpl implements ElkConnector{
 
                        String errorMessage = result.getErrorMessage();
                        if (errorMessage != null && !errorMessage.isEmpty()) {
-                               String xMessage = errorMessage;
+                               String xMessage;
                                if (errorMessage.contains("TokenMgrError")) {
                                        int indexError = errorMessage.lastIndexOf("TokenMgrError");
                                        xMessage = "Invalid Search Expression.  Details: " + errorMessage.substring(indexError);
@@ -198,6 +205,10 @@ public class ElkConnectorImpl implements ElkConnector{
                        return search(type, text);
                }
 
+               if(!isAlphaNumeric(text)){
+                       throw new IllegalArgumentException("Search must be alpha numeric");
+               }
+
                String matches_s = "";
                matches_s = "{\n" +
                                "    \"size\" : "+ ElkConnectorImpl.QUERY_MAXRECORDS + ",\n" +
@@ -338,13 +349,13 @@ public class ElkConnectorImpl implements ElkConnector{
 
                if (result.isSucceeded()) {
                        if (LOGGER.isInfoEnabled())
-                               LOGGER.info("OK: PUT operation of " + "->"  + ": " +
+                               LOGGER.info("ElkConnector: OK: PUT operation of " + "->"  + ": " +
                                                "success=" + result.isSucceeded() + "[" + result.getResponseCode() + ":" +
                                                result.getPathToResult() + "]" + System.lineSeparator() +
                                                result.getJsonString());
                } else {
                        if (LOGGER.isWarnEnabled())
-                               LOGGER.warn("FAILURE: PUT operation of "+ "->" + ": " +
+                               LOGGER.warn("ElkConnector: FAILURE: PUT operation of "+ "->" + ": " +
                                                "success=" + result.isSucceeded() + "[" + result.getResponseCode() + ":" +
                                                result.getPathToResult() + "]" + System.lineSeparator() +
                                                result.getJsonString());