Creates a static username and password for open-source clients to access the ApsaraMQ for RabbitMQ server. Clients must pass authentication to access the server. ApsaraMQ for RabbitMQ allows you to generate a username and password by using the AccessKey ID and AccessKey secret of a Resource Access Management (RAM) user.
Operation description
Background information
When an open-source client accesses the ApsaraMQ for RabbitMQ server, it must provide a username and password for authentication. The client can access the server only after passing authentication. ApsaraMQ for RabbitMQ allows you to generate a username and password by using the AccessKey ID and AccessKey secret of a Resource Access Management (RAM) user.
Precautions
The instance for which you want to create a static username and password must be in the Running state.
An AccessKey ID corresponds to only one static username and password within an instance. You can create only one static username and password for each AccessKey ID in an instance.
To update a static username and password, delete the existing static username and password and create a new one.
Sample code for calculating the username
The value of the userName request parameter is obtained by constructing a string with the instance ID and AccessKey ID, and then encoding the string in Base64. The calculation method is as follows:
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Utils {
public static final Charset UTF8 = StandardCharsets.UTF_8;
/**
* Decodes a Base64 string.
*
* @param str
* @return
*/
public static String decode(String str) {
return new String(Base64.getDecoder().decode(str.getBytes(UTF8)), UTF8);
}
public static String encode(String bytes) {
return new String(Base64.getEncoder().encode(bytes.getBytes(UTF8)), UTF8);
}
public static void main(String[] args) {
// Use your instance ID and the AccessKey ID of the account that purchased the instance to construct a string.
String str = "2:${instanceId}:${ak}";
// Generate the userName used for the API.
String userName = encode(str);
System.out.println("userName=" + userName);
String decodeStr = decode(userName);
System.out.println("originStr=" + decodeStr);
}
}
Sample code for the signature algorithm
The values of the signature and secretSign request parameters are calculated by using an encryption algorithm with the creation timestamp of the specified static username and the AccessKey secret. The specific algorithm and sample code are as follows:
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class SignatureUtils {
public static String hamcsha1(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
return byte2hex(mac.doFinal(data));
}
public static String byte2hex(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
hs.append('0');
}
hs.append(stmp);
}
return hs.toString().toUpperCase();
}
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
// createTimestamp is the createTimestamp parameter of the API.
String createTimestamp = String.valueOf(System.currentTimeMillis());
System.out.println("timestamp:" + createTimestamp);
// The AccessKey secret of your account.
String sk = "${sk}";
// Generate the SecretSign used for the API.
String SecretSign = hamcsha1(sk.getBytes(), createTimestamp.getBytes());
System.out.println("SecretSign:" + SecretSign);
// Generate the signature used for the API.
String signature = hamcsha1(createTimestamp.getBytes(), sk.getBytes());
System.out.println("signature:" + signature);
}
}
Try it now
Test
RAM authorization
|
Action |
Access level |
Resource type |
Condition key |
Dependent action |
|
amqp:FetchStaticAccount |
create |
*Instance
|
None |
|
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
| instanceId |
string |
Yes |
The ID of the ApsaraMQ for RabbitMQ instance for which you want to create a static username and password. |
amqp-cn-********* |
| accountAccessKey |
string |
Yes |
The AccessKey ID of your Alibaba Cloud account or Resource Access Management (RAM) user. For more information about how to obtain an AccessKey ID, see Create an AccessKey pair. Note
If you use the AccessKey ID of a Resource Access Management (RAM) user to create a static username and password for connecting to ApsaraMQ for RabbitMQ and sending or receiving messages, make sure that the Resource Access Management (RAM) user is granted the permissions to send and receive messages. For more information, see RAM access policy. |
yourAccessKeyID |
| userName |
string |
Yes |
The static username to be created. The value of this parameter is obtained by constructing a string with the instance ID and AccessKey ID and then encoding the string in Base64. For more information about the calculation method, see the Sample code for calculating the username section in this topic. |
Mjo**************** |
| signature |
string |
Yes |
The signature. The system calculates the static user password based on the signature, the AccessKey secret signature, and the username in the request parameters. The signature is calculated by using the HMAC-SHA1 encryption algorithm to encrypt the creation UNIX timestamp of the specified username and the AccessKey ID. For more information about the calculation method, see the Sample code for the signature algorithm section in this topic. |
22c2d7d1769cb53c5a6d9213248e2de524****** |
| createTimestamp |
integer |
Yes |
The timestamp when the username and password are created. Unit: milliseconds. Note
This timestamp is used to calculate the static user password and is custom-defined. It is not the timestamp when the system generates the username and password. |
1671175303522 |
| secretSign |
string |
Yes |
The AccessKey secret signature. The system calculates the static user password based on the signature, the AccessKey secret signature, and the username in the request parameters. The AccessKey secret signature is calculated by using the HMAC-SHA1 encryption algorithm to encrypt the creation UNIX timestamp of the specified username and the AccessKey ID. For more information about the calculation method, see the Sample code for the signature algorithm section in this topic. |
4c1a6367ce4c4255e9617326f9133ac635****** |
| Remark |
string |
No |
The remark for the static user. |
*** environment |
Response elements
|
Element |
Type |
Description |
Example |
|
object |
The returned result. |
||
| RequestId |
string |
The request ID. |
FEBA5E0C-50D0-4FA6-A794-4901E5465*** |
| Code |
integer |
The return code. A value of 200 indicates success. |
200 |
| Message |
string |
The returned message. |
operation success |
| Success |
boolean |
Indicates whether the request was successful. |
true |
| Data |
object |
The returned data. |
|
| AccessKey |
string |
The AccessKey ID used to create the username and password. |
LTAI**************** |
| Password |
string |
The created static user password. |
OUYwQzM2QjZBRkUxNDRFM***************MzZCNzdDQzoxNjcxNDMwMzkyODI1 |
| CreateTimeStamp |
integer |
The timestamp when the username and password were created. Unit: milliseconds. |
1671175303522 |
| InstanceId |
string |
The ID of the ApsaraMQ for RabbitMQ instance. |
amqp-cn-********* |
| MasterUId |
integer |
The ID of the Alibaba Cloud account or Resource Access Management (RAM) user to which the AccessKey used to create the static username and password belongs. |
1565***********01 |
| UserName |
string |
The created static username. |
MjphbXFwLWNuLXVxbTJ6cjc2djAwMzpMVEFJNX*******ZNMWVSWnRFSjZ2Zm8= |
| Remark |
string |
The remark. |
*** environment |
Examples
Success response
JSON format
{
"RequestId": "FEBA5E0C-50D0-4FA6-A794-4901E5465***",
"Code": 200,
"Message": "operation success",
"Success": true,
"Data": {
"AccessKey": "LTAI****************",
"Password": "OUYwQzM2QjZBRkUxNDRFM***************MzZCNzdDQzoxNjcxNDMwMzkyODI1",
"CreateTimeStamp": 1671175303522,
"InstanceId": "amqp-cn-*********",
"MasterUId": 0,
"UserName": "MjphbXFwLWNuLXVxbTJ6cjc2djAwMzpMVEFJNX*******ZNMWVSWnRFSjZ2Zm8=",
"Remark": "*** environment"
}
}
Error codes
See Error Codes for a complete list.
Release notes
See Release Notes for a complete list.