Merge pull request elastic/elasticsearch#2810 from rjernst/deguice2

Internal: Remove guice construction of most license classes

Original commit: elastic/x-pack-elasticsearch@a2b48e62e9
This commit is contained in:
Ryan Ernst 2016-07-14 19:55:32 -07:00 committed by GitHub
commit 952ef78f98
36 changed files with 288 additions and 572 deletions

View File

@ -9,6 +9,7 @@ import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.inject.util.Providers;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.ActionPlugin;
@ -43,15 +44,12 @@ public class Graph extends Plugin implements ActionPlugin {
} }
public Collection<Module> createGuiceModules() { public Collection<Module> createGuiceModules() {
return Collections.singletonList(new GraphModule(enabled, transportClientMode)); return Collections.singletonList(b -> {
} XPackPlugin.bindFeatureSet(b, GraphFeatureSet.class);
if (transportClientMode) {
@Override b.bind(GraphLicensee.class).toProvider(Providers.of(null));
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() { }
if (enabled == false|| transportClientMode) { });
return Collections.emptyList();
}
return Collections.singletonList(GraphLicensee.class);
} }
@Override @Override

View File

@ -6,21 +6,18 @@
package org.elasticsearch.xpack.graph; package org.elasticsearch.xpack.graph;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
public class GraphLicensee extends AbstractLicenseeComponent<GraphLicensee> { public class GraphLicensee extends AbstractLicenseeComponent {
public static final String ID = Graph.NAME; public static final String ID = Graph.NAME;
@Inject public GraphLicensee(Settings settings) {
public GraphLicensee(Settings settings, LicenseeRegistry clientService) { super(settings, ID);
super(settings, ID, clientService);
} }
@Override @Override

View File

@ -1,35 +0,0 @@
/*
* 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.xpack.graph;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.util.Providers;
import org.elasticsearch.xpack.XPackPlugin;
/**
*
*/
public class GraphModule extends AbstractModule {
private final boolean enabled;
private final boolean transportClientNode;
public GraphModule(boolean enabled, boolean transportClientNode) {
this.enabled = enabled;
this.transportClientNode = transportClientNode;
}
@Override
protected void configure() {
XPackPlugin.bindFeatureSet(binder(), GraphFeatureSet.class);
if (enabled && transportClientNode == false) {
bind(GraphLicensee.class).asEagerSingleton();
} else {
bind(GraphLicensee.class).toProvider(Providers.of(null));
}
}
}

View File

@ -14,114 +14,81 @@ import static org.hamcrest.Matchers.is;
public class LicenseTests extends AbstractLicenseeTestCase { public class LicenseTests extends AbstractLicenseeTestCase {
private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry(); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY);
public void testPlatinumTrialLicenseCanDoEverything() throws Exception { public void testPlatinumTrialLicenseCanDoEverything() throws Exception {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
public void testBasicLicenseIsDisabled() throws Exception { public void testBasicLicenseIsDisabled() throws Exception {
licenseeRegistry.setOperationMode(OperationMode.BASIC); setOperationMode(graphLicensee, OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testStandardLicenseIsDisabled() throws Exception { public void testStandardLicenseIsDisabled() throws Exception {
licenseeRegistry.setOperationMode(OperationMode.STANDARD); setOperationMode(graphLicensee, OperationMode.STANDARD);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testNoLicenseDoesNotWork() { public void testNoLicenseDoesNotWork() {
licenseeRegistry.setOperationMode(OperationMode.BASIC); setOperationMode(graphLicensee, OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); disable(graphLicensee);
licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable();
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testExpiredPlatinumTrialLicenseIsRestricted() throws Exception { public void testExpiredPlatinumTrialLicenseIsRestricted() throws Exception {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); disable(graphLicensee);
licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable();
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testUpgradingFromBasicLicenseWorks() { public void testUpgradingFromBasicLicenseWorks() {
licenseeRegistry.setOperationMode(OperationMode.BASIC); setOperationMode(graphLicensee, OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
public void testDowngradingToBasicLicenseWorks() { public void testDowngradingToBasicLicenseWorks() {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(OperationMode.BASIC); setOperationMode(graphLicensee, OperationMode.BASIC);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testUpgradingFromStandardLicenseWorks() { public void testUpgradingFromStandardLicenseWorks() {
licenseeRegistry.setOperationMode(OperationMode.STANDARD); setOperationMode(graphLicensee, OperationMode.STANDARD);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
public void testDowngradingToStandardLicenseWorks() { public void testDowngradingToStandardLicenseWorks() {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(OperationMode.STANDARD); setOperationMode(graphLicensee, OperationMode.STANDARD);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testDowngradingToGoldLicenseWorks() { public void testDowngradingToGoldLicenseWorks() {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(OperationMode.GOLD); setOperationMode(graphLicensee, OperationMode.GOLD);
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testUpgradingExpiredLicenseWorks() { public void testUpgradingExpiredLicenseWorks() {
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); disable(graphLicensee);
licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable();
assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrStandardGoldOrNoneOrExpiredBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode()); setOperationMode(graphLicensee, randomTrialOrPlatinumMode());
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }

View File

@ -8,8 +8,7 @@ package org.elasticsearch.license.plugin;
import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -19,8 +18,6 @@ import org.elasticsearch.license.plugin.action.get.GetLicenseAction;
import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction; import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesMetaData; import org.elasticsearch.license.plugin.core.LicensesMetaData;
import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction; import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction;
@ -28,6 +25,12 @@ import org.elasticsearch.license.plugin.rest.RestGetLicenseAction;
import org.elasticsearch.license.plugin.rest.RestPutLicenseAction; import org.elasticsearch.license.plugin.rest.RestPutLicenseAction;
import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.xpack.graph.GraphLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.security.SecurityLicenseState;
import org.elasticsearch.xpack.security.SecurityLicensee;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.watcher.WatcherLicensee;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -42,7 +45,8 @@ import static org.elasticsearch.xpack.XPackPlugin.transportClientMode;
public class Licensing implements ActionPlugin { public class Licensing implements ActionPlugin {
public static final String NAME = "license"; public static final String NAME = "license";
private final boolean isTransportClient; protected final Settings settings;
protected final boolean isTransportClient;
private final boolean isTribeNode; private final boolean isTribeNode;
static { static {
@ -50,10 +54,15 @@ public class Licensing implements ActionPlugin {
} }
public Licensing(Settings settings) { public Licensing(Settings settings) {
this.settings = settings;
isTransportClient = transportClientMode(settings); isTransportClient = transportClientMode(settings);
isTribeNode = isTribeNode(settings); isTribeNode = isTribeNode(settings);
} }
public Collection<Module> nodeModules() {
return Collections.emptyList();
}
@Override @Override
public List<ActionHandler<? extends ActionRequest<?>, ? extends ActionResponse>> getActions() { public List<ActionHandler<? extends ActionRequest<?>, ? extends ActionResponse>> getActions() {
if (isTribeNode) { if (isTribeNode) {
@ -74,22 +83,16 @@ public class Licensing implements ActionPlugin {
RestDeleteLicenseAction.class); RestDeleteLicenseAction.class);
} }
public Collection<Class<? extends LifecycleComponent>> nodeServices() { public Collection<Object> createComponents(ClusterService clusterService, Clock clock,
if (isTransportClient == false && isTribeNode == false) { SecurityLicenseState securityLicenseState) {
return Collections.<Class<? extends LifecycleComponent>>singletonList(LicensesService.class); SecurityLicensee securityLicensee = new SecurityLicensee(settings, securityLicenseState);
} WatcherLicensee watcherLicensee = new WatcherLicensee(settings);
return Collections.emptyList(); MonitoringLicensee monitoringLicensee = new MonitoringLicensee(settings);
} GraphLicensee graphLicensee = new GraphLicensee(settings);
LicensesService licensesService = new LicensesService(settings, clusterService, clock,
Arrays.asList(securityLicensee, watcherLicensee, monitoringLicensee, graphLicensee));
public Collection<Module> nodeModules() { return Arrays.asList(licensesService, securityLicenseState, securityLicensee, watcherLicensee, monitoringLicensee, graphLicensee);
if (isTransportClient == false && isTribeNode == false) {
return Collections.singletonList(b -> {
b.bind(LicensesService.class).asEagerSingleton();
b.bind(LicenseeRegistry.class).to(LicensesService.class);
b.bind(LicensesManagerService.class).to(LicensesService.class);
});
}
return Collections.emptyList();
} }
public List<Setting<?>> getSettings() { public List<Setting<?>> getSettings() {

View File

@ -9,28 +9,28 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeReadAction; import org.elasticsearch.action.support.master.TransportMasterNodeReadAction;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
public class TransportGetLicenseAction extends TransportMasterNodeReadAction<GetLicenseRequest, GetLicenseResponse> { public class TransportGetLicenseAction extends TransportMasterNodeReadAction<GetLicenseRequest, GetLicenseResponse> {
private final LicensesManagerService licensesManagerService; private final LicensesService licensesService;
@Inject @Inject
public TransportGetLicenseAction(Settings settings, TransportService transportService, ClusterService clusterService, public TransportGetLicenseAction(Settings settings, TransportService transportService, ClusterService clusterService,
LicensesManagerService licensesManagerService, ThreadPool threadPool, ActionFilters actionFilters, LicensesService licensesService, ThreadPool threadPool, ActionFilters actionFilters,
IndexNameExpressionResolver indexNameExpressionResolver) { IndexNameExpressionResolver indexNameExpressionResolver) {
super(settings, GetLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver, super(settings, GetLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver,
GetLicenseRequest::new); GetLicenseRequest::new);
this.licensesManagerService = licensesManagerService; this.licensesService = licensesService;
} }
@Override @Override
@ -51,6 +51,6 @@ public class TransportGetLicenseAction extends TransportMasterNodeReadAction<Get
@Override @Override
protected void masterOperation(final GetLicenseRequest request, ClusterState state, final ActionListener<GetLicenseResponse> protected void masterOperation(final GetLicenseRequest request, ClusterState state, final ActionListener<GetLicenseResponse>
listener) throws ElasticsearchException { listener) throws ElasticsearchException {
listener.onResponse(new GetLicenseResponse(licensesManagerService.getLicense())); listener.onResponse(new GetLicenseResponse(licensesService.getLicense()));
} }
} }

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.license.plugin.core; package org.elasticsearch.license.plugin.core;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -14,33 +15,17 @@ import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* A supporting base class for injectable Licensee components. * A supporting base class for injectable Licensee components.
*/ */
public abstract class AbstractLicenseeComponent<T extends AbstractLicenseeComponent<T>> extends AbstractLifecycleComponent public abstract class AbstractLicenseeComponent extends AbstractComponent implements Licensee {
implements Licensee {
private final String id; private final String id;
private final LicenseeRegistry clientService;
private final List<Listener> listeners = new CopyOnWriteArrayList<>(); private final List<Listener> listeners = new CopyOnWriteArrayList<>();
// we initialize the licensee state to enabled with trial operation mode // we initialize the licensee state to enabled with trial operation mode
protected volatile Status status = Status.ENABLED; protected volatile Status status = Status.ENABLED;
protected AbstractLicenseeComponent(Settings settings, String id, LicenseeRegistry clientService) { protected AbstractLicenseeComponent(Settings settings, String id) {
super(settings); super(settings);
this.id = id; this.id = id;
this.clientService = clientService;
}
@Override
protected void doStart() {
clientService.register(this);
}
@Override
protected void doStop() {
}
@Override
protected void doClose() {
} }
@Override @Override

View File

@ -1,14 +0,0 @@
/*
* 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.plugin.core;
public interface LicenseeRegistry {
/**
* Registers a licensee for license notifications
*/
void register(Licensee licensee);
}

View File

@ -1,23 +0,0 @@
/*
* 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.plugin.core;
import org.elasticsearch.license.core.License;
import java.util.List;
public interface LicensesManagerService {
/**
* @return current {@link LicenseState}
*/
LicenseState licenseState();
/**
* @return the currently active license, or {@code null} if no license is currently installed
*/
License getLicense();
}

View File

@ -42,29 +42,20 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/** /**
* Service responsible for managing {@link LicensesMetaData} * Service responsible for managing {@link LicensesMetaData}
* Interfaces through which this is exposed are: * Interfaces through which this is exposed are:
* - LicensesManagerService - responsible for managing signed and one-time-trial licenses
* - LicensesClientService - responsible for listener registration of consumer plugin(s) * - LicensesClientService - responsible for listener registration of consumer plugin(s)
* <p> * <p>
* Registration Scheme:
* <p>
* A consumer plugin is registered with {@link LicenseeRegistry#register(Licensee)}
* This method can be called at any time during the life-cycle of the consumer plugin.
* If the listener can not be registered immediately, it is queued up and registered on the first clusterChanged event with
* no {@link org.elasticsearch.gateway.GatewayService#STATE_NOT_RECOVERED_BLOCK} block
* Upon successful registration, the listeners are notified appropriately using the notification scheme
* <p>
* Notification Scheme: * Notification Scheme:
* <p> * <p>
* All registered listeners are notified of the current license upon registration or when a new license is installed in the cluster state. * All registered listeners are notified of the current license upon registration or when a new license is installed in the cluster state.
* When a new license is notified as enabled to the registered listener, a notification is scheduled at the time of license expiry. * When a new license is notified as enabled to the registered listener, a notification is scheduled at the time of license expiry.
* Registered listeners are notified using {@link #onUpdate(LicensesMetaData)} * Registered listeners are notified using {@link #onUpdate(LicensesMetaData)}
*/ */
public class LicensesService extends AbstractLifecycleComponent implements ClusterStateListener, LicensesManagerService, public class LicensesService extends AbstractLifecycleComponent implements ClusterStateListener, SchedulerEngine.Listener {
LicenseeRegistry, SchedulerEngine.Listener {
// pkg private for tests // pkg private for tests
static final TimeValue TRIAL_LICENSE_DURATION = TimeValue.timeValueHours(30 * 24); static final TimeValue TRIAL_LICENSE_DURATION = TimeValue.timeValueHours(30 * 24);
@ -74,7 +65,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
/** /**
* Currently active consumers to notify to * Currently active consumers to notify to
*/ */
private final List<InternalLicensee> registeredLicensees = new CopyOnWriteArrayList<>(); private final List<InternalLicensee> registeredLicensees;
/** /**
* Currently active license * Currently active license
@ -105,14 +96,15 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " + private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " +
"please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:"; "please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:";
@Inject public LicensesService(Settings settings, ClusterService clusterService, Clock clock,
public LicensesService(Settings settings, ClusterService clusterService, Clock clock) { List<Licensee> registeredLicensees) {
super(settings); super(settings);
this.clusterService = clusterService; this.clusterService = clusterService;
populateExpirationCallbacks(); populateExpirationCallbacks();
this.clock = clock; this.clock = clock;
this.scheduler = new SchedulerEngine(clock); this.scheduler = new SchedulerEngine(clock);
this.scheduler.register(this); this.scheduler.register(this);
this.registeredLicensees = registeredLicensees.stream().map(InternalLicensee::new).collect(Collectors.toList());
} }
private void populateExpirationCallbacks() { private void populateExpirationCallbacks() {
@ -313,7 +305,6 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
}); });
} }
@Override
public LicenseState licenseState() { public LicenseState licenseState() {
if (registeredLicensees.size() > 0) { if (registeredLicensees.size() > 0) {
return registeredLicensees.get(0).currentLicenseState; return registeredLicensees.get(0).currentLicenseState;
@ -323,7 +314,6 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
} }
} }
@Override
public License getLicense() { public License getLicense() {
final License license = getLicense(clusterService.state().metaData().custom(LicensesMetaData.TYPE)); final License license = getLicense(clusterService.state().metaData().custom(LicensesMetaData.TYPE));
return license == LicensesMetaData.LICENSE_TOMBSTONE ? null : license; return license == LicensesMetaData.LICENSE_TOMBSTONE ? null : license;
@ -377,6 +367,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
protected void doStart() throws ElasticsearchException { protected void doStart() throws ElasticsearchException {
clusterService.add(this); clusterService.add(this);
scheduler.start(Collections.emptyList()); scheduler.start(Collections.emptyList());
registeredLicensees.forEach(x -> initLicensee(x.licensee));
} }
@Override @Override
@ -502,15 +493,8 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
} }
} }
@Override private void initLicensee(Licensee licensee) {
public void register(Licensee licensee) { logger.debug("initializing licensee [{}]", licensee.id());
for (final InternalLicensee existingLicensee : registeredLicensees) {
if (existingLicensee.id().equals(licensee.id())) {
throw new IllegalStateException("listener: [" + licensee.id() + "] has been already registered");
}
}
logger.debug("registering licensee [{}]", licensee.id());
registeredLicensees.add(new InternalLicensee(licensee));
final ClusterState clusterState = clusterService.state(); final ClusterState clusterState = clusterService.state();
if (clusterService.lifecycleState() == Lifecycle.State.STARTED if (clusterService.lifecycleState() == Lifecycle.State.STARTED
&& clusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) == false && clusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) == false
@ -521,7 +505,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
// triggers a cluster changed event // triggers a cluster changed event
// eventually notifying the current licensee // eventually notifying the current licensee
registerTrialLicense(); registerTrialLicense();
} else if (lifecycleState() == Lifecycle.State.STARTED) { } else {
notifyLicensees(currentMetaData.getLicense()); notifyLicensees(currentMetaData.getLicense());
} }
} }

View File

@ -176,6 +176,7 @@ public class LicensesServiceClusterTests extends AbstractLicensesIntegrationTest
License putLicenses = generateSignedLicense(TimeValue.timeValueMinutes(1)); License putLicenses = generateSignedLicense(TimeValue.timeValueMinutes(1));
PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder(cluster, PutLicenseAction.INSTANCE); PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder(cluster, PutLicenseAction.INSTANCE);
putLicenseRequestBuilder.setLicense(putLicenses); putLicenseRequestBuilder.setLicense(putLicenses);
putLicenseRequestBuilder.setAcknowledge(true);
final PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); final PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get();
assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true));
assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID));

View File

@ -5,6 +5,9 @@
*/ */
package org.elasticsearch.license.plugin.core; package org.elasticsearch.license.plugin.core;
import java.util.Arrays;
import java.util.Collections;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
@ -37,11 +40,11 @@ public abstract class AbstractLicenseServiceTestCase extends ESTestCase {
public void init() throws Exception { public void init() throws Exception {
clusterService = mock(ClusterService.class); clusterService = mock(ClusterService.class);
clock = new ClockMock(); clock = new ClockMock();
licensesService = new LicensesService(Settings.EMPTY, clusterService, clock);
discoveryNodes = mock(DiscoveryNodes.class); discoveryNodes = mock(DiscoveryNodes.class);
} }
protected void setInitialState(License license) { protected void setInitialState(License license, Licensee... licensees) {
licensesService = new LicensesService(Settings.EMPTY, clusterService, clock, Arrays.asList(licensees));
ClusterState state = mock(ClusterState.class); ClusterState state = mock(ClusterState.class);
final ClusterBlocks noBlock = ClusterBlocks.builder().build(); final ClusterBlocks noBlock = ClusterBlocks.builder().build();
when(state.blocks()).thenReturn(noBlock); when(state.blocks()).thenReturn(noBlock);

View File

@ -169,35 +169,18 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
return String.format(Locale.ROOT, "From [%s] to [%s]", fromMode, toMode); return String.format(Locale.ROOT, "From [%s] to [%s]", fromMode, toMode);
} }
public static class SimpleLicenseeRegistry extends AbstractComponent implements LicenseeRegistry { private OperationMode operationMode;
private final List<Licensee> licensees = new ArrayList<>();
private OperationMode operationMode;
public SimpleLicenseeRegistry() { public void setOperationMode(Licensee licensee, OperationMode operationMode) {
super(Settings.EMPTY); this.operationMode = operationMode;
} enable(licensee);
}
@Override public void disable(Licensee licensee) {
public void register(Licensee licensee) { licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
licensees.add(licensee); }
enable();
}
public void enable() { public void enable(Licensee licensee) {
for (Licensee licensee : licensees) { licensee.onChange(new Licensee.Status(operationMode, randomEnabledOrGracePeriodState()));
licensee.onChange(new Licensee.Status(operationMode, randomEnabledOrGracePeriodState()));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
}
}
public void setOperationMode(License.OperationMode operationMode) {
this.operationMode = operationMode;
enable();
}
} }
} }

View File

@ -35,10 +35,9 @@ public class LicenseClusterChangeTests extends AbstractLicenseServiceTestCase {
@Before @Before
public void setup() { public void setup() {
setInitialState(null);
licensesService.start();
licensee = new TestUtils.AssertingLicensee("LicenseClusterChangeTests", logger); licensee = new TestUtils.AssertingLicensee("LicenseClusterChangeTests", logger);
licensesService.register(licensee); setInitialState(null, licensee);
licensesService.start();
} }
@After @After

View File

@ -21,13 +21,11 @@ import static org.mockito.Mockito.when;
public class LicenseRegistrationTests extends AbstractLicenseServiceTestCase { public class LicenseRegistrationTests extends AbstractLicenseServiceTestCase {
public void testTrialLicenseRequestOnEmptyLicenseState() throws Exception { public void testTrialLicenseRequestOnEmptyLicenseState() throws Exception {
setInitialState(null);
when(discoveryNodes.isLocalNodeElectedMaster()).thenReturn(true);
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee( TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(
"testTrialLicenseRequestOnEmptyLicenseState", logger); "testTrialLicenseRequestOnEmptyLicenseState", logger);
setInitialState(null, licensee);
when(discoveryNodes.isLocalNodeElectedMaster()).thenReturn(true);
licensesService.start(); licensesService.start();
licensesService.register(licensee);
ClusterState state = ClusterState.builder(new ClusterName("a")).build(); ClusterState state = ClusterState.builder(new ClusterName("a")).build();
ArgumentCaptor<ClusterStateUpdateTask> stateUpdater = ArgumentCaptor.forClass(ClusterStateUpdateTask.class); ArgumentCaptor<ClusterStateUpdateTask> stateUpdater = ArgumentCaptor.forClass(ClusterStateUpdateTask.class);
@ -42,11 +40,10 @@ public class LicenseRegistrationTests extends AbstractLicenseServiceTestCase {
} }
public void testNotificationOnRegistration() throws Exception { public void testNotificationOnRegistration() throws Exception {
setInitialState(TestUtils.generateSignedLicense(TimeValue.timeValueHours(2)));
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee( TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(
"testNotificationOnRegistration", logger); "testNotificationOnRegistration", logger);
setInitialState(TestUtils.generateSignedLicense(TimeValue.timeValueHours(2)), licensee);
licensesService.start(); licensesService.start();
licensesService.register(licensee);
assertThat(licensee.statuses.size(), equalTo(1)); assertThat(licensee.statuses.size(), equalTo(1));
final LicenseState licenseState = licensee.statuses.get(0).getLicenseState(); final LicenseState licenseState = licensee.statuses.get(0).getLicenseState();
assertTrue(licenseState == LicenseState.ENABLED); assertTrue(licenseState == LicenseState.ENABLED);

View File

@ -27,13 +27,13 @@ import static org.mockito.Mockito.verify;
public class LicensesAcknowledgementTests extends AbstractLicenseServiceTestCase { public class LicensesAcknowledgementTests extends AbstractLicenseServiceTestCase {
public void testAcknowledgment() throws Exception { public void testAcknowledgment() throws Exception {
setInitialState(TestUtils.generateSignedLicense("trial", TimeValue.timeValueHours(2)));
licensesService.start();
String id = "testAcknowledgment"; String id = "testAcknowledgment";
String[] acknowledgeMessages = new String[] {"message"}; String[] acknowledgeMessages = new String[] {"message"};
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(id, logger); TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(id, logger);
setInitialState(TestUtils.generateSignedLicense("trial", TimeValue.timeValueHours(2)), licensee);
licensesService.start();
licensee.setAcknowledgementMessages(acknowledgeMessages); licensee.setAcknowledgementMessages(acknowledgeMessages);
licensesService.register(licensee);
// try installing a signed license // try installing a signed license
License signedLicense = generateSignedLicense(TimeValue.timeValueHours(10)); License signedLicense = generateSignedLicense(TimeValue.timeValueHours(10));
PutLicenseRequest putLicenseRequest = new PutLicenseRequest().license(signedLicense); PutLicenseRequest putLicenseRequest = new PutLicenseRequest().license(signedLicense);
@ -57,8 +57,6 @@ public class LicensesAcknowledgementTests extends AbstractLicenseServiceTestCase
} }
public void testAcknowledgementMultipleLicensee() throws Exception { public void testAcknowledgementMultipleLicensee() throws Exception {
setInitialState(TestUtils.generateSignedLicense("trial", TimeValue.timeValueHours(2)));
licensesService.start();
String id1 = "testAcknowledgementMultipleLicensee_1"; String id1 = "testAcknowledgementMultipleLicensee_1";
String[] acknowledgeMessages1 = new String[] {"testAcknowledgementMultipleLicensee_1"}; String[] acknowledgeMessages1 = new String[] {"testAcknowledgementMultipleLicensee_1"};
String id2 = "testAcknowledgementMultipleLicensee_2"; String id2 = "testAcknowledgementMultipleLicensee_2";
@ -67,8 +65,8 @@ public class LicensesAcknowledgementTests extends AbstractLicenseServiceTestCase
licensee1.setAcknowledgementMessages(acknowledgeMessages1); licensee1.setAcknowledgementMessages(acknowledgeMessages1);
TestUtils.AssertingLicensee licensee2 = new TestUtils.AssertingLicensee(id2, logger); TestUtils.AssertingLicensee licensee2 = new TestUtils.AssertingLicensee(id2, logger);
licensee2.setAcknowledgementMessages(acknowledgeMessages2); licensee2.setAcknowledgementMessages(acknowledgeMessages2);
licensesService.register(licensee1); setInitialState(TestUtils.generateSignedLicense("trial", TimeValue.timeValueHours(2)), licensee1, licensee2);
licensesService.register(licensee2); licensesService.start();
// try installing a signed license // try installing a signed license
License signedLicense = generateSignedLicense(TimeValue.timeValueHours(10)); License signedLicense = generateSignedLicense(TimeValue.timeValueHours(10));
PutLicenseRequest putLicenseRequest = new PutLicenseRequest().license(signedLicense); PutLicenseRequest putLicenseRequest = new PutLicenseRequest().license(signedLicense);

View File

@ -5,27 +5,27 @@
*/ */
package org.elasticsearch.license.plugin.core; package org.elasticsearch.license.plugin.core;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse; import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.xpack.graph.Graph;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.TestUtils; import org.elasticsearch.license.plugin.TestUtils;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.xpack.monitoring.Monitoring;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.xpack.security.Security;
import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.graph.Graph;
import org.elasticsearch.xpack.monitoring.Monitoring;
import org.elasticsearch.xpack.security.Security;
import org.elasticsearch.xpack.watcher.Watcher; import org.elasticsearch.xpack.watcher.Watcher;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.elasticsearch.license.plugin.TestUtils.generateSignedLicense; import static org.elasticsearch.license.plugin.TestUtils.generateSignedLicense;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
@ -116,9 +116,6 @@ public class LicensesManagerServiceTests extends ESSingleNodeTestCase {
LicensesService licensesService = getInstanceFromNode(LicensesService.class); LicensesService licensesService = getInstanceFromNode(LicensesService.class);
ClusterService clusterService = getInstanceFromNode(ClusterService.class); ClusterService clusterService = getInstanceFromNode(ClusterService.class);
// generate a trial license for one feature
licensesService.register(new TestUtils.AssertingLicensee("", logger));
// generate signed licenses // generate signed licenses
License license = generateSignedLicense(TimeValue.timeValueHours(1)); License license = generateSignedLicense(TimeValue.timeValueHours(1));
TestUtils.registerAndAckSignedLicenses(licensesService, license, LicensesStatus.VALID); TestUtils.registerAndAckSignedLicenses(licensesService, license, LicensesStatus.VALID);
@ -131,53 +128,6 @@ public class LicensesManagerServiceTests extends ESSingleNodeTestCase {
assertThat(licensesMetaData.getLicense(), equalTo(LicensesMetaData.LICENSE_TOMBSTONE)); assertThat(licensesMetaData.getLicense(), equalTo(LicensesMetaData.LICENSE_TOMBSTONE));
} }
public void testRemoveLicensesAndLicenseeNotification() throws Exception {
LicensesService licensesService = getInstanceFromNode(LicensesService.class);
licensesService.start();
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
// generate a trial license for one feature
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee("", logger);
licensesService.register(licensee);
// we should get a trial license to begin with
assertBusy(new Runnable() {
@Override
public void run() {
assertThat(licensee.statuses, hasSize(1));
assertThat(licensee.statuses.get(0).getMode(), is(License.OperationMode.TRIAL));
assertThat(licensee.statuses.get(0).getLicenseState(), is(LicenseState.ENABLED));
}
});
// generate signed licenses
License license = generateSignedLicense("gold", TimeValue.timeValueHours(1));
TestUtils.registerAndAckSignedLicenses(licensesService, license, LicensesStatus.VALID);
assertBusy(new Runnable() {
@Override
public void run() {
assertThat(licensee.statuses, hasSize(2));
assertThat(licensee.statuses.get(1).getMode(), not(License.OperationMode.TRIAL));
assertThat(licensee.statuses.get(1).getLicenseState(), is(LicenseState.ENABLED));
}
});
// remove signed licenses
removeAndAckSignedLicenses(licensesService);
assertBusy(new Runnable() {
@Override
public void run() {
assertThat(licensee.statuses, hasSize(3));
}
});
LicensesMetaData licensesMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE);
assertThat(licensesMetaData.getLicense(), is(LicensesMetaData.LICENSE_TOMBSTONE));
assertThat(licensee.statuses, hasSize(3));
assertThat(licensee.statuses.get(2).getLicenseState(), is(LicenseState.DISABLED));
assertThat(licensee.statuses.get(2).getMode(), is(License.OperationMode.MISSING));
}
private void removeAndAckSignedLicenses(final LicensesService licensesService) { private void removeAndAckSignedLicenses(final LicensesService licensesService) {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false); final AtomicBoolean success = new AtomicBoolean(false);

View File

@ -5,6 +5,8 @@
*/ */
package org.elasticsearch.license.plugin.core; package org.elasticsearch.license.plugin.core;
import java.util.List;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.TestUtils; import org.elasticsearch.license.plugin.TestUtils;
@ -12,21 +14,20 @@ import org.elasticsearch.license.plugin.TestUtils.AssertingLicensee;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import java.util.List;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class LicensesNotificationTests extends AbstractLicenseServiceTestCase { public class LicensesNotificationTests extends AbstractLicenseServiceTestCase {
public void testLicenseNotification() throws Exception { public void testLicenseNotification() throws Exception {
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(48)); final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(48));
setInitialState(license);
licensesService.start();
int nLicensee = randomIntBetween(1, 3); int nLicensee = randomIntBetween(1, 3);
AssertingLicensee[] assertingLicensees = new AssertingLicensee[nLicensee]; AssertingLicensee[] assertingLicensees = new AssertingLicensee[nLicensee];
for (int i = 0; i < assertingLicensees.length; i++) { for (int i = 0; i < assertingLicensees.length; i++) {
assertingLicensees[i] = new AssertingLicensee("testLicenseNotification" + i, logger); assertingLicensees[i] = new AssertingLicensee("testLicenseNotification" + i, logger);
licensesService.register(assertingLicensees[i]); }
setInitialState(license, assertingLicensees);
licensesService.start();
for (int i = 0; i < assertingLicensees.length; i++) {
assertLicenseStates(assertingLicensees[i], LicenseState.ENABLED); assertLicenseStates(assertingLicensees[i], LicenseState.ENABLED);
} }
clock.fastForward(TimeValue.timeValueMillis(license.expiryDate() - clock.millis())); clock.fastForward(TimeValue.timeValueMillis(license.expiryDate() - clock.millis()));

View File

@ -78,9 +78,7 @@ public class Monitoring implements ActionPlugin {
if (enabled == false || transportClientMode || tribeNode) { if (enabled == false || transportClientMode || tribeNode) {
return Collections.emptyList(); return Collections.emptyList();
} }
return Arrays.<Class<? extends LifecycleComponent>>asList(MonitoringLicensee.class, return Arrays.<Class<? extends LifecycleComponent>>asList(AgentService.class, CleanerService.class);
AgentService.class,
CleanerService.class);
} }
@Override @Override

View File

@ -6,15 +6,12 @@
package org.elasticsearch.xpack.monitoring; package org.elasticsearch.xpack.monitoring;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
/** /**
* {@code MonitoringLicensee} determines whether certain features of Monitoring are enabled or disabled. * {@code MonitoringLicensee} determines whether certain features of Monitoring are enabled or disabled.
@ -25,11 +22,10 @@ import org.elasticsearch.license.plugin.core.LicenseeRegistry;
* <li>Cleaning up (deleting) older indices.</li> * <li>Cleaning up (deleting) older indices.</li>
* </ul> * </ul>
*/ */
public class MonitoringLicensee extends AbstractLicenseeComponent<MonitoringLicensee> implements Licensee { public class MonitoringLicensee extends AbstractLicenseeComponent {
@Inject public MonitoringLicensee(Settings settings) {
public MonitoringLicensee(Settings settings, LicenseeRegistry clientService) { super(settings, Monitoring.NAME);
super(settings, Monitoring.NAME, clientService);
} }
/** /**

View File

@ -26,11 +26,10 @@ public class MonitoringModule extends AbstractModule {
XPackPlugin.bindFeatureSet(binder(), MonitoringFeatureSet.class); XPackPlugin.bindFeatureSet(binder(), MonitoringFeatureSet.class);
if (enabled && transportClientMode == false) { if (enabled && transportClientMode == false) {
bind(MonitoringLicensee.class).asEagerSingleton();
bind(MonitoringSettings.class).asEagerSingleton(); bind(MonitoringSettings.class).asEagerSingleton();
bind(AgentService.class).asEagerSingleton(); bind(AgentService.class).asEagerSingleton();
bind(CleanerService.class).asEagerSingleton(); bind(CleanerService.class).asEagerSingleton();
} else { } else if (transportClientMode) {
bind(MonitoringLicensee.class).toProvider(Providers.of(null)); bind(MonitoringLicensee.class).toProvider(Providers.of(null));
} }
} }

View File

@ -14,7 +14,7 @@ import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicenseUtils; import org.elasticsearch.license.plugin.core.LicenseUtils;
import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee; import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.MonitoringSettings;
import org.elasticsearch.xpack.monitoring.agent.collector.AbstractCollector; import org.elasticsearch.xpack.monitoring.agent.collector.AbstractCollector;
@ -40,16 +40,16 @@ public class ClusterStatsCollector extends AbstractCollector {
public static final String NAME = "cluster-stats-collector"; public static final String NAME = "cluster-stats-collector";
private final LicensesManagerService licensesManagerService; private final LicensesService licensesService;
private final Client client; private final Client client;
@Inject @Inject
public ClusterStatsCollector(Settings settings, ClusterService clusterService, public ClusterStatsCollector(Settings settings, ClusterService clusterService,
MonitoringSettings monitoringSettings, MonitoringLicensee licensee, InternalClient client, MonitoringSettings monitoringSettings, MonitoringLicensee licensee, InternalClient client,
LicensesManagerService licensesManagerService) { LicensesService licensesService) {
super(settings, NAME, clusterService, monitoringSettings, licensee); super(settings, NAME, clusterService, monitoringSettings, licensee);
this.client = client; this.client = client;
this.licensesManagerService = licensesManagerService; this.licensesService = licensesService;
} }
@Override @Override
@ -85,7 +85,7 @@ public class ClusterStatsCollector extends AbstractCollector {
clusterInfoDoc.setSourceNode(sourceNode); clusterInfoDoc.setSourceNode(sourceNode);
clusterInfoDoc.setClusterName(clusterService.getClusterName().value()); clusterInfoDoc.setClusterName(clusterService.getClusterName().value());
clusterInfoDoc.setVersion(Version.CURRENT.toString()); clusterInfoDoc.setVersion(Version.CURRENT.toString());
clusterInfoDoc.setLicense(licensesManagerService.getLicense()); clusterInfoDoc.setLicense(licensesService.getLicense());
clusterInfoDoc.setClusterStats(clusterStats); clusterInfoDoc.setClusterStats(clusterStats);
results.add(clusterInfoDoc); results.add(clusterInfoDoc);

View File

@ -5,43 +5,43 @@
*/ */
package org.elasticsearch.xpack.monitoring.agent.collector; package org.elasticsearch.xpack.monitoring.agent.collector;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.carrotsearch.randomizedtesting.RandomizedTest; import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.SysGlobals; import com.carrotsearch.randomizedtesting.SysGlobals;
import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.block.ClusterBlocks; import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.Licensing; import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee; import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.xpack.security.InternalClient;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.graph.GraphLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.MonitoringSettings;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
import org.elasticsearch.xpack.security.InternalClient;
import org.elasticsearch.xpack.security.SecurityLicenseState;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.watcher.WatcherLicensee;
import org.junit.Before; import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static org.elasticsearch.common.unit.TimeValue.timeValueMinutes; import static org.elasticsearch.common.unit.TimeValue.timeValueMinutes;
@ -110,7 +110,7 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license.operationMode(), LicenseState.ENABLED); service.onChange(license.operationMode(), LicenseState.ENABLED);
} }
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.update(license); service.update(license);
} }
} }
@ -123,7 +123,7 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license.operationMode(), LicenseState.GRACE_PERIOD); service.onChange(license.operationMode(), LicenseState.GRACE_PERIOD);
} }
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.update(license); service.update(license);
} }
} }
@ -136,7 +136,7 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license.operationMode(), LicenseState.DISABLED); service.onChange(license.operationMode(), LicenseState.DISABLED);
} }
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.update(license); service.update(license);
} }
} }
@ -149,7 +149,7 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.onChange(license.operationMode(), LicenseState.DISABLED); service.onChange(license.operationMode(), LicenseState.DISABLED);
} }
for (LicensesManagerServiceForCollectors service : internalCluster().getInstances(LicensesManagerServiceForCollectors.class)) { for (LicenseServiceForCollectors service : internalCluster().getInstances(LicenseServiceForCollectors.class)) {
service.update(license); service.update(license);
} }
} }
@ -190,16 +190,18 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
@Override @Override
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
return Collections.<Module>singletonList(new AbstractModule() { return Collections.singletonList(b -> b.bind(LicensesService.class).to(LicenseServiceForCollectors.class));
}
@Override @Override
protected void configure() { public Collection<Object> createComponents(ClusterService clusterService, Clock clock,
bind(LicenseServiceForCollectors.class).asEagerSingleton(); SecurityLicenseState securityLicenseState) {
bind(LicenseeRegistry.class).to(LicenseServiceForCollectors.class); WatcherLicensee watcherLicensee = new WatcherLicensee(settings);
bind(LicensesManagerServiceForCollectors.class).asEagerSingleton(); MonitoringLicensee monitoringLicensee = new MonitoringLicensee(settings);
bind(LicensesManagerService.class).to(LicensesManagerServiceForCollectors.class); GraphLicensee graphLicensee = new GraphLicensee(settings);
} LicensesService licensesService = new LicenseServiceForCollectors(settings,
}); Arrays.asList(watcherLicensee, monitoringLicensee, graphLicensee));
return Arrays.asList(licensesService, watcherLicensee, monitoringLicensee, graphLicensee);
} }
@Override @Override
@ -211,11 +213,6 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
public List<Class<? extends RestHandler>> getRestHandlers() { public List<Class<? extends RestHandler>> getRestHandlers() {
return emptyList(); return emptyList();
} }
@Override
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
return Collections.emptyList();
}
} }
public static class InternalXPackPlugin extends XPackPlugin { public static class InternalXPackPlugin extends XPackPlugin {
@ -226,18 +223,15 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
} }
} }
public static class LicenseServiceForCollectors extends AbstractComponent implements LicenseeRegistry { public static class LicenseServiceForCollectors extends LicensesService {
private final List<Licensee> licensees = new ArrayList<>(); private final List<Licensee> licensees;
private volatile License license;
@Inject @Inject
public LicenseServiceForCollectors(Settings settings) { public LicenseServiceForCollectors(Settings settings, List<Licensee> licensees) {
super(settings); super(settings, null, null, licensees);
} this.licensees = licensees;
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
} }
public void onChange(License.OperationMode operationMode, LicenseState state) { public void onChange(License.OperationMode operationMode, LicenseState state) {
@ -245,11 +239,6 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
licensee.onChange(new Licensee.Status(operationMode, state)); licensee.onChange(new Licensee.Status(operationMode, state));
} }
} }
}
public static class LicensesManagerServiceForCollectors implements LicensesManagerService {
private volatile License license;
@Override @Override
public LicenseState licenseState() { public LicenseState licenseState() {
@ -264,5 +253,11 @@ public abstract class AbstractCollectorTestCase extends MonitoringIntegTestCase
public synchronized void update(License license) { public synchronized void update(License license) {
this.license = license; this.license = license;
} }
@Override
protected void doStart() {}
@Override
protected void doStop() {}
} }
} }

View File

@ -5,11 +5,13 @@
*/ */
package org.elasticsearch.xpack.monitoring.agent.collector.cluster; package org.elasticsearch.xpack.monitoring.agent.collector.cluster;
import java.util.Collection;
import org.apache.lucene.util.LuceneTestCase.BadApple; import org.apache.lucene.util.LuceneTestCase.BadApple;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.xpack.monitoring.MonitoredSystem; import org.elasticsearch.xpack.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee; import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringSettings; import org.elasticsearch.xpack.monitoring.MonitoringSettings;
@ -17,8 +19,6 @@ import org.elasticsearch.xpack.monitoring.agent.collector.AbstractCollector;
import org.elasticsearch.xpack.monitoring.agent.collector.AbstractCollectorTestCase; import org.elasticsearch.xpack.monitoring.agent.collector.AbstractCollectorTestCase;
import org.elasticsearch.xpack.monitoring.agent.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.agent.exporter.MonitoringDoc;
import java.util.Collection;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
@ -133,7 +133,7 @@ public class ClusterStatsCollectorTests extends AbstractCollectorTestCase {
internalCluster().getInstance(MonitoringSettings.class, nodeId), internalCluster().getInstance(MonitoringSettings.class, nodeId),
internalCluster().getInstance(MonitoringLicensee.class, nodeId), internalCluster().getInstance(MonitoringLicensee.class, nodeId),
securedClient(nodeId), securedClient(nodeId),
internalCluster().getInstance(LicensesManagerService.class, nodeId)); internalCluster().getInstance(LicensesService.class, nodeId));
} }
private void assertCanCollect(AbstractCollector collector, Class<?>... classes) { private void assertCanCollect(AbstractCollector collector, Class<?>... classes) {

View File

@ -7,9 +7,7 @@ package org.elasticsearch.xpack.monitoring.license;
import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -17,14 +15,17 @@ import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.Licensing; import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee; import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.graph.GraphLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee; import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase;
import org.elasticsearch.xpack.security.SecurityLicenseState;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.watcher.WatcherLicensee;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -92,7 +93,18 @@ public class LicenseIntegrationTests extends MonitoringIntegTestCase {
@Override @Override
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
return Collections.<Module>singletonList(new InternalLicenseModule()); return Collections.singletonList(b -> b.bind(LicensesService.class).to(MockLicenseService.class));
}
@Override
public Collection<Object> createComponents(ClusterService clusterService, Clock clock,
SecurityLicenseState securityLicenseState) {
WatcherLicensee watcherLicensee = new WatcherLicensee(settings);
MonitoringLicensee monitoringLicensee = new MonitoringLicensee(settings);
GraphLicensee graphLicensee = new GraphLicensee(settings);
LicensesService licensesService = new MockLicenseService(settings,
Arrays.asList(watcherLicensee, monitoringLicensee, graphLicensee));
return Arrays.asList(licensesService, watcherLicensee, monitoringLicensee, graphLicensee);
} }
@Override @Override
@ -104,36 +116,16 @@ public class LicenseIntegrationTests extends MonitoringIntegTestCase {
public List<Class<? extends RestHandler>> getRestHandlers() { public List<Class<? extends RestHandler>> getRestHandlers() {
return emptyList(); return emptyList();
} }
@Override
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
return Collections.emptyList();
}
} }
public static class InternalLicenseModule extends AbstractModule { public static class MockLicenseService extends LicensesService {
@Override
protected void configure() {
bind(MockLicenseService.class).asEagerSingleton();
bind(LicenseeRegistry.class).to(MockLicenseService.class);
bind(LicensesManagerService.class).to(MockLicenseService.class);
}
}
public static class MockLicenseService extends AbstractComponent implements LicenseeRegistry, LicensesManagerService { private final List<Licensee> licensees;
private final List<Licensee> licensees = new ArrayList<>();
@Inject @Inject
public MockLicenseService(Settings settings) { public MockLicenseService(Settings settings, List<Licensee> licensees) {
super(settings); super(settings, null, null, licensees);
enable(); this.licensees = licensees;
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable(); enable();
} }
@ -159,6 +151,12 @@ public class LicenseIntegrationTests extends MonitoringIntegTestCase {
public License getLicense() { public License getLicense() {
return null; return null;
} }
@Override
protected void doStart() {}
@Override
protected void doStop() {}
} }
public static class InternalXPackPlugin extends XPackPlugin { public static class InternalXPackPlugin extends XPackPlugin {

View File

@ -10,7 +10,6 @@ import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee.Status; import org.elasticsearch.license.plugin.core.Licensee.Status;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee; import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -18,7 +17,6 @@ import java.util.function.Predicate;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
@ -27,8 +25,7 @@ import static org.mockito.Mockito.when;
* If you change the behavior of these tests, then it means that licensing changes for Monitoring! * If you change the behavior of these tests, then it means that licensing changes for Monitoring!
*/ */
public class MonitoringLicenseeTests extends AbstractLicenseeTestCase { public class MonitoringLicenseeTests extends AbstractLicenseeTestCase {
private final LicenseeRegistry registry = mock(LicenseeRegistry.class); private final MonitoringLicensee licensee = new MonitoringLicensee(Settings.EMPTY);
private final MonitoringLicensee licensee = new MonitoringLicensee(Settings.EMPTY, registry);
public void testAcknowledgementMessagesToAnyFromFreeIsNoOp() { public void testAcknowledgementMessagesToAnyFromFreeIsNoOp() {
assertEmptyAck(OperationMode.BASIC, randomMode(), licensee); assertEmptyAck(OperationMode.BASIC, randomMode(), licensee);
@ -93,7 +90,6 @@ public class MonitoringLicenseeTests extends AbstractLicenseeTestCase {
assertThat(predicate.test(licensee), equalTo(expected)); assertThat(predicate.test(licensee), equalTo(expected));
verify(status).getLicenseState(); verify(status).getLicenseState();
verifyNoMoreInteractions(registry);
} }
/** /**
@ -112,6 +108,5 @@ public class MonitoringLicenseeTests extends AbstractLicenseeTestCase {
assertThat(predicate.test(licensee), equalTo(expected)); assertThat(predicate.test(licensee), equalTo(expected));
verify(status).getMode(); verify(status).getMode();
verifyNoMoreInteractions(registry);
} }
} }

View File

@ -117,7 +117,7 @@ public class Security implements ActionPlugin {
private final Settings settings; private final Settings settings;
private final boolean enabled; private final boolean enabled;
private final boolean transportClientMode; private final boolean transportClientMode;
private SecurityLicenseState securityLicenseState; private final SecurityLicenseState securityLicenseState;
private final CryptoService cryptoService; private final CryptoService cryptoService;
public Security(Settings settings, Environment env) throws IOException { public Security(Settings settings, Environment env) throws IOException {
@ -130,12 +130,17 @@ public class Security implements ActionPlugin {
} else { } else {
cryptoService = null; cryptoService = null;
} }
securityLicenseState = new SecurityLicenseState();
} }
public CryptoService getCryptoService() { public CryptoService getCryptoService() {
return cryptoService; return cryptoService;
} }
public SecurityLicenseState getSecurityLicenseState() {
return securityLicenseState;
}
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
@ -147,7 +152,7 @@ public class Security implements ActionPlugin {
if (enabled == false) { if (enabled == false) {
return modules; return modules;
} }
modules.add(new SecurityModule(settings, securityLicenseState)); modules.add(new SecurityModule(settings));
modules.add(new SecurityTransportModule(settings)); modules.add(new SecurityTransportModule(settings));
modules.add(new SSLModule(settings)); modules.add(new SSLModule(settings));
return modules; return modules;
@ -157,7 +162,7 @@ public class Security implements ActionPlugin {
modules.add(new AuthorizationModule(settings)); modules.add(new AuthorizationModule(settings));
if (enabled == false) { if (enabled == false) {
modules.add(b -> b.bind(CryptoService.class).toProvider(Providers.of(null))); modules.add(b -> b.bind(CryptoService.class).toProvider(Providers.of(null)));
modules.add(new SecurityModule(settings, securityLicenseState)); modules.add(new SecurityModule(settings));
modules.add(new AuditTrailModule(settings)); modules.add(new AuditTrailModule(settings));
modules.add(new SecurityTransportModule(settings)); modules.add(new SecurityTransportModule(settings));
return modules; return modules;
@ -166,9 +171,9 @@ public class Security implements ActionPlugin {
// we can't load that at construction time since the license plugin might not have been loaded at that point // we can't load that at construction time since the license plugin might not have been loaded at that point
// which might not be the case during Plugin class instantiation. Once nodeModules are pulled // which might not be the case during Plugin class instantiation. Once nodeModules are pulled
// everything should have been loaded // everything should have been loaded
securityLicenseState = new SecurityLicenseState();
modules.add(b -> b.bind(CryptoService.class).toInstance(cryptoService)); modules.add(b -> b.bind(CryptoService.class).toInstance(cryptoService));
modules.add(new SecurityModule(settings, securityLicenseState)); modules.add(new SecurityModule(settings));
modules.add(new AuditTrailModule(settings)); modules.add(new AuditTrailModule(settings));
modules.add(new SecurityRestModule(settings)); modules.add(new SecurityRestModule(settings));
modules.add(new SecurityActionModule(settings)); modules.add(new SecurityActionModule(settings));
@ -182,7 +187,6 @@ public class Security implements ActionPlugin {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Class<? extends LifecycleComponent>> list = new ArrayList<>(); List<Class<? extends LifecycleComponent>> list = new ArrayList<>();
list.add(SecurityLicensee.class);
list.add(FileRolesStore.class); list.add(FileRolesStore.class);
list.add(Realms.class); list.add(Realms.class);
return list; return list;

View File

@ -5,28 +5,21 @@
*/ */
package org.elasticsearch.xpack.security; package org.elasticsearch.xpack.security;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
/** /**
* *
*/ */
public class SecurityLicensee extends AbstractLicenseeComponent<SecurityLicensee> implements Licensee { public class SecurityLicensee extends AbstractLicenseeComponent {
private final boolean isTribeNode;
private final SecurityLicenseState securityLicenseState; private final SecurityLicenseState securityLicenseState;
@Inject public SecurityLicensee(Settings settings, SecurityLicenseState securityLicenseState) {
public SecurityLicensee(Settings settings, LicenseeRegistry clientService, SecurityLicenseState securityLicenseState) { super(settings, Security.NAME);
super(settings, Security.NAME, clientService);
this.securityLicenseState = securityLicenseState; this.securityLicenseState = securityLicenseState;
this.isTribeNode = settings.getGroups("tribe", true).isEmpty() == false;
} }
@Override @Override
@ -96,14 +89,4 @@ public class SecurityLicensee extends AbstractLicenseeComponent<SecurityLicensee
} }
return Strings.EMPTY_ARRAY; return Strings.EMPTY_ARRAY;
} }
@Override
protected void doStart() throws ElasticsearchException {
// we rely on the initial licensee state to be enabled with trial operation mode
// to ensure no operation is blocked due to not registering the licensee on a
// tribe node
if (isTribeNode == false) {
super.doStart();
}
}
} }

View File

@ -5,21 +5,17 @@
*/ */
package org.elasticsearch.xpack.security; package org.elasticsearch.xpack.security;
import org.elasticsearch.common.inject.util.Providers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.security.support.AbstractSecurityModule;
import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.security.support.AbstractSecurityModule;
/** /**
* *
*/ */
public class SecurityModule extends AbstractSecurityModule { public class SecurityModule extends AbstractSecurityModule {
private final SecurityLicenseState securityLicenseState; public SecurityModule(Settings settings) {
public SecurityModule(Settings settings, SecurityLicenseState securityLicenseState) {
super(settings); super(settings);
this.securityLicenseState = securityLicenseState;
} }
@Override @Override
@ -28,12 +24,6 @@ public class SecurityModule extends AbstractSecurityModule {
return; return;
} }
if (securityLicenseState != null) {
bind(SecurityLicenseState.class).toInstance(securityLicenseState);
} else {
bind(SecurityLicenseState.class).toProvider(Providers.<SecurityLicenseState>of(null));
}
XPackPlugin.bindFeatureSet(binder(), SecurityFeatureSet.class); XPackPlugin.bindFeatureSet(binder(), SecurityFeatureSet.class);
if (securityEnabled) { if (securityEnabled) {

View File

@ -5,6 +5,13 @@
*/ */
package org.elasticsearch.integration; package org.elasticsearch.integration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.ElasticsearchSecurityException;
import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
@ -19,10 +26,7 @@ import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.transport.NoNodeAvailableException; import org.elasticsearch.client.transport.NoNodeAvailableException;
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -31,7 +35,7 @@ import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.Licensing; import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee; import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
@ -40,16 +44,16 @@ import org.elasticsearch.test.SecuritySettingsSource;
import org.elasticsearch.transport.Transport; import org.elasticsearch.transport.Transport;
import org.elasticsearch.xpack.MockNetty3Plugin; import org.elasticsearch.xpack.MockNetty3Plugin;
import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.graph.GraphLicensee;
import org.elasticsearch.xpack.monitoring.MonitoringLicensee;
import org.elasticsearch.xpack.security.Security; import org.elasticsearch.xpack.security.Security;
import org.elasticsearch.xpack.security.SecurityLicenseState;
import org.elasticsearch.xpack.security.SecurityLicensee;
import org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.watcher.WatcherLicensee;
import org.junit.After; import org.junit.After;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
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;
@ -231,7 +235,7 @@ public class LicensingTests extends SecurityIntegTestCase {
} }
public static void disableLicensing(OperationMode operationMode) { public static void disableLicensing(OperationMode operationMode) {
for (InternalLicenseeRegistry service : internalCluster().getInstances(InternalLicenseeRegistry.class)) { for (TestLicensesService service : internalCluster().getInstances(TestLicensesService.class)) {
service.disable(operationMode); service.disable(operationMode);
} }
} }
@ -241,7 +245,7 @@ public class LicensingTests extends SecurityIntegTestCase {
} }
public static void enableLicensing(OperationMode operationMode) { public static void enableLicensing(OperationMode operationMode) {
for (InternalLicenseeRegistry service : internalCluster().getInstances(InternalLicenseeRegistry.class)) { for (TestLicensesService service : internalCluster().getInstances(TestLicensesService.class)) {
service.enable(operationMode); service.enable(operationMode);
} }
} }
@ -250,7 +254,20 @@ public class LicensingTests extends SecurityIntegTestCase {
@Override @Override
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
return Collections.<Module>singletonList(new InternalLicenseModule()); return Collections.singletonList(b -> b.bind(LicensesService.class).to(TestLicensesService.class));
}
@Override
public Collection<Object> createComponents(ClusterService clusterService, Clock clock,
SecurityLicenseState securityLicenseState) {
SecurityLicensee securityLicensee = new SecurityLicensee(settings, securityLicenseState);
WatcherLicensee watcherLicensee = new WatcherLicensee(settings);
MonitoringLicensee monitoringLicensee = new MonitoringLicensee(settings);
GraphLicensee graphLicensee = new GraphLicensee(settings);
TestLicensesService licensesService = new TestLicensesService(settings,
Arrays.asList(securityLicensee, watcherLicensee, monitoringLicensee, graphLicensee));
return Arrays.asList(securityLicensee, licensesService, watcherLicensee, monitoringLicensee,
graphLicensee, securityLicenseState);
} }
public InternalLicensing() { public InternalLicensing() {
@ -266,11 +283,6 @@ public class LicensingTests extends SecurityIntegTestCase {
public List<Class<? extends RestHandler>> getRestHandlers() { public List<Class<? extends RestHandler>> getRestHandlers() {
return emptyList(); return emptyList();
} }
@Override
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
return Collections.emptyList();
}
} }
public static class InternalXPackPlugin extends XPackPlugin { public static class InternalXPackPlugin extends XPackPlugin {
@ -281,27 +293,13 @@ public class LicensingTests extends SecurityIntegTestCase {
} }
} }
public static class InternalLicenseModule extends AbstractModule { public static class TestLicensesService extends LicensesService {
@Override
protected void configure() {
bind(InternalLicenseeRegistry.class).asEagerSingleton();
bind(LicenseeRegistry.class).to(InternalLicenseeRegistry.class);
}
}
public static class InternalLicenseeRegistry extends AbstractComponent implements LicenseeRegistry { private final List<Licensee> licensees;
private final List<Licensee> licensees = new ArrayList<>(); public TestLicensesService(Settings settings, List<Licensee> licensees) {
super(settings, null, null, Collections.emptyList());
@Inject this.licensees = licensees;
public InternalLicenseeRegistry(Settings settings) {
super(settings);
enable(OperationMode.BASIC);
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable(OperationMode.BASIC); enable(OperationMode.BASIC);
} }
@ -316,5 +314,11 @@ public class LicensingTests extends SecurityIntegTestCase {
licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED)); licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
} }
} }
@Override
protected void doStart() {}
@Override
protected void doStop() {}
} }
} }

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License.OperationMode; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase;
import org.elasticsearch.license.plugin.core.Licensee.Status; import org.elasticsearch.license.plugin.core.Licensee.Status;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -23,39 +22,18 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/ */
public class SecurityLicenseeTests extends AbstractLicenseeTestCase { public class SecurityLicenseeTests extends AbstractLicenseeTestCase {
private final SecurityLicenseState securityLicenseState = mock(SecurityLicenseState.class); private final SecurityLicenseState securityLicenseState = mock(SecurityLicenseState.class);
private final LicenseeRegistry registry = mock(LicenseeRegistry.class);
public void testStartsWithoutTribeNode() {
SecurityLicensee licensee = new SecurityLicensee(Settings.EMPTY, registry, securityLicenseState);
// starting the Licensee start trigger it being registered
licensee.start();
verify(registry).register(licensee);
verifyNoMoreInteractions(registry, securityLicenseState);
}
public void testDoesNotStartWithTribeNode() {
Settings settings = Settings.builder().put("tribe.fake.cluster.name", "notchecked").build();
SecurityLicensee licensee = new SecurityLicensee(settings, registry, securityLicenseState);
// starting the Licensee as a tribe node should not trigger it being registered
licensee.start();
verifyNoMoreInteractions(registry, securityLicenseState);
}
public void testOnChangeModifiesSecurityLicenseState() { public void testOnChangeModifiesSecurityLicenseState() {
Status status = mock(Status.class); Status status = mock(Status.class);
SecurityLicensee licensee = new SecurityLicensee(Settings.EMPTY, registry, securityLicenseState); SecurityLicensee licensee = new SecurityLicensee(Settings.EMPTY, securityLicenseState);
licensee.onChange(status); licensee.onChange(status);
assertSame(status, licensee.getStatus()); assertSame(status, licensee.getStatus());
verify(securityLicenseState).updateStatus(status); verify(securityLicenseState).updateStatus(status);
verifyNoMoreInteractions(registry, securityLicenseState); verifyNoMoreInteractions(securityLicenseState);
} }
public void testAcknowledgementMessagesFromBasicToAnyNotGoldOrStandardIsNoOp() { public void testAcknowledgementMessagesFromBasicToAnyNotGoldOrStandardIsNoOp() {
@ -97,6 +75,6 @@ public class SecurityLicenseeTests extends AbstractLicenseeTestCase {
} }
private SecurityLicensee buildLicensee() { private SecurityLicensee buildLicensee() {
return new SecurityLicensee(Settings.EMPTY, registry, securityLicenseState); return new SecurityLicensee(Settings.EMPTY, securityLicenseState);
} }
} }

View File

@ -33,6 +33,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexModule;
import org.elasticsearch.license.plugin.Licensing; import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin; import org.elasticsearch.plugins.ScriptPlugin;
@ -156,7 +157,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
ArrayList<Module> modules = new ArrayList<>(); ArrayList<Module> modules = new ArrayList<>();
modules.add(b -> b.bind(Clock.class).toInstance(getClock())); modules.add(b -> b.bind(Clock.class).toInstance(getClock()));
modules.addAll(notification.nodeModules()); modules.addAll(notification.nodeModules());
modules.addAll(licensing.nodeModules());
modules.addAll(security.nodeModules()); modules.addAll(security.nodeModules());
modules.addAll(watcher.nodeModules()); modules.addAll(watcher.nodeModules());
modules.addAll(monitoring.nodeModules()); modules.addAll(monitoring.nodeModules());
@ -164,6 +164,8 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
if (transportClientMode == false) { if (transportClientMode == false) {
modules.add(new TextTemplateModule()); modules.add(new TextTemplateModule());
// Note: this only exists so LicensesService subclasses can be bound in mock tests
modules.addAll(licensing.nodeModules());
} }
return modules; return modules;
} }
@ -172,30 +174,27 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() { public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
ArrayList<Class<? extends LifecycleComponent>> services = new ArrayList<>(); ArrayList<Class<? extends LifecycleComponent>> services = new ArrayList<>();
services.addAll(notification.nodeServices()); services.addAll(notification.nodeServices());
services.addAll(licensing.nodeServices());
services.addAll(security.nodeServices()); services.addAll(security.nodeServices());
services.addAll(watcher.nodeServices());
services.addAll(monitoring.nodeServices()); services.addAll(monitoring.nodeServices());
services.addAll(graph.getGuiceServiceClasses());
return services; return services;
} }
@Override @Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService) { ResourceWatcherService resourceWatcherService) {
List<Object> components = new ArrayList<>(); List<Object> components = new ArrayList<>();
if (transportClientMode == false) { final InternalClient internalClient = new InternalClient(settings, threadPool, client, security.getCryptoService());
final InternalClient internalClient = new InternalClient(settings, threadPool, client, security.getCryptoService()); components.add(internalClient);
components.add(internalClient);
// watcher http stuff components.addAll(licensing.createComponents(clusterService, getClock(), security.getSecurityLicenseState()));
Map<String, HttpAuthFactory> httpAuthFactories = new HashMap<>();
httpAuthFactories.put(BasicAuth.TYPE, new BasicAuthFactory(security.getCryptoService())); // watcher http stuff
// TODO: add more auth types, or remove this indirection Map<String, HttpAuthFactory> httpAuthFactories = new HashMap<>();
HttpAuthRegistry httpAuthRegistry = new HttpAuthRegistry(httpAuthFactories); httpAuthFactories.put(BasicAuth.TYPE, new BasicAuthFactory(security.getCryptoService()));
components.add(new HttpRequestTemplate.Parser(httpAuthRegistry)); // TODO: add more auth types, or remove this indirection
components.add(new HttpClient(settings, httpAuthRegistry, env)); HttpAuthRegistry httpAuthRegistry = new HttpAuthRegistry(httpAuthFactories);
} components.add(new HttpRequestTemplate.Parser(httpAuthRegistry));
components.add(new HttpClient(settings, httpAuthRegistry, env));
return components; return components;
} }
@ -205,7 +204,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
Settings.Builder builder = Settings.builder(); Settings.Builder builder = Settings.builder();
builder.put(security.additionalSettings()); builder.put(security.additionalSettings());
builder.put(watcher.additionalSettings()); builder.put(watcher.additionalSettings());
builder.put(graph.additionalSettings());
return builder.build(); return builder.build();
} }
@ -240,7 +238,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
filters.addAll(notification.getSettingsFilter()); filters.addAll(notification.getSettingsFilter());
filters.addAll(security.getSettingsFilter()); filters.addAll(security.getSettingsFilter());
filters.addAll(MonitoringSettings.getSettingsFilter()); filters.addAll(MonitoringSettings.getSettingsFilter());
filters.addAll(graph.getSettingsFilter());
return filters; return filters;
} }
@ -273,7 +270,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
filters.addAll(monitoring.getActionFilters()); filters.addAll(monitoring.getActionFilters());
filters.addAll(security.getActionFilters()); filters.addAll(security.getActionFilters());
filters.addAll(watcher.getActionFilters()); filters.addAll(watcher.getActionFilters());
filters.addAll(graph.getActionFilters());
return filters; return filters;
} }
@ -298,7 +294,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
public void onIndexModule(IndexModule module) { public void onIndexModule(IndexModule module) {
security.onIndexModule(module); security.onIndexModule(module);
graph.onIndexModule(module);
} }
public static void bindFeatureSet(Binder binder, Class<? extends XPackFeatureSet> featureSet) { public static void bindFeatureSet(Binder binder, Class<? extends XPackFeatureSet> featureSet) {

View File

@ -126,13 +126,6 @@ public class Watcher implements ActionPlugin {
return modules; return modules;
} }
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
if (enabled == false|| transportClient) {
return Collections.emptyList();
}
return Collections.singletonList(WatcherLicensee.class);
}
public Settings additionalSettings() { public Settings additionalSettings() {
return Settings.EMPTY; return Settings.EMPTY;
} }

View File

@ -6,21 +6,18 @@
package org.elasticsearch.xpack.watcher; package org.elasticsearch.xpack.watcher;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee> { public class WatcherLicensee extends AbstractLicenseeComponent {
public static final String ID = Watcher.NAME; public static final String ID = Watcher.NAME;
@Inject public WatcherLicensee(Settings settings) {
public WatcherLicensee(Settings settings, LicenseeRegistry clientService) { super(settings, ID);
super(settings, ID, clientService);
} }
@Override @Override

View File

@ -25,16 +25,13 @@ public class WatcherModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
if (transportClientMode) { if (transportClientMode) {
bind(WatcherLicensee.class).toProvider(Providers.of(null));
return; return;
} }
if (enabled == false) { if (enabled == false) {
bind(WatcherLicensee.class).toProvider(Providers.of(null));
// watcher service must be null, so that the watcher feature set can be instantiated even if watcher is not enabled // watcher service must be null, so that the watcher feature set can be instantiated even if watcher is not enabled
bind(WatcherService.class).toProvider(Providers.of(null)); bind(WatcherService.class).toProvider(Providers.of(null));
} else { } else {
bind(WatcherLicensee.class).asEagerSingleton();
bind(WatcherLifeCycleService.class).asEagerSingleton(); bind(WatcherLifeCycleService.class).asEagerSingleton();
bind(WatcherIndexTemplateRegistry.class).asEagerSingleton(); bind(WatcherIndexTemplateRegistry.class).asEagerSingleton();
} }

View File

@ -8,6 +8,8 @@ package org.elasticsearch.xpack.watcher.license;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase;
import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.xpack.watcher.WatcherLicensee; import org.elasticsearch.xpack.watcher.WatcherLicensee;
import static org.elasticsearch.license.core.License.OperationMode.BASIC; import static org.elasticsearch.license.core.License.OperationMode.BASIC;
@ -19,7 +21,6 @@ import static org.hamcrest.Matchers.is;
public class LicenseTests extends AbstractLicenseeTestCase { public class LicenseTests extends AbstractLicenseeTestCase {
private final SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry();
private WatcherLicensee watcherLicensee; private WatcherLicensee watcherLicensee;
public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception { public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception {
@ -34,14 +35,14 @@ public class LicenseTests extends AbstractLicenseeTestCase {
public void testNoLicenseDisablesWatcher() { public void testNoLicenseDisablesWatcher() {
initLicense(BASIC, STANDARD); initLicense(BASIC, STANDARD);
licenseeRegistry.disable(); disable(watcherLicensee);
assertWatcherActionsNotAllowed(watcherLicensee); assertWatcherActionsNotAllowed(watcherLicensee);
} }
public void testExpiredPlatinumGoldTrialLicenseDisablesWatcher() throws Exception { public void testExpiredPlatinumGoldTrialLicenseDisablesWatcher() throws Exception {
initLicense(TRIAL, GOLD, PLATINUM); initLicense(TRIAL, GOLD, PLATINUM);
licenseeRegistry.disable(); disable(watcherLicensee);
assertWatcherActionsNotAllowed(watcherLicensee); assertWatcherActionsNotAllowed(watcherLicensee);
} }
@ -50,7 +51,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
initLicense(BASIC, STANDARD); initLicense(BASIC, STANDARD);
assertWatcherActionsNotAllowed(watcherLicensee); assertWatcherActionsNotAllowed(watcherLicensee);
licenseeRegistry.setOperationMode(randomFrom(TRIAL, GOLD, PLATINUM)); setOperationMode(watcherLicensee, randomFrom(TRIAL, GOLD, PLATINUM));
assertWatcherActionsAllowed(watcherLicensee); assertWatcherActionsAllowed(watcherLicensee);
} }
@ -58,17 +59,17 @@ public class LicenseTests extends AbstractLicenseeTestCase {
initLicense(TRIAL, GOLD, PLATINUM); initLicense(TRIAL, GOLD, PLATINUM);
assertWatcherActionsAllowed(watcherLicensee); assertWatcherActionsAllowed(watcherLicensee);
licenseeRegistry.setOperationMode(randomFrom(BASIC, STANDARD)); setOperationMode(watcherLicensee, randomFrom(BASIC, STANDARD));
assertWatcherActionsNotAllowed(watcherLicensee); assertWatcherActionsNotAllowed(watcherLicensee);
} }
public void testUpgradingExpiredLicenseWorks() { public void testUpgradingExpiredLicenseWorks() {
initLicense(TRIAL, GOLD, PLATINUM); initLicense(TRIAL, GOLD, PLATINUM);
licenseeRegistry.disable(); disable(watcherLicensee);
assertWatcherActionsNotAllowed(watcherLicensee); assertWatcherActionsNotAllowed(watcherLicensee);
licenseeRegistry.setOperationMode(randomFrom(TRIAL, GOLD, PLATINUM)); setOperationMode(watcherLicensee, randomFrom(TRIAL, GOLD, PLATINUM));
assertWatcherActionsAllowed(watcherLicensee); assertWatcherActionsAllowed(watcherLicensee);
} }
@ -87,8 +88,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
private void initLicense(License.OperationMode ... allowedLicenses) { private void initLicense(License.OperationMode ... allowedLicenses) {
licenseeRegistry.setOperationMode(randomFrom(allowedLicenses)); watcherLicensee = new WatcherLicensee(Settings.EMPTY);
watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); setOperationMode(watcherLicensee, randomFrom(allowedLicenses));
licenseeRegistry.register(watcherLicensee);
} }
} }