cookstyle 6.17.5 → 6.19.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -1
  3. data/bin/cookstyle +0 -1
  4. data/config/cookstyle.yml +85 -15
  5. data/config/disable_all.yml +8 -0
  6. data/config/upstream.yml +50 -3
  7. data/cookstyle.gemspec +1 -2
  8. data/lib/cookstyle.rb +5 -5
  9. data/lib/cookstyle/version.rb +2 -2
  10. data/lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb +1 -1
  11. data/lib/rubocop/cop/chef/correctness/chef_application_fatal.rb +2 -1
  12. data/lib/rubocop/cop/chef/correctness/conditional_ruby_shellout.rb +1 -1
  13. data/lib/rubocop/cop/chef/correctness/incorrect_library_injection.rb +24 -14
  14. data/lib/rubocop/cop/chef/correctness/invalid_platform_family_values_in_case.rb +1 -1
  15. data/lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb +2 -2
  16. data/lib/rubocop/cop/chef/correctness/invalid_platform_values_in_case.rb +2 -1
  17. data/lib/rubocop/cop/chef/correctness/invalid_version_metadata.rb +1 -0
  18. data/lib/rubocop/cop/chef/correctness/lazy_eval_node_attribute_defaults.rb +1 -1
  19. data/lib/rubocop/cop/chef/correctness/lazy_in_resource_guard.rb +67 -0
  20. data/lib/rubocop/cop/chef/correctness/macos_userdefaults_invalid_type.rb +2 -2
  21. data/lib/rubocop/cop/chef/correctness/node_normal.rb +1 -0
  22. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +1 -0
  23. data/lib/rubocop/cop/chef/correctness/node_save.rb +1 -0
  24. data/lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb +1 -1
  25. data/lib/rubocop/cop/chef/correctness/property_without_type.rb +59 -0
  26. data/lib/rubocop/cop/chef/correctness/resource_with_none_action.rb +1 -1
  27. data/lib/rubocop/cop/chef/correctness/scoped_file_exist.rb +1 -1
  28. data/lib/rubocop/cop/chef/correctness/supports_must_be_float.rb +2 -2
  29. data/lib/rubocop/cop/chef/deprecation/chef_handler_supports.rb +1 -1
  30. data/lib/rubocop/cop/chef/deprecation/chef_rewind.rb +2 -2
  31. data/lib/rubocop/cop/chef/deprecation/chef_shellout.rb +2 -2
  32. data/lib/rubocop/cop/chef/deprecation/chef_windows_platform_helper.rb +2 -1
  33. data/lib/rubocop/cop/chef/deprecation/chefdk_generators.rb +1 -1
  34. data/lib/rubocop/cop/chef/deprecation/chefspec_coverage_report.rb +1 -1
  35. data/lib/rubocop/cop/chef/deprecation/chefspec_legacy_runner.rb +1 -1
  36. data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +1 -3
  37. data/lib/rubocop/cop/chef/deprecation/deprecated_platform_methods.rb +1 -0
  38. data/lib/rubocop/cop/chef/deprecation/deprecated_yum_repository_properties.rb +1 -1
  39. data/lib/rubocop/cop/chef/deprecation/epic_fail.rb +1 -1
  40. data/lib/rubocop/cop/chef/deprecation/execute_path_property.rb +1 -1
  41. data/lib/rubocop/cop/chef/deprecation/execute_relative_creates_without_cwd.rb +1 -1
  42. data/lib/rubocop/cop/chef/deprecation/inherits_compat_resource.rb +1 -1
  43. data/lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb +2 -2
  44. data/lib/rubocop/cop/chef/deprecation/node_deep_fetch.rb +2 -0
  45. data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +22 -27
  46. data/lib/rubocop/cop/chef/deprecation/node_set.rb +1 -0
  47. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +1 -0
  48. data/lib/rubocop/cop/chef/deprecation/node_set_without_level.rb +1 -0
  49. data/lib/rubocop/cop/chef/deprecation/partial_search_class_usage.rb +1 -0
  50. data/lib/rubocop/cop/chef/deprecation/powershell_cookbook_helpers.rb +2 -1
  51. data/lib/rubocop/cop/chef/deprecation/resource_overrides_provides_method.rb +2 -2
  52. data/lib/rubocop/cop/chef/deprecation/ruby_27_keyword_argument_warnings.rb +2 -2
  53. data/lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb +2 -2
  54. data/lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb +1 -1
  55. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +1 -1
  56. data/lib/rubocop/cop/chef/deprecation/windows_package_installer_type_string.rb +1 -1
  57. data/lib/rubocop/cop/chef/deprecation/windows_task_change_action.rb +2 -2
  58. data/lib/rubocop/cop/chef/deprecation/windows_version_helpers.rb +5 -4
  59. data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +1 -1
  60. data/lib/rubocop/cop/chef/effortless/chef_vault_used.rb +89 -0
  61. data/lib/rubocop/cop/chef/effortless/depends_chef_vault.rb +49 -0
  62. data/lib/rubocop/cop/chef/modernize/action_method_in_resource.rb +2 -2
  63. data/lib/rubocop/cop/chef/modernize/berksfile_source.rb +2 -2
  64. data/lib/rubocop/cop/chef/modernize/build_essential.rb +1 -1
  65. data/lib/rubocop/cop/chef/modernize/compile_time_resources.rb +1 -0
  66. data/lib/rubocop/cop/chef/modernize/conditional_using_test.rb +2 -2
  67. data/lib/rubocop/cop/chef/modernize/cron_manage_resource.rb +1 -1
  68. data/lib/rubocop/cop/chef/modernize/databag_helpers.rb +2 -1
  69. data/lib/rubocop/cop/chef/modernize/foodcritic_comments.rb +1 -1
  70. data/lib/rubocop/cop/chef/modernize/if_provides_default_action.rb +1 -1
  71. data/lib/rubocop/cop/chef/modernize/libarchive_file.rb +2 -2
  72. data/lib/rubocop/cop/chef/modernize/macos_user_defaults.rb +1 -1
  73. data/lib/rubocop/cop/chef/modernize/node_init_package.rb +5 -4
  74. data/lib/rubocop/cop/chef/modernize/node_roles_include.rb +2 -1
  75. data/lib/rubocop/cop/chef/modernize/openssl_rsa_key_resource.rb +1 -1
  76. data/lib/rubocop/cop/chef/modernize/openssl_x509_resource.rb +1 -1
  77. data/lib/rubocop/cop/chef/modernize/osx_config_profile_resource.rb +1 -1
  78. data/lib/rubocop/cop/chef/modernize/property_with_name_attribute.rb +1 -1
  79. data/lib/rubocop/cop/chef/modernize/respond_to_compile_time.rb +7 -4
  80. data/lib/rubocop/cop/chef/modernize/respond_to_metadata.rb +6 -6
  81. data/lib/rubocop/cop/chef/modernize/respond_to_provides.rb +10 -10
  82. data/lib/rubocop/cop/chef/modernize/respond_to_resource_name.rb +2 -2
  83. data/lib/rubocop/cop/chef/modernize/shell_out_helper.rb +2 -1
  84. data/lib/rubocop/cop/chef/modernize/simplify_apt_ppa_setup.rb +1 -1
  85. data/lib/rubocop/cop/chef/modernize/sysctl_param_resource.rb +1 -1
  86. data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +1 -1
  87. data/lib/rubocop/cop/chef/modernize/use_require_relative.rb +1 -1
  88. data/lib/rubocop/cop/chef/modernize/zypper_repo.rb +1 -1
  89. data/lib/rubocop/cop/chef/redundant/double_compile_time.rb +2 -1
  90. data/lib/rubocop/cop/chef/redundant/multiple_platform_checks.rb +1 -1
  91. data/lib/rubocop/cop/chef/redundant/name_property_and_required.rb +1 -0
  92. data/lib/rubocop/cop/chef/redundant/ohai_attribute_to_string.rb +2 -1
  93. data/lib/rubocop/cop/chef/redundant/property_splat_regex.rb +1 -0
  94. data/lib/rubocop/cop/chef/redundant/use_create_if_missing.rb +1 -1
  95. data/lib/rubocop/cop/chef/sharing/include_property_descriptions.rb +1 -1
  96. data/lib/rubocop/cop/chef/sharing/insecure_cookbook_url.rb +1 -1
  97. data/lib/rubocop/cop/chef/sharing/invalid_license_string.rb +1 -1
  98. data/lib/rubocop/cop/chef/style/attribute_keys.rb +2 -1
  99. data/lib/rubocop/cop/chef/style/chef_whaaat.rb +1 -1
  100. data/lib/rubocop/cop/chef/style/comment_sentence_spacing.rb +2 -2
  101. data/lib/rubocop/cop/chef/style/comments_copyright_format.rb +2 -2
  102. data/lib/rubocop/cop/chef/style/comments_default_copyright.rb +1 -1
  103. data/lib/rubocop/cop/chef/style/comments_format.rb +6 -13
  104. data/lib/rubocop/cop/chef/style/file_mode.rb +2 -2
  105. data/lib/rubocop/cop/chef/style/immediate_notification_timing.rb +2 -2
  106. data/lib/rubocop/cop/chef/style/include_recipe_with_parentheses.rb +2 -2
  107. data/lib/rubocop/cop/chef/style/negating_only_if.rb +2 -2
  108. data/lib/rubocop/cop/chef/style/overly_complex_supports_depends_metadata.rb +2 -2
  109. data/lib/rubocop/cop/chef/style/simplify_platform_major_version_check.rb +2 -1
  110. data/lib/rubocop/cop/chef/style/true_false_resource_properties.rb +2 -2
  111. data/lib/rubocop/cop/chef/style/unnecessary_os_check.rb +5 -4
  112. data/lib/rubocop/cop/chef/style/unnecessary_platform_case_statement.rb +2 -2
  113. data/lib/rubocop/cop/chef/style/use_platform_helpers.rb +7 -6
  114. metadata +8 -4
@@ -37,6 +37,8 @@ module RuboCop
37
37
  class NodeDeepFetch < Base
38
38
  extend RuboCop::Cop::AutoCorrector
39
39
 
40
+ RESTRICT_ON_SEND = [:deep_fetch, :deep_fetch!].freeze
41
+
40
42
  def_node_matcher :node_deep_fetch?, <<-PATTERN
41
43
  (send (send _ :node) ${:deep_fetch :deep_fetch!} _)
42
44
  PATTERN
@@ -41,43 +41,38 @@ module RuboCop
41
41
  extend AutoCorrector
42
42
 
43
43
  MSG = 'Use node attributes to access Ohai data instead of node methods, which were deprecated in Chef Infra Client 13.'
44
+ RESTRICT_ON_SEND = %i(
45
+ current_user
46
+ domain
47
+ fqdn
48
+ hostname
49
+ ip6address
50
+ ipaddress
51
+ macaddress
52
+ machinename
53
+ ohai_time
54
+ os
55
+ os_version
56
+ platform
57
+ platform_build
58
+ platform_family
59
+ platform_version
60
+ root_group
61
+ shard_seed
62
+ uptime
63
+ uptime_seconds).freeze
44
64
 
45
65
  def_node_matcher :node_ohai_methods?, <<-PATTERN
46
- (send (send nil? :node) #non_nested_ohai_attribute?)
66
+ (send (send nil? :node) _)
47
67
  PATTERN
48
68
 
49
69
  def on_send(node)
50
70
  node_ohai_methods?(node) do
51
71
  add_offense(node.loc.selector, message: MSG, severity: :warning) do |corrector|
52
- corrector.replace(node.loc.expression, "node['#{node.method_name}']")
72
+ corrector.replace(node, "node['#{node.method_name}']")
53
73
  end
54
74
  end
55
75
  end
56
-
57
- private
58
-
59
- def non_nested_ohai_attribute?(attribute)
60
- %i(
61
- current_user
62
- domain
63
- fqdn
64
- hostname
65
- ip6address
66
- ipaddress
67
- macaddress
68
- machinename
69
- ohai_time
70
- os
71
- os_version
72
- platform
73
- platform_build
74
- platform_family
75
- platform_version
76
- root_group
77
- shard_seed
78
- uptime
79
- uptime_seconds).include?(attribute)
80
- end
81
76
  end
82
77
  end
83
78
  end
@@ -34,6 +34,7 @@ module RuboCop
34
34
  extend AutoCorrector
35
35
 
36
36
  MSG = 'Do not use node.set. Replace with node.normal to keep identical behavior.'
37
+ RESTRICT_ON_SEND = [:set].freeze
37
38
 
38
39
  def_node_matcher :node_set?, <<-PATTERN
39
40
  (send (send _ :node) $:set)
@@ -34,6 +34,7 @@ module RuboCop
34
34
  extend AutoCorrector
35
35
 
36
36
  MSG = 'Do not use node.set_unless. Replace with node.normal_unless to keep identical behavior.'
37
+ RESTRICT_ON_SEND = [:set_unless].freeze
37
38
 
38
39
  def_node_matcher :node_set_unless?, <<-PATTERN
39
40
  (send (send _ :node) $:set_unless)
@@ -36,6 +36,7 @@ module RuboCop
36
36
  #
37
37
  class NodeSetWithoutLevel < Base
38
38
  MSG = 'When setting a node attribute in Chef Infra Client 11 and later you must specify the precedence level.'
39
+ RESTRICT_ON_SEND = [:[]=, :<<].freeze
39
40
 
40
41
  def on_op_asgn(node)
41
42
  # make sure it was a += or -=
@@ -49,6 +49,7 @@ module RuboCop
49
49
  #
50
50
  class PartialSearchClassUsage < Base
51
51
  MSG = 'Legacy Chef::PartialSearch class usage should be updated to use the search helper instead with the filter_result key.'
52
+ RESTRICT_ON_SEND = [:new].freeze
52
53
 
53
54
  def_node_matcher :partial_search_class?, <<-PATTERN
54
55
  (send (const (const ... :Chef) :PartialSearch) :new)
@@ -36,6 +36,7 @@ module RuboCop
36
36
  extend AutoCorrector
37
37
 
38
38
  MSG = "Use node['powershell']['version'] or the new powershell_version helper available in Chef Infra Client 15.8+ instead of the deprecated PowerShell cookbook helpers."
39
+ RESTRICT_ON_SEND = [:powershell_version?].freeze
39
40
 
40
41
  def_node_matcher :ps_cb_helper?, <<-PATTERN
41
42
  (send
@@ -47,7 +48,7 @@ module RuboCop
47
48
  def on_send(node)
48
49
  ps_cb_helper?(node) do |ver|
49
50
  add_offense(node, message: MSG, severity: :warning) do |corrector|
50
- corrector.replace(node.loc.expression, "node['powershell']['version'].to_f == #{ver.source}")
51
+ corrector.replace(node, "node['powershell']['version'].to_f == #{ver.source}")
51
52
  end
52
53
  end
53
54
  end
@@ -34,12 +34,12 @@ module RuboCop
34
34
  class ResourceOverridesProvidesMethod < Base
35
35
  MSG = "Don't override the provides? method in a resource provider. Use provides :SOME_PROVIDER_NAME instead. This will cause failures in Chef Infra Client 13 and later."
36
36
 
37
- def_node_search :provides, '(send nil? :provides ...)'
37
+ def_node_search :calls_provides?, '(send nil? :provides ...)'
38
38
 
39
39
  def on_def(node)
40
40
  return unless node.method_name == :provides?
41
41
 
42
- add_offense(node, message: MSG, severity: :warning) if provides(processed_source.ast).count == 0
42
+ add_offense(node, message: MSG, severity: :warning) unless calls_provides?(processed_source.ast)
43
43
  end
44
44
  end
45
45
  end
@@ -45,8 +45,8 @@ module RuboCop
45
45
  def on_send(node)
46
46
  positional_shellout?(node) do |h|
47
47
  next unless h.braces?
48
- add_offense(h.loc.expression, message: MSG, severity: :refactor) do |corrector|
49
- corrector.replace(h.loc.expression, h.loc.expression.source[1..-2])
48
+ add_offense(h, message: MSG, severity: :refactor) do |corrector|
49
+ corrector.replace(h, h.source[1..-2])
50
50
  end
51
51
  end
52
52
  end
@@ -50,8 +50,8 @@ module RuboCop
50
50
  match_property_in_resource?(:ruby_block, 'action', node) do |ruby_action|
51
51
  ruby_action.arguments.each do |action|
52
52
  next unless action.source == ':create'
53
- add_offense(action.loc.expression, message: MSG, severity: :warning) do |corrector|
54
- corrector.replace(action.loc.expression, ':run')
53
+ add_offense(action, message: MSG, severity: :warning) do |corrector|
54
+ corrector.replace(action, ':run')
55
55
  end
56
56
  end
57
57
  end
@@ -50,7 +50,7 @@ module RuboCop
50
50
  def on_send(node)
51
51
  search_method?(node) do
52
52
  add_offense(node, message: MSG, severity: :warning) do |corrector|
53
- corrector.replace(node.loc.expression, corrected_string(node))
53
+ corrector.replace(node, corrected_string(node))
54
54
  end if positional_arguments?(node)
55
55
  end
56
56
  end
@@ -60,7 +60,7 @@ module RuboCop
60
60
  new_text << "#{key_value} #{v.source}"
61
61
  end
62
62
 
63
- corrector.replace(property.loc.expression, new_text.join("\n "))
63
+ corrector.replace(property, new_text.join("\n "))
64
64
  end
65
65
  end
66
66
  end
@@ -49,7 +49,7 @@ module RuboCop
49
49
  return unless offense.arguments.count == 1 # we can only analyze simple string args
50
50
  return unless offense.arguments.first.str_type? # anything else is fine
51
51
 
52
- add_offense(offense.loc.expression, message: MSG, severity: :warning) do |corrector|
52
+ add_offense(offense, message: MSG, severity: :warning) do |corrector|
53
53
  corrector.replace(offense.arguments.first.source_range, ":#{offense.arguments.first.value}")
54
54
  end
55
55
  end
@@ -67,11 +67,11 @@ module RuboCop
67
67
  if ast_obj.respond_to?(:value) && ast_obj.value == :change
68
68
  add_offense(ast_obj, message: MSG, severity: :warning) do |corrector|
69
69
  if ast_obj.parent.send_type? # :change was the only action
70
- corrector.replace(ast_obj.loc.expression, ':create')
70
+ corrector.replace(ast_obj, ':create')
71
71
  # chances are it's [:create, :change] since that's all that makes sense, but double check that theory
72
72
  elsif ast_obj.parent.child_nodes.count == 2 &&
73
73
  ast_obj.parent.child_nodes.map(&:value).sort == [:change, :create]
74
- corrector.replace(ast_obj.parent.loc.expression, ':create')
74
+ corrector.replace(ast_obj.parent, ':create')
75
75
  end
76
76
  end
77
77
  end
@@ -42,6 +42,7 @@ module RuboCop
42
42
  minimum_target_chef_version '14.0'
43
43
 
44
44
  MSG = "Use node['platform_version'] and node['kernel'] data introduced in Chef Infra Client 14 instead of the deprecated Windows::VersionHelper helpers from the Windows cookbook."
45
+ RESTRICT_ON_SEND = [:nt_version, :server_version?, :core_version?, :workstation_version?].freeze
45
46
 
46
47
  def_node_matcher :windows_helper?, <<-PATTERN
47
48
  (send ( const ( const {nil? cbase} :Windows ) :VersionHelper ) $_ )
@@ -52,13 +53,13 @@ module RuboCop
52
53
  add_offense(node, message: MSG, severity: :refactor) do |corrector|
53
54
  case method
54
55
  when :nt_version
55
- corrector.replace(node.loc.expression, 'node[\'platform_version\'].to_f')
56
+ corrector.replace(node, 'node[\'platform_version\'].to_f')
56
57
  when :server_version?
57
- corrector.replace(node.loc.expression, 'node[\'kernel\'][\'product_type\'] == \'Server\'')
58
+ corrector.replace(node, 'node[\'kernel\'][\'product_type\'] == \'Server\'')
58
59
  when :core_version?
59
- corrector.replace(node.loc.expression, 'node[\'kernel\'][\'server_core\']')
60
+ corrector.replace(node, 'node[\'kernel\'][\'server_core\']')
60
61
  when :workstation_version?
61
- corrector.replace(node.loc.expression, 'node[\'kernel\'][\'product_type\'] == \'Workstation\'')
62
+ corrector.replace(node, 'node[\'kernel\'][\'product_type\'] == \'Workstation\'')
62
63
  end
63
64
  end
64
65
  end
@@ -40,7 +40,7 @@ module RuboCop
40
40
  xml_ruby_recipe?(node) do
41
41
  node = node.parent if node.parent&.conditional? && node.parent&.single_line_condition? # make sure we catch any inline conditionals
42
42
  add_offense(node, message: MSG, severity: :warning) do |corrector|
43
- corrector.remove(node.loc.expression)
43
+ corrector.remove(node)
44
44
  end
45
45
  end
46
46
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: 2020, Chef Software Inc.
4
+ # Author:: Scott Vidmar (<svidmar@chef.io>)
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
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefEffortless
23
+ # Chef Vault is not supported in the Effortless pattern, so usage of Chef Vault must be shifted to another secrets management solution before leveraging the Effortless pattern.
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # require 'chef-vault'
29
+ #
30
+ # # bad
31
+ # ChefVault::Item
32
+ #
33
+ # # bad
34
+ # include_recipe 'chef-vault'
35
+ #
36
+ # # bad
37
+ # chef_gem 'chef-vault'
38
+ #
39
+ # # bad
40
+ # chef_vault_item_for_environment(arg, arg1)
41
+ #
42
+ # # bad
43
+ # chef_vault_item(arg, arg1)
44
+ #
45
+
46
+ class ChefVaultUsed < Base
47
+ MSG = 'Chef Vault usage is not supported in the Effortless pattern'
48
+ RESTRICT_ON_SEND = [:chef_vault_item,
49
+ :chef_vault_item_for_environment,
50
+ :include_recipe,
51
+ :require,
52
+ :chef_gem].freeze
53
+
54
+ def_node_matcher :require?, <<-PATTERN
55
+ (send nil? { :require :include_recipe :chef_gem }
56
+ (str "chef-vault"))
57
+ PATTERN
58
+
59
+ def_node_matcher :vault_const?, <<-PATTERN
60
+ (const
61
+ (const nil? :ChefVault)
62
+ :Item)
63
+ PATTERN
64
+
65
+ def_node_matcher :chef_vault_item_for_environment?, <<-PATTERN
66
+ (send nil? :chef_vault_item_for_environment _ _)
67
+ PATTERN
68
+
69
+ def_node_matcher :chef_vault_item?, <<-PATTERN
70
+ (send nil? :chef_vault_item _ _)
71
+ PATTERN
72
+
73
+ def on_send(node)
74
+ return unless require?(node) ||
75
+ chef_vault_item_for_environment?(node) ||
76
+ chef_vault_item?(node)
77
+ add_offense(node.loc.expression, message: MSG, severity: :refactor)
78
+ end
79
+
80
+ def on_const(node)
81
+ vault_const?(node) do
82
+ add_offense(node.loc.expression, message: MSG, severity: :refactor)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: 2020, Chef Software Inc.
4
+ # Author:: Scott Vidmar (<svidmar@chef.io>)
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
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefEffortless
23
+ # Chef Vault is not supported in the Effortless pattern, so usage of Chef Vault must be shifted to another secrets management solution before leveraging the Effortless pattern.
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # depends 'chef-vault'
29
+ #
30
+
31
+ class DependsChefVault < Base
32
+ MSG = 'Chef Vault usage is not supported in the Effortless pattern'
33
+ RESTRICT_ON_SEND = [:depends].freeze
34
+
35
+ def_node_matcher :depends?, <<-PATTERN
36
+ (send nil? :depends
37
+ (str "chef-vault"))
38
+ PATTERN
39
+
40
+ def on_send(node)
41
+ depends?(node) do
42
+ add_offense(node.loc.expression, message: MSG, severity: :refactor)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -45,9 +45,9 @@ module RuboCop
45
45
  return if node.arguments? # if they passed in arguments they may actually need this
46
46
  return if node.parent && includes_poise?(node.parent)
47
47
 
48
- add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
48
+ add_offense(node, message: MSG, severity: :refactor) do |corrector|
49
49
  # @todo when we drop ruby 2.4 support we can convert this to use delete_suffix
50
- corrector.replace(node.loc.expression, node.source.gsub("def #{node.method_name}", "action :#{node.method_name.to_s.gsub(/^action_/, '')} do"))
50
+ corrector.replace(node, node.source.gsub("def #{node.method_name}", "action :#{node.method_name.to_s.gsub(/^action_/, '')} do"))
51
51
  end
52
52
  end
53
53
  end
@@ -55,13 +55,13 @@ module RuboCop
55
55
  def on_send(node)
56
56
  berksfile_source?(node) do
57
57
  add_offense(node, message: MSG, severity: :refactor) do |corrector|
58
- corrector.replace(node.loc.expression, "source 'https://supermarket.chef.io'")
58
+ corrector.replace(node, "source 'https://supermarket.chef.io'")
59
59
  end
60
60
  end
61
61
 
62
62
  berksfile_site?(node) do
63
63
  add_offense(node, message: MSG, severity: :refactor) do |corrector|
64
- corrector.replace(node.loc.expression, "source 'https://supermarket.chef.io'")
64
+ corrector.replace(node, "source 'https://supermarket.chef.io'")
65
65
  end
66
66
  end
67
67
  end
@@ -44,7 +44,7 @@ module RuboCop
44
44
  def on_send(node)
45
45
  build_essential_recipe_usage?(node) do
46
46
  add_offense(node, message: MSG, severity: :refactor) do |corrector|
47
- corrector.replace(node.loc.expression, "build_essential 'install compilation tools'")
47
+ corrector.replace(node, "build_essential 'install compilation tools'")
48
48
  end
49
49
  end
50
50
  end
@@ -35,6 +35,7 @@ module RuboCop
35
35
  #
36
36
  class ResourceForcingCompileTime < Base
37
37
  MSG = "Set 'compile_time true' in resources when available instead of forcing resources to run at compile time by setting an action on the block."
38
+ RESTRICT_ON_SEND = [:run_action].freeze
38
39
 
39
40
  def_node_matcher :compile_time_resource?, <<-PATTERN
40
41
  (send (block (send nil? {:build_essential :chef_gem :hostname :ohai_hint} (...)) (args) (...)) $:run_action (sym ...))
@@ -36,7 +36,7 @@ module RuboCop
36
36
  RESTRICT_ON_SEND = [:not_if, :only_if].freeze
37
37
 
38
38
  def_node_matcher :resource_conditional?, <<~PATTERN
39
- (send nil? {:not_if :only_if} $str )
39
+ (send nil? {:not_if :only_if} $str )
40
40
  PATTERN
41
41
 
42
42
  def on_send(node)
@@ -44,7 +44,7 @@ module RuboCop
44
44
  return unless conditional.value.match?(/^test -[ef] \S*$/)
45
45
  add_offense(node, message: MSG, severity: :refactor) do |corrector|
46
46
  new_string = "{ ::File.exist?('#{conditional.value.match(/^test -[ef] (\S*)$/)[1]}') }"
47
- corrector.replace(conditional.loc.expression, new_string)
47
+ corrector.replace(conditional, new_string)
48
48
  end
49
49
  end
50
50
  end