You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2016/10/24 18:32:39 UTC

[35/50] [abbrv] lucene-solr:jira/solr-8542-v2: SOLR-9662: New parameter -u in bin/post to pass basicauth credentials

SOLR-9662: New parameter -u <user:pass> in bin/post to pass basicauth credentials


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/e3a8a0fe
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/e3a8a0fe
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/e3a8a0fe

Branch: refs/heads/jira/solr-8542-v2
Commit: e3a8a0fe5f7ebff46509f51f9d490a5c801626ba
Parents: c9cf0ef
Author: Jan H�ydahl <ja...@apache.org>
Authored: Sat Oct 22 02:02:07 2016 +0200
Committer: Jan H�ydahl <ja...@apache.org>
Committed: Sat Oct 22 02:02:07 2016 +0200

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 +
 solr/bin/post                                   | 23 ++++++--
 .../org/apache/solr/util/SimplePostTool.java    | 59 +++++++++++++++-----
 3 files changed, 65 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e3a8a0fe/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index b4b0a33..e1c3971 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -152,6 +152,8 @@ New Features
 * SOLR-9326: Ability to create/delete/list snapshots at collection level.
   (Hrishikesh Gadre via yonik)
 
+* SOLR-9662: New parameter -u <user:pass> in bin/post to pass basicauth credentials (janhoy)
+
 Bug Fixes
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e3a8a0fe/solr/bin/post
----------------------------------------------------------------------
diff --git a/solr/bin/post b/solr/bin/post
index 73e59ed..4c629d1 100755
--- a/solr/bin/post
+++ b/solr/bin/post
@@ -68,6 +68,7 @@ function print_usage() {
   echo "    -host <host> (default: localhost)"
   echo "    -p or -port <port> (default: 8983)"
   echo "    -commit yes|no (default: yes)"
+  echo "    -u or -user <user:pass> (sets BasicAuth credentials)"
   # optimize intentionally omitted, but can be used as '-optimize yes' (default: no)
   echo ""
   echo "  Web crawl options:"
@@ -155,13 +156,23 @@ while [ $# -gt 0 ]; do
             ARGS+=("<add/>")
           fi
         fi
-      else
-        key="${1:1}"
+      elif [[ ("$1" == "-u" || "$1" == "-user") ]]; then
         shift
-#       echo "$1: PROP"
-        PROPS+=("-D$key=$1")
-        if [[ "$key" == "url" ]]; then
-          SOLR_URL=$1
+        PROPS+=("-Dbasicauth=$1")
+      else
+        if [[ "$1" == -D* ]] ; then
+          PROPS+=("$1")
+          if [[ "${1:2:4}" == "url=" ]]; then
+            SOLR_URL=${1:6}
+          fi
+        else          
+          key="${1:1}"
+          shift
+  #       echo "$1: PROP"
+          PROPS+=("-D$key=$1")
+          if [[ "$key" == "url" ]]; then
+            SOLR_URL=$1
+          fi
         fi
       fi
     else

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e3a8a0fe/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java b/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
index 20e7231..a8ef372 100644
--- a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
+++ b/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
@@ -42,6 +42,7 @@ import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Base64;
@@ -65,6 +66,9 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 /**
  * A simple utility class for posting raw updates to a Solr server, 
  * has a main method so it can be run on the command line.
@@ -86,6 +90,7 @@ public class SimplePostTool {
   private static final int MAX_WEB_DEPTH = 10;
   private static final String DEFAULT_CONTENT_TYPE = "application/xml";
   private static final String DEFAULT_FILE_TYPES = "xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log";
+  private static final String BASIC_AUTH = "basicauth";
 
   static final String DATA_MODE_FILES = "files";
   static final String DATA_MODE_ARGS = "args";
@@ -233,6 +238,15 @@ public class SimplePostTool {
       }
       urlStr = SimplePostTool.appendParam(urlStr, params);
       URL url = new URL(urlStr);
+      String user = null;
+      if (url.getUserInfo() != null && url.getUserInfo().trim().length() > 0) {
+        user = url.getUserInfo().split(":")[0];
+      } else if (System.getProperty(BASIC_AUTH) != null) {
+        user = System.getProperty(BASIC_AUTH).trim().split(":")[0];
+      }
+      if (user != null)
+        info("Basic Authentication enabled, user=" + user);
+      
       boolean auto = isOn(System.getProperty("auto", DEFAULT_AUTO));
       String type = System.getProperty("type");
       String format = System.getProperty("format");
@@ -385,6 +399,7 @@ public class SimplePostTool {
      "  -Dtype=<content-type> (default=" + DEFAULT_CONTENT_TYPE + ")\n"+
      "  -Dhost=<host> (default: " + DEFAULT_POST_HOST+ ")\n"+
      "  -Dport=<port> (default: " + DEFAULT_POST_PORT+ ")\n"+
+     "  -Dbasicauth=<user:pass> (sets Basic Authentication credentials)\n"+
      "  -Dauto=yes|no (default=" + DEFAULT_AUTO + ")\n"+
      "  -Drecursive=yes|no|<depth> (default=" + DEFAULT_RECURSIVE + ")\n"+
      "  -Ddelay=<seconds> (default=0 for files, 10 for web)\n"+
@@ -851,14 +866,13 @@ public class SimplePostTool {
     try {
       if(mockMode) return;
       HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
-      if (url.getUserInfo() != null) {
-        String encoding = Base64.getEncoder().encodeToString(url.getUserInfo().getBytes(StandardCharsets.US_ASCII));
-        urlc.setRequestProperty("Authorization", "Basic " + encoding);
-      }
+      basicAuth(urlc);
       urlc.connect();
       checkResponseCode(urlc);
     } catch (IOException e) {
-      warn("An error occurred posting data to "+url+". Please check that Solr is running.");
+      warn("An error occurred getting data from "+url+". Please check that Solr is running.");
+    } catch (Exception e) {
+      warn("An error occurred getting data from "+url+". Message: " + e.getMessage());
     }
   }
 
@@ -886,10 +900,7 @@ public class SimplePostTool {
         urlc.setUseCaches(false);
         urlc.setAllowUserInteraction(false);
         urlc.setRequestProperty("Content-type", type);
-        if (url.getUserInfo() != null) {
-          String encoding = Base64.getEncoder().encodeToString(url.getUserInfo().getBytes(StandardCharsets.US_ASCII));
-          urlc.setRequestProperty("Authorization", "Basic " + encoding);
-        }
+        basicAuth(urlc);
         if (null != length) {
           urlc.setFixedLengthStreamingMode(length);
         } else {
@@ -899,13 +910,14 @@ public class SimplePostTool {
       } catch (IOException e) {
         fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
         success = false;
+      } catch (Exception e) {
+        fatal("POST failed with error " + e.getMessage());
       }
-      
+
       try (final OutputStream out = urlc.getOutputStream()) {
         pipe(data, out);
       } catch (IOException e) {
         fatal("IOException while posting data: " + e);
-        success = false;
       }
       
       try {
@@ -916,14 +928,29 @@ public class SimplePostTool {
       } catch (IOException e) {
         warn("IOException while reading response: " + e);
         success = false;
+      } catch (GeneralSecurityException e) {
+        fatal("Looks like Solr is secured and would not let us in. Try with another user in '-u' parameter");
       }
     } finally {
       if (urlc!=null) urlc.disconnect();
     }
     return success;
   }
-  
-  private static boolean checkResponseCode(HttpURLConnection urlc) throws IOException {
+
+  private static void basicAuth(HttpURLConnection urlc) throws Exception {
+    if (urlc.getURL().getUserInfo() != null) {
+      String encoding = Base64.getEncoder().encodeToString(urlc.getURL().getUserInfo().getBytes(US_ASCII));
+      urlc.setRequestProperty("Authorization", "Basic " + encoding);
+    } else if (System.getProperty(BASIC_AUTH) != null) {
+      String basicauth = System.getProperty(BASIC_AUTH).trim();
+      if (!basicauth.contains(":")) {
+        throw new Exception("System property '"+BASIC_AUTH+"' must be of format user:pass");
+      }
+      urlc.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(basicauth.getBytes(UTF_8)));
+    }
+  }
+
+  private static boolean checkResponseCode(HttpURLConnection urlc) throws IOException, GeneralSecurityException {
     if (urlc.getResponseCode() >= 400) {
       warn("Solr returned an error #" + urlc.getResponseCode() + 
             " (" + urlc.getResponseMessage() + ") for url: " + urlc.getURL());
@@ -948,6 +975,12 @@ public class SimplePostTool {
           warn(response.toString().trim());
         }
       }
+      if (urlc.getResponseCode() == 401) {
+        throw new GeneralSecurityException("Solr requires authentication (response 401). Please try again with '-u' option");
+      }
+      if (urlc.getResponseCode() == 403) {
+        throw new GeneralSecurityException("You are not authorized to perform this action against Solr. (response 403)");
+      }
       return false;
     }
     return true;