foreman_webhooks 0.0.1 → 0.0.2

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: e4c7f3b67cd40e7de9b5226bbee4a9bccc1b1132c1cdeb08bd2521135036c8ce
4
- data.tar.gz: 9c47ea60965737999ef8ed63a036b1d58c0fe205d1f5d8df3a5a497c6816e5c0
3
+ metadata.gz: a9463b5bd544237c5746c80546263831614867cd514146ec019332759618e2b8
4
+ data.tar.gz: ee3c69e62926cd35b9ed665b5dc09199052fb7d72556040851d571cdbd0a01de
5
5
  SHA512:
6
- metadata.gz: bb768c45df8d5470eb8f5bb3add9d7b7bc81ed76d4148c2216a85d61a99b62b988d3c7fa9d2152c344f61716fed0ea9083e0928f965e5ef47486dc2909897a76
7
- data.tar.gz: a313ab830d249d8585c9ff90001417fc95cf6925fc58e0b01e229631d378eae936c16a0f86043e483e8b209a28ff1887ea8101c3cd390eeba0aca13ef21e1e27
6
+ metadata.gz: 8b3580cd4a8987e5ad9e3410bf87be6b9c84c299f0e38bcc68db14dfe97c7458f4976621f19d614d39b56380be8a7ba8cefbf7a42d358bbe91a417b05603d153
7
+ data.tar.gz: ef59e908b1f0c9cad872b73bd86922064f01552a8f8287b720d040f0a9b4f44fbf066d752492637aff633752db5453b35e07cf376f7e86838d756cb4d8fd1dcb
@@ -28,7 +28,8 @@ module Api
28
28
  param :snippet, :bool, allow_nil: true
29
29
  param :audit_comment, String, allow_nil: true
30
30
  param :locked, :bool, desc: N_('Whether or not the template is locked for editing')
31
- param :default, :bool, desc: N_('Whether or not the template is added automatically to new organizations and locations')
31
+ param :default, :bool,
32
+ desc: N_('Whether or not the template is added automatically to new organizations and locations')
32
33
  param_group :taxonomies, ::Api::V2::BaseController
33
34
  end
34
35
  end
@@ -90,11 +91,6 @@ module Api
90
91
 
91
92
  private
92
93
 
93
- # Overload this method to avoid using search_for method
94
- def resource_scope_for_index(options = {})
95
- resource_scope(options).paginate(paginate_options)
96
- end
97
-
98
94
  def action_permission
99
95
  case params[:action]
100
96
  when 'clone', 'import'
@@ -5,12 +5,12 @@ module Api
5
5
  class WebhooksController < V2::BaseController
6
6
  include Api::Version2
7
7
  include ForemanWebhooks::Controller::Parameters::Webhook
8
- include Foreman::Controller::TemplateImport
9
8
 
10
9
  before_action :find_resource, only: %i[show update destroy]
11
10
 
12
11
  api :GET, '/webhooks/', N_('List Webhooks')
13
12
  param_group :search_and_pagination, ::Api::V2::BaseController
13
+ add_scoped_search_description_for(Webhook)
14
14
  def index
15
15
  @webhooks = resource_scope_for_index
16
16
  end
@@ -25,7 +25,8 @@ module Api
25
25
  param :target_url, String, required: true
26
26
  param :http_method, Webhook::ALLOWED_HTTP_METHODS
27
27
  param :http_content_type, String
28
- param :event, String, required: true
28
+ events = Webhook.available_events.sort.map { |e| e.delete_suffix(Webhook::EVENT_POSTFIX) }
29
+ param :event, events, required: true
29
30
  param :webhook_template_id, :identifier
30
31
  param :enabled, :boolean
31
32
  param :verify_ssl, :boolean
@@ -24,7 +24,9 @@ module ForemanWebhooks
24
24
  required :hash, Hash, 'Key=value object with with data that should be present in payload'
25
25
  keyword :with_defaults, [true, false], 'If set to true, adds default entries to the payload', default: true
26
26
  returns String, 'JSON string with the final payload'
27
- example 'payload({ id: @object.id, name: @object.name }) #=> "{ "id": 1, "name": "host.example.com", "context": { ... }, "event_name": "host_created.event.foreman" }"'
27
+ example 'payload({ id: @object.id, name: @object.name }) #=> ' \
28
+ '"{ "id": 1, "name": "host.example.com", "context": { ... }, ' \
29
+ '"event_name": "host_created.event.foreman" }"'
28
30
  end
29
31
  def payload(hash, with_defaults: true)
30
32
  hash.merge!(@defaults) if with_defaults
@@ -11,15 +11,6 @@ class Webhook < ApplicationRecord
11
11
 
12
12
  EVENT_POSTFIX = ".#{Foreman::Observable::DEFAULT_NAMESPACE}"
13
13
 
14
- EVENT_ALLOWLIST = %w[
15
- host_created host_updated host_destroyed
16
- hostgroup_created hostgroup_updated hostgroup_destroyed
17
- user_created user_updated user_destroyed
18
- domain_created domain_updated domain_destroyed
19
- subnet_created subnet_updated subnet_destroyed
20
- build_entered build_exited status_changed
21
- ].map { |e| e + EVENT_POSTFIX }.freeze
22
-
23
14
  DEFAULT_PAYLOAD_TEMPLATE = 'Webhook Template - Payload Default'
