Skip to content

feat: support StsToken mode in cli profile #969

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

Merged
merged 1 commit into from
Apr 18, 2025
Merged
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 @@ -66,6 +66,9 @@ AlibabaCloudCredentialsProvider reloadCredentialsProvider(Config config, String
if ("AK".equals(mode)) {
return new StaticCredentialsProvider(
new BasicCredentials(profile.getAccessKeyId(), profile.getAccessKeySecret()));
} else if ("StsToken".equals(mode)) {
return new StaticCredentialsProvider(
new BasicSessionCredentials(profile.getAccessKeyId(), profile.getAccessKeySecret(), profile.getSecurityToken()));
} else if ("RamRoleArn".equals(mode)) {
AlibabaCloudCredentialsProvider innerProvider = new StaticCredentialsProvider(
new BasicCredentials(profile.getAccessKeyId(), profile.getAccessKeySecret()));
Expand Down Expand Up @@ -185,6 +188,8 @@ static class Profile {
private String accessKeyId;
@SerializedName("access_key_secret")
private String accessKeySecret;
@SerializedName("sts_token")
private String securityToken;
@SerializedName("ram_role_arn")
private String roleArn;
@SerializedName("ram_session_name")
Expand Down Expand Up @@ -224,6 +229,10 @@ public String getAccessKeySecret() {
return accessKeySecret;
}

public String getSecurityToken() {
return securityToken;
}

public String getRoleArn() {
return roleArn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public void reloadCredentialsProviderTest() throws ClientException, IOException
Assert.assertEquals("akid", credential.getAccessKeyId());
Assert.assertEquals("secret", credential.getAccessKeySecret());

credentialsProvider = provider.reloadCredentialsProvider(config, "StsToken");
Assert.assertTrue(credentialsProvider instanceof StaticCredentialsProvider);
BasicSessionCredentials sessionCredentials = (BasicSessionCredentials) credentialsProvider.getCredentials();
Assert.assertEquals("accessKeyId", sessionCredentials.getAccessKeyId());
Assert.assertEquals("accessKeySecret", sessionCredentials.getAccessKeySecret());
Assert.assertEquals("stsToken", sessionCredentials.getSessionToken());

credentialsProvider = provider.reloadCredentialsProvider(config, "RamRoleArn");
Assert.assertTrue(credentialsProvider instanceof STSAssumeRoleSessionCredentialsProvider);
try {
Expand Down
7 changes: 7 additions & 0 deletions aliyun-java-sdk-core/src/test/resources/.aliyun/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"access_key_id": "akid",
"access_key_secret": "secret"
},
{
"name": "StsToken",
"mode": "StsToken",
"access_key_id": "accessKeyId",
"access_key_secret": "accessKeySecret",
"sts_token": "stsToken"
},
{
"name": "RamRoleArn",
"mode": "RamRoleArn",
Expand Down
Loading