Search in sources :

Example 16 with SolrRequest

use of org.apache.solr.client.solrj.SolrRequest 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 17 with SolrRequest

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

the class ShardSplitTest method splitShard.

protected void splitShard(String collection, String shardId, List<DocRouter.Range> subRanges, String splitKey) throws SolrServerException, IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionParams.CollectionAction.SPLITSHARD.toString());
    params.set("collection", collection);
    if (shardId != null) {
        params.set("shard", shardId);
    }
    if (subRanges != null) {
        StringBuilder ranges = new StringBuilder();
        for (int i = 0; i < subRanges.size(); i++) {
            DocRouter.Range subRange = subRanges.get(i);
            ranges.append(subRange.toString());
            if (i < subRanges.size() - 1)
                ranges.append(",");
        }
        params.set("ranges", ranges.toString());
    }
    if (splitKey != null) {
        params.set("split.key", splitKey);
    }
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    String baseUrl = ((HttpSolrClient) shardToJetty.get(SHARD1).get(0).client.solrClient).getBaseURL();
    baseUrl = baseUrl.substring(0, baseUrl.length() - "collection1".length());
    try (HttpSolrClient baseServer = getHttpSolrClient(baseUrl)) {
        baseServer.setConnectionTimeout(30000);
        baseServer.setSoTimeout(60000 * 5);
        baseServer.request(request);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) DocRouter(org.apache.solr.common.cloud.DocRouter) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 18 with SolrRequest

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

the class SyncSliceTest method test.

@Test
public void test() throws Exception {
    handle.clear();
    handle.put("timestamp", SKIPVAL);
    waitForThingsToLevelOut(30);
    del("*:*");
    List<CloudJettyRunner> skipServers = new ArrayList<>();
    int docId = 0;
    indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "to come to the aid of their country.");
    indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "old haven was blue.");
    skipServers.add(shardToJetty.get("shard1").get(1));
    indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "but the song was fancy.");
    skipServers.add(shardToJetty.get("shard1").get(2));
    indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "under the moon and over the lake");
    commit();
    waitForRecoveriesToFinish(false);
    // shard should be inconsistent
    String shardFailMessage = checkShardConsistency("shard1", true, false);
    assertNotNull(shardFailMessage);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionAction.SYNCSHARD.toString());
    params.set("collection", "collection1");
    params.set("shard", "shard1");
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    String baseUrl = ((HttpSolrClient) shardToJetty.get("shard1").get(2).client.solrClient).getBaseURL();
    baseUrl = baseUrl.substring(0, baseUrl.length() - "collection1".length());
    try (HttpSolrClient baseClient = getHttpSolrClient(baseUrl)) {
        // we only set the connect timeout, not so timeout
        baseClient.setConnectionTimeout(30000);
        baseClient.request(request);
    }
    waitForThingsToLevelOut(15);
    checkShardConsistency(false, true);
    long cloudClientDocs = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(4, cloudClientDocs);
    // kill the leader - new leader could have all the docs or be missing one
    CloudJettyRunner leaderJetty = shardToLeaderJetty.get("shard1");
    // but not the leader
    skipServers = getRandomOtherJetty(leaderJetty, null);
    // this doc won't be on one node
    indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "to come to the aid of their country.");
    Set<CloudJettyRunner> jetties = new HashSet<>();
    jetties.addAll(shardToJetty.get("shard1"));
    jetties.remove(leaderJetty);
    assertEquals(getShardCount() - 1, jetties.size());
    chaosMonkey.killJetty(leaderJetty);
    Thread.sleep(3000);
    waitForNoShardInconsistency();
    Thread.sleep(1000);
    checkShardConsistency(false, true);
    cloudClientDocs = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(5, cloudClientDocs);
    CloudJettyRunner deadJetty = leaderJetty;
    // let's get the latest leader
    while (deadJetty == leaderJetty) {
        updateMappingsFromZk(this.jettys, this.clients);
        leaderJetty = shardToLeaderJetty.get("shard1");
    }
    // bring back dead node
    // he is not the leader anymore
    ChaosMonkey.start(deadJetty.jetty);
    waitTillAllNodesActive();
    skipServers = getRandomOtherJetty(leaderJetty, deadJetty);
    skipServers.addAll(getRandomOtherJetty(leaderJetty, deadJetty));
    // skip list should be 
    //    System.out.println("leader:" + leaderJetty.url);
    //    System.out.println("dead:" + deadJetty.url);
    //    System.out.println("skip list:" + skipServers);
    // we are skipping  2 nodes
    assertEquals(2, skipServers.size());
    // more docs than can peer sync
    for (int i = 0; i < 300; i++) {
        indexDoc(skipServers, id, docId++, i1, 50, tlong, 50, t1, "to come to the aid of their country.");
    }
    commit();
    Thread.sleep(1000);
    waitForRecoveriesToFinish(false);
    // shard should be inconsistent
    shardFailMessage = waitTillInconsistent();
    assertNotNull("Test Setup Failure: shard1 should have just been set up to be inconsistent - but it's still consistent. Leader:" + leaderJetty.url + " Dead Guy:" + deadJetty.url + "skip list:" + skipServers, shardFailMessage);
    // good place to test compareResults
    boolean shouldFail = CloudInspectUtil.compareResults(controlClient, cloudClient);
    assertTrue("A test that compareResults is working correctly failed", shouldFail);
    jetties = new HashSet<>();
    jetties.addAll(shardToJetty.get("shard1"));
    jetties.remove(leaderJetty);
    assertEquals(getShardCount() - 1, jetties.size());
    // kill the current leader
    chaosMonkey.killJetty(leaderJetty);
    waitForNoShardInconsistency();
    checkShardConsistency(true, true);
    success = true;
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ArrayList(java.util.ArrayList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrQuery(org.apache.solr.client.solrj.SolrQuery) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with SolrRequest

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

the class TestRebalanceLeaders method listCollection.

private void listCollection() throws IOException, SolrServerException {
    //CloudSolrServer client = createCloudClient(null);
    try {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.LIST.toString());
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        NamedList<Object> rsp = cloudClient.request(request);
        List<String> collections = (List<String>) rsp.get("collections");
        assertTrue("control_collection was not found in list", collections.contains("control_collection"));
        assertTrue(DEFAULT_COLLECTION + " was not found in list", collections.contains(DEFAULT_COLLECTION));
        assertTrue(COLLECTION_NAME + " was not found in list", collections.contains(COLLECTION_NAME));
    } finally {
    //remove collections
    //client.shutdown();
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 20 with SolrRequest

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

the class TestRebalanceLeaders method issuePreferred.

void issuePreferred(String slice, Replica rep) throws IOException, SolrServerException, InterruptedException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString());
    // Insure we get error returns when omitting required parameters
    params.set("collection", COLLECTION_NAME);
    params.set("shard", slice);
    params.set("replica", rep.getName());
    params.set("property", "preferredLeader");
    params.set("property.value", "true");
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    cloudClient.request(request);
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

SolrRequest (org.apache.solr.client.solrj.SolrRequest)42 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)36 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)35 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)18 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)11 NamedList (org.apache.solr.common.util.NamedList)11 HashMap (java.util.HashMap)10 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)7 Test (org.junit.Test)7 List (java.util.List)6 SolrClient (org.apache.solr.client.solrj.SolrClient)5 CollectionAdminResponse (org.apache.solr.client.solrj.response.CollectionAdminResponse)5 LBHttpSolrClient (org.apache.solr.client.solrj.impl.LBHttpSolrClient)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 SolrException (org.apache.solr.common.SolrException)3 Utils.makeMap (org.apache.solr.common.util.Utils.makeMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2