|
|
@@ -10,42 +10,35 @@
|
|
|
package org.elasticsearch.repositories.url;
|
|
|
|
|
|
import fixture.url.URLFixture;
|
|
|
+import io.netty.handler.codec.http.HttpMethod;
|
|
|
|
|
|
import com.carrotsearch.randomizedtesting.annotations.Name;
|
|
|
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
|
|
|
|
|
-import org.apache.http.HttpEntity;
|
|
|
-import org.apache.http.entity.ContentType;
|
|
|
-import org.apache.http.nio.entity.NStringEntity;
|
|
|
import org.elasticsearch.client.Request;
|
|
|
import org.elasticsearch.client.Response;
|
|
|
-import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
|
|
import org.elasticsearch.core.PathUtils;
|
|
|
import org.elasticsearch.repositories.fs.FsRepository;
|
|
|
-import org.elasticsearch.rest.RestStatus;
|
|
|
import org.elasticsearch.test.cluster.ElasticsearchCluster;
|
|
|
+import org.elasticsearch.test.rest.ESRestTestCase;
|
|
|
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
|
|
|
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
|
|
|
-import org.elasticsearch.xcontent.ToXContent;
|
|
|
-import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.ClassRule;
|
|
|
import org.junit.rules.RuleChain;
|
|
|
import org.junit.rules.TestRule;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.net.InetAddress;
|
|
|
import java.net.URI;
|
|
|
-import java.net.URL;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.function.UnaryOperator;
|
|
|
|
|
|
-import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
|
|
|
-import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
+import static org.hamcrest.Matchers.startsWith;
|
|
|
|
|
|
public class RepositoryURLClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
|
|
|
|
|
|
@@ -54,7 +47,7 @@ public class RepositoryURLClientYamlTestSuiteIT extends ESClientYamlSuiteTestCas
|
|
|
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
|
|
|
.module("repository-url")
|
|
|
.setting("path.repo", urlFixture::getRepositoryDir)
|
|
|
- .setting("repositories.url.allowed_urls", () -> "http://snapshot.test*, " + urlFixture.getAddress())
|
|
|
+ .setting("repositories.url.allowed_urls", () -> "http://snapshot.test*, " + urlFixture.getAddress() + "," + urlFixture.getFtpUrl())
|
|
|
.build();
|
|
|
|
|
|
@ClassRule
|
|
|
@@ -75,11 +68,16 @@ public class RepositoryURLClientYamlTestSuiteIT extends ESClientYamlSuiteTestCas
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * This method registers 3 snapshot/restore repositories:
|
|
|
- * - repository-fs: this FS repository is used to create snapshots.
|
|
|
- * - repository-url: this URL repository is used to restore snapshots created using the previous repository. It uses
|
|
|
- * the URLFixture to restore snapshots over HTTP.
|
|
|
- * - repository-file: similar as the previous repository but using a file:// prefix instead of http://.
|
|
|
+ * This method registers 4 snapshot/restore repositories:
|
|
|
+ * <ol>
|
|
|
+ * <li>{@code repository-fs}: this FS repository is used to create snapshots.</li>
|
|
|
+ * <li>{@code repository-url-http}: this URL repository is used to restore snapshots created using the previous repository. It uses
|
|
|
+ * the {@link URLFixture} to restore snapshots over HTTP.</li>
|
|
|
+ * <li>{@code repository-url-file}: similar as the previous repository but using the {@code file://} scheme instead of
|
|
|
+ * {@code http://}.</li>
|
|
|
+ * <li>{@code repository-url-ftp}: similar as the previous repository but using the {@code ftp://} scheme instead of
|
|
|
+ * {@code http://}.</li>
|
|
|
+ * </ol>
|
|
|
**/
|
|
|
@Before
|
|
|
public void registerRepositories() throws IOException {
|
|
|
@@ -97,54 +95,25 @@ public class RepositoryURLClientYamlTestSuiteIT extends ESClientYamlSuiteTestCas
|
|
|
final String pathRepo = pathRepos.get(0);
|
|
|
final URI pathRepoUri = PathUtils.get(pathRepo).toUri().normalize();
|
|
|
|
|
|
- // Create a FS repository using the path.repo location
|
|
|
- Request createFsRepositoryRequest = new Request("PUT", "/_snapshot/repository-fs");
|
|
|
- createFsRepositoryRequest.setEntity(
|
|
|
- buildRepositorySettings(FsRepository.TYPE, Settings.builder().put("location", pathRepo).build())
|
|
|
- );
|
|
|
- Response createFsRepositoryResponse = client().performRequest(createFsRepositoryRequest);
|
|
|
- assertThat(createFsRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
|
|
|
-
|
|
|
- // Create a URL repository using the file://{path.repo} URL
|
|
|
- Request createFileRepositoryRequest = new Request("PUT", "/_snapshot/repository-file");
|
|
|
- createFileRepositoryRequest.setEntity(
|
|
|
- buildRepositorySettings("url", Settings.builder().put("url", pathRepoUri.toString()).build())
|
|
|
- );
|
|
|
- Response createFileRepositoryResponse = client().performRequest(createFileRepositoryRequest);
|
|
|
- assertThat(createFileRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
|
|
|
-
|
|
|
- // Create a URL repository using the http://{fixture} URL
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- List<String> allowedUrls = (List<String>) XContentMapValues.extractValue("defaults.repositories.url.allowed_urls", clusterSettings);
|
|
|
- for (String allowedUrl : allowedUrls) {
|
|
|
- try {
|
|
|
- InetAddress inetAddress = InetAddress.getByName(new URL(allowedUrl).getHost());
|
|
|
- if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress()) {
|
|
|
- Request createUrlRepositoryRequest = new Request("PUT", "/_snapshot/repository-url");
|
|
|
- createUrlRepositoryRequest.setEntity(buildRepositorySettings("url", Settings.builder().put("url", allowedUrl).build()));
|
|
|
- Response createUrlRepositoryResponse = client().performRequest(createUrlRepositoryRequest);
|
|
|
- assertThat(createUrlRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
|
|
|
- break;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- logger.debug("Failed to resolve inet address for allowed URL [{}], skipping", allowedUrl);
|
|
|
- }
|
|
|
- }
|
|
|
+ createRepository("repository-fs", FsRepository.TYPE, b -> b.put("location", pathRepo));
|
|
|
+ createUrlRepository("file", pathRepoUri.toString());
|
|
|
+ createUrlRepository("http", urlFixture.getAddress());
|
|
|
+ createUrlRepository("ftp", urlFixture.getFtpUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void createUrlRepository(final String nameSuffix, final String url) throws IOException {
|
|
|
+ assertThat(url, startsWith(nameSuffix + "://"));
|
|
|
+ createRepository("repository-url-" + nameSuffix, URLRepository.TYPE, b -> b.put("url", url));
|
|
|
}
|
|
|
|
|
|
- private static HttpEntity buildRepositorySettings(final String type, final Settings settings) throws IOException {
|
|
|
- try (XContentBuilder builder = jsonBuilder()) {
|
|
|
- builder.startObject();
|
|
|
- {
|
|
|
- builder.field("type", type);
|
|
|
- builder.startObject("settings");
|
|
|
- {
|
|
|
- settings.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
+ private static void createRepository(final String name, final String type, final UnaryOperator<Settings.Builder> settings)
|
|
|
+ throws IOException {
|
|
|
+ assertOK(client().performRequest(ESRestTestCase.newXContentRequest(HttpMethod.PUT, "/_snapshot/" + name, (builder, params) -> {
|
|
|
+ builder.field("type", type);
|
|
|
+ builder.startObject("settings");
|
|
|
+ settings.apply(Settings.builder()).build().toXContent(builder, params);
|
|
|
builder.endObject();
|
|
|
- return new NStringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
|
|
|
- }
|
|
|
+ return builder;
|
|
|
+ })));
|
|
|
}
|
|
|
}
|