Skip to content

Commit

Permalink
mo tests and todos
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Oct 21, 2024
1 parent fa10f80 commit 4095cca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
import org.elasticsearch.xpack.core.security.authz.permission.RemoteClusterPermissions;
import org.junit.Ignore;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -355,9 +356,11 @@ public void testEmptyAccessIsNotAllowed() throws IOException {
assertThat(e2.getMessage(), containsString("doesn't support values of type: VALUE_NULL"));
}

@Ignore("TODO: create automaton and test that the permissions are supported instead of checking the names directly")
public void testAPIKeyAllowsAllRemoteClusterPrivilegesForCCS() {
// if users can add remote cluster permissions to a role, then the APIKey should also allow that for that permission
// the inverse however, is not guaranteed. cross_cluster_search exists largely for internal use and is not exposed to the users role
// TODO: create automaton and test that the permissions are supported instead of checking the names directly.
assertTrue(Set.of(CCS_CLUSTER_PRIVILEGE_NAMES).containsAll(RemoteClusterPermissions.getSupportedRemoteClusterPermissions()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.TransportVersions.ROLE_MONITOR_STATS;
import static org.elasticsearch.xpack.core.security.authz.permission.RemoteClusterPermissions.ROLE_REMOTE_CLUSTER_PRIVS;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -126,7 +127,8 @@ public void testPrivilegeNames() {
// create random groups with random privileges for random clusters
List<RemoteClusterPermissionGroup> randomGroups = generateRandomGroups(true);
// replace a random value with one that is allowed
groupPrivileges.get(0)[0] = "monitor_enrich";
String singleValidPrivilege = randomFrom(RemoteClusterPermissions.allowedRemoteClusterPermissions.get(TransportVersion.current()));
groupPrivileges.get(0)[0] = singleValidPrivilege;

for (int i = 0; i < randomGroups.size(); i++) {
String[] privileges = groupPrivileges.get(i);
Expand All @@ -149,7 +151,7 @@ public void testPrivilegeNames() {
assertFalse(Arrays.equals(privileges, found));
if (i == 0) {
// ensure that for the current version we only find the valid "monitor_enrich"
assertThat(Set.of(found), equalTo(Set.of("monitor_enrich")));
assertThat(Set.of(found), equalTo(Set.of(singleValidPrivilege)));
} else {
// all other groups should be found to not have any privileges
assertTrue(found.length == 0);
Expand All @@ -160,20 +162,25 @@ public void testPrivilegeNames() {
}

public void testMonitorEnrichPerVersion() {
// test monitor_enrich before, after and on monitor enrich version
String[] privileges = randomBoolean() ? new String[] { "monitor_enrich" } : new String[] { "monitor_enrich", "foo", "bar" };
testRemotePermissionPerVersion("monitor_enrich", ROLE_REMOTE_CLUSTER_PRIVS);
testRemotePermissionPerVersion("monitor_stats", ROLE_MONITOR_STATS);
}

private void testRemotePermissionPerVersion(String permission, TransportVersion version) {
// test permission before, after and on the version
String[] privileges = randomBoolean() ? new String[] { permission } : new String[] { permission, "foo", "bar" };
String[] before = new RemoteClusterPermissions().addGroup(new RemoteClusterPermissionGroup(privileges, new String[] { "*" }))
.privilegeNames("*", TransportVersionUtils.getPreviousVersion(ROLE_REMOTE_CLUSTER_PRIVS));
.privilegeNames("*", TransportVersionUtils.getPreviousVersion(version));
// empty set since monitor_enrich is not allowed in the before version
assertThat(Set.of(before), equalTo(Collections.emptySet()));
String[] on = new RemoteClusterPermissions().addGroup(new RemoteClusterPermissionGroup(privileges, new String[] { "*" }))
.privilegeNames("*", ROLE_REMOTE_CLUSTER_PRIVS);
.privilegeNames("*", version);
// only monitor_enrich since the other values are not allowed
assertThat(Set.of(on), equalTo(Set.of("monitor_enrich")));
assertThat(Set.of(on), equalTo(Set.of(permission)));
String[] after = new RemoteClusterPermissions().addGroup(new RemoteClusterPermissionGroup(privileges, new String[] { "*" }))
.privilegeNames("*", TransportVersion.current());
// only monitor_enrich since the other values are not allowed
assertThat(Set.of(after), equalTo(Set.of("monitor_enrich")));
assertThat(Set.of(after), equalTo(Set.of(permission)));
}

public void testValidate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ static Set<RoleDescriptor> maybeRemoveRemotePrivileges(
+ ". Remote cluster privileges are not supported by all nodes in the cluster."
);
}
// TODO: support the additional cases where we are trying to send to somethign like 8.16 that understands remote cluster,
// but does not support the new privilege
}
return result;
}
Expand Down

0 comments on commit 4095cca

Please sign in to comment.