Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command cat/indices will filter results per the Do Not Fail On Forbidden setting #3236

Merged
merged 9 commits into from
Aug 29, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.opensearch.security.privileges;

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opensearch.test.framework.TestSecurityConfig;
import org.opensearch.test.framework.cluster.ClusterManager;
import org.opensearch.test.framework.cluster.LocalCluster;
import org.opensearch.test.framework.cluster.TestRestClient;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL;

@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
public class PrivilegesEvaluatorDNFOFTest {
derek-ho marked this conversation as resolved.
Show resolved Hide resolved

protected final static TestSecurityConfig.User GET_INDICES = new TestSecurityConfig.User("get_indices_user").roles(
new TestSecurityConfig.Role("get_indices_role").indexPermissions("*").on("logs-*").clusterPermissions("*")
);

private String TEST_DOC = "{\"source\": {\"title\": \"Spirited Away\"}}";

@ClassRule
public static LocalCluster dnfofCluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS)
.authc(AUTHC_HTTPBASIC_INTERNAL)
.users(TestSecurityConfig.User.USER_ADMIN, GET_INDICES)
.doNotFailOnForbidden(true)
.anonymousAuth(false)
.build();

@Test
public void testGetIndicesSuccess() {
// Insert doc into logs-123 index with admin user
try (TestRestClient client = dnfofCluster.getRestClient(TestSecurityConfig.User.USER_ADMIN)) {
TestRestClient.HttpResponse response = client.postJson("logs-123/_doc", TEST_DOC);
assertThat(response.getStatusCode(), equalTo(HttpStatus.SC_CREATED));
}

try (TestRestClient client = dnfofCluster.getRestClient(GET_INDICES)) {
final String catIndices = "/_cat/indices";
final TestRestClient.HttpResponse catIndicesResponse = client.get(catIndices);
assertThat(catIndicesResponse.getStatusCode(), equalTo(HttpStatus.SC_OK));
assertThat(catIndicesResponse.getBody(), containsString("logs-123"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class PrivilegesEvaluator {
private static final WildcardMatcher ACTION_MATCHER = WildcardMatcher.from("indices:data/read/*search*");

private static final Pattern DNFOF_PATTERNS = Pattern.compile(
"indices:(data/read/.*|(admin/(mappings/fields/get.*|shards/search_shards|resolve/index)))"
"indices:(data/read/.*|(admin/(mappings/fields/get.*|shards/search_shards|resolve/index))|(monitor/((settings/get)|(stats))))"
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
);
derek-ho marked this conversation as resolved.
Show resolved Hide resolved

private static final IndicesOptions ALLOW_EMPTY = IndicesOptions.fromOptions(true, true, false, false);
Expand Down