inspecstyle 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c05d8e13dc59b97ef50ee7e38f9e728dc3e52ade3e648c4dc73241d3f32d60f
4
- data.tar.gz: be29a3ae8643a818ef62e7f0a491cc161940b9a675641ae34343cdc082470ec2
3
+ metadata.gz: c51bedfaba90e6e4cf18d3a51f66198bf2fa16a497b49b0df0463090f8405104
4
+ data.tar.gz: '090fe9248bb711be0f8c77c845317c98a1acc66dd377c6f7f24923338afd9638'
5
5
  SHA512:
6
- metadata.gz: 460c48ff0252cbfff1fb0a1b0245366eba8ae3ed939c03dc418a0561ce9d8d31fc8d9beefaf941fe24ea93f52681057104e6ec8863b58b248399ea1072c7a3dd
7
- data.tar.gz: e8bb21c13461a344f7ed3b75ef55634755adb454d0530672024ef8fb60d15fdfb3d0c06aedb60322ca864e7520f88cc65e4913979d5a76051467e3f44dafe49a
6
+ metadata.gz: 0fa18de10357a081e004f385a8635d658eb878779468db447d2584f8bc4697a6b6a6a7f348811a697cfce61cd61c7928669dbd779331bc0585f93b4079ac42a6
7
+ data.tar.gz: b3082f4bc9201180cd09aa31bcfde73e2ec6c6ac157da1b6307e50eb83161cc97c8943ba4eec6ebeb691e4296a31846bc5322c4adca85789170471be5a9ebbe1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- inspecstyle (0.1.3)
4
+ inspecstyle (0.2.0)
5
5
  rubocop
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ # WIP: This gem is __not__ live, but we would love your help and input! Core issue here: https://github.com/inspec/inspec/issues/5095
2
+
3
+
1
4
  # InSpecStyle
2
5
 
3
6
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubocop/inspecstyle`. To experiment with that code, run `bin/console` for an interactive prompt.
@@ -1,3 +1,14 @@
1
+ InSpecStyle/Apache:
2
+ Description: 'Use apache_conf instead of apache'
3
+ Enabled: true
4
+ VersionAdded: '0.82'
5
+ PreferredReplacement: 'apache_conf'
6
+
7
+ InSpecStyle/AwsIamUserProperty:
8
+ Description: 'For the aws_iam_users resource name and user properties are deprecated. Use username and aws_user_struct instead'
9
+ Enabled: true
10
+ VersionAdded: '0.82'
11
+
1
12
  InSpecStyle/AzureGenericResource:
2
13
  Description: 'Do not use azure_generic_resource'
3
14
  Enabled: true
@@ -11,17 +22,82 @@ InSpecStyle/DeprecatedAttributes:
11
22
  StyleGuide: 'https://github.com/inspec/inspec/issues/3802'
12
23
  PreferredReplacement: 'input'
13
24
 
25
+ InSpecStyle/FileBeMounted:
26
+ Description: 'aws_iam_users resource name and user properties are deprecated for username and aws_user_struct'
27
+ Enabled: true
28
+ VersionAdded: '0.82'
29
+
30
+ InSpecStyle/FileSize:
31
+ Description: '`size` property for `file` resource is deprecated for `size_kb` and will be removed in InSpec5'
32
+ Enabled: true
33
+ VersionAdded: '0.82'
34
+ PreferredReplacement: 'size_kb'
35
+
36
+ InSpecStyle/HostProto:
37
+ Description: 'Proto property for host has been deprecated for protocol'
38
+ Enabled: true
39
+ VersionAdded: '0.82'
40
+ PreferredReplacement: 'protocol'
41
+
42
+ InSpecStyle/IisWebsite:
43
+ Description: 'iis_website resource has been deprecated for iis_site'
44
+ Enabled: true
45
+ VersionAdded: '0.82'
46
+ PreferredReplacement: 'iis_site'
47
+
48
+ InSpecStyle/LinuxKernelParameter:
49
+ Description: 'linux_kernel_parameter resource has been deprecated for kernel_parameter'
50
+ Enabled: true
51
+ VersionAdded: '0.82'
52
+ PreferredReplacement: 'kernel_parameter'
53
+
54
+ InSpecStyle/MSSQLSessionPass:
55
+ Description: 'mssql_session resource parameter pass is deprecated for password'
56
+ Enabled: true
57
+ VersionAdded: '0.82'
58
+
14
59
  InSpecStyle/OracleDbSessionPass:
15
- Description: 'Use `pass` instead of `password`'
60
+ Description: 'Use `password` instead of `pass`'
61
+ Enabled: true
62
+ VersionAdded: '0.82'
63
+ PreferredReplacement: 'password'
64
+
65
+ InSpecStyle/PPAResource:
66
+ Description: 'The ppa resource is deprecated in favor of the apt resource'
67
+ Enabled: true
68
+ VersionAdded: '0.82'
69
+ PreferredReplacement: 'apt'
70
+
71
+ InSpecStyle/ProcessesList:
72
+ Description: 'list property on processes resource is deprecated for entries'
16
73
  Enabled: true
17
74
  VersionAdded: '0.82'
75
+ PreferredReplacement: 'entries'
76
+
77
+ InSpecStyle/ScriptResource:
78
+ Description: 'Use powershell instead of script'
79
+ Enabled: true
80
+ VersionAdded: '0.82'
81
+ PreferredReplacement: 'powershell'
18
82
 
19
83
  InSpecStyle/ShadowProperties:
20
- Description: '`user` attribute for shadow resources is deprecated for `users`'
84
+ Description: 'Deprecated properties for the shadow resource'
85
+ Enabled: true
86
+ VersionAdded: '0.82'
87
+
88
+ InSpecStyle/UsersResourceMatchers:
89
+ Description: 'Deprecated matchers for the users resource'
90
+ Enabled: true
91
+ VersionAdded: '0.82'
92
+
93
+ InSpecStyle/WindowsRegistryKey:
94
+ Description: 'The windows_registry_key resource is deprecated for registry_key'
21
95
  Enabled: true
22
96
  VersionAdded: '0.82'
97
+ PreferredReplacement: 'registry_key'
23
98
 
24
- InspecStyle/FirstCop:
25
- Description: 'This first cop is a sandbox for exploring new InspecStyle cops.'
99
+ InSpecStyle/WmiWmisClass:
100
+ Description: "wmi('wmisclass') is deprecated for wmi({class: wmisclass})"
26
101
  Enabled: true
27
102
  VersionAdded: '0.82'
103
+ PreferredReplacement: "wmi({class: 'wmisclass'})"
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # Do not use apache
7
+ #
8
+ # @example EnforcedStyle: InSpecStyle (default)
9
+ # # apache has been deprecated
10
+ # # 'https://github.com/inspec/inspec/issues/3131'
11
+ # # Since there are multiples replacements autocorrect is not supported.
12
+ #
13
+ # # bad
14
+ # apache
15
+ #
16
+ # # good
17
+ # azurerm_virtual_machine # use a specific resource pack resource
18
+ #
19
+ class Apache < Cop
20
+ MSG = 'Use a `apache_conf` instead of `#apache`. '\
21
+ 'This resource will be removed in InSpec 5.'
22
+
23
+ def_node_matcher :spec?, <<-PATTERN
24
+ (block
25
+ (send nil? :describe ...)
26
+ ...)
27
+ PATTERN
28
+
29
+ def_node_matcher :apache?, <<~PATTERN
30
+ (send nil? :apache ...)
31
+ PATTERN
32
+
33
+ def on_send(node)
34
+ return unless apache?(node)
35
+
36
+ add_offense(node, location: :selector)
37
+ end
38
+
39
+ def autocorrect(node)
40
+ lambda do |corrector|
41
+ corrector.replace(node.loc.selector, preferred_replacement)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def inside_spec?(root)
48
+ spec?(root)
49
+ end
50
+
51
+ def preferred_replacement
52
+ cop_config.fetch('PreferredReplacement')
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # aws_iam_users resource properties `user|password|last_change|expiry_date|line` is deprecated in favor of `users|passwords|last_changes|expiry_dates|lines`
7
+ #
8
+ # @example EnforcedStyle: InSpecStyle (default)
9
+ # # Use users instead
10
+ #
11
+ # # bad
12
+ # describe aws_iam_user('/etc/my-custom-place/aws_iam_user') do
13
+ # its('name') { should eq 'user' }
14
+ # end
15
+ #
16
+ # # good
17
+ # describe aws_iam_user('/etc/my-custom-place/aws_iam_user') do
18
+ # its('username') { should eq 'user' }
19
+ # end
20
+ #
21
+ class AwsIamUserProperty < Cop
22
+ include RangeHelp
23
+
24
+ MSG = 'Use `:%<solution>s` instead of `:%<violation>s` as a property ' \
25
+ 'for the `aws_iam_user` resource. This property will be removed in InSpec 5'
26
+
27
+ MAP = {
28
+ user: "aws_user_struct",
29
+ name: "username"
30
+ }
31
+
32
+ def_node_matcher :deprecated_aws_iam_users_property?, <<~PATTERN
33
+ (block
34
+ (send _ :its
35
+ (str ${"name" "user"}) ...)
36
+ ...)
37
+ PATTERN
38
+
39
+ def_node_matcher :spec?, <<-PATTERN
40
+ (block
41
+ (send nil? :describe ...)
42
+ ...)
43
+ PATTERN
44
+
45
+ def_node_matcher :aws_iam_users_resource?, <<-PATTERN
46
+ (block
47
+ (send nil? :describe
48
+ (send nil? :aws_iam_users ...)
49
+ ...)
50
+ ...)
51
+ PATTERN
52
+
53
+ def on_block(node)
54
+ return unless inside_aws_iam_users_spec?(node)
55
+ node.descendants.each do |descendant|
56
+ deprecated_aws_iam_users_property?(descendant) do |violation|
57
+ add_offense(
58
+ descendant,
59
+ location: offense_range(descendant),
60
+ message: format(
61
+ MSG,
62
+ violation: violation,
63
+ solution: MAP[violation.to_sym]
64
+ )
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ def autocorrect(node)
71
+ lambda do |corrector|
72
+ case node.children[0].children[-1].inspect
73
+ when "s(:str, \"name\")"
74
+ corrector.replace(offense_range(node), 'username')
75
+ when "s(:str, \"user\")"
76
+ corrector.replace(offense_range(node), 'aws_user_struct')
77
+ else
78
+ break
79
+ end
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def inside_aws_iam_users_spec?(root)
86
+ spec?(root) && aws_iam_users_resource?(root)
87
+ end
88
+
89
+ def offense_range(node)
90
+ source = node.children[0].children[-1].loc.expression
91
+ range_between(source.begin_pos+1, source.end_pos-1)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NOTE TO SELF - this one works BUT not if other its statements are defined. Needs
4
+ # to work in any arrangement. This is a powerful one to crack as this pattern
5
+ # will be used in a LOT of other cops.
6
+
7
+ module RuboCop
8
+ module Cop
9
+ module InSpecStyle
10
+ # file resource deprecates matchers `be_mounted.with` and `be_mounted.only_with` in favor of the mount resource
11
+ #
12
+
13
+ class FileBeMounted < Cop
14
+ MSG = 'Use the `:mount` resource instead of `be_mounted.%<violation>s` ' \
15
+ "\nThis matcher will be removed in InSpec 5"
16
+
17
+ def_node_matcher :deprecated_file_matcher?, <<~PATTERN
18
+ (send
19
+ (send _ :be_mounted ...) ${:with :only_with}
20
+ ...)
21
+ PATTERN
22
+
23
+ def_node_matcher :spec?, <<-PATTERN
24
+ (block
25
+ (send nil? :describe ...)
26
+ ...)
27
+ PATTERN
28
+
29
+ def_node_matcher :file_resource?, <<-PATTERN
30
+ (block
31
+ (send nil? :describe
32
+ (send nil? :file ...)
33
+ ...)
34
+ ...)
35
+ PATTERN
36
+
37
+ def on_block(node)
38
+ return unless inside_file_spec?(node)
39
+ node.descendants.each do |descendant|
40
+ deprecated_file_matcher?(descendant) do |violation|
41
+ add_offense(
42
+ descendant,
43
+ location: offense_range(descendant),
44
+ message: format(
45
+ MSG,
46
+ violation: violation
47
+ )
48
+ )
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def inside_file_spec?(root)
56
+ spec?(root) && file_resource?(root)
57
+ end
58
+
59
+ def offense_range(node)
60
+ node.loc.selector
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # `size` property for `file` resource is deprecated for `size_kb` and will be removed in InSpec5
8
+ #
9
+ # # bad
10
+ # describe file('my_file.txt') do
11
+ # its('size') { should eq 12345 }
12
+ # end
13
+ #
14
+ # # good
15
+ # describe file('my_file.txt') do
16
+ # its('size_kb') { should eq 12345 }
17
+ # end
18
+ #
19
+ class FileSize < Cop
20
+ include RangeHelp
21
+
22
+ MSG = '`size` property for `file` resource is deprecated for `size_kb` and will be removed in InSpec5'
23
+
24
+ def_node_matcher :file_resource_size_property?, <<~PATTERN
25
+ (block
26
+ (send _ :its
27
+ (str "size") ...)
28
+ ...)
29
+ PATTERN
30
+
31
+ def_node_matcher :spec?, <<-PATTERN
32
+ (block
33
+ (send nil? :describe ...)
34
+ ...)
35
+ PATTERN
36
+
37
+ def_node_matcher :file_resource?, <<-PATTERN
38
+ (block
39
+ (send nil? :describe
40
+ (send nil? :file ...)
41
+ ...)
42
+ ...)
43
+ PATTERN
44
+
45
+
46
+ def on_block(node)
47
+ return unless inside_file_spec?(node)
48
+ node.descendants.each do |descendant|
49
+ next unless file_resource_size_property?(descendant)
50
+ add_offense(descendant, location: offense_range(descendant))
51
+ end
52
+ end
53
+
54
+ def autocorrect(node)
55
+ lambda do |corrector|
56
+ corrector.replace(offense_range(node), preferred_replacement)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def inside_file_spec?(root)
63
+ spec?(root) && file_resource?(root)
64
+ end
65
+
66
+ def preferred_replacement
67
+ cop_config.fetch('PreferredReplacement')
68
+ end
69
+
70
+ def offense_range(node)
71
+ source = node.children[0].children[-1].loc.expression
72
+ range_between(source.begin_pos+1, source.end_pos-1)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # `proto` property for `host` resource is deprecated for `protocol` and will be removed in InSpec5
8
+ #
9
+ # # bad
10
+ # describe host('my_host.txt') do
11
+ # its('proto') { should eq 12345 }
12
+ # end
13
+ #
14
+ # # good
15
+ # describe host('my_host.txt') do
16
+ # its('protocol') { should eq 12345 }
17
+ # end
18
+ #
19
+ class HostProto < Cop
20
+ include RangeHelp
21
+
22
+ MSG = '`proto` property for `host` resource is deprecated for `protocol` and will be removed in InSpec5'
23
+
24
+ def_node_matcher :host_resource_proto_property?, <<~PATTERN
25
+ (block
26
+ (send _ :its
27
+ (str "proto") ...)
28
+ ...)
29
+ PATTERN
30
+
31
+ def_node_matcher :host_resource?, <<-PATTERN
32
+ (block
33
+ (send nil? :describe
34
+ (send nil? :host ...)
35
+ ...)
36
+ ...)
37
+ PATTERN
38
+
39
+
40
+ def on_block(node)
41
+ return unless inside_resource_spec?(node)
42
+ node.descendants.each do |descendant|
43
+ next unless host_resource_proto_property?(descendant)
44
+ add_offense(descendant, location: offense_range(descendant))
45
+ end
46
+ end
47
+
48
+ def autocorrect(node)
49
+ lambda do |corrector|
50
+ corrector.replace(offense_range(node), preferred_replacement)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def inside_resource_spec?(root)
57
+ host_resource?(root)
58
+ end
59
+
60
+ def preferred_replacement
61
+ cop_config.fetch('PreferredReplacement')
62
+ end
63
+
64
+ def offense_range(node)
65
+ source = node.children[0].children[-1].loc.expression
66
+ range_between(source.begin_pos+1, source.end_pos-1)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # iis_website has been deprecated as a resource. Use iis_site instead
8
+ #
9
+ class IisWebsite < Cop
10
+ MSG = 'Use `iis_site` instead of `iis_website`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :iis_website?, <<~PATTERN
14
+ (send _ :iis_website ...)
15
+ PATTERN
16
+
17
+ def on_send(node)
18
+ return unless iis_website?(node)
19
+ add_offense(node, location: :selector)
20
+ end
21
+
22
+ def autocorrect(node)
23
+ lambda do |corrector|
24
+ corrector.replace(node.loc.selector, preferred_replacement)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def preferred_replacement
31
+ cop_config.fetch('PreferredReplacement')
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # linux_kernel_parameter has been deprecated as a resource. Use kernel_parameter instead
8
+ #
9
+ class LinuxKernelParameter < Cop
10
+ MSG = 'Use `kernel_parameter` instead of `linux_kernel_parameter`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :linux_kernel_parameter?, <<~PATTERN
14
+ (send nil? :linux_kernel_parameter ...)
15
+ PATTERN
16
+
17
+ def on_send(node)
18
+ return unless linux_kernel_parameter?(node)
19
+
20
+ add_offense(node, location: :selector)
21
+ end
22
+
23
+ def autocorrect(node)
24
+ lambda do |corrector|
25
+ corrector.replace(node.loc.selector, preferred_replacement)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def inside_spec?(root)
32
+ spec?(root)
33
+ end
34
+
35
+ def preferred_replacement
36
+ cop_config.fetch('PreferredReplacement')
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Note: highlighting could possibly be better
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module InSpecStyle
8
+ #
9
+ # @example EnforcedStyle: InSpecStyle (default)
10
+ # # Description of the `bar` style.
11
+ #
12
+ # # bad
13
+ # sql = mssql_session(user: 'my_user', pass: 'password')
14
+ #
15
+ # # good
16
+ # sql = mssql_session(user: 'my_user', password: 'password
17
+ #
18
+ class MSSQLSessionPass < Cop
19
+ include MatchRange
20
+ MSG = 'Use `:password` instead of `:pass`. This will be removed in '\
21
+ 'InSpec 5'
22
+
23
+ def_node_matcher :mssql_session_pass?, <<~PATTERN
24
+ (send _ :mssql_session
25
+ (hash
26
+ ...
27
+ (pair
28
+ (sym $:pass)
29
+ ...)))
30
+ PATTERN
31
+
32
+ # Getting location was a bit tricky on this one, looking at docs perhaps
33
+ # convention does allow highlighting an entire line.
34
+ def on_send(node)
35
+ return unless result = mssql_session_pass?(node)
36
+ add_offense(node, message: MSG)
37
+ end
38
+
39
+ def autocorrect(node)
40
+ lambda do |corrector|
41
+ corrector.replace(offense_range(node), preferred_replacement)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def offense_range(node)
48
+ node.descendants.map do |x|
49
+ x.descendants.find {|y| y.inspect == "s(:sym, :pass)"}
50
+ end.compact.first.source_range
51
+ end
52
+
53
+ def preferred_replacement
54
+ cop_config.fetch('PreferredReplacement')
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Note: highlighting could possibly be better
4
+
3
5
  module RuboCop
4
6
  module Cop
5
7
  module InSpecStyle
@@ -11,7 +13,8 @@ module RuboCop
11
13
  # sql = oracledb_session(user: 'my_user', pass: 'password')
12
14
  #
13
15
  # # good
14
- # sql = oracledb_session(user: 'my_user', password: 'password')
16
+ # sql = oracledb_session(user: 'my_user', password: 'password
17
+ #
15
18
  class OracleDbSessionPass < Cop
16
19
  include MatchRange
17
20
  MSG = 'Use `:password` instead of `:pass`. This will be removed in '\
@@ -32,6 +35,24 @@ module RuboCop
32
35
  return unless result = oracledb_session_pass?(node)
33
36
  add_offense(node, message: MSG)
34
37
  end
38
+
39
+ def autocorrect(node)
40
+ lambda do |corrector|
41
+ corrector.replace(offense_range(node), preferred_replacement)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def offense_range(node)
48
+ node.descendants.map do |x|
49
+ x.descendants.find {|y| y.inspect == "s(:sym, :pass)"}
50
+ end.compact.first.source_range
51
+ end
52
+
53
+ def preferred_replacement
54
+ cop_config.fetch('PreferredReplacement')
55
+ end
35
56
  end
36
57
  end
37
58
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # ppa has been deprecated as a resource. Use apt instead
8
+ #
9
+ class PPAResource < Cop
10
+ MSG = 'Use `apt` instead of `ppa`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :ppa?, <<~PATTERN
14
+ (send _ :ppa ...)
15
+ PATTERN
16
+
17
+ def on_send(node)
18
+ return unless ppa?(node)
19
+ add_offense(node, location: :selector)
20
+ end
21
+
22
+ def autocorrect(node)
23
+ lambda do |corrector|
24
+ corrector.replace(node.loc.selector, preferred_replacement)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def preferred_replacement
31
+ cop_config.fetch('PreferredReplacement')
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # `list` property for `processes` resource is deprecated for `entries` and will be removed in InSpec5
8
+ #
9
+ # # bad
10
+ # describe processes('my_processes.txt') do
11
+ # its('list') { should eq 12345 }
12
+ # end
13
+ #
14
+ # # good
15
+ # describe processes('my_processes.txt') do
16
+ # its('entries') { should eq 12345 }
17
+ # end
18
+ #
19
+ class ProcessesList < Cop
20
+ include RangeHelp
21
+
22
+ MSG = '`list` property for `processes` resource is deprecated for `entries` and will be removed in InSpec5'
23
+
24
+ def_node_matcher :processes_resource_list?, <<~PATTERN
25
+ (block
26
+ (send _ :its
27
+ (str "list") ...)
28
+ ...)
29
+ PATTERN
30
+
31
+ def_node_matcher :spec?, <<-PATTERN
32
+ (block
33
+ (send nil? :describe ...)
34
+ ...)
35
+ PATTERN
36
+
37
+ def_node_matcher :processes_resource?, <<-PATTERN
38
+ (block
39
+ (send nil? :describe
40
+ (send nil? :processes ...)
41
+ ...)
42
+ ...)
43
+ PATTERN
44
+
45
+
46
+ def on_block(node)
47
+ return unless inside_processes_spec?(node)
48
+ node.descendants.each do |descendant|
49
+ next unless processes_resource_list?(descendant)
50
+ add_offense(descendant, location: offense_range(descendant))
51
+ end
52
+ end
53
+
54
+ def autocorrect(node)
55
+ lambda do |corrector|
56
+ corrector.replace(offense_range(node), preferred_replacement)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def inside_processes_spec?(root)
63
+ spec?(root) && processes_resource?(root)
64
+ end
65
+
66
+ def preferred_replacement
67
+ cop_config.fetch('PreferredReplacement')
68
+ end
69
+
70
+ def offense_range(node)
71
+ source = node.children[0].children[-1].loc.expression
72
+ range_between(source.begin_pos+1, source.end_pos-1)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # script has been deprecated as a resource. Use powershell instead
8
+ #
9
+ class ScriptResource < Cop
10
+ MSG = 'Use `powershell` instead of `script`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :spec?, <<-PATTERN
14
+ (block
15
+ (send nil? :describe ...)
16
+ ...)
17
+ PATTERN
18
+
19
+ def_node_matcher :script?, <<~PATTERN
20
+ (send nil? :script ...)
21
+ PATTERN
22
+
23
+ def on_send(node)
24
+ return unless inside_spec?(node)
25
+ return unless script?(node)
26
+
27
+ add_offense(node, location: :selector)
28
+ end
29
+
30
+ def autocorrect(node)
31
+ lambda do |corrector|
32
+ corrector.replace(node.loc.selector, preferred_replacement)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def inside_spec?(root)
39
+ spec?(root)
40
+ end
41
+
42
+ def preferred_replacement
43
+ cop_config.fetch('PreferredReplacement')
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # NOTE TO SELF - this one works BUT not if other its statements are defined. Needs
4
- # to work in any arrangement. This is a powerful one to crack as this pattern
5
- # will be used in a LOT of other cops.
6
-
7
3
  module RuboCop
8
4
  module Cop
9
5
  module InSpecStyle
10
- # Shadow resource property user is deprecated in favor of `users`
6
+ # Shadow resource properties `user|password|last_change|expiry_date|line` is deprecated in favor of `users|passwords|last_changes|expiry_dates|lines`
11
7
  #
12
8
  # @example EnforcedStyle: InSpecStyle (default)
13
9
  # # Use users instead
@@ -23,37 +19,63 @@ module RuboCop
23
19
  # end
24
20
  #
25
21
  class ShadowProperties < Cop
26
- # TODO: Implement the cop in here.
27
- #
28
- # In many cases, you can use a node matcher for matching node pattern.
29
- # See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/node_pattern.rb
30
- #
31
- # For example
32
- MSG = 'Use `:%<modifier>ss` instead of `:%<modifier>s` as a property ' \
22
+ include RangeHelp
23
+
24
+ MSG = 'Use `:%<violation>ss` instead of `:%<violation>s` as a property ' \
33
25
  'for the `shadow` resource. This property will be removed in InSpec 5'
34
26
 
35
- def_node_matcher :shadow_resource_user_property?, <<~PATTERN
27
+ def_node_matcher :deprecated_shadow_property?, <<~PATTERN
36
28
  (block
37
- (send _ :describe
38
- (send _ :shadow ...) ...)
39
- (args ...)
40
- (block
41
- (send _ :its
42
- (str ${"user" "password" "last_change" "expiry_date" "line"} ...) ...) ...) ...)
29
+ (send _ :its
30
+ (str ${"user" "password" "last_change" "expiry_date" "line"}) ...)
31
+ ...)
32
+ PATTERN
33
+
34
+ def_node_matcher :spec?, <<-PATTERN
35
+ (block
36
+ (send nil? :describe ...)
37
+ ...)
38
+ PATTERN
39
+
40
+ def_node_matcher :shadow_resource?, <<-PATTERN
41
+ (block
42
+ (send nil? :describe
43
+ (send nil? :shadow ...)
44
+ ...)
45
+ ...)
43
46
  PATTERN
44
47
 
45
48
  def on_block(node)
46
- return unless shadow_resource_user_property?(node) do |modifier|
47
- message = format(MSG, modifier: modifier)
48
- range = locate_range(modifier, node)
49
- add_offense(node, message: message, location: range)
49
+ return unless inside_shadow_spec?(node)
50
+ node.descendants.each do |descendant|
51
+ deprecated_shadow_property?(descendant) do |violation|
52
+ add_offense(
53
+ descendant,
54
+ location: offense_range(descendant),
55
+ message: format(
56
+ MSG,
57
+ violation: violation
58
+ )
59
+ )
60
+ end
61
+ end
62
+ end
63
+
64
+ def autocorrect(node)
65
+ lambda do |corrector|
66
+ corrector.insert_after(offense_range(node), 's')
50
67
  end
51
68
  end
52
69
 
53
70
  private
54
71
 
55
- def locate_range(modifier, node)
56
- node.children.find { |child| child.type == :block }.children.first.children.find{|x| x == s(:str, modifier)}.source_range
72
+ def inside_shadow_spec?(root)
73
+ spec?(root) && shadow_resource?(root)
74
+ end
75
+
76
+ def offense_range(node)
77
+ source = node.children[0].children[-1].loc.expression
78
+ range_between(source.begin_pos+1, source.end_pos-1)
57
79
  end
58
80
  end
59
81
  end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NOTE TO SELF - this one works BUT not if other its statements are defined. Needs
4
+ # to work in any arrangement. This is a powerful one to crack as this pattern
5
+ # will be used in a LOT of other cops.
6
+
7
+ module RuboCop
8
+ module Cop
9
+ module InSpecStyle
10
+ # Users resource deprecated matchers
11
+ #
12
+ # @example EnforcedStyle: InSpecStyle (default)
13
+ #
14
+ # # bad
15
+ # describe users('/etc/my-custom-place/users') do
16
+ # its('has_home_directory?') { should eq 'foo' }
17
+ # end
18
+ #
19
+ # # good
20
+ # describe users('/etc/my-custom-place/users') do
21
+ # its('users') { should eq 'foo' }
22
+ # end
23
+ #
24
+ class UsersResourceMatchers < Cop
25
+ MSG = 'Use `%<solution>s` instead of `%<violation>s` as a matcher ' \
26
+ "for the `users` resource. \nThis matcher will be removed in InSpec 5"
27
+
28
+ MAP = {
29
+ has_home_directory?: "its('home')",
30
+ has_login_shell?: "its('shell')",
31
+ has_authorized_key?: "another matcher",
32
+ maximum_days_between_password_change: :maxdays,
33
+ has_uid?: "another matcher",
34
+ minimum_days_between_password_change: "mindays"
35
+ }
36
+
37
+ def_node_matcher :deprecated_users_matcher?, <<~PATTERN
38
+ (send _ ${#{MAP.keys.map(&:inspect).join(' ')}} ...)
39
+ PATTERN
40
+
41
+ def_node_matcher :spec?, <<-PATTERN
42
+ (block
43
+ (send nil? :describe ...)
44
+ ...)
45
+ PATTERN
46
+
47
+ def_node_matcher :users_resource?, <<-PATTERN
48
+ (block
49
+ (send nil? :describe
50
+ (send nil? :users ...)
51
+ ...)
52
+ ...)
53
+ PATTERN
54
+
55
+ def on_block(node)
56
+ return unless inside_users_spec?(node)
57
+ node.descendants.each do |descendant|
58
+ deprecated_users_matcher?(descendant) do |violation|
59
+ add_offense(
60
+ descendant,
61
+ location: offense_range(descendant),
62
+ message: format(
63
+ MSG,
64
+ violation: violation,
65
+ solution: MAP[violation]
66
+ )
67
+ )
68
+ end
69
+ end
70
+ end
71
+
72
+ def autocorrect(node)
73
+ lambda do |corrector|
74
+ # Only these two matchers are autocorrectable
75
+ [
76
+ 'maximum_days_between_password_change',
77
+ 'minimum_days_between_password_change'
78
+ ].map do |violation|
79
+ if node.inspect.include?(violation)
80
+ corrector.replace(node.loc.selector, MAP[violation.to_sym])
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def inside_users_spec?(root)
89
+ spec?(root) && users_resource?(root)
90
+ end
91
+
92
+ def offense_range(node)
93
+ node.loc.selector
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # windows_registry_key has been deprecated as a resource. Use registry_key instead
8
+ #
9
+ class WindowsRegistryKey < Cop
10
+ MSG = 'Use `registry_key` instead of `windows_registry_key`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :windows_registry_key?, <<~PATTERN
14
+ (send _ :windows_registry_key ...)
15
+ PATTERN
16
+
17
+ def on_send(node)
18
+ return unless windows_registry_key?(node)
19
+ add_offense(node, location: :selector)
20
+ end
21
+
22
+ def autocorrect(node)
23
+ lambda do |corrector|
24
+ corrector.replace(node.loc.selector, preferred_replacement)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def preferred_replacement
31
+ cop_config.fetch('PreferredReplacement')
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # wmi_wmis_class has been deprecated as a resource. Use iis_site instead
8
+ #
9
+ class WmiWmisClass < Cop
10
+ MSG = 'Use `iis_site` instead of `wmi_wmis_class`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :wmi_wmis_class?, <<~PATTERN
14
+ (send _ :wmi
15
+ (str "wmisclass") ...)
16
+ PATTERN
17
+
18
+ def on_send(node)
19
+ return unless wmi_wmis_class?(node)
20
+ add_offense(node)
21
+ end
22
+
23
+ def autocorrect(node)
24
+ lambda do |corrector|
25
+ corrector.replace(node.source_range, preferred_replacement)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def preferred_replacement
32
+ cop_config.fetch('PreferredReplacement')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,6 +1,19 @@
1
1
  # frozen_string_literal: true
2
- require_relative 'inspecstyle/first_cop'
3
2
  require_relative 'inspecstyle/deprecated_attributes'
4
3
  require_relative 'inspecstyle/azure_generic_resource'
5
4
  require_relative 'inspecstyle/shadow_properties'
6
5
  require_relative 'inspecstyle/oracle_db_session_pass'
6
+ require_relative 'inspecstyle/file_size'
7
+ require_relative 'inspecstyle/apache'
8
+ require_relative 'inspecstyle/script_resource'
9
+ require_relative 'inspecstyle/users_resource_matchers'
10
+ require_relative 'inspecstyle/iis_website'
11
+ require_relative 'inspecstyle/host_proto'
12
+ require_relative 'inspecstyle/linux_kernel_parameter'
13
+ require_relative 'inspecstyle/mssql_session_pass'
14
+ require_relative 'inspecstyle/ppa_resource'
15
+ require_relative 'inspecstyle/processes_list'
16
+ require_relative 'inspecstyle/windows_registry_key'
17
+ require_relative 'inspecstyle/aws_iam_user_property'
18
+ require_relative 'inspecstyle/file_be_mounted'
19
+ require_relative 'inspecstyle/wmi_wmis_class'
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module InSpecStyle
3
- VERSION = "0.1.6"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -45,4 +45,11 @@ node.source
45
45
  NodePattern.new('(send ...)').match(node) # => true
46
46
  ```
47
47
 
48
+ - Implement CI
49
+
48
50
  Correction docs at rubocop's: lib/rubocop/cop/corrector.rb
51
+
52
+ ### Great Repos for cop examples
53
+ - rubocop-ast
54
+ - rubocop-rspec
55
+ - rubocop
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inspecstyle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Schwaderer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-12 00:00:00.000000000 Z
11
+ date: 2020-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -71,11 +71,24 @@ files:
71
71
  - doc/top-level-namespace.html
72
72
  - inspecstyle.gemspec
73
73
  - lib/inspecstyle.rb
74
+ - lib/rubocop/cop/inspecstyle/apache.rb
75
+ - lib/rubocop/cop/inspecstyle/aws_iam_user_property.rb
74
76
  - lib/rubocop/cop/inspecstyle/azure_generic_resource.rb
75
77
  - lib/rubocop/cop/inspecstyle/deprecated_attributes.rb
76
- - lib/rubocop/cop/inspecstyle/first_cop.rb
78
+ - lib/rubocop/cop/inspecstyle/file_be_mounted.rb
79
+ - lib/rubocop/cop/inspecstyle/file_size.rb
80
+ - lib/rubocop/cop/inspecstyle/host_proto.rb
81
+ - lib/rubocop/cop/inspecstyle/iis_website.rb
82
+ - lib/rubocop/cop/inspecstyle/linux_kernel_parameter.rb
83
+ - lib/rubocop/cop/inspecstyle/mssql_session_pass.rb
77
84
  - lib/rubocop/cop/inspecstyle/oracle_db_session_pass.rb
85
+ - lib/rubocop/cop/inspecstyle/ppa_resource.rb
86
+ - lib/rubocop/cop/inspecstyle/processes_list.rb
87
+ - lib/rubocop/cop/inspecstyle/script_resource.rb
78
88
  - lib/rubocop/cop/inspecstyle/shadow_properties.rb
89
+ - lib/rubocop/cop/inspecstyle/users_resource_matchers.rb
90
+ - lib/rubocop/cop/inspecstyle/windows_registry_key.rb
91
+ - lib/rubocop/cop/inspecstyle/wmi_wmis_class.rb
79
92
  - lib/rubocop/cop/inspecstyle_cops.rb
80
93
  - lib/rubocop/inspecstyle.rb
81
94
  - lib/rubocop/inspecstyle/inject.rb
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module InSpecStyle
6
- # TODO: Write cop description and example of bad / good code. For every
7
- # `SupportedStyle` and unique configuration, there needs to be examples.
8
- # Examples must have valid Ruby syntax. Do not use upticks.
9
- #
10
- # @example EnforcedStyle: bar (default)
11
- # # Description of the `bar` style.
12
- #
13
- # # bad
14
- # bad_bar_method
15
- #
16
- # # bad
17
- # bad_bar_method(args)
18
- #
19
- # # good
20
- # good_bar_method
21
- #
22
- # # good
23
- # good_bar_method(args)
24
- #
25
- # @example EnforcedStyle: foo
26
- # # Description of the `foo` style.
27
- #
28
- # # bad
29
- # bad_foo_method
30
- #
31
- # # bad
32
- # bad_foo_method(args)
33
- #
34
- # # good
35
- # good_foo_method
36
- #
37
- # # good
38
- # good_foo_method(args)
39
- #
40
- class FirstCop < Cop
41
- # TODO: Implement the cop in here.
42
- #
43
- # In many cases, you can use a node matcher for matching node pattern.
44
- # See https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/node_pattern.rb
45
- #
46
- # For example
47
- MSG = 'Use `#good_method` instead of `#bad_method`. %<example_insertion>'
48
-
49
- def_node_matcher :bad_method?, <<~PATTERN
50
- (send nil? :bad_method ...)
51
- PATTERN
52
-
53
- def on_send(node)
54
- return unless bad_method?(node)
55
- message = format(MSG, example_insertion: node.first.source)
56
- add_offense(node, message: message)
57
- end
58
-
59
- def autocorrect(node)
60
- ->(corrector) do
61
- corrector.insert_before(node.source_range, 'good_method')
62
- corrector.remove(node.source_range)
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end