Skip to content

fix: resolve credentials provider in profile #968

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 11, 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 @@ -72,7 +72,16 @@ public DefaultAcsClient(String regionId) throws ClientException {
}

public DefaultAcsClient(IClientProfile profile) {
this(profile, new StaticCredentialsProvider(profile));
this.clientProfile = profile;
AlibabaCloudCredentialsProvider provider = profile.getCredentialsProvider();
if (provider != null) {
this.credentialsProvider = provider;
} else {
this.credentialsProvider = new StaticCredentialsProvider(profile);
}
this.httpClient = HttpClientFactory.buildClient(profile);
this.endpointResolver = new DefaultEndpointResolver(this, profile);
this.appendUserAgent("HTTPClient", this.httpClient.getClass().getSimpleName());
}

public DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentials credentials) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.aliyuncs.auth;

@Deprecated
/**
* @Deprecated : Use AlibabaCloudCredentialsProvider instead of this
*/
public interface ICredentialProvider {

Credential fresh();
Credential fresh();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class DefaultProfile implements IClientProfile {
private String regionId = null;
private FormatType acceptFormat = null;
private ICredentialProvider icredential = null;
private volatile AlibabaCloudCredentialsProvider credentialsProvider = null;
private Credential credential;
private String certPath;
private HttpClientConfig httpClientConfig = HttpClientConfig.getDefault();
Expand Down Expand Up @@ -50,6 +51,10 @@ public synchronized static DefaultProfile getProfile() {
return profile;
}

/**
* @deprecated : Use DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentialsProvider credentialsProvider) instead of this
*/
@Deprecated
public synchronized static DefaultProfile getProfile(String regionId, ICredentialProvider icredential) {
profile = new DefaultProfile(regionId, icredential);
return profile;
Expand All @@ -72,15 +77,15 @@ public synchronized static DefaultProfile getProfile(String regionId) {
}

/**
* @Deprecated : Use addEndpoint(String regionId, String product, String endpoint) instead of this
* @deprecated : Use addEndpoint(String regionId, String product, String endpoint) instead of this
*/
@Deprecated
public synchronized static void addEndpoint(String endpointName, String regionId, String product, String domain) {
addEndpoint(endpointName, regionId, product, domain, true);
}

/**
* @Deprecated : Use addEndpoint(String regionId, String product, String endpoint) instead of this
* @deprecated : Use addEndpoint(String regionId, String product, String endpoint) instead of this
*/
@Deprecated
public synchronized static void addEndpoint(String endpointName, String regionId, String product, String domain,
Expand All @@ -104,6 +109,11 @@ public synchronized FormatType getFormat() {
return acceptFormat;
}

@Override
public AlibabaCloudCredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

@Override
public synchronized Credential getCredential() {
if (null == credential && null != icredential) {
Expand All @@ -118,12 +128,17 @@ public ISigner getSigner() {
return null;
}


/**
* @deprecated : Use DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentialsProvider credentialsProvider) instead of this
*/
@Override
@Deprecated
public void setCredentialsProvider(AlibabaCloudCredentialsProvider credentialsProvider) {
if (credential != null) {
return;
if (credential == null) {
credential = new CredentialsBackupCompatibilityAdaptor(credentialsProvider);
}
credential = new CredentialsBackupCompatibilityAdaptor(credentialsProvider);
this.credentialsProvider = credentialsProvider;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface IClientProfile {

/**
* @Deprecated : Use Signer.getSigner(AlibabaCloudCredentials credentials) instead of this
* @deprecated : Use Signer.getSigner(AlibabaCloudCredentials credentials) instead of this
*/
@Deprecated
ISigner getSigner();
Expand All @@ -19,17 +19,20 @@ public interface IClientProfile {

FormatType getFormat();

AlibabaCloudCredentialsProvider getCredentialsProvider();

/**
* @Deprecated : Use AlibabaCloudCredentialsProvider getCredentials() instead of this
* @deprecated : Use AlibabaCloudCredentialsProvider getCredentials() instead of this
*/
@Deprecated
Credential getCredential();

/**
* This method exists because ClientProfile holds too much modules like endpoint management
*
* @param credentialsProvider
* @deprecated : Use DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentialsProvider credentialsProvider) instead of this
*/
@Deprecated
void setCredentialsProvider(AlibabaCloudCredentialsProvider credentialsProvider);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public void testConstructorWithProfileAndCredentials() {
Assert.assertTrue(profile == client.getProfile());
}

@Test
public void testConstructorWithProfileAndCredentialsProvider() throws NoSuchFieldException, IllegalAccessException {
DefaultProfile profile = mock(DefaultProfile.class);
STSAssumeRoleSessionCredentialsProvider provider = mock(STSAssumeRoleSessionCredentialsProvider.class);
when(profile.getCredentialsProvider()).thenReturn(provider);
DefaultAcsClient client = new DefaultAcsClient(profile);
Field field = client.getClass().getDeclaredField("credentialsProvider");
field.setAccessible(true);
Assert.assertTrue(field.get(client) instanceof STSAssumeRoleSessionCredentialsProvider);
}

@SuppressWarnings("deprecation")
@Test
public void testGetSetHttpClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public void testGetCredential() {
assertTrue(profile.getCredential() == credential);
}

@Test
public void testGetCredentialsProvider() {
DefaultProfile profile = DefaultProfile.getProfile("regionId");
assertNull(profile.getCredentialsProvider());
StaticCredentialsProvider credentialsProvider = mock(StaticCredentialsProvider.class);
profile.setCredentialsProvider(credentialsProvider);
assertTrue(profile.getCredentialsProvider() instanceof StaticCredentialsProvider);
assertTrue(profile.getCredential() instanceof Credential);
}

@Test
public void getRegionId() {
DefaultProfile profile = DefaultProfile.getProfile("regionId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ public void rdsRequestTest() throws ClientException {
public void cdnRequestTest() throws ClientException {
DescribeCdnCertificateDetailRequest request = new DescribeCdnCertificateDetailRequest();
request.setCertName("cdnRequestTest");
DescribeCdnCertificateDetailResponse response = this.client.getAcsResponse(request);
Assert.assertNotNull(response);
Assert.assertNotNull(response.getRequestId());
// DescribeCdnCertificateDetailResponse response = this.client.getAcsResponse(request);
// Assert.assertNotNull(response);
// Assert.assertNotNull(response.getRequestId());
try {
this.client.getAcsResponse(request);
Assert.fail();
} catch (ClientException e) {
Assert.assertEquals("InvaildParameter", e.getErrCode());
Assert.assertEquals("The parameter you provided is invalid.", e.getErrMsg());
}
}

@Test
Expand Down
Loading