Skip to content

Commit 7f09568

Browse files
committed
wip
Signed-off-by: Nils Bandener <[email protected]>
1 parent 674c287 commit 7f09568

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

src/main/java/org/opensearch/security/privileges/IndicesRequestResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.opensearch.cluster.metadata.ResolvedIndices;
2222

2323
public class IndicesRequestResolver {
24-
private final IndexNameExpressionResolver indexNameExpressionResolver;
24+
protected final IndexNameExpressionResolver indexNameExpressionResolver;
2525

2626
public IndicesRequestResolver(IndexNameExpressionResolver indexNameExpressionResolver) {
2727
this.indexNameExpressionResolver = indexNameExpressionResolver;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*
8+
* Modifications Copyright OpenSearch Contributors. See
9+
* GitHub history for details.
10+
*/
11+
package org.opensearch.security.privileges.actionlevel.legacy;
12+
13+
import org.opensearch.action.ActionRequest;
14+
import org.opensearch.action.IndicesRequest;
15+
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
16+
import org.opensearch.action.support.ActionRequestMetadata;
17+
import org.opensearch.cluster.ClusterState;
18+
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
19+
import org.opensearch.cluster.metadata.OptionallyResolvedIndices;
20+
import org.opensearch.cluster.metadata.ResolvedIndices;
21+
import org.opensearch.security.privileges.IndicesRequestResolver;
22+
import org.opensearch.security.privileges.PrivilegesEvaluationContext;
23+
24+
import java.util.ArrayList;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Set;
28+
import java.util.function.Supplier;
29+
30+
/**
31+
* A modified IndicesRequestResolver which keeps the default index resolution behavior of OpenSearch 3.2.0
32+
*/
33+
public class LegacyIndicesRequestResolver extends IndicesRequestResolver {
34+
35+
public LegacyIndicesRequestResolver(IndexNameExpressionResolver indexNameExpressionResolver) {
36+
super(indexNameExpressionResolver);
37+
}
38+
39+
@Override
40+
public OptionallyResolvedIndices resolve(
41+
ActionRequest request,
42+
ActionRequestMetadata<?, ?> actionRequestMetadata,
43+
Supplier<ClusterState> clusterStateSupplier
44+
) {
45+
if (request instanceof IndicesAliasesRequest indicesAliasesRequest) {
46+
List<String> indices = new ArrayList<>();
47+
ClusterState clusterState = clusterStateSupplier.get();
48+
for (IndicesAliasesRequest.AliasActions aliasActions : indicesAliasesRequest.getAliasActions()) {
49+
indices.addAll(indexNameExpressionResolver.concreteResolvedIndices(clusterState, aliasActions).namesOfIndices(clusterState));
50+
}
51+
return ResolvedIndices.of(indices);
52+
} else {
53+
return super.resolve(request, actionRequestMetadata, clusterStateSupplier);
54+
}
55+
}
56+
}

src/main/java/org/opensearch/security/privileges/actionlevel/legacy/PrivilegesEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public PrivilegesEvaluator(
200200
systemIndexAccessEvaluator = new SystemIndexAccessEvaluator(settings, auditLog);
201201
protectedIndexAccessEvaluator = new ProtectedIndexAccessEvaluator(settings, auditLog);
202202
termsAggregationEvaluator = new TermsAggregationEvaluator();
203-
this.indicesRequestResolver = new IndicesRequestResolver(resolver);
203+
this.indicesRequestResolver = new LegacyIndicesRequestResolver(resolver);
204204

205205
this.pluginIdToActionPrivileges.putAll(createActionPrivileges(pluginIdToRolePrivileges, staticActionGroups));
206206
this.updateConfiguration(actionGroups, rolesConfiguration, generalConfiguration);

src/test/java/org/opensearch/security/IndexIntegrationTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ public void testAliases() throws Exception {
595595
);
596596
assertContains(res, "*\"hits\" : {*\"value\" : 0,*\"hits\" : [ ]*");
597597

598+
res = rh.executePutRequest("/logstash-1/_alias/alog1", "", encodeBasicHeader("aliasmngt", "nagilum"));
599+
System.out.println(res.getBody());
600+
598601
// add alias to allowed index
599602
assertThat(
600603
HttpStatus.SC_OK,
@@ -664,11 +667,23 @@ public void testIndexResolveInvalidIndexName() throws Exception {
664667
setup();
665668
final RestHelper rh = nonSslRestHelper();
666669

667-
// invalid_index_name_exception should be thrown and responded when invalid index name is mentioned in requests.
670+
// invalid_index_name_exception should be thrown and responded when invalid index name is mentioned in requests AND if the
671+
// user has in theory privileges for the (invalid) index name. This is because the index name validation takes
672+
// place in the transport action itself.
673+
// The security plugin should not engage itself in any validation logic that is outside of its scope.
674+
675+
// We do not have privileges for the index below, thus we get a 403 error
668676
HttpResponse res = rh.executeGetRequest(
669677
URLEncoder.encode("_##pdt_data/_search", "UTF-8"),
670678
encodeBasicHeader("ccsresolv", "nagilum")
671679
);
680+
assertThat(res.getStatusCode(), is(HttpStatus.SC_FORBIDDEN));
681+
682+
// We have privileges for the invalid index name below, thus we get through to the validation logic
683+
res = rh.executeGetRequest(
684+
URLEncoder.encode("aabc#data/_search", "UTF-8"),
685+
encodeBasicHeader("ccsresolv", "nagilum")
686+
);
672687
assertThat(res.getStatusCode(), is(HttpStatus.SC_BAD_REQUEST));
673688
Assert.assertTrue(res.getBody().contains("invalid_index_name_exception"));
674689
}

0 commit comments

Comments
 (0)