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

Fix bug where admin can read system index #4774

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,29 @@ public void adminShouldNotBeAbleToDeleteSecurityIndex() {
assertThat(response4.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
}
}

@Test
public void adminShouldNotBeAbleToReadSecurityIndex() {
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
// Create system index and index a dummy document as the super admin user, data returned to super admin
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
HttpResponse response1 = client.put(".system-index1");

assertThat(response1.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
String doc = "{\"field\":\"value\"}";
HttpResponse adminPostResponse = client.postJson(".system-index1/_doc/1?refresh=true", doc);
assertThat(adminPostResponse.getStatusCode(), equalTo(RestStatus.CREATED.getStatus()));
HttpResponse response2 = client.get(".system-index1/_search");

assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
assertThat(response2.getBody(), response2.getBody().contains("\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"}"));
}

// Regular users should not be able to read it
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
// regular user cannot read system index
HttpResponse response1 = client.get(".system-index1/_search");

assertThat(response1.getBody(), response1.getBody().contains("\"hits\":{\"total\":{\"value\":0,\"relation\":\"eq\"}"));
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.opensearch.core.common.transport.TransportAddress;
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
import org.opensearch.core.index.Index;
import org.opensearch.index.IndexService;
import org.opensearch.indices.SystemIndexRegistry;
import org.opensearch.security.privileges.PrivilegesEvaluator;
import org.opensearch.security.securityconf.ConfigModel;
import org.opensearch.security.securityconf.SecurityRoles;
Expand Down Expand Up @@ -152,7 +153,8 @@ protected final boolean isBlockedProtectedIndexRequest() {
}

protected final boolean isBlockedSystemIndexRequest() {
boolean isSystemIndex = systemIndexMatcher.test(index.getName());
boolean matchesSystemIndexRegisteredWithCore = !SystemIndexRegistry.matchesSystemIndexPattern(Set.of(index.getName())).isEmpty();
boolean isSystemIndex = systemIndexMatcher.test(index.getName()) || matchesSystemIndexRegisteredWithCore;
if (!isSystemIndex) {
return false;
}
Expand All @@ -161,7 +163,7 @@ protected final boolean isBlockedSystemIndexRequest() {
final User user = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (user == null) {
// allow request without user from plugin.
return systemIndexMatcher.test(index.getName());
return systemIndexMatcher.test(index.getName()) || matchesSystemIndexRegisteredWithCore;
}
final TransportAddress caller = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_REMOTE_ADDRESS);
final Set<String> mappedRoles = evaluator.mapRoles(user, caller);
Expand Down
Loading