|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog2.migrations; |
| 18 | + |
| 19 | +import org.graylog2.plugin.cluster.ClusterConfigService; |
| 20 | +import org.graylog2.users.UserConfiguration; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | +import org.threeten.extra.PeriodDuration; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | +import java.time.temporal.ChronoUnit; |
| 30 | + |
| 31 | +import static org.mockito.Mockito.verify; |
| 32 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +@ExtendWith(MockitoExtension.class) |
| 36 | +class V20250804104500_TightenTokenSecurityTest { |
| 37 | + //We prepare some existing config with explicitly updated values, so we can safely check they're not touched by the migration: |
| 38 | + private final UserConfiguration existingConfig = UserConfiguration.create(true, Duration.of(10, ChronoUnit.HOURS), true, false, PeriodDuration.of(Duration.ofDays(7))); |
| 39 | + |
| 40 | + @Mock |
| 41 | + private ClusterConfigService configService; |
| 42 | + |
| 43 | + private V20250804104500_TightenTokenSecurity testee; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + testee = new V20250804104500_TightenTokenSecurity(configService); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void doNothingIfMigrationAlreadyRanSuccessfully() { |
| 52 | + setupMocks(true, false); |
| 53 | + |
| 54 | + testee.upgrade(); |
| 55 | + |
| 56 | + verify(configService).get(V20250804104500_TightenTokenSecurity.MigrationCompleted.class); |
| 57 | + verifyNoMoreInteractions(configService); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void persistDefaultValuesIfNoConfigExists() { |
| 62 | + testee = new V20250804104500_TightenTokenSecurity(configService); |
| 63 | + setupMocks(false, false); |
| 64 | + |
| 65 | + testee.upgrade(); |
| 66 | + |
| 67 | + verify(configService).get(V20250804104500_TightenTokenSecurity.MigrationCompleted.class); |
| 68 | + verify(configService).write(UserConfiguration.DEFAULT_VALUES); |
| 69 | + verify(configService).write(new V20250804104500_TightenTokenSecurity.MigrationCompleted()); |
| 70 | + verifyNoMoreInteractions(configService); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void existingConfigIsUpdatedWithStricterValues() { |
| 75 | + setupMocks(false, true); |
| 76 | + //Expected to be written - keeps existing values for globalSessionTimeout and -interval, but applies default values for token access management |
| 77 | + final UserConfiguration updated = UserConfiguration.create(existingConfig.enableGlobalSessionTimeout(), |
| 78 | + existingConfig.globalSessionTimeoutInterval(), |
| 79 | + UserConfiguration.DEFAULT_VALUES.allowAccessTokenForExternalUsers(), |
| 80 | + UserConfiguration.DEFAULT_VALUES.restrictAccessTokenToAdmins(), |
| 81 | + UserConfiguration.DEFAULT_VALUES.defaultTTLForNewTokens()); |
| 82 | + |
| 83 | + testee.upgrade(); |
| 84 | + |
| 85 | + verify(configService).get(V20250804104500_TightenTokenSecurity.MigrationCompleted.class); |
| 86 | + verify(configService).get(UserConfiguration.class); |
| 87 | + verify(configService).write(updated); |
| 88 | + verify(configService).write(new V20250804104500_TightenTokenSecurity.MigrationCompleted()); |
| 89 | + verifyNoMoreInteractions(configService); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private void setupMocks(boolean migrationAlreadyRan, boolean configExists) { |
| 94 | + if (migrationAlreadyRan) { |
| 95 | + when(configService.get(V20250804104500_TightenTokenSecurity.MigrationCompleted.class)).thenReturn(new V20250804104500_TightenTokenSecurity.MigrationCompleted()); |
| 96 | + } else { |
| 97 | + when(configService.get(V20250804104500_TightenTokenSecurity.MigrationCompleted.class)).thenReturn(null); |
| 98 | + when(configService.get(UserConfiguration.class)).thenReturn(configExists ? existingConfig : null); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments