org.elasticsearch.action.support.WriteRequest.RefreshPolicy Java Examples

The following examples show how to use org.elasticsearch.action.support.WriteRequest.RefreshPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomFieldMaskedComplexMappingTest.java    From deprecated-security-advanced-modules with Apache License 2.0 7 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    try {
        tc.admin().indices().create(new CreateIndexRequest("logs").mapping("_doc", FileHelper.loadFile("dlsfls/masked_field_mapping.json"), XContentType.JSON)).actionGet();


        byte[] data = FileHelper.loadFile("dlsfls/logs_bulk_data.json").getBytes(StandardCharsets.UTF_8);
        BulkRequest br = new BulkRequest().add(data, 0, data.length, XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
        if(tc.bulk(br).actionGet().hasFailures()) {
            Assert.fail("bulk import failed");
        }
        Thread.sleep(1000);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }

}
 
Example #2
Source File: DlsTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 10}", XContentType.JSON)).actionGet();
    tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 1500}", XContentType.JSON)).actionGet();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("q");
    System.out.println(Strings.toString(tc.search(new SearchRequest().indices(".opendistro_security")).actionGet()));
    tc.search(new SearchRequest().indices("deals")).actionGet();
}
 
Example #3
Source File: DateMathTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", OpenDistroSecurityUtils.EN_Locale);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        String date = sdf.format(new Date());
        tc.index(new IndexRequest("logstash-"+date).type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1a\", \"ipaddr\": \"10.0.0.0\",\"msgid\": \"12\"}", XContentType.JSON)).actionGet();

        tc.index(new IndexRequest("logstash-"+date).type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1b\", \"ipaddr\": \"10.0.0.1\",\"msgid\": \"14\"}", XContentType.JSON)).actionGet();

        tc.index(new IndexRequest("logstash-1-"+date).type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1c\", \"ipaddr\": \"10.0.0.2\",\"msgid\": \"12\"}", XContentType.JSON)).actionGet();

        tc.index(new IndexRequest("logstash-1-"+date).type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1d\", \"ipaddr\": \"10.0.0.3\",\"msgid\": \"14\"}", XContentType.JSON)).actionGet();
    }
 
Example #4
Source File: FlsFieldsWcTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.admin().indices().create(new CreateIndexRequest("deals")
        .mapping("deals", "timestamp","type=date","@timestamp","type=date")).actionGet();

        try {
            String doc = FileHelper.loadFile("dlsfls/doc1.json");

            for (int i = 0; i < 10; i++) {
                final String moddoc = doc.replace("<name>", "cust" + i).replace("<employees>", "" + i).replace("<date>", "1970-01-02");
                tc.index(new IndexRequest("deals").type("deals").id("0" + i).setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(moddoc, XContentType.JSON)).actionGet();
            }

        } catch (IOException e) {
            Assert.fail(e.toString());
        }

    }
 
Example #5
Source File: FlsFieldsTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.admin().indices().create(new CreateIndexRequest("deals")
        .mapping("deals", "timestamp","type=date","@timestamp","type=date")).actionGet();

        try {
            String doc = FileHelper.loadFile("dlsfls/doc1.json");

            for (int i = 0; i < 10; i++) {
                final String moddoc = doc.replace("<name>", "cust" + i).replace("<employees>", "" + i).replace("<date>", "1970-01-02");
                tc.index(new IndexRequest("deals").type("deals").id("0" + i).setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(moddoc, XContentType.JSON)).actionGet();
            }

        } catch (IOException e) {
            Assert.fail(e.toString());
        }

    }
 
Example #6
Source File: DlsDateMathTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {



    LocalDateTime yesterday = LocalDateTime.now(ZoneId.of("UTC")).minusDays(1);
    LocalDateTime today = LocalDateTime.now(ZoneId.of("UTC"));
    LocalDateTime tomorrow = LocalDateTime.now(ZoneId.of("UTC")).plusDays(1);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    
    tc.index(new IndexRequest("logstash").type("_doc").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"@timestamp\": \""+formatter.format(yesterday)+"\"}", XContentType.JSON)).actionGet();
    tc.index(new IndexRequest("logstash").type("_doc").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"@timestamp\": \""+formatter.format(today)+"\"}", XContentType.JSON)).actionGet();
    tc.index(new IndexRequest("logstash").type("_doc").id("3").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"@timestamp\": \""+formatter.format(tomorrow)+"\"}", XContentType.JSON)).actionGet();
}
 
Example #7
Source File: DlsScrollTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 3}", XContentType.JSON)).actionGet(); //not in

    tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 10}", XContentType.JSON)).actionGet(); //not in

    tc.index(new IndexRequest("deals").type("deals").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 1500}", XContentType.JSON)).actionGet();

    tc.index(new IndexRequest("deals").type("deals").id("4").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 21500}", XContentType.JSON)).actionGet(); //not in

    for(int i=0; i<100; i++) {
        tc.index(new IndexRequest("deals").type("deals").id("gen"+i).setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"amount\": 1500}", XContentType.JSON)).actionGet();
    }
}
 
Example #8
Source File: FlsDlsTestAB.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        //aaa
        tc.index(new IndexRequest("aaa").type("aaa").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_a0\", \"f2\": \"f2_a0\", \"f3\": \"f3_a0\", \"f4\": \"f4_a0\",\"type\": \"a\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("aaa").type("aaa").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_a1\", \"f2\": \"f2_a1\", \"f3\": \"f3_a1\", \"f4\": \"f4_a1\",\"type\": \"a\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("aaa").type("aaa").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_a2\", \"f2\": \"f2_a2\", \"f3\": \"f3_a2\", \"f4\": \"f4_a2\",\"type\": \"x\"}", XContentType.JSON)).actionGet();

        //bbb
        tc.index(new IndexRequest("bbb").type("bbb").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_b0\", \"f2\": \"f2_b0\", \"f3\": \"f3_b0\", \"f4\": \"f4_b0\",\"type\": \"b\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("bbb").type("bbb").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_b1\", \"f2\": \"f2_b1\", \"f3\": \"f3_b1\", \"f4\": \"f4_b1\",\"type\": \"b\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("bbb").type("bbb").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"f1\": \"f1_b2\", \"f2\": \"f2_b2\", \"f3\": \"f3_b2\", \"f4\": \"f4_b2\",\"type\": \"x\"}", XContentType.JSON)).actionGet();

        tc.admin().indices().aliases(new IndicesAliasesRequest().addAliasAction(AliasActions.add().indices("aaa","bbb").alias("abalias"))).actionGet();

    }
 
Example #9
Source File: ElasticsearchIntegrationTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
private boolean uploadFile(Client tc, String filepath, String index, String type) {
    log.info("Will update '" + type + "' with " + filepath);
    try (Reader reader = new FileReader(filepath)) {

        final String id = tc
                .index(new IndexRequest(index).type(type).id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                        .source(type, readXContent(reader, XContentType.YAML))).actionGet().getId();

        if ("0".equals(id)) {
            log.info("   SUCC: Configuration for '" + type + "' created or updated");
            return true;
        } else {
            log.info("   FAIL: Configuration for '" + type
                    + "' failed for unknown reasons. Pls. consult logfile of elasticsearch");
        }
    } catch (Exception e) {
        log.info("   FAIL: Configuration for '" + type + "' failed because of " + e.toString());
    }

    return false;
}
 
Example #10
Source File: AbstractApiAction.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void saveAnUpdateConfigs(final Client client, final RestRequest request, final CType cType,
								   final SecurityDynamicConfiguration<?> configuration, OnSucessActionListener<IndexResponse> actionListener) {
	final IndexRequest ir = new IndexRequest(this.opendistroIndex);

	//final String type = "_doc";
	final String id = cType.toLCString();

	configuration.removeStatic();

	try {
		client.index(ir.id(id)
						.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
						.setIfSeqNo(configuration.getSeqNo())
						.setIfPrimaryTerm(configuration.getPrimaryTerm())
						.source(id, XContentHelper.toXContent(configuration, XContentType.JSON, false)),
				new ConfigUpdatingActionListener<IndexResponse>(client, actionListener));
	} catch (IOException e) {
		throw ExceptionsHelper.convertToElastic(e);
	}
}
 
Example #11
Source File: InternalESSink.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
public boolean doStore(final AuditMessage msg) {

		if (Boolean.parseBoolean((String) HeaderHelper.getSafeFromHeader(threadPool.getThreadContext(), ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER))) {
			if (log.isTraceEnabled()) {
				log.trace("audit log of audit log will not be executed");
			}
			return true;
		}

		try (StoredContext ctx = threadPool.getThreadContext().stashContext()) {
			try {
				final IndexRequestBuilder irb = clientProvider.prepareIndex(getExpandedIndexName(indexPattern, index), type).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setSource(msg.getAsMap());
				threadPool.getThreadContext().putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true");
				irb.setTimeout(TimeValue.timeValueMinutes(1));
				irb.execute().actionGet();
				return true;
			} catch (final Exception e) {
				log.error("Unable to index audit log {} due to {}", msg, e.toString(), e);
				return false;
			}
		}
	}
 
Example #12
Source File: SSLTest.java    From deprecated-security-ssl with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransportClientSSL() throws Exception {

    final Settings settings = Settings.builder().put("opendistro_security.ssl.transport.enabled", true)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_HTTP_ENABLE_OPENSSL_IF_AVAILABLE, allowOpenSSL)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_TRANSPORT_ENABLE_OPENSSL_IF_AVAILABLE, allowOpenSSL)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_TRANSPORT_KEYSTORE_ALIAS, "node-0")
            .put("opendistro_security.ssl.transport.keystore_filepath", getAbsoluteFilePathFromClassPath("node-0-keystore.jks"))
            .put("opendistro_security.ssl.transport.truststore_filepath", getAbsoluteFilePathFromClassPath("truststore.jks"))
            .put("opendistro_security.ssl.transport.enforce_hostname_verification", false)
            .put("opendistro_security.ssl.transport.resolve_hostname", false).build();

    startES(settings);
    
    log.debug("Elasticsearch started");

    final Settings tcSettings = Settings.builder().put("cluster.name", clustername).put(settings).build();

    try (TransportClient tc = new TransportClientImpl(tcSettings, asCollection(OpenDistroSecuritySSLPlugin.class))) {
        
        log.debug("TransportClient built, connect now to {}:{}", nodeHost, nodePort);
        
        tc.addTransportAddress(new TransportAddress(new InetSocketAddress(nodeHost, nodePort)));
        Assert.assertEquals(3, tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());            
        log.debug("TransportClient connected");           
        Assert.assertEquals("test", tc.index(new IndexRequest("test","test").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"a\":5}", XContentType.JSON)).actionGet().getIndex());            
        log.debug("Index created");           
        Assert.assertEquals(1L, tc.search(new SearchRequest("test")).actionGet().getHits().getTotalHits());
        log.debug("Search done");
        Assert.assertEquals(3, tc.admin().cluster().health(new ClusterHealthRequest("test")).actionGet().getNumberOfNodes());
        log.debug("ClusterHealth done");            
        Assert.assertEquals(3, tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());           
        log.debug("NodesInfoRequest asserted");
    }
}
 
Example #13
Source File: ACLDocumentManager.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private BulkResponse writeAcl(ACLDocumentOperation operation, Collection<SearchGuardACLDocument> docs) throws Exception {
    BulkRequestBuilder builder = client.getClient().prepareBulk().setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
    BulkRequest request = operation.buildRequest(this.client.getClient(), builder, docs);
    client.addCommonHeaders();
    return this.client.getClient().bulk(request).actionGet();
}
 
Example #14
Source File: TracingTests.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchScroll() throws Exception {

    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            e.printStackTrace();

        }
    });

final Settings settings = Settings.builder()
        .putList(ConfigConstants.OPENDISTRO_SECURITY_AUTHCZ_REST_IMPERSONATION_USERS+".worf", "knuddel","nonexists")
        .build();
