File: pools_rest_utils.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (385 lines) | stat: -rw-r--r-- 10,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
--
-- (C) 2017-22 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path

require "lua_utils"
local json = require "dkjson"
local rest_utils = require "rest_utils"
local pools = require "pools"
local pools_lua_utils = require "pools_lua_utils"
local host_pools = require "host_pools"
local tracker = require("tracker")
local auth = require "auth"

-- ##############################################

local pools_rest_utils = {}

-- ##############################################

-- @brief Add a pool
function pools_rest_utils.add_pool(pools)
   local name = _POST["pool_name"]
   local members = _POST["pool_members"]
   local recipients = _POST["recipients"]

   if not auth.has_capability(auth.capabilities.pools) then
      rest_utils.answer(rest_utils.consts.err.not_granted)
      return
   end

   if not name or not members then
      rest_utils.answer(rest_utils.consts.err.invalid_args)
      return
   end

   -- Create an instance out of the `pools` passed as argument
   local s = pools:create()

   -- Too many pools created for this version
   if s:get_num_pools() >= s:get_max_num_pools() then
      if ntop.isEnterpriseM() then
	 rest_utils.answer(rest_utils.consts.err.add_pool_failed_too_many_pools_enterprise)
      else
	 rest_utils.answer(rest_utils.consts.err.add_pool_failed_too_many_pools)
      end
      return
   end

   members_list = s:parse_members(members)
   recipients = s:parse_recipients(recipients)

   local new_pool_id = s:add_pool(
      name,
      members_list --[[ an array of valid interface ids]],
      recipients --[[ an array of valid recipient ids (names)]]
   )

   if not new_pool_id then
      rest_utils.answer(rest_utils.consts.err.add_pool_failed)
      return
   end

   local rc = rest_utils.consts.success.pool_added
   local res = {
      pool_id = new_pool_id
   }

   rest_utils.answer(rc, res)

   -- TRACKER HOOK
   tracker.log('add_pool', { pool_name = name, members = members, pool_key = s.key })
end

-- ##############################################

-- @brief Edit a pool
function pools_rest_utils.edit_pool(pools)
   local pool_id = _POST["pool"]
   local name = _POST["pool_name"]
   local members = _POST["pool_members"]
   local recipients = _POST["recipients"]

   if not auth.has_capability(auth.capabilities.pools) then
      rest_utils.answer(rest_utils.consts.err.not_granted)
      return
   end

   if not pool_id or not name or not members then
      rest_utils.answer(rest_utils.consts.err.invalid_args)
      return
   end

   -- Create the instance
   local s = pools:create()

   members_list = s:parse_members(members)
   recipients = s:parse_recipients(recipients)
   -- pool_id as number
   pool_id = tonumber(pool_id)

   local res = s:edit_pool(pool_id,
      name,
      members_list --[[ an array of valid interface ids]], 
      recipients --[[ an array of valid recipient ids (names)]]
   )

   if not res then
      rest_utils.answer(rest_utils.consts.err.edit_pool_failed)
      return
   end

   local rc = rest_utils.consts.success.pool_edited
   rest_utils.answer(rc)

   -- TRACKER HOOK
   tracker.log('edit_pool', { pool_id = pool_id, pool_name = name, members = members, pool_key = s.key })
end

-- ##############################################

-- @brief Delete a pool
function pools_rest_utils.delete_pool(pools)
   local pool_id = _POST["pool"]

   if not auth.has_capability(auth.capabilities.pools) then
      rest_utils.answer(rest_utils.consts.err.not_granted)
      return
   end

   if not pool_id then
      rest_utils.answer(rest_utils.consts.err.invalid_args)
      return
   end

   -- pool_id as number
   pool_id = tonumber(pool_id)

   -- Create the instance
   local s = pools:create()

   -- Fetch the existing pool
   local existing_pool = s:get_pool(pool_id)
   if not existing_pool then
      rest_utils.answer(rest_utils.consts.err.pool_not_found)
      return
   end

   -- Delete the pool
   local res = s:delete_pool(pool_id)
   if not res then
      rest_utils.answer(rest_utils.consts.err.pool_not_found)
      return
   end

   local rc = rest_utils.consts.success.pool_deleted
   local res = {
      pool_id = new_pool_id
   }

   rest_utils.answer(rc, res)

   -- TRACKER HOOK
   tracker.log('delete_pool', { pool_id = pool_id,  pool_name = existing_pool["name"], pool_key = s.key })
end

-- ##############################################

-- @brief Bind a member to a pool
function pools_rest_utils.bind_member(pools)
   local old_pool_name = _GET["pool_name"]
   local pool_id = _GET["pool"]
   local member = _POST["member"]
   local action = _POST["action"]
   local old_member = _POST["old_member"]

   if not isAdministrator() then
      rest_utils.answer(rest_utils.consts.err.not_granted)
      return
   end

   if not pool_id or not member then
      rest_utils.answer(rest_utils.consts.err.invalid_args)
      return
   end

   -- pool_id as number
   pool_id = tonumber(pool_id)

   -- Create the instance
   local s = pools:create()
   local res, err

   if action == 'edit' then
      res, err = s:bind_member(old_member, s.DEFAULT_POOL_ID)
   end

   if pool_id == s.DEFAULT_POOL_ID then
      -- Always bind the member to the default pool id (possibly removing it from any other pool)
      res, err = s:bind_member(member, pool_id)
      if old_pool_name == host_pools.DROP_HOST_POOL_NAME and ntop.isPro() then
	 package.path = dirs.installdir .. "/pro/scripts/lua/modules/?.lua;" .. package.path
	 local policy_utils = require "policy_utils"

	 policy_utils.broadcast_ips_rules()
      end
   else
      -- Bind the member only if it is not already in another pool
      res, err = s:bind_member_if_not_already_bound(member, pool_id)
   end

   if not res then
      if err == pools.ERRORS.ALREADY_BOUND then
	 -- Member already existing, return current pool information in the response
	 local cur_pool = s:get_pool_by_member(member)
	 rest_utils.answer(rest_utils.consts.err.bind_pool_member_already_bound, cur_pool)
      else
	 -- Generic
	 rest_utils.answer(rest_utils.consts.err.bind_pool_member_failed)
      end

      return
   end

   local rc = rest_utils.consts.success.pool_member_bound
   rest_utils.answer(rc)

   local dst_pool = s:get_pool(pool_id)

   -- TRACKER HOOK
   tracker.log('bind_pool_member', { pool_id = pool_id, pool_name = dst_pool["name"], member = member, pool_key = s.key })
end

-- ##############################################

-- @brief Get one or all pools
function pools_rest_utils.get_pools(pools)
   local pool_id = _GET["pool"]

   -- pool_id as number
   pool_id = tonumber(pool_id)

   local res = {}

   -- Create the instance
   local s = pools:create()

   if pool_id then
      -- Return only one pool
      local cur_pool = s:get_pool(pool_id)

      if cur_pool then
	 res[pool_id] = cur_pool
      else
	 rest_utils.answer(rest_utils.consts.err.pool_not_found)
	 return
      end
   else
      -- Return all pool ids
      res = s:get_all_pools()
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc, res)
end

-- ##############################################

-- @brief Get all pools of all the available (currently implemented) pool instances matching a given `recipient_id`
function pools_rest_utils.get_all_instances_pools_by_recipient(recipient_id)
   local res = {}
   local all_instances = pools_lua_utils.all_pool_instances_factory()

   for _, instance in pairs(all_instances) do
      local instance_pools = instance:get_all_pools()

      for _, instance_pool in pairs(instance_pools) do
	 instance_pool["key"] = instance.key -- e.g., 'interface', 'host', etc.
	 for _, recipient in pairs(instance_pool["recipients"]) do
	    if tonumber(recipient.recipient_id) == (recipient_id) then
	       -- Match, return the recipient
	       instance_pool["key"] = instance.key -- e.g., 'interface', 'host', etc.
	       res[#res + 1] = instance_pool
	       break
	    end
	 end
      end
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc, res)
end

-- ##############################################

-- @brief Get all pools of all the available (currently implemented) pool instances
function pools_rest_utils.get_all_instances_pools()
   local res = {}
   local all_instances = pools_lua_utils.all_pool_instances_factory()

   for _, instance in pairs(all_instances) do
      local instance_pools = instance:get_all_pools()

      for _, instance_pool in pairs(instance_pools) do
	 instance_pool["key"] = instance.key -- e.g., 'interface', 'host', etc.
	 res[#res + 1] = instance_pool
      end
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc, res)
end

-- ##############################################

-- @brief Get all pools of all the available (currently implemented) pool instances
function pools_rest_utils.delete_all_instances_pools()
   local all_instances = pools_lua_utils.all_pool_instances_factory()

   for _, instance in pairs(all_instances) do
      instance:cleanup()
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc)
end

-- ##############################################

-- @brief Get one or all pools
function pools_rest_utils.get_pool_members(pools)
   local pool_id = _GET["pool"]

   -- pool_id as number
   pool_id = tonumber(pool_id)

   local res = {}

   -- Create the instance
   local s = pools:create()

   local cur_pool = s:get_pool(pool_id)

   if not cur_pool then
      rest_utils.answer(rest_utils.consts.err.pool_not_found)
      return
   end

   for member, details in pairs(cur_pool["member_details"]) do
      details["member"] = member
      res[#res + 1] = details
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc, res)
end

-- ##############################################

-- @brief Get one or all pools
function pools_rest_utils.get_pool_by_member(pools)
   local member = _POST["member"]

   if not member then
      rest_utils.answer(rest_utils.consts.err.invalid_args)
      return
   end

   local res = {}

   -- Create the instance
   local s = pools:create()
   local cur_pool = s:get_pool_by_member(member)
   if cur_pool then
      res = cur_pool
   end

   local rc = rest_utils.consts.success.ok
   rest_utils.answer(rc, res)
end

-- ##############################################

return pools_rest_utils