Skip to content

Commit ca99025

Browse files
committed
Add role stats to _security/stats
1 parent abca68e commit ca99025

File tree

7 files changed

+119
-6
lines changed

7 files changed

+119
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9169000
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
security_stats_endpoint,9168000
1+
roles_security_stats,9169000

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/stats/GetSecurityStatsNodeResponse.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,74 @@
77

88
package org.elasticsearch.xpack.core.security.action.stats;
99

10+
import org.elasticsearch.TransportVersion;
1011
import org.elasticsearch.action.support.nodes.BaseNodeResponse;
1112
import org.elasticsearch.cluster.node.DiscoveryNode;
1213
import org.elasticsearch.common.io.stream.StreamInput;
1314
import org.elasticsearch.common.io.stream.StreamOutput;
15+
import org.elasticsearch.core.Nullable;
1416
import org.elasticsearch.xcontent.ToXContentObject;
1517
import org.elasticsearch.xcontent.XContentBuilder;
1618

1719
import java.io.IOException;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
import java.util.Objects;
1823

1924
public class GetSecurityStatsNodeResponse extends BaseNodeResponse implements ToXContentObject {
2025

26+
private static final TransportVersion ROLES_SECURITY_STATS = TransportVersion.fromName("roles_security_stats");
27+
28+
@Nullable
29+
private final Map<String, Object> rolesStoreStats;
30+
2131
public GetSecurityStatsNodeResponse(final StreamInput in) throws IOException {
2232
super(in);
33+
this.rolesStoreStats = in.getTransportVersion().supports(ROLES_SECURITY_STATS) ? in.readGenericMap() : null;
2334
}
2435

25-
public GetSecurityStatsNodeResponse(final DiscoveryNode node) {
36+
public GetSecurityStatsNodeResponse(final DiscoveryNode node, @Nullable final Map<String, Object> rolesStoreStats) {
2637
super(node);
38+
this.rolesStoreStats = rolesStoreStats;
2739
}
2840

2941
@Override
3042
public void writeTo(final StreamOutput out) throws IOException {
3143
super.writeTo(out);
44+
if (out.getTransportVersion().supports(ROLES_SECURITY_STATS)) {
45+
out.writeGenericMap(rolesStoreStats);
46+
}
3247
}
3348

3449
@Override
3550
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
51+
if (rolesStoreStats != null) {
52+
builder.field("roles", rolesStoreStats);
53+
}
3654
return builder;
3755
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
return this == o
60+
|| (o instanceof GetSecurityStatsNodeResponse that
61+
&& Objects.equals(getNode(), that.getNode())
62+
&& Objects.equals(rolesStoreStats, that.rolesStoreStats));
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(getNode(), rolesStoreStats);
68+
}
69+
70+
// for testing
71+
@Nullable
72+
Map<String, Object> getRolesStoreStats() {
73+
return rolesStoreStats == null ? null : Collections.unmodifiableMap(rolesStoreStats);
74+
}
75+
76+
// for testing
77+
DiscoveryNode getDiscoveryNode() {
78+
return getNode();
79+
}
3880
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.core.security.action.stats;
9+
10+
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
11+
import org.elasticsearch.common.io.stream.Writeable;
12+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
13+
14+
import java.io.IOException;
15+
import java.util.Map;
16+
17+
public class GetSecurityStatsNodeResponseTests extends AbstractWireSerializingTestCase<GetSecurityStatsNodeResponse> {
18+
19+
@Override
20+
protected Writeable.Reader<GetSecurityStatsNodeResponse> instanceReader() {
21+
return GetSecurityStatsNodeResponse::new;
22+
}
23+
24+
@Override
25+
protected GetSecurityStatsNodeResponse createTestInstance() {
26+
return new GetSecurityStatsNodeResponse(
27+
DiscoveryNodeUtils.create(randomUUID()),
28+
randomBoolean() ? null : Map.of("key", randomUUID())
29+
);
30+
}
31+
32+
@Override
33+
protected GetSecurityStatsNodeResponse mutateInstance(GetSecurityStatsNodeResponse instance) throws IOException {
34+
return switch (randomIntBetween(0, 1)) {
35+
case 0 -> new GetSecurityStatsNodeResponse(DiscoveryNodeUtils.create(randomUUID()), instance.getRolesStoreStats());
36+
case 1 -> new GetSecurityStatsNodeResponse(instance.getDiscoveryNode(), Map.of("key", randomUUID()));
37+
default -> throw new IllegalStateException("Unexpected value");
38+
};
39+
}
40+
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/stats/TransportSecurityStatsAction.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
*/
77
package org.elasticsearch.xpack.security.action.stats;
88

9+
import org.elasticsearch.action.ActionListener;
910
import org.elasticsearch.action.FailedNodeException;
1011
import org.elasticsearch.action.support.ActionFilters;
1112
import org.elasticsearch.action.support.nodes.TransportNodesAction;
1213
import org.elasticsearch.cluster.node.DiscoveryNode;
1314
import org.elasticsearch.cluster.service.ClusterService;
1415
import org.elasticsearch.common.io.stream.StreamInput;
16+
import org.elasticsearch.core.Nullable;
1517
import org.elasticsearch.injection.guice.Inject;
1618
import org.elasticsearch.tasks.Task;
1719
import org.elasticsearch.threadpool.ThreadPool;
@@ -21,9 +23,12 @@
2123
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodeResponse;
2224
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodesRequest;
2325
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodesResponse;
26+
import org.elasticsearch.xpack.security.authz.store.CompositeRolesStore;
2427

2528
import java.io.IOException;
2629
import java.util.List;
30+
import java.util.Map;
31+
import java.util.concurrent.CompletableFuture;
2732

2833
public class TransportSecurityStatsAction extends TransportNodesAction<
2934
GetSecurityStatsNodesRequest,
@@ -32,12 +37,16 @@ public class TransportSecurityStatsAction extends TransportNodesAction<
3237
GetSecurityStatsNodeResponse,
3338
Void> {
3439

40+
@Nullable
41+
private final CompositeRolesStore rolesStore;
42+
3543
@Inject
3644
public TransportSecurityStatsAction(
3745
ThreadPool threadPool,
3846
ClusterService clusterService,
3947
TransportService transportService,
40-
ActionFilters actionFilters
48+
ActionFilters actionFilters,
49+
CompositeRolesStore rolesStore
4150
) {
4251
super(
4352
GetSecurityStatsAction.INSTANCE.name(),
@@ -47,6 +56,7 @@ public TransportSecurityStatsAction(
4756
GetSecurityStatsNodeRequest::new,
4857
threadPool.executor(ThreadPool.Names.MANAGEMENT)
4958
);
59+
this.rolesStore = rolesStore;
5060
}
5161

5262
@Override
@@ -70,6 +80,12 @@ protected GetSecurityStatsNodeResponse newNodeResponse(final StreamInput in, fin
7080

7181
@Override
7282
protected GetSecurityStatsNodeResponse nodeOperation(final GetSecurityStatsNodeRequest request, final Task task) {
73-
return new GetSecurityStatsNodeResponse(clusterService.localNode());
83+
final CompletableFuture<Map<String, Object>> rolesStatsFuture = new CompletableFuture<>();
84+
if (rolesStore == null) {
85+
rolesStatsFuture.complete(null);
86+
} else {
87+
rolesStore.usageStats(ActionListener.wrap(rolesStatsFuture::complete, rolesStatsFuture::completeExceptionally));
88+
}
89+
return new GetSecurityStatsNodeResponse(clusterService.localNode(), rolesStatsFuture.join());
7490
}
7591
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"Security stats returns empty response":
2+
"Security stats returns a number of stats":
33
- requires:
44
cluster_features: [ "security_stats_endpoint" ]
55
reason: Introduced in 9.2
@@ -9,4 +9,4 @@
99

1010
- set:
1111
nodes._arbitrary_key_: node_id
12-
- length: { nodes.$node_id: 0 }
12+
- length: { nodes.$node_id: 1 }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"Security stats return roles stats":
3+
- requires:
4+
cluster_features: [ "security_stats_endpoint" ]
5+
reason: Introduced in 9.2
6+
7+
- do:
8+
security.get_stats: {}
9+
10+
- set:
11+
nodes._arbitrary_key_: node_id
12+
- match: { nodes.$node_id.roles.dls.bit_set_cache.hits: 0 }
13+
- match: { nodes.$node_id.roles.file.remote_indices: 0 }
14+
- match: { nodes.$node_id.roles.native.remote_indices: 0 }

0 commit comments

Comments
 (0)