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@91abdf73c8
This commit is contained in:
parent
a8bb433aa1
commit
5e8dd26d93
|
@ -10,19 +10,19 @@ import org.elasticsearch.rest.RestStatus;
|
||||||
|
|
||||||
public class LicenseUtils {
|
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
|
* Exception to be thrown when a feature action requires a valid license, but license
|
||||||
* has expired
|
* has expired
|
||||||
*
|
*
|
||||||
* <code>feature</code> accessible through {@link #EXPIRED_FEATURE_HEADER} in the
|
* <code>feature</code> accessible through {@link #EXPIRED_FEATURE_METADATA} in the
|
||||||
* exception's rest header
|
* exception's rest header
|
||||||
*/
|
*/
|
||||||
public static ElasticsearchSecurityException newComplianceException(String feature) {
|
public static ElasticsearchSecurityException newComplianceException(String feature) {
|
||||||
ElasticsearchSecurityException e = new ElasticsearchSecurityException("current license is non-compliant for [{}]",
|
ElasticsearchSecurityException e = new ElasticsearchSecurityException("current license is non-compliant for [{}]",
|
||||||
RestStatus.FORBIDDEN, feature);
|
RestStatus.FORBIDDEN, feature);
|
||||||
e.addHeader(EXPIRED_FEATURE_HEADER, feature);
|
e.addMetadata(EXPIRED_FEATURE_METADATA, feature);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,6 @@ public class LicenseUtils {
|
||||||
* requires a valid license, but the license has expired.
|
* requires a valid license, but the license has expired.
|
||||||
*/
|
*/
|
||||||
public static boolean isLicenseExpiredException(ElasticsearchSecurityException exception) {
|
public static boolean isLicenseExpiredException(ElasticsearchSecurityException exception) {
|
||||||
return (exception != null) && (exception.getHeader(EXPIRED_FEATURE_HEADER) != null);
|
return (exception != null) && (exception.getMetadata(EXPIRED_FEATURE_METADATA) != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,9 @@ public class LicenseUtilsTests extends ESTestCase {
|
||||||
for (String feature : Arrays.asList("feature", randomAsciiOfLength(5), null, "")) {
|
for (String feature : Arrays.asList("feature", randomAsciiOfLength(5), null, "")) {
|
||||||
ElasticsearchSecurityException exception = LicenseUtils.newComplianceException(feature);
|
ElasticsearchSecurityException exception = LicenseUtils.newComplianceException(feature);
|
||||||
assertNotNull(exception);
|
assertNotNull(exception);
|
||||||
assertThat(exception.getHeaderKeys(), contains(LicenseUtils.EXPIRED_FEATURE_HEADER));
|
assertThat(exception.getMetadataKeys(), contains(LicenseUtils.EXPIRED_FEATURE_METADATA));
|
||||||
assertThat(exception.getHeader(LicenseUtils.EXPIRED_FEATURE_HEADER), hasSize(1));
|
assertThat(exception.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA), hasSize(1));
|
||||||
assertThat(exception.getHeader(LicenseUtils.EXPIRED_FEATURE_HEADER).iterator().next(), equalTo(feature));
|
assertThat(exception.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA).iterator().next(), equalTo(feature));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,6 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.license;
|
package org.elasticsearch.license;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
import org.elasticsearch.ElasticsearchSecurityException;
|
import org.elasticsearch.ElasticsearchSecurityException;
|
||||||
import org.elasticsearch.action.DocWriteResponse;
|
import org.elasticsearch.action.DocWriteResponse;
|
||||||
|
@ -32,8 +28,8 @@ import org.elasticsearch.test.SecurityIntegTestCase;
|
||||||
import org.elasticsearch.test.SecuritySettingsSource;
|
import org.elasticsearch.test.SecuritySettingsSource;
|
||||||
import org.elasticsearch.transport.Netty4Plugin;
|
import org.elasticsearch.transport.Netty4Plugin;
|
||||||
import org.elasticsearch.transport.Transport;
|
import org.elasticsearch.transport.Transport;
|
||||||
import org.elasticsearch.xpack.XPackPlugin;
|
|
||||||
import org.elasticsearch.xpack.TestXPackTransportClient;
|
import org.elasticsearch.xpack.TestXPackTransportClient;
|
||||||
|
import org.elasticsearch.xpack.XPackPlugin;
|
||||||
import org.elasticsearch.xpack.security.Security;
|
import org.elasticsearch.xpack.security.Security;
|
||||||
import org.elasticsearch.xpack.security.action.user.GetUsersResponse;
|
import org.elasticsearch.xpack.security.action.user.GetUsersResponse;
|
||||||
import org.elasticsearch.xpack.security.authc.support.SecuredString;
|
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.elasticsearch.xpack.security.client.SecurityClient;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
@ -239,7 +238,7 @@ public class LicensingTests extends SecurityIntegTestCase {
|
||||||
|
|
||||||
private static void assertElasticsearchSecurityException(ThrowingRunnable runnable) {
|
private static void assertElasticsearchSecurityException(ThrowingRunnable runnable) {
|
||||||
ElasticsearchSecurityException ee = expectThrows(ElasticsearchSecurityException.class, 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));
|
assertThat(ee.status(), is(RestStatus.FORBIDDEN));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ package org.elasticsearch.test;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.elasticsearch.ElasticsearchSecurityException;
|
import org.elasticsearch.ElasticsearchSecurityException;
|
||||||
|
import org.elasticsearch.license.LicenseUtils;
|
||||||
import org.elasticsearch.rest.RestStatus;
|
import org.elasticsearch.rest.RestStatus;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
|
|
||||||
|
@ -27,7 +28,7 @@ public class SecurityTestsUtils {
|
||||||
public static void assertAuthenticationException(ElasticsearchSecurityException e) {
|
public static void assertAuthenticationException(ElasticsearchSecurityException e) {
|
||||||
assertThat(e.status(), is(RestStatus.UNAUTHORIZED));
|
assertThat(e.status(), is(RestStatus.UNAUTHORIZED));
|
||||||
// making sure it's not a license expired exception
|
// 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);
|
assertContainsWWWAuthenticateHeader(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,11 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.rest.RestStatus;
|
import org.elasticsearch.rest.RestStatus;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
import org.elasticsearch.test.VersionUtils;
|
||||||
import org.elasticsearch.xpack.monitoring.exporter.ExportException;
|
import org.elasticsearch.xpack.monitoring.exporter.ExportException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.elasticsearch.test.VersionUtils.randomVersion;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
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));
|
response = new MonitoringBulkResponse(Math.abs(randomLong()), new MonitoringBulkResponse.Error(exception));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Version version = VersionUtils.randomVersion(random());
|
||||||
BytesStreamOutput output = new BytesStreamOutput();
|
BytesStreamOutput output = new BytesStreamOutput();
|
||||||
Version outputVersion = randomVersion(random());
|
output.setVersion(version);
|
||||||
output.setVersion(outputVersion);
|
|
||||||
response.writeTo(output);
|
response.writeTo(output);
|
||||||
|
|
||||||
StreamInput streamInput = output.bytes().streamInput();
|
StreamInput streamInput = output.bytes().streamInput();
|
||||||
streamInput.setVersion(randomVersion(random()));
|
streamInput.setVersion(version);
|
||||||
MonitoringBulkResponse response2 = new MonitoringBulkResponse();
|
MonitoringBulkResponse response2 = new MonitoringBulkResponse();
|
||||||
response2.readFrom(streamInput);
|
response2.readFrom(streamInput);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue