chef 17.2.29-universal-mingw32 → 17.5.22-universal-mingw32

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.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -5
  3. data/chef.gemspec +3 -0
  4. data/lib/chef/application/base.rb +11 -1
  5. data/lib/chef/application.rb +3 -1
  6. data/lib/chef/client.rb +2 -3
  7. data/lib/chef/compliance/default_attributes.rb +5 -3
  8. data/lib/chef/compliance/input.rb +115 -0
  9. data/lib/chef/compliance/input_collection.rb +139 -0
  10. data/lib/chef/compliance/profile.rb +122 -0
  11. data/lib/chef/compliance/profile_collection.rb +109 -0
  12. data/lib/chef/compliance/reporter/automate.rb +1 -1
  13. data/lib/chef/compliance/runner.rb +62 -6
  14. data/lib/chef/compliance/waiver.rb +115 -0
  15. data/lib/chef/compliance/waiver_collection.rb +143 -0
  16. data/lib/chef/data_bag.rb +1 -2
  17. data/lib/chef/data_bag_item.rb +1 -2
  18. data/lib/chef/deprecated.rb +10 -4
  19. data/lib/chef/dsl/compliance.rb +38 -0
  20. data/lib/chef/dsl/reader_helpers.rb +51 -0
  21. data/lib/chef/dsl/recipe.rb +4 -2
  22. data/lib/chef/dsl/render_helpers.rb +44 -0
  23. data/lib/chef/dsl/secret.rb +62 -0
  24. data/lib/chef/dsl/toml.rb +116 -0
  25. data/lib/chef/dsl/universal.rb +7 -0
  26. data/lib/chef/dsl.rb +1 -0
  27. data/lib/chef/event_dispatch/base.rb +44 -2
  28. data/lib/chef/exceptions.rb +20 -0
  29. data/lib/chef/formatters/doc.rb +60 -13
  30. data/lib/chef/formatters/error_mapper.rb +2 -2
  31. data/lib/chef/formatters/minimal.rb +6 -5
  32. data/lib/chef/handler/slow_report.rb +1 -1
  33. data/lib/chef/http/basic_client.rb +15 -7
  34. data/lib/chef/http.rb +12 -8
  35. data/lib/chef/json_compat.rb +1 -1
  36. data/lib/chef/policy_builder/policyfile.rb +88 -45
  37. data/lib/chef/provider/execute.rb +1 -1
  38. data/lib/chef/provider/file.rb +4 -2
  39. data/lib/chef/provider/group/dscl.rb +1 -1
  40. data/lib/chef/provider/launchd.rb +6 -6
  41. data/lib/chef/provider/link.rb +2 -2
  42. data/lib/chef/provider/lwrp_base.rb +1 -1
  43. data/lib/chef/provider/package/habitat.rb +168 -0
  44. data/lib/chef/provider/package/powershell.rb +5 -0
  45. data/lib/chef/provider/registry_key.rb +3 -2
  46. data/lib/chef/provider/remote_file/http.rb +1 -1
  47. data/lib/chef/provider/subversion.rb +4 -4
  48. data/lib/chef/provider/support/yum_repo.erb +1 -1
  49. data/lib/chef/provider/systemd_unit.rb +17 -16
  50. data/lib/chef/provider/template.rb +1 -1
  51. data/lib/chef/provider/user/mac.rb +3 -3
  52. data/lib/chef/provider/yum_repository.rb +27 -43
  53. data/lib/chef/provider/zypper_repository.rb +3 -3
  54. data/lib/chef/provider.rb +26 -1
  55. data/lib/chef/provider_resolver.rb +8 -2
  56. data/lib/chef/providers.rb +1 -0
  57. data/lib/chef/resource/archive_file.rb +17 -14
  58. data/lib/chef/resource/chef_client_config.rb +7 -2
  59. data/lib/chef/resource/chef_client_cron.rb +1 -1
  60. data/lib/chef/resource/chef_client_launchd.rb +1 -1
  61. data/lib/chef/resource/chef_client_scheduled_task.rb +46 -3
  62. data/lib/chef/resource/chef_client_systemd_timer.rb +1 -1
  63. data/lib/chef/resource/chef_client_trusted_certificate.rb +2 -2
  64. data/lib/chef/resource/chef_vault_secret.rb +2 -2
  65. data/lib/chef/resource/chocolatey_config.rb +13 -13
  66. data/lib/chef/resource/dsc_resource.rb +1 -1
  67. data/lib/chef/resource/execute.rb +5 -5
  68. data/lib/chef/resource/file/verification/json.rb +50 -0
  69. data/lib/chef/resource/file/verification/yaml.rb +52 -0
  70. data/lib/chef/resource/gem_package.rb +2 -1
  71. data/lib/chef/resource/habitat/_habitat_shared.rb +28 -0
  72. data/lib/chef/resource/habitat/habitat_package.rb +129 -0
  73. data/lib/chef/resource/habitat/habitat_sup.rb +329 -0
  74. data/lib/chef/resource/habitat/habitat_sup_systemd.rb +67 -0
  75. data/lib/chef/resource/habitat/habitat_sup_windows.rb +90 -0
  76. data/lib/chef/resource/habitat_config.rb +107 -0
  77. data/lib/chef/resource/habitat_install.rb +247 -0
  78. data/lib/chef/resource/habitat_service.rb +451 -0
  79. data/lib/chef/resource/habitat_user_toml.rb +92 -0
  80. data/lib/chef/resource/homebrew_cask.rb +1 -1
  81. data/lib/chef/resource/inspec_input.rb +128 -0
  82. data/lib/chef/resource/inspec_waiver.rb +185 -0
  83. data/lib/chef/resource/inspec_waiver_file_entry.rb +2 -2
  84. data/lib/chef/resource/launchd.rb +3 -3
  85. data/lib/chef/resource/lwrp_base.rb +1 -1
  86. data/lib/chef/resource/mount.rb +1 -1
  87. data/lib/chef/resource/registry_key.rb +36 -48
  88. data/lib/chef/resource/remote_file.rb +99 -3
  89. data/lib/chef/resource/rhsm_subscription.rb +5 -5
  90. data/lib/chef/resource/ruby_block.rb +100 -0
  91. data/lib/chef/resource/scm/subversion.rb +1 -1
  92. data/lib/chef/resource/support/HabService.dll.config.erb +19 -0
  93. data/lib/chef/resource/support/client.erb +8 -1
  94. data/lib/chef/resource/support/sup.toml.erb +179 -0
  95. data/lib/chef/resource/sysctl.rb +2 -2
  96. data/lib/chef/resource/systemd_unit.rb +3 -3
  97. data/lib/chef/resource/timezone.rb +2 -2
  98. data/lib/chef/resource/user_ulimit.rb +1 -0
  99. data/lib/chef/resource/windows_defender.rb +163 -0
  100. data/lib/chef/resource/windows_defender_exclusion.rb +125 -0
  101. data/lib/chef/resource/windows_printer.rb +78 -44
  102. data/lib/chef/resource/windows_printer_port.rb +1 -1
  103. data/lib/chef/resource/windows_uac.rb +3 -1
  104. data/lib/chef/resource/windows_update_settings.rb +259 -0
  105. data/lib/chef/resource/windows_user_privilege.rb +1 -1
  106. data/lib/chef/resource/yum_package.rb +1 -5
  107. data/lib/chef/resource.rb +13 -17
  108. data/lib/chef/resource_inspector.rb +6 -2
  109. data/lib/chef/resources.rb +14 -1
  110. data/lib/chef/run_context/cookbook_compiler.rb +112 -28
  111. data/lib/chef/run_context.rb +31 -1
  112. data/lib/chef/secret_fetcher/akeyless_vault.rb +57 -0
  113. data/lib/chef/secret_fetcher/aws_secrets_manager.rb +65 -0
  114. data/lib/chef/secret_fetcher/azure_key_vault.rb +78 -0
  115. data/lib/chef/secret_fetcher/base.rb +76 -0
  116. data/lib/chef/secret_fetcher/example.rb +46 -0
  117. data/lib/chef/secret_fetcher/hashi_vault.rb +100 -0
  118. data/lib/chef/secret_fetcher.rb +61 -0
  119. data/lib/chef/version.rb +1 -1
  120. data/spec/data/archive_file/test_archive.tar.gz +0 -0
  121. data/spec/functional/mixin/from_file_spec.rb +1 -1
  122. data/spec/functional/resource/archive_file_spec.rb +87 -0
  123. data/spec/functional/resource/group_spec.rb +5 -1
  124. data/spec/functional/resource/link_spec.rb +8 -0
  125. data/spec/integration/compliance/compliance_spec.rb +61 -0
  126. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -1
  127. data/spec/integration/recipes/resource_action_spec.rb +6 -6
  128. data/spec/spec_helper.rb +3 -0
  129. data/spec/support/platform_helpers.rb +4 -0
  130. data/spec/support/ruby_installer.rb +51 -0
  131. data/spec/support/shared/unit/provider/file.rb +2 -8
  132. data/spec/unit/compliance/input_spec.rb +104 -0
  133. data/spec/unit/compliance/profile_spec.rb +120 -0
  134. data/spec/unit/compliance/runner_spec.rb +46 -2
  135. data/spec/unit/compliance/waiver_spec.rb +104 -0
  136. data/spec/unit/data_bag_item_spec.rb +2 -2
  137. data/spec/unit/data_bag_spec.rb +1 -1
  138. data/spec/unit/dsl/render_helpers_spec.rb +102 -0
  139. data/spec/unit/dsl/secret_spec.rb +71 -0
  140. data/spec/unit/formatters/doc_spec.rb +1 -1
  141. data/spec/unit/http/basic_client_spec.rb +30 -0
  142. data/spec/unit/http_spec.rb +8 -2
  143. data/spec/unit/policy_builder/dynamic_spec.rb +0 -5
  144. data/spec/unit/policy_builder/policyfile_spec.rb +144 -56
  145. data/spec/unit/provider/apt_update_spec.rb +3 -1
  146. data/spec/unit/provider/link_spec.rb +13 -7
  147. data/spec/unit/provider/mount/aix_spec.rb +1 -1
  148. data/spec/unit/provider/package/powershell_spec.rb +74 -12
  149. data/spec/unit/provider/remote_file/http_spec.rb +10 -0
  150. data/spec/unit/provider/template_spec.rb +2 -2
  151. data/spec/unit/provider_spec.rb +23 -0
  152. data/spec/unit/resource/archive_file_spec.rb +414 -3
  153. data/spec/unit/resource/chef_client_scheduled_task_spec.rb +69 -0
  154. data/spec/unit/resource/file/verification/json_spec.rb +72 -0
  155. data/spec/unit/resource/file/verification/yaml_spec.rb +67 -0
  156. data/spec/unit/resource/homebrew_cask_spec.rb +29 -11
  157. data/spec/unit/resource/inspec_input_spec.rb +300 -0
  158. data/spec/unit/resource/inspec_waiver_spec.rb +312 -0
  159. data/spec/unit/resource/mount_spec.rb +10 -0
  160. data/spec/unit/resource/rhsm_subscription_spec.rb +50 -3
  161. data/spec/unit/resource/systemd_unit_spec.rb +1 -1
  162. data/spec/unit/resource/user_ulimit_spec.rb +14 -1
  163. data/spec/unit/resource/windows_defender_exclusion_spec.rb +62 -0
  164. data/spec/unit/resource/windows_defender_spec.rb +71 -0
  165. data/spec/unit/resource/windows_update_settings_spec.rb +64 -0
  166. data/spec/unit/resource_spec.rb +19 -8
  167. data/spec/unit/secret_fetcher/akeyless_vault_spec.rb +37 -0
  168. data/spec/unit/secret_fetcher/aws_secrets_manager_spec.rb +70 -0
  169. data/spec/unit/secret_fetcher/azure_key_vault_spec.rb +70 -0
  170. data/spec/unit/secret_fetcher/hashi_vault_spec.rb +80 -0
  171. data/spec/unit/secret_fetcher_spec.rb +82 -0
  172. data/tasks/rspec.rb +2 -1
  173. metadata +106 -7
@@ -12,6 +12,8 @@ class Chef
12
12
 
13
13
  attr_accessor :run_id
14
14
  attr_reader :node
15
+ attr_reader :run_context
16
+
15
17
  def_delegators :node, :logger
16
18
 
17
19
  def enabled?
@@ -25,7 +27,9 @@ class Chef
25
27
  logger.debug("#{self.class}##{__method__}: audit cookbook? #{audit_cookbook_present}")
26
28
  logger.debug("#{self.class}##{__method__}: compliance phase attr? #{node["audit"]["compliance_phase"]}")
27
29
 
28
- if node["audit"]["compliance_phase"].nil?
30
+ if safe_profile_collection&.using_profiles?
31
+ true
32
+ elsif node["audit"]["compliance_phase"].nil?
29
33
  inspec_profiles.any? && !audit_cookbook_present
30
34
  else
31
35
  node["audit"]["compliance_phase"]
@@ -41,6 +45,14 @@ class Chef
41
45
  self.node = node
42
46
  end
43
47
 
48
+ # This hook gives us the run_context immediately after it is created so that we can wire up this object to it.
49
+ #
50
+ # (see EventDispatch::Base#)
51
+ #
52
+ def cookbook_compilation_start(run_context)
53
+ @run_context = run_context
54
+ end
55
+
44
56
  def run_started(run_status)
