|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.repositories.s3; |
| 20 | + |
| 21 | +import com.amazonaws.services.s3.AmazonS3; |
| 22 | + |
| 23 | +import org.elasticsearch.client.node.NodeClient; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.settings.SettingsFilter; |
| 26 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 27 | +import org.elasticsearch.env.Environment; |
| 28 | +import org.elasticsearch.plugins.Plugin; |
| 29 | +import org.elasticsearch.repositories.Repository; |
| 30 | +import org.elasticsearch.rest.AbstractRestChannel; |
| 31 | +import org.elasticsearch.rest.RestController; |
| 32 | +import org.elasticsearch.rest.RestRequest; |
| 33 | +import org.elasticsearch.rest.RestResponse; |
| 34 | +import org.elasticsearch.rest.action.admin.cluster.RestGetRepositoriesAction; |
| 35 | +import org.elasticsearch.test.ESIntegTestCase; |
| 36 | +import org.elasticsearch.test.rest.FakeRestRequest; |
| 37 | + |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.Locale; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.concurrent.CountDownLatch; |
| 43 | +import java.util.concurrent.atomic.AtomicReference; |
| 44 | + |
| 45 | +import static java.util.Collections.emptyMap; |
| 46 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 47 | +import static org.hamcrest.Matchers.containsString; |
| 48 | +import static org.hamcrest.Matchers.not; |
| 49 | +import static org.mockito.Mockito.mock; |
| 50 | +import static org.mockito.Mockito.when; |
| 51 | +import static org.mockito.Matchers.any; |
| 52 | + |
| 53 | +public class S3BlobStoreRepositoryTests extends ESIntegTestCase { |
| 54 | + |
| 55 | + private final String bucket = "bucket_" + randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); |
| 56 | + private final String client = "client_" + randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); |
| 57 | + private final String accessKey = "accessKey_" + randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); |
| 58 | + private final String secureKey = "secureKey_" + randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); |
| 59 | + |
| 60 | + protected void createTestRepository(final String name) { |
| 61 | + assertAcked(client().admin().cluster().preparePutRepository(name) |
| 62 | + .setType(S3Repository.TYPE) |
| 63 | + .setVerify(false) |
| 64 | + .setSettings(Settings.builder() |
| 65 | + .put(S3Repository.BUCKET_SETTING.getKey(), bucket) |
| 66 | + .put(InternalAwsS3Service.CLIENT_NAME.getKey(), client) |
| 67 | + .put(S3Repository.ACCESS_KEY_SETTING.getKey(), accessKey) |
| 68 | + .put(S3Repository.SECRET_KEY_SETTING.getKey(), secureKey))); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 73 | + return Collections.singletonList(EmptyS3RepositoryPlugin.class); |
| 74 | + } |
| 75 | + |
| 76 | + public static class EmptyS3RepositoryPlugin extends S3RepositoryPlugin { |
| 77 | + |
| 78 | + public EmptyS3RepositoryPlugin(final Settings settings) { |
| 79 | + super(settings); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Map<String, Repository.Factory> getRepositories(final Environment env, final NamedXContentRegistry registry) { |
| 84 | + return Collections.singletonMap(S3Repository.TYPE, (metadata) -> |
| 85 | + new S3Repository(metadata, env.settings(), registry, new InternalAwsS3Service(env.settings(), emptyMap()) { |
| 86 | + @Override |
| 87 | + public synchronized AmazonS3 client(final Settings repositorySettings) { |
| 88 | + final AmazonS3 client = mock(AmazonS3.class); |
| 89 | + when(client.doesBucketExist(any(String.class))).thenReturn(true); |
| 90 | + return client; |
| 91 | + } |
| 92 | + })); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void testInsecureRepositoryCredentials() throws Exception { |
| 97 | + final String repositoryName = "testInsecureRepositoryCredentials"; |
| 98 | + createTestRepository(repositoryName); |
| 99 | + final NodeClient nodeClient = internalCluster().getInstance(NodeClient.class); |
| 100 | + final RestGetRepositoriesAction getRepoAction = new RestGetRepositoriesAction(Settings.EMPTY, mock(RestController.class), |
| 101 | + internalCluster().getInstance(SettingsFilter.class)); |
| 102 | + final RestRequest getRepoRequest = new FakeRestRequest(); |
| 103 | + getRepoRequest.params().put("repository", repositoryName); |
| 104 | + final CountDownLatch getRepoLatch = new CountDownLatch(1); |
| 105 | + final AtomicReference<AssertionError> getRepoError = new AtomicReference<>(); |
| 106 | + getRepoAction.handleRequest(getRepoRequest, new AbstractRestChannel(getRepoRequest, true) { |
| 107 | + @Override |
| 108 | + public void sendResponse(RestResponse response) { |
| 109 | + try { |
| 110 | + final String responseContent = response.content().utf8ToString(); |
| 111 | + assertThat(responseContent, containsString(bucket)); |
| 112 | + assertThat(responseContent, containsString(client)); |
| 113 | + assertThat(responseContent, not(containsString(accessKey))); |
| 114 | + assertThat(responseContent, not(containsString(secureKey))); |
| 115 | + } catch (final AssertionError ex) { |
| 116 | + getRepoError.set(ex); |
| 117 | + } |
| 118 | + getRepoLatch.countDown(); |
| 119 | + } |
| 120 | + }, nodeClient); |
| 121 | + getRepoLatch.await(); |
| 122 | + if (getRepoError.get() != null) { |
| 123 | + throw getRepoError.get(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments