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

improve environment variable implements #900

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
String regionId, AlibabaCloudCredentials credentials, Signer signer, FormatType format)
throws ClientException, ServerException {


doActionWithProxy(request.getSysProtocol(), System.getenv("HTTPS_PROXY"), System.getenv("HTTP_PROXY"));
doActionWithIgnoreSSL(request, X509TrustAll.ignoreSSLCerts);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.aliyuncs.auth;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.utils.AuthUtils;
import com.aliyuncs.utils.EnvHelper;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,7 +16,7 @@ public DefaultCredentialsProvider() throws ClientException {
defaultProviders.add(new SystemPropertiesCredentialsProvider());
defaultProviders.add(new EnvironmentVariableCredentialsProvider());
defaultProviders.add(new ProfileCredentialsProvider());
String roleName = AuthUtils.getEnvironmentECSMetaData();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要保留和兼容之前的get方式,因为AuthUtils有对应的set方法

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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -165,16 +165,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种方式还不能删掉,用户是可能会用到的,可以先置为弃用,标注弃用时间。时间到期再删。

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.aliyuncs.utils;

import java.util.HashMap;
import java.util.Map;

public class EnvHelper {
// 中间存储
private static Map<String, String> shadowMap = new HashMap<String, String>();

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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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);

Expand All @@ -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) {
Expand All @@ -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();
}

Expand Down
Loading
Loading