|
| 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.aws.DynamicRegionSupplier; |
| 13 | +import fixture.s3.S3HttpFixture; |
| 14 | +import fixture.s3.S3HttpHandler; |
| 15 | + |
| 16 | +import com.carrotsearch.randomizedtesting.annotations.SuppressForbidden; |
| 17 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; |
| 18 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 19 | +import com.sun.net.httpserver.HttpExchange; |
| 20 | +import com.sun.net.httpserver.HttpHandler; |
| 21 | + |
| 22 | +import org.elasticsearch.ExceptionsHelper; |
| 23 | +import org.elasticsearch.common.ReferenceDocs; |
| 24 | +import org.elasticsearch.common.Strings; |
| 25 | +import org.elasticsearch.common.io.Streams; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 28 | +import org.elasticsearch.test.cluster.LogType; |
| 29 | +import org.elasticsearch.test.fixtures.testcontainers.TestContainersThreadFilter; |
| 30 | +import org.junit.ClassRule; |
| 31 | +import org.junit.rules.RuleChain; |
| 32 | +import org.junit.rules.TestRule; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.function.Supplier; |
| 36 | +import java.util.function.UnaryOperator; |
| 37 | + |
| 38 | +import static fixture.aws.AwsCredentialsUtils.fixedAccessKey; |
| 39 | +import static org.hamcrest.Matchers.allOf; |
| 40 | +import static org.hamcrest.Matchers.containsString; |
| 41 | +import static org.hamcrest.Matchers.hasItem; |
| 42 | + |
| 43 | +@ThreadLeakFilters(filters = { TestContainersThreadFilter.class }) |
| 44 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) // https://github.com/elastic/elasticsearch/issues/102482 |
| 45 | +@SuppressForbidden("HttpExchange and Headers are ok here") |
| 46 | +public class RepositoryS3ConditionalWritesUnsupportedRestIT extends AbstractRepositoryS3RestTestCase { |
| 47 | + |
| 48 | + private static final String PREFIX = getIdentifierPrefix("RepositoryS3BasicCredentialsRestIT"); |
| 49 | + private static final String BUCKET = PREFIX + "bucket"; |
| 50 | + private static final String BASE_PATH = PREFIX + "base_path"; |
| 51 | + private static final String ACCESS_KEY = PREFIX + "access-key"; |
| 52 | + private static final String SECRET_KEY = PREFIX + "secret-key"; |
| 53 | + private static final String CLIENT = "no_conditional_writes_client"; |
| 54 | + |
| 55 | + private static final Supplier<String> regionSupplier = new DynamicRegionSupplier(); |
| 56 | + |
| 57 | + private static final S3HttpFixture s3Fixture = new S3HttpFixture( |
| 58 | + true, |
| 59 | + BUCKET, |
| 60 | + BASE_PATH, |
| 61 | + fixedAccessKey(ACCESS_KEY, regionSupplier, "s3") |
| 62 | + ) { |
| 63 | + @Override |
| 64 | + @SuppressForbidden("HttpExchange and Headers are ok here") |
| 65 | + protected HttpHandler createHandler() { |
| 66 | + return new AssertNoConditionalWritesHandler(asInstanceOf(S3HttpHandler.class, super.createHandler())); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + @SuppressForbidden("HttpExchange and Headers are ok here") |
| 71 | + private static class AssertNoConditionalWritesHandler implements HttpHandler { |
| 72 | + |
| 73 | + private final S3HttpHandler delegateHandler; |
| 74 | + |
| 75 | + private AssertNoConditionalWritesHandler(S3HttpHandler delegateHandler) { |
| 76 | + this.delegateHandler = delegateHandler; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void handle(HttpExchange exchange) throws IOException { |
| 81 | + if (exchange.getRequestHeaders().containsKey("if-match") || exchange.getRequestHeaders().containsKey("if-none-match")) { |
| 82 | + final var exception = new AssertionError( |
| 83 | + Strings.format( |
| 84 | + "unsupported conditional write: [%s] with headers [%s]", |
| 85 | + delegateHandler.parseRequest(exchange), |
| 86 | + exchange.getRequestHeaders() |
| 87 | + ) |
| 88 | + ); |
| 89 | + ExceptionsHelper.maybeDieOnAnotherThread(exception); |
| 90 | + throw exception; |
| 91 | + } |
| 92 | + delegateHandler.handle(exchange); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected Settings extraRepositorySettings() { |
| 98 | + return Settings.builder() |
| 99 | + .put(super.extraRepositorySettings()) |
| 100 | + .put(S3Repository.UNSAFELY_INCOMPATIBLE_WITH_S3_WRITES.getKey(), true) |
| 101 | + .build(); |
| 102 | + } |
| 103 | + |
| 104 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 105 | + .module("repository-s3") |
| 106 | + .systemProperty("aws.region", regionSupplier) |
| 107 | + .keystore("s3.client." + CLIENT + ".access_key", ACCESS_KEY) |
| 108 | + .keystore("s3.client." + CLIENT + ".secret_key", SECRET_KEY) |
| 109 | + .setting("s3.client." + CLIENT + ".endpoint", s3Fixture::getAddress) |
| 110 | + .build(); |
| 111 | + |
| 112 | + @ClassRule |
| 113 | + public static TestRule ruleChain = RuleChain.outerRule(s3Fixture).around(cluster); |
| 114 | + |
| 115 | + @Override |
| 116 | + protected String getTestRestCluster() { |
| 117 | + return cluster.getHttpAddresses(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected String getBucketName() { |
| 122 | + return BUCKET; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected String getBasePath() { |
| 127 | + return BASE_PATH; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected String getClientName() { |
| 132 | + return CLIENT; |
| 133 | + } |
| 134 | + |
| 135 | + public void testWarningLog() throws IOException { |
| 136 | + final var repoName = randomIdentifier(); |
| 137 | + final var testRepository = new TestRepository(repoName, getClientName(), getBucketName(), getBasePath(), extraRepositorySettings()); |
| 138 | + try (var ignored = testRepository.register(UnaryOperator.identity()); var logStream = cluster.getNodeLog(0, LogType.SERVER)) { |
| 139 | + assertThat( |
| 140 | + Streams.readAllLines(logStream), |
| 141 | + hasItem( |
| 142 | + allOf( |
| 143 | + containsString("WARN"), |
| 144 | + containsString(repoName), |
| 145 | + containsString(""" |
| 146 | + is configured to unsafely avoid conditional writes which may lead to repository corruption; to resolve this \ |
| 147 | + warning, upgrade your storage to a system that is fully compatible with AWS S3 and then remove the \ |
| 148 | + [unsafely_incompatible_with_s3_conditional_writes] repository setting"""), |
| 149 | + containsString(ReferenceDocs.S3_COMPATIBLE_REPOSITORIES.toString()) |
| 150 | + ) |
| 151 | + ) |
| 152 | + ); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments