Search in sources :

Example 11 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class BasicAuthIntegrationTest method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    boolean isUseV2Api = random().nextBoolean();
    String authcPrefix = "/admin/authentication";
    String authzPrefix = "/admin/authorization";
    if (isUseV2Api) {
        authcPrefix = "/____v2/cluster/security/authentication";
        authzPrefix = "/____v2/cluster/security/authorization";
    }
    NamedList<Object> rsp;
    HttpClient cl = null;
    try {
        cl = HttpClientUtil.createClient(null);
        JettySolrRunner randomJetty = cluster.getRandomJetty(random());
        String baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);
        zkClient().setData("/security.json", STD_CONF.replaceAll("'", "\"").getBytes(UTF_8), true);
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        randomJetty.stop();
        randomJetty.start(false);
        baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        String command = "{\n" + "'set-user': {'harry':'HarryIsCool'}\n" + "}";
        final SolrRequest genericReq;
        if (isUseV2Api) {
            genericReq = new V2Request.Builder("/cluster/security/authentication").withMethod(SolrRequest.METHOD.POST).build();
        } else {
            genericReq = new GenericSolrRequest(SolrRequest.METHOD.POST, authcPrefix, new ModifiableSolrParams());
            ((GenericSolrRequest) genericReq).setContentStreams(Collections.singletonList(new ContentStreamBase.ByteArrayStream(command.getBytes(UTF_8), "")));
        }
        HttpSolrClient.RemoteSolrException exp = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> {
            cluster.getSolrClient().request(genericReq);
        });
        assertEquals(401, exp.code());
        command = "{\n" + "'set-user': {'harry':'HarryIsUberCool'}\n" + "}";
        HttpPost httpPost = new HttpPost(baseUrl + authcPrefix);
        setBasicAuthHeader(httpPost, "solr", "SolrRocks");
        httpPost.setEntity(new ByteArrayEntity(command.getBytes(UTF_8)));
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);
        HttpResponse r = cl.execute(httpPost);
        int statusCode = r.getStatusLine().getStatusCode();
        Utils.consumeFully(r.getEntity());
        assertEquals("proper_cred sent, but access denied", 200, statusCode);
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);
        command = "{\n" + "'set-user-role': {'harry':'admin'}\n" + "}";
        executeCommand(baseUrl + authzPrefix, cl, command, "solr", "SolrRocks");
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/user-role/harry", NOT_NULL_PREDICATE, 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("collection", "x", "path", "/update/*", "role", "dev"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[1]/collection", "x", 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("name", "collection-admin-edit", "role", "admin"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[2]/name", "collection-admin-edit", 20);
        CollectionAdminRequest.Reload reload = CollectionAdminRequest.reloadCollection(COLLECTION);
        try (HttpSolrClient solrClient = getHttpSolrClient(baseUrl)) {
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
            reload.setMethod(SolrRequest.METHOD.POST);
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
        }
        cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "HarryIsUberCool"));
        try {
            cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "Cool12345"));
            fail("This should not succeed");
        } catch (HttpSolrClient.RemoteSolrException e) {
        }
        executeCommand(baseUrl + authzPrefix, cl, "{set-permission : { name : update , role : admin}}", "harry", "HarryIsUberCool");
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "4");
        UpdateRequest update = new UpdateRequest();
        update.setBasicAuthCredentials("harry", "HarryIsUberCool");
        update.add(doc);
        update.setCommitWithin(100);
        cluster.getSolrClient().request(update, COLLECTION);
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: true}}", "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/blockUnknown", "true", 20, "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + "/admin/info/key?wt=json", "key", NOT_NULL_PREDICATE, 20);
        String[] toolArgs = new String[] { "status", "-solr", baseUrl };
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
        SolrCLI.StatusTool tool = new SolrCLI.StatusTool(stdoutSim);
        try {
            System.setProperty("basicauth", "harry:HarryIsUberCool");
            tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
            Map obj = (Map) Utils.fromJSON(new ByteArrayInputStream(baos.toByteArray()));
            assertTrue(obj.containsKey("version"));
            assertTrue(obj.containsKey("startTime"));
            assertTrue(obj.containsKey("uptime"));
            assertTrue(obj.containsKey("memory"));
        } catch (Exception e) {
            log.error("RunExampleTool failed due to: " + e + "; stdout from tool prior to failure: " + baos.toString(StandardCharsets.UTF_8.name()));
        }
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: false}}", "harry", "HarryIsUberCool");
    } finally {
        if (cl != null) {
            HttpClientUtil.close(cl);
        }
    }
}
Also used : GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) HttpPost(org.apache.http.client.methods.HttpPost) SolrRequest(org.apache.solr.client.solrj.SolrRequest) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) SolrCLI(org.apache.solr.util.SolrCLI) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) PrintStream(java.io.PrintStream) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V2Request(org.apache.solr.client.solrj.request.V2Request) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.Test)

Example 12 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class BasicAuthStandaloneTest method testBasicAuth.

@Test
@LogLevel("org.apache.solr=DEBUG")
public void testBasicAuth() throws Exception {
    String authcPrefix = "/admin/authentication";
    HttpClient cl = null;
    HttpSolrClient httpSolrClient = null;
    try {
        cl = HttpClientUtil.createClient(null);
        String baseUrl = buildUrl(jetty.getLocalPort(), "/solr");
        httpSolrClient = getHttpSolrClient(baseUrl);
        verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);
        // Write security.json locally. Should cause security to be initialized
        securityConfHandler.persistConf(new SecurityConfHandler.SecurityConfig().setData(Utils.fromJSONString(STD_CONF.replaceAll("'", "\""))));
        securityConfHandler.securityConfEdited();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        String command = "{\n" + "'set-user': {'harry':'HarryIsCool'}\n" + "}";
        GenericSolrRequest genericReq = new GenericSolrRequest(SolrRequest.METHOD.POST, authcPrefix, new ModifiableSolrParams());
        genericReq.setContentStreams(Collections.singletonList(new ContentStreamBase.ByteArrayStream(command.getBytes(UTF_8), "")));
        HttpSolrClient finalHttpSolrClient = httpSolrClient;
        HttpSolrClient.RemoteSolrException exp = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> {
            finalHttpSolrClient.request(genericReq);
        });
        assertEquals(401, exp.code());
        command = "{\n" + "'set-user': {'harry':'HarryIsUberCool'}\n" + "}";
        HttpPost httpPost = new HttpPost(baseUrl + authcPrefix);
        setBasicAuthHeader(httpPost, "solr", "SolrRocks");
        httpPost.setEntity(new ByteArrayEntity(command.getBytes(UTF_8)));
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);
        HttpResponse r = cl.execute(httpPost);
        int statusCode = r.getStatusLine().getStatusCode();
        Utils.consumeFully(r.getEntity());
        assertEquals("proper_cred sent, but access denied", 200, statusCode);
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);
        // Read file from SOLR_HOME and verify that it contains our new user
        assertTrue(new String(Utils.toJSON(securityConfHandler.getSecurityConfig(false).getData()), Charset.forName("UTF-8")).contains("harry"));
    } finally {
        if (cl != null) {
            HttpClientUtil.close(cl);
            httpSolrClient.close();
        }
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test) LogLevel(org.apache.solr.util.LogLevel)

Example 13 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class TestInPlaceUpdatesDistrib method reorderedDBQsResurrectionTest.

/* Test for a situation when a document requiring in-place update cannot be "resurrected"
   * when the original full indexed document has been deleted by an out of order DBQ.
   * Expected behaviour in this case should be to throw the replica into LIR (since this will
   * be rare). Here's an example of the situation:
        ADD(id=x, val=5, ver=1)
        UPD(id=x, val=10, ver = 2)
        DBQ(q=val:10, v=4)
        DV(id=x, val=5, ver=3)
   */
private void reorderedDBQsResurrectionTest() throws Exception {
    if (onlyLeaderIndexes) {
        log.info("RTG with DBQs are not working in tlog replicas");
        return;
    }
    clearIndex();
    commit();
    buildRandomIndex(0);
    // RTG straight from the index
    SolrDocument sdoc = LEADER.getById("0");
    //assertEquals(value, sdoc.get("inplace_updatable_float"));
    assertEquals("title0", sdoc.get("title_s"));
    long version0 = (long) sdoc.get("_version_");
    String field = "inplace_updatable_int";
    // put replica out of sync
    List<UpdateRequest> updates = new ArrayList<>();
    // full update
    updates.add(simulatedUpdateRequest(null, "id", 0, "title_s", "title0_new", field, 5, "_version_", version0 + 1));
    // inplace_updatable_float=101
    updates.add(simulatedUpdateRequest(version0 + 1, "id", 0, field, 10, "_version_", version0 + 2));
    // inplace_updatable_float=101
    updates.add(simulatedUpdateRequest(version0 + 2, "id", 0, field, 5, "_version_", version0 + 3));
    // supposed to not delete anything
    updates.add(simulatedDeleteRequest(field + ":10", version0 + 4));
    // order the updates correctly for NONLEADER 1
    for (UpdateRequest update : updates) {
        log.info("Issuing well ordered update: " + update.getDocuments());
        NONLEADERS.get(1).request(update);
    }
    // Reordering needs to happen using parallel threads
    ExecutorService threadpool = ExecutorUtil.newMDCAwareFixedThreadPool(updates.size() + 1, new DefaultSolrThreadFactory(getTestName()));
    // re-order the last two updates for NONLEADER 0
    List<UpdateRequest> reorderedUpdates = new ArrayList<>(updates);
    Collections.swap(reorderedUpdates, 2, 3);
    List<Future<UpdateResponse>> updateResponses = new ArrayList<>();
    for (UpdateRequest update : reorderedUpdates) {
        // pretend as this update is coming from the other non-leader, so that
        // the resurrection can happen from there (instead of the leader)
        update.setParam(DistributedUpdateProcessor.DISTRIB_FROM, ((HttpSolrClient) NONLEADERS.get(1)).getBaseURL());
        AsyncUpdateWithRandomCommit task = new AsyncUpdateWithRandomCommit(update, NONLEADERS.get(0), random().nextLong());
        updateResponses.add(threadpool.submit(task));
        // while we can't guarantee/trust what order the updates are executed in, since multiple threads
        // are involved, but we're trying to bias the thread scheduling to run them in the order submitted
        Thread.sleep(10);
    }
    threadpool.shutdown();
    assertTrue("Thread pool didn't terminate within 15 secs", threadpool.awaitTermination(15, TimeUnit.SECONDS));
    int successful = 0;
    for (Future<UpdateResponse> resp : updateResponses) {
        try {
            UpdateResponse r = resp.get();
            if (r.getStatus() == 0) {
                successful++;
            }
        } catch (Exception ex) {
            if (!ex.getMessage().contains("Tried to fetch missing update" + " from the leader, but missing wasn't present at leader.")) {
                throw ex;
            }
        }
    }
    // All should succeed, i.e. no LIR
    assertEquals(updateResponses.size(), successful);
    log.info("Non leader 0: " + ((HttpSolrClient) NONLEADERS.get(0)).getBaseURL());
    log.info("Non leader 1: " + ((HttpSolrClient) NONLEADERS.get(1)).getBaseURL());
    SolrDocument doc0 = NONLEADERS.get(0).getById(String.valueOf(0), params("distrib", "false"));
    SolrDocument doc1 = NONLEADERS.get(1).getById(String.valueOf(0), params("distrib", "false"));
    log.info("Doc in both replica 0: " + doc0);
    log.info("Doc in both replica 1: " + doc1);
    // assert both replicas have same effect
    for (int i = 0; i < NONLEADERS.size(); i++) {
        // 0th is re-ordered replica, 1st is well-ordered replica
        SolrClient client = NONLEADERS.get(i);
        SolrDocument doc = client.getById(String.valueOf(0), params("distrib", "false"));
        assertNotNull("Client: " + ((HttpSolrClient) client).getBaseURL(), doc);
        assertEquals("Client: " + ((HttpSolrClient) client).getBaseURL(), 5, doc.getFieldValue(field));
    }
    log.info("reorderedDBQsResurrectionTest: This test passed fine...");
    clearIndex();
    commit();
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ArrayList(java.util.ArrayList) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) SolrServerException(org.apache.solr.client.solrj.SolrServerException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 14 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class SolrExampleTests method testUpdateField.

@Test
public void testUpdateField() throws Exception {
    //no versions
    SolrClient client = getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "unique");
    doc.addField("name", "gadget");
    doc.addField("price", 1);
    client.add(doc);
    client.commit();
    SolrQuery q = new SolrQuery("*:*");
    q.setFields("id", "price", "name", "_version_");
    QueryResponse resp = client.query(q);
    assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
    Long version = (Long) resp.getResults().get(0).getFirstValue("_version_");
    assertNotNull("no version returned", version);
    assertEquals(1.0f, resp.getResults().get(0).getFirstValue("price"));
    //update "price" with incorrect version (optimistic locking)
    //need better api for this???
    HashMap<String, Object> oper = new HashMap<>();
    oper.put("set", 100);
    doc = new SolrInputDocument();
    doc.addField("id", "unique");
    doc.addField("_version_", version + 1);
    doc.addField("price", oper);
    try {
        client.add(doc);
        if (client instanceof HttpSolrClient) {
            //XXX concurrent client reports exceptions differently
            fail("Operation should throw an exception!");
        } else {
            //just to be sure the client has sent the doc
            client.commit();
            ErrorTrackingConcurrentUpdateSolrClient concurrentClient = (ErrorTrackingConcurrentUpdateSolrClient) client;
            assertNotNull("ConcurrentUpdateSolrClient did not report an error", concurrentClient.lastError);
            assertTrue("ConcurrentUpdateSolrClient did not report an error", concurrentClient.lastError.getMessage().contains("Conflict"));
        }
    } catch (SolrException se) {
        assertTrue("No identifiable error message", se.getMessage().contains("version conflict for unique"));
    }
    //update "price", use correct version (optimistic locking)
    doc = new SolrInputDocument();
    doc.addField("id", "unique");
    doc.addField("_version_", version);
    doc.addField("price", oper);
    client.add(doc);
    client.commit();
    resp = client.query(q);
    assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
    assertEquals("price was not updated?", 100.0f, resp.getResults().get(0).getFirstValue("price"));
    assertEquals("no name?", "gadget", resp.getResults().get(0).getFirstValue("name"));
    //update "price", no version
    oper.put("set", 200);
    doc = new SolrInputDocument();
    doc.addField("id", "unique");
    doc.addField("price", oper);
    client.add(doc);
    client.commit();
    resp = client.query(q);
    assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
    assertEquals("price was not updated?", 200.0f, resp.getResults().get(0).getFirstValue("price"));
    assertEquals("no name?", "gadget", resp.getResults().get(0).getFirstValue("name"));
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HashMap(java.util.HashMap) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) SolrException(org.apache.solr.common.SolrException) RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) Test(org.junit.Test)

Example 15 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class SolrExampleBinaryTest method createNewSolrClient.

@Override
public SolrClient createNewSolrClient() {
    try {
        // setup the server...
        String url = jetty.getBaseUrl().toString() + "/collection1";
        HttpSolrClient client = getHttpSolrClient(url);
        client.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
        client.setUseMultiPartPost(random().nextBoolean());
        // where the magic happens
        client.setParser(new BinaryResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        return client;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter)

Aggregations

HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)187 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)48 SolrClient (org.apache.solr.client.solrj.SolrClient)46 Test (org.junit.Test)46 SolrQuery (org.apache.solr.client.solrj.SolrQuery)38 ArrayList (java.util.ArrayList)35 Replica (org.apache.solr.common.cloud.Replica)34 SolrInputDocument (org.apache.solr.common.SolrInputDocument)31 IOException (java.io.IOException)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 Slice (org.apache.solr.common.cloud.Slice)25 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)24 SolrException (org.apache.solr.common.SolrException)23 SolrServerException (org.apache.solr.client.solrj.SolrServerException)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)20 NamedList (org.apache.solr.common.util.NamedList)19 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)18 DocCollection (org.apache.solr.common.cloud.DocCollection)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)15 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)15