Skip to content

Convert remaining license methods to isAllowed #55908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {

private static <T> ContextParser<String, T> checkLicense(ContextParser<String, T> realParser) {
return (parser, name) -> {
if (getLicenseState().isAnalyticsAllowed() == false) {
if (getLicenseState().isAllowed(XPackLicenseState.Feature.ANALYTICS) == false) {
throw LicenseUtils.newComplianceException(XPackField.ANALYTICS);
}
return realParser.parse(parser, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String name() {

@Override
public boolean available() {
return licenseState.isAnalyticsAllowed();
return licenseState.isAllowed(XPackLicenseState.Feature.ANALYTICS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AnalyticsUsageTransportAction(TransportService transportService, ClusterS
@Override
protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state,
ActionListener<XPackUsageFeatureResponse> listener) {
boolean available = licenseState.isAnalyticsAllowed();
boolean available = licenseState.isAllowed(XPackLicenseState.Feature.ANALYTICS);
if (available) {
AnalyticsStatsAction.Request statsRequest = new AnalyticsStatsAction.Request();
statsRequest.setParentTask(clusterService.localNode().getId(), task.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testAvailable() throws Exception {
AnalyticsInfoTransportAction featureSet = new AnalyticsInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), licenseState);
boolean available = randomBoolean();
when(licenseState.isAnalyticsAllowed()).thenReturn(available);
when(licenseState.isAllowed(XPackLicenseState.Feature.ANALYTICS)).thenReturn(available);
assertThat(featureSet.available(), is(available));
Client client = mockClient();
AnalyticsUsageTransportAction usageAction = new AnalyticsUsageTransportAction(mock(TransportService.class), clusterService, null,
Expand All @@ -89,7 +89,7 @@ public void testEnabled() throws Exception {
assertThat(featureSet.enabled(), is(true));
assertTrue(featureSet.enabled());
boolean available = randomBoolean();
when(licenseState.isAnalyticsAllowed()).thenReturn(available);
when(licenseState.isAllowed(XPackLicenseState.Feature.ANALYTICS)).thenReturn(available);
Client client = mockClient();
AnalyticsUsageTransportAction usageAction = new AnalyticsUsageTransportAction(mock(TransportService.class),
clusterService, null, mock(ActionFilters.class), null, licenseState, client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public class CcrLicenseChecker {
* Constructs a CCR license checker with the default rule based on the license state for checking if CCR is allowed.
*/
CcrLicenseChecker() {
this(XPackPlugin.getSharedLicenseState()::isCcrAllowed, XPackPlugin.getSharedLicenseState()::isSecurityEnabled);
this(() -> XPackPlugin.getSharedLicenseState().isAllowed(XPackLicenseState.Feature.CCR),
XPackPlugin.getSharedLicenseState()::isSecurityEnabled);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void testAvailable() {
CCRInfoTransportAction featureSet = new CCRInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState);

when(licenseState.isCcrAllowed()).thenReturn(false);
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(false);
assertThat(featureSet.available(), equalTo(false));

when(licenseState.isCcrAllowed()).thenReturn(true);
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(true);
assertThat(featureSet.available(), equalTo(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() {
whenLocalNodeElectedMaster(isElectedMaster);

// this controls the blockage
when(licenseState.isMonitoringAllowed()).thenReturn(false);
when(licenseState.isCcrAllowed()).thenReturn(ccrAllowed);
when(licenseState.isAllowed(XPackLicenseState.Feature.MONITORING)).thenReturn(false);
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(ccrAllowed);

final StatsCollector collector = createCollector(settings, clusterService, licenseState, client);

assertThat(collector.shouldCollect(isElectedMaster), is(false));
if (isElectedMaster) {
verify(licenseState).isMonitoringAllowed();
verify(licenseState).isAllowed(XPackLicenseState.Feature.MONITORING);
}
}

public void testShouldCollectReturnsFalseIfNotMaster() {
// regardless of CCR being enabled
final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings());

when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean());
when(licenseState.isCcrAllowed()).thenReturn(randomBoolean());
when(licenseState.isAllowed(XPackLicenseState.Feature.MONITORING)).thenReturn(randomBoolean());
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(randomBoolean());
// this controls the blockage
final boolean isElectedMaster = false;

Expand All @@ -75,8 +75,8 @@ public void testShouldCollectReturnsFalseIfCCRIsDisabled() {
// this is controls the blockage
final Settings settings = ccrDisabledSettings();

when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean());
when(licenseState.isCcrAllowed()).thenReturn(randomBoolean());
when(licenseState.isAllowed(XPackLicenseState.Feature.MONITORING)).thenReturn(randomBoolean());
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(randomBoolean());

final boolean isElectedMaster = randomBoolean();
whenLocalNodeElectedMaster(isElectedMaster);
Expand All @@ -86,16 +86,16 @@ public void testShouldCollectReturnsFalseIfCCRIsDisabled() {
assertThat(collector.shouldCollect(isElectedMaster), is(false));

if (isElectedMaster) {
verify(licenseState).isMonitoringAllowed();
verify(licenseState).isAllowed(XPackLicenseState.Feature.MONITORING);
}
}

public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() {
final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings());

when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean());
when(licenseState.isAllowed(XPackLicenseState.Feature.MONITORING)).thenReturn(randomBoolean());
// this is controls the blockage
when(licenseState.isCcrAllowed()).thenReturn(false);
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(false);
final boolean isElectedMaster = randomBoolean();
whenLocalNodeElectedMaster(isElectedMaster);

Expand All @@ -104,22 +104,22 @@ public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() {
assertThat(collector.shouldCollect(isElectedMaster), is(false));

if (isElectedMaster) {
verify(licenseState).isMonitoringAllowed();
verify(licenseState).isAllowed(XPackLicenseState.Feature.MONITORING);
}
}

public void testShouldCollectReturnsTrue() {
final Settings settings = ccrEnabledSettings();

when(licenseState.isMonitoringAllowed()).thenReturn(true);
when(licenseState.isCcrAllowed()).thenReturn(true);
when(licenseState.isAllowed(XPackLicenseState.Feature.MONITORING)).thenReturn(true);
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(true);
final boolean isElectedMaster = true;

final StatsCollector collector = createCollector(settings, clusterService, licenseState, client);

assertThat(collector.shouldCollect(isElectedMaster), is(true));

verify(licenseState).isMonitoringAllowed();
verify(licenseState).isAllowed(XPackLicenseState.Feature.MONITORING);
}

public void testDoCollect() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class XPackLicenseState {
* Each value defines the licensed state necessary for the feature to be allowed.
*/
public enum Feature {
SECURITY(OperationMode.BASIC, false),
SECURITY_IP_FILTERING(OperationMode.GOLD, false),
SECURITY_AUDITING(OperationMode.GOLD, false),
SECURITY_DLS_FLS(OperationMode.PLATINUM, false),
Expand All @@ -46,7 +47,49 @@ public enum Feature {
SECURITY_API_KEY_SERVICE(OperationMode.MISSING, false),
SECURITY_AUTHORIZATION_REALM(OperationMode.PLATINUM, true),
SECURITY_AUTHORIZATION_ENGINE(OperationMode.PLATINUM, true),
SPATIAL_GEO_CENTROID(OperationMode.GOLD, true);
SECURITY_STATS_AND_HEALTH(OperationMode.MISSING, true),

WATCHER(OperationMode.STANDARD, true),
MONITORING(OperationMode.MISSING, true),
// TODO: should just check WATCHER directly?
MONITORING_CLUSTER_ALERTS(OperationMode.STANDARD, true),
MONITORING_UPDATE_RETENTION(OperationMode.STANDARD, false),

CCR(OperationMode.PLATINUM, true),

GRAPH(OperationMode.PLATINUM, true),

MACHINE_LEARNING(OperationMode.PLATINUM, true),

TRANSFORM(OperationMode.MISSING, true),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the use of MISSING here - I think it will confuse future readers and those who are trying to add Basic licensed features, but I think the solution is to remove MISSING entirely in a separate PR. I don't think we need to retain it as an operation mode.


ROLLUP(OperationMode.MISSING, true),

VOTING_ONLY(OperationMode.MISSING, true),

LOGSTASH(OperationMode.STANDARD, true),

DEPRECATION(OperationMode.MISSING, true),

ILM(OperationMode.MISSING, true),

ENRICH(OperationMode.MISSING, true),

EQL(OperationMode.MISSING, true),

SQL(OperationMode.MISSING, true),

JDBC(OperationMode.PLATINUM, true),

ODBC(OperationMode.PLATINUM, true),

VECTORS(OperationMode.MISSING, true),

SPATIAL(OperationMode.MISSING, true),

SPATIAL_GEO_CENTROID(OperationMode.GOLD, true),

ANALYTICS(OperationMode.MISSING, true);

final OperationMode minimumOperationMode;
final boolean needsActive;
Expand Down Expand Up @@ -432,57 +475,10 @@ public boolean isAllowed(Feature feature) {
return isAllowedByLicense(feature.minimumOperationMode, feature.needsActive);
}

public boolean isStatsAndHealthAllowed() {
return allowForAllLicenses();
}

public boolean isWatcherAllowed() {
return isAllowedByLicense(OperationMode.STANDARD);
}

public boolean isMonitoringAllowed() {
return allowForAllLicenses();
}

/**
* Monitoring Cluster Alerts requires the equivalent license to use Watcher.
*
* @return {@link #isWatcherAllowed()}
* @see #isWatcherAllowed()
*/
public boolean isMonitoringClusterAlertsAllowed() {
return isWatcherAllowed();
}

/**
* Determine if the current license allows the retention of indices to be modified.
* <p>
* Only users with a non-{@link OperationMode#BASIC} license can update the retention period.
* <p>
* Note: This does not consider the <em>state</em> of the license so that any change is remembered for when they fix their license.
*
* @return {@code true} if the user is allowed to modify the retention. Otherwise {@code false}.
*/
public boolean isUpdateRetentionAllowed() {
return isAllowedByLicense(OperationMode.STANDARD, false);
}

public boolean isGraphAllowed() {
return isAllowedByLicense(OperationMode.PLATINUM);
}

public boolean isMachineLearningAllowed() {
return isAllowedByLicense(OperationMode.PLATINUM);
}

public static boolean isMachineLearningAllowedForOperationMode(final OperationMode operationMode) {
return isAllowedByOperationMode(operationMode, OperationMode.PLATINUM);
}

public boolean isTransformAllowed() {
return allowForAllLicenses();
}

public static boolean isTransformAllowedForOperationMode(final OperationMode operationMode) {
// any license (basic and upwards)
return operationMode != License.OperationMode.MISSING;
Expand All @@ -492,91 +488,6 @@ public static boolean isFipsAllowedForOperationMode(final OperationMode operatio
return isAllowedByOperationMode(operationMode, OperationMode.PLATINUM);
}

public boolean isRollupAllowed() {
return allowForAllLicenses();
}

public boolean isVotingOnlyAllowed() {
return allowForAllLicenses();
}

public boolean isLogstashAllowed() {
return isAllowedByLicense(OperationMode.STANDARD);
}

public boolean isBeatsAllowed() {
return isAllowedByLicense(OperationMode.STANDARD);
}

public boolean isDeprecationAllowed() {
return allowForAllLicenses();
}

public boolean isUpgradeAllowed() {
return allowForAllLicenses();
}

public boolean isIndexLifecycleAllowed() {
return allowForAllLicenses();
}

public boolean isEnrichAllowed() {
return allowForAllLicenses();
}

public boolean isEqlAllowed() {
return allowForAllLicenses();
}

public boolean isSqlAllowed() {
return allowForAllLicenses();
}

public boolean isJdbcAllowed() {
return isAllowedByLicense(OperationMode.PLATINUM);
}

public boolean isFlattenedAllowed() {
return allowForAllLicenses();
}

public boolean isVectorsAllowed() {
return allowForAllLicenses();
}


/**
* Determine if Wildcard support should be enabled.
* <p>
* Wildcard is available for all license types except {@link OperationMode#MISSING}
*/
public synchronized boolean isWildcardAllowed() {
return status.active;
}

public boolean isOdbcAllowed() {
return isAllowedByLicense(OperationMode.PLATINUM);
}

public boolean isSpatialAllowed() {
return allowForAllLicenses();
}

public boolean isAnalyticsAllowed() {
return allowForAllLicenses();
}

public boolean isConstantKeywordAllowed() {
return allowForAllLicenses();
}

/**
* @return true if security is available to be used with the current license type
*/
public boolean isSecurityAvailable() {
return checkAgainstStatus(status -> status.mode != OperationMode.MISSING);
}

/**
* Returns whether security is enabled, taking into account the default enabled state
* based on the current license level.
Expand Down Expand Up @@ -616,13 +527,6 @@ private static boolean isSecurityEnabled(final OperationMode mode, final boolean
}
}

/**
* Determine if cross-cluster replication is allowed
*/
public boolean isCcrAllowed() {
return isAllowedByLicense(OperationMode.PLATINUM);
}

public static boolean isCcrAllowedForOperationMode(final OperationMode operationMode) {
return isAllowedByOperationMode(operationMode, OperationMode.PLATINUM);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String name() {

@Override
public boolean available() {
return licenseState.isCcrAllowed();
return licenseState.isAllowed(XPackLicenseState.Feature.CCR);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat
lastFollowTimeInMillis = Math.max(0, Instant.now().toEpochMilli() - lastFollowerIndexCreationDate);
}

CCRInfoTransportAction.Usage usage = new CCRInfoTransportAction.Usage(licenseState.isCcrAllowed(),
CCRInfoTransportAction.Usage usage = new CCRInfoTransportAction.Usage(licenseState.isAllowed(XPackLicenseState.Feature.CCR),
XPackSettings.CCR_ENABLED_SETTING.get(settings), numberOfFollowerIndices, numberOfAutoFollowPatterns, lastFollowTimeInMillis);
listener.onResponse(new XPackUsageFeatureResponse(usage));
}
Expand Down
Loading