insights-api-common 4.1.0 → 4.1.5

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
  SHA256:
3
- metadata.gz: e43686bd25c46149f1fd5886e5c60452fd04a06c183042ea4f678f227a6b1ca6
4
- data.tar.gz: 882ea648b87f2ee1d7c6c8fa171e85f828c1c46ad818799f3e0d638b4e7b9384
3
+ metadata.gz: f23ebe490e2b124e093a772ba711f5f93942725df5da610609aacb0619f57194
4
+ data.tar.gz: bf033da653e5130461d092a06fc5a9b26c9e451e7e900e33a34efaf8338d07e6
5
5
  SHA512:
6
- metadata.gz: cc9dd41c400b94bf67648ac2ddf0f6b5d30ec44ed17cc5f3294155e518908911e261cd5b304518ed9f730840695c06a2b5ecbbc05a6149f38571d95c1d4d8748
7
- data.tar.gz: 92e36a165e0b440a279951f6d5a0e8ddd516eb439b9b31768be0ef880350eacf52b8ec2286552f112553eec01cf72e2ba3d2bb635d6791cdab3ea6e134c267d6
6
+ metadata.gz: 1abf3875849663909685e77d9d9a2b212bf52a02cf8772e6f30530c96e7b814e28ff167565dfd07908ccbb5bddb50264b6ba6f9b21dce60119ce4ae5291f84a2
7
+ data.tar.gz: dab5a4c9ee7a93f7f0eaec01befe41367f3799fc1f87bd0a83855f2f12148e7070de15d4d0736969f3201f76c44ff54da711cc959b0d670695c65c41b9ca6a1a
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,27 @@ 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
+ errors = Insights::API::Common::ErrorDocument.new.tap do |error_document|
18
+ exception_list_from(exception).each do |exc|
19
+ if api_client_exception?(exc)
20
+ api_client_errors(exc, error_document)
21
+ elsif custom_exception?(exc)
22
+ message = fetch_custom_message(exc)
23
+ error_document.add(error_code_from_class(exc).to_s, message)
24
+ else
25
+ yield error_document, exc
26
+ end
27
+ end
22
28
  end
29
+
30
+ render :json => errors.to_h, :status => error_code_from_class(exception)
23
31
  end
24
32
 
25
33
  def exception_list_from(exception)
@@ -31,6 +39,14 @@ module Insights
31
39
  end
32
40
  end
33
41
 
42
+ def custom_exception?(exception)
43
+ Insights::API::Common::CustomExceptions::CUSTOM_EXCEPTION_LIST.include?(exception.class.to_s)
44
+ end
45
+
46
+ def fetch_custom_message(exception)
47
+ Insights::API::Common::CustomExceptions.custom_message(exception)
48
+ end
49
+
34
50
  def error_code_from_class(exception)
35
51
  if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s)
36
52
  Rack::Utils.status_code(ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s])
@@ -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
@@ -6,18 +6,21 @@ module Insights
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)
12
- setup_custom_metrics(args[:custom_metrics])
13
16
  end
14
17
 
15
18
  private_class_method def self.ensure_exporter_server
16
19
  require 'socket'
17
- TCPSocket.open("localhost", 9394) {}
20
+ TCPSocket.open("localhost", metrics_port) {}
18
21
  rescue Errno::ECONNREFUSED
19
22
  require 'prometheus_exporter/server'
20
- server = PrometheusExporter::Server::WebServer.new(port: 9394)
23
+ server = PrometheusExporter::Server::WebServer.new(port: metrics_port)
21
24
  server.start
22
25
 
23
26
  PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(collector: server.collector)
@@ -39,18 +42,26 @@ module Insights
39
42
  return if custom_metrics.nil?
40
43
 
41
44
  custom_metrics.each do |metric|
42
- instance_variable_set("@#{metric[:name]}_#{metric[:type]}", PrometheusExporter::Client.default.register(metric[:type], metric[:name], metric[:description]))
43
-
44
- define_singleton_method(metric[:name]) do
45
- case metric[:type]
46
- when :counter
47
- instance_variable_get("@#{metric[:name]}_#{metric[:type]}")&.observe(1)
48
- else
49
- "Metric of type #{metric[:type]} unsupported, implement it in Insights::API::Common::Metrics#L45"
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
50
57
  end
51
58
  end
52
59
  end
53
60
  end
61
+
62
+ private_class_method def self.metrics_port
63
+ @metrics_port ||= (ENV['METRICS_PORT']&.to_i || 9394)
64
+ end
54
65
  end
55
66
  end
56
67
  end
@@ -441,7 +441,7 @@ module Insights
441
441
  end
442
442
 
443
443
  # Take existing attrs, that we won't generate
444
- ['example', 'format', 'readOnly', 'title', 'description'].each do |property_key|
444
+ ['example', 'format', 'nullable', 'readOnly', 'title', 'description'].each do |property_key|
445
445
  property_value = openapi_contents.dig(*path_parts(SCHEMAS_PATH), klass_name, "properties", key, property_key)
446
446
  properties_value[property_key] = property_value if property_value
447
447
  end
@@ -57,6 +57,9 @@ module Insights
57
57
  self.current = request
58
58
  self.current_request_id = current&.request_id
59
59
  yield current
60
+ rescue => exception
61
+ Rails.logger.error("#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}")
62
+ raise
60
63
  ensure
61
64
  self.current = saved
62
65
  self.current_request_id = saved_request_id
@@ -8,7 +8,7 @@ module Insights
8
8
  @route_mapper = route_mapper
9
9
  end
10
10
 
11
- def redirect_major_version(version, prefix, via: [:delete, :get, :options, :patch, :post])
11
+ def redirect_major_version(version, prefix, via: [:delete, :get, :options])
12
12
  route_mapper.match(
13
13
  "/#{version.split('.').first}/*path(.:format)",
14
14
  :format => false,
@@ -1,7 +1,7 @@
1
1
  module Insights
2
2
  module API
3
3
  module Common
4
- VERSION = "4.1.0".freeze
4
+ VERSION = "4.1.5".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.1.0
4
+ version: 4.1.5
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-06-02 00:00:00.000000000 Z
11
+ date: 2020-10-06 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
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '0.2'
95
+ version: 0.2.1
96
96
  type: :runtime
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: '0.2'
102
+ version: 0.2.1
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: prometheus_exporter
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -236,16 +236,16 @@ dependencies:
236
236
  name: rake
237
237
  requirement: !ruby/object:Gem::Requirement
238
238
  requirements:
239
- - - "~>"
239
+ - - ">="
240
240
  - !ruby/object:Gem::Version
241
- version: '10.0'
241
+ version: 12.3.3
242
242
  type: :development
243
243
  prerelease: false
244
244
  version_requirements: !ruby/object:Gem::Requirement
245
245
  requirements:
246
- - - "~>"
246
+ - - ">="
247
247
  - !ruby/object:Gem::Version
248
- version: '10.0'
248
+ version: 12.3.3
249
249
  - !ruby/object:Gem::Dependency
250
250
  name: rspec
251
251
  requirement: !ruby/object:Gem::Requirement
@@ -345,6 +345,7 @@ files:
345
345
  - lib/insights/api/common/application_controller_mixins/request_body_validation.rb
346
346
  - lib/insights/api/common/application_controller_mixins/request_parameter_validation.rb
347
347
  - lib/insights/api/common/application_controller_mixins/request_path.rb
348
+ - lib/insights/api/common/custom_exceptions.rb
348
349
  - lib/insights/api/common/engine.rb
349
350
  - lib/insights/api/common/entitlement.rb
350
351
  - lib/insights/api/common/error_document.rb