45
57
  self.run_id = run_status.run_id
46
58
  end
@@ -113,8 +125,25 @@ class Chef
113
125
  logger.info "Chef Infra Compliance Phase Complete"
114
126
  end
115
127
 
128
+ def inputs_from_attributes
129
+ if !node["audit"]["inputs"].empty?
130
+ node["audit"]["inputs"].to_h
131
+ else
132
+ node["audit"]["attributes"].to_h
133
+ end
134
+ end
135
+
136
+ def inputs_from_collection
137
+ safe_input_collection&.inspec_data || {}
138
+ end
139
+
140
+ def waivers_from_collection
141
+ safe_waiver_collection&.inspec_data || {}
142
+ end
143
+
116
144
  def inspec_opts
117
- inputs = node["audit"]["attributes"].to_h
145
+ inputs = inputs_from_attributes.merge(inputs_from_collection).merge(waivers_from_collection)
146
+
118
147
  if node["audit"]["chef_node_attribute_enabled"]
119
148
  inputs["chef_node"] = node.to_h
120
149
  inputs["chef_node"]["chef_environment"] = node.chef_environment
@@ -124,24 +153,34 @@ class Chef
124
153
  backend_cache: node["audit"]["inspec_backend_cache"],
125
154
  inputs: inputs,
126
155
  logger: logger,
