Search in sources :

Example 16 with UserService

use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.

the class UserContextTest method runAs.

@Test
void runAs() {
    // Simulate what we do in the DefaultSecurityManagerProvider
    DefaultSecurityManager sm = new DefaultSecurityManager();
    SecurityUtils.setSecurityManager(sm);
    final DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
    final DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator() {

        @Override
        public boolean isSessionStorageEnabled(Subject subject) {
            // save to session if we already have a session. do not create on just for saving the subject
            return subject.getSession(false) != null;
        }
    };
    sessionStorageEvaluator.setSessionStorageEnabled(false);
    subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
    sm.setSubjectDAO(subjectDAO);
    final User user = new UserImpl(mock(PasswordAlgorithmFactory.class), mock(Permissions.class), ImmutableMap.of());
    when(userService.load(anyString())).thenReturn(user);
    when(userService.loadById(anyString())).thenReturn(user);
    final String USERID = "123456";
    UserContext.<Void>runAs(USERID, () -> {
        final UserContext userContext = new UserContext.Factory(userService).create();
        assertThat(userContext.getUserId()).isEqualTo(USERID);
        assertThat(userContext.getUser()).isEqualTo(user);
        return null;
    });
}
Also used : DefaultSubjectDAO(org.apache.shiro.mgt.DefaultSubjectDAO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) User(org.graylog2.plugin.database.users.User) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DefaultSessionStorageEvaluator(org.apache.shiro.mgt.DefaultSessionStorageEvaluator) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test)

Example 17 with UserService

use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.

the class ViewFacadeTest method setUp.

@Before
public void setUp() {
    objectMapper.registerSubtypes(new NamedType(AggregationConfigDTO.class, AggregationConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(MessageListConfigDTO.class, MessageListConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(TimeHistogramConfigDTO.class, TimeHistogramConfigDTO.NAME));
    objectMapper.registerSubtypes(new NamedType(OrFilter.class, OrFilter.NAME));
    objectMapper.registerSubtypes(new NamedType(StreamFilter.class, StreamFilter.NAME));
    objectMapper.registerSubtypes(new NamedType(QueryStringFilter.class, QueryStringFilter.NAME));
    objectMapper.registerSubtypes(new NamedType(AutoIntervalDTO.class, AutoIntervalDTO.type));
    objectMapper.registerSubtypes(MessageListEntity.class);
    objectMapper.registerSubtypes(PivotEntity.class);
    objectMapper.registerSubtypes(EventListEntity.class);
    objectMapper.registerSubtypes(MessageList.class);
    objectMapper.registerSubtypes(Pivot.class);
    objectMapper.registerSubtypes(EventList.class);
    final MongoConnection mongoConnection = mongodb.mongoConnection();
    final MongoJackObjectMapperProvider mapper = new MongoJackObjectMapperProvider(objectMapper);
    searchDbService = new TestSearchDBService(mongoConnection, mapper);
    viewService = new TestViewService(mongoConnection, mapper, null);
    viewSummaryService = new TestViewSummaryService(mongoConnection, mapper);
    userService = mock(UserService.class);
    facade = new SearchFacade(objectMapper, searchDbService, viewService, viewSummaryService, userService);
}
Also used : UserService(org.graylog2.shared.users.UserService) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) TimeHistogramConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.TimeHistogramConfigDTO) AutoIntervalDTO(org.graylog.plugins.views.search.views.widgets.aggregation.AutoIntervalDTO) OrFilter(org.graylog.plugins.views.search.filter.OrFilter) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) QueryStringFilter(org.graylog.plugins.views.search.filter.QueryStringFilter) AggregationConfigDTO(org.graylog.plugins.views.search.views.widgets.aggregation.AggregationConfigDTO) MessageListConfigDTO(org.graylog.plugins.views.search.views.widgets.messagelist.MessageListConfigDTO) MongoConnection(org.graylog2.database.MongoConnection) Before(org.junit.Before)

Example 18 with UserService

use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.

the class ProvisionerServiceTest method testFullNameOnlySuccess.

@Test
public void testFullNameOnlySuccess() throws ValidationException {
    when(authServiceBackend.backendId()).thenReturn(BACKEND_ID);
    when(authServiceBackend.backendType()).thenReturn(BACKEND_TYPE);
    final UserDetails.Builder detailsBuilder = provisionerService.newDetails(authServiceBackend);
    assertNotNull(detailsBuilder);
    detailsBuilder.fullName(FULL_NAME).base64AuthServiceUid("id").username(USERNAME).accountIsEnabled(true).email(EMAIL).defaultRoles(Collections.emptySet());
    final UserDetails userDetails = detailsBuilder.build();
    assertEquals(BACKEND_ID, userDetails.authServiceId());
    assertEquals(BACKEND_TYPE, userDetails.authServiceType());
    final User user = mock(User.class);
    when(userService.create()).thenReturn(user);
    when(userService.save(isA(User.class))).thenReturn(USER_ID);
    provisionerService.provision(userDetails);
    verify(userService, times(1)).save(isA(User.class));
    verify(user, times(1)).setFullName(FULL_NAME);
}
Also used : User(org.graylog2.plugin.database.users.User) Test(org.junit.Test)

Example 19 with UserService

use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.

the class MigrationHelpersTest method ensureUserWithoutExpectedRoles.

@Test
public void ensureUserWithoutExpectedRoles() throws Exception {
    final Permissions permissions = new Permissions(ImmutableSet.of());
    final User existingUser = newUser(permissions);
    existingUser.setName("test-user");
    existingUser.setFirstLastFullNames("Test", "User");
    existingUser.setPassword("password");
    existingUser.setEmail("test@example.com");
    existingUser.setTimeZone(DateTimeZone.UTC);
    // Set invalid role IDs so the use gets updated
    existingUser.setRoleIds(ImmutableSet.of());
    when(userService.load("test-user")).thenReturn(existingUser);
    when(userService.save(any(User.class))).thenReturn("new-id");
    assertThat(migrationHelpers.ensureUser("test-user", "pass", "Test", "User", "test@example.com", ImmutableSet.of("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002"))).isEqualTo("new-id");
    final ArgumentCaptor<User> userArg = ArgumentCaptor.forClass(User.class);
    verify(userService, times(1)).save(userArg.capture());
    assertThat(userArg.getValue()).satisfies(user -> {
        assertThat(user.getName()).describedAs("user name").isEqualTo("test-user");
        assertThat(user.getFullName()).describedAs("user full-name").isEqualTo("Test User");
        assertThat(user.getHashedPassword()).describedAs("user hashed password").isNotBlank();
        assertThat(user.getEmail()).describedAs("user email").isEqualTo("test@example.com");
        assertThat(user.isReadOnly()).describedAs("user is read-only").isFalse();
        assertThat(user.getPermissions()).describedAs("user permissions").containsOnlyElementsOf(permissions.userSelfEditPermissions("test-user"));
        assertThat(user.getRoleIds()).describedAs("user roles").containsOnly("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002");
        assertThat(user.getTimeZone()).describedAs("user timezone").isEqualTo(DateTimeZone.UTC);
    });
}
Also used : User(org.graylog2.plugin.database.users.User) Permissions(org.graylog2.shared.security.Permissions) Test(org.junit.Test)

Example 20 with UserService

use of org.graylog2.shared.users.UserService in project graylog2-server by Graylog2.

the class RolesToGrantsMigrationTest method setUp.

@BeforeEach
void setUp(MongoDBTestService mongodb, MongoJackObjectMapperProvider mongoJackObjectMapperProvider, GRNRegistry grnRegistry, TestUserService userService) {
    when(permissions.readerBasePermissions()).thenReturn(ImmutableSet.of());
    when(validator.validate(any())).thenReturn(ImmutableSet.of());
    this.grnRegistry = grnRegistry;
    roleService = new RoleServiceImpl(mongodb.mongoConnection(), mongoJackObjectMapperProvider, permissions, validator);
    dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
    this.userService = userService;
    DBGrantService dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
    migration = new RolesToGrantsMigration(roleService, userService, dbGrantService, grnRegistry, "admin");
}
Also used : DBGrantService(org.graylog.security.DBGrantService) RoleServiceImpl(org.graylog2.users.RoleServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

User (org.graylog2.plugin.database.users.User)11 Before (org.junit.Before)10 Permissions (org.graylog2.shared.security.Permissions)8 Test (org.junit.Test)8 MongoConnection (org.graylog2.database.MongoConnection)5 UserService (org.graylog2.shared.users.UserService)5 UserImpl (org.graylog2.users.UserImpl)4 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)3 HashMap (java.util.HashMap)3 GRN (org.graylog.grn.GRN)3 MongoJackObjectMapperProvider (org.graylog2.bindings.providers.MongoJackObjectMapperProvider)3 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)3 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 DBNotificationService (org.graylog.events.notifications.DBNotificationService)2 NotificationResourceHandler (org.graylog.events.notifications.NotificationResourceHandler)2 DBEventDefinitionService (org.graylog.events.processor.DBEventDefinitionService)2 OrFilter (org.graylog.plugins.views.search.filter.OrFilter)2 QueryStringFilter (org.graylog.plugins.views.search.filter.QueryStringFilter)2 StreamFilter (org.graylog.plugins.views.search.filter.StreamFilter)2 AggregationConfigDTO (org.graylog.plugins.views.search.views.widgets.aggregation.AggregationConfigDTO)2