From 2b00967b019ccfc1bb266bb4970e3f07dffe912d Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 15 Apr 2016 09:16:37 +0200 Subject: [PATCH] Watcher: Fix license check for STANDARD license The license check in Watcher was issued in the wrong way, so that new licenses were not affected by the check. This commit explicitely lists the license types that are allowed to execute watcher actions as well as fixing the tests. Relates elastic/elasticsearch#1263 Original commit: elastic/x-pack-elasticsearch@afd55965b097c76f5fb574fd37d3951e695de522 --- .../watcher/license/WatcherLicensee.java | 8 +- .../watcher/license/LicenseTests.java | 88 +++++++++---------- 2 files changed, 43 insertions(+), 53 deletions(-) diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/license/WatcherLicensee.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/license/WatcherLicensee.java index c936d7ddc3d..e3cb1f885d6 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/license/WatcherLicensee.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/license/WatcherLicensee.java @@ -15,10 +15,6 @@ import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.watcher.Watcher; -import static org.elasticsearch.license.core.License.OperationMode.TRIAL; -import static org.elasticsearch.license.core.License.OperationMode.GOLD; -import static org.elasticsearch.license.core.License.OperationMode.PLATINUM; - public class WatcherLicensee extends AbstractLicenseeComponent { public static final String ID = Watcher.NAME; @@ -74,7 +70,6 @@ public class WatcherLicensee extends AbstractLicenseeComponent *
    *
  • {@link OperationMode#PLATINUM}
  • *
  • {@link OperationMode#GOLD}
  • - *
  • {@link OperationMode#STANDARD}
  • *
  • {@link OperationMode#TRIAL}
  • *
* @@ -84,6 +79,7 @@ public class WatcherLicensee extends AbstractLicenseeComponent // status is volatile, so a local variable is used for a consistent view Status localStatus = status; - return localStatus.getLicenseState() != LicenseState.DISABLED && localStatus.getMode() != OperationMode.BASIC; + return localStatus.getLicenseState() != LicenseState.DISABLED && (localStatus.getMode() == OperationMode.TRIAL || + localStatus.getMode() == OperationMode.GOLD || localStatus.getMode() == OperationMode.PLATINUM); } } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/license/LicenseTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/license/LicenseTests.java index c47a8ac4457..def1a44e72b 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/license/LicenseTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/license/LicenseTests.java @@ -6,94 +6,88 @@ package org.elasticsearch.watcher.license; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.license.core.License.OperationMode; +import org.elasticsearch.license.core.License; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; +import static org.elasticsearch.license.core.License.OperationMode.BASIC; +import static org.elasticsearch.license.core.License.OperationMode.GOLD; +import static org.elasticsearch.license.core.License.OperationMode.PLATINUM; +import static org.elasticsearch.license.core.License.OperationMode.STANDARD; +import static org.elasticsearch.license.core.License.OperationMode.TRIAL; import static org.hamcrest.Matchers.is; public class LicenseTests extends AbstractLicenseeTestCase { - private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry(); + private final SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry(); + private WatcherLicensee watcherLicensee; public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception { - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); - - assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); + initLicense(TRIAL, GOLD, PLATINUM); + assertWatcherActionsAllowed(watcherLicensee); } - public void testBasicLicenseIsDisabled() throws Exception { - licenseeRegistry.setOperationMode(OperationMode.BASIC); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); - - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); + public void testBasicStandardLicenseDisablesWatcher() throws Exception { + initLicense(BASIC, STANDARD); + assertWatcherActionsNotAllowed(watcherLicensee); } - public void testNoLicenseDoesNotWork() { - licenseeRegistry.setOperationMode(OperationMode.BASIC); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); + public void testNoLicenseDisablesWatcher() { + initLicense(BASIC, STANDARD); licenseeRegistry.disable(); - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); + assertWatcherActionsNotAllowed(watcherLicensee); } - public void testExpiredPlatinumGoldTrialLicenseIsRestricted() throws Exception { - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); + public void testExpiredPlatinumGoldTrialLicenseDisablesWatcher() throws Exception { + initLicense(TRIAL, GOLD, PLATINUM); licenseeRegistry.disable(); - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); + assertWatcherActionsNotAllowed(watcherLicensee); } - public void testUpgradingFromBasicLicenseWorks() { - licenseeRegistry.setOperationMode(OperationMode.BASIC); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); + public void testUpgradingFromBasicOrStandardLicenseWorks() { + initLicense(BASIC, STANDARD); + assertWatcherActionsNotAllowed(watcherLicensee); - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); - - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); + licenseeRegistry.setOperationMode(randomFrom(TRIAL, GOLD, PLATINUM)); + assertWatcherActionsAllowed(watcherLicensee); } - public void testDowngradingToBasicLicenseWorks() { - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); + public void testDowngradingToBasicOrStandardLicenseWorks() { + initLicense(TRIAL, GOLD, PLATINUM); + assertWatcherActionsAllowed(watcherLicensee); - assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); - - licenseeRegistry.setOperationMode(OperationMode.BASIC); - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); + licenseeRegistry.setOperationMode(randomFrom(BASIC, STANDARD)); + assertWatcherActionsNotAllowed(watcherLicensee); } public void testUpgradingExpiredLicenseWorks() { - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); - licenseeRegistry.register(watcherLicensee); + initLicense(TRIAL, GOLD, PLATINUM); licenseeRegistry.disable(); - assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); + assertWatcherActionsNotAllowed(watcherLicensee); - licenseeRegistry.setOperationMode(randomTrialStandardGoldOrPlatinumMode()); - assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); + licenseeRegistry.setOperationMode(randomFrom(TRIAL, GOLD, PLATINUM)); + assertWatcherActionsAllowed(watcherLicensee); } - private void assertLicenseGoldPlatinumTrialBehaviour(WatcherLicensee watcherLicensee) { + private void assertWatcherActionsAllowed(WatcherLicensee watcherLicensee) { assertThat("Expected putting a watch to be allowed", watcherLicensee.isPutWatchAllowed(), is(true)); assertThat("Expected getting a watch to be allowed", watcherLicensee.isGetWatchAllowed(), is(true)); assertThat("Expected watcher transport actions to be allowed", watcherLicensee.isWatcherTransportActionAllowed(), is(true)); assertThat("Expected actions of a watch to be executed", watcherLicensee.isExecutingActionsAllowed(), is(true)); } - private void assertLicenseBasicOrNoneOrExpiredBehaviour(WatcherLicensee watcherLicensee) { + private void assertWatcherActionsNotAllowed(WatcherLicensee watcherLicensee) { assertThat("Expected putting a watch not to be allowed", watcherLicensee.isPutWatchAllowed(), is(false)); assertThat("Expected getting a watch not to be allowed", watcherLicensee.isGetWatchAllowed(), is(false)); assertThat("Expected watcher transport actions not to be allowed", watcherLicensee.isWatcherTransportActionAllowed(), is(false)); assertThat("Expected actions of a watch not to be executed", watcherLicensee.isExecutingActionsAllowed(), is(false)); } + + private void initLicense(License.OperationMode ... allowedLicenses) { + licenseeRegistry.setOperationMode(randomFrom(allowedLicenses)); + watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); + licenseeRegistry.register(watcherLicensee); + } }