foreman_google 3.1.1 → 3.2.0

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: 367ea676f11cf5a5d354258ec346a46fe763033c2a51a5bbe98da9fa2a79c5ee
4
- data.tar.gz: 2b0bb4dfe19b4b6351c37049757c0982f4398b8b47c930b76709f074533175dc
3
+ metadata.gz: af8138079174df0021538ef3957ab3b4e91fbfddc5647306358e036a54623bc6
4
+ data.tar.gz: 4a44a5c32b98e15025078bdcfbd5a003b045d8d570767c6e7024e1fd1f0746cd
5
5
  SHA512:
6
- metadata.gz: 22d1b5fd31a5036b547e5a2aa07909cb4b76ca65c9189c1661b3fa3b89adee9bb44977869c56b3135242193def49a11df33ade1d1c208e0b190988d63994feca
7
- data.tar.gz: 4196d8ec947fea1c2105b9e2848299079e2d01d6dae9dce3a074192fe18df9ce30e1a5ef52ec0634d9a9bd737e78aa04f70fb39fa948d2c5f5102f3190d5323e
6
+ metadata.gz: '07847edac1b7ea54faafa702f5718178ba4c23a43fc8d861479d9c57a069ce23278515990f4ba844a9ea98e81fba382d1bb3bd6e20a09d555c5b0ac51fc9b767'
7
+ data.tar.gz: 68794184e443c7037deceb999fcb65a03c32ff18711b5807d61c38c2441873e6b58a9d8145748f10251faa1c83927ad0e88ad712bf4e8b98c05a9dad2432b142
@@ -7,7 +7,7 @@ module Foreman
7
7
  class_methods do
8
8
  def compute_resource_params_filter
9
9
  super.tap do |filter|
10
- filter.permit :key_path, :zone
10
+ filter.permit :key_content, :zone
11
11
  end
12
12
  end
13
13
  end
@@ -6,7 +6,7 @@ module ForemanGoogle
6
6
 
7
7
  update_api(:create, :update) do
8
8
  param :compute_resource, Hash do
9
- param :key_path, String, desc: N_('Certificate path, for GCE only')
9
+ param :key_content, String, desc: N_('Certificate JSON content, for GCE only')
10
10
  param :zone, String, desc: N_('Zone, for GCE only')
11
11
  param :project, String, desc: N_('Deprecated, project is automatically loaded from the JSON file. For GCE only')
12
12
  param :email, String, desc: N_('Deprecated, email is automatically loaded from the JSON file. For GCE only')
@@ -6,16 +6,30 @@ module ForemanGoogle
6
6
 
7
7
  # rubocop:disable Rails/LexicallyScopedActionFilter
8
8
  included do
9
- before_action :read_key, only: [:create]
10
- before_action :deprecated_params, only: [:create]
9
+ filter_parameters lambda { |key, value|
10
+ value.replace('[FILTERED]') if key == 'key_content'
11
+ }
12
+ before_action :read_key, only: %i[create update]
13
+ before_action :deprecated_params, only: %i[create update]
11
14
  end
12
15
  # rubocop:enable Rails/LexicallyScopedActionFilter
13
16
 
14
17
  private
15
18
 
16
19
  def read_key
17
- return unless compute_resource_params['provider'] == 'GCE'
18
- params[:compute_resource][:password] = File.read(params['compute_resource'].delete('key_path'))
20
+ return unless gce_provider?
21
+
22
+ key_content = extract_key_content
23
+ params[:compute_resource][:password] = key_content if key_content.present?
24
+ end
25
+
26
+ def gce_provider?
27
+ provider = action_name == 'create' ? params[:compute_resource][:provider] : @compute_resource&.provider
28
+ provider == 'GCE'
29
+ end
30
+
31
+ def extract_key_content
32
+ params[:compute_resource].delete(:key_content)
19
33
  end
20
34
 
21
35
  def deprecated_params
@@ -16,6 +16,10 @@ module ForemanGoogle
16
16
  end
17
17
  end
18
18
 
19
+ initializer 'foreman_google.filter_parameters' do |app|
20
+ app.config.filter_parameters << 'key_content'
21
+ end
22
+
19
23
  initializer 'foreman_google.register_plugin', before: :finisher_hook do |_app|
20
24
  Foreman::Plugin.register :foreman_google do
21
25
  requires_foreman '>= 5.1'
@@ -31,10 +35,11 @@ module ForemanGoogle
31
35
  # Include concerns in this config.to_prepare block
32
36
  config.to_prepare do
33
37
  ::Host::Managed.include ForemanGoogle::HostManagedExtensions
38
+ ::Api::V2::ComputeResourcesController.include Foreman::Controller::FilterParameters
34
39
  ::Api::V2::ComputeResourcesController.include ForemanGoogle::Api::V2::ComputeResourcesExtensions
35
40
  ::Api::V2::ComputeResourcesController.include ForemanGoogle::Api::V2::ApipieExtensions
36
- ::Api::V2::ComputeResourcesController.include Foreman::Controller::Parameters::ComputeResourceExtension
37
- ::ComputeResourcesController.include Foreman::Controller::Parameters::ComputeResourceExtension
41
+ ::Api::V2::ComputeResourcesController.prepend Foreman::Controller::Parameters::ComputeResourceExtension
42
+ ::ComputeResourcesController.prepend Foreman::Controller::Parameters::ComputeResourceExtension
38
43
  Google::Cloud::Compute::V1::AttachedDisk.include GoogleExtensions::AttachedDisk
39
44
  rescue StandardError => e
40
45
  Rails.logger.warn "ForemanGoogle: skipping engine hook (#{e})"
@@ -1,3 +1,3 @@
1
1
  module ForemanGoogle
2
- VERSION = '3.1.1'.freeze
2
+ VERSION = '3.2.0'.freeze
3
3
  end
@@ -13,14 +13,14 @@ module ForemanGoogle
13
13
  it 'permits Google compute resource parameters' do
14
14
  context = ::Api::V2::ComputeResourcesController.parameter_filter_context
15
15
  params = ActionController::Parameters.new(
16
- compute_resource: { key_path: '/tmp/key.json', zone: 'us-east1-b' }
16
+ compute_resource: { key_content: '{"type":"service_account"}', zone: 'us-east1-b' }
17
17
  )
18
18
 
19
19
  filtered = ::Api::V2::ComputeResourcesController
20
20
  .compute_resource_params_filter
21
21
  .filter_params(params, context)
22
22
 
23
- assert_equal({ 'key_path' => '/tmp/key.json', 'zone' => 'us-east1-b' }, filtered.to_h)
23
+ assert_equal({ 'key_content' => '{"type":"service_account"}', 'zone' => 'us-east1-b' }, filtered.to_h)
24
24
  end
25
25
  end
26
26
 
@@ -29,9 +29,13 @@ module ForemanGoogle
29
29
  def self.before_action(*)
30
30
  end
31
31
 
32
+ def self.filter_parameters(*)
33
+ # Stub for parameter filtering in tests
34
+ end
35
+
32
36
  include ForemanGoogle::Api::V2::ComputeResourcesExtensions
33
37
 
34
- attr_accessor :params
38
+ attr_accessor :params, :action_name, :compute_resource
35
39
 
36
40
  def compute_resource_params
37
41
  params[:compute_resource]
@@ -40,29 +44,40 @@ module ForemanGoogle
40
44
  end
41
45
 
42
46
  describe '#read_key' do
43
- it 'reads key file content into password for GCE provider' do
47
+ it 'copies key_content into password for GCE provider on create' do
44
48
  key_content = '{"type": "service_account", "project_id": "test"}'
49
+ ctrl = controller_class.new
50
+ ctrl.action_name = 'create'
51
+ ctrl.params = { 'compute_resource' => { 'provider' => 'GCE', 'key_content' => key_content } }.with_indifferent_access
52
+ ctrl.send(:read_key)
45
53
 
46
- Tempfile.open('gce_key') do |f|
47
- f.write(key_content)
48
- f.flush
54
+ assert_equal key_content, ctrl.params[:compute_resource][:password]
55
+ assert_not ctrl.params[:compute_resource].key?('key_content')
56
+ end
49
57
 
50
- ctrl = controller_class.new
51
- ctrl.params = { 'compute_resource' => { 'provider' => 'GCE', 'key_path' => f.path } }.with_indifferent_access
52
- ctrl.send(:read_key)
58
+ it 'copies key_content into password for GCE provider on update' do
59
+ key_content = '{"type": "service_account", "project_id": "test"}'
60
+ ctrl = controller_class.new
61
+ ctrl.action_name = 'update'
62
+ compute_resource = mock('compute_resource')
63
+ compute_resource.stubs(:provider).returns('GCE')
64
+ ctrl.instance_variable_set(:@compute_resource, compute_resource)
65
+ ctrl.params = { 'compute_resource' => { 'key_content' => key_content } }.with_indifferent_access
66
+ ctrl.send(:read_key)
53
67
 
54
- assert_equal key_content, ctrl.params[:compute_resource][:password]
55
- assert_not ctrl.params[:compute_resource].key?('key_path')
56
- end
68
+ assert_equal key_content, ctrl.params[:compute_resource][:password]
69
+ assert_not ctrl.params[:compute_resource].key?('key_content')
57
70
  end
58
71
 
59
72
  it 'skips when provider is not GCE' do
73
+ key_content = '{"type": "service_account", "project_id": "test"}'
60
74
  ctrl = controller_class.new
61
- ctrl.params = { 'compute_resource' => { 'provider' => 'EC2', 'key_path' => '/nonexistent' } }.with_indifferent_access
75
+ ctrl.action_name = 'create'
76
+ ctrl.params = { 'compute_resource' => { 'provider' => 'EC2', 'key_content' => key_content } }.with_indifferent_access
62
77
  ctrl.send(:read_key)
63
78
 
64
79
  assert_nil ctrl.params[:compute_resource][:password]
65
- assert_equal '/nonexistent', ctrl.params[:compute_resource]['key_path']
80
+ assert_equal key_content, ctrl.params[:compute_resource]['key_content']
66
81
  end
67
82
  end
68
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Foreman Team