Remove identity default value. (#10108)

This commit is contained in:
杨翊 SionYang 2023-03-16 11:08:33 +08:00 committed by GitHub
parent 214e0c9077
commit cdd2be65af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 7 deletions

View File

@ -16,12 +16,14 @@
package com.alibaba.nacos.auth.config; package com.alibaba.nacos.auth.config;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.event.ServerConfigChangeEvent; import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
import com.alibaba.nacos.common.notify.Event; import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.constant.Constants; import com.alibaba.nacos.plugin.auth.constant.Constants;
import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.PropertiesUtil; import com.alibaba.nacos.sys.utils.PropertiesUtil;
@ -30,6 +32,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -80,6 +83,24 @@ public class AuthConfigs extends Subscriber<ServerConfigChangeEvent> {
refreshPluginProperties(); refreshPluginProperties();
} }
/**
* Validate auth config.
*
* @throws NacosException If the config is not valid.
*/
@PostConstruct
public void validate() throws NacosException {
if (!authEnabled) {
return;
}
if (StringUtils.isEmpty(nacosAuthSystemType)) {
throw new NacosException(AuthErrorCode.INVALID_TYPE.getCode(), AuthErrorCode.INVALID_TYPE.getMsg());
}
if (StringUtils.isEmpty(serverIdentityKey) || StringUtils.isEmpty(serverIdentityValue)) {
throw new NacosException(AuthErrorCode.EMPTY_IDENTITY.getCode(), AuthErrorCode.EMPTY_IDENTITY.getMsg());
}
}
private void refreshPluginProperties() { private void refreshPluginProperties() {
try { try {
Map<String, Properties> newProperties = new HashMap<>(1); Map<String, Properties> newProperties = new HashMap<>(1);

View File

@ -0,0 +1,51 @@
/*
* Copyright 1999-2021 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.config;
/**
* Auth relative error codes, start with 5000X.
*
* @author xiweng.yy
*/
public enum AuthErrorCode {
/**
* invalid auth type.
*/
INVALID_TYPE(50001,
"Invalid auth type, Please set `nacos.core.auth.system.type`, detail: https://nacos.io/zh-cn/docs/v2/plugin/auth-plugin.html"),
EMPTY_IDENTITY(50002,
"Empty identity, Please set `nacos.core.auth.server.identity.key` and `nacos.core.auth.server.identity.value`, detail: https://nacos.io/zh-cn/docs/v2/guide/user/auth.html");
private final Integer code;
private final String msg;
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
AuthErrorCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}

View File

@ -120,8 +120,8 @@ nacos.core.auth.enable.userAgentAuthWhite=false
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false. ### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server. ### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=serverIdentity nacos.core.auth.server.identity.key=
nacos.core.auth.server.identity.value=security nacos.core.auth.server.identity.value=
### worked when nacos.core.auth.system.type=nacos ### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds: ### The token expiration in seconds:

View File

@ -145,8 +145,8 @@ nacos.core.auth.enable.userAgentAuthWhite=false
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false. ### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server. ### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=serverIdentity nacos.core.auth.server.identity.key=
nacos.core.auth.server.identity.value=security nacos.core.auth.server.identity.value=
### worked when nacos.core.auth.system.type=nacos ### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds: ### The token expiration in seconds:

View File

@ -154,7 +154,7 @@ nacos.core.auth.enabled=false
nacos.core.auth.default.token.expire.seconds=18000 nacos.core.auth.default.token.expire.seconds=18000
### The default token: ### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789 #nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay. ### 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 nacos.core.auth.caching.enabled=true
@ -164,8 +164,8 @@ nacos.core.auth.enable.userAgentAuthWhite=false
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false. ### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server. ### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=serverIdentity #nacos.core.auth.server.identity.key=example
nacos.core.auth.server.identity.value=security #nacos.core.auth.server.identity.value=example
#*************** Istio Related Configurations ***************# #*************** Istio Related Configurations ***************#
### If turn on the MCP server: ### If turn on the MCP server: