Skip to content

Commit 4368696

Browse files
JacksonTianyndu13
authored andcommitted
Add OIDCCredentialsProvider into default credentials provider chain
1 parent 3a115a1 commit 4368696

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
319319
String regionId, AlibabaCloudCredentials credentials, Signer signer, FormatType format)
320320
throws ClientException, ServerException {
321321

322-
323322
doActionWithProxy(request.getSysProtocol(), System.getenv("HTTPS_PROXY"), System.getenv("HTTP_PROXY"));
324323
doActionWithIgnoreSSL(request, X509TrustAll.ignoreSSLCerts);
325324

@@ -584,7 +583,6 @@ public void shutdown() {
584583
IOUtils.closeQuietly(this.httpClient);
585584
this.httpClient = null;
586585
}
587-
588586
}
589587

590588
public DefaultProfile getProfile() {

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/DefaultCredentialsProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.aliyuncs.exceptions.ClientException;
44
import com.aliyuncs.utils.AuthUtils;
5+
import com.aliyuncs.utils.StringUtils;
56

67
import java.util.ArrayList;
78
import java.util.List;
@@ -15,6 +16,14 @@ public class DefaultCredentialsProvider implements AlibabaCloudCredentialsProvid
1516
public DefaultCredentialsProvider() throws ClientException {
1617
defaultProviders.add(new SystemPropertiesCredentialsProvider());
1718
defaultProviders.add(new EnvironmentVariableCredentialsProvider());
19+
// Add oidc credentials provider
20+
String oidcProviderArn = System.getenv("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
21+
String roleArn = System.getenv("ALIBABA_CLOUD_ROLE_ARN");
22+
String oidcTokenFile = System.getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE");
23+
if (!StringUtils.isEmpty(oidcProviderArn) && !StringUtils.isEmpty(oidcTokenFile) && !StringUtils.isEmpty(roleArn)) {
24+
defaultProviders.add(new OIDCCredentialsProvider(roleArn, oidcProviderArn, oidcTokenFile, "java-sdk-v1-default-rsn", null));
25+
}
26+
1827
defaultProviders.add(new ProfileCredentialsProvider());
1928
String roleName = AuthUtils.getEnvironmentECSMetaData();
2029
if (roleName != null) {
@@ -42,6 +51,7 @@ public AlibabaCloudCredentials getCredentials() throws ClientException {
4251
return credential;
4352
}
4453
}
54+
4555
throw new ClientException("not found credentials");
4656
}
4757

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/SystemPropertiesCredentialsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ public AlibabaCloudCredentials getCredentials() throws ClientException, ServerEx
1212
if (!"default".equals(AuthUtils.getClientType())) {
1313
return null;
1414
}
15+
1516
String accessKeyId = System.getProperty(AuthConstant.SYSTEM_ACCESSKEYID);
1617
String accessKeySecret = System.getProperty(AuthConstant.SYSTEM_ACCESSKEYSECRET);
1718
if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(accessKeySecret)) {
1819
return null;
1920
}
21+
2022
return new BasicCredentials(accessKeyId, accessKeySecret);
2123
}
2224
}

java-sdk-function-test/src/test/java/com/aliyuncs/CredentialsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ public void oidcProviderTest() throws ClientException {
4545
Assert.assertTrue(response.getArn().endsWith("/sessionname"));
4646
}
4747

48+
@Test
49+
public void oidcProviderForDefaultCredentialsProviderTest() throws ClientException {
50+
DefaultAcsClient client = new DefaultAcsClient(this.regionId);
51+
GetCallerIdentityRequest request = new GetCallerIdentityRequest();
52+
GetCallerIdentityResponse response = client.getAcsResponse(request);
53+
Assert.assertNotNull(response);
54+
Assert.assertEquals("AssumedRoleUser", response.getIdentityType());
55+
Assert.assertTrue(response.getArn().endsWith("/java-sdk-v1-default-rsn"));
56+
}
4857
}

0 commit comments

Comments
 (0)