marty 2.4.4 → 2.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c54e0da8076cd506b9ae72e1ada230cd4893c16398e865188f997cfd78177314
4
- data.tar.gz: 9d2235393db1114e9a212966163085ef86841a194b147f94dafeae2f84dd64f3
3
+ metadata.gz: 47d75b731f7458392df1acc9383452faf199aeb2f414fecf70c208f7700fae46
4
+ data.tar.gz: 1d50e28afa9e48074f4c87d74ce9c23834ce995ffa3c13142bf00a19d4af51b0
5
5
  SHA512:
6
- metadata.gz: 2ed996e67795195c99962721ca6620ccfc7d9e6ae3e732d8fb931711584613666ad5c133791bbb02d97099d366cb676949f1f19e8e341ce489dcb90f5ecd2760
7
- data.tar.gz: 70f0167540c03d1e8151e7750e0f4e2d9ba5b000266363631be1cf4da77b4301f06d5c232ce3008a87484eb042696b928037eb63689e0304808951416e709c0d
6
+ metadata.gz: d144bce2db55a1cd1023bf223839da9d5f5570b520926d06951e60a244563d4c5c2eaca8b9dd990bee4dd846895fc6cb05e4f07b92f691970f15cad7a0ff52c7
7
+ data.tar.gz: 2ce052dae89ccda7d9648d4ce2ea751b226281d7c7bb5ea3db369f52886e8ec83591941f0a58c1122067b81b2456b0b06a39217425b017f47dbfcce297378f8a
@@ -1,37 +1,63 @@
1
1
  class Marty::RpcController < ActionController::Base
2
2
  def evaluate
3
- massaged_params = massage_params(params)
4
- api_config = Marty::ApiConfig.lookup(*massaged_params.values_at(
5
- :script,
6
- :node,
7
- :attr)
8
- ) || {}
3
+ begin
4
+ # set default result and params in case of unexpected errors
5
+ # to ensure logging capabilities
6
+ result = nil
7
+ api_params = {}
8
+ start_time = nil
9
+ auth = nil
9
10
 
10
- api = api_config.present? ? api_config[:api_class].constantize :
11
- Marty::Api::Base
11
+ # massage request params
12
+ massaged_params = massage_params(params)
12
13
 
13
- api.respond_to(self) do
14
- next massaged_params if massaged_params.include?(:error)
14
+ # resolve api config in order to determine api class and settings
15
+ api_config = Marty::ApiConfig.lookup(*massaged_params.values_at(
16
+ :script,
17
+ :node,
18
+ :attr)
19
+ ) || {}
15
20
 
16
- auth = api.is_authorized?(massaged_params)
17
- next {error: "Permission denied"} unless auth
21
+ # default to base class if no config is present
22
+ api = api_config.present? ? api_config[:api_class].constantize :
23
+ Marty::Api::Base
18
24
 
19
- start_time = Time.zone.now
25
+ return result = massaged_params if massaged_params.include?(:error)
20
26
 
21
27
  api_params = api.process_params(massaged_params)
28
+ auth = api.is_authorized?(massaged_params)
29
+ return result = {error: "Permission denied"} unless auth
30
+
31
+ start_time = Time.zone.now
22
32
  api.before_evaluate(api_params)
23
33
  result = api.evaluate(api_params, request, api_config)
24
34
  api.after_evaluate(api_params, result)
35
+ rescue => e
36
+ # log unexpected failures in rpc controller and respond with
37
+ # generic server error
38
+ Marty::Logger.log('rpc_controller', 'failure', e.message)
39
+ result = {error: 'internal server error'}
40
+ ensure
41
+ # if logging is enabled, always log the result even on error
42
+ if api_config && api_config[:logged] && api
43
+ api.log(result,
44
+ api_params + {start_time: start_time, auth: auth},
45
+ request)
46
+ end
25
47
 
26
- log_params = {start_time: start_time, auth: auth}
27
- api.log(result, api_params + log_params, request) if
28
- api_config[:logged]
29
-
30
- result
48
+ api.respond_to(self) do
49
+ result || {'error' => 'internal server error'}
50
+ end
31
51
  end
32
52
  end
33
53
 
34
54
  private
55
+ def process_active_params params
56
+ # must permit params before conversion to_h
57
+ # convert hash to json and parse to get expected hash (not indifferent)
58
+ params.permit!
59
+ JSON.parse(params.to_h.to_json)
60
+ end
35
61
 
36
62
  def massage_params request_params
37
63
  sname,
@@ -66,13 +92,10 @@ class Marty::RpcController < ActionController::Base
66
92
  case params
67
93
  when String
68
94
  params = ActiveSupport::JSON.decode(params)
69
- when ActionController::Parameters
70
- # must permit params before conversion to_h
71
- # convert hash to json and parse to get expected hash (not indifferent)
72
- params.permit!
73
- params = JSON.parse(params.to_h.to_json)
74
95
  when nil
75
96
  params = {}
97
+ when ActionController::Parameters
98
+ params = process_active_params(params)
76
99
  else
77
100
  return {error: "Bad params"}
78
101
  end
@@ -82,14 +105,10 @@ class Marty::RpcController < ActionController::Base
82
105
 
83
106
  return {error: "Malformed params"} unless params.is_a?(Hash)
84
107
 
85
- {
86
- script: sname,
87
- tag: tag,
88
- node: node,
108
+ # permit request params and convert to hash
109
+ process_active_params(request_params.except(:rpc)).symbolize_keys + {
89
110
  attr: attr,
90
111
  params: params,
91
- api_key: api_key,
92
- background: background,
93
112
  return_array: ret_arr
94
113
  }
95
114
  end
data/lib/marty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Marty
2
- VERSION = "2.4.4"
2
+ VERSION = "2.4.5"
3
3
  end
@@ -166,23 +166,25 @@ class Marty::Api::Base
166
166
  end
167
167
 
168
168
  def self.filter_hash hash, filter_params
169
+ return unless hash
169
170
  pf = ActionDispatch::Http::ParameterFilter.new(filter_params)
170
171
  pf.filter(hash)
171
172
  end
172
173
 
173
174
  def self.log_hash result, params, request
175
+ res = result.is_a?(Hash) ? result.stringify_keys : result
174
176
  ret_arr = params[:return_array]
175
177
  input = filter_hash(params[:params], engine_params_filter)
176
178
  {script: params[:script],
177
179
  node: params[:node],
178
180
  attrs: ret_arr ? [params[:attr]] : params[:attr],
179
181
  input: input,
180
- output: (result.is_a?(Hash) &&
181
- result.include?('error')) ? nil : result,
182
+ output: (res.is_a?(Hash) &&
183
+ res.include?('error')) ? nil : res,
182
184
  start_time: params[:start_time],
183
185
  end_time: Time.zone.now,
184
- error: (result.is_a?(Hash) &&
185
- result.include?('error')) ? result : nil,
186
+ error: (res.is_a?(Hash) &&
187
+ res.include?('error')) ? res['error'] : nil,
186
188
  remote_ip: request.remote_ip,
187
189
  auth_name: params[:auth]
188
190
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marty
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arman Bostani
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2018-12-07 00:00:00.000000000 Z
17
+ date: 2018-12-10 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: pg