-
Notifications
You must be signed in to change notification settings - Fork 1.1k
improve environment variable implements #900
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
Open
JacksonTian
wants to merge
2
commits into
master
Choose a base branch
from
env
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/EnvironmentUtils.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要保留和兼容之前的get方式,因为AuthUtils有对应的set方法