Skip to content

Add workaround for missing header in snapshot request #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class RestClientTransport implements ElasticsearchTransport {
Expand Down Expand Up @@ -331,9 +334,17 @@ private <ResponseT> ResponseT decodeResponse(
}
}

// Endpoints that (incorrectly) do not return the Elastic product header
private static final Set<String> endpointsMissingProductHeader = new HashSet<>(Arrays.asList(
"es/snapshot.create" // #74 / elastic/elasticsearch#82358
));

private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint) throws IOException {
String header = clientResp.getHeader("X-Elastic-Product");
if (header == null) {
if (endpointsMissingProductHeader.contains(endpoint.id())) {
return;
}
throw new TransportException(
"Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch "
+ "instance, and that any networking filters are preserving that header.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
import co.elastic.clients.elasticsearch.indices.IndexState;
import co.elastic.clients.elasticsearch.model.ModelTestCase;
import co.elastic.clients.elasticsearch.snapshot.CreateRepositoryResponse;
import co.elastic.clients.elasticsearch.snapshot.CreateSnapshotResponse;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
Expand Down Expand Up @@ -76,17 +78,19 @@ public class RequestTest extends Assert {

@BeforeClass
public static void setup() {
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.15.1")
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.16.2")
.withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")
.withEnv("path.repo", "/tmp") // for snapshots
.withStartupTimeout(Duration.ofSeconds(30))
.withPassword("changeme");
container.start();
int port = container.getMappedPort(9200);

BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
credsProv.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
);
restClient = RestClient.builder(new HttpHost("localhost", container.getMappedPort(9200)))
restClient = RestClient.builder(new HttpHost("localhost", port))
.setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(credsProv))
.build();
transport = new RestClientTransport(restClient, mapper);
Expand Down Expand Up @@ -394,6 +398,30 @@ public void testDefaultIndexSettings() throws IOException {
assertNull(settings.get(index).defaults());
}

@Test
public void testSnapshotCreation() throws IOException {
// https://github.com/elastic/elasticsearch-java/issues/74
// https://github.com/elastic/elasticsearch/issues/82358

CreateRepositoryResponse repo = client.snapshot().createRepository(b1 -> b1
.name("test")
.type("fs")
.settings(b2 -> b2
.location("/tmp/test-repo")
)
);

assertTrue(repo.acknowledged());

CreateSnapshotResponse snapshot = client.snapshot().create(b -> b
.repository("test")
.snapshot("1")
.waitForCompletion(true)
);

assertNotNull(snapshot.snapshot());
}

@Test
public void testValueBodyResponse() throws Exception {
DiskUsageResponse resp = client.indices().diskUsage(b -> b
Expand Down