perus 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48150f3119813db0b986b3a78316724d3c454600
4
- data.tar.gz: a74d668d0a6ffcb96109958d51ca504d5553cda5
3
+ metadata.gz: 2eb9babcf7c051aeca8860be3b4865d0f566d27a
4
+ data.tar.gz: 0a04455e38f26ccc96cefdca91023e0a89586db9
5
5
  SHA512:
6
- metadata.gz: 96cf0f1b9cf9e01cdf518bb14c8945ac94973e6f2d1248d8968c1e345aa037997c2bf54eee8b530b4b88de2eee90c6db7b3e33611f6a8b32916cb2843e0b8f4e
7
- data.tar.gz: 8bc42c6192e42236463339d07f865a192ac7b42781f4104a38f792ba210e32598731bcbaa493fcd3f3a1e614b2f9787b5ea0ed3c426735bfd76e8634df9939d5
6
+ metadata.gz: 0524c08e915553654605df6e52c8560562442637fe23932d7e8e3ecf2b4d64af6724ff930118ff3580a3b56495212a8392795f8268fedf44895ced29c8a6b961
7
+ data.tar.gz: 4bac65c5037b1770705274b4175fed6e66d35110beb92d9a1748dc0d208b82d14888efb9c6e6c1e354837b451355cd7bf3bec0ac78b235b9d6825524222d59aa
@@ -19,12 +19,14 @@ module Perus::Server
19
19
 
20
20
  # list
21
21
  get '/admin/#{plural}' do
22
+ protected!
22
23
  @records = #{klass}.dataset.order_by(:name).all
23
24
  erb :'admin/index'
24
25
  end
25
26
 
26
27
  # new form
27
28
  get '/admin/#{plural}/new' do
29
+ protected!
28
30
  @record = #{klass}.new
29
31
  @form = Form.new(@record)
30
32
  erb :'admin/new'
@@ -32,6 +34,7 @@ module Perus::Server
32
34
 
33
35
  # create
34
36
  post '/admin/#{plural}' do
37
+ protected!
35
38
  @record = #{klass}.new(params[:record])
36
39
  if @record.valid?
37
40
  begin
@@ -52,6 +55,7 @@ module Perus::Server
52
55
 
53
56
  # edit
54
57
  get '/admin/#{plural}/:id' do
58
+ protected!
55
59
  @record = #{klass}.with_pk!(params['id'])
56
60
  @form = Form.new(@record)
57
61
  erb :'admin/edit'
@@ -59,6 +63,7 @@ module Perus::Server
59
63
 
60
64
  # update
61
65
  put '/admin/#{plural}/:id' do
66
+ protected!
62
67
  @record = #{klass}.with_pk!(params['id'])
63
68
  if @record.valid?
64
69
  begin
@@ -75,6 +80,7 @@ module Perus::Server
75
80
 
76
81
  # delete
77
82
  delete '/admin/#{plural}/:id' do
83
+ protected!
78
84
  @record = #{klass}.with_pk!(params['id'])
79
85
  @record.destroy
80
86
  redirect url_prefix + 'admin/#{plural}'
@@ -6,19 +6,6 @@ require 'uri'
6
6
 
7
7
  module Perus::Server
8
8
  class App < Sinatra::Application
9
- def self.new(*)
10
- unless Server.options.auth['username'].empty?
11
- app = Rack::Auth::Digest::MD5.new(super) do |username|
12
- {Server.options.auth['username'] => Server.options.auth['password']}[username]
13
- end
14
- app.realm = 'Protected Area'
15
- app.opaque = 'secretkey'
16
- app
17
- else
18
- super
19
- end
20
- end
21
-
22
9
  #----------------------
23
10
  # config
24
11
  #----------------------
@@ -54,6 +41,7 @@ module Perus::Server
54
41
  end
55
42
 
56
43
  post '/admin/scripts/:id/commands' do
44
+ protected!
57
45
  script = Script.with_pk!(params['id'])
58
46
  script_command = ScriptCommand.new
59
47
  script_command.script_id = params['id']
@@ -74,6 +62,7 @@ module Perus::Server
74
62
  end
75
63
 
76
64
  post '/admin/scripts/:script_id/commands/:id' do
65
+ protected!
77
66
  script_command = ScriptCommand.with_pk!(params['id'])
78
67
  if params['action'] == 'Delete'
79
68
  script_command.destroy
@@ -85,6 +74,7 @@ module Perus::Server
85
74
  end
86
75
 
87
76
  post '/admin/configs/:id/metrics' do
77
+ protected!
88
78
  config = Config.with_pk!(params['id'])
89
79
  config_metric = ConfigMetric.new
90
80
  config_metric.config_id = params['id']
@@ -105,6 +95,7 @@ module Perus::Server
105
95
  end
106
96
 
107
97
  post '/admin/configs/:config_id/metrics/:id' do
98
+ protected!
108
99
  config_metric = ConfigMetric.with_pk!(params['id'])
109
100
  if params['action'] == 'Delete'
110
101
  config_metric.destroy
@@ -116,6 +107,7 @@ module Perus::Server
116
107
  end
117
108
 
118
109
  get '/admin/stats' do
110
+ protected!
119
111
  @stats = Stats.new
120
112
  @queue_length = Server.ping_queue.length
121
113
  erb :stats
@@ -127,6 +119,7 @@ module Perus::Server
127
119
  #----------------------
128
120
  # csv for graphs shown on system page
129
121
  get '/systems/:id/values' do
122
+ protected!
130
123
  system = System.with_pk!(params['id'])
131
124
  metrics = params[:metrics].to_s.split(',')
132
125
 
@@ -213,6 +206,7 @@ module Perus::Server
213
206
 
214
207
  # render all errors in html to replace the shortened subset on the system page
215
208
  get '/systems/:id/errors' do
209
+ protected!
216
210
  system = System.with_pk!(params['id'])
217
211
  errors = system.collection_errors
218
212
  erb :errors, layout: false, locals: {errors: errors}
@@ -220,6 +214,7 @@ module Perus::Server
220
214
 
221
215
  # clear collection errors
222
216
  delete '/systems/:id/errors' do
217
+ protected!
223
218
  system = System.with_pk!(params['id'])
224
219
  system.collection_errors.each(&:delete)
225
220
  redirect "#{url_prefix}systems/#{system.id}"
@@ -227,12 +222,14 @@ module Perus::Server
227
222
 
228
223
  # create a new action
229
224
  post '/systems/:id/actions' do
225
+ protected!
230
226
  Action.add(params['id'], params)
231
227
  redirect "#{url_prefix}systems/#{params['id']}#actions"
232
228
  end
233
229
 
234
230
  # create an action for all systems in a group
235
231
  post '/groups/:id/systems/actions' do
232
+ protected!
236
233
  group = Group.with_pk!(params['id'])
237
234
  group.systems.each do |system|
238
235
  Action.add(system.id, params)
@@ -243,6 +240,7 @@ module Perus::Server
243
240
 
244
241
  # delete completed actions in a group
245
242
  delete '/groups/:id/systems/actions' do
243
+ protected!
246
244
  group = Group.with_pk!(params['id'])
247
245
  group.systems.each do |system|
248
246
  system.actions.each do |action|
@@ -256,6 +254,7 @@ module Perus::Server
256
254
 
257
255
  # create an action for all systems
258
256
  post '/systems/actions' do
257
+ protected!
259
258
  System.each do |system|
260
259
  Action.add(system.id, params)
261
260
  end
@@ -265,6 +264,7 @@ module Perus::Server
265
264
 
266
265
  # delete all completed actions
267
266
  delete '/systems/actions' do
267
+ protected!
268
268
  Action.each do |action|
269
269
  next if action.timestamp.nil?
270
270
  action.destroy
@@ -275,6 +275,7 @@ module Perus::Server
275
275
 
276
276
  # delete an action. deletion also clears any uploaded files.
277
277
  delete '/systems/:system_id/actions/:id' do
278
+ protected!
278
279
  action = Action.with_pk!(params['id'])
279
280
  action.destroy
280
281
  redirect "#{url_prefix}systems/#{params['system_id']}#actions"
@@ -286,6 +287,7 @@ module Perus::Server
286
287
  #----------------------
287
288
  # overview
288
289
  get '/' do
290
+ protected!
289
291
  systems = System.all
290
292
  @alerts = Alert.all.sort_by(&:severity_level).reverse
291
293
  erb :index
@@ -293,6 +295,7 @@ module Perus::Server
293
295
 
294
296
  # list of systems
295
297
  get '/systems' do
298
+ protected!
296
299
  @systems = System.all.group_by(&:orientation)
297
300
  @title = 'All Systems'
298
301
  @scripts = Script.all
@@ -302,6 +305,7 @@ module Perus::Server
302
305
 
303
306
  # list of systems by group
304
307
  get '/groups/:id/systems' do
308
+ protected!
305
309
  group = Group.with_pk!(params['id'])
306
310
  @systems = group.systems_dataset.order_by(:name).all.group_by(&:orientation)
307
311
  @title = group.name
@@ -312,6 +316,7 @@ module Perus::Server
312
316
 
313
317
  # info page for a system
314
318
  get '/systems/:id' do
319
+ protected!
315
320
  @system = System.with_pk!(params['id'])
316
321
  @uploads = @system.upload_urls
317
322
  metrics = @system.metrics
@@ -348,6 +353,7 @@ module Perus::Server
348
353
 
349
354
  # helper to make uploads publicly accessible
350
355
  get '/uploads/*' do
356
+ protected!
351
357
  path = params['splat'][0]
352
358
  raise 'Invalid path' if path.include?('..')
353
359
  full_path = File.join(Server.options.uploads_dir, path)
@@ -46,5 +46,21 @@ module Perus::Server
46
46
  def url_prefix
47
47
  Server.options.url_prefix
48
48
  end
49
+
50
+ def protected!
51
+ return if authorised?
52
+ headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
53
+ halt 401, "Not authorized\n"
54
+ end
55
+
56
+ def authorised?
57
+ return true if Server.options.auth['username'].empty?
58
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
59
+ @auth.provided? && @auth.basic? && @auth.credentials &&
60
+ @auth.credentials == [
61
+ Server.options.auth['username'],
62
+ Server.options.auth['password']
63
+ ]
64
+ end
49
65
  end
50
66
  end
data/lib/perus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Perus
2
- VERSION = "0.1.20"
2
+ VERSION = "0.1.21"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Cannings
@@ -338,4 +338,3 @@ signing_key:
338
338
  specification_version: 4
339
339
  summary: Simple system overview server
340
340
  test_files: []
341
- has_rdoc: