Skip to content

Commit

Permalink
[ISSUE #12017] Split console authentication (#12474)
Browse files Browse the repository at this point in the history
* [ISSUE #12017] Git Test

* Git Test

* [ISSUE #12017] Fix Git Test

* Fix Git Test

* [ISSUE #12017] Splitting server and console authentication

* Add ApiType annotations

* Update configuration file to add fields

* Update console authentication status fetch

* Update `application.properties` - Reuse `nacos.core.auth.enabled` and add `nacos.core.auth.console.enabled`

* Update `AuthConfigs` - Modify `isAuthEnabled()` to manage plugin initialization

* Update `AbstractProtocolAuthService` - Implement `isAuthEnabled()` method for `Secured` annotation and configuration-based authentication

* Update `RemoteRequestAuthFilter` - Use `authConfigs.isAuthEnabled()` for initialization and `protocolAuthService.authEnabled(secured)` for authentication checks

* [ISSUE #12017] Update the location of the authentication judgment

* Update the location of the authentication judgment
  • Loading branch information
RickonZhang0929 authored Aug 27, 2024
1 parent 95e9a22 commit 59858d1
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 11 deletions.
26 changes: 21 additions & 5 deletions auth/src/main/java/com/alibaba/nacos/auth/config/AuthConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ public class AuthConfigs extends Subscriber<ServerConfigChangeEvent> {
private static Boolean cachingEnabled = null;

/**
* Whether auth enabled.
* Whether server auth enabled.
*/
@Value("${" + Constants.Auth.NACOS_CORE_AUTH_ENABLED + ":false}")
private boolean authEnabled;

/**
* Whether console auth enabled.
*/
@Value("${" + Constants.Auth.NACOS_CORE_AUTH_CONSOLE_ENABLED + ":true}")
private boolean consoleAuthEnabled;

/**
* Which auth system is in use.
*/
Expand Down Expand Up @@ -94,7 +100,7 @@ public AuthConfigs() {
*/
@PostConstruct
public void validate() throws NacosException {
if (!authEnabled) {
if (!authEnabled && !consoleAuthEnabled) {
return;
}
if (StringUtils.isEmpty(nacosAuthSystemType)) {
Expand Down Expand Up @@ -152,14 +158,23 @@ public boolean isEnableUserAgentAuthWhite() {
}

/**
* auth function is open.
* console auth function is open.
*
* @return console auth function is open
*/
public boolean isConsoleAuthEnabled() {
return consoleAuthEnabled;
}

/**
* server auth function is open.
*
* @return auth function is open
* @return server auth function is open
*/
public boolean isAuthEnabled() {
return authEnabled;
}

/**
* Whether permission information can be cached.
*
Expand Down Expand Up @@ -189,6 +204,7 @@ public static void setCachingEnabled(boolean cachingEnabled) {
public void onEvent(ServerConfigChangeEvent event) {
try {
authEnabled = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_ENABLED, Boolean.class, false);
consoleAuthEnabled = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_CONSOLE_ENABLED, Boolean.class, true);
cachingEnabled = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_CACHING_ENABLED, Boolean.class, true);
serverIdentityKey = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_KEY, "");
serverIdentityValue = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AuthModuleStateBuilder implements ModuleStateBuilder {
public ModuleState build() {
ModuleState result = new ModuleState(AUTH_MODULE);
AuthConfigs authConfigs = ApplicationUtils.getBean(AuthConfigs.class);
result.newState(AUTH_ENABLED, authConfigs.isAuthEnabled());
result.newState(AUTH_ENABLED, authConfigs.isConsoleAuthEnabled());
result.newState(LOGIN_PAGE_ENABLED, isLoginPageEnabled(authConfigs));
result.newState(AUTH_SYSTEM_TYPE, authConfigs.getNacosAuthSystemType());
result.newState(AUTH_ADMIN_REQUEST, isAdminRequest(authConfigs));
Expand Down
45 changes: 45 additions & 0 deletions auth/src/main/java/com/alibaba/nacos/auth/enums/ApiType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.alibaba.nacos.auth.enums;

/**
* The type of API.
*
* @author zhangyukun
*/
public enum ApiType {
/**
* console API.
*/
CONSOLE_API("CONSOLE_API"),
/**
* server API.
*/
OPEN_API("OPEN_API");

private final String description;

ApiType(String description) {
this.description = description;
}

@Override
public String toString() {
return description;
}
}
3 changes: 2 additions & 1 deletion console/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=nacos

### If turn on auth system:
### If turn on auth system v3:
nacos.core.auth.enabled=false
nacos.core.auth.console.enabled=true

### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=true
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.auth.HttpProtocolAuthService;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.enums.ApiType;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
Expand Down Expand Up @@ -67,7 +68,7 @@ public AuthFilter(AuthConfigs authConfigs, ControllerMethodsCache methodsCache)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

if (!authConfigs.isAuthEnabled()) {
if (!authConfigs.isConsoleAuthEnabled() && !authConfigs.isAuthEnabled()) {
chain.doFilter(request, response);
return;
}
Expand Down Expand Up @@ -108,13 +109,22 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}

if (method.isAnnotationPresent(Secured.class) && authConfigs.isAuthEnabled()) {
if (method.isAnnotationPresent(Secured.class) && (authConfigs.isConsoleAuthEnabled() || authConfigs.isAuthEnabled())) {

if (Loggers.AUTH.isDebugEnabled()) {
Loggers.AUTH.debug("auth start, request: {} {}", req.getMethod(), req.getRequestURI());
}

Secured secured = method.getAnnotation(Secured.class);
ApiType apiType = secured.apiType();
if (apiType == ApiType.CONSOLE_API && !authConfigs.isConsoleAuthEnabled()) {
chain.doFilter(request, response);
return;
}
if (apiType == ApiType.OPEN_API && !authConfigs.isAuthEnabled()) {
chain.doFilter(request, response);
return;
}
if (!protocolAuthService.enableAuth(secured)) {
chain.doFilter(request, response);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.auth.GrpcProtocolAuthService;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.enums.ApiType;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.core.context.RequestContext;
import com.alibaba.nacos.core.context.RequestContextHolder;
Expand Down Expand Up @@ -62,13 +63,20 @@ public Response filter(Request request, RequestMeta meta, Class handlerClazz) th
try {

Method method = getHandleMethod(handlerClazz);
if (method.isAnnotationPresent(Secured.class) && authConfigs.isAuthEnabled()) {
if (method.isAnnotationPresent(Secured.class) && (authConfigs.isConsoleAuthEnabled() || authConfigs.isAuthEnabled())) {

if (Loggers.AUTH.isDebugEnabled()) {
Loggers.AUTH.debug("auth start, request: {}", request.getClass().getSimpleName());
}

Secured secured = method.getAnnotation(Secured.class);
ApiType apiType = secured.apiType();
if (apiType == ApiType.CONSOLE_API && !authConfigs.isConsoleAuthEnabled()) {
return null;
}
if (apiType == ApiType.OPEN_API && !authConfigs.isAuthEnabled()) {
return null;
}
if (!protocolAuthService.enableAuth(secured)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String getAuthServiceName() {

@Override
public boolean isLoginEnabled() {
return ApplicationUtils.getBean(AuthConfigs.class).isAuthEnabled();
return ApplicationUtils.getBean(AuthConfigs.class).isConsoleAuthEnabled();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Constants {
public static class Auth {

public static final String NACOS_CORE_AUTH_ENABLED = "nacos.core.auth.enabled";

public static final String NACOS_CORE_AUTH_CONSOLE_ENABLED = "nacos.core.auth.console.enabled";

public static final String NACOS_CORE_AUTH_SYSTEM_TYPE = "nacos.core.auth.system.type";

Expand Down

0 comments on commit 59858d1

Please # to comment.