Skip to content

Commit 335cf91

Browse files
authored
Add enabled status for token and api key service (#38687) (#38882)
Right now there is no way to determine whether the token service or API key service is enabled or not. This commit adds support for the enabled status of token and API key service to the security feature set usage API `/_xpack/usage`. Closes #38535
1 parent 96e7d71 commit 335cf91

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public class SecurityFeatureSetUsage extends XPackFeatureSet.Usage {
2222
private static final String ROLES_XFIELD = "roles";
2323
private static final String ROLE_MAPPING_XFIELD = "role_mapping";
2424
private static final String SSL_XFIELD = "ssl";
25+
private static final String TOKEN_SERVICE_XFIELD = "token_service";
26+
private static final String API_KEY_SERVICE_XFIELD = "api_key_service";
2527
private static final String AUDIT_XFIELD = "audit";
2628
private static final String IP_FILTER_XFIELD = "ipfilter";
2729
private static final String ANONYMOUS_XFIELD = "anonymous";
2830

2931
private Map<String, Object> realmsUsage;
3032
private Map<String, Object> rolesStoreUsage;
3133
private Map<String, Object> sslUsage;
34+
private Map<String, Object> tokenServiceUsage;
35+
private Map<String, Object> apiKeyServiceUsage;
3236
private Map<String, Object> auditUsage;
3337
private Map<String, Object> ipFilterUsage;
3438
private Map<String, Object> anonymousUsage;
@@ -39,6 +43,10 @@ public SecurityFeatureSetUsage(StreamInput in) throws IOException {
3943
realmsUsage = in.readMap();
4044
rolesStoreUsage = in.readMap();
4145
sslUsage = in.readMap();
46+
if (in.getVersion().onOrAfter(Version.V_7_1_0)) {
47+
tokenServiceUsage = in.readMap();
48+
apiKeyServiceUsage = in.readMap();
49+
}
4250
auditUsage = in.readMap();
4351
ipFilterUsage = in.readMap();
4452
if (in.getVersion().before(Version.V_6_0_0_beta1)) {
@@ -52,12 +60,15 @@ public SecurityFeatureSetUsage(StreamInput in) throws IOException {
5260
public SecurityFeatureSetUsage(boolean available, boolean enabled, Map<String, Object> realmsUsage,
5361
Map<String, Object> rolesStoreUsage, Map<String, Object> roleMappingStoreUsage,
5462
Map<String, Object> sslUsage, Map<String, Object> auditUsage,
55-
Map<String, Object> ipFilterUsage, Map<String, Object> anonymousUsage) {
63+
Map<String, Object> ipFilterUsage, Map<String, Object> anonymousUsage,
64+
Map<String, Object> tokenServiceUsage, Map<String, Object> apiKeyServiceUsage) {
5665
super(XPackField.SECURITY, available, enabled);
5766
this.realmsUsage = realmsUsage;
5867
this.rolesStoreUsage = rolesStoreUsage;
5968
this.roleMappingStoreUsage = roleMappingStoreUsage;
6069
this.sslUsage = sslUsage;
70+
this.tokenServiceUsage = tokenServiceUsage;
71+
this.apiKeyServiceUsage = apiKeyServiceUsage;
6172
this.auditUsage = auditUsage;
6273
this.ipFilterUsage = ipFilterUsage;
6374
this.anonymousUsage = anonymousUsage;
@@ -69,6 +80,8 @@ public void writeTo(StreamOutput out) throws IOException {
6980
out.writeMap(realmsUsage);
7081
out.writeMap(rolesStoreUsage);
7182
out.writeMap(sslUsage);
83+
out.writeMap(tokenServiceUsage);
84+
out.writeMap(apiKeyServiceUsage);
7285
out.writeMap(auditUsage);
7386
out.writeMap(ipFilterUsage);
7487
if (out.getVersion().before(Version.V_6_0_0_beta1)) {
@@ -87,6 +100,8 @@ protected void innerXContent(XContentBuilder builder, Params params) throws IOEx
87100
builder.field(ROLES_XFIELD, rolesStoreUsage);
88101
builder.field(ROLE_MAPPING_XFIELD, roleMappingStoreUsage);
89102
builder.field(SSL_XFIELD, sslUsage);
103+
builder.field(TOKEN_SERVICE_XFIELD, tokenServiceUsage);
104+
builder.field(API_KEY_SERVICE_XFIELD, apiKeyServiceUsage);
90105
builder.field(AUDIT_XFIELD, auditUsage);
91106
builder.field(IP_FILTER_XFIELD, ipFilterUsage);
92107
builder.field(ANONYMOUS_XFIELD, anonymousUsage);
@@ -96,4 +111,5 @@ protected void innerXContent(XContentBuilder builder, Params params) throws IOEx
96111
public Map<String, Object> getRealmsUsage() {
97112
return Collections.unmodifiableMap(realmsUsage);
98113
}
114+
99115
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/SecurityFeatureSet.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import java.util.concurrent.atomic.AtomicReference;
3030

3131
import static java.util.Collections.singletonMap;
32+
import static org.elasticsearch.xpack.core.XPackSettings.API_KEY_SERVICE_ENABLED_SETTING;
3233
import static org.elasticsearch.xpack.core.XPackSettings.HTTP_SSL_ENABLED;
34+
import static org.elasticsearch.xpack.core.XPackSettings.TOKEN_SERVICE_ENABLED_SETTING;
3335
import static org.elasticsearch.xpack.core.XPackSettings.TRANSPORT_SSL_ENABLED;
3436

3537
/**
@@ -93,6 +95,8 @@ public Map<String, Object> nativeCodeInfo() {
9395
@Override
9496
public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
9597
Map<String, Object> sslUsage = sslUsage(settings);
98+
Map<String, Object> tokenServiceUsage = tokenServiceUsage(settings);
99+
Map<String, Object> apiKeyServiceUsage = apiKeyServiceUsage(settings);
96100
Map<String, Object> auditUsage = auditUsage(settings);
97101
Map<String, Object> ipFilterUsage = ipFilterUsage(ipFilter);
98102
Map<String, Object> anonymousUsage = singletonMap("enabled", AnonymousUser.isAnonymousEnabled(settings));
@@ -103,9 +107,9 @@ public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
103107
final CountDown countDown = new CountDown(3);
104108
final Runnable doCountDown = () -> {
105109
if (countDown.countDown()) {
106-
listener.onResponse(new SecurityFeatureSetUsage(available(), enabled(), realmsUsageRef.get(),
107-
rolesUsageRef.get(), roleMappingUsageRef.get(),
108-
sslUsage, auditUsage, ipFilterUsage, anonymousUsage));
110+
listener.onResponse(new SecurityFeatureSetUsage(available(), enabled(), realmsUsageRef.get(), rolesUsageRef.get(),
111+
roleMappingUsageRef.get(), sslUsage, auditUsage, ipFilterUsage, anonymousUsage, tokenServiceUsage,
112+
apiKeyServiceUsage));
109113
}
110114
};
111115

@@ -152,6 +156,14 @@ static Map<String, Object> sslUsage(Settings settings) {
152156
return map;
153157
}
154158

159+
static Map<String, Object> tokenServiceUsage(Settings settings) {
160+
return singletonMap("enabled", TOKEN_SERVICE_ENABLED_SETTING.get(settings));
161+
}
162+
163+
static Map<String, Object> apiKeyServiceUsage(Settings settings) {
164+
return singletonMap("enabled", API_KEY_SERVICE_ENABLED_SETTING.get(settings));
165+
}
166+
155167
static Map<String, Object> auditUsage(Settings settings) {
156168
Map<String, Object> map = new HashMap<>(2);
157169
map.put("enabled", XPackSettings.AUDIT_ENABLED.get(settings));

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityFeatureSetTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ public void testUsage() throws Exception {
9696
settings.put("xpack.security.http.ssl.enabled", httpSSLEnabled);
9797
final boolean transportSSLEnabled = randomBoolean();
9898
settings.put("xpack.security.transport.ssl.enabled", transportSSLEnabled);
99+
100+
boolean configureEnabledFlagForTokenService = randomBoolean();
101+
final boolean tokenServiceEnabled;
102+
if (configureEnabledFlagForTokenService) {
103+
tokenServiceEnabled = randomBoolean();
104+
settings.put("xpack.security.authc.token.enabled", tokenServiceEnabled);
105+
} else {
106+
tokenServiceEnabled = httpSSLEnabled;
107+
}
108+
boolean configureEnabledFlagForApiKeyService = randomBoolean();
109+
final boolean apiKeyServiceEnabled;
110+
if (configureEnabledFlagForApiKeyService) {
111+
apiKeyServiceEnabled = randomBoolean();
112+
settings.put("xpack.security.authc.api_key.enabled", apiKeyServiceEnabled);
113+
} else {
114+
apiKeyServiceEnabled = httpSSLEnabled;
115+
}
116+
99117
final boolean auditingEnabled = randomBoolean();
100118
settings.put(XPackSettings.AUDIT_ENABLED.getKey(), auditingEnabled);
101119
final boolean httpIpFilterEnabled = randomBoolean();
@@ -185,6 +203,12 @@ public void testUsage() throws Exception {
185203
assertThat(source.getValue("ssl.http.enabled"), is(httpSSLEnabled));
186204
assertThat(source.getValue("ssl.transport.enabled"), is(transportSSLEnabled));
187205

206+
// check Token service
207+
assertThat(source.getValue("token_service.enabled"), is(tokenServiceEnabled));
208+
209+
// check API Key service
210+
assertThat(source.getValue("api_key_service.enabled"), is(apiKeyServiceEnabled));
211+
188212
// auditing
189213
assertThat(source.getValue("audit.enabled"), is(auditingEnabled));
190214
if (auditingEnabled) {
@@ -218,6 +242,8 @@ public void testUsage() throws Exception {
218242
} else {
219243
assertThat(source.getValue("realms"), is(nullValue()));
220244
assertThat(source.getValue("ssl"), is(nullValue()));
245+
assertThat(source.getValue("token_service"), is(nullValue()));
246+
assertThat(source.getValue("api_key_service"), is(nullValue()));
221247
assertThat(source.getValue("audit"), is(nullValue()));
222248
assertThat(source.getValue("anonymous"), is(nullValue()));
223249
assertThat(source.getValue("ipfilter"), is(nullValue()));

0 commit comments

Comments
 (0)