puppet-ghostbuster 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ded6aa277f38f2383a92a014237dfcf53e4c5d08b9a31ddca68c04a1ba9d5a0
4
- data.tar.gz: 9bc03b84ae579a7d1c0c8087d19dcf1f9a096882e261126941b333f43bfc49df
3
+ metadata.gz: a49a8e2b82cb4e340e41a2767d047eb168864c83a723ab49d4087f5c1228b0e1
4
+ data.tar.gz: f8d07b03c0c73d9e1966884f91eeba42e0e5be2ecc2cfbb615cf2230f3291dbe
5
5
  SHA512:
6
- metadata.gz: 9626a929c8fa55a70ab0600dc807f83db204bcc23d4f9db39c9cd5eda4fe55fd93c6dc03e8eae680dba8ad678cfc1a2e8e031cdb2eb49e70d0815dc89b6831ef
7
- data.tar.gz: 8c664c1ecd7d405319f6567646104d7f7d9c96f47a17dd4049d7d1a1285999e5682c432e4fccfb121fa495e56147e3ae158079c4b479ece41f8831a301c3198e
6
+ metadata.gz: 561a9fc699dfe34e2ce6f25e7fb740df7121095bcca428bd00c4938a0a75c886e325f7eb0eca4c25a5b46a0577bf01f763e544bb657589e6d3bcdb30c8053da3
7
+ data.tar.gz: c9f876fa728ce126598afc6598088ec4919010cb0196b58a3aaf7ca2aef9f47b430c8aa6a70f41417f67dc2639e021a2127970ca005d26ab46ed2683bf1eed9f
@@ -76,7 +76,7 @@ jobs:
76
76
  uses: actions/download-artifact@v8
77
77
  with:
78
78
  name: gem-artifact
79
- - uses: rubygems/configure-rubygems-credentials@v2.1.0
79
+ - uses: rubygems/configure-rubygems-credentials@v2.2.0
80
80
  - name: Publish gem to rubygems.org
81
81
  shell: bash
82
82
  run: gem push *.gem
data/CHANGELOG.md CHANGED
@@ -2,9 +2,21 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- ## [2.2.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/2.2.0) (2026-10-08)
5
+ ## [2.3.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/2.3.0) (2026-12-04)
6
6
 
