Skip to content

Commit 0dfecdf

Browse files
committed
Adding functions for granting rw access to tube
``` -- call from admin user tube:grant(username, { call = true/false }) ``` closes gh-59
1 parent d58da97 commit 0dfecdf

File tree

6 files changed

+181
-13
lines changed

6 files changed

+181
-13
lines changed

queue/abstract.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,40 @@ function tube.on_task_change(self, cb)
213213
return old_cb
214214
end
215215

216+
function tube.grant(self, user, args)
217+
local function tube_grant_space(user, name, tp)
218+
box.schema.user.grant(user, tp or 'read,write', 'space', name, {
219+
if_not_exists = true,
220+
})
221+
end
222+
223+
local function tube_grant_func(user, name)
224+
box.schema.func.create(name, { if_not_exists = true })
225+
box.schema.user.grant(user, 'execute', 'function', name, {
226+
if_not_exists = true
227+
})
228+
end
229+
230+
args = args or {}
231+
232+
tube_grant_space(user, '_queue', 'read')
233+
tube_grant_space(user, '_queue_consumers')
234+
tube_grant_space(user, '_queue_taken')
235+
tube_grant_space(user, self.name)
236+
237+
if args.call then
238+
local prefix = (args.prefix or 'queue.tube') .. ('.%s:'):format(self.name)
239+
tube_grant_func(user, prefix .. 'take')
240+
tube_grant_func(user, prefix .. 'touch')
241+
tube_grant_func(user, prefix .. 'ack')
242+
tube_grant_func(user, prefix .. 'release')
243+
tube_grant_func(user, prefix .. 'peek')
244+
tube_grant_func(user, prefix .. 'bury')
245+
tube_grant_func(user, prefix .. 'kick')
246+
tube_grant_func(user, prefix .. 'delete')
247+
end
248+
end
249+
216250
-- methods
217251
local method = {}
218252

queue/compat.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,25 @@ local function get_actual_vinylname(version)
4444
return check_version({1, 7}, version) and 'vinyl' or nil
4545
end
4646

47+
local function get_optname_snapdir(version)
48+
return check_version({1, 7}, version) and 'memtx_dir' or 'snap_dir'
49+
end
50+
51+
local function get_optname_logger(version)
52+
return check_version({1, 7}, version) and 'log' or 'logger'
53+
end
54+
55+
local function pack_args(...)
56+
return check_version({1, 7}) and { ... } or ...
57+
end
58+
4759
return {
48-
split_version = split_version,
49-
check_version = check_version,
50-
vinyl_name = get_actual_vinylname,
51-
num_type = get_actual_numtype,
52-
str_type = get_actual_strtype
60+
split_version = split_version,
61+
check_version = check_version,
62+
vinyl_name = get_actual_vinylname,
63+
num_type = get_actual_numtype,
64+
str_type = get_actual_strtype,
65+
snapdir_optname = get_optname_snapdir,
66+
logger_optname = get_optname_logger,
67+
pack_args = pack_args,
5368
}

t/070-compat.t

100644100755
File mode changed.

t/080-otc-cb.t

100644100755
File mode changed.

t/090-grant-check.t

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env tarantool
2+
local test = require('tap').test()
3+
test:plan(2)
4+
5+
local test_user = 'test'
6+
local test_pass = '1234'
7+
local test_host = 'localhost'
8+
local test_port = '3388'
9+
10+
local uri = require('uri')
11+
local netbox = require('net.box')
12+
13+
local tnt = require('t.tnt')
14+
15+
tnt.cfg{
16+
listen = uri.format({ host = test_host, service = test_port })
17+
}
18+
19+
local qc = require('queue.compat')
20+
21+
test:test('check for space grants', function(test)
22+
-- prepare for tests
23+
local queue = require('queue')
24+
box.schema.user.create(test_user, { password = test_pass })
25+
26+
test:plan(5)
27+
28+
local tube = queue.create_tube('test', 'fifo')
29+
tube:put('help');
30+
local task = tube:take();
31+
test:is(task[1], 0, 'we can get record')
32+
tube:release(task[1])
33+
34+
-- checking without grants
35+
box.session.su('test')
36+
local stat, er = pcall(tube.take, tube)
37+
test:is(stat, false, 'we\'re getting error')
38+
box.session.su('admin')
39+
40+
-- checking with grants
41+
tube:grant('test')
42+
box.session.su('test')
43+
local a = tube:take()
44+
test:is(a[1], 0, 'we aren\'t getting any error')
45+
local b = tube:take(0.1)
46+
test:isnil(b, 'we aren\'t getting any error')
47+
local c = tube:ack(a[1])
48+
test:is(a[1], 0, 'we aren\'t getting any error')
49+
box.session.su('admin')
50+
51+
-- checking double grants
52+
tube:grant('test')
53+
54+
box.schema.user.drop(test_user)
55+
tube:drop()
56+
end)
57+
58+
test:test('check for call grants', function(test)
59+
-- prepare for tests
60+
_G.queue = require('queue')
61+
box.schema.user.create(test_user, { password = test_pass })
62+
63+
test:plan(9)
64+
65+
local tube = queue.create_tube('test', 'fifo')
66+
tube:put('help');
67+
local task = tube:take();
68+
test:is(task[1], 0, 'we can get record')
69+
tube:release(task[1])
70+
71+
-- checking without grants
72+
box.session.su('test')
73+
local stat, er = pcall(tube.take, tube)
74+
test:is(stat, false, 'we\'re getting error')
75+
box.session.su('admin')
76+
77+
-- checking with grants
78+
tube:grant('test')
79+
80+
box.session.su('test')
81+
local a = tube:take()
82+
test:is(a[1], 0, 'we aren\'t getting any error')
83+
local b = tube:take(0.1)
84+
test:isnil(b, 'we aren\'t getting any error')
85+
local c = tube:release(a[1])
86+
test:is(a[1], 0, 'we aren\'t getting any error')
87+
box.session.su('admin')
88+
89+
local con = netbox.connect(uri.format({
90+
login = test_user, password = test_pass,
91+
host = test_host, service = test_port,
92+
}, true))
93+
94+
local stat, er = pcall(con.call, con, tube, 'queue.tube.test:take')
95+
test:is(stat, false, 'we\'re getting error')
96+
97+
-- granting call
98+
tube:grant('test', { call = true })
99+
100+
local a = con:call('queue.tube.test:take')
101+
test:is(a[1], 0, 'we aren\'t getting any error')
102+
local b = con:call('queue.tube.test:take', qc.pack_args(0.1))
103+
test:isnil(b, 'we aren\'t getting any error')
104+
local c = con:call('queue.tube.test:ack', qc.pack_args(a[1]))
105+
test:is(a[1], 0, 'we aren\'t getting any error')
106+
107+
-- check grants again
108+
tube:grant('test', { call = true })
109+
110+
_G.queue = nil
111+
tube:drop()
112+
end)
113+
114+
tnt.finish()
115+
116+
os.exit(test:check() == true and 0 or -1)
117+
-- vim: set ft=lua :

t/tnt/init.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ local errno = require('errno')
66
local dir = os.getenv('QUEUE_TMP')
77
local cleanup = false
88

9-
local vinyl_name = require('queue.compat').vinyl_name
9+
local qc = require('queue.compat')
10+
local vinyl_name = qc.vinyl_name
11+
local snapdir_optname = qc.snapdir_optname
12+
local logger_optname = qc.logger_optname
1013

1114
if dir == nil then
1215
dir = fio.tempdir()
@@ -23,14 +26,15 @@ local function tnt_prepare(cfg_args)
2326
end
2427
end
2528

26-
cfg_args['wal_dir'] = dir
27-
cfg_args['snap_dir'] = dir
29+
cfg_args['wal_dir'] = dir
30+
cfg_args[snapdir_optname()] = dir
31+
cfg_args[logger_optname()] = fio.pathjoin(dir, 'tarantool.log')
2832
if vinyl_name() then
29-
cfg_args[vinyl_name() .. '_dir'] = dir
33+
local vinyl_optname = vinyl_name() .. '_dir'
34+
cfg_args[vinyl_optname] = dir
3035
end
31-
cfg_args['logger'] = fio.pathjoin(dir, 'tarantool.log')
3236

33-
box.cfg (cfg_args)
37+
box.cfg(cfg_args)
3438
end
3539

3640
return {
@@ -75,5 +79,3 @@ return {
7579

7680
cfg = tnt_prepare
7781
}
78-
79-

0 commit comments

Comments
 (0)