Skip to content

Commit 701e311

Browse files
committed
[bxrabbitmq] +permissions
1 parent 054a442 commit 701e311

File tree

7 files changed

+144
-13
lines changed

7 files changed

+144
-13
lines changed

examples/management/ex1.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int main ()
1616
bool ok;
1717
rabbitmq::error_response error;
1818
rabbitmq::vhost::list vhosts;
19-
rabbitmq::rabbit_mgr mgr ("localhost", 15672, "guest", "guest");
19+
rabbitmq::rabbit_mgr mgr ("localhost", 15671, "admin", "what");
2020

2121
ok = mgr.list_vhosts (vhosts, error);
2222
if (ok) {

examples/tutorials/t1_receive.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ void receive ()
3232
{
3333
std::clog << "\nTUTORIAL 1 : 'Hello World' - receive\n\n" ;
3434
rabbitmq::connection_parameters c_par;
35+
c_par.host = "caerabbitmq.in2p3.fr";
36+
c_par.port = 5671;
37+
c_par.login = "guest;
38+
c_par.passwd = "guest";
3539
rabbitmq::connection con (c_par);
3640
if (con.is_ok ()) {
3741
std::string routing_key;

examples/tutorials/t1_send.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ void send ()
2828
{
2929
std::clog << "\nTUTORIAL 1 : 'Hello World' - send\n\n" ;
3030
rabbitmq::connection_parameters c_par;
31+
c_par.host = "caerabbitmq.in2p3.fr";
32+
c_par.port = 5671;
33+
c_par.login = "guest";
34+
c_par.passwd = "guest";
3135
rabbitmq::connection con (c_par);
3236
if (con.is_ok ()) {
3337
rabbitmq::channel & chan = con.grab_channel ();

src/rabbitmq/management.cc

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace rabbitmq {
100100
}
101101

102102

103-
/*** PERMISSION **********************************************************/
103+
/*** PERMISSIONS **********************************************************/
104104

105105
void permissions::jsonize (jsontools::node & node_,
106106
const unsigned long int /* version_ */)
@@ -113,5 +113,100 @@ namespace rabbitmq {
113113
return;
114114
}
115115

116+
void permissions::clear () {
117+
user = "";
118+
vhost = "";
119+
configure = "";
120+
write = "";
121+
read = "";
122+
}
123+
124+
void permissions::vire_add_exchange_request_perms (const std::string exchange_name) {
125+
add_amqgen_to (configure);
126+
add_exchange_to (write, exchange_name);
127+
add_amqgen_to (read);
128+
}
129+
130+
void permissions::vire_add_exchange_service_perms (const std::string exchange_name) {
131+
add_amqgen_to (configure);
132+
add_amqdef_to (write);
133+
add_amqgen_to (write);
134+
add_exchange_to (read, exchange_name);
135+
add_amqgen_to (read);
136+
}
137+
138+
void permissions::vire_add_exchange_event_producer_perms (const std::string exchange_name) {
139+
add_exchange_to (write, exchange_name);
140+
}
141+
142+
void permissions::vire_add_exchange_event_listener_perms (const std::string exchange_name) {
143+
add_amqgen_to (configure);
144+
add_amqgen_to (write);
145+
add_exchange_to (read, exchange_name);
146+
add_amqgen_to (read);
147+
}
148+
149+
void permissions::vire_add_direct_request_perms () {
150+
add_amqgen_to (configure);
151+
add_amqdef_to (write);
152+
add_amqgen_to (read);
153+
}
154+
155+
void permissions::vire_add_direct_service_perms (const std::string queue_name) {
156+
add_amqdef_to (write);
157+
add_queue_to (read, queue_name);
158+
}
159+
160+
void permissions::vire_add_direct_event_producer_perms () {
161+
add_amqdef_to (write);
162+
}
163+
164+
void permissions::vire_add_direct_event_listener_perms (const std::string queue_name) {
165+
add_queue_to (read, queue_name);
166+
}
167+
168+
void permissions::add_amqgen_to (std::string & single_perm) {
169+
const std::string AMQGEN_PERM ("^amq\\.gen*");
170+
add_regexp_to (single_perm, AMQGEN_PERM);
171+
}
172+
173+
void permissions::add_amqdef_to (std::string & single_perm) {
174+
const std::string AMQDEF_PERM ("^amq\\.default$");
175+
add_regexp_to (single_perm, AMQDEF_PERM);
176+
}
177+
178+
void permissions::add_queue_to (std::string & single_perm, const std::string queue_name) {
179+
add_mbox_to (single_perm, queue_name);
180+
}
181+
182+
void permissions::add_exchange_to (std::string & single_perm, const std::string exchange_name) {
183+
add_mbox_to (single_perm, exchange_name);
184+
}
185+
186+
void permissions::add_mbox_to (std::string & single_perm, const std::string mbox) {
187+
const std::string mbox_perm = mbox_to_regexp (mbox);
188+
add_regexp_to (single_perm, mbox_perm);
189+
}
190+
191+
void permissions::add_regexp_to (std::string & single_perm, const std::string regexp) {
192+
if (single_perm == "") {
193+
single_perm = regexp;
194+
} else if (single_perm.find (regexp) == std::string::npos) {
195+
single_perm = single_perm + "|" + regexp;
196+
}
197+
}
198+
199+
std::string permissions::mbox_to_regexp (const std::string & mbox) {
200+
size_t pos = 0;
201+
std::string reg_mbox (mbox);
202+
while ((pos = reg_mbox.find (".", pos)) != std::string::npos) {
203+
reg_mbox.replace (pos, 1, "\\.");
204+
pos = pos + 2;
205+
}
206+
reg_mbox = "^" + reg_mbox;
207+
reg_mbox = reg_mbox + "$";
208+
return reg_mbox;
209+
}
210+
116211

117212
} // end of namespace rabbitmq

src/rabbitmq/management.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#include <string>
88
#include <list>
99

10-
// Third party:
11-
// - Bayeux/jsontools:
10+
// Bayeux/jsontools:
1211
#include "jsontools/i_jsonizable.h"
1312

1413
namespace rabbitmq {
@@ -101,6 +100,31 @@ namespace rabbitmq {
101100
std::string configure;
102101
std::string write;
103102
std::string read;
103+
104+
void clear ();
105+
106+
// VIRE Permissions setup (TODO move this to a better place)
107+
void vire_add_exchange_request_perms (const std::string exchange_name);
108+
void vire_add_exchange_service_perms (const std::string exchange_name);
109+
void vire_add_exchange_event_producer_perms (const std::string exchange_name);
110+
void vire_add_exchange_event_listener_perms (const std::string exchange_name);
111+
void vire_add_direct_request_perms ();
112+
void vire_add_direct_service_perms (const std::string queue_name);
113+
void vire_add_direct_event_producer_perms ();
114+
void vire_add_direct_event_listener_perms (const std::string queue_name);
115+
116+
// Single permission modifiers (single perm is conf, writ or read)
117+
static void add_amqgen_to (std::string & single_perm);
118+
static void add_amqdef_to (std::string & single_perm);
119+
static void add_queue_to (std::string & single_perm, const std::string queue_name);
120+
static void add_exchange_to (std::string & single_perm, const std::string exchange_name);
121+
122+
private :
123+
// Mailbox name and single perm tools
124+
static void add_mbox_to (std::string & single_perm, const std::string mbox_name);
125+
static void add_regexp_to (std::string & single_perm, const std::string regexp);
126+
static std::string mbox_to_regexp (const std::string & mbox); // mbox is a queue or an exchange
127+
104128
};
105129

106130

src/rabbitmq/rabbit_mgr.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ namespace rabbitmq {
277277
return _request_perform_ (request, error_);
278278
}
279279

280+
bool rabbit_mgr::set_permissions (const permissions & perms_,
281+
error_response & error_)
282+
{
283+
return set_permissions (perms_.user, perms_.vhost, perms_.configure, perms_.write, perms_.read, error_);
284+
}
285+
280286

281287
/*** private ************************************************************************/
282288

src/rabbitmq/rabbit_mgr.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
#ifndef BXRABBITMQ_RABBIT_MGR_H
44
#define BXRABBITMQ_RABBIT_MGR_H
55

6-
// Third party:
7-
// - cURLpp:
86
#include <curlpp/Easy.hpp>
97

10-
// This project:
118
#include "rabbitmq/management.h"
129

1310
namespace rabbitmq {
@@ -20,19 +17,17 @@ namespace rabbitmq {
2017
static const uint16_t SSL_PORT = 15671;
2118

2219
public:
23-
// Constructor & co
20+
/// Constructor & co
2421
rabbit_mgr (const std::string & server_host_,
2522
const uint16_t server_port_,
2623
const std::string & user_login_,
2724
const std::string & user_passwd_);
28-
29-
// Dtor & co : forbidden
25+
// Ctor & co : forbidden
26+
~rabbit_mgr ();
3027
rabbit_mgr () = delete;
3128
rabbit_mgr (const rabbit_mgr & mgr_) = delete;
3229
rabbit_mgr & operator = (const rabbit_mgr & mgr_) = delete;
33-
34-
// Dtor
35-
~rabbit_mgr ();
30+
//
3631

3732
// TEST
3833
bool test (std::string & response_);
@@ -107,6 +102,9 @@ namespace rabbitmq {
107102
const std::string & read_,
108103
error_response & error_);
109104

105+
bool set_permissions (const permissions & perms_,
106+
error_response & error_);
107+
110108
// TODO set_perm & bindings
111109

112110
private:

0 commit comments

Comments
 (0)