Skip to content
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

fix: preventing access to login page after Halo setup #6865

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.experimental.Accessors;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
Expand All @@ -29,6 +30,7 @@
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.index.query.QueryFactory;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.JsonUtils;

Expand All @@ -42,6 +44,7 @@
@RequiredArgsConstructor
public class AuthProviderServiceImpl implements AuthProviderService {
private final ReactiveExtensionClient client;
private final ObjectProvider<SystemConfigurableEnvironmentFetcher> environmentFetcherProvider;

@Override
public Mono<AuthProvider> enable(String name) {
Expand Down Expand Up @@ -146,7 +149,7 @@ public int compareTo(@NonNull AuthProviderWithPriority o) {
}

private Mono<List<SystemSetting.AuthProviderState>> fetchProviderStates() {
return fetchSystemConfigMap()
return getSystemConfigMap()
.map(AuthProviderServiceImpl::getAuthProviderConfig)
.map(SystemSetting.AuthProvider::getStates)
.defaultIfEmpty(List.of())
Expand Down Expand Up @@ -218,7 +221,7 @@ private static SystemSetting.AuthProvider getAuthProviderConfig(ConfigMap config
}

private Mono<ConfigMap> updateAuthProviderEnabled(String name, boolean enabled) {
return Mono.defer(() -> fetchSystemConfigMap()
return Mono.defer(() -> getSystemConfigMap()
.flatMap(configMap -> {
var providerConfig = getAuthProviderConfig(configMap);
var stateToFoundOpt = providerConfig.getStates()
Expand All @@ -244,7 +247,12 @@ private Mono<ConfigMap> updateAuthProviderEnabled(String name, boolean enabled)
.filter(OptimisticLockingFailureException.class::isInstance));
}

Mono<ConfigMap> fetchSystemConfigMap() {
return client.fetch(ConfigMap.class, SystemSetting.SYSTEM_CONFIG);
private Mono<ConfigMap> getSystemConfigMap() {
var systemFetcher = environmentFetcherProvider.getIfUnique();
if (systemFetcher == null) {
return Mono.error(
new IllegalStateException("No SystemConfigurableEnvironmentFetcher found"));
}
return systemFetcher.getConfigMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ data:
{
"search-engine": ["search-engine-lucene"]
}
authProvider: |
{
"states": [{
"name": "local",
"enabled": true,
"priority": 0
}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import java.util.HashMap;
import org.json.JSONException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.data.domain.Sort;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand All @@ -28,6 +30,7 @@
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.JsonUtils;

Expand All @@ -43,9 +46,20 @@ class AuthProviderServiceImplTest {
@Mock
ReactiveExtensionClient client;

@Mock
ObjectProvider<SystemConfigurableEnvironmentFetcher> systemFetchProvider;

@Mock
SystemConfigurableEnvironmentFetcher systemConfigFetcher;

@InjectMocks
AuthProviderServiceImpl authProviderService;

@BeforeEach
void setUp() {
when(systemFetchProvider.getIfUnique()).thenReturn(systemConfigFetcher);
}

@Test
void testEnable() throws JSONException {
// Create a test auth provider
Expand Down Expand Up @@ -199,7 +213,7 @@ AuthProvider createAuthProvider(String name) {
void pileSystemConfigMap() {
ConfigMap configMap = new ConfigMap();
configMap.setData(new HashMap<>());
when(client.fetch(eq(ConfigMap.class), eq(SystemSetting.SYSTEM_CONFIG)))
when(systemConfigFetcher.getConfigMap())
.thenReturn(Mono.just(configMap));
}
}
Loading