foreman_vault 0.2.0 → 0.3.0

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: a11fc63d2584b7fd9f4ad6b561cdbdd471503d3de307d4a77610094d67d26b62
4
- data.tar.gz: 215b5f6e87ed318a16b7623bedccb25d17dd001ee73281f144b6726a44aee28b
3
+ metadata.gz: dbbedb9d2f03e0e5d76b5c088d054852e1cbb8f44ff2d8454026b2fecf39215f
4
+ data.tar.gz: 0f8099efcfc129acc8d640e0240ef9418b7d4a657609d13a4e2094be7c0b8ec9
5
5
  SHA512:
6
- metadata.gz: b73b1a230f9982752e8ede2a38b2afd1a071a7ca397fe5f518d6447dbd30ca4f94efcbf29d4c39430cb8f8bf5b9508d9c4edd542aead2ebde60d00e82f889833
7
- data.tar.gz: 3b85d123010e4ff958ed12885792b759cddd81b550c4f3cc287cce45b38881ea6589e700caa99751f0f0baf79002a89c2089f5a5784b23d6dafbf3509dc938cd
6
+ metadata.gz: 50d747b63207ee9ae91733eb32bbbd8ee8479e985d1acf6584928a6b1017ab73c1a753b1ed792280ae68ded7f509c01243a106f4695b5f014ae9b88e480e1291
7
+ data.tar.gz: c146baed359276db93d4e696bd1b1f0cd700d022b4af12ba391cdfaccbfd858d1f0780b8d5ba20a56ea572215b19541e1213ac9b17989af05fccae6bcb0b4d76
data/README.md CHANGED
@@ -18,6 +18,13 @@ Auth methods also get deleted after the host is removed from Foreman.
18
18
 
19
19
  This allows Foreman to create everything needed to access Hashicorp Vault directly from a VM using it's Puppet certificate (e.g. for _Deferred functions_ in Puppet or other CLI tools).
20
20
 
21
+ ## Compatibility
22
+
23
+ | Foreman Version | Plugin Version |
24
+ | --------------- | -------------- |
25
+ | >= 1.23 | ~> 0.3 |
26
+ | >= 1.20 | ~> 0.2 |
27
+
21
28
  ## Requirements
22
29
 
23
30
  - Foreman >= 1.20
@@ -16,6 +16,7 @@ module ForemanVault
16
16
 
17
17
  def queue_vault_push
18
18
  return if !managed? || errors.any?
19
+ return unless orchestration_enabled?
19
20
  return unless vault_policy.valid?
20
21
  return unless vault_auth_method.valid?
21
22
 
@@ -25,6 +26,8 @@ module ForemanVault
25
26
 
26
27
  def queue_vault_destroy
27
28
  return if !managed? || errors.any?
29
+ return unless orchestration_enabled?
30
+ return unless vault_auth_method.valid?
28
31
 
29
32
  queue.create(name: _('Clear %s Vault data') % self, priority: 60,
30
33
  action: [self, :del_vault])
@@ -54,6 +57,13 @@ module ForemanVault
54
57
  Foreman::Logging.exception("Failed to clear #{name} Vault data", e)
55
58
  failure format(_("Failed to clear %{name} Vault data: %{message}\n "), name: name, message: e.message), e
56
59
  end
60
+
61
+ def orchestration_enabled?
62
+ return false unless Setting[:vault_orchestration_enabled]
63
+ return false if vault_connection.nil?
64
+
65
+ true
66
+ end
57
67
  end
58
68
  end
59
69
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanVault
4
+ module ProvisioningTemplateExtensions
5
+ extend ActiveSupport::Concern
6
+
7
+ def render(host: nil, params: {}, variables: {}, mode: Foreman::Renderer::REAL_MODE, template_input_values: {}, source_klass: nil)
8
+ source_klass = Foreman::Renderer::Source::Database if template_kind == TemplateKind.find_by(name: 'VaultPolicy')
9
+
10
+ super
11
+ end
12
+ end
13
+ end
@@ -6,7 +6,7 @@ class Setting
6
6
  BLANK_ATTRS << 'vault_policy_template'
7
7
 
8
8
  def self.default_settings
9
- [set_vault_connection, set_vault_policy_template]
9
+ [set_vault_connection, set_vault_policy_template, set_vault_orchestration_enabled]
10
10
  end
11
11
 
12
12
  def self.load_defaults
@@ -40,12 +40,15 @@ class Setting
40
40
  end
41
41
 
42
42
  def default_vault_connection
43
+ return nil unless VaultConnection.table_exists?
43
44
  return unless VaultConnection.unscoped.count == 1
44
45
 
45
46
  VaultConnection.unscoped.first.name
46
47
  end
47
48
 
48
49
  def vault_connections_collection
50
+ return [] unless VaultConnection.table_exists?
51
+
49
52
  proc { Hash[VaultConnection.unscoped.all.map { |vc| [vc.name, vc.name] }] }
50
53
  end
51
54
 
@@ -68,6 +71,15 @@ class Setting
68
71
  def vault_policy_templates_collection
69
72
  proc { Hash[ProvisioningTemplate.unscoped.of_kind(:VaultPolicy).map { |tmpl| [tmpl.name, tmpl.name] }] }
70
73
  end
74
+
75
+ def set_vault_orchestration_enabled
76
+ set(
77
+ 'vault_orchestration_enabled',
78
+ N_('Enable or disable the Vault orchestration step for managing policies and auth methods'),
79
+ false,
80
+ N_('Vault Orchestration enabled')
81
+ )
82
+ end
71
83
  end
72
84
  end
73
85
  end
@@ -23,7 +23,7 @@ module ForemanVault
23
23
  end
24
24
 
25
25
  def delete
26
- return false unless name
26
+ return false unless valid?
27
27
 
28
28
  delete_certificate(name)
29
29
  end
@@ -29,7 +29,7 @@ module ForemanVault
29
29
  end
30
30
 
31
31
  def delete
32
- return false unless name
32
+ return false unless valid?
33
33
 
34
34
  delete_policy(name)
35
35
  end
@@ -29,7 +29,7 @@ module ForemanVault
29
29
 
30
30
  initializer 'foreman_vault.register_plugin', before: :finisher_hook do |_app|
31
31
  Foreman::Plugin.register :foreman_vault do
32
- requires_foreman '>= 1.20'
32
+ requires_foreman '>= 1.23'
33
33
 
34
34
  apipie_documented_controllers ["#{ForemanVault::Engine.root}/app/controllers/api/v2/*.rb"]
35
35
 
@@ -55,6 +55,7 @@ module ForemanVault
55
55
  config.to_prepare do
56
56
  begin
57
57
  ::Host::Managed.include(ForemanVault::HostExtensions)
58
+ ::ProvisioningTemplate.include(ForemanVault::ProvisioningTemplateExtensions)
58
59
  ::Foreman::Renderer::Scope::Base.include(ForemanVault::Macros)
59
60
  ::Foreman::Renderer.configure { |c| c.allowed_generic_helpers += [:vault_secret, :vault_issue_certificate] }
60
61
  rescue StandardError => e
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanVault
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -4,6 +4,7 @@ FactoryBot.modify do
4
4
  factory :provisioning_template do
5
5
  trait :vault_policy do
6
6
  name { Setting['vault_policy_template'] || 'Default Vault Policy' }
7
+ template_kind { TemplateKind.find_or_create_by(name: 'VaultPolicy') }
7
8
  template { File.read(File.join(ForemanVault::Engine.root, 'app/views/unattended/provisioning_templates/VaultPolicy/default.erb')) }
8
9
  end
9
10
  end
@@ -10,11 +10,14 @@ module ForemanVault
10
10
  let(:queue) { mock('queue') }
11
11
  let(:vault_policy) { mock('vault_policy') }
12
12
  let(:vault_auth_method) { mock('vault_auth_method') }
13
+ let(:vault_connection) { FactoryBot.create(:vault_connection, :without_callbacks) }
13
14
 
14
15
  setup do
15
16
  host.stubs(:queue).returns(queue)
16
17
  host.stubs(:vault_policy).returns(vault_policy)
17
18
  host.stubs(:vault_auth_method).returns(vault_auth_method)
19
+ FactoryBot.create(:parameter, name: 'vault_connection', value: vault_connection.name)
20
+ FactoryBot.create(:setting, name: :vault_orchestration_enabled, value: true)
18
21
  end
19
22
 
20
23
  test 'should queue Vault orchestration' do
@@ -50,6 +53,44 @@ module ForemanVault
50
53
  end
51
54
  end
52
55
 
56
+ describe '#queue_vault_destroy' do
57
+ let(:host) { FactoryBot.create(:host, :managed) }
58
+ let(:queue) { mock('queue') }
59
+ let(:vault_policy) { mock('vault_policy') }
60
+ let(:vault_auth_method) { mock('vault_auth_method') }
61
+ let(:vault_connection) { FactoryBot.create(:vault_connection, :without_callbacks) }
62
+
63
+ setup do
64
+ host.stubs(:queue).returns(queue)
65
+ host.stubs(:vault_policy).returns(vault_policy)
66
+ host.stubs(:vault_auth_method).returns(vault_auth_method)
67
+ FactoryBot.create(:parameter, name: 'vault_connection', value: vault_connection.name)
68
+ FactoryBot.create(:setting, name: :vault_orchestration_enabled, value: true)
69
+ end
70
+
71
+ context 'when auth_method is valid' do
72
+ test 'should queue del_vault' do
73
+ vault_auth_method.stubs(:valid?).returns(true)
74
+
75
+ queue.expects(:create).with(
76
+ name: "Clear #{host} Vault data",
77
+ priority: 60,
78
+ action: [host, :del_vault]
79
+ ).once
80
+ host.send(:queue_vault_destroy)
81
+ end
82
+ end
83
+
84
+ context 'when auth_method is not valid' do
85
+ test 'should not queue del_vault' do
86
+ vault_auth_method.stubs(:valid?).returns(false)
87
+
88
+ queue.expects(:create).never
89
+ host.send(:queue_vault_destroy)
90
+ end
91
+ end
92
+ end
93
+
53
94
  describe '#set_vault' do
54
95
  let(:environment) { FactoryBot.create(:environment, name: 'MyEnv') }
55
96
  let(:host) { FactoryBot.create(:host, :managed, environment: environment) }
@@ -92,6 +133,7 @@ module ForemanVault
92
133
 
93
134
  setup do
94
135
  Setting.find_by(name: 'ssl_ca_file').update(value: File.join(ForemanVault::Engine.root, 'test/fixtures/ca.crt'))
136
+ FactoryBot.create(:setting, name: :vault_orchestration_enabled, value: true)
95
137
  FactoryBot.create(:setting, :vault_policy)
96
138
  FactoryBot.create(:provisioning_template, :vault_policy, name: Setting['vault_policy_template'])
97
139
  FactoryBot.create(:parameter, name: 'vault_connection', value: vault_connection.name)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class VaultPolicyTemplateTest < ActiveSupport::TestCase
6
+ let(:template) { FactoryBot.create(:provisioning_template, :vault_policy) }
7
+
8
+ it 'is rendered from a database' do
9
+ Foreman::Renderer.expects(:get_source).with(has_entry(klass: Foreman::Renderer::Source::Database))
10
+ Foreman::Renderer.stubs(:get_scope)
11
+ Foreman::Renderer.stubs(:render)
12
+
13
+ template.render
14
+ end
15
+ end
@@ -91,18 +91,18 @@ class VaultAuthMethodTest < ActiveSupport::TestCase
91
91
  end
92
92
 
93
93
  describe '#delete' do
