|
74 | 74 | import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.EmptyAuthorizationInfo;
|
75 | 75 | import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames;
|
76 | 76 | import org.elasticsearch.xpack.core.security.user.AnonymousUser;
|
| 77 | +import org.elasticsearch.xpack.core.security.user.AsyncSearchUser; |
77 | 78 | import org.elasticsearch.xpack.core.security.user.SystemUser;
|
78 | 79 | import org.elasticsearch.xpack.core.security.user.User;
|
| 80 | +import org.elasticsearch.xpack.core.security.user.XPackSecurityUser; |
| 81 | +import org.elasticsearch.xpack.core.security.user.XPackUser; |
79 | 82 | import org.elasticsearch.xpack.security.audit.AuditTrail;
|
80 | 83 | import org.elasticsearch.xpack.security.audit.AuditTrailService;
|
81 | 84 | import org.elasticsearch.xpack.security.audit.AuditUtil;
|
|
108 | 111 | import static org.elasticsearch.xpack.core.security.support.Exceptions.authenticationError;
|
109 | 112 | import static org.elasticsearch.xpack.security.authc.TokenServiceTests.mockGetTokenFromId;
|
110 | 113 | import static org.hamcrest.Matchers.arrayContaining;
|
| 114 | +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
111 | 115 | import static org.hamcrest.Matchers.contains;
|
112 | 116 | import static org.hamcrest.Matchers.containsString;
|
113 | 117 | import static org.hamcrest.Matchers.emptyOrNullString;
|
@@ -902,6 +906,48 @@ public void testAnonymousUserTransportWithDefaultUser() throws Exception {
|
902 | 906 | assertThreadContextContainsAuthentication(result);
|
903 | 907 | }
|
904 | 908 |
|
| 909 | + public void testInheritAnonymousUserRoles() { |
| 910 | + Settings settings = Settings.builder() |
| 911 | + .putList(AnonymousUser.ROLES_SETTING.getKey(), "r3", "r4", "r5") |
| 912 | + .build(); |
| 913 | + final AnonymousUser anonymousUser = new AnonymousUser(settings); |
| 914 | + service = new AuthenticationService(settings, realms, auditTrailService, |
| 915 | + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), |
| 916 | + threadPool, anonymousUser, tokenService, apiKeyService); |
| 917 | + User user1 = new User("username", "r1", "r2", "r3"); |
| 918 | + when(firstRealm.token(threadContext)).thenReturn(token); |
| 919 | + when(firstRealm.supports(token)).thenReturn(true); |
| 920 | + mockAuthenticate(firstRealm, token, user1); |
| 921 | + // this call does not actually go async |
| 922 | + final AtomicBoolean completed = new AtomicBoolean(false); |
| 923 | + service.authenticate(restRequest, true, ActionListener.wrap(authentication -> { |
| 924 | + assertThat(authentication.getUser().roles(), arrayContainingInAnyOrder("r1", "r2", "r3", "r4", "r5")); |
| 925 | + setCompletedToTrue(completed); |
| 926 | + }, this::logAndFail)); |
| 927 | + assertTrue(completed.get()); |
| 928 | + } |
| 929 | + |
| 930 | + public void testSystemUsersDoNotInheritAnonymousRoles() { |
| 931 | + Settings settings = Settings.builder() |
| 932 | + .putList(AnonymousUser.ROLES_SETTING.getKey(), "r3", "r4", "r5") |
| 933 | + .build(); |
| 934 | + final AnonymousUser anonymousUser = new AnonymousUser(settings); |
| 935 | + service = new AuthenticationService(settings, realms, auditTrailService, |
| 936 | + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), |
| 937 | + threadPool, anonymousUser, tokenService, apiKeyService); |
| 938 | + when(firstRealm.token(threadContext)).thenReturn(token); |
| 939 | + when(firstRealm.supports(token)).thenReturn(true); |
| 940 | + final User sysUser = randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE); |
| 941 | + mockAuthenticate(firstRealm, token, sysUser); |
| 942 | + // this call does not actually go async |
| 943 | + final AtomicBoolean completed = new AtomicBoolean(false); |
| 944 | + service.authenticate(restRequest, true, ActionListener.wrap(authentication -> { |
| 945 | + assertThat(authentication.getUser().roles(), equalTo(sysUser.roles())); |
| 946 | + setCompletedToTrue(completed); |
| 947 | + }, this::logAndFail)); |
| 948 | + assertTrue(completed.get()); |
| 949 | + } |
| 950 | + |
905 | 951 | public void testRealmTokenThrowingException() throws Exception {
|
906 | 952 | final String reqId = AuditUtil.getOrGenerateRequestId(threadContext);
|
907 | 953 | when(firstRealm.token(threadContext)).thenThrow(authenticationError("realm doesn't like tokens"));
|
|
0 commit comments