diff --git a/plugin/src/test/java/org/elasticsearch/license/LicenseServiceWithSecurityTests.java b/plugin/src/test/java/org/elasticsearch/license/LicenseServiceWithSecurityTests.java new file mode 100644 index 00000000000..4b7895cda52 --- /dev/null +++ b/plugin/src/test/java/org/elasticsearch/license/LicenseServiceWithSecurityTests.java @@ -0,0 +1,53 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.license; + +import org.elasticsearch.analysis.common.CommonAnalysisPlugin; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.test.SecurityIntegTestCase; +import org.elasticsearch.transport.Netty4Plugin; +import org.elasticsearch.xpack.XPackPlugin; + +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.CoreMatchers.equalTo; + +/** + * Basic integration test that checks if license can be upgraded to a production license if TLS is enabled and vice versa. + */ +public class LicenseServiceWithSecurityTests extends SecurityIntegTestCase { + + @Override + protected Collection> nodePlugins() { + return Arrays.asList(XPackPlugin.class, CommonAnalysisPlugin.class, Netty4Plugin.class); + } + + @Override + protected Collection> transportClientPlugins() { + return nodePlugins(); + } + + public void testLicenseUpgradeFailsWithoutTLS() throws Exception { + assumeFalse("transport ssl is enabled", isTransportSSLEnabled()); + LicensingClient licensingClient = new LicensingClient(client()); + License license = licensingClient.prepareGetLicense().get().license(); + License prodLicense = TestUtils.generateSignedLicense("platinum", TimeValue.timeValueHours(24)); + IllegalStateException ise = expectThrows(IllegalStateException.class, () -> licensingClient.preparePutLicense(prodLicense).get()); + assertEquals("Can not upgrade to a production license unless TLS is configured or security is disabled", ise.getMessage()); + assertThat(licensingClient.prepareGetLicense().get().license(), equalTo(license)); + } + + public void testLicenseUpgradeSucceedsWithTLS() throws Exception { + assumeTrue("transport ssl is disabled", isTransportSSLEnabled()); + LicensingClient licensingClient = new LicensingClient(client()); + License prodLicense = TestUtils.generateSignedLicense("platinum", TimeValue.timeValueHours(24)); + PutLicenseResponse putLicenseResponse = licensingClient.preparePutLicense(prodLicense).get(); + assertEquals(putLicenseResponse.status(), LicensesStatus.VALID); + assertThat(licensingClient.prepareGetLicense().get().license(), equalTo(prodLicense)); + } +} diff --git a/plugin/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java b/plugin/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java index 7f9539d78e5..09632c649b1 100644 --- a/plugin/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java @@ -518,4 +518,8 @@ public abstract class SecurityIntegTestCase extends ESIntegTestCase { } return null; } + + protected boolean isTransportSSLEnabled() { + return customSecuritySettingsSource.isSslEnabled(); + } } diff --git a/plugin/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java b/plugin/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java index 6aeec9c236a..02bf772fa4c 100644 --- a/plugin/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java +++ b/plugin/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java @@ -339,4 +339,8 @@ public class SecuritySettingsSource extends ClusterDiscoveryConfiguration.Unicas throw new ElasticsearchException("exception while reading the store", e); } } + + public boolean isSslEnabled() { + return sslEnabled; + } }