setup(settings);
final RestHelper rh = nonSslRestHelper();

    try (TransportClient tc = getInternalTransportClient()) {
        for(int i=0; i<3; i++)
        tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
    }


    System.out.println("########search");
    HttpResponse res;
    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executeGetRequest("vulcangov/_search?scroll=1m&pretty=true", encodeBasicHeader("admin", "admin"))).getStatusCode());

    System.out.println(res.getBody());
    int start = res.getBody().indexOf("_scroll_id") + 15;
    String scrollid = res.getBody().substring(start, res.getBody().indexOf("\"", start+1));
    System.out.println(scrollid);
    System.out.println("########search scroll");
    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executePostRequest("/_search/scroll?pretty=true", "{\"scroll_id\" : \""+scrollid+"\"}", encodeBasicHeader("admin", "admin"))).getStatusCode());


    System.out.println("########search done");


}
 
Example #15
Source File: BasicAuditlogTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteByQuery() throws Exception {

    final Settings settings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_TRANSPORT, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_REST, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_RESOLVE_BULK_REQUESTS, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "NONE")
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .build();
    setup(settings);

    try (TransportClient tc = getInternalTransportClient()) {
        for(int i=0; i<3; i++)
        tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
    }

    TestAuditlogImpl.clear();

    HttpResponse res;
    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executePostRequest("/vulcango*/_delete_by_query?refresh=true&wait_for_completion=true&pretty=true", "{\"query\" : {\"match_all\" : {}}}", encodeBasicHeader("admin", "admin"))).getStatusCode());
    assertContains(res, "*\"deleted\" : 3,*");
    String auditlogContents = TestAuditlogImpl.sb.toString();
    Assert.assertTrue(auditlogContents.contains("indices:data/write/delete/byquery"));
    Assert.assertTrue(auditlogContents.contains("indices:data/write/bulk"));
    Assert.assertTrue(auditlogContents.contains("indices:data/read/search"));
}
 
Example #16
Source File: BasicAuditlogTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testAliasResolution() throws Exception {

    Settings additionalSettings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_TRANSPORT, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_REST, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_RESOLVE_BULK_REQUESTS, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "NONE")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();

    setup(additionalSettings);


    try (TransportClient tc = getInternalTransportClient()) {
        for(int i=0; i<3; i++)
        tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
        tc.admin().indices().aliases(new IndicesAliasesRequest().addAliasAction(AliasActions.add().alias("thealias").index("vulcangov"))).actionGet();
    }

    TestAuditlogImpl.clear();
    HttpResponse response = rh.executeGetRequest("thealias/_search?pretty", encodeBasicHeader("admin", "admin"));
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
    System.out.println(TestAuditlogImpl.sb.toString());
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("thealias"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("audit_trace_resolved_indices"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("vulcangov"));
    Assert.assertEquals(1, TestAuditlogImpl.messages.size());
    Assert.assertTrue(validateMsgs(TestAuditlogImpl.messages));
    TestAuditlogImpl.clear();
}
 
Example #17
Source File: BasicAuditlogTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testScroll() throws Exception {

    Settings additionalSettings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_TRANSPORT, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_RESOLVE_BULK_REQUESTS, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "NONE")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();

    setup(additionalSettings);

    try (TransportClient tc = getInternalTransportClient()) {
        for(int i=0; i<3; i++)
        tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
    }

    TestAuditlogImpl.clear();

    HttpResponse res;
    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executeGetRequest("vulcangov/_search?scroll=1m&pretty=true", encodeBasicHeader("admin", "admin"))).getStatusCode());
    int start = res.getBody().indexOf("_scroll_id") + 15;
    String scrollid = res.getBody().substring(start, res.getBody().indexOf("\"", start+1));
    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executePostRequest("/_search/scroll?pretty=true", "{\"scroll_id\" : \""+scrollid+"\"}", encodeBasicHeader("admin", "admin"))).getStatusCode());
    Assert.assertEquals(4, TestAuditlogImpl.messages.size());

    Assert.assertEquals(HttpStatus.SC_OK, (res=rh.executeGetRequest("vulcangov/_search?scroll=1m&pretty=true", encodeBasicHeader("admin", "admin"))).getStatusCode());
    start = res.getBody().indexOf("_scroll_id") + 15;
    scrollid = res.getBody().substring(start, res.getBody().indexOf("\"", start+1));
    TestAuditlogImpl.clear();
    Assert.assertEquals(HttpStatus.SC_FORBIDDEN, (res=rh.executePostRequest("/_search/scroll?pretty=true", "{\"scroll_id\" : \""+scrollid+"\"}", encodeBasicHeader("admin2", "admin"))).getStatusCode());
    Thread.sleep(1000);
    System.out.println(TestAuditlogImpl.sb.toString());
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("InternalScrollSearchRequest"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("MISSING_PRIVILEGES"));
    Assert.assertTrue(TestAuditlogImpl.messages.size() > 2);
    Assert.assertTrue(validateMsgs(TestAuditlogImpl.messages));
}
 
Example #18
Source File: ComplianceAuditlogTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdate() throws Exception {

    Settings additionalSettings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_TRANSPORT, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_ENABLE_REST, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_RESOLVE_BULK_REQUESTS, true)
            .put(ConfigConstants.OPENDISTRO_SECURITY_COMPLIANCE_HISTORY_EXTERNAL_CONFIG_ENABLED, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_COMPLIANCE_HISTORY_INTERNAL_CONFIG_ENABLED, false)
            .put(ConfigConstants.OPENDISTRO_SECURITY_COMPLIANCE_HISTORY_WRITE_WATCHED_INDICES, "finance")
            .put(ConfigConstants.OPENDISTRO_SECURITY_COMPLIANCE_HISTORY_READ_WATCHED_FIELDS, "humanresources,Designation,FirstName,LastName")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();

    setup(additionalSettings);


    try (TransportClient tc = getInternalTransportClient()) {
        tc.prepareIndex("humanresources", "employees", "100")
        .setRefreshPolicy(RefreshPolicy.IMMEDIATE)
        .setSource("Age", 456)
        .execute()
        .actionGet();
    }

    TestAuditlogImpl.clear();

    String body = "{\"doc\": {\"Age\":123}}";

    HttpResponse response = rh.executePostRequest("humanresources/employees/100/_update?pretty", body, encodeBasicHeader("admin", "admin"));
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
    System.out.println(response.getBody());
    Thread.sleep(1500);
    Assert.assertTrue(TestAuditlogImpl.messages.isEmpty());
    Assert.assertTrue(validateMsgs(TestAuditlogImpl.messages));
}
 
Example #19
Source File: ElasticsearchIntegrationTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
protected void givenDocumentIsIndexed(String index, String type, String id, XContentBuilder content)
        throws Exception {
    ThreadContext threadContext = esNode1.client().threadPool().getThreadContext();
    try (StoredContext cxt = threadContext.stashContext()) {
        threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
        IndexResponse response = esNode1.client().prepareIndex(index, type, id)
                .setSource(content)
                .setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .execute()
                .get();
        if(!Result.CREATED.equals(response.getResult())){
            throw new RuntimeException("Test setup failed trying to index a document.  Exp. CREATED but was: " + response.getResult());
        }
    }
}
 
Example #20
Source File: FessEsClient.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean store(final String index, final Object obj) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    @SuppressWarnings("unchecked")
    final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj);
    final String id = (String) source.remove(fessConfig.getIndexFieldId());
    source.remove(fessConfig.getIndexFieldVersion());
    final Number seqNo = (Number) source.remove(fessConfig.getIndexFieldSeqNo());
    final Number primaryTerm = (Number) source.remove(fessConfig.getIndexFieldPrimaryTerm());
    IndexResponse response;
    try {
        if (id == null) {
            // TODO throw Exception in next release
            // create
            response =
                    client.prepareIndex().setIndex(index).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                            .setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout());
        } else {
            // create or update
            final IndexRequestBuilder builder =
                    client.prepareIndex().setIndex(index).setId(id).setSource(new DocMap(source))
                            .setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX);
            if (seqNo != null) {
                builder.setIfSeqNo(seqNo.longValue());
            }
            if (primaryTerm != null) {
                builder.setIfPrimaryTerm(primaryTerm.longValue());
            }
            response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        }
        final Result result = response.getResult();
        return result == Result.CREATED || result == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to store: " + obj, e);
    }
}
 
Example #21
Source File: CustomFieldMaskedTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust1\", \"street\":\"testroad\"}, \"ip_source\": \"100.100.1.1\",\"ip_dest\": \"123.123.1.1\",\"amount\": 10, \"mynum\": 1000000000000000000}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust2\", \"street\":\"testroad\"}, \"ip_source\": \"100.100.2.2\",\"ip_dest\": \"123.123.2.2\",\"amount\": 20, \"mynum\": 1000000000000000000}", XContentType.JSON)).actionGet();


        for (int i=0; i<30;i++) {
            tc.index(new IndexRequest("deals").type("deals").id("a"+i).setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                    .source("{\"customer\": {\"name\":\"cust1\", \"street\":\"testroad\"}, \"ip_source\": \"200.100.1.1\",\"ip_dest\": \"123.123.1.1\",\"amount\": 10, \"mynum\": 1000000000000000000}", XContentType.JSON)).actionGet();
        }

     }
 
Example #22
Source File: FessEsClient.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean delete(final String index, final String id, final Number seqNo, final Number primaryTerm) {
    try {
        final DeleteRequestBuilder builder = client.prepareDelete().setIndex(index).setId(id).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
        if (seqNo != null) {
            builder.setIfSeqNo(seqNo.longValue());
        }
        if (primaryTerm != null) {
            builder.setIfPrimaryTerm(primaryTerm.longValue());
        }
        final DeleteResponse response = builder.execute().actionGet(ComponentUtil.getFessConfig().getIndexDeleteTimeout());
        return response.getResult() == Result.DELETED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to delete: " + index + "/" + id + "@" + seqNo + ":" + primaryTerm, e);
    }
}
 
Example #23
Source File: DlsNestedTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    String mapping = "{" +
            "        \"mytype\" : {" +
            "            \"properties\" : {" +
            "                \"amount\" : {\"type\": \"integer\"}," +
            "                \"owner\" : {\"type\": \"text\"}," +
            "                \"my_nested_object\" : {\"type\" : \"nested\"}" +
            "            }" +
            "        }" +
            "    }" +
            "";

    tc.admin().indices().create(new CreateIndexRequest("deals")
    .settings(Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build())
    .mapping("mytype", mapping, XContentType.JSON)).actionGet();

    //tc.index(new IndexRequest("deals").type("mytype").id("3").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
    //        .source("{\"amount\": 7,\"owner\": \"a\", \"my_nested_object\" : {\"name\": \"spock\"}}", XContentType.JSON)).actionGet();
    //tc.index(new IndexRequest("deals").type("mytype").id("4").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
    //        .source("{\"amount\": 8, \"my_nested_object\" : {\"name\": \"spock\"}}", XContentType.JSON)).actionGet();
    //tc.index(new IndexRequest("deals").type("mytype").id("5").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
    //        .source("{\"amount\": 1400,\"owner\": \"a\", \"my_nested_object\" : {\"name\": \"spock\"}}", XContentType.JSON)).actionGet();
    tc.index(new IndexRequest("deals").type("mytype").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 1500,\"owner\": \"b\", \"my_nested_object\" : {\"name\": \"spock\"}}", XContentType.JSON)).actionGet();
}
 
Example #24
Source File: MultitenancyTests.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testKibanaAlias() throws Exception {
    final Settings settings = Settings.builder()
            .build();
    setup(settings);

    try (TransportClient tc = getInternalTransportClient()) {
        String body = "{\"buildNum\": 15460, \"defaultIndex\": \"humanresources\", \"tenant\": \"human_resources\"}";
        Map indexSettings = new HashMap();
        indexSettings.put("number_of_shards", 1);
        indexSettings.put("number_of_replicas", 0);
        tc.admin().indices().create(new CreateIndexRequest(".kibana-6")
            .alias(new Alias(".kibana"))
            .settings(indexSettings))
            .actionGet();

        tc.index(new IndexRequest(".kibana-6").type("doc").id("6.2.2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(body, XContentType.JSON)).actionGet();
    }

    final RestHelper rh = nonSslRestHelper();

    HttpResponse res;
    Assert.assertEquals(HttpStatus.SC_OK, (res = rh.executeGetRequest(".kibana-6/doc/6.2.2?pretty", encodeBasicHeader("kibanaro", "kibanaro"))).getStatusCode());
    Assert.assertEquals(HttpStatus.SC_OK, (res = rh.executeGetRequest(".kibana/doc/6.2.2?pretty", encodeBasicHeader("kibanaro", "kibanaro"))).getStatusCode());

    System.out.println(res.getBody());

}
 
Example #25
Source File: MultitenancyTests.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testKibanaAlias65() throws Exception {
    final Settings settings = Settings.builder()
            .build();
    setup(settings);

    try (TransportClient tc = getInternalTransportClient()) {
        String body = "{\"buildNum\": 15460, \"defaultIndex\": \"humanresources\", \"tenant\": \"human_resources\"}";
        Map indexSettings = new HashMap();
        indexSettings.put("number_of_shards", 1);
        indexSettings.put("number_of_replicas", 0);
        tc.admin().indices().create(new CreateIndexRequest(".kibana_1")
            .alias(new Alias(".kibana"))
            .settings(indexSettings))
            .actionGet();

        tc.index(new IndexRequest(".kibana_1").type("doc").id("6.2.2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(body, XContentType.JSON)).actionGet();
        tc.index(new IndexRequest(".kibana_-900636979_kibanaro").type("doc").id("6.2.2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(body, XContentType.JSON)).actionGet();

    }

    final RestHelper rh = nonSslRestHelper();

    HttpResponse res;
    Assert.assertEquals(HttpStatus.SC_OK, (res = rh.executeGetRequest(".kibana/doc/6.2.2?pretty", new BasicHeader("securitytenant", "__user__"), encodeBasicHeader("kibanaro", "kibanaro"))).getStatusCode());
    System.out.println(res.getBody());
    Assert.assertTrue(res.getBody().contains(".kibana_-900636979_kibanaro"));
}
 
Example #26
Source File: DlsPropsReplaceTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("prop1").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"prop_replace\": \"yes\", \"amount\": 1010}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("prop1").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"prop_replace\": \"no\", \"amount\": 2020}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("prop2").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"role\": \"prole1\", \"amount\": 3030}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("prop2").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"role\": \"prole2\", \"amount\": 4040}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("prop2").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"role\": \"prole3\", \"amount\": 5050}", XContentType.JSON)).actionGet();

    }
 
Example #27
Source File: FlsTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust1\"}, \"zip\": \"12345\",\"secret\": \"tellnoone\",\"amount\": 10}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust2\", \"ctype\":\"industry\"}, \"amount\": 1500}", XContentType.JSON)).actionGet();
    }
 
Example #28
Source File: FlsDlsTestForbiddenField.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust1\"}, \"zip\": \"12345\",\"secret\": \"tellnoone\",\"amount\": 10}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust2\", \"ctype\":\"industry\"}, \"amount\": 1500}", XContentType.JSON)).actionGet();

    }
 
Example #29
Source File: FlsDlsTestMulti.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust1\"}, \"zip\": \"12345\",\"secret\": \"tellnoone\",\"amount\": 10}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust2\", \"ctype\":\"industry\"}, \"amount\": 1500}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust3\", \"ctype\":\"industry\"}, \"amount\": 200}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("deals").type("deals").id("3").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"customer\": {\"name\":\"cust4\", \"ctype\":\"industry\"}, \"amount\": 20001}", XContentType.JSON)).actionGet();



    }
 
Example #30
Source File: IndexPatternTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.index(new IndexRequest("logstash-2016").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1a\", \"ipaddr\": \"10.0.0.0\",\"msgid\": \"12\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("logstash-2016").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1b\", \"ipaddr\": \"10.0.0.1\",\"msgid\": \"14\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("logstash-2018").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1c\", \"ipaddr\": \"10.0.0.2\",\"msgid\": \"12\"}", XContentType.JSON)).actionGet();
        tc.index(new IndexRequest("logstash-2018").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                .source("{\"message\":\"mymsg1d\", \"ipaddr\": \"10.0.0.3\",\"msgid\": \"14\"}", XContentType.JSON)).actionGet();
            }