Skip to content

Commit 30253d9

Browse files
authored
Add DLS stats to _security/stats (#135271)
- Add `roles` with DLS stats to security stats, using the same code and format as `/_xpack/usage`:
1 parent 734a6b7 commit 30253d9

File tree

9 files changed

+130
-6
lines changed

9 files changed

+130
-6
lines changed

docs/changelog/135271.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 135271
2+
summary: Add DLS stats to `_security/stats`
3+
area: Authorization
4+
type: enhancement
5+
issues: []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9176000
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
contextual_ai_service,9175000
1+
roles_security_stats,9176000

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import java.util.Objects;
17+
18+
public class GetSecurityStatsNodeResponseTests extends AbstractWireSerializingTestCase<GetSecurityStatsNodeResponse> {
19+
20+
@Override
21+
protected Writeable.Reader<GetSecurityStatsNodeResponse> instanceReader() {
22+
return GetSecurityStatsNodeResponse::new;
23+
}
24+
25+
@Override
26+
protected GetSecurityStatsNodeResponse createTestInstance() {
27+
return new GetSecurityStatsNodeResponse(
28+
DiscoveryNodeUtils.builder(randomAlphaOfLength(10)).ephemeralId(randomAlphanumericOfLength(10)).build(),
29+
randomBoolean() ? null : Map.of("key", randomAlphaOfLength(5))
30+
);
31+
}
32+
33+
@Override
34+
protected GetSecurityStatsNodeResponse mutateInstance(GetSecurityStatsNodeResponse instance) throws IOException {
35+
final var node = instance.getDiscoveryNode();
36+
final var value = Objects.requireNonNullElse(instance.getRolesStoreStats(), Map.of()).get("key");
37+
return switch (randomIntBetween(0, 1)) {
38+
case 0 -> new GetSecurityStatsNodeResponse(
39+
DiscoveryNodeUtils.builder(randomValueOtherThan(node.getId(), () -> randomAlphaOfLength(10)))
40+
// DiscoverNode#hashCode only tests ephemeralId, so make sure to change it too
41+
.ephemeralId(randomValueOtherThan(node.getEphemeralId(), () -> randomAlphanumericOfLength(10)))
42+
.build(),
43+
instance.getRolesStoreStats()
44+
);
45+
case 1 -> new GetSecurityStatsNodeResponse(node, Map.of("key", randomValueOtherThan(value, () -> randomAlphaOfLength(5))));
46+
default -> throw new IllegalStateException("Unexpected value");
47+
};
48+
}
49+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.node.DiscoveryNode;
1313
import org.elasticsearch.cluster.service.ClusterService;
1414
import org.elasticsearch.common.io.stream.StreamInput;
15+
import org.elasticsearch.core.Nullable;
1516
import org.elasticsearch.injection.guice.Inject;
1617
import org.elasticsearch.tasks.Task;
1718
import org.elasticsearch.threadpool.ThreadPool;
@@ -21,6 +22,7 @@
2122
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodeResponse;
2223
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodesRequest;
2324
import org.elasticsearch.xpack.core.security.action.stats.GetSecurityStatsNodesResponse;
25+
import org.elasticsearch.xpack.security.authz.store.CompositeRolesStore;
2426

2527
import java.io.IOException;
2628
import java.util.List;
@@ -32,12 +34,16 @@ public class TransportSecurityStatsAction extends TransportNodesAction<
3234
GetSecurityStatsNodeResponse,
3335
Void> {
3436

37+
@Nullable
38+
private final CompositeRolesStore rolesStore;
39+
3540
@Inject
3641
public TransportSecurityStatsAction(
3742
ThreadPool threadPool,
3843
ClusterService clusterService,
3944
TransportService transportService,
40-
ActionFilters actionFilters
45+
ActionFilters actionFilters,
46+
CompositeRolesStore rolesStore
4147
) {
4248
super(
4349
GetSecurityStatsAction.INSTANCE.name(),
@@ -47,6 +53,7 @@ public TransportSecurityStatsAction(
4753
GetSecurityStatsNodeRequest::new,
4854
threadPool.executor(ThreadPool.Names.MANAGEMENT)
4955
);
56+
this.rolesStore = rolesStore;
5057
}
5158

5259
@Override
@@ -70,6 +77,6 @@ protected GetSecurityStatsNodeResponse newNodeResponse(final StreamInput in, fin
7077

7178
@Override
7279
protected GetSecurityStatsNodeResponse nodeOperation(final GetSecurityStatsNodeRequest request, final Task task) {
73-
return new GetSecurityStatsNodeResponse(clusterService.localNode());
80+
return new GetSecurityStatsNodeResponse(clusterService.localNode(), rolesStore == null ? null : rolesStore.usageStatsWithJustDls());
7481
}
7582
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ Iterable<ProjectScoped<RoleKey>> cachedRoles() {
669669
return this.roleCache.keys();
670670
}
671671

672+
public Map<String, Object> usageStatsWithJustDls() {
673+
return Map.of("dls", Map.of("bit_set_cache", dlsBitsetCache.usageStats()));
674+
}
675+
672676
public void usageStats(ActionListener<Map<String, Object>> listener) {
673677
final Map<String, Object> usage = new HashMap<>();
674678
usage.put("dls", Map.of("bit_set_cache", dlsBitsetCache.usageStats()));
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"Security stats return just DLS 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+
- length: { nodes.$node_id: 1 }
13+
- length: { nodes.$node_id.roles: 1 }
14+
- length: { nodes.$node_id.roles.dls: 1 }
15+
- length: { nodes.$node_id.roles.dls.bit_set_cache: 8 }
16+
- gte: { nodes.$node_id.roles.dls.bit_set_cache.count: 0 }

0 commit comments

Comments
 (0)