7
- [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.1.0...2.2.0)
7
+ [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.2.0...2.3.0)
8
+
9
+ **Implemented enhancements:**
10
+
11
+ - support check\_ghostbuster\_files for directories with recurse = 'remote' [\#97](https://github.com/voxpupuli/puppet-ghostbuster/pull/97) ([batmac](https://github.com/batmac))
12
+
13
+ **Fixed bugs:**
14
+
15
+ - fix: detect unused Hiera files with puppet-lint 5.1 [\#133](https://github.com/voxpupuli/puppet-ghostbuster/pull/133) ([rwaffen](https://github.com/rwaffen))
16
+
17
+ ## [v2.2.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/v2.2.0) (2026-10-08)
18
+
19
+ [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.1.0...v2.2.0)
8
20
 
9
21
  **Implemented enhancements:**
10
22
 
@@ -1,3 +1,3 @@
1
1
  class PuppetGhostbuster
2
- VERSION = '2.2.0'.freeze
2
+ VERSION = '2.3.0'.freeze
3
3
  end
@@ -35,7 +35,7 @@ PuppetLint.new_check(:ghostbuster_files) do
35
35
  query = "resources[title] {
36
36
  (parameters.source = 'puppet:///modules/#{module_name}/#{dir_name}'
37
37
  or parameters.source = 'puppet:///modules/#{module_name}/#{dir_name}/')
38
- and parameters.recurse = true
38
+ and (parameters.recurse = true or parameters.recurse = 'remote')
39
39
  and nodes { deactivated is null } }"
40
40
  return if puppetdb.client.request('', query).data.size > 0
41
41
 
@@ -1,4 +1,11 @@
1
1
  class PuppetLint::Checks
2
+ # puppet-lint 5.1 filters YAML checks through a frozen allowlist.
3
+ if const_defined?(:YAML_COMPATIBLE_CHECKS, false)
4
+ yaml_checks = const_get(:YAML_COMPATIBLE_CHECKS)
5
+ remove_const(:YAML_COMPATIBLE_CHECKS)
6
+ const_set(:YAML_COMPATIBLE_CHECKS, (yaml_checks | [:ghostbuster_hiera_files]).freeze)
7
+ end
8
+
2
9
  def load_data(path, content)
3
10
  lexer = PuppetLint::Lexer.new
4
11
  PuppetLint::Data.path = path
@@ -28,67 +35,114 @@ PuppetLint.new_check(:ghostbuster_hiera_files) do
28
35
  hiera.dig('defaults', 'datadir') || 'data'
29
36
  end
30
37
 
38
+ def hiera_datadir(hierarchy)
39
+ (hierarchy['datadir'] || default_datadir).chomp('/')
40
+ end
41
+
42
+ def hiera_data_file?(path)
43
+ # Only consider actual hiera data files (keep noise like .gitignore out).
44
+ %w[.yaml .yml .eyaml].include?(File.extname(path))
45
+ end
46
+
31
47
  def path_in_datadirs?(path)
32
48
  return false unless hiera
33
49
 
34
- @data_dirs ||= hiera['hierarchy'].map { |h| (h['datadir'] || default_datadir).chomp('/') }.uniq
35
- path.match?(%(./#{Regexp.union(@data_dirs)}/))
50
+ @data_dirs ||= hiera['hierarchy'].map { |h| hiera_datadir(h) }.uniq
51
+ # Match a data directory at the path start or after '/'.
52
+ # Regexp.union combines directory alternatives and escapes special characters.
53
+ # Example: ['data', 'private.d'] becomes %r{data|private\.d}.
54
+ path.match?(%r{(^|/)#{Regexp.union(@data_dirs)}/})
36
55
  end
37
56
 
38
- def regexprs
39
- regs = {}
57
+ def hiera_patterns
58
+ patterns = []
59
+
40
60
  hiera['hierarchy'].each do |hierarchy|
41
- path_datadir = Regexp.escape((hierarchy['datadir'] || default_datadir).chomp('/'))
61
+ path_datadir = hiera_datadir(hierarchy)
42
62
  ([*hierarchy['path']] + [*hierarchy['paths']]).each do |level|
63
+ next if level.nil? || level.empty?
64
+
43
65
  level = File.join(path_datadir, level)
44
- regex = Regexp.new(level.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^}]+\}/, '(.+)').gsub(/%\{[^}]+\}/, '.+'))
45
- facts = level.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
46
- regs[regex] = facts
66
+
67
+ # Extract the names inside %{...} placeholders.
68
+ # Example: 'data/%{trusted.certname}/%{facts.os}.yaml' becomes ['certname', ['fact', 'os']].
69
+ fields = level.scan(/%\{([^}]+)\}/).flatten.map { |placeholder| puppetdb_field(placeholder) }
70
+
71
+ # Replace escaped placeholders with captures for non-empty path values.
72
+ # Example: 'data/%{trusted.certname}.yaml' becomes the regex source 'data/(.+)\.yaml'.
73
+ escaped_level = Regexp.escape(level).gsub(/%\\\{[^}]+\\\}/, '(.+)')
74
+
75
+ patterns << { regex: Regexp.new(escaped_level), fields: fields }
47
76
  end
48
77
  end
49
- regs
78
+
79
+ patterns
80
+ end
81
+
82
+ def puppetdb_field(placeholder)
83
+ # Remove a leading Puppet top-scope prefix.
84
+ name = placeholder.sub(/^::/, '')
85
+
86
+ case name
87
+ when 'certname', 'clientcert', 'fqdn', 'trusted.certname'
88
+ 'certname'
89
+ # Capture the complete fact name after 'facts.' or 'server_facts.'.
90
+ # Example: 'facts.os' becomes ['fact', 'os']; 'server_facts.environment' becomes ['fact', 'environment'].
91
+ when /\A(?:facts|server_facts)\.(.+)\z/
92
+ ['fact', Regexp.last_match(1)]
93
+ # Match names starting with 'trusted.'.
94
+ when /\Atrusted\./
95
+ # Other trusted fields cannot be queried reliably through PuppetDB.
96
+ nil
97
+ else
98
+ ['fact', name]
99
+ end
100
+ end
101
+
102
+ def coerce_fact_value(value)
103
+ case value
104
+ when 'true'
105
+ true
106
+ when 'false'
107
+ false
108
+ else
109
+ value
110
+ end
50
111
  end
51
112
 
52
113
  def check
53
- return unless path_in_datadirs? path
114
+ # Provides the path of the file being analysed as it was provided to puppet-lint to the check plugins.
115
+ # path() is a method
116
+ return unless path_in_datadirs?(path) && hiera_data_file?(path)
54
117
 
55
118
  puppetdb = PuppetGhostbuster::PuppetDB.new
119
+ matched_supported = false
120
+ matched_unsupported = false
56
121
 
57
- regexprs.each do |k, v|
58
- m = path.match(k)
59
- next if m.nil?
60
-
61
- return if m.captures.size == 0
62
-
63
- if m.captures.size > 1
64
- i = 0
65
- query = [:and]
66
- m.captures.map do |value|
67
- query << if v[i] == 'certname'
68
- [:'=', v[i], value]
69
- else
70
- [:'=', ['fact', v[i]], value]
71
- end
72
- i += 1
73
- end
74
- elsif v[0] == 'certname'
75
- query = [:'=', 'certname', m.captures[0]]
76
- else
77
- value = if m.captures[0] == 'true'
78
- true
79
- elsif m.captures[0] == 'false'
80
- false
81
- else
82
- m.captures[0]
83
- end
84
- query = [:'=', ['fact', v[0]], value]
85
- end
122
+ hiera_patterns.each do |pattern|
123
+ match = path.match(pattern[:regex])
124
+ next unless match
86
125
 
87
- query = [:and, query, [:'=', 'deactivated', nil]]
126
+ if pattern[:fields].include?(nil)
127
+ matched_unsupported = true
128
+ next
129
+ end
88
130
 
89
- return if puppetdb.client.request('nodes', query).data.size > 0
131
+ matched_supported = true
132
+ conditions = pattern[:fields].each_with_index.map do |field, index|
133
+ value = match.captures[index]
134
+ value = coerce_fact_value(value) unless field == 'certname'
135
+ [:'=', field, value]
136
+ end
137
+ # Fixed paths such as common.yaml only require an active node.
138
+ conditions << [:'=', 'deactivated', nil]
139
+ query = conditions.reduce { |left, right| [:and, left, right] }
140
+ return if puppetdb.client.request('nodes', query).data.any?
90
141
  end
91
142
 
