puppet-lint-hiera_functions-check 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2fd789c616e42ae4aeacb3f858b19c2f4f741ac6ad2f8521d800d4fa3d66be0b
4
+ data.tar.gz: 4ee89c18633c97cf151d7dba494e270be8e51858bb2b1e98f661955d2efaf87b
5
+ SHA512:
6
+ metadata.gz: 31b75c0c6d1643c02de09ea34c5208014181728969f52265fde9ffb53ab4740df4b8adc21dac438151ec4fae418868267d600656948c58d098236762b95da935
7
+ data.tar.gz: b39eaaf4fe7c1024a525b3365de738dfc1c2ef471a446629dd8e828de3d5906b616bbf1566413199d7ccfc376fc101d11f5bd2f9f751781cc9d0fe4a19a484ab
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024, Christopher Bowman
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ puppet-lint-hiera_functions-check
2
+ =================================
3
+
4
+ [![License](https://img.shields.io/github/license/cbowman0/puppet-lint-hiera_functions-check.svg)](https://github.com/cbowman0/puppet-lint-hiera_functions-check/blob/master/LICENSE)
5
+ [![CI](https://github.com/cbowman0/puppet-lint-hiera_functions-check/actions/workflows/ci.yaml/badge.svg)](https://github.com/cbowman0/puppet-lint-hiera_functions-check/actions/workflows/ci.yaml)
6
+ [![RubyGem Version](https://img.shields.io/gem/v/puppet-lint-hiera_functions-check.svg)](https://rubygems.org/gems/puppet-lint-hiera_functions-check)
7
+ [![RubyGem Downloads](https://img.shields.io/gem/dt/puppet-lint-hiera_functions-check.svg)](https://rubygems.org/gems/puppet-lint-hiera_functions-check)
8
+
9
+ A puppet-lint plugin to check for deprecated hiera() function usage
10
+
11
+ ## Installing
12
+
13
+ ### From the command line
14
+
15
+ ```shell
16
+ $ gem install puppet-lint-hiera_functions-check
17
+ ```
18
+
19
+ ### In a Gemfile
20
+
21
+ ```ruby
22
+ gem 'puppet-lint-hiera_functions-check', :require => false
23
+ ```
24
+
25
+ ## Checks
26
+
27
+ ### Hiera function used
28
+
29
+ Usage of the hiera() function is deprecated
30
+
31
+ #### What you have done
32
+
33
+ ```puppet
34
+ $key = hiera('key')
35
+ $key = hiera_array('key')
36
+ $key = hiera_hash('key')
37
+ $key = hiera_include('key')
38
+ ```
39
+
40
+ #### What you should have done
41
+
42
+ ```puppet
43
+ $key = lookup('key')
44
+ $key = lookup('key', {merge => unique})
45
+ $key = lookup('key', {merge => hash})
46
+ $key = lookup('key', {merge => unique}).include
47
+ ```
48
+
49
+ #### Disabling the check
50
+
51
+ To disable this check, you can add `--no-hiera_functions-check` to your puppet-lint command line.
52
+
53
+ ```shell
54
+ $ puppet-lint --no-hiera_functions-check path/to/file.pp
55
+ ```
56
+
57
+ Alternatively, if you’re calling puppet-lint via the Rake task, you should insert the following line to your `Rakefile`.
58
+
59
+ ```ruby
60
+ PuppetLint.configuration.send('disable_hiear_functions')
61
+ ```
62
+
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Checks manifests for deprecated Hiera functions
4
+ # hiera(), hiera_arrya(), hiera_hash() and hiera_include()
5
+ #
6
+ # @see https://www.puppet.com/docs/puppet/8/hiera_migrate.html
7
+ #
8
+ PuppetLint.new_check(:hiera_functions) do
9
+ def check
10
+ # Get all the functions. The function_index in puppet-lint is supposed to do this,
11
+ # but it was always empty when I tried to use it
12
+ function_tokens = tokens.select { |token| token.type == :FUNCTION_NAME }
13
+
14
+ # Check each function for matching the hiera function list.
15
+ function_tokens.each do |function_token|
16
+ next unless function_token.value.match?(%r{^hiera(|_array|_hash|_include)$})
17
+
18
+ notify(
19
+ :warning,
20
+ message: "#{function_token.value} function is deprecated. Use lookup()",
21
+ line: function_token.line,
22
+ column: function_token.column,
23
+ token: function_token,
24
+ )
25
+ end
26
+ end
27
+
28
+ # Attempt the fix
29
+ #
30
+ # hiera('key') -> lookup('key')
31
+ # hiera_array('key') -> lookup('key', {merge => unique})
32
+ # hiera_hash('key') -> lookup('key', {merge => hash})
33
+ # hiera_include('key') -> lookup('key', {merge => unique}).include
34
+ #
35
+ def fix(problem)
36
+ case problem[:token].value
37
+ when 'hiera_array'
38
+ fix_hiera_with_merge(problem, 'unique')
39
+ when 'hiera_include'
40
+ fix_hiera_with_merge(problem, 'unique')
41
+ fix_hiera_append_include(problem)
42
+ when 'hiera_hash'
43
+ fix_hiera_with_merge(problem, 'hash')
44
+ end
45
+
46
+ # Always swap hiera() for lookup()
47
+ problem[:token].value = 'lookup'
48
+ end
49
+
50
+ private
51
+
52
+ def fix_hiera_with_merge(problem, merge_type)
53
+ index = tokens.index(problem[:token].next_token_of(:LPAREN).next_token_of(:RPAREN))
54
+
55
+ # Add the necessary tokens to the end in reverse order, so the index doesn't change =)
56
+ add_token(index, PuppetLint::Lexer::Token.new(:RBRACE, '}', 0, 0))
57
+ add_token(index, PuppetLint::Lexer::Token.new(:NAME, merge_type, 0, 0))
58
+ add_token(index, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
59
+ add_token(index, PuppetLint::Lexer::Token.new(:FARROW, '=>', 0, 0))
60
+ add_token(index, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
61
+ add_token(index, PuppetLint::Lexer::Token.new(:NAME, 'merge', 0, 0))
62
+ add_token(index, PuppetLint::Lexer::Token.new(:LBRACE, '{', 0, 0))
63
+ add_token(index, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
64
+ add_token(index, PuppetLint::Lexer::Token.new(:COMMA, ',', 0, 0))
65
+ end
66
+
67
+ def fix_hiera_append_include(problem)
68
+ # Get the index of the token _past_ the right paren
69
+ index = tokens.index(problem[:token].next_token_of(:LPAREN).next_token_of(:RPAREN)) + 1
70
+
71
+ # Add the tokens in reverse order so the index doesn't change
72
+ add_token(index, PuppetLint::Lexer::Token.new(:NAME, 'include', 0, 0))
73
+ add_token(index, PuppetLint::Lexer::Token.new(:DOT, '.', 0, 0))
74
+ end
75
+ end
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'hiera_functions' do
6
+ let(:msg) { 'function is deprecated. Use lookup()' }
7
+
8
+ context 'with fix disabled' do
9
+ context 'with hiera function' do
10
+ let(:code) do
11
+ <<-PP
12
+ class function_tester {
13
+ $test_key = hiera('test-key-name')
14
+ }
15
+ PP
16
+ end
17
+
18
+ it 'detects a single problem' do
19
+ expect(problems.size).to eq(1)
20
+ end
21
+
22
+ it 'creates warnings' do
23
+ expect(problems).to contain_warning("hiera #{msg}").on_line(2).in_column(23)
24
+ end
25
+ end
26
+
27
+ context 'with hiera_array function' do
28
+ let(:code) do
29
+ <<-PP
30
+ class function_tester {
31
+ $test_key = hiera_array('test-key-name')
32
+ }
33
+ PP
34
+ end
35
+
36
+ it 'detects a single problem' do
37
+ expect(problems.size).to eq(1)
38
+ end
39
+
40
+ it 'creates warnings' do
41
+ expect(problems).to contain_warning("hiera_array #{msg}").on_line(2).in_column(23)
42
+ end
43
+ end
44
+
45
+ context 'with hiera_hash function' do
46
+ let(:code) do
47
+ <<-PP
48
+ class function_tester {
49
+ $test_key = hiera_hash('test-key-name')
50
+ }
51
+ PP
52
+ end
53
+
54
+ it 'detects a single problem' do
55
+ expect(problems.size).to eq(1)
56
+ end
57
+
58
+ it 'creates warnings' do
59
+ expect(problems).to contain_warning("hiera_hash #{msg}").on_line(2).in_column(23)
60
+ end
61
+ end
62
+
63
+ context 'with hiera_include function' do
64
+ let(:code) do
65
+ <<-PP
66
+ class function_tester {
67
+ $test_key = hiera_include('test-key-name')
68
+ }
69
+ PP
70
+ end
71
+
72
+ it 'detects a single problem' do
73
+ expect(problems.size).to eq(1)
74
+ end
75
+
76
+ it 'creates warnings' do
77
+ expect(problems).to contain_warning("hiera_include #{msg}").on_line(2).in_column(23)
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'with fix enabled' do
83
+ before(:each) do
84
+ PuppetLint.configuration.fix = true
85
+ end
86
+
87
+ after(:each) do
88
+ PuppetLint.configuration.fix = false
89
+ end
90
+
91
+ context 'with hiera function' do
92
+ let(:code) do
93
+ <<-PP
94
+ class function_tester {
95
+ $hiera = hiera('test-key-name')
96
+ }
97
+ PP
98
+ end
99
+
100
+ let(:fixed_code) do
101
+ <<-PP
102
+ class function_tester {
103
+ $hiera = lookup('test-key-name')
104
+ }
105
+ PP
106
+ end
107
+
108
+ it 'detects a single problem' do
109
+ expect(problems).to contain_fixed("hiera #{msg}").on_line(2)
110
+ end
111
+
112
+ it 'changes hiera to lookup' do
113
+ expect(manifest).to eq(fixed_code)
114
+ end
115
+ end
116
+
117
+ context 'with hiera_array function' do
118
+ let(:code) do
119
+ <<-PP
120
+ class function_tester {
121
+ $test_key = hiera_array('test-key-name')
122
+ }
123
+ PP
124
+ end
125
+
126
+ let(:fixed_code) do
127
+ <<-PP
128
+ class function_tester {
129
+ $test_key = lookup('test-key-name', {merge => unique})
130
+ }
131
+ PP
132
+ end
133
+
134
+ it 'detects a single problem' do
135
+ expect(problems).to contain_fixed("hiera_array #{msg}").on_line(2)
136
+ end
137
+
138
+ it 'changes hiera to lookup' do
139
+ expect(manifest).to eq(fixed_code)
140
+ end
141
+ end
142
+
143
+ context 'with hiera_hash function' do
144
+ let(:code) do
145
+ <<-PP
146
+ class function_tester {
147
+ $test_key = hiera_hash('test-key-name')
148
+ }
149
+ PP
150
+ end
151
+
152
+ let(:fixed_code) do
153
+ <<-PP
154
+ class function_tester {
155
+ $test_key = lookup('test-key-name', {merge => hash})
156
+ }
157
+ PP
158
+ end
159
+
160
+ it 'detects a single problem' do
161
+ expect(problems).to contain_fixed("hiera_hash #{msg}").on_line(2)
162
+ end
163
+
164
+ it 'changes hiera to lookup' do
165
+ expect(manifest).to eq(fixed_code)
166
+ end
167
+ end
168
+
169
+ context 'with hiera_include function' do
170
+ let(:code) do
171
+ <<-PP
172
+ class function_tester {
173
+ $test_key = hiera_include('test-key-name')
174
+ }
175
+ PP
176
+ end
177
+
178
+ let(:fixed_code) do
179
+ <<-PP
180
+ class function_tester {
181
+ $test_key = lookup('test-key-name', {merge => unique}).include
182
+ }
183
+ PP
184
+ end
185
+
186
+ it 'detects a single problem' do
187
+ expect(problems).to contain_fixed("hiera_include #{msg}").on_line(2)
188
+ end
189
+
190
+ it 'changes hiera to lookup' do
191
+ expect(manifest).to eq(fixed_code)
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'puppet-lint'
4
+
5
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-hiera_functions-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Bowman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puppet-lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '13.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec-collection_matchers
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-its
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec-json_expectations
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.2'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.2'
103
+ - !ruby/object:Gem::Dependency
104
+ name: voxpupuli-rubocop
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ description: |2
118
+ A puppet-lint plugin to check for deprecated hieras function usage.
119
+
120
+ The legacy Hiera functions (hiera, hiera_array, hiera_hash, and hiera_include) should be replaced with lookup()
121
+ email: cbowman0@gmail.com
122
+ executables: []
123
+ extensions: []
124
+ extra_rdoc_files: []
125
+ files:
126
+ - LICENSE
127
+ - README.md
128
+ - lib/puppet-lint/plugins/hiera_functions.rb
129
+ - spec/puppet-lint/plugins/check_hiera_functions_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/cbowman0/puppet-lint-hiera_functions-check
132
+ licenses:
133
+ - MIT
134
+ metadata:
135
+ rubygems_mfa_required: 'true'
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 3.0.0
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubygems_version: 3.5.22
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A puppet-lint plugin to check for deprecated hiera functions usage.
155
+ test_files: []