Skip to content

Commit

Permalink
Replace usages with HLRC in plugin examples with new client (elastic#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira authored Nov 17, 2021
1 parent c628941 commit 5ade8d7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 64 deletions.
12 changes: 8 additions & 4 deletions plugins/examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ subprojects {
targetCompatibility = 11

repositories {
if (!gradle.includedBuilds) {
// Only necessary when building plugins against SNAPSHOT versions of Elasticsearch
maven {
url = 'https://snapshots.elastic.co/maven/'
// Only necessary when building plugins against SNAPSHOT versions of Elasticsearch
maven {
url = 'https://snapshots.elastic.co/maven/'
mavenContent {
if (gradle.includedBuilds) {
// When building in a composite, restrict this to only resolve the Java REST client
includeModule 'co.elastic.clients', 'elasticsearch-java'
}
}
}

Expand Down
28 changes: 8 additions & 20 deletions plugins/examples/security-authorization-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@ esplugin {
noticeFile rootProject.file('NOTICE.txt')
}

sourceSets {
javaRestTest {
// let the javaRestTest see the classpath of main
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}

if (!gradle.includedBuilds) {
configurations.all {
resolutionStrategy.dependencySubstitution {
// Temporary workaround for https://github.com/elastic/elasticsearch/issues/77602
substitute module('org.elasticsearch:server') using module("org.elasticsearch:elasticsearch:${elasticsearchVersion}")
substitute module('org.elasticsearch.client:rest') using module("org.elasticsearch.client:elasticsearch-rest-client:${elasticsearchVersion}")
}
}
}

dependencies {
compileOnly "org.elasticsearch.plugin:x-pack-core:${elasticsearchVersion}"
testImplementation "org.apache.logging.log4j:log4j-core:2.11.1"
testImplementation "org.elasticsearch.plugin:x-pack-core:${elasticsearchVersion}"
javaRestTestImplementation "org.elasticsearch.plugin:x-pack-core:${elasticsearchVersion}"
javaRestTestImplementation "org.elasticsearch.client:elasticsearch-rest-high-level-client:${elasticsearchVersion}"
javaRestTestImplementation "org.apache.logging.log4j:log4j-core:2.11.1"
javaRestTestImplementation("co.elastic.clients:elasticsearch-java:${elasticsearchVersion}") {
// See: https://github.com/elastic/elasticsearch-java/issues/47
exclude module: 'jakarta.json-api'
}
javaRestTestImplementation "org.elasticsearch.client:elasticsearch-rest-client:${elasticsearchVersion}"
javaRestTestImplementation "org.elasticsearch:elasticsearch:${elasticsearchVersion}"
javaRestTestImplementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
javaRestTestImplementation "org.elasticsearch.test:framework:${elasticsearchVersion}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

package org.elasticsearch.example;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.security.PutUserRequest;
import org.elasticsearch.client.security.RefreshPolicy;
import org.elasticsearch.client.security.user.User;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand All @@ -26,10 +26,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

Expand All @@ -50,9 +47,13 @@ protected Settings restClientSettings() {
}

public void testClusterAction() throws IOException {
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
ElasticsearchClient restClient = new ElasticsearchClient(new RestClientTransport(client(), new JacksonJsonpMapper()));
restClient.security().putUser(req -> req
.username("custom_user")
.roles("custom_superuser")
.password("x-pack-test-password")
.enabled(true)
);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
Expand All @@ -65,8 +66,12 @@ public void testClusterAction() throws IOException {
}

{
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(req -> req
.username("custom_user2")
.roles("not_superuser")
.password("x-pack-test-password")
.enabled(true)
);
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user2", new SecureString("x-pack-test-password".toCharArray())));
Expand All @@ -78,9 +83,13 @@ public void testClusterAction() throws IOException {
}

public void testIndexAction() throws IOException {
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
ElasticsearchClient restClient = new ElasticsearchClient(new RestClientTransport(client(), new JacksonJsonpMapper()));
restClient.security().putUser(req -> req
.username("custom_user")
.roles("custom_superuser")
.password("x-pack-test-password")
.enabled(true)
);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
Expand All @@ -93,8 +102,12 @@ public void testIndexAction() throws IOException {
}

{
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(req -> req
.username("custom_user2")
.roles("not_superuser")
.password("x-pack-test-password")
.enabled(true)
);
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user2", new SecureString("x-pack-test-password".toCharArray())));
Expand All @@ -106,13 +119,25 @@ public void testIndexAction() throws IOException {
}

public void testRunAs() throws IOException {
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user3", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
ElasticsearchClient restClient = new ElasticsearchClient(new RestClientTransport(client(), new JacksonJsonpMapper()));
restClient.security().putUser(req -> req
.username("custom_user")
.roles("custom_superuser")
.password("x-pack-test-password")
.enabled(true)
);
restClient.security().putUser(req -> req
.username("custom_user2")
.roles("custom_superuser")
.password("x-pack-test-password")
.enabled(true)
);
restClient.security().putUser(req -> req
.username("custom_user3")
.roles("not_superuser")
.password("x-pack-test-password")
.enabled(true)
);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
Expand Down Expand Up @@ -149,10 +174,4 @@ public void testRunAs() throws IOException {
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
}
}

private class TestRestHighLevelClient extends RestHighLevelClient {
TestRestHighLevelClient() {
super(client(), restClient -> {}, Collections.emptyList());
}
}
}
14 changes: 4 additions & 10 deletions plugins/examples/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ rootDir.listFiles().findAll { it.directory && new File(it, 'build.gradle').exist
gradle.rootProject {
ext {
// Fetch Elasticsearch version from outer build
if (!gradle.includedBuilds) {
new File(rootDir.parentFile.parentFile, 'build-tools-internal/version.properties').withInputStream { is ->
def props = new Properties()
props.load(is)
elasticsearchVersion = "${props.get('elasticsearch')}-SNAPSHOT"
}
} else {
// In a composite we substitute these dependencies so the version doesn't matter
elasticsearchVersion = '0.0.1-SNAPSHOT'
new File(rootDir.parentFile.parentFile, 'build-tools-internal/version.properties').withInputStream { is ->
def props = new Properties()
props.load(is)
elasticsearchVersion = "${props.get('elasticsearch')}-SNAPSHOT"
}
}
}
Expand All @@ -34,7 +29,6 @@ gradle.projectsEvaluated {
resolutionStrategy.dependencySubstitution {
// When using composite builds we need to tell Gradle to use the project names since we rename the published artifacts
substitute module('org.elasticsearch:elasticsearch') using module("org.elasticsearch:server:${elasticsearchVersion}")
substitute module('org.elasticsearch.client:elasticsearch-rest-high-level-client') using module("org.elasticsearch.client:rest-high-level:${elasticsearchVersion}")
substitute module('org.elasticsearch.client:elasticsearch-rest-client') using module("org.elasticsearch.client:rest:${elasticsearchVersion}")
substitute module('org.elasticsearch.plugin:x-pack-core') with module("org.elasticsearch.plugin:core:${elasticsearchVersion}")
substitute module('org.elasticsearch.plugin:elasticsearch-scripting-painless-spi') with module("org.elasticsearch.plugin:spi:${elasticsearchVersion}")
Expand Down

0 comments on commit 5ade8d7

Please sign in to comment.