24
15
 
25
16
  ALLOWED_HTTP_METHODS = %w[POST GET PUT DELETE PATCH].freeze
@@ -47,7 +38,7 @@ class Webhook < ApplicationRecord
47
38
  scoped_search on: :enabled, complete_value: { true: true, false: false }
48
39
 
49
40
  def self.available_events
50
- ::Foreman::EventSubscribers.all_observable_events & EVENT_ALLOWLIST
41
+ ::Foreman::EventSubscribers.all_observable_events
51
42
  end
52
43
 
53
44
  def self.deliver(event_name:, payload:)
@@ -25,7 +25,7 @@ module ForemanWebhooks
25
25
  logger.debug("Headers: #{rendered_headers}")
26
26
  end
27
27
 
28
- response = self.class.request(url: webhook.target_url,
28
+ response = self.class.request(url: rendered_url,
29
29
  payload: payload,
30
30
  http_method: webhook.http_method,
31
31
  user: webhook.user,
@@ -50,7 +50,8 @@ module ForemanWebhooks
50
50
  message: response.message,
51
51
  http_status: response.code.to_i
52
52
  }
53
- rescue SocketError, OpenSSL::SSL::SSLError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout => e
53
+ rescue SocketError, OpenSSL::SSL::SSLError, Errno::ECONNREFUSED, Errno::ECONNRESET,
54
+ Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout => e
54
55
  Foreman::Logging.exception("Failed to execute the webhook #{webhook.name} -> #{event_name}", e)
55
56
  {
56
57
  status: :error,
@@ -58,7 +59,9 @@ module ForemanWebhooks
58
59
  }
59
60
  end
60
61
 
61
- def self.request(url:, payload: '', http_method: :GET, user: nil, password: nil, content_type: 'application/json', headers: {}, ca_string: nil, ca_verify: false, follow_redirects: true, redirect_limit: 3)
62
+ def self.request(url:, payload: '', http_method: :GET, user: nil, password: nil,
63
+ content_type: 'application/json', headers: {}, ca_string: nil,
64
+ ca_verify: false, follow_redirects: true, redirect_limit: 3)
62
65
  uri = URI.parse(url)
63
66
 
64
67
  request = Object.const_get("Net::HTTP::#{http_method.to_s.capitalize}").new(uri.request_uri)
@@ -86,11 +89,13 @@ module ForemanWebhooks
86
89
  end
87
90
  http.request(request) do |response|
88
91
  case response
89
- when Net::HTTPRedirection then
92
+ when Net::HTTPRedirection
90
93
  new_location = response['location']
91
94
  Rails.logger.debug "Redirected to #{new_location} (redirects left: #{redirect_limit})"
92
- raise(::Foreman::Exception, N_(format('Too many HTTP redirects when calling %{uri}', uri: uri, code: response.code))) if redirect_limit <= 0
93
-
95
+ if redirect_limit <= 0
96
+ raise(::Foreman::Exception,
97
+ N_(format('Too many HTTP redirects when calling %{uri}', uri: uri, code: response.code)))
98
+ end
94
99
  self.request(url: new_location,
95
100
  payload: payload,
96
101
  http_method: http_method,
@@ -10,7 +10,9 @@ attributes :target_url,
10
10
  :http_content_type,
11
11
  :enabled,
12
12
  :verify_ssl,
13
- :http_headers
13
+ :http_headers,
14
+ :ssl_ca_certs,
15
+ :user
14
16
 
15
17
  child :webhook_template do
16
18
  extends 'api/v2/webhook_templates/base'
@@ -0,0 +1,21 @@
1
+ <%#
2
+ name: Remote Execution Host Job
3
+ description: Example payload for actions.remote_execution.run_host_job_succeeded
4
+ snippet: false
5
+ model: WebhookTemplate
6
+ -%>
7
+ #
8
+ # Example webhook template, @object carries Action instance.
9
+ # For more information about available helpers visit /templates_doc
10
+ #
11
+ # Run job host name <%= @object.host_name %>
12
+ # Run job host id <%= @object.host_id %>
13
+ # Run job full host object <%= @object.host %>
14
+ # Run job job_invocation_id <%= @object.job_invocation_id %>
15
+ # Run job job_invocation <%= @object.job_invocation %>
16
+ # Task label <%= @object.task.label %>
17
+ # Task started at <%= @object.task.started_at %>
18
+ # Task ended at <%= @object.task.ended_at %>
19
+ # Task resulted with <%= @object.task.result %>
20
+ # Task state <%= @object.task.state %>
21
+ # Task action output <%= @object.task.action_output %>
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ proxy_feature = Feature.where(name: 'Shellhooks').first_or_create
4
+ return unless proxy_feature.nil? || proxy_feature.errors.any?
5
+
6
+ raise "Unable to create proxy feature: #{format_errors proxy_feature}"
@@ -35,8 +35,8 @@ module ForemanWebhooks
35
35
  'api/v2/webhooks': [:update] }, resource_type: 'Webhook'
36
36
  permission :destroy_webhooks, { webhooks: [:destroy],
37
37
  'api/v2/webhooks': [:destroy] }, resource_type: 'Webhook'
38
- permission :view_webhook_templates, { webhook_templates: %i[index show auto_complete_search preview export],
39
- 'api/v2/webhook_templates': %i[index show export] },
38
+ permission :view_webhook_templates, { webhook_templates: %i[index show auto_complete_search preview export],
39
+ 'api/v2/webhook_templates': %i[index show export] },
40
40
  resource_type: 'WebhookTemplate'
41
41
  permission :create_webhook_templates, { webhook_templates: %i[new create clone_template],
42
42
  'api/v2/webhook_templates': %i[create clone import] },
@@ -52,6 +52,15 @@ module ForemanWebhooks
52
52
  resource_type: 'WebhookTemplate'
53
53
  end
54
54
 
55
+ role 'Webhooks Reader',
56
+ %i[view_webhooks view_webhook_templates]
57
+
58
+ role 'Webhooks Manager',
59
+ %i[view_webhooks create_webhooks edit_webhooks destroy_webhooks
60
+ view_webhook_templates create_webhook_templates
61
+ edit_webhook_templates destroy_webhook_templates
62
+ lock_webhook_templates]
63
+
55
64
  # add menu entry
56
65
  divider :admin_menu, caption: N_('Webhook'), parent: :administer_menu
57
66
  menu :admin_menu, :webhooks, url: '/webhooks',
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanWebhooks
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -44,4 +44,6 @@ end
44
44
  Rake::Task[:test].enhance ['test:foreman_webhooks']
45
45
 
46
46
  load 'tasks/jenkins.rake'
47
- Rake::Task['jenkins:unit'].enhance ['test:foreman_webhooks', 'foreman_webhooks:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
47
+ if Rake::Task.task_defined?(:'jenkins:unit')
48
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_webhooks', 'foreman_webhooks:rubocop']
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_webhooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timo Goebel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-25 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: rubocop
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.71.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.71.0
41
41
  description: Plugin for Foreman that allows to configure Webhooks.
@@ -74,6 +74,7 @@ files:
74
74
  - app/views/api/v2/webhooks/update.json.rabl
75
75
  - app/views/foreman_webhooks/webhook_templates/ansible_tower_-_host_in_inventory.erb
76
76
  - app/views/foreman_webhooks/webhook_templates/empty_payload.erb
77
+ - app/views/foreman_webhooks/webhook_templates/remote_execution_-_host_job.erb
77
78
  - app/views/foreman_webhooks/webhook_templates/webhook_template_-_payload_default.erb
78
79
  - app/views/webhook_templates/_alerts.html.erb
79
80
  - app/views/webhook_templates/_custom_tab_headers.html.erb
@@ -93,6 +94,7 @@ files:
93
94
  - db/migrate/20200908004234_add_columns_to_webhooks.rb
94
95
  - db/migrate/20201014115147_rename_ca_file_column.rb
95
96
  - db/migrate/20201109135301_add_http_headers.rb
97
+ - db/seeds.d/62_shellhooks_proxy_feature.rb
96
98
  - db/seeds.d/95_webhook_templates.rb
97
99
  - lib/foreman_webhooks.rb
98
100
  - lib/foreman_webhooks/engine.rb
@@ -165,27 +167,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
167
  requirements:
166
168
  - - ">="
167
169
  - !ruby/object:Gem::Version
168
- version: '0'
170
+ version: 2.5.0
169
171
  required_rubygems_version: !ruby/object:Gem::Requirement
170
172
  requirements:
171
173
  - - ">="
172
174
  - !ruby/object:Gem::Version
173
175
  version: '0'
174
176
  requirements: []
175
- rubygems_version: 3.1.2
177
+ rubygems_version: 3.1.4
176
178
  signing_key:
177
179
  specification_version: 4
178
180
  summary: Configure webhooks for Foreman.
179
181
  test_files:
180
- - test/jobs/foreman_webhooks/deliver_webhook_job_test.rb
181
- - test/test_plugin_helper.rb
182
- - test/unit/foreman_webhooks/webhook_service_test.rb
182
+ - test/controllers/api/v2/webhook_templates_controller_test.rb
183
+ - test/controllers/api/v2/webhooks_controller_test.rb
183
184
  - test/factories/webhook.rb
184
185
  - test/factories/webhook_target.rb
185
186
  - test/factories/webhook_template.rb
186
- - test/controllers/api/v2/webhook_templates_controller_test.rb
187
- - test/controllers/api/v2/webhooks_controller_test.rb
187
+ - test/jobs/foreman_webhooks/deliver_webhook_job_test.rb
188
188
  - test/models/webhook_test.rb
189
+ - test/unit/foreman_webhooks/webhook_service_test.rb
190
+ - test/test_plugin_helper.rb
189
191
  - webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/Formatters/__tests__/enabledCellFormatter.test.js
190
192
  - webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/__tests__/EnabledCell.test.js
191
193
  - webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/__tests__/WebhooksTable.test.js