-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
New configuration to (dis-)allow external users create access tokens #21438
Open
fpetersen-gl
wants to merge
3
commits into
master
Choose a base branch
from
new_config_allow_tokens_for_externals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−46
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Introduce new configuration for allowing external users to create an …
…access token (or not). Starting with graylog 6.2, this is set to not allow.
commit b84922ff2a9266fd9696deaec4971615459f516c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,18 +17,24 @@ | |
package org.graylog2.rest.resources.users; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import jakarta.ws.rs.ForbiddenException; | ||
import jakarta.ws.rs.core.Response; | ||
import org.apache.shiro.mgt.DefaultSecurityManager; | ||
import org.apache.shiro.subject.Subject; | ||
import org.bson.types.ObjectId; | ||
import org.graylog.security.authservice.GlobalAuthServiceConfig; | ||
import org.graylog.testing.mongodb.MongoDBInstance; | ||
import org.graylog2.Configuration; | ||
import org.graylog2.configuration.HttpConfiguration; | ||
import org.graylog2.plugin.Tools; | ||
import org.graylog2.plugin.cluster.ClusterConfigService; | ||
import org.graylog2.plugin.database.ValidationException; | ||
import org.graylog2.rest.models.users.requests.CreateUserRequest; | ||
import org.graylog2.rest.models.users.requests.Startpage; | ||
import org.graylog2.rest.models.users.requests.UpdateUserPreferences; | ||
import org.graylog2.rest.models.users.responses.Token; | ||
import org.graylog2.security.AccessToken; | ||
import org.graylog2.security.AccessTokenImpl; | ||
import org.graylog2.security.AccessTokenService; | ||
import org.graylog2.security.MongoDBSessionService; | ||
import org.graylog2.security.PasswordAlgorithmFactory; | ||
|
@@ -39,7 +45,9 @@ | |
import org.graylog2.shared.users.UserManagementService; | ||
import org.graylog2.users.PaginatedUserService; | ||
import org.graylog2.users.RoleService; | ||
import org.graylog2.users.UserConfiguration; | ||
import org.graylog2.users.UserImpl; | ||
import org.joda.time.DateTime; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
|
@@ -48,18 +56,21 @@ | |
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.graylog2.shared.security.RestPermissions.USERS_TOKENCREATE; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class UsersResourceTest { | ||
|
@@ -100,6 +111,8 @@ public class UsersResourceTest { | |
private DefaultSecurityManager securityManager; | ||
@Mock | ||
private GlobalAuthServiceConfig globalAuthServiceConfig; | ||
@Mock | ||
private ClusterConfigService clusterConfigService; | ||
|
||
UserImplFactory userImplFactory; | ||
|
||
|
@@ -109,7 +122,7 @@ public void setUp() throws Exception { | |
new Permissions(ImmutableSet.of(new RestPermissions()))); | ||
usersResource = new TestUsersResource(userManagementService, paginatedUserService, accessTokenService, | ||
roleService, sessionService, new HttpConfiguration(), subject, | ||
sessionTerminationService, securityManager, globalAuthServiceConfig); | ||
sessionTerminationService, securityManager, globalAuthServiceConfig, clusterConfigService); | ||
} | ||
|
||
/** | ||
|
@@ -132,6 +145,78 @@ public void savePreferencesSuccess() throws ValidationException { | |
verify(userManagementService, times(1)).save(isA(UserImpl.class)); | ||
} | ||
|
||
@Test | ||
public void createTokenForInternalUserSucceeds() { | ||
final Map<String, Object> userProps = Map.of(UserImpl.USERNAME, USERNAME); | ||
final String tokenName = "tokenName"; | ||
final String token = "someToken"; | ||
final DateTime lastAccess = Tools.nowUTC(); | ||
final Map<String, Object> tokenProps = Map.of(AccessTokenImpl.NAME, tokenName, AccessTokenImpl.TOKEN, token, AccessTokenImpl.LAST_ACCESS, lastAccess); | ||
final ObjectId tokenId = new ObjectId(); | ||
final AccessToken accessToken = new AccessTokenImpl(tokenId, tokenProps); | ||
final Token expected = Token.create(tokenId.toHexString(), tokenName, token, lastAccess); | ||
|
||
when(userManagementService.loadById(eq(USERNAME))).thenReturn(userImplFactory.create(userProps)); | ||
when(subject.isPermitted(eq(USERS_TOKENCREATE + ":" + USERNAME))).thenReturn(true); | ||
when(accessTokenService.create(USERNAME, tokenName)).thenReturn(accessToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider factoring out token creation into a separate method to improve clarity and reduce code size.
|
||
|
||
try { | ||
final Token actual = usersResource.generateNewToken(USERNAME, tokenName, "{}"); | ||
assertEquals(expected, actual); | ||
} finally { | ||
verify(subject).isPermitted(USERS_TOKENCREATE + ":" + USERNAME); | ||
verify(accessTokenService).create(USERNAME, tokenName); | ||
verifyNoMoreInteractions(clusterConfigService, accessTokenService); | ||
} | ||
} | ||
|
||
@Test(expected = ForbiddenException.class) | ||
public void createTokenForExternalUserFailsIfConfigured() { | ||
final Map<String, Object> userProps = Map.of(UserImpl.USERNAME, USERNAME, UserImpl.EXTERNAL_USER, "TRUE"); | ||
final String tokenName = "tokenName"; | ||
|
||
when(userManagementService.loadById(eq(USERNAME))).thenReturn(userImplFactory.create(userProps)); | ||
when(subject.isPermitted(eq(USERS_TOKENCREATE + ":" + USERNAME))).thenReturn(true); | ||
when(clusterConfigService.getOrDefault(UserConfiguration.class, UserConfiguration.DEFAULT_VALUES)) | ||
.thenReturn(UserConfiguration.create(false, Duration.of(8, ChronoUnit.HOURS), false)); | ||
|
||
try { | ||
usersResource.generateNewToken(USERNAME, tokenName, "{}"); | ||
} finally { | ||
verify(subject).isPermitted(USERS_TOKENCREATE + ":" + USERNAME); | ||
verify(clusterConfigService).getOrDefault(UserConfiguration.class, UserConfiguration.DEFAULT_VALUES); | ||
verifyNoMoreInteractions(clusterConfigService, accessTokenService); | ||
} | ||
} | ||
|
||
@Test | ||
public void createTokenForExternalUserSucceedsIfConfigured() { | ||
final Map<String, Object> userProps = Map.of(UserImpl.USERNAME, USERNAME, UserImpl.EXTERNAL_USER, "TRUE"); | ||
final String tokenName = "tokenName"; | ||
final String token = "someToken"; | ||
final DateTime lastAccess = Tools.nowUTC(); | ||
final Map<String, Object> tokenProps = Map.of(AccessTokenImpl.NAME, tokenName, AccessTokenImpl.TOKEN, token, AccessTokenImpl.LAST_ACCESS, lastAccess); | ||
final ObjectId tokenId = new ObjectId(); | ||
final AccessToken accessToken = new AccessTokenImpl(tokenId, tokenProps); | ||
final Token expected = Token.create(tokenId.toHexString(), tokenName, token, lastAccess); | ||
|
||
when(userManagementService.loadById(eq(USERNAME))).thenReturn(userImplFactory.create(userProps)); | ||
when(subject.isPermitted(eq(USERS_TOKENCREATE + ":" + USERNAME))).thenReturn(true); | ||
when(clusterConfigService.getOrDefault(UserConfiguration.class, UserConfiguration.DEFAULT_VALUES)) | ||
.thenReturn(UserConfiguration.create(false, Duration.of(8, ChronoUnit.HOURS), true)); | ||
when(accessTokenService.create(USERNAME, tokenName)).thenReturn(accessToken); | ||
|
||
try { | ||
final Token actual = usersResource.generateNewToken(USERNAME, tokenName, "{}"); | ||
assertEquals(expected, actual); | ||
} finally { | ||
verify(subject).isPermitted(USERS_TOKENCREATE + ":" + USERNAME); | ||
verify(clusterConfigService).getOrDefault(UserConfiguration.class, UserConfiguration.DEFAULT_VALUES); | ||
verify(accessTokenService).create(USERNAME, tokenName); | ||
verifyNoMoreInteractions(clusterConfigService, accessTokenService); | ||
} | ||
} | ||
|
||
private CreateUserRequest buildCreateUserRequest() { | ||
return CreateUserRequest.create(USERNAME, PASSWORD, EMAIL, | ||
FIRST_NAME, LAST_NAME, Collections.singletonList(""), | ||
|
@@ -151,9 +236,10 @@ public TestUsersResource(UserManagementService userManagementService, PaginatedU | |
AccessTokenService accessTokenService, RoleService roleService, | ||
MongoDBSessionService sessionService, HttpConfiguration configuration, | ||
Subject subject, UserSessionTerminationService sessionTerminationService, | ||
DefaultSecurityManager securityManager, GlobalAuthServiceConfig globalAuthServiceConfig) { | ||
DefaultSecurityManager securityManager, GlobalAuthServiceConfig globalAuthServiceConfig, | ||
ClusterConfigService clusterConfigService) { | ||
super(userManagementService, paginatedUserService, accessTokenService, roleService, sessionService, | ||
sessionTerminationService, securityManager, globalAuthServiceConfig); | ||
sessionTerminationService, securityManager, globalAuthServiceConfig, clusterConfigService); | ||
this.subject = subject; | ||
super.configuration = configuration; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary
eq()
:Do the same for other instances in this file.