156
+ # output: STDOUT,
127
157
  output: node["audit"]["quiet"] ? ::File::NULL : STDOUT,
128
158
  report: true,
129
159
  reporter: ["json-automate"],
160
+ # reporter: ["cli"],
130
161
  reporter_backtrace_inclusion: node["audit"]["result_include_backtrace"],
131
162
  reporter_message_truncation: node["audit"]["result_message_limit"],
132
- waiver_file: Array(node["audit"]["waiver_file"]),
163
+ waiver_file: waiver_files,
133
164
  }
134
165
  end
135
166
 
167
+ def waiver_files
168
+ Array(node["audit"]["waiver_file"])
169
+ end
170
+
136
171
  def inspec_profiles
137
172
  profiles = node["audit"]["profiles"]
138
173
  unless profiles.respond_to?(:map) && profiles.all? { |_, p| p.respond_to?(:transform_keys) && p.respond_to?(:update) }
139
174
  raise "CMPL010: #{Inspec::Dist::PRODUCT_NAME} profiles specified in an unrecognized format, expected a hash of hashes."
140
175
  end
141
176
 
142
- profiles.map do |name, profile|
177
+ from_attributes = profiles.map do |name, profile|
143
178
  profile.transform_keys(&:to_sym).update(name: name)
144
- end
179
+ end || []
180
+
181
+ from_cookbooks = safe_profile_collection&.inspec_data || []
182
+
183
+ from_attributes + from_cookbooks
145
184
  end
146
185
 
147
186
  def load_fetchers!
@@ -171,7 +210,7 @@ class Chef
171
210
  logger.info "Running profiles from: #{profiles.inspect}"
172
211
  runner.run
173
212
  runner.report.tap do |r|
174
- logger.debug "Compliance Report #{r}"
213
+ logger.debug "Compliance Phase report #{r}"
175
214
  end
176
215
  rescue Inspec::FetcherFailure => e
177
216
  failed_report("Cannot fetch all profiles: #{profiles}. Please make sure you're authenticated and the server is reachable. #{e.message}")
