Skip to content

Commit

Permalink
[Test] Replace all mock authentication with concrete objects (#86424)
Browse files Browse the repository at this point in the history
Changes in this PR enables marking the Authentication class to final in
a future PR as part of the overall plan for closing down Authentication.
  • Loading branch information
ywangd authored May 6, 2022
1 parent 339a3c0 commit 051f971
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.xpack.core.security.SecurityContext;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authc.support.AuthenticationContextSerializer;
import org.elasticsearch.xpack.core.security.authz.permission.DocumentPermissions;
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions;
import org.elasticsearch.xpack.core.security.user.User;

import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -78,9 +78,7 @@ public void testDLS() throws Exception {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext);

final Authentication authentication = mock(Authentication.class);
when(authentication.getUser()).thenReturn(mock(User.class));
when(authentication.encode()).thenReturn(randomAlphaOfLength(24)); // don't care as long as it's not null
final Authentication authentication = AuthenticationTestHelper.builder().build();
new AuthenticationContextSerializer().writeToContext(authentication, threadContext);

IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(shardId.getIndex(), Settings.EMPTY);
Expand Down Expand Up @@ -215,9 +213,7 @@ public void testDLSWithLimitedPermissions() throws Exception {

final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext);
final Authentication authentication = mock(Authentication.class);
when(authentication.getUser()).thenReturn(mock(User.class));
when(authentication.encode()).thenReturn(randomAlphaOfLength(24)); // don't care as long as it's not null
final Authentication authentication = AuthenticationTestHelper.builder().build();
new AuthenticationContextSerializer().writeToContext(authentication, threadContext);

final boolean noFilteredIndexPermissions = randomBoolean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilege;
import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilegeResolver;
import org.elasticsearch.xpack.core.security.authz.privilege.ConfigurableClusterPrivilege;
Expand Down Expand Up @@ -42,7 +43,7 @@ public ClusterPermission.Builder buildPermission(ClusterPermission.Builder build
@Before
public void setup() {
mockTransportRequest = mock(TransportRequest.class);
mockAuthentication = mock(Authentication.class);
mockAuthentication = AuthenticationTestHelper.builder().build();
}

public void testClusterPermissionBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authz.RestrictedIndices;
import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl;
import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege;
Expand Down Expand Up @@ -183,7 +184,7 @@ public void testCheckClusterAction() {
Role fromRole = Role.builder(EMPTY_RESTRICTED_INDICES, "a-role")
.cluster(Collections.singleton("manage_security"), Collections.emptyList())
.build();
Authentication authentication = mock(Authentication.class);
Authentication authentication = AuthenticationTestHelper.builder().build();
assertThat(fromRole.checkClusterAction("cluster:admin/xpack/security/x", mock(TransportRequest.class), authentication), is(true));
{
Role limitedByRole = Role.builder(EMPTY_RESTRICTED_INDICES, "limited-role")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.xpack.core.security.action.privilege.GetPrivilegesRequest;
import org.elasticsearch.xpack.core.security.action.privilege.PutPrivilegesRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;
import org.elasticsearch.xpack.core.security.authz.privilege.ConfigurableClusterPrivileges.ManageApplicationPrivileges;

Expand All @@ -40,7 +41,6 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;

public class ManageApplicationPrivilegesTests extends ESTestCase {

Expand Down Expand Up @@ -99,7 +99,7 @@ public void testActionAndRequestPredicate() {
assertThat(kibanaAndLogstashPermission, notNullValue());
assertThat(cloudAndSwiftypePermission, notNullValue());

final Authentication authentication = mock(Authentication.class);
final Authentication authentication = AuthenticationTestHelper.builder().build();
final GetPrivilegesRequest getKibana1 = new GetPrivilegesRequest();
getKibana1.application("kibana-1");
assertTrue(kibanaAndLogstashPermission.check("cluster:admin/xpack/security/privilege/get", getKibana1, authentication));
Expand Down Expand Up @@ -128,7 +128,7 @@ public void testActionAndRequestPredicate() {
}

public void testSecurityForGetAllApplicationPrivileges() {
final Authentication authentication = mock(Authentication.class);
final Authentication authentication = AuthenticationTestHelper.builder().build();
final GetPrivilegesRequest getAll = new GetPrivilegesRequest();
getAll.application(null);
getAll.privileges(new String[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyAction;
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTests;
import org.elasticsearch.xpack.core.security.authc.RealmConfig;
import org.elasticsearch.xpack.core.security.authc.RealmDomain;
Expand Down Expand Up @@ -196,7 +197,7 @@ public void testCheckQueryApiKeyRequest() {
queryApiKeyRequest.setFilterForCurrentUser();
}
assertThat(
clusterPermission.check(QueryApiKeyAction.NAME, queryApiKeyRequest, mock(Authentication.class)),
clusterPermission.check(QueryApiKeyAction.NAME, queryApiKeyRequest, AuthenticationTestHelper.builder().build()),
is(queryApiKeyRequest.isFilterForCurrentUser())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;
import org.elasticsearch.xpack.core.security.support.Automatons;
import org.junit.Rule;
Expand Down Expand Up @@ -50,14 +50,14 @@ public void testSubActionPattern() throws Exception {
private void verifyClusterActionAllowed(ClusterPrivilege clusterPrivilege, String... actions) {
ClusterPermission clusterPermission = clusterPrivilege.buildPermission(ClusterPermission.builder()).build();
for (String action : actions) {
assertTrue(clusterPermission.check(action, mock(TransportRequest.class), mock(Authentication.class)));
assertTrue(clusterPermission.check(action, mock(TransportRequest.class), AuthenticationTestHelper.builder().build()));
}
}

private void verifyClusterActionDenied(ClusterPrivilege clusterPrivilege, String... actions) {
ClusterPermission clusterPermission = clusterPrivilege.buildPermission(ClusterPermission.builder()).build();
for (String action : actions) {
assertFalse(clusterPermission.check(action, mock(TransportRequest.class), mock(Authentication.class)));
assertFalse(clusterPermission.check(action, mock(TransportRequest.class), AuthenticationTestHelper.builder().build()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.xpack.core.security.action.profile.UpdateProfileDataAction;
import org.elasticsearch.xpack.core.security.action.profile.UpdateProfileDataRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void testActionAndRequestPredicate() {
.build();
assertThat(writeProfileDataPermission, notNullValue());

final Authentication authentication = mock(Authentication.class);
final Authentication authentication = AuthenticationTestHelper.builder().build();
// request application name matches privilege wildcard
UpdateProfileDataRequest updateProfileDataRequest = randomBoolean()
? newUpdateProfileDataRequest(Set.of(prefix + randomAlphaOfLengthBetween(0, 2)), Set.of())
Expand Down Expand Up @@ -180,7 +181,7 @@ public void testParseAbnormals() throws Exception {
? newUpdateProfileDataRequest(Set.of(randomAlphaOfLengthBetween(0, 2)), Set.of())
: newUpdateProfileDataRequest(Set.of(), Set.of(randomAlphaOfLengthBetween(0, 2)));
ClusterPermission perm = priv.buildPermission(ClusterPermission.builder()).build();
assertFalse(perm.check(UpdateProfileDataAction.NAME, updateProfileDataRequest, mock(Authentication.class)));
assertFalse(perm.check(UpdateProfileDataAction.NAME, updateProfileDataRequest, AuthenticationTestHelper.builder().build()));
}
final String aNullApplication = "{\"write\":{\"applications\":[null]}}";
try (
Expand Down Expand Up @@ -217,12 +218,18 @@ public void testParseAbnormals() throws Exception {
? newUpdateProfileDataRequest(Set.of(randomAlphaOfLengthBetween(1, 2)), Set.of())
: newUpdateProfileDataRequest(Set.of(), Set.of(randomAlphaOfLengthBetween(1, 2)));
ClusterPermission perm = priv.buildPermission(ClusterPermission.builder()).build();
assertFalse(perm.check(UpdateProfileDataAction.NAME, updateProfileDataRequest, mock(Authentication.class)));
assertFalse(perm.check(UpdateProfileDataAction.NAME, updateProfileDataRequest, AuthenticationTestHelper.builder().build()));
updateProfileDataRequest = randomBoolean()
? newUpdateProfileDataRequest(Set.of(""), Set.of())
: newUpdateProfileDataRequest(Set.of(), Set.of(""));
perm = priv.buildPermission(ClusterPermission.builder()).build();
assertTrue(perm.check("cluster:admin/xpack/security/profile/put/data", updateProfileDataRequest, mock(Authentication.class)));
assertTrue(
perm.check(
"cluster:admin/xpack/security/profile/put/data",
updateProfileDataRequest,
AuthenticationTestHelper.builder().build()
)
);
}
}

Expand Down
Loading

0 comments on commit 051f971

Please sign in to comment.