diff --git a/lib/puppet-lint/plugins/check_file_source_rights.rb b/lib/puppet-lint/plugins/check_file_source_rights.rb new file mode 100644 index 0000000..680fbc7 --- /dev/null +++ b/lib/puppet-lint/plugins/check_file_source_rights.rb @@ -0,0 +1,31 @@ +PuppetLint.new_check(:source_without_rights) do + def token_attr(resource, name) + resource[:tokens].select do |t| + t.type == :NAME && t.value == name && \ + t.next_code_token && t.next_code_token.type == :FARROW + end + end + + def check_attr(resource, name, source_t) + if token_attr(resource, name).empty? + notify :warning, { + :message => "file with source missing #{name}", + :line => source_t.line, + :column => source_t.column, + :token => source_t, + } + end + end + + def check + resource_indexes.each do |r| + next unless r[:type].value == 'file' + source_t = token_attr(r, 'source') + next if source_t.empty? + + check_attr(r, 'owner', source_t[0]) + check_attr(r, 'group', source_t[0]) + check_attr(r, 'mode', source_t[0]) + end + end +end diff --git a/spec/puppet-lint/plugins/check_file_source_rights/source_without_rights_spec.rb b/spec/puppet-lint/plugins/check_file_source_rights/source_without_rights_spec.rb new file mode 100644 index 0000000..cfce43e --- /dev/null +++ b/spec/puppet-lint/plugins/check_file_source_rights/source_without_rights_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe 'source_without_rights' do + context 'when rights are passed' do + let(:code) do + <<-EOS + file { '/tmp/foo': + ensure => file, + owner => 'root', + group => '0', + mode => '0644', + source => 'puppet:///modules/bar/foo', + } + + file { '/tmp/bar': + ensure => file, + content => 'qux', + } + EOS + end + + it 'should not detect any problems' do + expect(problems).to have(0).problems + end + end + + context 'when rights are not passed' do + let(:code) do + <<-EOS + file { '/tmp/foo': + ensure => file, + source => 'puppet:///modules/bar/foo', + } + EOS + end + + it 'should detect 3 problems' do + expect(problems).to have(3).problems + end + + it 'should create warnings' do + expect(problems).to contain_warning('file with source missing owner').on_line(3).in_column(11) + expect(problems).to contain_warning('file with source missing group').on_line(3).in_column(11) + expect(problems).to contain_warning('file with source missing mode').on_line(3).in_column(11) + end + end +end