I'm developing a web site which allows the administrator to create users on RabbitMQ. I checked the RabbitMQ api doc, and found an example to create user on windows/unix prompt. The command line seens like this:
curl -i -u ADMIN_USER: ADMIN_PASSW -H "content-type:application/json" -XPUT
{"password":"PASSW_WRITTEN","tags":"administrator"}
http://IP_ADRESS:PORT/api/users/USER_NAME
Here follows the link to the doc
http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_4_1/priv/www/api/index.html
In the prompt, it works fine. But on a web site, I have no idea how to send this command.
Note I'm developping on Java Web with JSF, but any other web language example is welcome.
Thanks for any help.
After a long time searching on the internet, I finally found how to create users on RabbitMQ programmatically. Basically, you have to send a HTTP request with PUT or POST "status". Since I'm developing on Java Web, I could easily find a Java library to support me. I used Apache HTTP library, you can find it here:
http://hc.apache.org/downloads.cgi
So, my Java code it's posted below:
For libs, imports:
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.commons.codec.binary.Base64;
The code to create a new User:
// First, save your user/pw with permission to create new users.
// NOTE: this user is already created on RabbitMQ with permission to create new users
String enc = new String( Base64.encodeBase64( "USER_NAME_WITH_PERMISSION:PASS_W".getBytes() ) );
try{
HttpPut putCriaUsuario = new HttpPut( "http://RABBIT_MQ_IP:PORT/api/users/USER_NAME_TO_CREATE );
putCriaUsuario.addHeader( "Authorization", "Basic " + enc ); // RabbitMQ requires a user with create permission, create it mannually first
putCriaUsuario.addHeader( "content-type", "application/json" );
putCriaUsuario.setEntity( new StringEntity( "{\"password\":\"YOUR_PASS_WORD\",\"tags\":\"none\"}" ) );
client.execute( putCriaUsuario );
//After create, configure RabbitMQ permissions
HttpPut putConfiguraPermissoes = new HttpPut( "http://RABBIT_MQ_IP:PORT/api/permissions/QUEUE_NAME/USER_NAME_CREATED" );
putConfiguraPermissoes.addHeader( "Authorization", "Basic " + enc );
putConfiguraPermissoes.addHeader( "content-type", "application/json" );
putConfiguraPermissoes.setEntity( new StringEntity( "{\"configure\":\"^$\",\"write\":\".*\",\"read\":\".*\"}" ) ); // Permission you wanna. Check RabbitMQ HTTP API for details
client.execute( putConfiguraPermissoes );
}catch( UnsupportedEncodingException ex ){
ex.printStackTrace();
}catch( IOException ex ){
ex.printStackTrace();
}
This is Java, so it can be used on desktop application or Java Web. On other language follows the same logic, just with another libs. Hope it help us all. Fells happy for sharing knowledge!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With