|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.repositories.s3; |
| 11 | + |
| 12 | +import fixture.s3.S3HttpFixture; |
| 13 | +import fixture.s3.S3HttpHandler; |
| 14 | + |
| 15 | +import com.sun.net.httpserver.HttpExchange; |
| 16 | +import com.sun.net.httpserver.HttpHandler; |
| 17 | + |
| 18 | +import org.elasticsearch.ExceptionsHelper; |
| 19 | +import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; |
| 20 | +import org.elasticsearch.action.admin.cluster.repositories.put.TransportPutRepositoryAction; |
| 21 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest; |
| 22 | +import org.elasticsearch.action.admin.cluster.snapshots.create.TransportCreateSnapshotAction; |
| 23 | +import org.elasticsearch.common.settings.MockSecureSettings; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.util.CollectionUtils; |
| 26 | +import org.elasticsearch.core.SuppressForbidden; |
| 27 | +import org.elasticsearch.plugins.Plugin; |
| 28 | +import org.elasticsearch.snapshots.SnapshotState; |
| 29 | +import org.elasticsearch.test.ESSingleNodeTestCase; |
| 30 | +import org.hamcrest.Matcher; |
| 31 | +import org.junit.runner.Description; |
| 32 | +import org.junit.runners.model.Statement; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 39 | +import static org.hamcrest.Matchers.hasItem; |
| 40 | +import static org.hamcrest.Matchers.not; |
| 41 | + |
| 42 | +public class AddPurposeCustomQueryParameterTests extends ESSingleNodeTestCase { |
| 43 | + |
| 44 | + @Override |
| 45 | + protected Collection<Class<? extends Plugin>> getPlugins() { |
| 46 | + return CollectionUtils.appendToCopyNoNullElements(super.getPlugins(), S3RepositoryPlugin.class); |
| 47 | + } |
| 48 | + |
| 49 | + private static final String TEST_ACCESS_KEY = "test-access-key"; |
| 50 | + |
| 51 | + @Override |
| 52 | + protected Settings nodeSettings() { |
| 53 | + final var secureSettings = new MockSecureSettings(); |
| 54 | + for (final var clientName : List.of("default", "with_purpose", "without_purpose")) { |
| 55 | + secureSettings.setString( |
| 56 | + S3ClientSettings.ACCESS_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), |
| 57 | + TEST_ACCESS_KEY |
| 58 | + ); |
| 59 | + secureSettings.setString( |
| 60 | + S3ClientSettings.SECRET_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), |
| 61 | + randomAlphaOfLengthBetween(14, 20) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return Settings.builder() |
| 66 | + .put(super.nodeSettings()) |
| 67 | + .put(S3ClientSettings.REGION.getConcreteSettingForNamespace("default").getKey(), randomIdentifier()) |
| 68 | + .put(S3ClientSettings.ADD_PURPOSE_CUSTOM_QUERY_PARAMETER.getConcreteSettingForNamespace("with_purpose").getKey(), "true") |
| 69 | + .put(S3ClientSettings.ADD_PURPOSE_CUSTOM_QUERY_PARAMETER.getConcreteSettingForNamespace("without_purpose").getKey(), "false") |
| 70 | + .setSecureSettings(secureSettings) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + private static final Matcher<Iterable<? super String>> HAS_CUSTOM_QUERY_PARAMETER = hasItem(S3BlobStore.CUSTOM_QUERY_PARAMETER_PURPOSE); |
| 75 | + private static final Matcher<Iterable<? super String>> NO_CUSTOM_QUERY_PARAMETER = not(HAS_CUSTOM_QUERY_PARAMETER); |
| 76 | + |
| 77 | + public void testCustomQueryParameterConfiguration() throws Throwable { |
| 78 | + final var indexName = randomIdentifier(); |
| 79 | + createIndex(indexName); |
| 80 | + prepareIndex(indexName).setSource("foo", "bar").get(); |
| 81 | + |
| 82 | + final var bucket = randomIdentifier(); |
| 83 | + final var basePath = randomIdentifier(); |
| 84 | + |
| 85 | + runCustomQueryParameterTest(bucket, basePath, null, Settings.EMPTY, NO_CUSTOM_QUERY_PARAMETER); |
| 86 | + runCustomQueryParameterTest(bucket, basePath, "default", Settings.EMPTY, NO_CUSTOM_QUERY_PARAMETER); |
| 87 | + runCustomQueryParameterTest(bucket, basePath, "without_purpose", Settings.EMPTY, NO_CUSTOM_QUERY_PARAMETER); |
| 88 | + runCustomQueryParameterTest(bucket, basePath, "with_purpose", Settings.EMPTY, HAS_CUSTOM_QUERY_PARAMETER); |
| 89 | + |
| 90 | + final var falseRepositorySetting = Settings.builder().put("add_purpose_custom_query_parameter", false).build(); |
| 91 | + final var trueRepositorySetting = Settings.builder().put("add_purpose_custom_query_parameter", true).build(); |
| 92 | + for (final var clientName : new String[] { null, "default", "with_purpose", "without_purpose" }) { |
| 93 | + // client name doesn't matter if repository setting specified |
| 94 | + runCustomQueryParameterTest(bucket, basePath, clientName, falseRepositorySetting, NO_CUSTOM_QUERY_PARAMETER); |
| 95 | + runCustomQueryParameterTest(bucket, basePath, clientName, trueRepositorySetting, HAS_CUSTOM_QUERY_PARAMETER); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void runCustomQueryParameterTest( |
| 100 | + String bucket, |
| 101 | + String basePath, |
| 102 | + String clientName, |
| 103 | + Settings extraRepositorySettings, |
| 104 | + Matcher<Iterable<? super String>> queryParamMatcher |
| 105 | + ) throws Throwable { |
| 106 | + final var httpFixture = new S3HttpFixture(true, bucket, basePath, TEST_ACCESS_KEY) { |
| 107 | + |
| 108 | + @SuppressForbidden(reason = "this test uses a HttpServer to emulate an S3 endpoint") |
| 109 | + class AssertingHandler extends S3HttpHandler { |
| 110 | + AssertingHandler() { |
| 111 | + super(bucket, basePath); |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressForbidden(reason = "this test uses a HttpServer to emulate an S3 endpoint") |
| 115 | + @Override |
| 116 | + public void handle(HttpExchange exchange) throws IOException { |
| 117 | + try { |
| 118 | + assertThat( |
| 119 | + parseRequestComponents(exchange.getRequestMethod() + " " + exchange.getRequestURI().toString()) |
| 120 | + .customQueryParameters() |
| 121 | + .keySet(), |
| 122 | + queryParamMatcher |
| 123 | + ); |
| 124 | + super.handle(exchange); |
| 125 | + } catch (Error e) { |
| 126 | + // HttpServer catches Throwable, so we must throw errors on another thread |
| 127 | + ExceptionsHelper.maybeDieOnAnotherThread(e); |
| 128 | + throw e; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @SuppressForbidden(reason = "this test uses a HttpServer to emulate an S3 endpoint") |
| 134 | + @Override |
| 135 | + protected HttpHandler createHandler() { |
| 136 | + return new AssertingHandler(); |
| 137 | + } |
| 138 | + }; |
| 139 | + httpFixture.apply(new Statement() { |
| 140 | + @Override |
| 141 | + public void evaluate() { |
| 142 | + final var repoName = randomIdentifier(); |
| 143 | + assertAcked( |
| 144 | + client().execute( |
| 145 | + TransportPutRepositoryAction.TYPE, |
| 146 | + new PutRepositoryRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, repoName).type(S3Repository.TYPE) |
| 147 | + .settings( |
| 148 | + Settings.builder() |
| 149 | + .put("bucket", bucket) |
| 150 | + .put("base_path", basePath) |
| 151 | + .put("endpoint", httpFixture.getAddress()) |
| 152 | + .put(clientName == null ? Settings.EMPTY : Settings.builder().put("client", clientName).build()) |
| 153 | + .put(extraRepositorySettings) |
| 154 | + ) |
| 155 | + ) |
| 156 | + ); |
| 157 | + |
| 158 | + assertEquals( |
| 159 | + SnapshotState.SUCCESS, |
| 160 | + client().execute( |
| 161 | + TransportCreateSnapshotAction.TYPE, |
| 162 | + new CreateSnapshotRequest(TEST_REQUEST_TIMEOUT, repoName, randomIdentifier()).waitForCompletion(true) |
| 163 | + ).actionGet(SAFE_AWAIT_TIMEOUT).getSnapshotInfo().state() |
| 164 | + ); |
| 165 | + } |
| 166 | + }, Description.EMPTY).evaluate(); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments