chef 17.6.18 → 17.9.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/chef.gemspec +1 -0
  4. data/lib/chef/application/base.rb +1 -1
  5. data/lib/chef/chef_fs/file_pattern.rb +1 -1
  6. data/lib/chef/chef_fs/path_utils.rb +1 -1
  7. data/lib/chef/compliance/default_attributes.rb +12 -2
  8. data/lib/chef/compliance/runner.rb +51 -5
  9. data/lib/chef/data_collector/run_end_message.rb +1 -1
  10. data/lib/chef/dsl/reboot_pending.rb +1 -1
  11. data/lib/chef/exceptions.rb +10 -0
  12. data/lib/chef/mixin/powershell_exec.rb +6 -5
  13. data/lib/chef/mixin/why_run.rb +8 -2
  14. data/lib/chef/powershell.rb +8 -6
  15. data/lib/chef/provider/cron.rb +6 -3
  16. data/lib/chef/provider/directory.rb +2 -2
  17. data/lib/chef/provider/git.rb +1 -1
  18. data/lib/chef/provider/ifconfig/debian.rb +1 -1
  19. data/lib/chef/provider/ifconfig.rb +4 -4
  20. data/lib/chef/provider/mount/linux.rb +16 -2
  21. data/lib/chef/provider/mount/mount.rb +1 -1
  22. data/lib/chef/provider/package/dnf.rb +1 -1
  23. data/lib/chef/provider/package/habitat.rb +1 -1
  24. data/lib/chef/provider/package/powershell.rb +13 -10
  25. data/lib/chef/provider/package/yum/python_helper.rb +81 -25
  26. data/lib/chef/provider/package/yum.rb +39 -12
  27. data/lib/chef/provider/package/zypper.rb +2 -0
  28. data/lib/chef/provider/package.rb +62 -27
  29. data/lib/chef/provider/subversion.rb +5 -5
  30. data/lib/chef/provider.rb +5 -2
  31. data/lib/chef/providers.rb +0 -1
  32. data/lib/chef/pwsh.rb +3 -2
  33. data/lib/chef/resource/apt_package.rb +2 -2
  34. data/lib/chef/resource/chef_client_config.rb +21 -1
  35. data/lib/chef/resource/chef_client_launchd.rb +1 -1
  36. data/lib/chef/resource/chef_client_trusted_certificate.rb +1 -0
  37. data/lib/chef/resource/chocolatey_config.rb +1 -1
  38. data/lib/chef/resource/chocolatey_feature.rb +1 -1
  39. data/lib/chef/resource/chocolatey_package.rb +3 -3
  40. data/lib/chef/resource/chocolatey_source.rb +24 -2
  41. data/lib/chef/resource/cron/cron.rb +75 -1
  42. data/lib/chef/resource/cron/cron_d.rb +2 -1
  43. data/lib/chef/resource/directory.rb +1 -1
  44. data/lib/chef/resource/dnf_package.rb +4 -6
  45. data/lib/chef/resource/dpkg_package.rb +5 -0
  46. data/lib/chef/resource/execute.rb +1 -4
  47. data/lib/chef/resource/habitat_install.rb +5 -5
  48. data/lib/chef/resource/homebrew_tap.rb +0 -4
  49. data/lib/chef/resource/inspec_waiver.rb +1 -1
  50. data/lib/chef/resource/inspec_waiver_file_entry.rb +1 -1
  51. data/lib/chef/resource/kernel_module.rb +27 -2
  52. data/lib/chef/resource/launchd.rb +0 -3
  53. data/lib/chef/resource/macos_userdefaults.rb +41 -131
  54. data/lib/chef/resource/powershell_package_source.rb +8 -8
  55. data/lib/chef/resource/rhsm_register.rb +31 -0
  56. data/lib/chef/resource/support/client.erb +7 -0
  57. data/lib/chef/resource/windows_auto_run.rb +1 -1
  58. data/lib/chef/resource/windows_dfs_namespace.rb +2 -2
  59. data/lib/chef/resource/windows_feature_powershell.rb +8 -9
  60. data/lib/chef/resource/windows_task.rb +25 -10
  61. data/lib/chef/resource/windows_update_settings.rb +3 -3
  62. data/lib/chef/resource.rb +2 -2
  63. data/lib/chef/resource_reporter.rb +1 -1
  64. data/lib/chef/secret_fetcher/azure_key_vault.rb +64 -8
  65. data/lib/chef/secret_fetcher/hashi_vault.rb +37 -3
  66. data/lib/chef/secret_fetcher.rb +0 -1
  67. data/lib/chef/version.rb +1 -1
  68. data/spec/functional/dsl/reboot_pending_spec.rb +3 -3
  69. data/spec/functional/dsl/registry_helper_spec.rb +1 -1
  70. data/spec/functional/resource/dnf_package_spec.rb +138 -124
  71. data/spec/functional/resource/dpkg_package_spec.rb +16 -0
  72. data/spec/functional/resource/dsc_script_spec.rb +2 -2
  73. data/spec/functional/resource/macos_userdefaults_spec.rb +139 -0
  74. data/spec/functional/resource/registry_spec.rb +81 -81
  75. data/spec/functional/resource/yum_package_spec.rb +789 -129
  76. data/spec/functional/resource/zypper_package_spec.rb +7 -0
  77. data/spec/functional/win32/registry_spec.rb +8 -8
  78. data/spec/integration/client/client_spec.rb +31 -0
  79. data/spec/unit/application/base_spec.rb +40 -0
  80. data/spec/unit/compliance/runner_spec.rb +62 -1
  81. data/spec/unit/data_collector_spec.rb +24 -1
  82. data/spec/unit/dsl/reboot_pending_spec.rb +1 -1
  83. data/spec/unit/file_access_control_spec.rb +1 -1
  84. data/spec/unit/mixin/default_paths_spec.rb +1 -1
  85. data/spec/unit/mixin/securable_spec.rb +3 -3
  86. data/spec/unit/mixin/why_run_spec.rb +53 -0
  87. data/spec/unit/provider/cron_spec.rb +45 -0
  88. data/spec/unit/provider/group/groupadd_spec.rb +1 -0
  89. data/spec/unit/provider/group/usermod_spec.rb +2 -2
  90. data/spec/unit/provider/ifconfig_spec.rb +2 -0
  91. data/spec/unit/provider/mount/linux_spec.rb +16 -3
  92. data/spec/unit/provider/package/bff_spec.rb +1 -0
  93. data/spec/unit/provider/package/powershell_spec.rb +114 -114
  94. data/spec/unit/provider/package/rubygems_spec.rb +8 -5
  95. data/spec/unit/provider/package/solaris_spec.rb +1 -0
  96. data/spec/unit/provider/package/windows_spec.rb +1 -1
  97. data/spec/unit/provider/registry_key_spec.rb +4 -4
  98. data/spec/unit/provider/service/arch_service_spec.rb +2 -2
  99. data/spec/unit/provider/service/debian_service_spec.rb +1 -0
  100. data/spec/unit/provider/service/gentoo_service_spec.rb +1 -0
  101. data/spec/unit/provider/service/macosx_spec.rb +1 -0
  102. data/spec/unit/provider/service/redhat_spec.rb +4 -1
  103. data/spec/unit/provider/service/simple_service_spec.rb +6 -4
  104. data/spec/unit/provider/service/windows_spec.rb +5 -5
  105. data/spec/unit/provider/subversion_spec.rb +4 -4
  106. data/spec/unit/provider/user_spec.rb +2 -0
  107. data/spec/unit/provider/windows_env_spec.rb +1 -1
  108. data/spec/unit/provider/zypper_repository_spec.rb +1 -1
  109. data/spec/unit/resource/chef_client_trusted_certificate_spec.rb +14 -0
  110. data/spec/unit/resource/chocolatey_config_spec.rb +1 -1
  111. data/spec/unit/resource/chocolatey_feature_spec.rb +1 -1
  112. data/spec/unit/resource/chocolatey_source_spec.rb +1 -1
  113. data/spec/unit/resource/dpkg_package_spec.rb +12 -0
  114. data/spec/unit/resource/kernel_module_spec.rb +2 -1
  115. data/spec/unit/resource/macos_user_defaults_spec.rb +36 -96
  116. data/spec/unit/resource/registry_key_spec.rb +10 -10
  117. data/spec/unit/resource/rhsm_register_spec.rb +42 -0
  118. data/spec/unit/resource/windows_auto_run_spec.rb +1 -1
  119. data/spec/unit/resource/windows_feature_powershell_spec.rb +1 -1
  120. data/spec/unit/resource/windows_firewall_rule_spec.rb +2 -2
  121. data/spec/unit/resource/windows_task_spec.rb +3 -3
  122. data/spec/unit/resource_reporter_spec.rb +2 -2
  123. data/spec/unit/resource_spec.rb +5 -0
  124. data/spec/unit/secret_fetcher/azure_key_vault_spec.rb +99 -20
  125. data/spec/unit/secret_fetcher/hashi_vault_spec.rb +46 -0
  126. data/spec/unit/util/backup_spec.rb +1 -1
  127. data/spec/unit/win32/registry_spec.rb +3 -3
  128. metadata +24 -9
  129. data/lib/chef/provider/group/suse.rb +0 -82
  130. data/spec/unit/provider/group/suse_spec.rb +0 -90
@@ -79,6 +79,23 @@ class Chef
79
79
  default: false, desired_state: false,
80
80
  introduced: "15.9"
81
81
 
82
+ property :server_url, String,
83
+ description: "The hostname of the subscription service to use. The default is Customer Portal Subscription Management, subscription.rhn.redhat.com. If you do not use this option, the system registers with Customer Portal Subscription Management.",
84
+ introduced: "17.8"
85
+
86
+ property :base_url, String,
87
+ description: "The hostname of the content delivery server to use to receive updates. Both Customer Portal Subscription Management and Subscription Asset Manager use Red Hat's hosted content delivery services, with the URL https://cdn.redhat.com. Since Satellite 6 hosts its own content, the URL must be used for systems registered with Satellite 6.",
88
+ introduced: "17.8"
89
+
90
+ property :service_level, String,
91
+ description: "Sets the service level to use for subscriptions on the registering machine. This is only used with the `auto_attach` option.",
92
+ introduced: "17.8"
93
+
94
+ property :release,
95
+ [Float, String],
96
+ description: "Sets the operating system minor release to use for subscriptions for the system. Products and updates are limited to the specified minor release version. This is used only used with the `auto_attach` option. For example, `release '6.4'` will append `--release=6.4` to the register command.",
97
+ introduced: "17.8"
98
+
82
99
  action :register, description: "Register the node with RHSM." do
83
100
  package "subscription-manager"
84
101
 
@@ -170,6 +187,8 @@ class Chef
170
187
  command << new_resource.activation_key.map { |key| "--activationkey=#{Shellwords.shellescape(key)}" }
171
188
  command << "--org=#{Shellwords.shellescape(new_resource.organization)}"
172
189
  command << "--name=#{Shellwords.shellescape(new_resource.system_name)}" if new_resource.system_name
190
+ command << "--serverurl=#{Shellwords.shellescape(new_resource.server_url)}" if new_resource.server_url
191
+ command << "--baseurl=#{Shellwords.shellescape(new_resource.base_url)}" if new_resource.base_url
173
192
  command << "--force" if new_resource.force
174
193
 
175
194
  return command.join(" ")
@@ -179,11 +198,23 @@ class Chef
179
198
  if new_resource.username && new_resource.password
180
199
  raise "Unable to register - you must specify environment when using username/password" if new_resource.environment.nil? && using_satellite_host?
181
200
 
201
+ if new_resource.service_level
202
+ raise "Unable to register - 'auto_attach' must be enabled when using property `service_level`." unless new_resource.auto_attach
203
+ end
204
+
205
+ if new_resource.release
206
+ raise "Unable to register - `auto_attach` must be enabled when using property `release`." unless new_resource.auto_attach
207
+ end
208
+
182
209
  command << "--username=#{Shellwords.shellescape(new_resource.username)}"
183
210
  command << "--password=#{Shellwords.shellescape(new_resource.password)}"
184
211
  command << "--environment=#{Shellwords.shellescape(new_resource.environment)}" if using_satellite_host?
185
212
  command << "--name=#{Shellwords.shellescape(new_resource.system_name)}" if new_resource.system_name
213
+ command << "--serverurl=#{Shellwords.shellescape(new_resource.server_url)}" if new_resource.server_url
214
+ command << "--baseurl=#{Shellwords.shellescape(new_resource.base_url)}" if new_resource.base_url
186
215
  command << "--auto-attach" if new_resource.auto_attach
216
+ command << "--servicelevel=#{Shellwords.shellescape(new_resource.service_level)}" if new_resource.service_level
217
+ command << "--release=#{Shellwords.shellescape(new_resource.release)}" if new_resource.release
187
218
  command << "--force" if new_resource.force
188
219
 
189
220
  return command.join(" ")
@@ -37,6 +37,13 @@ log_location <%= @log_location %>
37
37
  log_location <%= @log_location.inspect %>
38
38
  <% end -%>
39
39
  <% end -%>
40
+ <%# These data_collector options are special as they have a '.' -%>
41
+ <% unless @data_collector_server_url.nil? || @data_collector_server_url.empty? %>
42
+ data_collector.server_url <%= @data_collector_server_url %>
43
+ <% end %>
44
+ <% unless @data_collector_token.nil? || @data_collector_token.empty? %>
45
+ data_collector.token <%= @data_collector_token %>
46
+ <% end %>
40
47
  <%# The code below is not DRY on purpose to improve readability -%>
41
48
  <% unless @start_handlers.empty? -%>
42
49
  # Do not crash if a start handler is missing / not installed yet
@@ -88,7 +88,7 @@ class Chef
88
88
  # @return [String]
89
89
  def registry_path
90
90
  { machine: "HKLM", user: "HKCU" }[new_resource.root] + \
91
- '\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run'
91
+ "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
92
92
  end
93
93
  end
94
94
  end
@@ -38,7 +38,7 @@ class Chef
38
38
 
39
39
  property :full_users, Array,
40
40
  description: "Determines which users should have full access to the share.",
41
- default: ['BUILTIN\\administrators']
41
+ default: ["BUILTIN\\administrators"]
42
42
 
43
43
  property :change_users, Array,
44
44
  description: "Determines which users should have change access to the share.",
@@ -50,7 +50,7 @@ class Chef
50
50
 
51
51
  property :root, String,
52
52
  description: "The root from which to create the DFS tree. Defaults to C:\\DFSRoots.",
53
- default: 'C:\\DFSRoots'
53
+ default: "C:\\DFSRoots"
54
54
 
55
55
  action :create, description: "Creates the dfs namespace on the server." do
56
56
  directory file_path do
@@ -100,8 +100,8 @@ class Chef
100
100
  install_command << " -Source \"#{new_resource.source}\"" if new_resource.source
101
101
  install_command << " -IncludeManagementTools" if new_resource.management_tools
102
102
 
103
- cmd = powershell_out!(install_command, timeout: new_resource.timeout)
104
- Chef::Log.info(cmd.stdout)
103
+ cmd = powershell_exec!(install_command, timeout: new_resource.timeout)
104
+ Chef::Log.info(cmd.result)
105
105
 
106
106
  reload_cached_powershell_data # Reload cached powershell feature state
107
107
  end
@@ -115,8 +115,8 @@ class Chef
115
115
 
116
116
  unless features_to_remove.empty?
117
117
  converge_by("remove Windows feature#{"s" if features_to_remove.count > 1} #{features_to_remove.join(",")}") do
118
- cmd = powershell_out!("Uninstall-WindowsFeature #{features_to_remove.join(",")}", timeout: new_resource.timeout)
119
- Chef::Log.info(cmd.stdout)
118
+ cmd = powershell_exec!("Uninstall-WindowsFeature #{features_to_remove.join(",")}", timeout: new_resource.timeout)
119
+ Chef::Log.info(cmd.result)
120
120
 
121
121
  reload_cached_powershell_data # Reload cached powershell feature state
122
122
  end
@@ -132,8 +132,8 @@ class Chef
132
132
 
133
133
  unless features_to_delete.empty?
134
134
  converge_by("delete Windows feature#{"s" if features_to_delete.count > 1} #{features_to_delete.join(",")} from the image") do
135
- cmd = powershell_out!("Uninstall-WindowsFeature #{features_to_delete.join(",")} -Remove", timeout: new_resource.timeout)
136
- Chef::Log.info(cmd.stdout)
135
+ cmd = powershell_exec!("Uninstall-WindowsFeature #{features_to_delete.join(",")} -Remove", timeout: new_resource.timeout)
136
+ Chef::Log.info(cmd.result)
137
137
 
138
138
  reload_cached_powershell_data # Reload cached powershell feature state
139
139
  end
@@ -215,9 +215,8 @@ class Chef
215
215
  # fetch the list of available feature names and state in JSON and parse the JSON
216
216
  def parsed_feature_list
217
217
  # Grab raw feature information from WindowsFeature
218
- raw_list_of_features = powershell_out!("Get-WindowsFeature | Select-Object -Property Name,InstallState | ConvertTo-Json -Compress", timeout: new_resource.timeout).stdout
219
-
220
- Chef::JSONCompat.from_json(raw_list_of_features)
218
+ raw_list_of_features = powershell_exec!("Get-WindowsFeature | Select-Object -Property Name,InstallState", timeout: new_resource.timeout).result
219
+ raw_list_of_features || []
221
220
  end
222
221
 
223
222
  # add the features values to the appropriate array
@@ -149,7 +149,6 @@ class Chef
149
149
  DOC
150
150
 
151
151
  allowed_actions :create, :delete, :run, :end, :enable, :disable, :change
152
- default_action :create
153
152
 
154
153
  property :task_name, String, regex: [%r{\A[^/\:\*\?\<\>\|]+\z}],
155
154
  description: "An optional property to set the task name if it differs from the resource block's name. Example: `Task Name` or `/Task Name`",
@@ -182,10 +181,19 @@ class Chef
182
181
  default: false
183
182
 
184
183
  property :frequency_modifier, [Integer, String],
185
- default: 1
184
+ default: 1,
185
+ description: <<~DOCS
186
+ * For frequency `:minute` valid values are 1 to 1439
187
+ * For frequency `:hourly` valid values are 1 to 23
188
+ * For frequency `:daily` valid values are 1 to 365
189
+ * For frequency `:weekly` valid values are 1 to 52
190
+ * For frequency `:monthly` valid values are `('FIRST', 'SECOND', 'THIRD', 'FOURTH', 'LAST')` OR `1-12`.
191
+ * e.g. If user want to run the task on `second week of the month` use `frequency_modifier` value as `SECOND`. Multiple values for weeks of the month should be comma separated e.g. `"FIRST, THIRD, LAST"`.
192
+ * To run task every (n) months use values 1 to 12.
193
+ DOCS
186
194
 
187
195
  property :frequency, Symbol, equal_to: %i{minute hourly daily weekly monthly once on_logon onstart on_idle none},
188
- description: "The frequency with which to run the task."
196
+ description: "The frequency with which to run the task. Note: This property is required in Chef Infra Client 14.1 or later. Note: The `:once` value requires the `start_time` property to be set."
189
197
 
190
198
  property :start_day, String,
191
199
  description: "Specifies the first date on which the task runs in **MM/DD/YYYY** format.",
@@ -195,7 +203,14 @@ class Chef
195
203
  description: "Specifies the start time to run the task, in **HH:mm** format."
196
204
 
197
205
  property :day, [String, Integer],
198
- description: "The day(s) on which the task runs."
206
+ description: <<~DOCS
207
+ The day(s) on which the task runs.
208
+ * Use this property when setting `frequency` to `:monthly` or `:weekly`.
209
+ * Valid values with frequency `:weekly` are `MON`-`SUN` or `*`.
210
+ * Valid values with frequency `:monthly` are `1-31`, `MON`-`SUN`, and `LASTDAY`.
211
+ * Use `MON`-`SUN` or `LASTDAY` if you are setting `frequency_modifier` as "FIRST, SECOND, THIRD etc." else use 1-31.
212
+ * Multiple days should be comma separated. e.g `1, 2, 3` or `MON, WED, FRI`.
213
+ DOCS
199
214
 
200
215
  property :months, String,
201
216
  description: "The Months of the year on which the task runs, such as: `JAN, FEB` or `*`. Multiple months should be comma delimited. e.g. `Jan, Feb, Mar, Dec`."
@@ -961,7 +976,7 @@ class Chef
961
976
  end
962
977
  end
963
978
 
964
- action :create do
979
+ action :create, description: "Creates a scheduled task, or updates an existing task if any property has changed." do
965
980
  set_command_and_arguments if new_resource.command
966
981
 
967
982
  if current_resource.exists
@@ -998,7 +1013,7 @@ class Chef
998
1013
  end
999
1014
  end
1000
1015
 
1001
- action :run do
1016
+ action :run, description: "Runs a scheduled task." do
1002
1017
  if current_resource.exists
1003
1018
  logger.trace "#{new_resource} task exists"
1004
1019
  if current_resource.task.status == "running"
@@ -1013,7 +1028,7 @@ class Chef
1013
1028
  end
1014
1029
  end
1015
1030
 
1016
- action :delete do
1031
+ action :delete, description: "Deletes a scheduled task." do
1017
1032
  if current_resource.exists
1018
1033
  logger.trace "#{new_resource} task exists"
1019
1034
  converge_by("delete scheduled task #{new_resource}") do
@@ -1026,7 +1041,7 @@ class Chef
1026
1041
  end
1027
1042
  end
1028
1043
 
1029
- action :end do
1044
+ action :end, description: "Ends a scheduled task." do
1030
1045
  if current_resource.exists
1031
1046
  logger.trace "#{new_resource} task exists"
1032
1047
  if current_resource.task.status != "running"
@@ -1041,7 +1056,7 @@ class Chef
1041
1056
  end
1042
1057
  end
1043
1058
 
1044
- action :enable do
1059
+ action :enable, description: "Enables a scheduled task." do
1045
1060
  if current_resource.exists
1046
1061
  logger.trace "#{new_resource} task exists"
1047
1062
  if current_resource.task.status == "not scheduled"
@@ -1058,7 +1073,7 @@ class Chef
1058
1073
  end
1059
1074
  end
1060
1075
 
1061
- action :disable do
1076
+ action :disable, description: "Disables a scheduled task." do
1062
1077
  if current_resource.exists
1063
1078
  logger.info "#{new_resource} task exists"
1064
1079
  if %w{ready running}.include?(current_resource.task.status)
@@ -145,7 +145,7 @@ class Chef
145
145
  action :set, description: "Set Windows Update settings." do
146
146
  actual_day = convert_day(new_resource.scheduled_install_day)
147
147
 
148
- registry_key 'HKEY_LOCAL_MACHINE\\Software\\Policies\\Microsoft\\Windows\\WindowsUpdate' do
148
+ registry_key "HKEY_LOCAL_MACHINE\\Software\\Policies\\Microsoft\\Windows\\WindowsUpdate" do
149
149
  recursive true
150
150
  values [{
151
151
  name: "DisableOSUpgrade",
@@ -180,7 +180,7 @@ class Chef
180
180
  action :create
181
181
  end
182
182
 
183
- registry_key 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer' do
183
+ registry_key "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer" do
184
184
  recursive true
185
185
  values [{
186
186
  name: "NoWindowsUpdate",
@@ -190,7 +190,7 @@ class Chef
190
190
  action :create
191
191
  end
192
192
 
193
- registry_key 'HKEY_LOCAL_MACHINE\\Software\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU' do
193
+ registry_key "HKEY_LOCAL_MACHINE\\Software\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU" do
194
194
  recursive true
195
195
  values [{
196
196
  name: "AUOptions",
data/lib/chef/resource.rb CHANGED
@@ -454,7 +454,7 @@ class Chef
454
454
  # @param arg [String] The umask to apply while converging the resource.
455
455
  # @return [Boolean] The umask to apply while converging the resource.
456
456
  #
457
- property :umask, String,
457
+ property :umask, [String, Integer],
458
458
  desired_state: false,
459
459
  introduced: "16.2",
460
460
  description: "Set a umask to be used for the duration of converging the resource. Defaults to `nil`, which means to use the system umask. Unsupported on Windows because Windows lacks a direct equivalent to UNIX's umask."
@@ -1508,7 +1508,7 @@ class Chef
1508
1508
  # @return Chef::CookbookVersion The cookbook in which this Resource was defined.
1509
1509
  #
1510
1510
  def cookbook_version
1511
- if cookbook_name
1511
+ if cookbook_name && cookbook_name != "@recipe_files"
1512
1512
  run_context.cookbook_collection[cookbook_name]
1513
1513
  end
1514
1514
  end
@@ -41,7 +41,7 @@ class Chef
41
41
  as_hash["result"] = action_record.action.to_s
42
42
  if new_resource.cookbook_name
43
43
  as_hash["cookbook_name"] = new_resource.cookbook_name
44
- as_hash["cookbook_version"] = new_resource.cookbook_version.version
44
+ as_hash["cookbook_version"] = new_resource.cookbook_version&.version
45
45
  end
46
46
 
47
47
  as_hash
@@ -1,4 +1,8 @@
1
1
  require_relative "base"
2
+ require_relative "../exceptions"
3
+ require "json" unless defined?(JSON)
4
+ require "net/http" unless defined?(Net::HTTP)
5
+ require "uri" unless defined?(URI)
2
6
 
3
7
  class Chef
4
8
  class SecretFetcher
@@ -14,13 +18,19 @@ class Chef
14
18
  #
15
19
  # @example
16
20
  #
17
- # fetcher = SecretFetcher.for_service(:azure_key_vault, { vault: "my_vault" }, run_context )
21
+ # fetcher = SecretFetcher.for_service(:azure_key_vault, { vault: "my_vault" }, run_context)
18
22
  # fetcher.fetch("secretkey1", "v1")
19
23
  #
20
24
  # @example
21
25
  #
22
- # fetcher = SecretFetcher.for_service(:azure_key_vault, {}, run_context )
26
+ # fetcher = SecretFetcher.for_service(:azure_key_vault, {}, run_context)
23
27
  # fetcher.fetch("my_vault/secretkey1", "v1")
28
+ #
29
+ # @example
30
+ #
31
+ # fetcher = SecretFetcher.for_service(:azure_key_vault, { client_id: "540d76b6-7f76-456c-b68b-ccae4dc9d99d" }, run_context)
32
+ # fetcher.fetch("my_vault/secretkey1", "v1")
33
+ #
24
34
  class AzureKeyVault < Base
25
35
 
26
36
  def do_fetch(name, version)
@@ -48,6 +58,12 @@ class Chef
48
58
  end
49
59
  end
50
60
 
61
+ def validate!
62
+ raise Chef::Exceptions::Secret::ConfigurationInvalid, "You may only specify one (these are mutually exclusive): :object_id, :client_id, or :mi_res_id" if [object_id, client_id, mi_res_id].select { |x| !x.nil? }.length > 1
63
+ end
64
+
65
+ private
66
+
51
67
  # Determine the vault name and secret name from the provided name.
52
68
  # If it is not in the provided name in the form "vault_name/secret_name"
53
69
  # it will determine the vault name from `config[:vault]`.
@@ -63,16 +79,56 @@ class Chef
63
79
  end
64
80
  end
65
81
 
82
+ def api_version
83
+ "2018-02-01"
84
+ end
85
+
86
+ def resource
87
+ "https://vault.azure.net"
88
+ end
89
+
90
+ def object_id
91
+ config[:object_id]
92
+ end
93
+
94
+ def client_id
95
+ config[:client_id]
96
+ end
97
+
98
+ def mi_res_id
99
+ config[:mi_res_id]
100
+ end
101
+
102
+ def token_query
103
+ @token_query ||= begin
104
+ p = {}
105
+ p["api-version"] = api_version
106
+ p["resource"] = resource
107
+ p["object_id"] = object_id if object_id
108
+ p["client_id"] = client_id if client_id
109
+ p["mi_res_id"] = mi_res_id if mi_res_id
110
+ URI.encode_www_form(p)
111
+ end
112
+ end
113
+
66
114
  def fetch_token
67
- token_uri = URI.parse("http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net")
115
+ token_uri = URI.parse("http://169.254.169.254/metadata/identity/oauth2/token")
116
+ token_uri.query = token_query
68
117
  http = Net::HTTP.new(token_uri.host, token_uri.port)
69
118
  response = http.get(token_uri, { "Metadata" => "true" })
70
- body = JSON.parse(response.body)
71
- body["access_token"]
119
+
120
+ case response
121
+ when Net::HTTPSuccess
122
+ body = JSON.parse(response.body)
123
+ body["access_token"]
124
+ when Net::HTTPBadRequest
125
+ body = JSON.parse(response.body)
126
+ raise Chef::Exceptions::Secret::Azure::IdentityNotFound if body["error_description"] =~ /identity not found/i
127
+ else
128
+ body = JSON.parse(response.body)
129
+ body["access_token"]
130
+ end
72
131
  end
73
132
  end
74
133
  end
75
134
  end
76
-
77
-
78
-
@@ -31,6 +31,10 @@ class Chef
31
31
  # :auth_method - one of :iam_role, :token. default: :iam_role
32
32
  # :vault_addr - the address of a running Vault instance, eg https://vault.example.com:8200
33
33
  #
34
+ # For `:approle`: one of `:approle_name` or `:approle_id`
35
+ # `:approle_name`: The name of the approle to use for authentication. When specified, associated `:approle_id` will be found via query to Vault instance.
36
+ # `:approle_id`: The ID of the approle to use for authentication, requires `:approle_secret_id`
37
+ # `:approle_secret_id`: The Vault `secret_id` associated with the provided `:approle_name` or `:approle_id`. When specified, prevents need to create `:secret_id` with `:approle_name`.
34
38
  # For `:token` auth: `:token` - a Vault token valid for authentication.
35
39
  #
36
40
  # For `:iam_role`: `:role_name` - the name of the role in Vault that was created
@@ -47,14 +51,25 @@ class Chef
47
51
  #
48
52
  # @example
49
53
  #
50
- # fetcher = SecretFetcher.for_service(:hashi_vault, { role_name: "testing-role", vault_addr: https://localhost:8200}, run_context )
54
+ # fetcher = SecretFetcher.for_service(:hashi_vault, { auth_method: :iam_role, role_name: "testing-role", vault_addr: https://localhost:8200}, run_context )
51
55
  # fetcher.fetch("secretkey1")
52
56
  #
53
57
  # @example
54
58
  #
55
- # fetcher = SecretFetcher.for_service(:hashi_vault, { auth_method: :token, token: "s.1234abcdef", vault_addr: https://localhost:8200}, run_context )
59
+ # fetcher = SecretFetcher.for_service(:hashi_vault, { auth_method: :token, token: "s.1234abcdef", vault_addr: https://localhost:8200}, approle: 'approle_name', run_context )
56
60
  # fetcher.fetch("secretkey1")
57
- SUPPORTED_AUTH_TYPES = %i{iam_role token}.freeze
61
+ #
62
+ # @example
63
+ #
64
+ # fetcher = SecretFetcher.for_service(:hashi_vault, { auth_method: :approle, approle_id: "11111111-abcd-1111-abcd-111111111111", approle_secret_id: "22222222-abcd-2222-abcd-222222222222", vault_addr: https://localhost:8200}, run_context )
65
+ # fetcher.fetch("secretkey1")
66
+ #
67
+ # @example
68
+ #
69
+ # fetcher = SecretFetcher.for_service(:hashi_vault, { auth_method: :approle, approle_name: "testing-role", token: "s.1234abcdef", vault_addr: https://localhost:8200}, run_context )
70
+ # fetcher.fetch("secretkey1")
71
+ #
72
+ SUPPORTED_AUTH_TYPES = %i{approle iam_role token}.freeze
58
73
  class HashiVault < Base
59
74
 
60
75
  # Validate and authenticate the current session using the configured auth strategy and parameters
@@ -67,6 +82,25 @@ class Chef
67
82
  Vault.namespace = config[:namespace] unless config[:namespace].nil?
68
83
 
69
84
  case config[:auth_method]
85
+ when :approle
86
+ unless config[:approle_name] || config[:approle_id]
87
+ raise Chef::Exceptions::Secret::ConfigurationInvalid.new("You must provide the :approle_name or :approle_id in the configuration with :auth_method set to :approle")
88
+ end
89
+
90
+ # When :approle_id and :approle_secret_id are both specified, all pieces are present which are needed to authenticate using an approle.
91
+ # If either is missing, we need to authenticate to Vault to get the missing pieces with the :approle_name and optionally :token.
92
+ unless config[:approle_id] && config[:approle_secret_id]
93
+ if config[:approle_name].nil?
94
+ raise Chef::Exceptions::Secret::ConfigurationInvalid.new("You must provide the :approle_name in the configuration when :approle_id and :approle_secret_id are not both present with :auth_method set to :approle")
95
+ end
96
+
97
+ Vault.token = config[:token] unless config[:token].nil?
98
+ end
99
+
100
+ approle_id = config[:approle_id] || Vault.approle.role_id(config[:approle_name])
101
+ approle_secret_id = config[:approle_secret_id] || Vault.approle.create_secret_id(config[:approle_name]).data[:secret_id]
102
+
103
+ Vault.auth.approle(approle_id, approle_secret_id)
70
104
  when :token
71
105
  if config[:token].nil?
72
106
  raise Chef::Exceptions::Secret::ConfigurationInvalid.new("You must provide the token in the configuration as :token")
@@ -58,4 +58,3 @@ class Chef
58
58
  end
59
59
  end
60
60
  end
61
-
data/lib/chef/version.rb CHANGED
@@ -23,7 +23,7 @@ require_relative "version_string"
23
23
 
24
24
  class Chef
25
25
  CHEF_ROOT = File.expand_path("..", __dir__)
26
- VERSION = Chef::VersionString.new("17.6.18")
26
+ VERSION = Chef::VersionString.new("17.9.18")
27
27
  end
28
28
 
29
29
  #
@@ -39,8 +39,8 @@ describe Chef::DSL::RebootPending, :windows_only do
39
39
  let(:reg_key) { nil }
40
40
  let(:original_set) { false }
41
41
 
42
- describe 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations' do
43
- let(:reg_key) { 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager' }
42
+ describe "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\PendingFileRenameOperations" do
43
+ let(:reg_key) { "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager" }
44
44
  let(:original_set) { registry.value_exists?(reg_key, { name: "PendingFileRenameOperations" }) }
45
45
 
46
46
  it "returns true if the registry value exists" do
@@ -78,7 +78,7 @@ describe Chef::DSL::RebootPending, :windows_only do
78
78
 
79
79
  describe "when there is nothing to indicate a reboot is pending" do
80
80
  it "should return false" do
81
- skip "reboot pending" if registry_value_exists?('HKLM\SYSTEM\CurrentControlSet\Control\Session Manager', { name: "PendingFileRenameOperations" }) ||
81
+ skip "reboot pending" if registry_value_exists?("HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager", { name: "PendingFileRenameOperations" }) ||
82
82
  registry_key_exists?('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') ||
83
83
  registry_key_exists?('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending')
84
84
  expect(recipe.reboot_pending?).to be_falsey
@@ -24,7 +24,7 @@ describe Chef::Resource::RegistryKey, :windows_only do
24
24
  before(:all) do
25
25
  ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root"
26
26
  ::Win32::Registry::HKEY_CURRENT_USER.create "Software\\Root\\Branch"
27
- ::Win32::Registry::HKEY_CURRENT_USER.open('Software\\Root', Win32::Registry::KEY_ALL_ACCESS) do |reg|
27
+ ::Win32::Registry::HKEY_CURRENT_USER.open("Software\\Root", Win32::Registry::KEY_ALL_ACCESS) do |reg|
28
28
  reg["RootType1", Win32::Registry::REG_SZ] = "fibrous"
29
29
  reg.write("Roots", Win32::Registry::REG_MULTI_SZ, ["strong roots", "healthy tree"])
30
30
  end