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

Upgrade to 1.4.7 & add new property to support agent situation. #11644

Merged
merged 4 commits into from
Jan 12, 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
5 changes: 5 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public class PropertyKeyConst {

public static final String PUSH_RECEIVER_UDP_PORT = "push.receiver.udp.port";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
public static final String IS_USE_RAM_INFO_PARSING = "isUseRamInfoParsing";

/**
* Get the key value of some variable value from the system property.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public interface SystemPropertyKeyConst {
* It is also supported by the -D parameter.
*/
String IS_USE_ENDPOINT_PARSING_RULE = "nacos.use.endpoint.parsing.rule";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
String IS_USE_RAM_INFO_PARSING = "nacos.use.ram.info.parsing";
}
5 changes: 5 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ public class Constants {

public static final String ALL_PATTERN = "*";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
public static final String DEFAULT_USE_RAM_INFO_PARSING = "true";

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.RamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpClientConfig;
Expand Down Expand Up @@ -322,19 +323,8 @@ private void initAkSk(Properties properties) {
StsConfig.getInstance().setRamRoleName(ramRoleName);
}

String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk();
} else {
accessKey = ak;
}

String sk = properties.getProperty(PropertyKeyConst.SECRET_KEY);
if (StringUtils.isBlank(sk)) {
secretKey = SpasAdapter.getSk();
} else {
secretKey = sk;
}
accessKey = RamUtil.getAccessKey(properties);
secretKey = RamUtil.getSecretKey(properties);
}

private void initMaxRetry(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.AppNameUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.client.utils.RamUtil;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
Expand Down Expand Up @@ -70,7 +70,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -748,33 +747,16 @@ private void injectRoleName() {

public String getAccessKey() {
if (properties == null) {

return SpasAdapter.getAk();
}

return TemplateUtils
.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ACCESS_KEY), new Callable<String>() {

@Override
public String call() {
return SpasAdapter.getAk();
}
});
return RamUtil.getAccessKey(properties);
}

public String getSecretKey() {
if (properties == null) {

return SpasAdapter.getSk();
}

return TemplateUtils
.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SECRET_KEY), new Callable<String>() {
@Override
public String call() throws Exception {
return SpasAdapter.getSk();
}
});
return RamUtil.getSecretKey(properties);
}

public void setProperties(Properties properties) {
Expand Down
59 changes: 59 additions & 0 deletions client/src/main/java/com/alibaba/nacos/client/utils/RamUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2023 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.client.utils;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.Properties;

/**
* Util to get ram info, such as AK, SK and RAM role.
*
* @author xiweng.yy
*/
public class RamUtil {

public static String getAccessKey(Properties properties) {
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
Constants.DEFAULT_USE_RAM_INFO_PARSING)));

String result = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
result = SpasAdapter.getAk();
}
return result;
}

public static String getSecretKey(Properties properties) {
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
Constants.DEFAULT_USE_RAM_INFO_PARSING)));

String result = properties.getProperty(PropertyKeyConst.SECRET_KEY);
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
result = SpasAdapter.getSk();
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 1999-2023 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.client.config.http;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.net.URL;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@RunWith(MockitoJUnitRunner.class)
public class ServerHttpAgentTest {

@Mock
private ServerListManager serverListManager;

private Properties properties;

ServerHttpAgent serverHttpAgent;

@Before
public void setUp() throws Exception {
properties = new Properties();
}

@After
public void tearDown() throws Exception {
SpasAdapter.freeCredentialInstance();
System.clearProperty("spas.identity");
}

@Test
public void testInitAkSkFromProperties() throws NoSuchFieldException, IllegalAccessException {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "akProperties");
properties.setProperty(PropertyKeyConst.SECRET_KEY, "skProperties");
serverHttpAgent = new ServerHttpAgent(serverListManager, properties);
assertEquals("akProperties", getStringFieldValue("accessKey"));
assertEquals("skProperties", getStringFieldValue("secretKey"));
}

@Test
public void testInitAkSkFromSpasProperties() throws NoSuchFieldException, IllegalAccessException {
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
serverHttpAgent = new ServerHttpAgent(serverListManager, properties);
assertEquals("akFromSpas", getStringFieldValue("accessKey"));
assertEquals("skFromSpas", getStringFieldValue("secretKey"));
}

@Test
public void testInitAkSkFromPropertiesWithUseRamInfoParsingFalse() throws NoSuchFieldException, IllegalAccessException {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "akProperties");
properties.setProperty(PropertyKeyConst.SECRET_KEY, "skProperties");
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
serverHttpAgent = new ServerHttpAgent(serverListManager, properties);
assertEquals("akProperties", getStringFieldValue("accessKey"));
assertEquals("skProperties", getStringFieldValue("secretKey"));
}

@Test
public void testInitAkSkFromSpasWithUseRamInfoParsingFalse() throws NoSuchFieldException, IllegalAccessException {
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
serverHttpAgent = new ServerHttpAgent(serverListManager, properties);
assertNull(getStringFieldValue("accessKey"));
assertNull(getStringFieldValue("secretKey"));
}

private String getStringFieldValue(String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = ServerHttpAgent.class.getDeclaredField(fieldName);
field.setAccessible(true);
return (String) field.get(serverHttpAgent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 1999-2023 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.client.naming.net;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.client.config.http.ServerHttpAgentTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.URL;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class NamingProxyTest {

private Properties properties;

NamingProxy namingProxy;

@Before
public void setUp() throws Exception {
properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
}

@After
public void tearDown() throws Exception {
if (null != namingProxy) {
namingProxy.shutdown();
}
}

@Test
public void testGetAccessKeFromProperties() {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "akProperties");
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("akProperties", namingProxy.getAccessKey());
}

@Test
public void testGetSecretKeyFromProperties() {
properties.setProperty(PropertyKeyConst.SECRET_KEY, "skProperties");
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("skProperties", namingProxy.getSecretKey());
}

@Test
public void testGetAccessKeFromSpas() {
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("akFromSpas", namingProxy.getAccessKey());
}

@Test
public void testGetSecretKeyFromSpas() {
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("skFromSpas", namingProxy.getSecretKey());
}

@Test
public void testGetAccessKeFromPropertiesWithUseRamInfoParsingFalse() {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "akProperties");
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("akProperties", namingProxy.getAccessKey());
}

@Test
public void testGetSecretKeyFromPropertiesWithUseRamInfoParsingFalse() {
properties.setProperty(PropertyKeyConst.SECRET_KEY, "skProperties");
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertEquals("skProperties", namingProxy.getSecretKey());
}

@Test
public void testGetAccessKeFromSpasWithUseRamInfoParsingFalse() {
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertNull(namingProxy.getAccessKey());
}

@Test
public void testGetSecretKeyFromSpasWithUseRamInfoParsingFalse() {
properties.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
URL url = ServerHttpAgentTest.class.getResource("/test_ram_info.properties");
System.setProperty("spas.identity", url.getPath());
namingProxy = new NamingProxy("", "", "127.0.0.1:8848", properties);
assertNull(namingProxy.getSecretKey());
}
}
Loading
Loading