cookstyle 5.15.7 → 5.16.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/config/cookstyle.yml +172 -4
  3. data/lib/cookstyle.rb +6 -0
  4. data/lib/cookstyle/version.rb +1 -1
  5. data/lib/rubocop/chef/cookbook_helpers.rb +12 -4
  6. data/lib/rubocop/cop/chef/correctness/dnf_package_allow_downgrades.rb +50 -0
  7. data/lib/rubocop/cop/chef/correctness/invalid_notification_timing.rb +53 -0
  8. data/lib/rubocop/cop/chef/correctness/malformed_value_for_platform.rb +67 -0
  9. data/lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb +11 -3
  10. data/lib/rubocop/cop/chef/deprecation/chef_rewind.rb +6 -3
  11. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +4 -0
  12. data/lib/rubocop/cop/chef/deprecation/deprecated_platform_methods.rb +56 -0
  13. data/lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb +11 -3
  14. data/lib/rubocop/cop/chef/deprecation/name_property_and_default.rb +18 -22
  15. data/lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb +64 -0
  16. data/lib/rubocop/cop/chef/modernize/apt_default_recipe.rb +5 -2
  17. data/lib/rubocop/cop/chef/modernize/chef_14_resources.rb +4 -0
  18. data/lib/rubocop/cop/chef/modernize/cron_manage_resource.rb +4 -0
  19. data/lib/rubocop/cop/chef/modernize/depends_zypper_cookbook.rb +4 -0
  20. data/lib/rubocop/cop/chef/modernize/execute_sc_exe.rb +66 -0
  21. data/lib/rubocop/cop/chef/modernize/execute_sleep.rb +77 -0
  22. data/lib/rubocop/cop/chef/modernize/execute_tzutil.rb +3 -0
  23. data/lib/rubocop/cop/chef/modernize/foodcritic_comments.rb +51 -0
  24. data/lib/rubocop/cop/chef/modernize/libarchive_file.rb +4 -0
  25. data/lib/rubocop/cop/chef/modernize/macos_user_defaults.rb +4 -0
  26. data/lib/rubocop/cop/chef/modernize/openssl_rsa_key_resource.rb +4 -0
  27. data/lib/rubocop/cop/chef/modernize/openssl_x509_resource.rb +4 -0
  28. data/lib/rubocop/cop/chef/modernize/powershell_expand_archive.rb +3 -0
  29. data/lib/rubocop/cop/chef/modernize/powershell_guard_interpreter.rb +3 -0
  30. data/lib/rubocop/cop/chef/modernize/powershell_install_package.rb +3 -0
  31. data/lib/rubocop/cop/chef/modernize/powershell_install_windowsfeature.rb +3 -0
  32. data/lib/rubocop/cop/chef/modernize/property_with_name_attribute.rb +13 -15
  33. data/lib/rubocop/cop/chef/modernize/respond_to_metadata.rb +4 -0
  34. data/lib/rubocop/cop/chef/modernize/sc_windows_resource.rb +49 -0
  35. data/lib/rubocop/cop/chef/modernize/seven_zip_archive.rb +4 -0
  36. data/lib/rubocop/cop/chef/modernize/systctl_param_resource.rb +4 -2
  37. data/lib/rubocop/cop/chef/modernize/whyrun_supported_true.rb +4 -0
  38. data/lib/rubocop/cop/chef/modernize/windows_default_recipe.rb +1 -2
  39. data/lib/rubocop/cop/chef/modernize/windows_zipfile.rb +4 -0
  40. data/lib/rubocop/cop/chef/modernize/zipfile_resource.rb +3 -0
  41. data/lib/rubocop/cop/chef/modernize/zypper_repo.rb +4 -0
  42. data/lib/rubocop/cop/chef/redundant/custom_resource_with_allowed_actions.rb +1 -1
  43. data/lib/rubocop/cop/chef/redundant/long_description_metadata.rb +0 -1
  44. data/lib/rubocop/cop/chef/redundant/sensitive_property_in_resource.rb +48 -0
  45. data/lib/rubocop/cop/chef/redundant/unnecessary_desired_state.rb +57 -0
  46. data/lib/rubocop/cop/chef/redundant/unnecessary_name_property.rb +10 -7
  47. data/lib/rubocop/cop/chef/style/immediate_notification_timing.rb +59 -0
  48. data/lib/rubocop/cop/chef/style/true_false_resource_properties.rb +55 -0
  49. data/lib/rubocop/cop/chef/style/unnecessary_platform_case_statement.rb +90 -0
  50. data/lib/rubocop/cop/chef/style/use_platform_helpers.rb +21 -5
  51. data/lib/rubocop/cop/target_chef_version.rb +18 -0
  52. data/lib/rubocop/monkey_patches/commissioner.rb +26 -0
  53. data/lib/rubocop/monkey_patches/config.rb +15 -0
  54. data/lib/rubocop/monkey_patches/cop.rb +10 -0
  55. metadata +20 -2
@@ -0,0 +1,64 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
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
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefDeprecations
22
+ # Use the :run action in the ruby_block resource instead of the deprecated :create action
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # ruby_block 'my special ruby block' do
28
+ # block do
29
+ # puts 'running'
30
+ # end
31
+ # action :create
32
+ # end
33
+ #
34
+ # # good
35
+ # ruby_block 'my special ruby block' do
36
+ # block do
37
+ # puts 'running'
38
+ # end
39
+ # action :run
40
+ # end
41
+ #
42
+ class RubyBlockCreateAction < Cop
43
+ include RuboCop::Chef::CookbookHelpers
44
+
45
+ MSG = 'Use the :run action in the ruby_block resource instead of the deprecated :create action'.freeze
46
+
47
+ def on_block(node)
48
+ match_property_in_resource?(:ruby_block, 'action', node) do |ruby_action|
49
+ ruby_action.arguments.each do |action|
50
+ add_offense(action, location: :expression, message: MSG, severity: :refactor) if action.source == ':create'
51
+ end
52
+ end
53
+ end
54
+
55
+ def autocorrect(node)
56
+ lambda do |corrector|
57
+ corrector.replace(node.loc.expression, ':run')
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -18,8 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefModernize
21
- # Don't include the apt default recipe to update apt's package cache when you can
22
- # use the apt_update resource built into Chef Infra Client 12.7 and later.
21
+ # For many users the apt::default cookbook is used only to update apt's package cache. Chef Infra Client 12.7 and later include an apt_update resource which should be used to perform this instead. Keep in mind that some users will want to stick with the apt::default recipe as it also installs packages necessary for using https repositories on Debian systems and manages some configuration files.
23
22
  #
24
23
  # @example
25
24
  #
@@ -31,6 +30,10 @@ module RuboCop
31
30
  # apt_update
32
31
  #
33
32
  class IncludingAptDefaultRecipe < Cop
33
+ extend TargetChefVersion
34
+
35
+ minimum_target_chef_version '12.7'
36
+
34
37
  MSG = 'Do not include the Apt default recipe to update package cache. Instead use the apt_update resource, which is built into Chef Infra Client 12.7 and later.'.freeze
35
38
 
36
39
  def_node_matcher :apt_recipe_usage?, <<-PATTERN
@@ -32,6 +32,10 @@ module RuboCop
32
32
  # depends 'sysctl'
33
33
  #
34
34
  class UnnecessaryDependsChef14 < Cop
35
+ extend TargetChefVersion
36
+
37
+ minimum_target_chef_version '14.0'
38
+
35
39
  MSG = "Don't depend on cookbooks made obsolete by Chef Infra Client 14+. These community cookbooks contain resources that are now included in Chef Infra Client itself.".freeze
36
40
 
37
41
  def_node_matcher :legacy_depends?, <<-PATTERN
@@ -29,6 +29,10 @@ module RuboCop
29
29
  # cron_access 'mike'
30
30
  #
31
31
  class CronManageResource < Cop
32
+ extend TargetChefVersion
33
+
34
+ minimum_target_chef_version '14.4'
35
+
32
36
  MSG = 'The cron_manage resource was renamed to cron_access in the 6.1 release of the cron cookbook and later shipped in Chef Infra Client 14.4. The new resource name should be used.'.freeze
33
37
 
34
38
  def on_send(node)
@@ -26,6 +26,10 @@ module RuboCop
26
26
  # depends 'zypper'
27
27
  #
28
28
  class DependsOnZypperCookbook < Cop
29
+ extend TargetChefVersion
30
+
31
+ minimum_target_chef_version '13.3'
32
+
29
33
  MSG = "Don't include the zypper cookbook as the zypper_repository resource is built into Chef Infra Client 13.3+".freeze
30
34
 
31
35
  def_node_matcher :zypper_depends?, <<-PATTERN
@@ -0,0 +1,66 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software, Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
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
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ module ChefModernize
21
+ # Chef Infra Client 14.0 and later includes :create, :delete, and :configure actions with the full idempotency of the windows_service resource. See the windows_service documentation at https://docs.chef.io/resource_windows_service.html for additional details on creating services with the windows_service resource.
22
+ #
23
+ # # bad
24
+ # execute "Delete chef-client service" do
25
+ # command "sc.exe delete chef-client"
26
+ # action :run
27
+ # end
28
+ #
29
+ # # good
30
+ # windows_service 'chef-client' do
31
+ # action :delete
32
+ # end
33
+ #
34
+ class ExecuteScExe < Cop
35
+ include RuboCop::Chef::CookbookHelpers
36
+ extend TargetChefVersion
37
+
38
+ minimum_target_chef_version '14.0'
39
+
40
+ MSG = 'Chef Infra Client 14.0 and later includes :create, :delete, and :configure actions with the full idempotency of the windows_service resource. See the windows_service documentation at https://docs.chef.io/resource_windows_service.html for additional details on creating services with the windows_service resource'.freeze
41
+
42
+ # non block execute resources
43
+ def on_send(node)
44
+ return unless node.method_name == :execute
45
+
46
+ # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
47
+ # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
48
+ if node&.arguments.first&.source&.match?(/^("|')sc.exe/)
49
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
50
+ end
51
+ end
52
+
53
+ # block execute resources
54
+ def on_block(node)
55
+ match_property_in_resource?(:execute, 'command', node) do |code_property|
56
+ property_data = method_arg_ast_to_string(code_property)
57
+ if property_data && property_data.match?(/^sc.exe/i)
58
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software, Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
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
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ module ChefModernize
21
+ # Chef Infra Client 15.5 and later include a chef_sleep resource that should be used to sleep between executing resources if necessary instead of using the bash or execute resources to run the sleep command.
22
+ #
23
+ # # bad
24
+ # execute "sleep 60" do
25
+ # command "sleep 60"
26
+ # action :run
27
+ # end
28
+ #
29
+ # bash 'sleep' do
30
+ # user 'root'
31
+ # cwd '/tmp'
32
+ # code 'sleep 60'
33
+ # end
34
+ #
35
+ # # good
36
+ # chef_sleep '60'
37
+ #
38
+ class ExecuteSleep < Cop
39
+ include RuboCop::Chef::CookbookHelpers
40
+ extend TargetChefVersion
41
+
42
+ minimum_target_chef_version '15.5'
43
+
44
+ MSG = 'Chef Infra Client 15.5 and later include a chef_sleep resource that should be used to sleep between executing resources if necessary instead of using the bash or execute resources to run the sleep command.'.freeze
45
+
46
+ # non block execute resources
47
+ def on_send(node)
48
+ return unless node.method_name == :execute
49
+
50
+ # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
51
+ # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
52
+ if node&.arguments.first&.source&.match?(/^("|')sleep/)
53
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
54
+ end
55
+ end
56
+
57
+ # block execute resources
58
+ def on_block(node)
59
+ match_property_in_resource?(:execute, 'command', node) do |code_property|
60
+ property_data = method_arg_ast_to_string(code_property)
61
+ if property_data && property_data.match?(/^sleep/i)
62
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
63
+ end
64
+ end
65
+
66
+ match_property_in_resource?(:bash, 'code', node) do |code_property|
67
+ property_data = method_arg_ast_to_string(code_property)
68
+ if property_data && property_data.match?(/^sleep/i)
69
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -38,6 +38,9 @@ module RuboCop
38
38
  #
39
39
  class ExecuteTzUtil < Cop
40
40
  include RuboCop::Chef::CookbookHelpers
41
+ extend TargetChefVersion
42
+
43
+ minimum_target_chef_version '14.6'
41
44
 
42
45
  MSG = 'Use the timezone resource included in Chef Infra Client 14.6+ instead of shelling out to tzutil'.freeze
43
46
 
@@ -0,0 +1,51 @@
1
+ #
2
+ # Copyright:: 2016-2019, Chef Software, Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
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
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefModernize
22
+ # Remove legacy code comments that disable Foodcritic rules. These comments are no longer necessary if you've migrated from Foodcritic to Cookstyle for cookbook linting.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # # ~FC013
28
+ #
29
+ class FoodcriticComments < Cop
30
+ MSG = 'Remove legacy code comments that disable Foodcritic rules'.freeze
31
+
32
+ def investigate(processed_source)
33
+ return unless processed_source.ast
34
+
35
+ processed_source.comments.each do |comment|
36
+ if comment.text.match?(/#\s*~FC\d{3}.*/)
37
+ add_offense(comment, location: :expression, message: MSG, severity: :refactor)
38
+ end
39
+ end
40
+ end
41
+
42
+ def autocorrect(node)
43
+ lambda do |corrector|
44
+ corrector.remove(node.loc.expression)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -29,6 +29,10 @@ module RuboCop
29
29
  # end
30
30
  #
31
31
  class LibarchiveFileResource < Cop
32
+ extend TargetChefVersion
33
+
34
+ minimum_target_chef_version '15.0'
35
+
32
36
  MSG = 'Use the archive_file resource built into Chef Infra Client 15+ instead of the libarchive file resource'.freeze
33
37
 
34
38
  def on_send(node)
@@ -36,6 +36,10 @@ module RuboCop
36
36
  # end
37
37
  #
38
38
  class MacOsXUserdefaults < Cop
39
+ extend TargetChefVersion
40
+
41
+ minimum_target_chef_version '14.0'
42
+
39
43
  MSG = 'The mac_os_x_userdefaults resource was renamed to macos_userdefaults when it was added to Chef Infra Client 14.0. The new resource name should be used.'.freeze
40
44
 
41
45
  def on_send(node)
@@ -32,6 +32,10 @@ module RuboCop
32
32
  # end
33
33
  #
34
34
  class OpensslRsaKeyResource < Cop
35
+ extend TargetChefVersion
36
+
37
+ minimum_target_chef_version '14.0'
38
+
35
39
  MSG = 'The openssl_rsa_key resource was renamed to openssl_rsa_private_key in Chef Infra Client 14.0. The new resource name should be used.'.freeze
36
40
 
37
41
  def on_send(node)
@@ -38,6 +38,10 @@ module RuboCop
38
38
  # end
39
39
  #
40
40
  class OpensslX509Resource < Cop
41
+ extend TargetChefVersion
42
+
43
+ minimum_target_chef_version '14.4'
44
+
41
45
  MSG = 'The openssl_x509 resource was renamed to openssl_x509_certificate in Chef Infra Client 14.4. The new resource name should be used.'.freeze
42
46
 
43
47
  def on_send(node)
@@ -30,6 +30,9 @@ module RuboCop
30
30
  #
31
31
  class PowershellScriptExpandArchive < Cop
32
32
  include RuboCop::Chef::CookbookHelpers
33
+ extend TargetChefVersion
34
+
35
+ minimum_target_chef_version '15.0'
33
36
 
34
37
  MSG = 'Use the archive_file resource built into Chef Infra Client 15+ instead of using Expand-Archive in a powershell_script resource'.freeze
35
38
 
@@ -36,6 +36,9 @@ module RuboCop
36
36
  class PowerShellGuardInterpreter < Cop
37
37
  include RuboCop::Chef::CookbookHelpers
38
38
  include RangeHelp
39
+ extend TargetChefVersion
40
+
41
+ minimum_target_chef_version '13.0'
39
42
 
40
43
  MSG = 'PowerShell is already set as the default guard interpreter for powershell_script resources in Chef Infra Client 13 and later and does not need to be specified.'.freeze
41
44
 
@@ -33,6 +33,9 @@ module RuboCop
33
33
  #
34
34
  class PowershellInstallPackage < Cop
35
35
  include RuboCop::Chef::CookbookHelpers
36
+ extend TargetChefVersion
37
+
38
+ minimum_target_chef_version '12.16'
36
39
 
37
40
  MSG = 'Use the powershell_package resource built into Chef Infra Client 12.16+ instead of using Install-Package in a powershell_script resource'.freeze
38
41
 
@@ -36,6 +36,9 @@ module RuboCop
36
36
  #
37
37
  class PowershellInstallWindowsFeature < Cop
38
38
  include RuboCop::Chef::CookbookHelpers
39
+ extend TargetChefVersion
40
+
41
+ minimum_target_chef_version '13.0'
39
42
 
40
43
  MSG = 'Use the windows_feature resource built into Chef Infra Client 13+ instead of using Install-WindowsFeature or Add-WindowsFeature in a powershell_script resource'.freeze
41
44
 
@@ -29,28 +29,26 @@ module RuboCop
29
29
  # property :bob, String, name_property: true
30
30
  #
31
31
  class PropertyWithNameAttribute < Cop
32
- MSG = 'Resource property sets name_attribute not name_property'.freeze
32
+ MSG = 'Resource property sets name_attribute instead of name_property'.freeze
33
+
34
+ # match on a property that has any name and any type and a hash that
35
+ # contains name_attribute true. The hash pairs are wrapped in
36
+ # <> which means the order doesn't matter in the hash.
37
+ def_node_matcher :property_with_name_attribute?, <<-PATTERN
38
+ (send nil? :property (sym _) ... (hash <$(pair (sym :name_attribute) (true)) ...>))
39
+ PATTERN
33
40
 
34
41
  def on_send(node)
35
- add_offense(node, location: :expression, message: MSG, severity: :refactor) if attribute_method_mix?(node)
42
+ property_with_name_attribute?(node) do
43
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
44
+ end
36
45
  end
37
46
 
38
47
  def autocorrect(node)
39
48
  lambda do |corrector|
40
- corrector.replace(node.loc.expression, node.source.gsub('name_attribute', 'name_property'))
41
- end
42
- end
43
-
44
- private
45
-
46
- def attribute_method_mix?(node)
47
- if node.method_name == :property
48
- node.arguments.each do |arg|
49
- if arg.type == :hash
50
- return true if arg.source.match?(/name_attribute:/)
51
- end
49
+ property_with_name_attribute?(node) do |name_attribute|
50
+ corrector.replace(name_attribute.loc.expression, 'name_property: true')
52
51
  end
53
- false # no name_attribute found
54
52
  end
55
53
  end
56
54
  end