[license] simplify Licensee interface

Currently, Licensee#onChange is called with a license and its status, It would be
cleaner if it accepted the license's operationMode and status as parameters instead.
This hides the license responsible for the notification from the consumer plugins and only
provides the license information used by the commercial plugins to turn features on via
operationMode and status to act on whether the current license is enabled, in
grace_period or disabled.

Original commit: elastic/x-pack-elasticsearch@b0ea7ec32b
This commit is contained in:
Areek Zillur 2015-10-15 12:59:18 -04:00
parent fbca52e71c
commit ae552efc3c
9 changed files with 61 additions and 161 deletions

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.core.LicenseUtils;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.marvel.agent.collector.AbstractCollector;
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
@ -39,15 +40,15 @@ public class ClusterInfoCollector extends AbstractCollector<ClusterInfoMarvelDoc
public static final String TYPE = "cluster_info";
private final ClusterName clusterName;
private final MarvelLicensee marvelLicensee;
private final LicensesManagerService licensesManagerService;
private final Client client;
@Inject
public ClusterInfoCollector(Settings settings, ClusterService clusterService, MarvelSettings marvelSettings, MarvelLicensee marvelLicensee,
ClusterName clusterName, SecuredClient client) {
LicensesManagerService licensesManagerService, ClusterName clusterName, SecuredClient client) {
super(settings, NAME, clusterService, marvelSettings, marvelLicensee);
this.clusterName = clusterName;
this.marvelLicensee = marvelLicensee;
this.licensesManagerService = licensesManagerService;
this.client = client;
}
@ -61,7 +62,7 @@ public class ClusterInfoCollector extends AbstractCollector<ClusterInfoMarvelDoc
protected Collection<MarvelDoc> doCollect() throws Exception {
List<MarvelDoc> results = new ArrayList<>(1);
License license = marvelLicensee.getLicense();
License license = licensesManagerService.getLicense();
// Retrieves additional cluster stats
ClusterStatsResponse clusterStats = null;

View File

@ -104,7 +104,7 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
final License license = createTestingLicense(issueDate, expiryDate);
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license, LicenseState.ENABLED);
service.onChange(license.operationMode(), LicenseState.ENABLED);
}
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) {
service.update(license);
@ -117,7 +117,7 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
final License license = createTestingLicense(issueDate, expiryDate);
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license, LicenseState.GRACE_PERIOD);
service.onChange(license.operationMode(), LicenseState.GRACE_PERIOD);
}
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) {
service.update(license);
@ -130,7 +130,7 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
final License license = createTestingLicense(issueDate, expiryDate);
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license, LicenseState.DISABLED);
service.onChange(license.operationMode(), LicenseState.DISABLED);
}
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) {
service.update(license);
@ -143,7 +143,7 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
final License license = createTestingLicense(issueDate, expiryDate);
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license, LicenseState.DISABLED);
service.onChange(license.operationMode(), LicenseState.DISABLED);
}
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) {
service.update(license);
@ -224,16 +224,16 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
licensees.add(licensee);
}
public void onChange(License license, LicenseState state) {
public void onChange(License.OperationMode operationMode, LicenseState state) {
for (Licensee licensee : licensees) {
licensee.onChange(license, state);
licensee.onChange(new Licensee.Status(operationMode, state));
}
}
}
public static class LicensesManagerServiceForCollectors implements LicensesManagerService {
private final Map<String, License> licenses = Collections.synchronizedMap(new HashMap<String, License>());
private volatile License license;
@Override
public void registerLicense(PutLicenseRequest request, ActionListener<LicensesService.LicensesUpdateResponse> listener) {
@ -250,14 +250,11 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
@Override
public License getLicense() {
// TODO: we only take the first of the licenses that are updated
// FIXME
Iterator<License> iterator = licenses.values().iterator();
return iterator.hasNext() ? iterator.next() : null;
return license;
}
public void update(License license) {
licenses.put(license.uid(), license);
public synchronized void update(License license) {
this.license = license;
}
}
}

View File

@ -8,6 +8,7 @@ package org.elasticsearch.marvel.agent.collector.cluster;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.marvel.agent.collector.AbstractCollectorTestCase;
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
@ -101,6 +102,7 @@ public class ClusterInfoCollectorTests extends AbstractCollectorTestCase {
internalCluster().getInstance(ClusterService.class, nodeId),
internalCluster().getInstance(MarvelSettings.class, nodeId),
internalCluster().getInstance(MarvelLicensee.class, nodeId),
internalCluster().getInstance(LicensesManagerService.class, nodeId),
internalCluster().getInstance(ClusterName.class, nodeId),
securedClient(nodeId));
}

View File

@ -110,17 +110,6 @@ public class LicenseIntegrationTests extends MarvelIntegTestCase {
public static class MockLicenseService extends AbstractComponent implements LicenseeRegistry {
static final License DUMMY_LICENSE = License.builder()
.expiryDate(System.currentTimeMillis())
.issueDate(System.currentTimeMillis())
.issuedTo("LicensingTests")
.issuer("test")
.maxNodes(Integer.MAX_VALUE)
.signature("_signature")
.type("basic")
.uid(String.valueOf(RandomizedTest.systemPropertyAsInt(SysGlobals.CHILDVM_SYSPROP_JVM_ID, 0)) + System.identityHashCode(LicenseIntegrationTests.class))
.build();
private final List<Licensee> licensees = new ArrayList<>();
@Inject
@ -137,13 +126,13 @@ public class LicenseIntegrationTests extends MarvelIntegTestCase {
public void enable() {
for (Licensee licensee : licensees) {
licensee.onChange(DUMMY_LICENSE, randomBoolean() ? LicenseState.GRACE_PERIOD : LicenseState.ENABLED);
licensee.onChange(new Licensee.Status(License.OperationMode.BASIC, randomBoolean() ? LicenseState.ENABLED : LicenseState.GRACE_PERIOD));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(DUMMY_LICENSE, LicenseState.DISABLED);
licensee.onChange(new Licensee.Status(License.OperationMode.BASIC, LicenseState.DISABLED));
}
}
}

View File

@ -27,7 +27,7 @@ public class ShieldLicensee extends AbstractLicenseeComponent<ShieldLicensee> im
super(settings, ShieldPlugin.NAME, clientService);
add(new Listener() {
@Override
public void onChange(License license, Status status) {
public void onChange(Status status) {
shieldLicenseState.updateStatus(status);
}
});

View File

@ -169,7 +169,7 @@ public class LicensingTests extends ShieldIntegTestCase {
assertThat(ee.status(), is(RestStatus.UNAUTHORIZED));
}
enableLicensing(LicensingTests.generateLicense(randomFrom(OperationMode.values())));
enableLicensing(randomFrom(OperationMode.values()));
IndicesStatsResponse indicesStatsResponse = client.admin().indices().prepareStats().get();
assertNoFailures(indicesStatsResponse);
@ -194,7 +194,7 @@ public class LicensingTests extends ShieldIntegTestCase {
// generate a new license with a mode that enables auth
OperationMode mode = randomFrom(OperationMode.GOLD, OperationMode.TRIAL, OperationMode.PLATINUM);
enableLicensing(generateLicense(mode));
enableLicensing(mode);
assertThat(httpClient().path("/").execute().getStatusCode(), is(401));
}
@ -214,7 +214,7 @@ public class LicensingTests extends ShieldIntegTestCase {
// enable a license that enables security
OperationMode mode = randomFrom(OperationMode.GOLD, OperationMode.PLATINUM, OperationMode.TRIAL);
enableLicensing(generateLicense(mode));
enableLicensing(mode);
try (TransportClient client = TransportClient.builder().settings(builder).addPlugin(ShieldPlugin.class).build()) {
client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress());
@ -226,38 +226,25 @@ public class LicensingTests extends ShieldIntegTestCase {
}
public static void disableLicensing() {
disableLicensing(InternalLicenseeRegistry.DUMMY_LICENSE);
disableLicensing(OperationMode.BASIC);
}
public static void disableLicensing(License license) {
public static void disableLicensing(OperationMode operationMode) {
for (InternalLicenseeRegistry service : internalCluster().getInstances(InternalLicenseeRegistry.class)) {
service.disable(license);
service.disable(operationMode);
}
}
public static void enableLicensing() {
enableLicensing(InternalLicenseeRegistry.DUMMY_LICENSE);
enableLicensing(OperationMode.BASIC);
}
public static void enableLicensing(License license) {
public static void enableLicensing(OperationMode operationMode) {
for (InternalLicenseeRegistry service : internalCluster().getInstances(InternalLicenseeRegistry.class)) {
service.enable(license);
service.enable(operationMode);
}
}
public static License generateLicense(OperationMode operationMode) {
return License.builder()
.expiryDate(System.currentTimeMillis())
.issueDate(System.currentTimeMillis())
.issuedTo("LicensingTests")
.issuer("test")
.maxNodes(Integer.MAX_VALUE)
.signature("_signature")
.type(operationMode.toString().toLowerCase(Locale.ROOT))
.uid(String.valueOf(randomLong()) + System.identityHashCode(LicensingTests.class))
.build();
}
public static class InternalLicensePlugin extends Plugin {
public static final String NAME = "internal-licensing";
@ -290,38 +277,27 @@ public class LicensingTests extends ShieldIntegTestCase {
private final List<Licensee> licensees = new ArrayList<>();
static final License DUMMY_LICENSE = License.builder()
.expiryDate(System.currentTimeMillis())
.issueDate(System.currentTimeMillis())
.issuedTo("LicensingTests")
.issuer("test")
.maxNodes(Integer.MAX_VALUE)
.signature("_signature")
.type("basic")
.uid(String.valueOf(randomLong()) + System.identityHashCode(LicensingTests.class))
.build();
@Inject
public InternalLicenseeRegistry(Settings settings) {
super(settings);
enable(DUMMY_LICENSE);
enable(OperationMode.BASIC);
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable(DUMMY_LICENSE);
enable(OperationMode.BASIC);
}
void enable(License license) {
void enable(OperationMode operationMode) {
for (Licensee licensee : licensees) {
licensee.onChange(license, LicenseState.ENABLED);
licensee.onChange(new Licensee.Status(operationMode, LicenseState.ENABLED));
}
}
void disable(License license) {
void disable(OperationMode operationMode) {
for (Licensee licensee : licensees) {
licensee.onChange(license, LicenseState.DISABLED);
licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
}
}
}

View File

@ -108,7 +108,7 @@ public class ShieldPluginEnabledDisabledTests extends ShieldIntegTestCase {
OperationMode mode;
if (enabled) {
mode = randomFrom(OperationMode.values());
LicensingTests.enableLicensing(LicensingTests.generateLicense(mode));
LicensingTests.enableLicensing(mode);
} else {
// this is the default right now
mode = OperationMode.BASIC;

View File

@ -60,8 +60,7 @@ public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee>
}
public boolean isPutWatchAllowed() {
boolean isLicenseActive = getLicense().status() == License.Status.ACTIVE;
return isLicenseActive && isWatcherTransportActionAllowed();
return isWatcherTransportActionAllowed();
}
public boolean isWatcherTransportActionAllowed() {

View File

@ -5,8 +5,6 @@
*/
package org.elasticsearch.watcher.license;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.SysGlobals;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License;
@ -27,11 +25,7 @@ public class LicenseTests extends ESTestCase {
@Test
public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception {
License license = licenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(license);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
@ -40,101 +34,67 @@ public class LicenseTests extends ESTestCase {
@Test
public void testBasicLicenseIsDisabled() throws Exception {
License license = licenseBuilder()
.type("basic")
.build();
licenseeRegistry.setLicense(license);
licenseeRegistry.setOperationMode(License.OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
assertLicenseBasicOrNoneBehaviour(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
}
@Test
public void testNoLicenseDoesNotWork() {
License license = licenseBuilder()
.type("basic")
.build();
licenseeRegistry.setLicense(license);
licenseeRegistry.setOperationMode(License.OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable();
assertLicenseBasicOrNoneBehaviour(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
}
@Test
public void testExpiredPlatinumGoldTrialLicenseIsRestricted() throws Exception {
License license = expiredLicenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(license);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable();
assertLicenseExpiredBehaviour(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
}
@Test
public void testUpgradingFromBasicLicenseWorks() {
License basicLicense = licenseBuilder()
.type("basic")
.build();
licenseeRegistry.setLicense(basicLicense);
licenseeRegistry.setOperationMode(License.OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
assertLicenseBasicOrNoneBehaviour(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
License fancyLicense = licenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(fancyLicense);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
}
@Test
public void testDowngradingToBasicLicenseWorks() {
License basicLicense = licenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(basicLicense);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
License fancyLicense = licenseBuilder()
.type("basic")
.build();
licenseeRegistry.setLicense(fancyLicense);
assertLicenseBasicOrNoneBehaviour(watcherLicensee);
licenseeRegistry.setOperationMode(License.OperationMode.BASIC);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
}
@Test
public void testUpgradingExpiredLicenseWorks() {
License expiredLicense = expiredLicenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(expiredLicense);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable();
assertLicenseExpiredBehaviour(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
License fancyLicense = licenseBuilder()
.type(randomFrom("platinum", "gold", "trial"))
.build();
licenseeRegistry.setLicense(fancyLicense);
licenseeRegistry.setOperationMode(randomFrom(License.OperationMode.PLATINUM, License.OperationMode.GOLD, License.OperationMode.TRIAL));
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
}
@ -145,41 +105,17 @@ public class LicenseTests extends ESTestCase {
assertThat("Expected actions of a watch to be executed", watcherLicensee.isExecutingActionsAllowed(), is(true));
}
private void assertLicenseBasicOrNoneBehaviour(WatcherLicensee watcherLicensee) {
private void assertLicenseBasicOrNoneOrExpiredBehaviour(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 assertLicenseExpiredBehaviour(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 actions of a watch not to be executed", watcherLicensee.isExecutingActionsAllowed(), is(false));
assertThat("Expected watcher transport actions to be allowed", watcherLicensee.isWatcherTransportActionAllowed(), is(true));
}
private License.Builder expiredLicenseBuilder() {
return licenseBuilder()
.issueDate(System.currentTimeMillis() - 86400)
.expiryDate(System.currentTimeMillis() - 1);
}
private License.Builder licenseBuilder() {
return License.builder()
.issueDate(System.currentTimeMillis())
.expiryDate(System.currentTimeMillis() + (86400 * 1000))
.issuedTo("LicensingTests")
.issuer("test")
.maxNodes(Integer.MAX_VALUE)
.signature("_signature")
.uid(String.valueOf(RandomizedTest.systemPropertyAsInt(SysGlobals.CHILDVM_SYSPROP_JVM_ID, 0)) + System.identityHashCode(LicenseTests.class));
}
public static class SimpleLicenseeRegistry extends AbstractComponent implements LicenseeRegistry {
private final List<Licensee> licensees = new ArrayList<>();
private License license;
private License.OperationMode operationMode;
public SimpleLicenseeRegistry() {
super(Settings.EMPTY);
@ -193,18 +129,18 @@ public class LicenseTests extends ESTestCase {
public void enable() {
for (Licensee licensee : licensees) {
licensee.onChange(license, randomBoolean() ? LicenseState.GRACE_PERIOD : LicenseState.ENABLED);
licensee.onChange(new Licensee.Status(operationMode, randomBoolean() ? LicenseState.ENABLED : LicenseState.GRACE_PERIOD));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(license, LicenseState.DISABLED);
licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
}
}
public void setLicense(License newLicense) {
license = newLicense;
public void setOperationMode(License.OperationMode operationMode) {
this.operationMode = operationMode;
enable();
}
}