insights-api-common 4.0.1 → 4.1.2

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: da2ebb5e76ab8009c3a4afda352c91ebcaee7eaa293b1fcc7c55bcd7711f2e6b
4
- data.tar.gz: 2a772f8bc79d01d48f2ca637ba9adcf0a1fa67c1aefda8bb414d45f5111b4558
3
+ metadata.gz: 3b5d5b1fb4fcee54a7ebe9cb45e950c8360c6b53652a90d6aa2d7e0884fdf079
4
+ data.tar.gz: 83b4b1b28772acd3ccd73ea21380314e4aabfa8417e6bc34509d385c6941a337
5
5
  SHA512:
6
- metadata.gz: d174ee5da9f78f4c7312ef570c0e3563d059c7332a1e43ad58ac17d7ec9051a4a8ebda04f7f1afa9b4f0179c0c4751204f25dae81d21eded96186336c6ed61a3
7
- data.tar.gz: e46f808117d0c742c89cc0b477308b8ddfff3e68f72ee36dbd57cc886df1fec97f8070836609c80164457fe98f2982f0a1f25919eee1ffe3aae08400a40696f4
6
+ metadata.gz: 43f6f59fd6787c5ac69c746fc89b4f0ab2e8122d13a3e4c0151c2b9fdbd8d4dd5ccc4a9a8068b5da7c466e870299bee88e58994e5169b74571b00b466d2b61f5
7
+ data.tar.gz: ec3589a7a4327583e3bdbb604862a72f3e421305f566336adb64d1ac5ba9d353da5333865fabb3cf145f3ffef44f29ee67ce5a4519ec252db3b25a138096ffc1
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Insights::API::Common
2
2
 
3
3
  [![Build Status](https://travis-ci.org/RedHatInsights/insights-api-common-rails.svg)](https://travis-ci.org/RedHatInsights/insights-api-common-rails)
4
- [![Maintainability](https://api.codeclimate.com/v1/badges/790ea6c77d82da6be68a/maintainability)](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/maintainability)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/790ea6c77d82da6be68a/test_coverage)](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/test_coverage)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/8ec8a19165383fdaed47/maintainability)](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/8ec8a19165383fdaed47/test_coverage)](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/test_coverage)
6
6
  [![Security](https://hakiri.io/github/RedHatInsights/insights-api-common-rails/master.svg)](https://hakiri.io/github/RedHatInsights/insights-api-common-rails/master)
7
7
 
8
8
  Header, Encryption, RBAC, Serialization, Pagination and other common behavior for Insights microservices built with Rails
@@ -1,3 +1,4 @@
1
+ require "insights/api/common/custom_exceptions"
1
2
  require "insights/api/common/engine"
2
3
  require "insights/api/common/entitlement"
3
4
  require "insights/api/common/error_document"
@@ -7,19 +7,28 @@ module Insights
7
7
 
8
8
  def self.included(other)
9
9
  other.rescue_from(StandardError, RuntimeError) do |exception|
10
- logger.error("#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}")
11
- errors = Insights::API::Common::ErrorDocument.new.tap do |error_document|
12
- exception_list_from(exception).each do |exc|
13
- if api_client_exception?(exc)
14
- api_client_errors(exc, error_document)
15
- else
16
- error_document.add(error_code_from_class(exc).to_s, "#{exc.class}: #{exc.message}")
17
- end
18
- end
10
+ rescue_from_handler(exception) do |error_document, exc|
11
+ error_document.add(error_code_from_class(exc).to_s, "#{exc.class}: #{exc.message}")
19
12
  end
13
+ end
14
+ end
20
15
 
21
- render :json => errors.to_h, :status => error_code_from_class(exception)
16
+ def rescue_from_handler(exception)
17
+ logger.error("#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}")
18
+ errors = Insights::API::Common::ErrorDocument.new.tap do |error_document|
19
+ exception_list_from(exception).each do |exc|
20
+ if api_client_exception?(exc)
21
+ api_client_errors(exc, error_document)
22
+ elsif custom_exception?(exc)
23
+ message = fetch_custom_message(exc)
24
+ error_document.add(error_code_from_class(exc).to_s, message)
25
+ else
26
+ yield error_document, exc
27
+ end
28
+ end
22
29
  end
30
+
31
+ render :json => errors.to_h, :status => error_code_from_class(exception)
23
32
  end
24
33
 
25
34
  def exception_list_from(exception)
@@ -31,6 +40,14 @@ module Insights
31
40
  end
32
41
  end
33
42
 
43
+ def custom_exception?(exception)
44
+ Insights::API::Common::CustomExceptions::CUSTOM_EXCEPTION_LIST.include?(exception.class.to_s)
45
+ end
46
+
47
+ def fetch_custom_message(exception)
48
+ Insights::API::Common::CustomExceptions.custom_message(exception)
49
+ end
50
+
34
51
  def error_code_from_class(exception)
35
52
  if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s)
36
53
  Rack::Utils.status_code(ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s])
@@ -45,7 +62,7 @@ module Insights
45
62
  end
46
63
 
47
64
  def api_client_errors(exc, error_document)
48
- body = JSON.parse(exc.response_body)
65
+ body = json_parsed_body(exc)
49
66
  if body.is_a?(Hash) && body.key?('errors') && body['errors'].is_a?(Array)
50
67
  body['errors'].each do |error|
51
68
  next unless error.key?('status') && error.key?('detail')
@@ -56,6 +73,12 @@ module Insights
56
73
  error_document.add(exc.code.to_s, exc.message )
57
74
  end
58
75
  end
76
+
77
+ def json_parsed_body(exc)
78
+ JSON.parse(exc.response_body)
79
+ rescue StandardError
80
+ nil
81
+ end
59
82
  end
60
83
  end
61
84
  end
@@ -0,0 +1,16 @@
1
+ module Insights
2
+ module API
3
+ module Common
4
+ class CustomExceptions
5
+ CUSTOM_EXCEPTION_LIST = %w[Pundit::NotAuthorizedError].freeze
6
+
7
+ def self.custom_message(exception)
8
+ case exception.class.to_s
9
+ when "Pundit::NotAuthorizedError"
10
+ "You are not authorized to #{exception.query.to_s.delete_suffix('?')} this #{exception.record.model_name.human.downcase}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -2,10 +2,14 @@ module Insights
2
2
  module API
3
3
  module Common
4
4
  class Metrics
5
- def self.activate(config, prefix)
5
+ def self.activate(config, prefix, args = {})
6
6
  require 'prometheus_exporter'
7
7
  require 'prometheus_exporter/client'
8
8
 
9
+ setup_custom_metrics(args[:custom_metrics])
10
+
11
+ return if metrics_port == 0
12
+
9
13
  ensure_exporter_server
10
14
  enable_in_process_metrics
11
15
  enable_web_server_metrics(prefix)
@@ -13,10 +17,10 @@ module Insights
13
17
 
14
18
  private_class_method def self.ensure_exporter_server
15
19
  require 'socket'
16
- TCPSocket.open("localhost", 9394) {}
20
+ TCPSocket.open("localhost", metrics_port) {}
17
21
  rescue Errno::ECONNREFUSED
18
22
  require 'prometheus_exporter/server'
19
- server = PrometheusExporter::Server::WebServer.new(port: 9394)
23
+ server = PrometheusExporter::Server::WebServer.new(port: metrics_port)
20
24
  server.start
21
25
 
22
26
  PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(collector: server.collector)
@@ -33,6 +37,31 @@ module Insights
33
37
  require "insights/api/common/middleware/web_server_metrics"
34
38
  Rails.application.middleware.unshift(Insights::API::Common::Middleware::WebServerMetrics, :metrics_prefix => prefix)
35
39
  end
40
+
41
+ private_class_method def self.setup_custom_metrics(custom_metrics)
42
+ return if custom_metrics.nil?
43
+
44
+ custom_metrics.each do |metric|
45
+ if metrics_port == 0
46
+ define_singleton_method(metric[:name]) {}
47
+ else
48
+ instance_variable_set("@#{metric[:name]}_#{metric[:type]}", PrometheusExporter::Client.default.register(metric[:type], metric[:name], metric[:description]))
49
+
50
+ define_singleton_method(metric[:name]) do
51
+ case metric[:type]
52
+ when :counter
53
+ instance_variable_get("@#{metric[:name]}_#{metric[:type]}")&.observe(1)
54
+ else
55
+ "Metric of type #{metric[:type]} unsupported, implement it in Insights::API::Common::Metrics#L45"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ private_class_method def self.metrics_port
63
+ @metrics_port ||= (ENV['METRICS_PORT']&.to_i || 9394)
64
+ end
36
65
  end
37
66
  end
38
67
  end
@@ -1,2 +1,3 @@
1
1
  require "insights/api/common/open_api/docs"
2
+ require "insights/api/common/open_api/version_from_prefix"
2
3
  require "insights/api/common/open_api/serializer"
@@ -48,6 +48,7 @@ module Insights
48
48
  ENV['APP_NAME'] = app_name
49
49
  ENV['PATH_PREFIX'] = app_prefix
50
50
  Rails.application.reload_routes!
51
+ @operation_id_hash = {}
51
52
  end
52
53
 
53
54
  def server_base_path
@@ -117,8 +118,8 @@ module Insights
117
118
  "type" => "object",
118
119
  "properties" => {
119
120
  "status" => {
120
- "type" => "integer",
121
- "example" => 404
121
+ "type" => "string",
122
+ "example" => "404"
122
123
  },
123
124
  "detail" => {
124
125
  "type" => "string",
@@ -200,7 +201,7 @@ module Insights
200
201
  sub_collection = (primary_collection != klass_name)
201
202
  {
202
203
  "summary" => "List #{klass_name.pluralize}#{" for #{primary_collection}" if sub_collection}",
203
- "operationId" => "list#{primary_collection if sub_collection}#{klass_name.pluralize}",
204
+ "operationId" => operation_id(klass_name, primary_collection, sub_collection),
204
205
  "description" => "Returns an array of #{klass_name} objects",
205
206
  "parameters" => [
206
207
  { "$ref" => "##{PARAMETERS_PATH}/QueryLimit" },
@@ -440,7 +441,7 @@ module Insights
440
441
  end
441
442
 
442
443
  # Take existing attrs, that we won't generate
443
- ['example', 'format', 'readOnly', 'title', 'description'].each do |property_key|
444
+ ['example', 'format', 'nullable', 'readOnly', 'title', 'description'].each do |property_key|
444
445
  property_value = openapi_contents.dig(*path_parts(SCHEMAS_PATH), klass_name, "properties", key, property_key)
445
446
  properties_value[property_key] = property_value if property_value
446
447
  end
@@ -562,6 +563,24 @@ module Insights
562
563
  def schema_overrides
563
564
  {}
564
565
  end
566
+
567
+ def validate_operation_id(name, klass_name)
568
+ if @operation_id_hash.key?(name)
569
+ raise ArgumentError, "operation id cannot be duplicates, #{name} in class #{klass_name} has already been used in class #{@operation_id_hash[name]}"
570
+ end
571
+ @operation_id_hash[name] = klass_name
572
+ end
573
+
574
+ def operation_id(klass_name, primary_collection, sub_collection)
575
+ klass = klass_name.constantize
576
+ name = if klass.respond_to?(:list_operation_id)
577
+ klass.send(:list_operation_id)
578
+ else
579
+ "list#{primary_collection if sub_collection}#{klass_name.pluralize}"
580
+ end
581
+ validate_operation_id(name, klass_name)
582
+ name
583
+ end
565
584
  end
566
585
  end
567
586
  end
@@ -3,27 +3,40 @@ module Insights
3
3
  module Common
4
4
  module OpenApi
5
5
  module Serializer
6
+ include VersionFromPrefix
7
+
6
8
  def as_json(arg = {})
7
- previous = super
9
+ previous = super(:except => _excluded_attributes(arg))
10
+
8
11
  encrypted_columns_set = (self.class.try(:encrypted_columns) || []).to_set
9
12
  encryption_filtered = previous.except(*encrypted_columns_set)
10
13
  return encryption_filtered unless arg.key?(:prefixes)
11
- version = api_version_from_prefix(arg[:prefixes].first)
12
- presentation_name = self.class.try(:presentation_name) || self.class.name
13
- schema = ::Insights::API::Common::OpenApi::Docs.instance[version].definitions[presentation_name]
14
- attrs = encryption_filtered.slice(*schema["properties"].keys)
15
- schema["properties"].keys.each do |name|
14
+
15
+ attrs = encryption_filtered.slice(*_schema_properties(arg).keys)
16
+ _schema_properties(arg).keys.each do |name|
16
17
  next if attrs[name].nil?
17
18
  attrs[name] = attrs[name].iso8601 if attrs[name].kind_of?(Time)
18
19
  attrs[name] = attrs[name].to_s if name.ends_with?("_id") || name == "id"
19
- attrs[name] = self.public_send(name) if !attrs.key?(name) && !encrypted_columns_set.include?(name)
20
20
  end
21
21
  attrs.compact
22
22
  end
23
23
 
24
- def api_version_from_prefix(prefix)
25
- /\/?\w+\/v(?<major>\d+)[x\.]?(?<minor>\d+)?\// =~ prefix
26
- [major, minor].compact.join(".")
24
+ private
25
+
26
+ def _excluded_attributes(arg)
27
+ return [] unless arg.key?(:prefixes)
28
+
29
+ self.attributes.keys - _schema_properties(arg).keys
30
+ end
31
+
32
+ def _schema_properties(arg)
33
+ @schema_properties ||= _schema(arg)["properties"]
34
+ end
35
+
36
+ def _schema(arg)
37
+ version = api_version_from_prefix(arg[:prefixes].first)
38
+ presentation_name = self.class.try(:presentation_name) || self.class.name
39
+ ::Insights::API::Common::OpenApi::Docs.instance[version].definitions[presentation_name]
27
40
  end
28
41
  end
29
42
  end
@@ -0,0 +1,14 @@
1
+ module Insights
2
+ module API
3
+ module Common
4
+ module OpenApi
5
+ module VersionFromPrefix
6
+ def api_version_from_prefix(prefix)
7
+ /\/?\w+\/v(?<major>\d+)[x\.]?(?<minor>\d+)?\// =~ prefix
8
+ [major, minor].compact.join(".")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,7 +2,7 @@ module Insights
2
2
  module API
3
3
  module Common
4
4
  module RBAC
5
- require 'rbac-api-client'
5
+ require 'insights-rbac-api-client'
6
6
 
7
7
  class Seed
8
8
  def initialize(seed_file, user_file = nil)
@@ -2,7 +2,7 @@ module Insights
2
2
  module API
3
3
  module Common
4
4
  module RBAC
5
- require 'rbac-api-client'
5
+ require 'insights-rbac-api-client'
6
6
 
7
7
  class NetworkError < StandardError; end
8
8
  class TimedOutError < StandardError; end
@@ -25,6 +25,14 @@ module Insights
25
25
  Thread.current[:current_request]
26
26
  end
27
27
 
28
+ def self.current_request_id
29
+ Thread.current[:request_id]
30
+ end
31
+
32
+ def self.current_request_id=(id)
33
+ Thread.current[:request_id] = id
34
+ end
35
+
28
36
  def self.current!
29
37
  current || raise(RequestNotSet)
30
38
  end
@@ -45,10 +53,13 @@ module Insights
45
53
 
46
54
  def self.with_request(request)
47
55
  saved = current
56
+ saved_request_id = current&.request_id
48
57
  self.current = request
58
+ self.current_request_id = current&.request_id
49
59
  yield current
50
60
  ensure
51
61
  self.current = saved
62
+ self.current_request_id = saved_request_id
52
63
  end
53
64
 
54
65
  def self.current_forwardable
@@ -1,7 +1,7 @@
1
1
  module Insights
2
2
  module API
3
3
  module Common
4
- VERSION = "4.0.1".freeze
4
+ VERSION = "4.1.2".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insights-api-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Insights Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-27 00:00:00.000000000 Z
11
+ date: 2020-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: acts_as_tenant
@@ -58,20 +58,20 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '5.2'
61
+ version: 5.2.2
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: 5.2.1.1
64
+ version: 5.2.2.1
65
65
  type: :runtime
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
69
  - - "~>"
70
70
  - !ruby/object:Gem::Version
71
- version: '5.2'
71
+ version: 5.2.2
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: 5.2.1.1
74
+ version: 5.2.2.1
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: manageiq-loggers
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -142,6 +142,20 @@ dependencies:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
144
  version: 0.10.0
145
+ - !ruby/object:Gem::Dependency
146
+ name: insights-rbac-api-client
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '1.0'
152
+ type: :runtime
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '1.0'
145
159
  - !ruby/object:Gem::Dependency
146
160
  name: graphql
147
161
  requirement: !ruby/object:Gem::Requirement
@@ -222,16 +236,16 @@ dependencies:
222
236
  name: rake
223
237
  requirement: !ruby/object:Gem::Requirement
224
238
  requirements:
225
- - - "~>"
239
+ - - ">="
226
240
  - !ruby/object:Gem::Version
227
- version: '10.0'
241
+ version: 12.3.3
228
242
  type: :development
229
243
  prerelease: false
230
244
  version_requirements: !ruby/object:Gem::Requirement
231
245
  requirements:
232
- - - "~>"
246
+ - - ">="
233
247
  - !ruby/object:Gem::Version
234
- version: '10.0'
248
+ version: 12.3.3
235
249
  - !ruby/object:Gem::Dependency
236
250
  name: rspec
237
251
  requirement: !ruby/object:Gem::Requirement
@@ -278,16 +292,16 @@ dependencies:
278
292
  name: simplecov
279
293
  requirement: !ruby/object:Gem::Requirement
280
294
  requirements:
281
- - - ">="
295
+ - - "~>"
282
296
  - !ruby/object:Gem::Version
283
- version: '0'
297
+ version: 0.17.1
284
298
  type: :development
285
299
  prerelease: false
286
300
  version_requirements: !ruby/object:Gem::Requirement
287
301
  requirements:
288
- - - ">="
302
+ - - "~>"
289
303
  - !ruby/object:Gem::Version
290
- version: '0'
304
+ version: 0.17.1
291
305
  - !ruby/object:Gem::Dependency
292
306
  name: webmock
293
307
  requirement: !ruby/object:Gem::Requirement
@@ -331,6 +345,7 @@ files:
331
345
  - lib/insights/api/common/application_controller_mixins/request_body_validation.rb
332
346
  - lib/insights/api/common/application_controller_mixins/request_parameter_validation.rb
333
347
  - lib/insights/api/common/application_controller_mixins/request_path.rb
348
+ - lib/insights/api/common/custom_exceptions.rb
334
349
  - lib/insights/api/common/engine.rb
335
350
  - lib/insights/api/common/entitlement.rb
336
351
  - lib/insights/api/common/error_document.rb
@@ -357,6 +372,7 @@ files:
357
372
  - lib/insights/api/common/open_api/docs/object_definition.rb
358
373
  - lib/insights/api/common/open_api/generator.rb
359
374
  - lib/insights/api/common/open_api/serializer.rb
375
+ - lib/insights/api/common/open_api/version_from_prefix.rb
360
376
  - lib/insights/api/common/option_redirect_enhancements.rb
361
377
  - lib/insights/api/common/paginated_response.rb
362
378
  - lib/insights/api/common/paginated_response_v2.rb