Search in sources :

Example 76 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class SecurityConfHandlerTest method testEdit.

public void testEdit() throws Exception {
    MockSecurityHandler handler = new MockSecurityHandler();
    String command = "{\n" + "'set-user': {'tom':'TomIsCool'},\n" + "'set-user':{ 'tom':'TomIsUberCool'}\n" + "}";
    LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authentication");
    ContentStreamBase.ByteArrayStream o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    handler.handleRequestBody(req, new SolrQueryResponse());
    BasicAuthPlugin basicAuth = new BasicAuthPlugin();
    SecurityConfig securityCfg = handler.m.get("/security.json");
    basicAuth.init((Map<String, Object>) securityCfg.getData().get("authentication"));
    assertTrue(basicAuth.authenticate("tom", "TomIsUberCool"));
    command = "{\n" + "'set-user': {'harry':'HarryIsCool'},\n" + "'delete-user': ['tom','harry']\n" + "}";
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    handler.handleRequestBody(req, new SolrQueryResponse());
    securityCfg = handler.m.get("/security.json");
    assertEquals(3, securityCfg.getVersion());
    Map result = (Map) securityCfg.getData().get("authentication");
    result = (Map) result.get("credentials");
    assertTrue(result.isEmpty());
    command = "{'set-permission':{ collection : acoll ,\n" + "                      path : '/nonexistentpath',\n" + "                      role :guest },\n" + "'set-user-role': { 'tom': ['admin','dev']}," + "'set-permission':{'name': 'security-edit',\n" + "                  'role': 'admin'}\n" + "}";
    req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authorization");
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    SolrQueryResponse rsp = new SolrQueryResponse();
    handler.handleRequestBody(req, rsp);
    assertNull(rsp.getValues().get(CommandOperation.ERR_MSGS));
    Map authzconf = (Map) handler.m.get("/security.json").getData().get("authorization");
    Map userRoles = (Map) authzconf.get("user-role");
    List tomRoles = (List) userRoles.get("tom");
    assertTrue(tomRoles.contains("admin"));
    assertTrue(tomRoles.contains("dev"));
    List<Map> permissions = (List<Map>) authzconf.get("permissions");
    assertEquals(2, permissions.size());
    for (Map p : permissions) {
        assertEquals("acoll", p.get("collection"));
        break;
    }
    command = "{\n" + "'set-permission':{index : 2,  name : security-edit,\n" + "                  'role': ['admin','dev']\n" + "                  }}";
    req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authorization");
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    rsp = new SolrQueryResponse();
    handler.handleRequestBody(req, rsp);
    authzconf = (Map) handler.m.get("/security.json").getData().get("authorization");
    permissions = (List<Map>) authzconf.get("permissions");
    Map p = permissions.get(1);
    assertEquals("security-edit", p.get("name"));
    List rol = (List) p.get("role");
    assertEquals("admin", rol.get(0));
    assertEquals("dev", rol.get(1));
    command = "{\n" + "'update-permission':{'index': 1,\n" + "                  'role': ['guest','admin']\n" + "                  }}";
    req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authorization");
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    rsp = new SolrQueryResponse();
    handler.handleRequestBody(req, rsp);
    authzconf = (Map) handler.m.get("/security.json").getData().get("authorization");
    permissions = (List<Map>) authzconf.get("permissions");
    p = permissions.get(0);
    assertEquals("acoll", p.get("collection"));
    rol = (List) p.get("role");
    assertEquals("guest", rol.get(0));
    assertEquals("admin", rol.get(1));
    command = "{\n" + "delete-permission: 1,\n" + " set-user-role : { tom :null}\n" + "}";
    req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authorization");
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    rsp = new SolrQueryResponse();
    handler.handleRequestBody(req, rsp);
    assertNull(rsp.getValues().get(CommandOperation.ERR_MSGS));
    authzconf = (Map) handler.m.get("/security.json").getData().get("authorization");
    userRoles = (Map) authzconf.get("user-role");
    assertEquals(0, userRoles.size());
    permissions = (List<Map>) authzconf.get("permissions");
    assertEquals(1, permissions.size());
    for (Map permission : permissions) {
        assertFalse("some-permission".equals(permission.get("name")));
    }
    command = "{\n" + "'set-permission':{index : 2,  'name': 'security-edit',\n" + // -ve test security edit is a well-known permission , only role attribute should be provided
    "                  'method':'POST'," + "                  'role': 'admin'\n" + "                  }}";
    req = new LocalSolrQueryRequest(null, new ModifiableSolrParams());
    req.getContext().put("httpMethod", "POST");
    req.getContext().put("path", "/admin/authorization");
    o = new ContentStreamBase.ByteArrayStream(command.getBytes(StandardCharsets.UTF_8), "");
    req.setContentStreams(Collections.singletonList(o));
    rsp = new SolrQueryResponse();
    handler.handleRequestBody(req, rsp);
    List l = (List) ((Map) ((List) rsp.getValues().get("errorMessages")).get(0)).get("errorMessages");
    assertEquals(1, l.size());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BasicAuthPlugin(org.apache.solr.security.BasicAuthPlugin) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SecurityConfig(org.apache.solr.handler.admin.SecurityConfHandler.SecurityConfig) List(java.util.List) HashMap(java.util.HashMap) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) Map(java.util.Map) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 77 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class AtomicUpdateProcessorFactoryTest method testBasics.

public void testBasics() throws Exception {
    AddUpdateCommand cmd = new AddUpdateCommand(new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams().add("processor", "Atomic").add("Atomic.cat", "add").add("Atomic.title", "set").add("Atomic.count_i", "set").add("Atomic.name_s", "set").add("Atomic.multiDefault", "set").add("commit", "true")));
    cmd.solrDoc = new SolrInputDocument();
    cmd.solrDoc.addField("id", 1);
    cmd.solrDoc.addField("cat", "human");
    cmd.solrDoc.addField("title", "Mr");
    cmd.solrDoc.addField("count_i", 20);
    cmd.solrDoc.addField("name_s", "Virat");
    cmd.solrDoc.addField("multiDefault", "Delhi");
    AtomicUpdateProcessorFactory factory = new AtomicUpdateProcessorFactory();
    factory.inform(h.getCore());
    factory.getInstance(cmd.getReq(), new SolrQueryResponse(), new DistributedUpdateProcessor(cmd.getReq(), new SolrQueryResponse(), new RunUpdateProcessor(cmd.getReq(), null))).processAdd(cmd);
    assertU(commit());
    assertQ("Check the total number of docs", req("q", "id:1"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "cat:human"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "title:Mr"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "count_i:20"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "name_s:Virat"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "multiDefault:Delhi"), "//result[@numFound=1]");
    cmd = new AddUpdateCommand(new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams().add("processor", "Atomic").add("Atomic.cat", "add").add("Atomic.title", "set").add("Atomic.count_i", "inc").add("Atomic.name_s", "remove").add("Atomic.multiDefault", "removeregex").add("commit", "true")));
    cmd.solrDoc = new SolrInputDocument();
    cmd.solrDoc.addField("id", 1);
    cmd.solrDoc.addField("cat", "animal");
    cmd.solrDoc.addField("title", "Dr");
    cmd.solrDoc.addField("count_i", 20);
    cmd.solrDoc.addField("name_s", "Virat");
    cmd.solrDoc.addField("multiDefault", ".elh.");
    factory = new AtomicUpdateProcessorFactory();
    factory.inform(h.getCore());
    factory.getInstance(cmd.getReq(), new SolrQueryResponse(), new DistributedUpdateProcessor(cmd.getReq(), new SolrQueryResponse(), new RunUpdateProcessor(cmd.getReq(), null))).processAdd(cmd);
    assertU(commit());
    assertQ("Check the total number of docs", req("q", "id:1"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "cat:human"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "cat:animal"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "title:Mr"), "//result[@numFound=0]");
    assertQ("Check the total number of docs", req("q", "title:Dr"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "count_i:20"), "//result[@numFound=0]");
    assertQ("Check the total number of docs", req("q", "count_i:40"), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "name_s:Virat"), "//result[@numFound=0]");
    assertQ("Check the total number of docs", req("q", "multiDefault:Delhi"), "//result[@numFound=0]");
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 78 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class AtomicUpdateProcessorFactoryTest method testMultipleThreads.

public void testMultipleThreads() throws Exception {
    clearIndex();
    String[] strings = new String[5];
    for (int i = 0; i < 5; i++) {
        strings[i] = generateRandomString();
    }
    List<Thread> threads = new ArrayList<>(100);
    //int_i
    int finalCount = 0;
    for (int i = 0; i < 100; i++) {
        int index = random().nextInt(5);
        Thread t = new Thread() {

            @Override
            public void run() {
                AddUpdateCommand cmd = new AddUpdateCommand(new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams().add("processor", "Atomic").add("Atomic.cat", "add").add("Atomic.int_i", "inc").add("commit", "true")));
                cmd.solrDoc = new SolrInputDocument();
                //hardcoded id=2
                cmd.solrDoc.addField("id", 10);
                cmd.solrDoc.addField("cat", strings[index]);
                cmd.solrDoc.addField("int_i", index);
                try {
                    AtomicUpdateProcessorFactory factory = new AtomicUpdateProcessorFactory();
                    factory.inform(h.getCore());
                    factory.getInstance(cmd.getReq(), new SolrQueryResponse(), new DistributedUpdateProcessor(cmd.getReq(), new SolrQueryResponse(), new RunUpdateProcessor(cmd.getReq(), null))).processAdd(cmd);
                } catch (IOException e) {
                }
            }
        };
        t.run();
        threads.add(t);
        //int_i
        finalCount += index;
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertU(commit());
    assertQ("Check the total number of docs", req("q", "id:10"), "//result[@numFound=1]");
    StringJoiner queryString = new StringJoiner(" ");
    for (String string : strings) {
        queryString.add(string);
    }
    assertQ("Check the total number of docs", req("q", "cat:" + queryString.toString()), "//result[@numFound=1]");
    assertQ("Check the total number of docs", req("q", "int_i:" + finalCount), "//result[@numFound=1]");
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) StringJoiner(java.util.StringJoiner)

Example 79 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class AtomicUpdateProcessorFactoryTest method testWrongAtomicOpPassed.

public void testWrongAtomicOpPassed() throws Exception {
    AddUpdateCommand cmd = new AddUpdateCommand(new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams().add("processor", "Atomic").add("Atomic.cat", "delete").add("commit", "true")));
    try {
        AtomicUpdateProcessorFactory factory = new AtomicUpdateProcessorFactory();
        factory.inform(h.getCore());
        factory.getInstance(cmd.getReq(), new SolrQueryResponse(), null).processAdd(cmd);
    } catch (SolrException e) {
        assertEquals("Unexpected param(s) for AtomicUpdateProcessor, invalid atomic op passed: 'delete'", e.getMessage());
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 80 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class AtomicUpdateProcessorFactoryTest method testNoUniqueIdPassed.

public void testNoUniqueIdPassed() throws Exception {
    //TODO
    AddUpdateCommand cmd = new AddUpdateCommand(new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams().add("processor", "Atomic").add("Atomic.cat", "add").add("commit", "true")));
    cmd.solrDoc = new SolrInputDocument();
    cmd.solrDoc.addField("title", 1);
    try {
        AtomicUpdateProcessorFactory factory = new AtomicUpdateProcessorFactory();
        factory.inform(h.getCore());
        factory.getInstance(cmd.getReq(), new SolrQueryResponse(), null).processAdd(cmd);
    } catch (SolrException e) {
        assertEquals("Document passed with no unique field: 'id'", e.getMessage());
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Aggregations

LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)107 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)61 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)49 SolrCore (org.apache.solr.core.SolrCore)47 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 Test (org.junit.Test)41 HashMap (java.util.HashMap)32 NamedList (org.apache.solr.common.util.NamedList)26 ArrayList (java.util.ArrayList)23 MapSolrParams (org.apache.solr.common.params.MapSolrParams)21 SolrException (org.apache.solr.common.SolrException)18 List (java.util.List)15 LinkedHashMap (java.util.LinkedHashMap)11 SolrParams (org.apache.solr.common.params.SolrParams)10 SearchComponent (org.apache.solr.handler.component.SearchComponent)10 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)10 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)10 Map (java.util.Map)9 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)9 IOException (java.io.IOException)8