Skip to content

Commit 22b861f

Browse files
committed
Fix and more tests
1 parent baf63ed commit 22b861f

File tree

2 files changed

+92
-28
lines changed

2 files changed

+92
-28
lines changed

x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/privilege/FailureStoreSecurityRestIT.java renamed to x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/failurestore/FailureStoreSecurityRestIT.java

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77

8-
package org.elasticsearch.xpack.security.privilege;
8+
package org.elasticsearch.xpack.security.failurestore;
99

1010
import org.elasticsearch.client.Request;
1111
import org.elasticsearch.client.Response;
@@ -45,8 +45,16 @@ protected Settings restAdminSettings() {
4545
}
4646

4747
public void testGetUserPrivileges() throws IOException {
48-
Request roleRequest = new Request("PUT", "/_security/role/role");
49-
roleRequest.setJsonEntity("""
48+
Request userRequest = new Request("PUT", "/_security/user/user");
49+
userRequest.setJsonEntity("""
50+
{
51+
"password": "x-pack-test-password",
52+
"roles": ["role"]
53+
}
54+
""");
55+
assertOK(adminClient().performRequest(userRequest));
56+
57+
putRole("""
5058
{
5159
"cluster": ["all"],
5260
"indices": [
@@ -57,26 +65,7 @@ public void testGetUserPrivileges() throws IOException {
5765
]
5866
}
5967
""");
60-
assertOK(adminClient().performRequest(roleRequest));
61-
62-
Request userRequest = new Request("PUT", "/_security/user/user");
63-
userRequest.setJsonEntity("""
64-
{
65-
"password": "x-pack-test-password",
66-
"roles": ["role"]
67-
}
68-
""");
69-
assertOK(adminClient().performRequest(userRequest));
70-
71-
Request request = new Request("GET", "/_security/user/_privileges");
72-
request.setOptions(
73-
request.getOptions()
74-
.toBuilder()
75-
.addHeader("Authorization", basicAuthHeaderValue("user", new SecureString("x-pack-test-password".toCharArray())))
76-
);
77-
Response response = client().performRequest(request);
78-
assertOK(response);
79-
assertThat(responseAsMap(response), equalTo(mapFromJson("""
68+
expectUserPrivilegesResponse("""
8069
{
8170
"cluster": ["all"],
8271
"global": [],
@@ -92,7 +81,75 @@ public void testGetUserPrivileges() throws IOException {
9281
}],
9382
"applications": [],
9483
"run_as": []
95-
}""")));
84+
}""");
85+
86+
putRole("""
87+
{
88+
"cluster": ["all"],
89+
"indices": [
90+
{
91+
"names": ["*"],
92+
"privileges": ["read_failure_store"]
93+
}
94+
]
95+
}
96+
""");
97+
expectUserPrivilegesResponse("""
98+
{
99+
"cluster": ["all"],
100+
"global": [],
101+
"indices": [
102+
{
103+
"names": ["*"],
104+
"privileges": ["read_failure_store"],
105+
"allow_restricted_indices": false
106+
}],
107+
"applications": [],
108+
"run_as": []
109+
}""");
110+
111+
putRole("""
112+
{
113+
"cluster": ["all"],
114+
"indices": [
115+
{
116+
"names": ["*"],
117+
"privileges": ["all", "read_failure_store"]
118+
}
119+
]
120+
}
121+
""");
122+
expectUserPrivilegesResponse("""
123+
{
124+
"cluster": ["all"],
125+
"global": [],
126+
"indices": [
127+
{
128+
"names": ["*"],
129+
"privileges": ["all", "read_failure_store"],
130+
"allow_restricted_indices": false
131+
}],
132+
"applications": [],
133+
"run_as": []
134+
}""");
135+
}
136+
137+
private static void expectUserPrivilegesResponse(String userPrivilegesResponse) throws IOException {
138+
Request request = new Request("GET", "/_security/user/_privileges");
139+
request.setOptions(
140+
request.getOptions()
141+
.toBuilder()
142+
.addHeader("Authorization", basicAuthHeaderValue("user", new SecureString("x-pack-test-password".toCharArray())))
143+
);
144+
Response response = client().performRequest(request);
145+
assertOK(response);
146+
assertThat(responseAsMap(response), equalTo(mapFromJson(userPrivilegesResponse)));
147+
}
148+
149+
private static void putRole(String rolePayload) throws IOException {
150+
Request roleRequest = new Request("PUT", "/_security/role/role");
151+
roleRequest.setJsonEntity(rolePayload);
152+
assertOK(adminClient().performRequest(roleRequest));
96153
}
97154

98155
private static Map<String, Object> mapFromJson(String json) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.threadpool.ThreadPool;
2020
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
2121
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges;
22+
import org.elasticsearch.xpack.core.security.authz.privilege.IndexComponentSelectorPredicate;
2223
import org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege;
2324
import org.elasticsearch.xpack.core.security.support.Automatons;
2425
import org.elasticsearch.xpack.core.security.support.StringMatcher;
@@ -186,21 +187,27 @@ private void logDeprecatedPermission(RoleDescriptor roleDescriptor) {
186187
final Set<IndexPrivilege> aliasPrivileges = IndexPrivilege.splitBySelectorAccess(aliasPrivilegeNames);
187188
final SortedSet<String> inferiorIndexNames = new TreeSet<>();
188189
for (var aliasPrivilege : aliasPrivileges) {
190+
// TODO implement failures handling in a follow-up
191+
if (aliasPrivilege.getSelectorPredicate() == IndexComponentSelectorPredicate.FAILURES) {
192+
continue;
193+
}
189194
final Automaton aliasPrivilegeAutomaton = aliasPrivilege.getAutomaton();
190195
// check if the alias grants superiors privileges than the indices it points to
191196
for (Index index : aliasOrIndexMap.get(aliasName).getIndices()) {
192197
final Set<String> indexPrivileges = privilegesByIndexMap.get(index.getName());
193198
// null iff the index does not have *any* privilege
194199
if (indexPrivileges != null) {
195-
// compute automaton once per index no matter how many times it is pointed to
200+
// compute privilege set once per index no matter how many times it is pointed to
196201
final Set<IndexPrivilege> indexPrivilegeSet = indexPrivilegeMap.computeIfAbsent(
197202
index.getName(),
198203
i -> IndexPrivilege.splitBySelectorAccess(indexPrivileges)
199204
);
200205
for (var indexPrivilege : indexPrivilegeSet) {
201-
// TODO still not quite right
202-
if (indexPrivilege.getSelectorPredicate() == aliasPrivilege.getSelectorPredicate()
203-
&& false == Automatons.subsetOf(indexPrivilege.getAutomaton(), aliasPrivilegeAutomaton)) {
206+
// TODO implement failures handling in a follow-up
207+
if (indexPrivilege.getSelectorPredicate() == IndexComponentSelectorPredicate.FAILURES) {
208+
continue;
209+
}
210+
if (false == Automatons.subsetOf(indexPrivilege.getAutomaton(), aliasPrivilegeAutomaton)) {
204211
inferiorIndexNames.add(index.getName());
205212
}
206213
}

0 commit comments

Comments
 (0)