94
- context 'with name' do
94
+ context 'when valid' do
95
95
  it 'deletes Certificate' do
96
- subject.stubs(:name).returns('name')
96
+ subject.stubs(:valid?).returns(true)
97
97
 
98
98
  subject.expects(:delete_certificate).once.with(subject.name)
99
99
  subject.delete
100
100
  end
101
101
  end
102
102
 
103
- context 'without name' do
103
+ context 'when not valid' do
104
104
  it 'does not delete Certificate' do
105
- subject.stubs(:name).returns(nil)
105
+ subject.stubs(:valid?).returns(false)
106
106
 
107
107
  subject.expects(:delete_certificate).never
108
108
  subject.delete
@@ -112,18 +112,18 @@ class VaultPolicyTest < ActiveSupport::TestCase
112
112
  end
113
113
 
114
114
  describe '#delete' do
115
- context 'with name' do
115
+ context 'when valid' do
116
116
  it 'deletes Vault Policy' do
117
- subject.stubs(:name).returns('name')
117
+ subject.stubs(:valid?).returns(true)
118
118
 
119
119
  subject.expects(:delete_policy).once.with(subject.name)
120
120
  subject.delete
121
121
  end
122
122
  end
123
123
 
124
- context 'without name' do
124
+ context 'when not valid' do
125
125
  it 'does not delete Vault Policy' do
126
- subject.stubs(:name).returns(nil)
126
+ subject.stubs(:valid?).returns(false)
127
127
 
128
128
  subject.expects(:delete_policy).never
129
129
  subject.delete
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_vault
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dmTECH GmbH
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-03 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: vault
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.54.0
55
- description:
55
+ description:
56
56
  email:
57
57
  - opensource@dm.de
58
58
  executables: []
@@ -70,6 +70,7 @@ files:
70
70
  - app/lib/foreman_vault/macros.rb
71
71
  - app/models/concerns/foreman_vault/host_extensions.rb
72
72
  - app/models/concerns/foreman_vault/orchestration/vault_policy.rb
73
+ - app/models/concerns/foreman_vault/provisioning_template_extensions.rb
73
74
  - app/models/setting/vault.rb
74
75
  - app/models/vault_connection.rb
75
76
  - app/services/foreman_vault/vault_auth_method.rb
@@ -109,6 +110,7 @@ files:
109
110
  - test/jobs/refresh_vault_tokens_test.rb
110
111
  - test/models/foreman_vault/orchestration/vault_policy_test.rb
111
112
  - test/models/vault_connection_test.rb
113
+ - test/models/vault_policy_template_test.rb
112
114
  - test/test_plugin_helper.rb
113
115
  - test/unit/lib/foreman_vault/macros_test.rb
114
116
  - test/unit/services/foreman_vault/vault_auth_method_test.rb
@@ -118,7 +120,7 @@ homepage: https://github.com/dm-drogeriemarkt/foreman_vault
118
120
  licenses:
119
121
  - GPL-3.0
120
122
  metadata: {}
121
- post_install_message:
123
+ post_install_message:
122
124
  rdoc_options: []
123
125
  require_paths:
124
126
  - lib
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  version: '0'
135
137
  requirements: []
136
138
  rubygems_version: 3.1.2
137
- signing_key:
139
+ signing_key:
138
140
  specification_version: 4
139
141
  summary: Adds support for using credentials from Hashicorp Vault
140
142
  test_files:
@@ -142,6 +144,7 @@ test_files:
142
144
  - test/unit/services/foreman_vault/vault_client_test.rb
143
145
  - test/unit/services/foreman_vault/vault_policy_test.rb
144
146
  - test/unit/services/foreman_vault/vault_auth_method_test.rb
147
+ - test/models/vault_policy_template_test.rb
145
148
  - test/models/vault_connection_test.rb
146
149
  - test/models/foreman_vault/orchestration/vault_policy_test.rb
147
150
  - test/factories/vault_policy_template.rb