Skip to content

Commit ac7fc11

Browse files
authored
The Authentication class is now final (#86544)
This PR marks the Authentication class to be final which is the last step of locking down the Authentication class. The Authentication class is basically a record class and it has internal logics on what values can or cannot be used together. We don't expect it to be extended by any subclasses and a concrete object should always be created for tests instead of mocking. Relates: #86424 Relates: #86206
1 parent d90b0d1 commit ac7fc11

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
// TODO(hub-cap) Clean this up after moving User over - This class can re-inherit its field AUTHENTICATION_KEY in AuthenticationField.
6969
// That interface can be removed
70-
public class Authentication implements ToXContentObject {
70+
public final class Authentication implements ToXContentObject {
7171

7272
private static final Logger logger = LogManager.getLogger(Authentication.class);
7373

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportSetEnabledActionTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import static org.hamcrest.Matchers.nullValue;
4545
import static org.hamcrest.Matchers.sameInstance;
4646
import static org.mockito.ArgumentMatchers.eq;
47-
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
4847
import static org.mockito.Mockito.doAnswer;
4948
import static org.mockito.Mockito.mock;
5049
import static org.mockito.Mockito.times;
@@ -122,10 +121,10 @@ public void testValidUserWithInternalUsername() throws Exception {
122121

123122
public void testUserCanModifySameNameUserFromDifferentRealm() throws Exception {
124123
final User user = randomFrom(new ElasticUser(true), new KibanaUser(true), new User("joe"));
125-
Authentication authentication = mock(Authentication.class, RETURNS_DEEP_STUBS);
126-
when(authentication.getEffectiveSubject().getUser()).thenReturn(user);
127-
when(authentication.getEffectiveSubject().getRealm().getType()).thenReturn("other_realm");
128-
when(authentication.encode()).thenReturn(randomAlphaOfLength(24)); // just can't be null
124+
Authentication authentication = AuthenticationTestHelper.builder()
125+
.user(user)
126+
.realmRef(new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), "other_realm", randomAlphaOfLengthBetween(3, 8)))
127+
.build(false);
129128
testValidUser(user, authentication);
130129
}
131130

0 commit comments

Comments
 (0)