From fe65b33399a52671662c91bbbdfcee0de7da5a8c Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Wed, 12 Jun 2024 16:09:54 +0800 Subject: [PATCH 1/2] improve environment variable implementation --- .../auth/DefaultCredentialsProvider.java | 4 +- ...nvironmentVariableCredentialsProvider.java | 6 +- .../auth/ProfileCredentialsProvider.java | 5 +- .../java/com/aliyuncs/utils/AuthUtils.java | 96 +++++-------------- .../java/com/aliyuncs/utils/EnvHelper.java | 21 ++++ .../auth/DefaultCredentialsProviderTest.java | 28 +++--- ...onmentVariableCredentialsProviderTest.java | 20 ++-- .../auth/ProfileCredentialsProviderTest.java | 45 ++++----- .../com/aliyuncs/utils/EnvHelperTest.java | 20 ++++ 9 files changed, 122 insertions(+), 123 deletions(-) create mode 100644 aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvHelper.java create mode 100644 aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvHelperTest.java diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/DefaultCredentialsProvider.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/DefaultCredentialsProvider.java index bac28bc2e3..4681e4770b 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/DefaultCredentialsProvider.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/DefaultCredentialsProvider.java @@ -1,8 +1,8 @@ package com.aliyuncs.auth; import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.utils.AuthUtils; import com.aliyuncs.utils.StringUtils; +import com.aliyuncs.utils.EnvHelper; import java.util.ArrayList; import java.util.List; @@ -25,7 +25,7 @@ public DefaultCredentialsProvider() throws ClientException { } defaultProviders.add(new ProfileCredentialsProvider()); - String roleName = AuthUtils.getEnvironmentECSMetaData(); + String roleName = EnvHelper.getenv("ALIBABA_CLOUD_ECS_METADATA"); if (roleName != null) { if (roleName.length() == 0) { throw new ClientException("Environment variable roleName('ALIBABA_CLOUD_ECS_METADATA') cannot be empty"); diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProvider.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProvider.java index b50503c8b4..2e2e72597f 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProvider.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProvider.java @@ -2,6 +2,7 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; +import com.aliyuncs.utils.EnvHelper; public class EnvironmentVariableCredentialsProvider implements AlibabaCloudCredentialsProvider { @Override @@ -10,8 +11,9 @@ public AlibabaCloudCredentials getCredentials() throws ClientException { return null; } - String accessKeyId = AuthUtils.getEnvironmentAccessKeyId(); - String accessKeySecret = AuthUtils.getEnvironmentAccessKeySecret(); + String accessKeyId = EnvHelper.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); + String accessKeySecret = EnvHelper.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); + if (accessKeyId == null || accessKeySecret == null) { return null; } diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java index cd6594b756..84d286c0ed 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java @@ -2,6 +2,7 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; +import com.aliyuncs.utils.EnvHelper; import com.aliyuncs.utils.StringUtils; import org.ini4j.Profile; import org.ini4j.Wini; @@ -27,13 +28,15 @@ private static Wini getIni(String filePath) throws IOException { @Override public AlibabaCloudCredentials getCredentials() throws ClientException { - String filePath = AuthUtils.getEnvironmentCredentialsFile(); + String filePath = EnvHelper.getenv("ALIBABA_CLOUD_CREDENTIALS_FILE"); if (filePath == null) { filePath = AuthConstant.DEFAULT_CREDENTIALS_FILE_PATH; } + if (filePath.length() == 0) { throw new ClientException("The specified credentials file is empty"); } + Wini ini; try { ini = getIni(filePath); diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java index 34cecaa2dc..fd000b93a9 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java @@ -4,43 +4,43 @@ import java.io.FileInputStream; import java.io.IOException; - public class AuthUtils { - private static volatile String clientType = System.getenv("ALIBABA_CLOUD_PROFILE"); - private static volatile String environmentAccessKeyId; - private static volatile String environmentAccesskeySecret; - private static volatile String environmentECSMetaData; - private static volatile String environmentCredentialsFile; + private static volatile String clientType = EnvHelper.getenv("ALIBABA_CLOUD_PROFILE"); private static volatile String privateKey; public static String getPrivateKey(String filePath) { if (null == privateKey) { synchronized (AuthUtils.class) { if (null == privateKey) { - FileInputStream in = null; - byte[] buffer; - try { - in = new FileInputStream(new File(filePath)); - buffer = new byte[in.available()]; - in.read(buffer); - privateKey = new String(buffer, "UTF-8"); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } + privateKey = readFileContent(filePath); } } } return privateKey; } + public static String readFileContent(String filePath) { + FileInputStream in = null; + byte[] buffer; + try { + in = new FileInputStream(new File(filePath)); + buffer = new byte[in.available()]; + in.read(buffer); + return new String(buffer, "UTF-8"); + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + public static void setPrivateKey(String key) { privateKey = key; } @@ -57,52 +57,4 @@ public static String getClientType() { public static void setClientType(String clientType) { AuthUtils.clientType = clientType; } - - public static String getEnvironmentAccessKeyId() { - if (null == AuthUtils.environmentAccessKeyId) { - return System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); - } else { - return AuthUtils.environmentAccessKeyId; - } - } - - public static void setEnvironmentAccessKeyId(String environmentAccessKeyId) { - AuthUtils.environmentAccessKeyId = environmentAccessKeyId; - } - - public static String getEnvironmentAccessKeySecret() { - if (null == AuthUtils.environmentAccesskeySecret) { - return System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); - } else { - return AuthUtils.environmentAccesskeySecret; - } - } - - public static void setEnvironmentAccessKeySecret(String environmentAccesskeySecret) { - AuthUtils.environmentAccesskeySecret = environmentAccesskeySecret; - } - - public static String getEnvironmentECSMetaData() { - if (null == AuthUtils.environmentECSMetaData) { - return System.getenv("ALIBABA_CLOUD_ECS_METADATA"); - } else { - return AuthUtils.environmentECSMetaData; - } - } - - public static void setEnvironmentECSMetaData(String environmentECSMetaData) { - AuthUtils.environmentECSMetaData = environmentECSMetaData; - } - - public static String getEnvironmentCredentialsFile() { - if (null == AuthUtils.environmentCredentialsFile) { - return System.getenv("ALIBABA_CLOUD_CREDENTIALS_FILE"); - } else { - return AuthUtils.environmentCredentialsFile; - } - } - - public static void setEnvironmentCredentialsFile(String environmentCredentialsFile) { - AuthUtils.environmentCredentialsFile = environmentCredentialsFile; - } } diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvHelper.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvHelper.java new file mode 100644 index 0000000000..bf5ad9010f --- /dev/null +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvHelper.java @@ -0,0 +1,21 @@ +package com.aliyuncs.utils; + +import java.util.HashMap; +import java.util.Map; + +public class EnvHelper { + // 中间存储 + private static Map shadowMap = new HashMap(); + + public static String getenv(String key) { + if (shadowMap.containsKey(key)) { + return shadowMap.get(key); + } + + return System.getenv(key); + } + + public static void setenv(String key, String value) { + shadowMap.put(key, value); + } +} diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/DefaultCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/DefaultCredentialsProviderTest.java index 05648d56b2..91af139a5a 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/DefaultCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/DefaultCredentialsProviderTest.java @@ -2,6 +2,8 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; +import com.aliyuncs.utils.EnvHelper; + import org.junit.Assert; import org.junit.Test; @@ -23,7 +25,7 @@ public void userConfigurationProvidersTest() { @Test public void getCredentialsTest() throws ClientException { DefaultCredentialsProvider provider = new DefaultCredentialsProvider(); - AuthUtils.setEnvironmentECSMetaData(""); + EnvHelper.setenv("ALIBABA_CLOUD_ECS_METADATA", ""); try { new DefaultCredentialsProvider(); Assert.fail(); @@ -32,8 +34,8 @@ public void getCredentialsTest() throws ClientException { e.getMessage()); } - AuthUtils.setEnvironmentAccessKeyId("test"); - AuthUtils.setEnvironmentAccessKeySecret("test"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "test"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "test"); AlibabaCloudCredentials credential = provider.getCredentials(); Assert.assertTrue(credential instanceof BasicCredentials); @@ -53,11 +55,11 @@ public AlibabaCloudCredentials getCredentials() { Assert.assertTrue(credential instanceof BasicCredentials); DefaultCredentialsProvider.clearCredentialsProvider(); - AuthUtils.setEnvironmentECSMetaData(null); - AuthUtils.setEnvironmentAccessKeyId(null); - AuthUtils.setEnvironmentAccessKeySecret(null); + EnvHelper.setenv("ALIBABA_CLOUD_ECS_METADATA", null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", null); System.setProperty(AuthConstant.SYSTEM_ACCESSKEYID, ""); - AuthUtils.setEnvironmentCredentialsFile(null); + EnvHelper.setenv("ALIBABA_CLOUD_CREDENTIALS_FILE", null); try { provider.getCredentials(); } catch (ClientException e) { @@ -68,15 +70,15 @@ public AlibabaCloudCredentials getCredentials() { @Test public void defaultCredentialsProviderTest() throws ClientException { DefaultCredentialsProvider.clearCredentialsProvider(); - AuthUtils.setEnvironmentECSMetaData("test"); - AuthUtils.setEnvironmentAccessKeyId("test"); - AuthUtils.setEnvironmentAccessKeySecret("test"); + EnvHelper.setenv("ALIBABA_CLOUD_ECS_METADATA", "test"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "test"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "test"); DefaultCredentialsProvider provider = new DefaultCredentialsProvider(); DefaultCredentialsProvider.addCredentialsProvider(new SystemPropertiesCredentialsProvider()); Assert.assertTrue(provider.getCredentials() instanceof BasicCredentials); - AuthUtils.setEnvironmentECSMetaData(null); - AuthUtils.setEnvironmentAccessKeyId(null); - AuthUtils.setEnvironmentAccessKeySecret(null); + EnvHelper.setenv("ALIBABA_CLOUD_ECS_METADATA", null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", null); DefaultCredentialsProvider.clearCredentialsProvider(); } diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java index 5475559dd4..2e4962be96 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java @@ -2,6 +2,9 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; +import com.aliyuncs.utils.EnvHelper; +import com.aliyuncs.utils.EnvironmentUtils; + import org.junit.Assert; import org.junit.Test; @@ -15,28 +18,27 @@ public void getCredentialsTest() throws ClientException { Assert.assertNull(provider.getCredentials()); AuthUtils.setClientType("default"); - AuthUtils.setEnvironmentAccessKeyId("accessKeyIdTest"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "accessKeyIdTest"); Assert.assertNull(provider.getCredentials()); - AuthUtils.setEnvironmentAccessKeySecret("accessKeyIdTest"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "accessKeyIdTest"); AlibabaCloudCredentials credential = provider.getCredentials(); String accessKeyId = credential.getAccessKeyId(); String accessKeySecret = credential.getAccessKeySecret(); Assert.assertEquals("accessKeyIdTest", accessKeyId); Assert.assertEquals("accessKeyIdTest", accessKeySecret); - AuthUtils.setEnvironmentAccessKeyId(null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", null); Assert.assertNull(provider.getCredentials()); - - AuthUtils.setEnvironmentAccessKeyId(""); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", ""); try { provider.getCredentials(); Assert.fail(); } catch (ClientException e){ Assert.assertEquals("Environment variable accessKeyId cannot be empty", e.getMessage()); } - AuthUtils.setEnvironmentAccessKeyId("a"); - AuthUtils.setEnvironmentAccessKeySecret(""); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "a"); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", ""); try { provider.getCredentials(); Assert.fail(); @@ -44,7 +46,7 @@ public void getCredentialsTest() throws ClientException { Assert.assertEquals("Environment variable accessKeySecret cannot be empty", e.getMessage()); } - AuthUtils.setEnvironmentAccessKeyId(null); - AuthUtils.setEnvironmentAccessKeySecret(null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", null); + EnvHelper.setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", null); } } diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java index 990c14aedd..53a25c2a06 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java @@ -2,6 +2,8 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; +import com.aliyuncs.utils.EnvHelper; + import org.ini4j.Wini; import org.junit.Assert; import org.junit.Test; @@ -12,12 +14,11 @@ import java.util.HashMap; import java.util.Map; - public class ProfileCredentialsProviderTest { @Test public void getCredentialsTest() throws ClientException { - AuthUtils.setEnvironmentCredentialsFile(""); + EnvHelper.setenv("ALIBABA_CLOUD_CREDENTIALS_FILE", ""); ProfileCredentialsProvider provider = new ProfileCredentialsProvider(); try { provider.getCredentials(); @@ -25,9 +26,8 @@ public void getCredentialsTest() throws ClientException { } catch (ClientException e) { Assert.assertEquals("The specified credentials file is empty", e.getMessage()); } - String filePath = ProfileCredentialsProviderTest.class.getClassLoader(). - getResource("configTest.ini").getPath(); - AuthUtils.setEnvironmentCredentialsFile(filePath); + String filePath = ProfileCredentialsProviderTest.class.getClassLoader().getResource("configTest.ini").getPath(); + EnvHelper.setenv("ALIBABA_CLOUD_CREDENTIALS_FILE", filePath); provider = new ProfileCredentialsProvider(); Assert.assertNotNull(provider.getCredentials()); @@ -58,7 +58,6 @@ public void createCredentialTest() throws NoSuchMethodException, InvocationTarge Assert.assertEquals("The configured client type is empty", e.getCause().getLocalizedMessage()); } - client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_RAM); try { createCredential.invoke(provider, client, factory); @@ -118,7 +117,7 @@ public void getSTSAssumeRoleSessionCredentialsTest() throws NoSuchMethodExceptio } @Test - public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodException { + public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodException { ProfileCredentialsProvider provider = new ProfileCredentialsProvider(); Class providerClass = provider.getClass(); Method createCredential = providerClass.getDeclaredMethod( @@ -146,10 +145,9 @@ public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodExcep AuthUtils.setPrivateKey(null); } - @Test - public void createCredentialsProviderTest() throws - NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClientException { + public void createCredentialsProviderTest() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClientException { ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(); Class providerClass = profileCredentialsProvider.getClass(); Method createCredential = providerClass.getDeclaredMethod( @@ -162,12 +160,12 @@ public void createCredentialsProviderTest() throws client.put(AuthConstant.INI_ROLE_SESSION_NAME, AuthConstant.INI_TYPE_ARN); client.put(AuthConstant.INI_ROLE_ARN, AuthConstant.INI_TYPE_ARN); client.put(AuthConstant.DEFAULT_REGION, AuthConstant.INI_TYPE_ARN); - STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider = - Mockito.mock(STSAssumeRoleSessionCredentialsProvider.class); + STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider = Mockito + .mock(STSAssumeRoleSessionCredentialsProvider.class); Mockito.when(stsAssumeRoleSessionCredentialsProvider.getCredentials()).thenReturn(null); CredentialsProviderFactory factory = Mockito.mock(CredentialsProviderFactory.class); - Mockito.when(factory.createCredentialsProvider(Mockito.any(STSAssumeRoleSessionCredentialsProvider.class))). - thenReturn(stsAssumeRoleSessionCredentialsProvider); + Mockito.when(factory.createCredentialsProvider(Mockito.any(STSAssumeRoleSessionCredentialsProvider.class))) + .thenReturn(stsAssumeRoleSessionCredentialsProvider); Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); client.clear(); @@ -176,22 +174,22 @@ public void createCredentialsProviderTest() throws client.put(AuthConstant.INI_PRIVATE_KEY, AuthConstant.INI_TYPE_KEY_PAIR); client.put(AuthConstant.INI_PRIVATE_KEY_FILE, AuthConstant.INI_TYPE_KEY_PAIR); AuthUtils.setPrivateKey("test"); - STSGetSessionAccessKeyCredentialsProvider stsGetSessionAccessKeyCredentialsProvider = - Mockito.mock(STSGetSessionAccessKeyCredentialsProvider.class); + STSGetSessionAccessKeyCredentialsProvider stsGetSessionAccessKeyCredentialsProvider = Mockito + .mock(STSGetSessionAccessKeyCredentialsProvider.class); Mockito.when(stsGetSessionAccessKeyCredentialsProvider.getCredentials()).thenReturn(null); - Mockito.when(factory.createCredentialsProvider(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class))). - thenReturn(stsGetSessionAccessKeyCredentialsProvider); + Mockito.when(factory.createCredentialsProvider(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class))) + .thenReturn(stsGetSessionAccessKeyCredentialsProvider); Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); AuthUtils.setPrivateKey(null); client.clear(); client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_RAM); client.put(AuthConstant.INI_ROLE_NAME, AuthConstant.INI_TYPE_KEY_PAIR); - InstanceProfileCredentialsProvider instanceProfileCredentialsProvider = - Mockito.mock(InstanceProfileCredentialsProvider.class); + InstanceProfileCredentialsProvider instanceProfileCredentialsProvider = Mockito + .mock(InstanceProfileCredentialsProvider.class); Mockito.when(instanceProfileCredentialsProvider.getCredentials()).thenReturn(null); - Mockito.when(factory.createCredentialsProvider(Mockito.any(InstanceProfileCredentialsProvider.class))). - thenReturn(instanceProfileCredentialsProvider); + Mockito.when(factory.createCredentialsProvider(Mockito.any(InstanceProfileCredentialsProvider.class))) + .thenReturn(instanceProfileCredentialsProvider); Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); } @@ -202,8 +200,7 @@ public void getIniTest() throws NoSuchMethodException, InvocationTargetException Method getIni = providerClass.getDeclaredMethod( "getIni", String.class); getIni.setAccessible(true); - String file = ProfileCredentialsProviderTest.class.getClassLoader(). - getResource("configTest.ini").getPath(); + String file = ProfileCredentialsProviderTest.class.getClassLoader().getResource("configTest.ini").getPath(); Wini firstIni = (Wini) getIni.invoke(profileCredentialsProvider, file); Wini secondIni = (Wini) getIni.invoke(profileCredentialsProvider, file); Assert.assertTrue(firstIni.equals(secondIni)); diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvHelperTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvHelperTest.java new file mode 100644 index 0000000000..9a2a57130e --- /dev/null +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvHelperTest.java @@ -0,0 +1,20 @@ + +package com.aliyuncs.utils; + +import org.junit.Assert; +import org.junit.Test; + + +public class EnvHelperTest { + @Test + public void construct(){ + new EnvHelper(); + } + + @Test + public void getAndSet(){ + Assert.assertNull(EnvHelper.getenv("test")); + EnvHelper.setenv("test", "value"); + Assert.assertEquals("value", EnvHelper.getenv("test")); + } +} From 13c630a43d58628f753e2fcfda5610db0869d1fd Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Wed, 12 Jun 2024 16:49:37 +0800 Subject: [PATCH 2/2] remove EnvironmentUtils --- .../http/clients/ApacheHttpClient.java | 8 ++-- .../http/clients/CompatibleUrlConnClient.java | 8 ++-- .../com/aliyuncs/utils/EnvironmentUtils.java | 48 ------------------- ...onmentVariableCredentialsProviderTest.java | 1 - .../aliyuncs/utils/EnvironmentUtilsTest.java | 33 ------------- 5 files changed, 8 insertions(+), 90 deletions(-) delete mode 100644 aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvironmentUtils.java delete mode 100644 aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvironmentUtilsTest.java diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java index 9c59f1d95d..917f4f176e 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java @@ -2,7 +2,7 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.*; -import com.aliyuncs.utils.EnvironmentUtils; +import com.aliyuncs.utils.EnvHelper; import com.aliyuncs.utils.IOUtils; import com.aliyuncs.utils.StringUtils; import org.apache.http.Header; @@ -271,16 +271,16 @@ private HttpUriRequest parseToHttpRequest(HttpRequest apiReq) throws IOException } private HttpHost calcProxy(HttpRequest apiReq) throws MalformedURLException, ClientException { - boolean needProxy = HttpUtil.needProxy(new URL(apiReq.getSysUrl()).getHost(), clientConfig.getNoProxy(), EnvironmentUtils.getNoProxy()); + boolean needProxy = HttpUtil.needProxy(new URL(apiReq.getSysUrl()).getHost(), clientConfig.getNoProxy(), EnvHelper.getenv("NO_PROXY")); if (!needProxy) { return null; } URL url = new URL(apiReq.getSysUrl()); HttpHost proxy = null; if ("https".equalsIgnoreCase(url.getProtocol())) { - proxy = HttpUtil.getApacheProxy(clientConfig.getHttpsProxy(), EnvironmentUtils.getHttpsProxy(), apiReq); + proxy = HttpUtil.getApacheProxy(clientConfig.getHttpsProxy(), EnvHelper.getenv("HTTPS_PROXY"), apiReq); } else { - proxy = HttpUtil.getApacheProxy(clientConfig.getHttpProxy(), EnvironmentUtils.getHttpProxy(), apiReq); + proxy = HttpUtil.getApacheProxy(clientConfig.getHttpProxy(), EnvHelper.getenv("HTTP_PROXY"), apiReq); } return proxy; } diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/CompatibleUrlConnClient.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/CompatibleUrlConnClient.java index d51356156b..f9095eb969 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/CompatibleUrlConnClient.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/CompatibleUrlConnClient.java @@ -2,7 +2,7 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.*; -import com.aliyuncs.utils.EnvironmentUtils; +import com.aliyuncs.utils.EnvHelper; import org.apache.http.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.ssl.NoopHostnameVerifier; @@ -169,16 +169,16 @@ private void checkHttpRequest(HttpRequest request) { private Proxy calcProxy(URL url, HttpRequest request) throws ClientException { String targetHost = url.getHost(); - boolean needProxy = HttpUtil.needProxy(targetHost, clientConfig.getNoProxy(), EnvironmentUtils.getNoProxy()); + boolean needProxy = HttpUtil.needProxy(targetHost, clientConfig.getNoProxy(), EnvHelper.getenv("NO_PROXY")); if (!needProxy) { return Proxy.NO_PROXY; } Proxy proxy; if ("https".equalsIgnoreCase(url.getProtocol())) { - String httpsProxy = EnvironmentUtils.getHttpsProxy(); + String httpsProxy = EnvHelper.getenv("HTTPS_PROXY"); proxy = HttpUtil.getJDKProxy(clientConfig.getHttpsProxy(), httpsProxy, request); } else { - String httpProxy = EnvironmentUtils.getHttpProxy(); + String httpProxy = EnvHelper.getenv("HTTP_PROXY"); proxy = HttpUtil.getJDKProxy(clientConfig.getHttpProxy(), httpProxy, request); } return proxy; diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvironmentUtils.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvironmentUtils.java deleted file mode 100644 index c31d02be66..0000000000 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvironmentUtils.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.aliyuncs.utils; - -public class EnvironmentUtils { - - private static volatile String httpProxy; - private static volatile String httpsProxy; - private static volatile String noProxy; - - public static String getHttpProxy() { - if (null == httpProxy) { - String proxy0 = System.getenv("HTTP_PROXY"); - String proxy1 = System.getenv("http_proxy"); - return (!StringUtils.isEmpty(proxy0) ? proxy0 : proxy1); - } else { - return httpProxy; - } - } - - public static void setHttpProxy(String httpProxy) { - EnvironmentUtils.httpProxy = httpProxy; - } - - public static String getHttpsProxy() { - if (null == httpsProxy) { - return System.getenv("HTTPS_PROXY"); - } else { - return httpsProxy; - } - } - - public static void setHttpsProxy(String httpsProxy) { - EnvironmentUtils.httpsProxy = httpsProxy; - } - - - public static String getNoProxy() { - if (null == noProxy) { - return System.getenv("NO_PROXY"); - } else { - return noProxy; - } - } - - public static void setNoProxy(String noProxy) { - EnvironmentUtils.noProxy = noProxy; - } - -} diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java index 2e4962be96..c87536155b 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProviderTest.java @@ -3,7 +3,6 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.AuthUtils; import com.aliyuncs.utils.EnvHelper; -import com.aliyuncs.utils.EnvironmentUtils; import org.junit.Assert; import org.junit.Test; diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvironmentUtilsTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvironmentUtilsTest.java deleted file mode 100644 index 45efa41988..0000000000 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/EnvironmentUtilsTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.aliyuncs.utils; - -import org.junit.Assert; -import org.junit.Test; - -public class EnvironmentUtilsTest { - - @Test - public void testConstructor() { - Assert.assertNotNull(new EnvironmentUtils()); - } - - @Test - public void testGetSetHttpProxy() { - Assert.assertNull(EnvironmentUtils.getHttpProxy()); - EnvironmentUtils.setHttpProxy("http://www.aliyun.com"); - Assert.assertEquals("http://www.aliyun.com", EnvironmentUtils.getHttpProxy()); - } - - @Test - public void testGetSetHttpsProxy() { - Assert.assertNull(EnvironmentUtils.getHttpsProxy()); - EnvironmentUtils.setHttpsProxy("https://www.aliyun.com"); - Assert.assertEquals("https://www.aliyun.com", EnvironmentUtils.getHttpsProxy()); - } - - @Test - public void testGetSetNoProxy() { - Assert.assertNull(EnvironmentUtils.getNoProxy()); - EnvironmentUtils.setNoProxy("https://www.aliyun.com"); - Assert.assertEquals("https://www.aliyun.com", EnvironmentUtils.getNoProxy()); - } -}