Skip to content

Commit

Permalink
improve environment variable implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 8, 2024
1 parent 4368696 commit fe65b33
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
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 @@ -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) {
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);
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,36 +18,35 @@ 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();
} catch (ClientException e){
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);
}
}
Loading

0 comments on commit fe65b33

Please sign in to comment.