diff --git a/plugins/examples/build.gradle b/plugins/examples/build.gradle index 0c372013cd883..134e89fd5dd73 100644 --- a/plugins/examples/build.gradle +++ b/plugins/examples/build.gradle @@ -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' + } } } diff --git a/plugins/examples/security-authorization-engine/build.gradle b/plugins/examples/security-authorization-engine/build.gradle index a12c426c4565c..11b7fd2cefc2d 100644 --- a/plugins/examples/security-authorization-engine/build.gradle +++ b/plugins/examples/security-authorization-engine/build.gradle @@ -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}" } diff --git a/plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineIT.java b/plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineIT.java index 63ca6b71d8f64..df2911cfdd3d6 100644 --- a/plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineIT.java +++ b/plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineIT.java @@ -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; @@ -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; @@ -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(); @@ -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()))); @@ -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(); @@ -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()))); @@ -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(); @@ -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()); - } - } } diff --git a/plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java b/plugins/examples/security-authorization-engine/src/test/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java similarity index 100% rename from plugins/examples/security-authorization-engine/src/javaRestTest/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java rename to plugins/examples/security-authorization-engine/src/test/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java diff --git a/plugins/examples/settings.gradle b/plugins/examples/settings.gradle index 436a65c85227a..14ccdcb71d869 100644 --- a/plugins/examples/settings.gradle +++ b/plugins/examples/settings.gradle @@ -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" } } } @@ -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}")