From 5e8dd26d930a3f4c8973299fdc629bb634211deb Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Tue, 24 Jan 2017 16:13:03 +0100 Subject: [PATCH] adapt to "move es. headers to metadata set in ElasticsearchException and stop returning them as response headers" (elastic/elasticsearch#4693) Original commit: elastic/x-pack-elasticsearch@91abdf73c8e2080471da6a733dcabee73b36c899 --- .../java/org/elasticsearch/license/LicenseUtils.java | 8 ++++---- .../org/elasticsearch/license/LicenseUtilsTests.java | 6 +++--- .../org/elasticsearch/license/LicensingTests.java | 11 +++++------ .../org/elasticsearch/test/SecurityTestsUtils.java | 3 ++- .../action/MonitoringBulkResponseTests.java | 8 ++++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/license/LicenseUtils.java b/elasticsearch/src/main/java/org/elasticsearch/license/LicenseUtils.java index 42139471646..10672ef2c8c 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/license/LicenseUtils.java +++ b/elasticsearch/src/main/java/org/elasticsearch/license/LicenseUtils.java @@ -10,19 +10,19 @@ import org.elasticsearch.rest.RestStatus; public class LicenseUtils { - public static final String EXPIRED_FEATURE_HEADER = "es.license.expired.feature"; + public static final String EXPIRED_FEATURE_METADATA = "es.license.expired.feature"; /** * Exception to be thrown when a feature action requires a valid license, but license * has expired * - * feature accessible through {@link #EXPIRED_FEATURE_HEADER} in the + * feature accessible through {@link #EXPIRED_FEATURE_METADATA} in the * exception's rest header */ public static ElasticsearchSecurityException newComplianceException(String feature) { ElasticsearchSecurityException e = new ElasticsearchSecurityException("current license is non-compliant for [{}]", RestStatus.FORBIDDEN, feature); - e.addHeader(EXPIRED_FEATURE_HEADER, feature); + e.addMetadata(EXPIRED_FEATURE_METADATA, feature); return e; } @@ -31,6 +31,6 @@ public class LicenseUtils { * requires a valid license, but the license has expired. */ public static boolean isLicenseExpiredException(ElasticsearchSecurityException exception) { - return (exception != null) && (exception.getHeader(EXPIRED_FEATURE_HEADER) != null); + return (exception != null) && (exception.getMetadata(EXPIRED_FEATURE_METADATA) != null); } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/license/LicenseUtilsTests.java b/elasticsearch/src/test/java/org/elasticsearch/license/LicenseUtilsTests.java index ab063c0949c..c8c1d7ba8d7 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/license/LicenseUtilsTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/license/LicenseUtilsTests.java @@ -20,9 +20,9 @@ public class LicenseUtilsTests extends ESTestCase { for (String feature : Arrays.asList("feature", randomAsciiOfLength(5), null, "")) { ElasticsearchSecurityException exception = LicenseUtils.newComplianceException(feature); assertNotNull(exception); - assertThat(exception.getHeaderKeys(), contains(LicenseUtils.EXPIRED_FEATURE_HEADER)); - assertThat(exception.getHeader(LicenseUtils.EXPIRED_FEATURE_HEADER), hasSize(1)); - assertThat(exception.getHeader(LicenseUtils.EXPIRED_FEATURE_HEADER).iterator().next(), equalTo(feature)); + assertThat(exception.getMetadataKeys(), contains(LicenseUtils.EXPIRED_FEATURE_METADATA)); + assertThat(exception.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA), hasSize(1)); + assertThat(exception.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA).iterator().next(), equalTo(feature)); } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/license/LicensingTests.java b/elasticsearch/src/test/java/org/elasticsearch/license/LicensingTests.java index f4a7b9e5537..064eccc7a05 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/license/LicensingTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/license/LicensingTests.java @@ -5,10 +5,6 @@ */ package org.elasticsearch.license; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - import org.apache.http.message.BasicHeader; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.DocWriteResponse; @@ -32,8 +28,8 @@ import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSource; import org.elasticsearch.transport.Netty4Plugin; import org.elasticsearch.transport.Transport; -import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.TestXPackTransportClient; +import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.security.Security; import org.elasticsearch.xpack.security.action.user.GetUsersResponse; import org.elasticsearch.xpack.security.authc.support.SecuredString; @@ -41,6 +37,9 @@ import org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.security.client.SecurityClient; import org.junit.Before; +import java.util.ArrayList; +import java.util.Collection; + import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.hamcrest.Matchers.containsString; @@ -239,7 +238,7 @@ public class LicensingTests extends SecurityIntegTestCase { private static void assertElasticsearchSecurityException(ThrowingRunnable runnable) { ElasticsearchSecurityException ee = expectThrows(ElasticsearchSecurityException.class, runnable); - assertThat(ee.getHeader("es.license.expired.feature"), hasItem(XPackPlugin.SECURITY)); + assertThat(ee.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA), hasItem(XPackPlugin.SECURITY)); assertThat(ee.status(), is(RestStatus.FORBIDDEN)); } diff --git a/elasticsearch/src/test/java/org/elasticsearch/test/SecurityTestsUtils.java b/elasticsearch/src/test/java/org/elasticsearch/test/SecurityTestsUtils.java index 8bae59c74aa..d3917ec9714 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/test/SecurityTestsUtils.java +++ b/elasticsearch/src/test/java/org/elasticsearch/test/SecurityTestsUtils.java @@ -7,6 +7,7 @@ package org.elasticsearch.test; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.ElasticsearchSecurityException; +import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.rest.RestStatus; import org.hamcrest.Matcher; @@ -27,7 +28,7 @@ public class SecurityTestsUtils { public static void assertAuthenticationException(ElasticsearchSecurityException e) { assertThat(e.status(), is(RestStatus.UNAUTHORIZED)); // making sure it's not a license expired exception - assertThat(e.getHeader("es.license.expired.feature"), nullValue()); + assertThat(e.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA), nullValue()); assertContainsWWWAuthenticateHeader(e); } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java index 91bbd431c21..fafaad6f6ed 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xpack.monitoring.exporter.ExportException; import java.io.IOException; -import static org.elasticsearch.test.VersionUtils.randomVersion; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -52,13 +52,13 @@ public class MonitoringBulkResponseTests extends ESTestCase { response = new MonitoringBulkResponse(Math.abs(randomLong()), new MonitoringBulkResponse.Error(exception)); } + final Version version = VersionUtils.randomVersion(random()); BytesStreamOutput output = new BytesStreamOutput(); - Version outputVersion = randomVersion(random()); - output.setVersion(outputVersion); + output.setVersion(version); response.writeTo(output); StreamInput streamInput = output.bytes().streamInput(); - streamInput.setVersion(randomVersion(random())); + streamInput.setVersion(version); MonitoringBulkResponse response2 = new MonitoringBulkResponse(); response2.readFrom(streamInput);