cookstyle 6.3.4 → 6.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/config/cookstyle.yml +290 -15
  4. data/config/disable_all.yml +13 -1
  5. data/config/upstream.yml +82 -22
  6. data/lib/cookstyle.rb +1 -1
  7. data/lib/cookstyle/version.rb +2 -2
  8. data/lib/rubocop/chef/platform_helpers.rb +2 -1
  9. data/lib/rubocop/cop/chef/correctness/invalid_platform_family_values_in_case.rb +77 -0
  10. data/lib/rubocop/cop/chef/correctness/invalid_platform_values_in_case.rb +77 -0
  11. data/lib/rubocop/cop/chef/correctness/lazy_eval_node_attribute_defaults.rb +56 -0
  12. data/lib/rubocop/cop/chef/correctness/node_normal.rb +1 -1
  13. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +1 -1
  14. data/lib/rubocop/cop/chef/correctness/openssl_password_helpers.rb +45 -0
  15. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +1 -1
  16. data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +1 -1
  17. data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +10 -4
  18. data/lib/rubocop/cop/chef/deprecation/easy_install.rb +2 -2
  19. data/lib/rubocop/cop/chef/deprecation/erl_call.rb +1 -1
  20. data/lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb +141 -0
  21. data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +2 -2
  22. data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +1 -1
  23. data/lib/rubocop/cop/chef/deprecation/node_set.rb +2 -3
  24. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +2 -3
  25. data/lib/rubocop/cop/chef/deprecation/powershell_cookbook_helpers.rb +3 -3
  26. data/lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb +86 -0
  27. data/lib/rubocop/cop/chef/deprecation/ruby_27_keyword_argument_warnings.rb +59 -0
  28. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +6 -1
  29. data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +3 -3
  30. data/lib/rubocop/cop/chef/modernize/includes_mixin_shellout.rb +24 -3
  31. data/lib/rubocop/cop/chef/modernize/shell_out_helper.rb +64 -0
  32. data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +8 -4
  33. data/lib/rubocop/cop/chef/style/overly_complex_supports_depends_metadata.rb +1 -1
  34. data/lib/rubocop/cop/target_chef_version.rb +4 -0
  35. data/lib/rubocop/monkey_patches/team.rb +24 -0
  36. metadata +13 -6
  37. data/lib/rubocop/cop/chef/deprecation/resource_without_name_or_provides.rb +0 -81
  38. data/lib/rubocop/monkey_patches/commissioner.rb +0 -26
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright:: Copyright 2020, 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 ChefCorrectness
21
+ # Use valid platform values in case statements.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # case node['platform']
27
+ # when 'rhel'
28
+ # puts "I'm on a Red Hat system!"
29
+ # end
30
+ #
31
+ class InvalidPlatformInCase < Cop
32
+ include RangeHelp
33
+ include ::RuboCop::Chef::PlatformHelpers
34
+
35
+ MSG = 'Use valid platform values in case statements.'.freeze
36
+
37
+ def_node_matcher :node_platform?, <<-PATTERN
38
+ (send (send nil? :node) :[] (str "platform") )
39
+ PATTERN
40
+
41
+ def on_case(node)
42
+ node_platform?(node.condition) do
43
+ node.each_when do |when_node|
44
+ when_node.each_condition do |con|
45
+ next unless con.str_type? # if the condition isn't a string we can't check so skip
46
+
47
+ if INVALID_PLATFORMS[con.str_content]
48
+ add_offense(con, location: :expression, message: MSG, severity: :refactor)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def autocorrect(node)
56
+ new_value = INVALID_PLATFORMS[node.str_content]
57
+
58
+ # some invalid platform have no direct correction value and return nil instead
59
+ return unless new_value
60
+
61
+ # if the correct value already exists in the when statement then we just want to delete this node
62
+ if node.parent.conditions.any? { |x| x.str_content == new_value }
63
+ lambda do |corrector|
64
+ range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :both)
65
+ corrector.remove(range)
66
+ end
67
+ else
68
+ lambda do |corrector|
69
+ corrector.replace(node.loc.expression, "'#{new_value}'")
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,56 @@
1
+ #
2
+ # Copyright:: 2020, 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 ChefCorrectness
22
+ # When setting a node attribute as the default value for a custom resource property, wrap the node attribute in `lazy {}` so that its value is available when the resource executes.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # property :Something, String, default: node['hostname']
28
+ #
29
+ # # good
30
+ # property :Something, String, default: lazy { node['hostname'] }
31
+ #
32
+ class LazyEvalNodeAttributeDefaults < Cop
33
+ include RuboCop::Chef::CookbookHelpers
34
+
35
+ MSG = 'When setting a node attribute as the default value for a custom resource property, wrap the node attribute in `lazy {}` so that its value is available when the resource executes.'.freeze
36
+
37
+ def_node_matcher :non_lazy_node_attribute_default?, <<-PATTERN
38
+ (send nil? :property (sym _) ... (hash <(pair (sym :default) $(send (send _ :node) :[] _) ) ...>))
39
+ PATTERN
40
+
41
+ def on_send(node)
42
+ non_lazy_node_attribute_default?(node) do |default|
43
+ add_offense(default, location: :expression, message: MSG, severity: :refactor)
44
+ end
45
+ end
46
+
47
+ def autocorrect(node)
48
+ lambda do |corrector|
49
+ corrector.replace(node.loc.expression, "lazy { #{node.loc.expression.source} }")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Chef
19
19
  module ChefCorrectness
20
20
  # Normal attributes are discouraged since their semantics differ importantly from the
21
- # default and override levels. Their values persist in the node object even after
21
+ # default and override levels. Their values persist in the node object even after
22
22
  # all code referencing them has been deleted, unlike default and override.
23
23
  #
24
24
  # Code should be updated to use default or override levels, but this will change
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Chef
19
19
  module ChefCorrectness
20
20
  # Normal attributes are discouraged since their semantics differ importantly from the
21
- # default and override levels. Their values persist in the node object even after
21
+ # default and override levels. Their values persist in the node object even after
22
22
  # all code referencing them has been deleted, unlike default and override.
23
23
  #
24
24
  # Code should be updated to use default or override levels, but this will change
@@ -0,0 +1,45 @@
1
+
2
+ #
3
+ # Copyright:: Copyright 2020, Chef Software Inc.
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 ChefCorrectness
21
+ # The openSSL cookbook provides a deprecated `secure_password` helper in the `Opscode::OpenSSL::Password` class, which should not longer be used. This helper would generate a random password that would be used when a data bag or attribute was no present. The practice of generating passwords to be stored on the node is bad security as it exposes the password to anyone that can view the nodes, and deleting a node deletes the password. Passwords should be retrieved from a secure source for use in cookbooks.
22
+ #
23
+ # # bad
24
+ # ::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
25
+ # basic_auth_password = secure_password
26
+ #
27
+ class OpenSSLPasswordHelpers < Cop
28
+ MSG = 'The `secure_password` helper from the openssl cookbooks `Opscode::OpenSSL::Password` class should not be used to generate passwords.'.freeze
29
+
30
+ def_node_matcher :openssl_helper?, <<~PATTERN
31
+ (const
32
+ (const
33
+ (const nil? :Opscode) :OpenSSL) :Password)
34
+ PATTERN
35
+
36
+ def on_const(node)
37
+ openssl_helper?(node) do
38
+ add_offense(node, location: :expression, message: MSG, severity: :warning)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Don't depend on the deprecated compat_resource cookbook made obsolete by Chef 12.19+
21
+ # Don't depend on the deprecated compat_resource cookbook made obsolete by Chef Infra Client 12.19+
22
22
  #
23
23
  # @example
24
24
  #
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Don't depend on the partial_search cookbook made obsolete by Chef 13
21
+ # Don't depend on the partial_search cookbook made obsolete by Chef Infra Client 13
22
22
  #
23
23
  # @example
24
24
  #
@@ -47,22 +47,28 @@ module RuboCop
47
47
  '> 16.04, < 18.04' => true,
48
48
  },
49
49
  'fedora' => {
50
- '< 30' => '30',
50
+ '< 31' => '31',
51
51
  },
52
52
  'freebsd' => {
53
- '< 11' => '12',
53
+ '~> 11.0, < 11.2' => '11',
54
+ '= 12.0' => '12',
55
+ '< 11' => true,
54
56
  },
55
57
  'mac_os_x' => {
56
58
  '< 10.12' => '10.15',
57
59
  },
60
+ 'suse' => {
61
+ '~> 12.0, < 12.4' => '12',
62
+ '< 12' => true,
63
+ },
58
64
  'opensuse' => {
59
65
  '< 14' => true,
60
66
  '~> 42.0' => true,
61
67
  },
62
68
  'debian' => {
63
69
  '< 8' => true,
64
- '> 8.0, < 8.9' => '8',
65
- '> 9.0, < 9.8' => '9',
70
+ '> 8.0, < 8.10' => '8',
71
+ '> 9.0, < 9.9' => '9',
66
72
  },
67
73
  'centos' => {
68
74
  '< 6.0' => true,
@@ -19,7 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefDeprecations
22
- # Don't use the deprecated easy_install resource removed in Chef 13
22
+ # Don't use the deprecated easy_install resource removed in Chef Infra Client 13
23
23
  #
24
24
  # @example
25
25
  #
@@ -29,7 +29,7 @@ module RuboCop
29
29
  # end
30
30
  #
31
31
  class EasyInstallResource < Cop
32
- MSG = "Don't use the deprecated easy_install resource removed in Chef 13".freeze
32
+ MSG = "Don't use the deprecated easy_install resource removed in Chef Infra Client 13".freeze
33
33
 
34
34
  def on_send(node)
35
35
  add_offense(node, location: :expression, message: MSG, severity: :warning) if node.method_name == :easy_install
@@ -29,7 +29,7 @@ module RuboCop
29
29
  # end
30
30
  #
31
31
  class ErlCallResource < Cop
32
- MSG = "Don't use the deprecated erl_call resource removed in Chef 13".freeze
32
+ MSG = "Don't use the deprecated erl_call resource removed in Chef Infra Client 13".freeze
33
33
 
34
34
  def on_send(node)
35
35
  add_offense(node, location: :expression, message: MSG, severity: :warning) if node.method_name == :erl_call
@@ -0,0 +1,141 @@
1
+ #
2
+ # Copyright:: Copyright (c) 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
+ # Chef Infra Client 16 and later a legacy HWRP resource must use `provides` to define how the resource is called in recipes or other resources. To maintain compatibility with Chef Infra Client < 16 use both `resource_name` and `provides`.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # class Chef
28
+ # class Resource
29
+ # class UlimitRule < Chef::Resource
30
+ # property :type, [Symbol, String], required: true
31
+ # property :item, [Symbol, String], required: true
32
+ #
33
+ # # additional resource code
34
+ # end
35
+ # end
36
+ # end
37
+ #
38
+ # # bad
39
+ # class Chef
40
+ # class Resource
41
+ # class UlimitRule < Chef::Resource
42
+ # resource_name :ulimit_rule
43
+ #
44
+ # property :type, [Symbol, String], required: true
45
+ # property :item, [Symbol, String], required: true
46
+ #
47
+ # # additional resource code
48
+ # end
49
+ # end
50
+ # end
51
+ #
52
+ # # good when Chef Infra Client < 15 (but compatible with 16+ as well)
53
+ # class Chef
54
+ # class Resource
55
+ # class UlimitRule < Chef::Resource
56
+ # resource_name :ulimit_rule
57
+ # provides :ulimit_rule
58
+ #
59
+ # property :type, [Symbol, String], required: true
60
+ # property :item, [Symbol, String], required: true
61
+ #
62
+ # # additional resource code
63
+ # end
64
+ # end
65
+ # end
66
+ #
67
+ # # good when Chef Infra Client 16+
68
+ # class Chef
69
+ # class Resource
70
+ # class UlimitRule < Chef::Resource
71
+ # provides :ulimit_rule
72
+ #
73
+ # property :type, [Symbol, String], required: true
74
+ # property :item, [Symbol, String], required: true
75
+ #
76
+ # # additional resource code
77
+ # end
78
+ # end
79
+ # end
80
+ #
81
+ # # better
82
+ # Convert your legacy HWRPs to custom resources
83
+ #
84
+ class HWRPWithoutProvides < Cop
85
+ MSG = 'In Chef Infra Client 16 and later a legacy HWRP resource must use `provides` to define how the resource is called in recipes or other resources. To maintain compatibility with Chef Infra Client < 16 use both `resource_name` and `provides`.'.freeze
86
+
87
+ def_node_matcher :HWRP?, <<-PATTERN
88
+ (class
89
+ (const nil? :Chef) nil?
90
+ (class
91
+ (const nil? :Resource) nil?
92
+ $(class
93
+ (const nil? ... )
94
+ (const
95
+ (const nil? :Chef) :Resource)
96
+ (begin ... ))))
97
+ PATTERN
98
+
99
+ def_node_search :provides, '(send nil? :provides (sym $_) ...)'
100
+ def_node_search :resource_name_ast, '$(send nil? :resource_name ...)'
101
+ def_node_search :resource_name, '(send nil? :resource_name (sym $_))'
102
+
103
+ def on_class(node)
104
+ HWRP?(node) do |inherit|
105
+ add_offense(inherit, location: :expression, message: MSG, severity: :warning) unless has_provides?
106
+ end
107
+ end
108
+
109
+ def has_provides?
110
+ provides_ast = provides(processed_source.ast)
111
+ return false if provides_ast.count == 0
112
+
113
+ resource_ast = resource_name(processed_source.ast)
114
+
115
+ if resource_ast.count == 0
116
+ true # no resource_name, but provides
117
+ else
118
+ # since we have a resource and provides make sure the there is a provides that
119
+ # matches the resource name
120
+ provides_ast.include?(resource_ast.first)
121
+ end
122
+ end
123
+
124
+ def indentation(node)
125
+ node.source_range.source_line =~ /\S/
126
+ end
127
+
128
+ def autocorrect(node)
129
+ lambda do |corrector|
130
+ resource_name_ast(node) do |ast_match|
131
+ # build a new string to add after that includes the new line and the proper indentation
132
+ new_string = "\n" + ast_match.source.dup.gsub('resource_name', 'provides').prepend(' ' * indentation(ast_match))
133
+ corrector.insert_after(ast_match.source_range, new_string)
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 16
21
+ # The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 17
22
22
  #
23
23
  # @example
24
24
  #
@@ -31,7 +31,7 @@ module RuboCop
31
31
  class LocaleDeprecatedLcAllProperty < Cop
32
32
  include RuboCop::Chef::CookbookHelpers
33
33
 
34
- MSG = "The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 16".freeze
34
+ MSG = "The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 17".freeze
35
35
 
36
36
  def on_block(node)
37
37
  match_property_in_resource?(:locale, 'lc_all', node) do |property|
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Incorrectly using node methods for Ohai data when you really want node attributes
21
+ # Use node attributes to access data provided by Ohai instead of using node methods to access that data.
22
22
  #
23
23
  # @example
24
24
  #