From 645cd4a69acc2e1f86c63d890ab3fce1dd66d835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=96=87=E8=B6=85?= Date: Thu, 3 Nov 2022 13:48:59 +0800 Subject: [PATCH] [ISSUE #8622] sub pr of 9356 - auth module (#9447) * [ISSUE #8622] sub pr of 9356 - auth module - Replace system.getProperties and system.getEnv with NacosClientProperties - remove address server controller IT relate #9356 * [ISSUE #8622] rollback about app name * [ISSUE #8622] remove unused import * [ISSUE #8622] retry ci --- .../address/AddressServerControllerTests.java | 248 ------------------ .../auth/ram/identify/CredentialService.java | 3 +- .../auth/ram/identify/CredentialWatcher.java | 7 +- .../client/auth/ram/identify/StsConfig.java | 11 +- 4 files changed, 12 insertions(+), 257 deletions(-) delete mode 100644 address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java diff --git a/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java b/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java deleted file mode 100644 index 285b279ba7b..00000000000 --- a/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright 1999-2018 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.address; - -import com.alibaba.nacos.common.codec.Base64; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.RequestEntity; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.LinkedMultiValueMap; - -import java.nio.charset.StandardCharsets; -import java.util.concurrent.TimeUnit; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = { - "spring.security.user.password=123456", "spring.security.user.name=user"}) -public class AddressServerControllerTests { - - private static final String PRODUCT_CONFIG = "config"; - - private static final String PRODUCT_NAMING = "naming"; - - private static final String HTTP_BASIC_INFO = getHttpBasicInfo(); - - @Autowired - private TestRestTemplate restTemplate; - - @BeforeClass - public static void before() { - System.setProperty("nacos.standalone", "true"); - System.setProperty("embeddedStorage", "true"); - } - - @AfterClass - public static void teardown() { - System.clearProperty("nacos.standalone"); - System.clearProperty("embeddedStorage"); - } - - private static String getHttpBasicInfo() { - String userName = "user"; - String password = "123456"; - - String info = userName + ":" + password; - - final byte[] bytes = Base64.encodeBase64(info.getBytes(StandardCharsets.UTF_8)); - - return "Basic " + new String(bytes, StandardCharsets.UTF_8); - } - - @Test - public void postClusterWithoutLogin() { - - String ips = "127.0.0.100,127.0.0.102,127.0.0.104"; - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", ips); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class); - - Assert.assertEquals(postClusterResponseEntity.getStatusCode(), HttpStatus.UNAUTHORIZED); - } - - @Test - public void postCluster() throws InterruptedException { - - String ips = "127.0.0.100,127.0.0.102,127.0.0.104"; - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", ips); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity getClusterResponseEntity = restTemplate.exchange( - RequestEntity.get("/nacos/serverlist").build(), String.class); - - Assert.assertNotNull(getClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue()); - - } - - @Test - public void deleteClusterWithoutLogin() { - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", "127.0.0.104"); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class); - Assert.assertEquals(postClusterResponseEntity.getStatusCode(), HttpStatus.UNAUTHORIZED); - } - - @Test - public void deleteCluster() throws InterruptedException { - - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", "127.0.0.104"); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity deleteClusterResponseEntity = restTemplate.exchange( - RequestEntity.delete("/nacos/v1/as/nodes?ips={ips}", "127.0.0.104") - .header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO).build(), String.class); - - Assert.assertNotNull(deleteClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue()); - } - - @Test - public void postClusterWithProduct() throws InterruptedException { - - LinkedMultiValueMap params = new LinkedMultiValueMap<>(2); - - String ips = "127.0.0.101,127.0.0.102,127.0.0.103"; - params.add("ips", ips); - params.add("product", PRODUCT_CONFIG); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity getClusterResponseEntity = restTemplate.exchange( - RequestEntity.get("/{product}/serverlist", PRODUCT_CONFIG).build(), String.class); - - Assert.assertNotNull(getClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue()); - - final String body = getClusterResponseEntity.getBody(); - Assert.assertNotNull(body); - } - - @Test - public void deleteClusterWithProduct() throws InterruptedException { - - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", "127.0.0.104"); - params.add("product", PRODUCT_CONFIG); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity deleteClusterResponseEntity = restTemplate.exchange( - RequestEntity.delete("/nacos/v1/as/nodes?product={product}&ips={ips}", PRODUCT_CONFIG, "127.0.0.104") - .header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO).build(), String.class); - - Assert.assertNotNull(deleteClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue()); - } - - @Test - public void postClusterWithProductAndCluster() throws InterruptedException { - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - - String ips = "127.0.0.100,127.0.0.200,127.0.0.31"; - - params.add("ips", ips); - params.add("product", PRODUCT_NAMING); - params.add("cluster", "cluster01"); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity getClusterResponseEntity = restTemplate.exchange( - RequestEntity.get("/{product}/{cluster}", PRODUCT_NAMING, "cluster01").build(), String.class); - - Assert.assertNotNull(getClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue()); - - final String body = getClusterResponseEntity.getBody(); - Assert.assertNotNull(body); - } - - @Test - public void deleteClusterWithProductAndCluster() throws InterruptedException { - - LinkedMultiValueMap params = new LinkedMultiValueMap<>(1); - params.add("ips", "127.0.0.104"); - params.add("product", PRODUCT_NAMING); - params.add("cluster", "cluster01"); - - final ResponseEntity postClusterResponseEntity = restTemplate.exchange( - RequestEntity.post("/nacos/v1/as/nodes").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO) - .body(params), String.class); - Assert.assertNotNull(postClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue()); - - TimeUnit.MILLISECONDS.sleep(500L); - - final ResponseEntity deleteClusterResponseEntity = restTemplate.exchange( - RequestEntity.delete("/nacos/v1/as/nodes?product={product}&cluster={cluster}&ips={ips}", PRODUCT_NAMING, - "cluster01", "127.0.0.104").header(HttpHeaders.AUTHORIZATION, HTTP_BASIC_INFO).build(), - String.class); - - Assert.assertNotNull(deleteClusterResponseEntity); - Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue()); - } - -} diff --git a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialService.java b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialService.java index e02d79745cc..cdf55b8e967 100644 --- a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialService.java +++ b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialService.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.auth.ram.identify; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.common.utils.StringUtils; import org.slf4j.Logger; @@ -43,7 +44,7 @@ public final class CredentialService implements SpasCredentialLoader { private CredentialService(String appName) { if (appName == null) { - String value = System.getProperty(IdentifyConstants.PROJECT_NAME_PROPERTY); + String value = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.PROJECT_NAME_PROPERTY); if (StringUtils.isNotEmpty(value)) { appName = value; } diff --git a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialWatcher.java b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialWatcher.java index eaa61f563a3..dbac9169ad9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialWatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/CredentialWatcher.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.auth.ram.identify; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.common.executor.ExecutorFactory; import com.alibaba.nacos.common.executor.NameThreadFactory; @@ -113,7 +114,7 @@ private void loadCredential(boolean init) { } if (propertyPath == null || propertyPath.isEmpty()) { - String value = System.getProperty("spas.identity"); + String value = NacosClientProperties.PROTOTYPE.getProperty("spas.identity"); if (StringUtils.isNotEmpty(value)) { propertyPath = value; } @@ -157,8 +158,8 @@ private void loadCredential(boolean init) { String tenantId = null; if (propertiesIS == null) { propertyPath = null; - accessKey = System.getenv(IdentifyConstants.ENV_ACCESS_KEY); - secretKey = System.getenv(IdentifyConstants.ENV_SECRET_KEY); + accessKey = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.ENV_ACCESS_KEY); + secretKey = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.ENV_SECRET_KEY); if (accessKey == null && secretKey == null) { if (init) { SPAS_LOGGER.info("{} No credential found", appName); diff --git a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/StsConfig.java b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/StsConfig.java index 3d197c6e45f..06c19002194 100644 --- a/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/StsConfig.java +++ b/client/src/main/java/com/alibaba/nacos/client/auth/ram/identify/StsConfig.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.auth.ram.identify; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.common.utils.StringUtils; /** @@ -56,27 +57,27 @@ private static class Singleton { } private StsConfig() { - String ramRoleName = System.getProperty(IdentifyConstants.RAM_ROLE_NAME_PROPERTY); + String ramRoleName = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.RAM_ROLE_NAME_PROPERTY); if (!StringUtils.isBlank(ramRoleName)) { setRamRoleName(ramRoleName); } - String timeToRefreshInMillisecond = System.getProperty(IdentifyConstants.REFRESH_TIME_PROPERTY); + String timeToRefreshInMillisecond = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.REFRESH_TIME_PROPERTY); if (!StringUtils.isBlank(timeToRefreshInMillisecond)) { setTimeToRefreshInMillisecond(Integer.parseInt(timeToRefreshInMillisecond)); } - String securityCredentials = System.getProperty(IdentifyConstants.SECURITY_PROPERTY); + String securityCredentials = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.SECURITY_PROPERTY); if (!StringUtils.isBlank(securityCredentials)) { setSecurityCredentials(securityCredentials); } - String securityCredentialsUrl = System.getProperty(IdentifyConstants.SECURITY_URL_PROPERTY); + String securityCredentialsUrl = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.SECURITY_URL_PROPERTY); if (!StringUtils.isBlank(securityCredentialsUrl)) { setSecurityCredentialsUrl(securityCredentialsUrl); } - String cacheSecurityCredentials = System.getProperty(IdentifyConstants.SECURITY_CACHE_PROPERTY); + String cacheSecurityCredentials = NacosClientProperties.PROTOTYPE.getProperty(IdentifyConstants.SECURITY_CACHE_PROPERTY); if (!StringUtils.isBlank(cacheSecurityCredentials)) { setCacheSecurityCredentials(Boolean.parseBoolean(cacheSecurityCredentials)); }