2424import com .amazonaws .services .s3 .AmazonS3 ;
2525import org .apache .logging .log4j .LogManager ;
2626import org .apache .logging .log4j .Logger ;
27- import org .elasticsearch .client .node .NodeClient ;
2827import org .elasticsearch .cluster .metadata .RepositoryMetaData ;
29- import org .elasticsearch .common .SuppressForbidden ;
3028import org .elasticsearch .common .settings .MockSecureSettings ;
3129import org .elasticsearch .common .settings .Settings ;
32- import org .elasticsearch .common .settings .SettingsFilter ;
3330import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
3431import org .elasticsearch .plugins .Plugin ;
3532import org .elasticsearch .plugins .PluginsService ;
3633import org .elasticsearch .repositories .RepositoriesService ;
37- import org .elasticsearch .rest .AbstractRestChannel ;
38- import org .elasticsearch .rest .RestController ;
39- import org .elasticsearch .rest .RestRequest ;
40- import org .elasticsearch .rest .RestResponse ;
41- import org .elasticsearch .rest .action .admin .cluster .RestGetRepositoriesAction ;
4234import org .elasticsearch .test .ESSingleNodeTestCase ;
43- import org .elasticsearch .test .rest .FakeRestRequest ;
4435import org .elasticsearch .threadpool .ThreadPool ;
4536
46- import java .security .AccessController ;
47- import java .security .PrivilegedAction ;
4837import java .util .Collection ;
4938import java .util .List ;
50- import java .util .concurrent .CountDownLatch ;
51- import java .util .concurrent .atomic .AtomicReference ;
5239
5340import static org .elasticsearch .repositories .s3 .S3ClientSettings .ACCESS_KEY_SETTING ;
5441import static org .elasticsearch .repositories .s3 .S3ClientSettings .SECRET_KEY_SETTING ;
5542import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
56- import static org .hamcrest .Matchers .containsString ;
5743import static org .hamcrest .Matchers .instanceOf ;
5844import static org .hamcrest .Matchers .is ;
59- import static org .hamcrest .Matchers .not ;
6045import static org .hamcrest .Matchers .notNullValue ;
61- import static org .mockito .Mockito .mock ;
6246
63- @ SuppressForbidden (reason = "test requires to set a System property to allow insecure settings when running in IDE" )
6447public class RepositoryCredentialsTests extends ESSingleNodeTestCase {
6548
66- static {
67- AccessController .doPrivileged ((PrivilegedAction <Void >) () -> {
68- // required for client settings overwriting when running in IDE
69- System .setProperty ("es.allow_insecure_settings" , "true" );
70- return null ;
71- });
72- }
73-
7449 @ Override
7550 protected Collection <Class <? extends Plugin >> getPlugins () {
7651 return List .of (ProxyS3RepositoryPlugin .class );
@@ -95,52 +70,11 @@ protected Settings nodeSettings() {
9570 .build ();
9671 }
9772
98- public void testRepositoryCredentialsOverrideSecureCredentials () {
99- final String repositoryName = "repo-creds-override" ;
100- final Settings .Builder repositorySettings = Settings .builder ()
101- // repository settings for credentials override node secure settings
102- .put (S3Repository .ACCESS_KEY_SETTING .getKey (), "insecure_aws_key" )
103- .put (S3Repository .SECRET_KEY_SETTING .getKey (), "insecure_aws_secret" );
104-
105- final String clientName = randomFrom ("default" , "other" , null );
106- if (clientName != null ) {
107- repositorySettings .put (S3Repository .CLIENT_NAME .getKey (), clientName );
108- }
109- createRepository (repositoryName , repositorySettings .build ());
110-
111- final RepositoriesService repositories = getInstanceFromNode (RepositoriesService .class );
112- assertThat (repositories .repository (repositoryName ), notNullValue ());
113- assertThat (repositories .repository (repositoryName ), instanceOf (S3Repository .class ));
114-
115- final S3Repository repository = (S3Repository ) repositories .repository (repositoryName );
116- final AmazonS3 client = repository .createBlobStore ().clientReference ().client ();
117- assertThat (client , instanceOf (ProxyS3RepositoryPlugin .ClientAndCredentials .class ));
118-
119- final AWSCredentials credentials = ((ProxyS3RepositoryPlugin .ClientAndCredentials ) client ).credentials .getCredentials ();
120- assertThat (credentials .getAWSAccessKeyId (), is ("insecure_aws_key" ));
121- assertThat (credentials .getAWSSecretKey (), is ("insecure_aws_secret" ));
122-
123- assertWarnings (
124- "[secret_key] setting was deprecated in Elasticsearch and will be removed in a future release!"
125- + " See the breaking changes documentation for the next major version." ,
126- "Using s3 access/secret key from repository settings. Instead store these in named clients and"
127- + " the elasticsearch keystore for secure settings." ,
128- "[access_key] setting was deprecated in Elasticsearch and will be removed in a future release!"
129- + " See the breaking changes documentation for the next major version." );
130- }
131-
13273 public void testReinitSecureCredentials () {
13374 final String clientName = randomFrom ("default" , "other" );
13475
13576 final Settings .Builder repositorySettings = Settings .builder ();
136- final boolean hasInsecureSettings = randomBoolean ();
137- if (hasInsecureSettings ) {
138- // repository settings for credentials override node secure settings
139- repositorySettings .put (S3Repository .ACCESS_KEY_SETTING .getKey (), "insecure_aws_key" );
140- repositorySettings .put (S3Repository .SECRET_KEY_SETTING .getKey (), "insecure_aws_secret" );
141- } else {
142- repositorySettings .put (S3Repository .CLIENT_NAME .getKey (), clientName );
143- }
77+ repositorySettings .put (S3Repository .CLIENT_NAME .getKey (), clientName );
14478
14579 final String repositoryName = "repo-reinit-creds" ;
14680 createRepository (repositoryName , repositorySettings .build ());
@@ -155,10 +89,7 @@ public void testReinitSecureCredentials() {
15589 assertThat (client , instanceOf (ProxyS3RepositoryPlugin .ClientAndCredentials .class ));
15690
15791 final AWSCredentials credentials = ((ProxyS3RepositoryPlugin .ClientAndCredentials ) client ).credentials .getCredentials ();
158- if (hasInsecureSettings ) {
159- assertThat (credentials .getAWSAccessKeyId (), is ("insecure_aws_key" ));
160- assertThat (credentials .getAWSSecretKey (), is ("insecure_aws_secret" ));
161- } else if ("other" .equals (clientName )) {
92+ if ("other" .equals (clientName )) {
16293 assertThat (credentials .getAWSAccessKeyId (), is ("secure_other_key" ));
16394 assertThat (credentials .getAWSSecretKey (), is ("secure_other_secret" ));
16495 } else {
@@ -177,10 +108,7 @@ public void testReinitSecureCredentials() {
177108 plugin .reload (newSettings );
178109
179110 // check the not-yet-closed client reference still has the same credentials
180- if (hasInsecureSettings ) {
181- assertThat (credentials .getAWSAccessKeyId (), is ("insecure_aws_key" ));
182- assertThat (credentials .getAWSSecretKey (), is ("insecure_aws_secret" ));
183- } else if ("other" .equals (clientName )) {
111+ if ("other" .equals (clientName )) {
184112 assertThat (credentials .getAWSAccessKeyId (), is ("secure_other_key" ));
185113 assertThat (credentials .getAWSSecretKey (), is ("secure_other_secret" ));
186114 } else {
@@ -195,64 +123,11 @@ public void testReinitSecureCredentials() {
195123 assertThat (client , instanceOf (ProxyS3RepositoryPlugin .ClientAndCredentials .class ));
196124
197125 final AWSCredentials newCredentials = ((ProxyS3RepositoryPlugin .ClientAndCredentials ) client ).credentials .getCredentials ();
198- if (hasInsecureSettings ) {
199- assertThat (newCredentials .getAWSAccessKeyId (), is ("insecure_aws_key" ));
200- assertThat (newCredentials .getAWSSecretKey (), is ("insecure_aws_secret" ));
201- } else {
202- assertThat (newCredentials .getAWSAccessKeyId (), is ("new_secret_aws_key" ));
203- assertThat (newCredentials .getAWSSecretKey (), is ("new_secret_aws_secret" ));
204- }
205- }
206-
207- if (hasInsecureSettings ) {
208- assertWarnings (
209- "[secret_key] setting was deprecated in Elasticsearch and will be removed in a future release!"
210- + " See the breaking changes documentation for the next major version." ,
211- "Using s3 access/secret key from repository settings. Instead store these in named clients and"
212- + " the elasticsearch keystore for secure settings." ,
213- "[access_key] setting was deprecated in Elasticsearch and will be removed in a future release!"
214- + " See the breaking changes documentation for the next major version." );
126+ assertThat (newCredentials .getAWSAccessKeyId (), is ("new_secret_aws_key" ));
127+ assertThat (newCredentials .getAWSSecretKey (), is ("new_secret_aws_secret" ));
215128 }
216129 }
217130
218- public void testInsecureRepositoryCredentials () throws Exception {
219- final String repositoryName = "repo-insecure-creds" ;
220- createRepository (repositoryName , Settings .builder ()
221- .put (S3Repository .ACCESS_KEY_SETTING .getKey (), "insecure_aws_key" )
222- .put (S3Repository .SECRET_KEY_SETTING .getKey (), "insecure_aws_secret" )
223- .build ());
224-
225- final RestRequest fakeRestRequest = new FakeRestRequest ();
226- fakeRestRequest .params ().put ("repository" , repositoryName );
227- final RestGetRepositoriesAction action =
228- new RestGetRepositoriesAction (mock (RestController .class ), getInstanceFromNode (SettingsFilter .class ));
229-
230- final CountDownLatch latch = new CountDownLatch (1 );
231- final AtomicReference <AssertionError > error = new AtomicReference <>();
232- action .handleRequest (fakeRestRequest , new AbstractRestChannel (fakeRestRequest , true ) {
233- @ Override
234- public void sendResponse (RestResponse response ) {
235- try {
236- String responseAsString = response .content ().utf8ToString ();
237- assertThat (responseAsString , containsString (repositoryName ));
238- assertThat (responseAsString , not (containsString ("insecure_" )));
239- } catch (final AssertionError ex ) {
240- error .set (ex );
241- }
242- latch .countDown ();
243- }
244- }, getInstanceFromNode (NodeClient .class ));
245-
246- latch .await ();
247- if (error .get () != null ) {
248- throw error .get ();
249- }
250-
251- assertWarnings (
252- "Using s3 access/secret key from repository settings. Instead store these in named clients and"
253- + " the elasticsearch keystore for secure settings." );
254- }
255-
256131 private void createRepository (final String name , final Settings repositorySettings ) {
257132 assertAcked (client ().admin ().cluster ().preparePutRepository (name )
258133 .setType (S3Repository .TYPE )
0 commit comments