Skip to content

feat: add support for credential provider #20

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-auth</artifactId>
<version>0.2.16-beta</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
53 changes: 43 additions & 10 deletions src/main/java/com/aliyun/mq/http/MQClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.aliyun.mq.http;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.ICredentialProvider;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.mq.http.common.ClientException;
import com.aliyun.mq.http.common.http.ClientConfiguration;
import com.aliyun.mq.http.common.utils.Utils;
Expand All @@ -25,7 +28,7 @@ public class MQClient {
/**
* user info
*/
private ServiceCredentials credentials;
private ICredentialProvider credentialProvider;
private ClientConfiguration config;

private static volatile MQProducer PRODUCER;
Expand All @@ -37,16 +40,31 @@ private static MQConsumer buildConsumer(String instanceId, String topicName, Str
return new MQConsumer(instanceId, topicName, consumer, messageTag, client, credentials, endpoint);
}

private static MQConsumer buildConsumer(String instanceId, String topicName, String consumer, String messageTag, ServiceClient client,
ICredentialProvider credentialProvider, URI endpoint) {
return new MQConsumer(instanceId, topicName, consumer, messageTag, client, credentialProvider, endpoint);
}

private static MQProducer buildProducer(String instanceId, String topicName, ServiceClient client,
ServiceCredentials credentials, URI endpoint) {
return new MQProducer(instanceId, topicName, client, credentials, endpoint);
}

private static MQProducer buildProducer(String instanceId, String topicName, ServiceClient client,
ICredentialProvider credentialProvider, URI endpoint) {
return new MQProducer(instanceId, topicName, client, credentialProvider, endpoint);
}

private static MQTransProducer buildTransactionalProducer(String instanceId, String topicName, String groupId, ServiceClient client,
ServiceCredentials credentials, URI endpoint) {
return new MQTransProducer(instanceId, topicName, groupId, client, credentials, endpoint);
}

private static MQTransProducer buildTransactionalProducer(String instanceId, String topicName, String groupId, ServiceClient client,
ICredentialProvider credentialProvider, URI endpoint) {
return new MQTransProducer(instanceId, topicName, groupId, client, credentialProvider, endpoint);
}

/**
* init a MQ client with default client config
*
Expand Down Expand Up @@ -92,7 +110,22 @@ public MQClient(String accountEndpoint, String accessId, String accessKey, Strin
* @param config defined client config
*/
public MQClient(String accountEndpoint, String accessId, String accessKey, String securityToken, ClientConfiguration config) {
this.credentials = new ServiceCredentials(accessId, accessKey, securityToken);
this(accountEndpoint, StaticCredentialProvider.create(Credential.builder()
.accessKeyId(accessId)
.accessKeySecret(accessKey)
.securityToken(securityToken)
.build()), config);
}

/**
* init a MQ client with defined client config and customized credential provider
*
* @param accountEndpoint mq http endpoint, like: http://xxx.mqreset.cn-hangzhou.aliyuncs.com
* @param credentialProvider credentials provisioning
* @param config defined client config
*/
public MQClient(String accountEndpoint, ICredentialProvider credentialProvider, ClientConfiguration config) {
this.credentialProvider = credentialProvider;
this.endpoint = Utils.getHttpURI(accountEndpoint);
if (config == null) {
this.config = new ClientConfiguration();
Expand Down Expand Up @@ -131,7 +164,7 @@ public MQProducer getProducer(String topicName) {
if (null == PRODUCER) {
synchronized (MQClient.class) {
if (null == PRODUCER) {
PRODUCER = buildProducer(null, topicName, this.serviceClient, this.credentials, this.endpoint);
PRODUCER = buildProducer(null, topicName, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -149,7 +182,7 @@ public MQProducer getProducer(String instanceId, String topicName) {
if (null == PRODUCER) {
synchronized (MQClient.class) {
if (null == PRODUCER) {
PRODUCER = buildProducer(instanceId, topicName, this.serviceClient, this.credentials, this.endpoint);
PRODUCER = buildProducer(instanceId, topicName, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -167,7 +200,7 @@ public MQTransProducer getTransProducer(String topicName, String groupId) {
if (null == TRANSACTIONAL_PRODUCER) {
synchronized (MQClient.class) {
if (null == TRANSACTIONAL_PRODUCER) {
TRANSACTIONAL_PRODUCER = buildTransactionalProducer(null, topicName, groupId, this.serviceClient, this.credentials, this.endpoint);
TRANSACTIONAL_PRODUCER = buildTransactionalProducer(null, topicName, groupId, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -186,7 +219,7 @@ public MQTransProducer getTransProducer(String instanceId, String topicName, Str
if (null == TRANSACTIONAL_PRODUCER) {
synchronized (MQClient.class) {
if (null == TRANSACTIONAL_PRODUCER) {
TRANSACTIONAL_PRODUCER = buildTransactionalProducer(instanceId, topicName, groupId, this.serviceClient, this.credentials, this.endpoint);
TRANSACTIONAL_PRODUCER = buildTransactionalProducer(instanceId, topicName, groupId, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -205,7 +238,7 @@ public MQConsumer getConsumer(String topicName, String consumer, String messageT
if (null == CONSUMER) {
synchronized (MQClient.class) {
if (null == CONSUMER) {
CONSUMER = buildConsumer(null, topicName, consumer, messageTag, this.serviceClient, this.credentials, this.endpoint);
CONSUMER = buildConsumer(null, topicName, consumer, messageTag, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -223,7 +256,7 @@ public MQConsumer getConsumer(String topicName, String consumer) {
if (null == CONSUMER) {
synchronized (MQClient.class) {
if (null == CONSUMER) {
CONSUMER = buildConsumer(null, topicName, consumer, null, this.serviceClient, this.credentials, this.endpoint);
CONSUMER = buildConsumer(null, topicName, consumer, null, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -243,7 +276,7 @@ public MQConsumer getConsumer(String instanceId, String topicName, String consum
if (null == CONSUMER) {
synchronized (MQClient.class) {
if (null == CONSUMER) {
CONSUMER = buildConsumer(instanceId, topicName, consumer, messageTag, this.serviceClient, this.credentials, this.endpoint);
CONSUMER = buildConsumer(instanceId, topicName, consumer, messageTag, this.serviceClient, this.credentialProvider, this.endpoint);
}
}
}
Expand All @@ -254,7 +287,7 @@ public MQConsumer getConsumer(String instanceId, String topicName, String consum
public String toString() {
final StringBuilder sb = new StringBuilder("MQClient{");
sb.append("endpoint=").append(endpoint);
sb.append(", credentials=").append(credentials);
sb.append(", credentialProvider=").append(credentialProvider);
sb.append('}');
return sb.toString();
}
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/com/aliyun/mq/http/MQConsumer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.aliyun.mq.http;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.ICredentialProvider;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.mq.http.common.utils.ServiceCredentialsWrapper;
import com.aliyun.mq.http.model.AsyncCallback;
import com.aliyun.mq.http.model.AsyncResult;
import com.aliyun.mq.http.common.ClientException;
Expand Down Expand Up @@ -36,7 +40,7 @@ public class MQConsumer {
/**
* object content user auth info
*/
private final ServiceCredentials credentials;
private final ICredentialProvider credentialProvider;
/**
* user mq http endpoint, ie: http://uid.mqrest.region.aliyuncs.com/
*/
Expand All @@ -57,9 +61,27 @@ public class MQConsumer {
*/
protected MQConsumer(String instanceId, String topicName, String consumer, String messageTag, ServiceClient client,
ServiceCredentials credentials, URI endpoint) {
this(instanceId, topicName, consumer, messageTag, client, StaticCredentialProvider.create(Credential.builder()
.accessKeyId(credentials.getAccessKeyId())
.accessKeySecret(credentials.getAccessKeySecret())
.securityToken(credentials.getSecurityToken())
.build()), endpoint);
}

/**
* @param instanceId, instance id
* @param topicName, topic name
* @param consumer mq cid
* @param messageTag message tag for filter
* @param client, ServiceClient object
* @param credentialProvider, ICredentialProvider object
* @param endpoint, user mq http endpoint, ie: http://uid.mqrest.region.aliyuncs.com/
*/
protected MQConsumer(String instanceId, String topicName, String consumer, String messageTag, ServiceClient client,
ICredentialProvider credentialProvider, URI endpoint) {
this.instanceId = instanceId;
this.serviceClient = client;
this.credentials = credentials;
this.credentialProvider = credentialProvider;
this.endpoint = endpoint;

String uri = endpoint.toString();
Expand Down Expand Up @@ -139,6 +161,7 @@ public List<Message> consumeMessage(int num, int pollingSecond)
request.setInstanceId(this.instanceId);

try {
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
ConsumeMessageAction action = new ConsumeMessageAction(serviceClient, credentials, endpoint);
request.setRequestPath(topicURL + "/" + Constants.LOCATION_MESSAGES);
return action.executeWithCustomHeaders(request, null);
Expand Down Expand Up @@ -179,6 +202,7 @@ public List<Message> consumeMessageOrderly(int num, int pollingSecond)
request.setTrans("order");

try {
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
ConsumeMessageAction action = new ConsumeMessageAction(serviceClient, credentials, endpoint);
request.setRequestPath(topicURL + "/" + Constants.LOCATION_MESSAGES);
return action.executeWithCustomHeaders(request, null);
Expand Down Expand Up @@ -211,6 +235,7 @@ public AsyncResult<List<Message>> asyncConsumeMessage(int num, int pollingSecond
request.setWaitSeconds(pollingSecond);
request.setInstanceId(this.instanceId);

ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
ConsumeMessageAction action = new ConsumeMessageAction(serviceClient, credentials, endpoint);
request.setRequestPath(topicURL + "/" + Constants.LOCATION_MESSAGES);
return action.executeWithCustomHeaders(request, callback, null);
Expand All @@ -225,6 +250,7 @@ public AsyncResult<List<Message>> asyncConsumeMessage(int num, int pollingSecond
* @throws ClientException Exception from client
*/
public void ackMessage(List<String> receiptHandles) throws ServiceException, ClientException {
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
AckMessageAction action = new AckMessageAction(serviceClient, credentials, endpoint);

AckMessageRequest request = new AckMessageRequest();
Expand All @@ -248,6 +274,7 @@ public void ackMessage(List<String> receiptHandles) throws ServiceException, Cli
*/
public AsyncResult<Void> asyncAckMessage(List<String> receiptHandles, AsyncCallback<Void> callback)
throws ServiceException, ClientException {
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
AckMessageAction action = new AckMessageAction(serviceClient, credentials, endpoint);

AckMessageRequest request = new AckMessageRequest();
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/com/aliyun/mq/http/MQProducer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.aliyun.mq.http;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.ICredentialProvider;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.mq.http.common.utils.ServiceCredentialsWrapper;
import com.aliyun.mq.http.model.AsyncCallback;
import com.aliyun.mq.http.model.AsyncResult;
import com.aliyun.mq.http.model.action.PublishMessageAction;
Expand All @@ -23,7 +27,7 @@ public class MQProducer {
/**
* object content user auth info
*/
protected ServiceCredentials credentials;
protected ICredentialProvider credentialProvider;
/**
* user mq http endpoint, ie: http://uid.mqrest.region.aliyuncs.com/
*/
Expand All @@ -42,9 +46,26 @@ public class MQProducer {
*/
protected MQProducer(String instanceId, String topicName, ServiceClient client,
ServiceCredentials credentials, URI endpoint) {
this(instanceId, topicName, client,
StaticCredentialProvider.create(Credential.builder()
.accessKeyId(credentials.getAccessKeyId())
.accessKeySecret(credentials.getAccessKeySecret())
.securityToken(credentials.getSecurityToken())
.build()), endpoint);
}

/**
* @param instanceId, instance id
* @param topicName, topic name
* @param client, ServiceClient object
* @param credentialProvider, ICredentialProvider object
* @param endpoint, user mq http endpoint, ie: http://uid.mqrest.region.aliyuncs.com/
*/
protected MQProducer(String instanceId, String topicName, ServiceClient client,
ICredentialProvider credentialProvider, URI endpoint) {
this.instanceId = instanceId;
this.serviceClient = client;
this.credentials = credentials;
this.credentialProvider = credentialProvider;
this.endpoint = endpoint;

String uri = endpoint.toString();
Expand Down Expand Up @@ -105,6 +126,7 @@ public TopicMessage publishMessage(TopicMessage msg) throws ServiceException, Cl
PublishMessageRequest request = new PublishMessageRequest();
request.setMessage(msg);
request.setInstanceId(instanceId);
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
PublishMessageAction action = new PublishMessageAction(serviceClient, credentials, endpoint);
request.setRequestPath(topicURL + "/" + Constants.LOCATION_MESSAGES);
return action.executeWithCustomHeaders(request, null);
Expand All @@ -128,6 +150,7 @@ public AsyncResult<TopicMessage> asyncPublishMessage(TopicMessage msg, AsyncCall
PublishMessageRequest request = new PublishMessageRequest();
request.setMessage(msg);
request.setInstanceId(instanceId);
ServiceCredentials credentials = ServiceCredentialsWrapper.WrapServiceCredentials(credentialProvider.getCredentials());
PublishMessageAction action = new PublishMessageAction(serviceClient, credentials, endpoint);
request.setRequestPath(topicURL + "/" + Constants.LOCATION_MESSAGES);
return action.executeWithCustomHeaders(request, callback, null);
Expand Down
Loading