143
+ # Keep files whose only matches use unsupported trusted fields.
144
+ return if matched_unsupported && !matched_supported
145
+
92
146
  notify :warning, {
93
147
  message: "Hiera File #{path} seems unused",
94
148
  line: 1,
@@ -1,16 +1,21 @@
1
1
  [
2
2
  {
3
- "certname": "bar.example.com"
3
+ "certname": "bar.example.com",
4
+ "fact": {
5
+ "domain": "example.com"
6
+ }
4
7
  },
5
8
  {
6
9
  "certname": "virtual.example.com",
7
10
  "fact": {
11
+ "domain": "example.com",
8
12
  "is_virtual": true
9
13
  }
10
14
  },
11
15
  {
12
16
  "certname": "prod.example.com",
13
17
  "fact": {
18
+ "domain": "example.com",
14
19
  "environment": "production"
15
20
  }
16
21
  }
@@ -7,6 +7,92 @@ ENV['HIERA_YAML_PATH'] = './spec/fixtures/hiera.yaml'
7
7
  describe 'ghostbuster_hiera_files' do
8
8
  let(:code) { '' }
9
9
 
10
+ context 'when run through PuppetLint for a YAML file' do
11
+ let(:linter) { PuppetLint.new }
12
+ let(:code) { "---\ntest: true\n" }
13
+
14
+ before do
15
+ allow(ENV).to receive(:fetch).and_call_original
16
+ allow(ENV).to receive(:fetch).with('HIERA_YAML_PATH', false).and_return('./spec/fixtures/hiera.yaml')
17
+ allow(PuppetLint.configuration).to receive(:checks).and_return([:ghostbuster_hiera_files])
18
+ linter.path = path
19
+ linter.code = code
20
+ linter.run
21
+ end
22
+
23
+ context 'when the certname does not exist' do
24
+ let(:path) { './data/nodes/rwaffen.betadots.example.com.yaml' }
25
+
26
+ it 'warns about the unused file' do
27
+ expect(linter.problems).to contain_warning("Hiera File #{path} seems unused")
28
+ end
29
+ end
30
+
31
+ context 'when the certname exists' do
32
+ let(:path) { './data/nodes/bar.example.com.yaml' }
33
+
34
+ it 'does not report a problem' do
35
+ expect(linter.problems).to be_empty
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'with a custom hierarchy' do
41
+ before do
42
+ allow(YAML).to receive(:load_file).and_return('hierarchy' => [{ 'paths' => hierarchy_paths }])
43
+ end
44
+
45
+ context 'when multiple placeholders match the same node' do
46
+ let(:hierarchy_paths) { ['nodes/%{::trusted.certname}-%{facts.is_virtual}.yaml'] }
47
+ let(:path) { './data/nodes/virtual.example.com-true.yaml' }
48
+
49
+ it 'does not report a problem' do
50
+ expect(problems).to be_empty
51
+ end
52
+ end
53
+
54
+ context 'when the fact value does not match the node' do
55
+ let(:hierarchy_paths) { ['nodes/%{::trusted.certname}-%{facts.is_virtual}.yaml'] }
56
+ let(:path) { './data/nodes/virtual.example.com-false.yaml' }
57
+
58
+ it 'warns about the unused file' do
59
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
60
+ end
61
+ end
62
+
63
+ context 'with a fixed path' do
64
+ let(:hierarchy_paths) { ['common.yaml'] }
65
+ let(:path) { './data/common.yaml' }
66
+
67
+ it 'does not report a problem when active nodes exist' do
68
+ expect(problems).to be_empty
69
+ end
70
+
71
+ it 'warns when no active nodes exist' do
72
+ allow(PuppetGhostbuster::PuppetDB.client).to receive(:request).and_return(PuppetDB::Response.new([], 0))
73
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
74
+ end
75
+ end
76
+
77
+ context 'with an unsupported trusted field' do
78
+ let(:hierarchy_paths) { ['nodes/%{trusted.extensions.role}.yaml'] }
79
+ let(:path) { './data/nodes/unknown.yaml' }
80
+
81
+ it 'keeps potentially used files' do
82
+ expect(problems).to be_empty
83
+ end
84
+ end
85
+
86
+ context 'when supported and unsupported hierarchy entries match' do
87
+ let(:hierarchy_paths) { ['nodes/%{trusted.extensions.role}.yaml', 'nodes/%{trusted.certname}.yaml'] }
88
+ let(:path) { './data/nodes/unknown.yaml' }
89
+
90
+ it 'warns when the supported query finds no node' do
91
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
92
+ end
93
+ end
94
+ end
95
+
10
96
  context 'with fix disabled' do
11
97
  context 'when a certname file is NOT used' do
12
98
  let(:path) { './data/nodes/foo.example.com.yaml' }
data/spec/spec_helper.rb CHANGED
@@ -16,7 +16,7 @@ class PuppetDB::Client
16
16
 
17
17
  def process_pql_kv(q)
18
18
  k, v = q.split(/\s*=\s*/)
19
- ".#{k}=#{v}"
19
+ ".#{k.strip}=#{v.strip}"
20
20
  end
21
21
 
22
22
  def pql_to_jgrep(query)
@@ -34,6 +34,7 @@ class PuppetDB::Client
34
34
 
35
35
  jgrep_query_and_parts = []
36
36
  query.split(/\s+and\s+/).each do |q|
37
+ q.strip!
37
38
  newq = ''
38
39
  if q.start_with?('(')
39
40
  newq << '('
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-ghostbuster
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camptocamp