@@ -300,8 +339,25 @@ class Chef
300
339
  raise "CMPL002: Unrecognized Compliance Phase fetcher (node['audit']['fetcher'] = #{fetcher}). Supported fetchers are: #{SUPPORTED_FETCHERS.join(", ")}, or nil. For more information, see the documentation at https://docs.chef.io/chef_compliance_phase#fetch-profiles"
301
340
  end
302
341
  end
342
+
343
+ if !node["audit"]["attributes"].empty? && !node["audit"]["inputs"].empty?
344
+ raise "CMPL011: both node['audit']['inputs'] and node['audit']['attributes'] are set. The node['audit']['attributes'] setting is deprecated and should not be used."
345
+ end
346
+
303
347
  @validation_passed = true
304
348
  end
349
+
350
+ def safe_profile_collection
351
+ run_context&.profile_collection
352
+ end
353
+
354
+ def safe_waiver_collection
355
+ run_context&.waiver_collection
356
+ end
357
+
358
+ def safe_input_collection
359
+ run_context&.input_collection
360
+ end
305
361
  end
306
362
  end
307
363
  end
@@ -0,0 +1,115 @@
1
+ #
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "yaml"
19
+
20
+ class Chef
21
+ module Compliance
22
+ #
23
+ # Chef object that represents a single waiver file in the compliance
24
+ # segment of a cookbook
25
+ #
26
+ class Waiver
27
+ # @return [Boolean] if the waiver has been enabled
28
+ attr_reader :enabled
29
+
30
+ # @return [String] The name of the cookbook that the waiver is in
31
+ attr_reader :cookbook_name
32
+
33
+ # @return [String] The full path on the host to the waiver yml file
34
+ attr_reader :path
35
+
36
+ # @return [String] the pathname in the cookbook
37
+ attr_reader :pathname
38
+
39
+ # @api private
40
+ attr_reader :data
41
+
42
+ # Event dispatcher for this run.
43
+ #
44
+ # @return [Chef::EventDispatch::Dispatcher]
45
+ #
46
+ attr_accessor :events
47
+
48
+ def initialize(events, data, path, cookbook_name)
49
+ @events = events
50
+ @data = data
51
+ @cookbook_name = cookbook_name
52
+ @path = path
53
+ @pathname = File.basename(path, File.extname(path)) unless path.nil?
54
+ disable!
55
+ end
56
+
57
+ # @return [Boolean] if the waiver has been enabled
58
+ #
59
+ def enabled?
60
+ !!@enabled
61
+ end
62
+
63
+ # Set the waiver to being enabled
64
+ #
65
+ def enable!
66
+ events.compliance_waiver_enabled(self)
67
+ @enabled = true
68
+ end
69
+
70
+ # Set the waiver as being disabled
71
+ #
72
+ def disable!
73
+ @enabled = false
74
+ end
75
+
76
+ # Render the waiver in a way that it can be consumed by inspec
77
+ #
78
+ def inspec_data
79
+ data
80
+ end
81
+
82
+ HIDDEN_IVARS = [ :@events ].freeze
83
+
84
+ # Omit the event object from error output
85
+ #
86
+ def inspect
87
+ ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
88
+ "#{ivar}=#{instance_variable_get(ivar).inspect}"
89
+ end.join(", ")
90
+ "#<#{self.class}:#{object_id} #{ivar_string}>"
91
+ end
92
+
93
+ # Helper to construct a waiver object from a hash. Since the path and
94
+ # cookbook_name are required this is probably not externally useful.
95
+ #
96
+ def self.from_hash(events, hash, path = nil, cookbook_name = nil)
97
+ new(events, hash, path, cookbook_name)
98
+ end
99
+
100
+ # Helper to construct a waiver object from a yaml string. Since the path
101
+ # and cookbook_name are required this is probably not externally useful.
102
+ #
103
+ def self.from_yaml(events, string, path = nil, cookbook_name = nil)
104
+ from_hash(events, YAML.load(string), path, cookbook_name)
105
+ end
106
+
107
+ # @param filename [String] full path to the yml file in the cookbook
108
+ # @param cookbook_name [String] cookbook that the waiver is in
109
+ #
110
+ def self.from_file(events, filename, cookbook_name = nil)
111
+ from_yaml(events, IO.read(filename), filename, cookbook_name)
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,143 @@
1
+ # Copyright:: Copyright (c) Chef Software Inc.
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require_relative "waiver"
18
+
19
+ class Chef
20
+ module Compliance
21
+ class WaiverCollection < Array
22
+
23
+ # Event dispatcher for this run.
24
+ #
25
+ # @return [Chef::EventDispatch::Dispatcher]
26
+ #
27
+ attr_reader :events
28
+
29
+ def initialize(events)
30
+ @events = events
31
+ end
32
+
33
+ # Add a waiver to the waiver collection. The cookbook_name needs to be determined by the
34
+ # caller and is used in the `include_waiver` API to match on. The path should be the complete
35
+ # path on the host of the yml file, including the filename.
36
+ #
37
+ # @param path [String]
38
+ # @param cookbook_name [String]
39
+ #
40
+ def from_file(filename, cookbook_name)
41
+ new_waiver = Waiver.from_file(events, filename, cookbook_name)
42
+ self << new_waiver
43
+ events.compliance_waiver_loaded(new_waiver)
44
+ end
45
+
46
+ # Add a waiver from a raw hash. This waiver will be enabled by default.
47
+ #
48
+ # @param path [String]
49
+ # @param cookbook_name [String]
50
+ #
51
+ def from_hash(hash)
52
+ new_waiver = Waiver.from_hash(events, hash)
53
+ new_waiver.enable!
54
+ self << new_waiver
55
+ end
56
+
57
+ # @return [Array<Waiver>] inspec waivers which are enabled in a form suitable to pass to inspec
58
+ #
59
+ def inspec_data
60
+ select(&:enabled?).each_with_object({}) { |waiver, hash| hash.merge(waiver.inspec_data) }
61
+ end
62
+
63
+ # DSL method to enable waiver files. This matches on the filename of the waiver file.
64
+ # If the specific waiver is omitted then it uses the default waiver. The string
65
+ # supports regular expression matching.
66
+ #
67
+ # @example Specific waiver file in a cookbook
68
+ #
69
+ # include_waiver "acme_cookbook::ssh-001"
70
+ #
71
+ # @example The compliance/waiver/default.rb waiver file in a cookbook
72
+ #
73
+ # include_waiver "acme_cookbook"
74
+ #
75
+ # @example Every waiver file in a cookbook
76
+ #
77
+ # include_waiver "acme_cookbook::.*"
78
+ #
79
+ # @example Matching waivers by regexp in a cookbook
80
+ #
81
+ # include_waiver "acme_cookbook::ssh.*"
82
+ #
83
+ # @example Matching waivers by regexp in any cookbook in the cookbook collection
84
+ #
85
+ # include_waiver ".*::ssh.*"
86
+ #
87
+ # @example Adding an arbitrary hash of data (not from any file in a cookbook)
88
+ #
89
+ # include_waiver({ "ssh-01" => {
90
+ # "expiration_date" => "2033-07-31",
91
+ # "run" => false,
92
+ # "justification" => "the reason it is waived",
93
+ # } })
94
+ #
95
+ def include_waiver(arg)
96
+ raise "include_waiver was given a nil value" if arg.nil?
97
+
98
+ # if we're given a hash argument just shove it in the collection
99
+ if arg.is_a?(Hash)
100
+ from_hash(arg)
101
+ return
102
+ end
103
+
104
+ matching_waivers!(arg).each(&:enable!)
105
+ end
106
+
107
+ def valid?(arg)
108
+ !matching_waivers(arg).empty?
109
+ end
110
+
111
+ HIDDEN_IVARS = [ :@events ].freeze
112
+
113
+ # Omit the event object from error output
114
+ #
115
+ def inspect
116
+ ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
117
+ "#{ivar}=#{instance_variable_get(ivar).inspect}"
118
+ end.join(", ")
119
+ "#<#{self.class}:#{object_id} #{ivar_string}>"
120
+ end
121
+
122
+ private
123
+
124
+ def matching_waivers(arg, should_raise: false)
125
+ (cookbook_name, waiver_name) = arg.split("::")
126
+
127
+ waiver_name = "default" if waiver_name.nil?
128
+
129
+ waivers = select { |waiver| /^#{cookbook_name}$/.match?(waiver.cookbook_name) && /^#{waiver_name}$/.match?(waiver.pathname) }
130
+
131
+ if waivers.empty? && should_raise
132
+ raise "No inspec waivers matching '#{waiver_name}' found in cookbooks matching '#{cookbook_name}'"
133
+ end
134
+
135
+ waivers
136
+ end
137
+
138
+ def matching_waivers!(arg)
139
+ matching_waivers(arg, should_raise: true)
140
+ end
141
+ end
142
+ end
143
+ end
data/lib/chef/data_bag.rb CHANGED
@@ -32,8 +32,7 @@ class Chef
32
32
  include Chef::Mixin::FromFile
33
33
  include Chef::Mixin::ParamsValidate
34
34
 
35
- # Regex reference: https://rubular.com/r/oIMySIO4USPm5x
36
- VALID_NAME = /^[\-[:alnum:]_]+$/.freeze
35
+ VALID_NAME = /^[\.\-[:alnum:]_]+$/.freeze
37
36
  RESERVED_NAMES = /^(node|role|environment|client)$/.freeze
38
37
 
39
38
  def self.validate_name!(name)
@@ -36,8 +36,7 @@ class Chef
36
36
  include Chef::Mixin::FromFile
37
37
  include Chef::Mixin::ParamsValidate
38
38
 
39
- # Regex reference: https://rubular.com/r/oIMySIO4USPm5x
40
- VALID_ID = /^[\-[:alnum:]_]+$/.freeze
39
+ VALID_ID = /^[\.\-[:alnum:]_]+$/.freeze
41
40
 
42
41
  def self.validate_id!(id_str)
43
42
  if id_str.nil? || ( id_str !~ VALID_ID )
@@ -79,10 +79,12 @@ class Chef
79
79
  return true if location =~ /^(.*?):(\d+):in/ && begin
80
80
  # Don't buffer the whole file in memory, so read it one line at a time.
81
81
  line_no = $2.to_i
82
- location_file = ::File.open($1)
83
- (line_no - 1).times { location_file.readline } # Read all the lines we don't care about.
84
- relevant_line = location_file.readline
85
- relevant_line.match?(/#.*chef:silence_deprecation($|[^:]|:#{self.class.deprecation_key})/)
82
+ if File.exist?($1) # some stacktraces come from `eval` and not a file
83
+ location_file = ::File.open($1)
84
+ (line_no - 1).times { location_file.readline } # Read all the lines we don't care about.
85
+ relevant_line = location_file.readline
86
+ relevant_line.match?(/#.*chef:silence_deprecation($|[^:]|:#{self.class.deprecation_key})/)
87
+ end
86
88
  end
87
89
 
88
90
  false
@@ -257,6 +259,10 @@ class Chef
257
259
  target 34
258
260
  end
259
261
 
262
+ class PolicyfileCompatMode < Base
263
+ target 35
264
+ end
265
+
260
266
  class Generic < Base
261
267
  def url
262
268
  "https://docs.chef.io/chef_deprecations_client/"
@@ -0,0 +1,38 @@
1
+ #
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ class Chef
19
+ module DSL
20
+ module Compliance
21
+
22
+ # @see Chef::Compliance::ProfileCollection#include_profile
23
+ def include_profile(*args)
24
+ run_context.profile_collection.include_profile(*args)
25
+ end
26
+
27
+ # @see Chef::Compliance::WaiverCollection#include_waiver
28
+ def include_waiver(*args)
29
+ run_context.waiver_collection.include_waiver(*args)
30
+ end
31
+
32
+ # @see Chef::Compliance::inputCollection#include_input
33
+ def include_input(*args)
34
+ run_context.input_collection.include_input(*args)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ autoload :TOML, "tomlrb"
18
+ require_relative "../json_compat"
19
+ autoload :YAML, "yaml"
20
+
21
+ class Chef
22
+ module DSL
23
+ module ReaderHelpers
24
+
25
+ def parse_file(filename)
26
+ case File.extname(filename)
27
+ when ".toml"
28
+ parse_toml(filename)
29
+ when ".yaml", ".yml"
30
+ parse_yaml(filename)
31
+ when ".json"
32
+ parse_json(filename)
33
+ end
34
+ end
35
+
36
+ def parse_json(filename)
37
+ JSONCompat.parse(IO.read(filename))
38
+ end
39
+
40
+ def parse_toml(filename)
41
+ Tomlrb.load_file(filename)
42
+ end
43
+
44
+ def parse_yaml(filename)
45
+ YAML.load(IO.read(filename))
46
+ end
47
+
48
+ extend self
49
+ end
50
+ end
51
+ end
@@ -18,12 +18,13 @@
18
18
  #
19
19
 
20
20
  require_relative "../exceptions"
21
- require_relative "resources"
21
+ require_relative "compliance"
22
+ require_relative "declare_resource"
22
23
  require_relative "definitions"
23
24
  require_relative "include_recipe"
24
25
  require_relative "reboot_pending"
26
+ require_relative "resources"
25
27
  require_relative "universal"
26
- require_relative "declare_resource"
27
28
  require_relative "../mixin/notifying_block"
28
29
  require_relative "../mixin/lazy_module_include"
29
30
 
@@ -42,6 +43,7 @@ class Chef
42
43
  # - it also pollutes the namespace of nearly every context, watch out.
43
44
  #
44
45
  module Recipe
46
+ include Chef::DSL::Compliance
45
47
  include Chef::DSL::Universal
46
48
  include Chef::DSL::DeclareResource
47
49
  include Chef::Mixin::NotifyingBlock
@@ -0,0 +1,44 @@
1
+ #
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ require_relative "toml"
17
+ require_relative "../json_compat"
18
+ autoload :YAML, "yaml"
19
+
20
+ class Chef
21
+ module DSL
22
+ module RenderHelpers
23
+
24
+ # pretty-print a hash as a JSON string
25
+ def render_json(hash)
26
+ JSON.pretty_generate(hash) + "\n"
27
+ end
28
+
29
+ # pretty-print a hash as a TOML string
30
+ def render_toml(hash)
31
+ Chef::DSL::Toml::Dumper.new(hash).toml_str
32
+ end
33
+
34
+ # pretty-print a hash as a YAML string
35
+ def render_yaml(hash)
36
+ yaml_content = hash.transform_keys(&:to_s).to_yaml
37
+ # above replaces first-level keys with strings, below the rest
38
+ yaml_content.gsub!(" :", " ")
39
+ end
40
+
41
+ extend self
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,62 @@
1
+ #
2
+ # Author:: Marc Paradise (<marc@chef.io>)
3
+ # Copyright:: Copyright (c) Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require_relative "../secret_fetcher"
19
+
20
+ class Chef
21
+ module DSL
22
+ module Secret
23
+
24
+ # Helper method which looks up a secret using the given service and configuration,
25
+ # and returns the retrieved secret value.
26
+ # This DSL providers a wrapper around [Chef::SecretFetcher]
27
+ #
28
+ # Use of the secret helper in the context of a resource block will automatically mark
29
+ # that resource as 'sensitive', preventing resource data from being logged. See [Chef::Resource#sensitive].
30
+ #
31
+ # @option name [Object] The identifier or name for this secret
32
+ # @option version [Object] The secret version. If a service supports versions
33
+ # and no version is provided, the latest version will be fetched.
34
+ # @option service [Symbol] The service identifier for the service that will
35
+ # perform the secret lookup. See
36
+ # [Chef::SecretFetcher::SECRET_FETCHERS]
37
+ # @option config [Hash] The configuration that the named service expects
38
+ #
39
+ # @return result [Object] The response object type is determined by the fetcher but will usually be a string or a hash.
40
+ # See individual fetcher documentation to know what to expect for a given service.
41
+ #
42
+ # @example
43
+ #
44
+ # This example uses the built-in :example secret manager service, which
45
+ # accepts a hash of secrets.
46
+ #
47
+ # value = secret(name: "test1", service: :example, config: { "test1" => "value1" })
48
+ # log "My secret is #{value}"
49
+ #
50
+ # value = secret(name: "test1", service: :aws_secrets_manager, version: "v1", config: { region: "us-west-1" })
51
+ # log "My secret is #{value}"
52
+ def secret(name: nil, version: nil, service: nil, config: {})
53
+ Chef::Log.warn <<~EOM.gsub("\n", " ")
54
+ The secrets Chef Infra language helper is currently in beta. If you have feedback or you would
55
+ like to be part of the future design of this helper e-mail us at secrets_management_beta@progress.com"
56
+ EOM
57
+ sensitive(true) if is_a?(Chef::Resource)
58
+ Chef::SecretFetcher.for_service(service, config, run_context).fetch(name, version)
59
+ end
60
+ end
61
+ end
62
+ end