puppet-lint-legacy_fact-check 0.1.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: 04b65e777123a43554e8788a1b602c1529627f95e0237ab3dd5caae6700bebe4
4
+ data.tar.gz: 41a3fd9082bf4073d3e48e1e639975b35a9e85784585e0378ec410ae7176a78e
5
+ SHA512:
6
+ metadata.gz: 464b08bbff526379fd81cae724f9e2e1b512d86ab72f50dbb3bdc050cff75cb63b73d7c06b218e06d0e8e0b2cc438cb48324be6c94296f32908ce89273b0b0f8
7
+ data.tar.gz: 8350c3fd77953ad91c02c7126300c98d4560335c5e180e5e61a5a645ea531a8d0d569efa0baee717c43f241342eef8c0b1ef0381f817faf28a835c9b2b360f8e
@@ -0,0 +1,155 @@
1
+ PuppetLint.new_check(:legacy_fact) do
2
+ LEGACY_FACTS_STATIC = {
3
+ 'architecture' => "facts['os']['architecture']",
4
+ 'augeasversion' => "",
5
+ 'blockdevices' => "",
6
+ 'bios_release_date' => "",
7
+ 'bios_vendor' => "",
8
+ 'bios_version' => "",
9
+ 'boardassettag' => "",
10
+ 'boardmanufacturer' => "",
11
+ 'boardproductname' => "",
12
+ 'boardserialnumber' => "",
13
+ 'chassisassettag' => "",
14
+ 'chassistype' => "",
15
+ 'dhcp_servers' => "",
16
+ 'domain' => "facts['networking']['domain']",
17
+ 'fqdn' => "facts['networking']['fqdn']",
18
+ 'gid' => "",
19
+ 'hardwareisa' => "facts['os']['hardware']",
20
+ 'hardwaremodel' => "",
21
+ 'hostname' => "",
22
+ 'id' => "",
23
+ 'interfaces' => "",
24
+ 'ipaddress' => "",
25
+ 'ipaddress6' => "",
26
+ 'lsbdistcodename' => "",
27
+ 'lsbdistdescription' => "",
28
+ 'lsbdistid' => "",
29
+ 'lsbdistrelease' => "",
30
+ 'lsbmajdistrelease' => "",
31
+ 'lsbminordistrelease' => "",
32
+ 'lsbrelease' => "",
33
+ 'macaddress' => "",
34
+ 'macosx_buildversion' => "",
35
+ 'macosx_productname' => "",
36
+ 'macosx_productversion' => "",
37
+ 'macosx_productversion_major' => "",
38
+ 'macosx_productversion_minor' => "",
39
+ 'manufacturer' => "",
40
+ 'memoryfree' => "",
41
+ 'memoryfree_mb' => "",
42
+ 'memorysize' => "",
43
+ 'memorysize_mb' => "",
44
+ 'netmask' => "",
45
+ 'netmask6' => "",
46
+ 'network' => "",
47
+ 'network6' => "",
48
+ 'operatingsystem' => "facts['os']['release']",
49
+ 'operatingsystemmajrelease' => "facts['os']['release']['major']",
50
+ 'operatingsystemrelease' => "facts['os']['release']['full']",
51
+ 'osfamily' => "facts['os']['family']",
52
+ 'physicalprocessorcount' => "",
53
+ 'processorcount' => "",
54
+ 'productname' => "",
55
+ 'rubyplatform' => "",
56
+ 'rubysitedir' => "",
57
+ 'rubyversion' => "",
58
+ 'selinux' => "",
59
+ 'selinux_config_mode' => "",
60
+ 'selinux_config_policy' => "",
61
+ 'selinux_current_mode' => "",
62
+ 'selinux_enforced' => "",
63
+ 'selinux_policyversion' => "",
64
+ 'serialnumber' => "",
65
+ 'swapencrypted' => "",
66
+ 'swapfree' => "",
67
+ 'swapfree_mb' => "",
68
+ 'swapsize' => "",
69
+ 'swapsize_mb' => "",
70
+ 'system32' => "",
71
+ 'uptime' => "",
72
+ 'uptime_days' => "",
73
+ 'uptime_hours' => "",
74
+ 'uptime_seconds' => "",
75
+ 'uuid' => "",
76
+ 'xendomains' => "",
77
+ 'zonename' => "",
78
+ 'zones' => "",
79
+ }
80
+
81
+ LEGACY_FACTS_REGEX = {
82
+ /^blockdevice_(.+)_model$/ => "facts['disks']['%s']['model']",
83
+ /^blockdevice_(.+)_size$/ => "facts['disks']['%s']['size']",
84
+ /^blockdevice_(.+)_vendor$/ => "facts['disks']['%s']['vendor']",
85
+ /^ipaddress6_(.+)$/ => "facts['networking']['%s']['ip6']",
86
+ /^ipaddress_(.+)$/ => "facts['networking']['%s']['ip']",
87
+ /^ldom_(.+)$/ => "",
88
+ /^macaddress_(.+)$/ => "facts['networking']['%s']['mac']",
89
+ /^mtu_(.+)$/ => "facts['networking']['%s']['mtu']",
90
+ /^netmask6_(.+)$/ => "facts['networking']['%s']['netmask6']",
91
+ /^netmask_(.+)$/ => "facts['networking']['%s']['netmask']",
92
+ /^network6_(.+)$/ => "facts['networking']['%s']['network6']",
93
+ /^network_(.+)$/ => "facts['networking']['%s']['network']",
94
+ /^processor(.+)$/ => "facts['processors']['model'][%s]",
95
+ /^sp_(.+)$/ => "",
96
+ /^ssh(.+)key$/ => "facts['ssh']['%s']['key']",
97
+ /^sshfp_(.+)$/ => "", # TODO original is "facts['ssh']['%s']['sha256']" and sha1
98
+ /^zone_(.+)_brand$/ => "",
99
+ /^zone_(.+)_id$/ => "",
100
+ /^zone_(.+)_iptype$/ => "",
101
+ /^zone_(.+)_name$/ => "",
102
+ /^zone_(.+)_path$/ => "",
103
+ /^zone_(.+)_status$/ => "",
104
+ /^zone_(.+)_uuid$/ => "",
105
+ }
106
+
107
+ def check
108
+ tokens.select { |r|
109
+ Set[:VARIABLE, :UNENC_VARIABLE].include? r.type
110
+ }.each do |token|
111
+ if is_legacy_fact(normalize_fact(token.value))
112
+ notify :warning, {
113
+ :message => 'legacy fact used',
114
+ :line => token.line,
115
+ :column => token.column,
116
+ :token => token,
117
+ }
118
+ end
119
+ end
120
+ end
121
+
122
+ def fix(problem)
123
+ replacement = replacement_fact(normalize_fact(problem[:token].value))
124
+ raise PuppetLint::NoFix if replacement.nil? || replacement.empty?
125
+ problem[:token].value = replacement
126
+ end
127
+
128
+ private
129
+
130
+ def normalize_fact(fact)
131
+ fact.gsub(/^(::)?(.+)$/, '\2').gsub(/^facts\[("|')(.+)\1\]/, '\2')
132
+ end
133
+
134
+ def find_static_fact(fact)
135
+ LEGACY_FACTS_STATIC[fact]
136
+ end
137
+
138
+ def find_regex_fact(fact)
139
+ LEGACY_FACTS_REGEX.each do |pattern, replacement|
140
+ if (md = pattern.match(fact))
141
+ return replacement % md.captures
142
+ end
143
+ end
144
+
145
+ nil
146
+ end
147
+
148
+ def is_legacy_fact(fact)
149
+ !find_static_fact(fact).nil? || !find_regex_fact(fact).nil?
150
+ end
151
+
152
+ def replacement_fact(fact)
153
+ find_static_fact(fact) || find_regex_fact(fact)
154
+ end
155
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'legacy_fact' do
4
+ let(:msg) { 'legacy fact used' }
5
+
6
+ context 'with fix disabled' do
7
+ tests = [
8
+ ['top scoped', '$operatingsystem'],
9
+ ['absolute scoped', '$::operatingsystem'],
10
+ ['fact variable with single quotes', "$facts['operatingsystem']"],
11
+ ['fact variable with double quotes', '$facts["operatingsystem"]'],
12
+ ['regex-based variable', '$ipaddress_eth0'],
13
+ #['fact function with single quotes', "fact('operatingsystem')"],
14
+ #['fact function with double quotes', 'fact("operatingsystem")'],
15
+ ]
16
+ tests.each do |ctx, sample|
17
+ context "using #{ctx}: #{sample}" do
18
+ let(:code) { sample }
19
+
20
+ it 'should detect a single problem' do
21
+ expect(problems).to have(1).problem
22
+ end
23
+
24
+ it 'should create a warning' do
25
+ expect(problems).to contain_warning(msg).on_line(1).in_column(1)
26
+ end
27
+ end
28
+ end
29
+
30
+ context 'code using modern fact' do
31
+ let(:code) { "$facts['os']['name']" }
32
+
33
+ it 'should not detect any problems' do
34
+ expect(problems).to have(0).problems
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'with fix enabled' do
40
+ before do
41
+ PuppetLint.configuration.fix = true
42
+ end
43
+
44
+ after do
45
+ PuppetLint.configuration.fix = false
46
+ end
47
+
48
+ tests = [
49
+ ['top scoped', '$operatingsystem', "$facts['os']['release']"],
50
+ ['absolute scoped', '$::operatingsystem', "$facts['os']['release']"],
51
+ ['fact variable with single quotes', "$facts['operatingsystem']", "$facts['os']['release']"],
52
+ ['fact variable with double quotes', '$facts["operatingsystem"]', "$facts['os']['release']"],
53
+ ['regex-based variable', '$ipaddress_eth0', "$facts['networking']['eth0']['ip']"],
54
+ ]
55
+ tests.each do |ctx, sample, replacement|
56
+ context "using #{ctx}: #{sample}" do
57
+ let(:code) { sample }
58
+
59
+ it 'should detect a single problem' do
60
+ expect(problems).to have(1).problem
61
+ end
62
+
63
+ it 'should fix the problem' do
64
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(1)
65
+ end
66
+
67
+ it 'should add a newline to the end of the manifest' do
68
+ expect(manifest).to eq(replacement)
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'code using legacy fact without replacement' do
74
+ let(:code) { "$macosx_buildversion" }
75
+
76
+ it 'should detect a single problem' do
77
+ expect(problems).to have(1).problem
78
+ end
79
+
80
+ it 'should not fix the problem' do
81
+ expect(problems).to contain_warning(msg).on_line(1).in_column(1)
82
+ end
83
+
84
+ it 'should not modify the manifest' do
85
+ expect(manifest).to eq(code)
86
+ end
87
+ end
88
+
89
+ context 'code using modern fact' do
90
+ let(:code) { "$facts['os']['name']" }
91
+
92
+ it 'should not detect any problems' do
93
+ expect(problems).to have(0).problems
94
+ end
95
+
96
+ it 'should not modify the manifest' do
97
+ expect(manifest).to eq(code)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-legacy_fact-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ewoud Kohl van Wijngaarden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-16 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: " A puppet-lint plugin to check that manifest files don't use legacy
98
+ facts.\n"
99
+ email: ewoud+rubygems@kohlvanwijngaarden.nl
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/puppet-lint/plugins/legacy_fact.rb
105
+ - spec/puppet-lint/plugins/legacy_fact/legacy_fact_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/ekohl/puppet-lint-legacy_fact-check
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.0.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A puppet-lint plugin to check for legacy facts.
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/puppet-lint/plugins/legacy_fact/legacy_fact_spec.rb