Skip to content

Commit ad83ff0

Browse files
authored
Develop support ram info switch (#12382)
* add new property to support agent situation. * for checkstyle. * Upgrade cheery pick ut to junit5. * add ignored lefthook.yml. * add ignored lefthook.yml.
1 parent ed7bd03 commit ad83ff0

File tree

8 files changed

+147
-4
lines changed

8 files changed

+147
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ test/logs
1818
derby.log
1919
yarn.lock
2020
.flattened-pom.xml
21+
lefthook.yml

api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public class PropertyKeyConst {
9595

9696
public static final String LOG_ALL_PROPERTIES = "logAllProperties";
9797

98+
/**
99+
* Since 2.3.3, For some situation like java agent using nacos-client which can't use env ram info.
100+
*/
101+
public static final String IS_USE_RAM_INFO_PARSING = "isUseRamInfoParsing";
102+
98103
/**
99104
* Get the key value of some variable value from the system property.
100105
*/

api/src/main/java/com/alibaba/nacos/api/SystemPropertyKeyConst.java

+5
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public interface SystemPropertyKeyConst {
4646
* It is also supported by the -D parameter.
4747
*/
4848
String IS_USE_ENDPOINT_PARSING_RULE = "nacos.use.endpoint.parsing.rule";
49+
50+
/**
51+
* Since 2.3.3, For some situation like java agent using nacos-client which can't use env ram info.
52+
*/
53+
String IS_USE_RAM_INFO_PARSING = "nacos.use.ram.info.parsing";
4954
}

api/src/main/java/com/alibaba/nacos/api/common/Constants.java

+5
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ public class Constants {
247247

248248
public static final String CONFIG_GRAY_LABEL = "nacos.config.gray.label";
249249

250+
/**
251+
* Since 2.3.3, For some situation like java agent using nacos-client which can't use env ram info.
252+
*/
253+
public static final String DEFAULT_USE_RAM_INFO_PARSING = "true";
254+
250255
/**
251256
* The constants in config directory.
252257
*/

client/src/main/java/com/alibaba/nacos/client/auth/ram/RamClientAuthServiceImpl.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.alibaba.nacos.client.auth.ram.injector.AbstractResourceInjector;
2323
import com.alibaba.nacos.client.auth.ram.injector.ConfigResourceInjector;
2424
import com.alibaba.nacos.client.auth.ram.injector.NamingResourceInjector;
25+
import com.alibaba.nacos.client.auth.ram.utils.RamUtil;
2526
import com.alibaba.nacos.client.auth.ram.utils.SpasAdapter;
2627
import com.alibaba.nacos.common.utils.StringUtils;
2728
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
@@ -76,13 +77,11 @@ private void loadRoleName(Properties properties) {
7677
}
7778

7879
private void loadAccessKey(Properties properties) {
79-
String accessKey = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
80-
ramContext.setAccessKey(StringUtils.isBlank(accessKey) ? SpasAdapter.getAk() : accessKey);
80+
ramContext.setAccessKey(RamUtil.getAccessKey(properties));
8181
}
8282

8383
private void loadSecretKey(Properties properties) {
84-
String secretKey = properties.getProperty(PropertyKeyConst.SECRET_KEY);
85-
ramContext.setSecretKey(StringUtils.isBlank(secretKey) ? SpasAdapter.getSk() : secretKey);
84+
ramContext.setSecretKey(RamUtil.getSecretKey(properties));
8685
}
8786

8887
private void loadRegionId(Properties properties) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.client.auth.ram.utils;
18+
19+
import com.alibaba.nacos.api.PropertyKeyConst;
20+
import com.alibaba.nacos.api.SystemPropertyKeyConst;
21+
import com.alibaba.nacos.api.common.Constants;
22+
import com.alibaba.nacos.common.utils.StringUtils;
23+
24+
import java.util.Properties;
25+
26+
/**
27+
* Util to get ram info, such as AK, SK and RAM role.
28+
*
29+
* @author xiweng.yy
30+
*/
31+
public class RamUtil {
32+
33+
public static String getAccessKey(Properties properties) {
34+
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
35+
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
36+
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
37+
Constants.DEFAULT_USE_RAM_INFO_PARSING)));
38+
39+
String result = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
40+
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
41+
result = SpasAdapter.getAk();
42+
}
43+
return result;
44+
}
45+
46+
public static String getSecretKey(Properties properties) {
47+
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
48+
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
49+
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
50+
Constants.DEFAULT_USE_RAM_INFO_PARSING)));
51+
52+
String result = properties.getProperty(PropertyKeyConst.SECRET_KEY);
53+
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
54+
result = SpasAdapter.getSk();
55+
}
56+
return result;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.client.auth.ram.utils;
18+
19+
import com.alibaba.nacos.api.PropertyKeyConst;
20+
import com.alibaba.nacos.client.auth.ram.identify.CredentialService;
21+
import com.alibaba.nacos.client.auth.ram.identify.Credentials;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.Properties;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertNull;
30+
31+
public class RamUtilTest {
32+
33+
private Properties properties;
34+
35+
@BeforeEach
36+
public void setUp() throws Exception {
37+
SpasAdapter.freeCredentialInstance();
38+
Credentials credentials = new Credentials("spasAk", "spasSk", "spasNamespaceId");
39+
CredentialService.getInstance().setStaticCredential(credentials);
40+
properties = new Properties();
41+
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "userAk");
42+
properties.setProperty(PropertyKeyConst.SECRET_KEY, "userSk");
43+
}
44+
45+
@AfterEach
46+
public void tearDown() throws Exception {
47+
SpasAdapter.freeCredentialInstance();
48+
}
49+
50+
@Test
51+
public void testGetAccessKeyWithUserAkSk() {
52+
assertEquals("userAk", RamUtil.getAccessKey(properties));
53+
assertEquals("userSk", RamUtil.getSecretKey(properties));
54+
}
55+
56+
@Test
57+
public void testGetAccessKeyWithSpasAkSk() {
58+
assertEquals("spasAk", RamUtil.getAccessKey(new Properties()));
59+
assertEquals("spasSk", RamUtil.getSecretKey(new Properties()));
60+
}
61+
62+
@Test
63+
public void testGetAccessKeyWithoutSpasAkSk() {
64+
Properties properties1 = new Properties();
65+
properties1.setProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING, "false");
66+
assertNull(RamUtil.getAccessKey(properties1));
67+
assertNull(RamUtil.getSecretKey(properties1));
68+
}
69+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
<exclude>**/filter-config.json</exclude>
362362
<exclude>**/disk_cache_test/**</exclude>
363363
<exclude>**/failover_test/**</exclude>
364+
<exclude>lefthook.yml</exclude>
364365
</excludes>
365366
</configuration>
366367
<executions>

0 commit comments

Comments
 (0)