Skip to content

Commit 3189ef4

Browse files
authored
[X-Pack] Beats centralized management: security role + licensing (#30520)
* Adding Beats x-pack plugin + index templates * Adding built-in roles for Beats central management * Fixing typo * Refactoring: extract common code into method * More refactoring for more code reuse * Use a single index for Beats management * Rename "fragment" to "block" * Adding configuration block type * Expand kibana_system role to include Beats management index privileges * Fixing syntax * Adding test * Adding asserting for reserved role * Fixing privileges * Updating template * Removing beats plugin * Fixing tests * Fixing role variable name * Fixing assertions * Switching to preferred syntax for boolean false checks * Making class final * Making variables final * Updating Basic license message to be more accurate
1 parent dcbb115 commit 3189ef4

File tree

7 files changed

+120
-19
lines changed

7 files changed

+120
-19
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java

+30-18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class XPackLicenseState {
5252
messages.put(XPackField.LOGSTASH, new String[] {
5353
"Logstash will continue to poll centrally-managed pipelines"
5454
});
55+
messages.put(XPackField.BEATS, new String[] {
56+
"Beats will continue to poll centrally-managed configuration"
57+
});
5558
messages.put(XPackField.DEPRECATION, new String[] {
5659
"Deprecation APIs are disabled"
5760
});
@@ -81,6 +84,7 @@ public class XPackLicenseState {
8184
messages.put(XPackField.GRAPH, XPackLicenseState::graphAcknowledgementMessages);
8285
messages.put(XPackField.MACHINE_LEARNING, XPackLicenseState::machineLearningAcknowledgementMessages);
8386
messages.put(XPackField.LOGSTASH, XPackLicenseState::logstashAcknowledgementMessages);
87+
messages.put(XPackField.BEATS, XPackLicenseState::beatsAcknowledgementMessages);
8488
messages.put(XPackField.SQL, XPackLicenseState::sqlAcknowledgementMessages);
8589
ACKNOWLEDGMENT_MESSAGES = Collections.unmodifiableMap(messages);
8690
}
@@ -205,12 +209,19 @@ private static String[] machineLearningAcknowledgementMessages(OperationMode cur
205209
private static String[] logstashAcknowledgementMessages(OperationMode currentMode, OperationMode newMode) {
206210
switch (newMode) {
207211
case BASIC:
208-
switch (currentMode) {
209-
case TRIAL:
210-
case STANDARD:
211-
case GOLD:
212-
case PLATINUM:
213-
return new String[] { "Logstash will no longer poll for centrally-managed pipelines" };
212+
if (isBasic(currentMode) == false) {
213+
return new String[] { "Logstash will no longer poll for centrally-managed pipelines" };
214+
}
215+
break;
216+
}
217+
return Strings.EMPTY_ARRAY;
218+
}
219+
220+
private static String[] beatsAcknowledgementMessages(OperationMode currentMode, OperationMode newMode) {
221+
switch (newMode) {
222+
case BASIC:
223+
if (isBasic(currentMode) == false) {
224+
return new String[] { "Beats will no longer be able to use centrally-managed configuration" };
214225
}
215226
break;
216227
}
@@ -232,6 +243,10 @@ private static String[] sqlAcknowledgementMessages(OperationMode currentMode, Op
232243
return Strings.EMPTY_ARRAY;
233244
}
234245

246+
private static boolean isBasic(OperationMode mode) {
247+
return mode == OperationMode.BASIC;
248+
}
249+
235250
/** A wrapper for the license mode and state, to allow atomically swapping. */
236251
private static class Status {
237252

@@ -500,20 +515,17 @@ public boolean isRollupAllowed() {
500515
*/
501516
public boolean isLogstashAllowed() {
502517
Status localStatus = status;
518+
return localStatus.active && (isBasic(localStatus.mode) == false);
519+
}
503520

504-
if (localStatus.active == false) {
505-
return false;
506-
}
521+
/**
522+
* Beats is allowed as long as there is an active license of type TRIAL, STANDARD, GOLD or PLATINUM
523+
* @return {@code true} as long as there is a valid license
524+
*/
525+
public boolean isBeatsAllowed() {
526+
Status localStatus = status;
527+
return localStatus.active && (isBasic(localStatus.mode) == false);
507528

508-
switch (localStatus.mode) {
509-
case TRIAL:
510-
case GOLD:
511-
case PLATINUM:
512-
case STANDARD:
513-
return true;
514-
default:
515-
return false;
516-
}
517529
}
518530

519531
/**

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.xpack.core.graph.GraphFeatureSetUsage;
4343
import org.elasticsearch.xpack.core.graph.action.GraphExploreAction;
4444
import org.elasticsearch.xpack.core.logstash.LogstashFeatureSetUsage;
45+
import org.elasticsearch.xpack.core.beats.BeatsFeatureSetUsage;
4546
import org.elasticsearch.xpack.core.ml.MachineLearningFeatureSetUsage;
4647
import org.elasticsearch.xpack.core.ml.MlMetadata;
4748
import org.elasticsearch.xpack.core.ml.action.CloseJobAction;
@@ -320,6 +321,8 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
320321
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.GRAPH, GraphFeatureSetUsage::new),
321322
// logstash
322323
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.LOGSTASH, LogstashFeatureSetUsage::new),
324+
// beats
325+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.BEATS, BeatsFeatureSetUsage::new),
323326
// ML - Custom metadata
324327
new NamedWriteableRegistry.Entry(MetaData.Custom.class, "ml", MlMetadata::new),
325328
new NamedWriteableRegistry.Entry(NamedDiff.class, "ml", MlMetadata.MlMetadataDiff::new),

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackField.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public final class XPackField {
1919
public static final String MACHINE_LEARNING = "ml";
2020
/** Name constant for the Logstash feature. */
2121
public static final String LOGSTASH = "logstash";
22+
/** Name constant for the Beats feature. */
23+
public static final String BEATS = "beats";
2224
/** Name constant for the Deprecation API feature. */
2325
public static final String DEPRECATION = "deprecation";
2426
/** Name constant for the upgrade feature. */

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private XPackSettings() {
6767
public static final Setting<Boolean> LOGSTASH_ENABLED = Setting.boolSetting("xpack.logstash.enabled", true,
6868
Setting.Property.NodeScope);
6969

70+
/** Setting for enabling or disabling Beats extensions. Defaults to true. */
71+
public static final Setting<Boolean> BEATS_ENABLED = Setting.boolSetting("xpack.beats.enabled", true,
72+
Setting.Property.NodeScope);
73+
7074
/** Setting for enabling or disabling TLS. Defaults to false. */
7175
public static final Setting<Boolean> TRANSPORT_SSL_ENABLED = Setting.boolSetting("xpack.security.transport.ssl.enabled", false,
7276
Property.NodeScope);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.core.beats;
7+
8+
import org.elasticsearch.common.io.stream.StreamInput;
9+
import org.elasticsearch.xpack.core.XPackFeatureSet;
10+
import org.elasticsearch.xpack.core.XPackField;
11+
12+
import java.io.IOException;
13+
14+
public final class BeatsFeatureSetUsage extends XPackFeatureSet.Usage {
15+
16+
public BeatsFeatureSetUsage(StreamInput in) throws IOException {
17+
super(in);
18+
}
19+
20+
public BeatsFeatureSetUsage(boolean available, boolean enabled) {
21+
super(XPackField.BEATS, available, enabled);
22+
}
23+
24+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStore.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ private static Map<String, RoleDescriptor> initializeReservedRoles() {
8080
new RoleDescriptor.IndicesPrivileges[] {
8181
RoleDescriptor.IndicesPrivileges.builder().indices(".kibana*", ".reporting-*").privileges("all").build(),
8282
RoleDescriptor.IndicesPrivileges.builder()
83-
.indices(".monitoring-*").privileges("read", "read_cross_cluster").build()
83+
.indices(".monitoring-*").privileges("read", "read_cross_cluster").build(),
84+
RoleDescriptor.IndicesPrivileges.builder()
85+
.indices(".management-beats").privileges("create_index", "read", "write").build()
8486
},
8587
null, MetadataUtils.DEFAULT_RESERVED_METADATA))
8688
.put("logstash_system", new RoleDescriptor("logstash_system", new String[] { "monitor", MonitoringBulkAction.NAME},
8789
null, null, MetadataUtils.DEFAULT_RESERVED_METADATA))
90+
.put("beats_admin", new RoleDescriptor("beats_admin",
91+
null,
92+
new RoleDescriptor.IndicesPrivileges[] {
93+
RoleDescriptor.IndicesPrivileges.builder().indices(".management-beats").privileges("all").build()
94+
},
95+
null, MetadataUtils.DEFAULT_RESERVED_METADATA))
8896
.put(UsernamesField.BEATS_ROLE, new RoleDescriptor(UsernamesField.BEATS_ROLE,
8997
new String[] { "monitor", MonitoringBulkAction.NAME}, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA))
9098
.put("machine_learning_user", new RoleDescriptor("machine_learning_user", new String[] { "monitor_ml" },

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStoreTests.java

+48
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void testIsReserved() {
132132
assertThat(ReservedRolesStore.isReserved("watcher_user"), is(true));
133133
assertThat(ReservedRolesStore.isReserved("watcher_admin"), is(true));
134134
assertThat(ReservedRolesStore.isReserved("kibana_dashboard_only_user"), is(true));
135+
assertThat(ReservedRolesStore.isReserved("beats_admin"), is(true));
135136
assertThat(ReservedRolesStore.isReserved(XPackUser.ROLE_NAME), is(true));
136137
assertThat(ReservedRolesStore.isReserved(LogstashSystemUser.ROLE_NAME), is(true));
137138
assertThat(ReservedRolesStore.isReserved(BeatsSystemUser.ROLE_NAME), is(true));
@@ -220,6 +221,20 @@ public void testKibanaSystemRole() {
220221
assertThat(kibanaRole.indices().allowedIndicesMatcher(GetAction.NAME).test(index), is(true));
221222
assertThat(kibanaRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(index), is(true));
222223
});
224+
225+
// Beats management index
226+
final String index = ".management-beats";
227+
assertThat(kibanaRole.indices().allowedIndicesMatcher("indices:foo").test(index), is(false));
228+
assertThat(kibanaRole.indices().allowedIndicesMatcher("indices:bar").test(index), is(false));
229+
assertThat(kibanaRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(index), is(false));
230+
assertThat(kibanaRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(index), is(true));
231+
assertThat(kibanaRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(index), is(true));
232+
assertThat(kibanaRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(index), is(true));
233+
assertThat(kibanaRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(index), is(false));
234+
assertThat(kibanaRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(index), is(true));
235+
assertThat(kibanaRole.indices().allowedIndicesMatcher(MultiSearchAction.NAME).test(index), is(true));
236+
assertThat(kibanaRole.indices().allowedIndicesMatcher(GetAction.NAME).test(index), is(true));
237+
assertThat(kibanaRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(index), is(false));
223238
}
224239

225240
public void testKibanaUserRole() {
@@ -478,6 +493,39 @@ public void testLogstashSystemRole() {
478493
is(false));
479494
}
480495

496+
public void testBeatsAdminRole() {
497+
final RoleDescriptor roleDescriptor = new ReservedRolesStore().roleDescriptor("beats_admin");
498+
assertNotNull(roleDescriptor);
499+
assertThat(roleDescriptor.getMetadata(), hasEntry("_reserved", true));
500+
501+
final Role beatsAdminRole = Role.builder(roleDescriptor, null).build();
502+
assertThat(beatsAdminRole.cluster().check(ClusterHealthAction.NAME), is(false));
503+
assertThat(beatsAdminRole.cluster().check(ClusterStateAction.NAME), is(false));
504+
assertThat(beatsAdminRole.cluster().check(ClusterStatsAction.NAME), is(false));
505+
assertThat(beatsAdminRole.cluster().check(PutIndexTemplateAction.NAME), is(false));
506+
assertThat(beatsAdminRole.cluster().check(ClusterRerouteAction.NAME), is(false));
507+
assertThat(beatsAdminRole.cluster().check(ClusterUpdateSettingsAction.NAME), is(false));
508+
assertThat(beatsAdminRole.cluster().check(MonitoringBulkAction.NAME), is(false));
509+
510+
assertThat(beatsAdminRole.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(false));
511+
512+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher("indices:foo").test(randomAlphaOfLengthBetween(8, 24)),
513+
is(false));
514+
515+
final String index = ".management-beats";
516+
logger.info("index name [{}]", index);
517+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher("indices:foo").test(index), is(true));
518+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher("indices:bar").test(index), is(true));
519+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(index), is(true));
520+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(index), is(true));
521+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(index), is(true));
522+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(index), is(true));
523+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(index), is(true));
524+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(index), is(true));
525+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(MultiSearchAction.NAME).test(index), is(true));
526+
assertThat(beatsAdminRole.indices().allowedIndicesMatcher(GetAction.NAME).test(index), is(true));
527+
}
528+
481529
public void testBeatsSystemRole() {
482530
RoleDescriptor roleDescriptor = new ReservedRolesStore().roleDescriptor(BeatsSystemUser.ROLE_NAME);
483531
assertNotNull(roleDescriptor);

0 commit comments

Comments
 (0)