File: exec_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (243 lines) | stat: -rw-r--r-- 11,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require 'spec_helper'
require 'puppet/provider/exec'
require 'puppet_spec/compiler'
require 'puppet_spec/files'

describe Puppet::Provider::Exec do
  include PuppetSpec::Compiler
  include PuppetSpec::Files

  describe "#extractexe" do
    it "should return the first element of an array" do
      expect(subject.extractexe(['one', 'two'])).to eq('one')
    end

    {
      # double-quoted commands
      %q{"/has whitespace"}            => "/has whitespace",
      %q{"/no/whitespace"}             => "/no/whitespace",
      # singe-quoted commands
      %q{'/has whitespace'}            => "/has whitespace",
      %q{'/no/whitespace'}             => "/no/whitespace",
      # combinations
      %q{"'/has whitespace'"}          => "'/has whitespace'",
      %q{'"/has whitespace"'}          => '"/has whitespace"',
      %q{"/has 'special' characters"}  => "/has 'special' characters",
      %q{'/has "special" characters'}  => '/has "special" characters',
      # whitespace split commands
      %q{/has whitespace}              => "/has",
      %q{/no/whitespace}               => "/no/whitespace",
    }.each do |base_command, exe|
      ['', ' and args', ' "and args"', " 'and args'"].each do |args|
        command = base_command + args
        it "should extract #{exe.inspect} from #{command.inspect}" do
          expect(subject.extractexe(command)).to eq(exe)
        end
      end
    end
  end

  context "when handling sensitive data" do
    before :each do
      Puppet[:log_level] = 'debug'
    end

    let(:supersecret) { 'supersecret' }
    let(:path) do
      if Puppet::Util::Platform.windows?
        # The `apply_compiled_manifest` helper doesn't add the `path` fact, so
        # we can't reference that in our manifest. Windows PATHs can contain
        # double quotes and trailing backslashes, which confuse HEREDOC
        # interpolation below. So sanitize it:
        ENV['PATH'].split(File::PATH_SEPARATOR).map do |dir|
          dir.gsub(/"/, '\"').gsub(/\\$/, '')
        end.join(File::PATH_SEPARATOR)
      else
        ENV['PATH']
      end
    end

    def ruby_exit_0
      "ruby -e 'exit 0'"
    end

    def echo_from_ruby_exit_0(message)
      # Escape double quotes due to HEREDOC interpolation below
      "ruby -e 'puts \"#{message}\"; exit 0'".gsub(/"/, '\"')
    end

    def echo_from_ruby_exit_1(message)
      # Escape double quotes due to HEREDOC interpolation below
      "ruby -e 'puts \"#{message}\"; exit 1'".gsub(/"/, '\"')
    end

    context "when validating the command" do
      it "redacts the arguments if the command is relative" do
        expect {
          apply_compiled_manifest(<<-MANIFEST)
            exec { 'echo':
              command => Sensitive.new('echo #{supersecret}')
            }
          MANIFEST
        }.to raise_error do |err|
          expect(err).to be_a(Puppet::Error)
          expect(err.message).to match(/'echo' is not qualified and no path was specified. Please qualify the command or specify a path./)
          expect(err.message).to_not match(/#{supersecret}/)
        end
      end

      it "redacts the arguments if the command is a directory" do
        dir = tmpdir('exec')
        apply_compiled_manifest(<<-MANIFEST)
          exec { 'echo':
            command => Sensitive.new('#{dir} #{supersecret}'),
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :err, message: /'#{dir}' is a directory, not a file/))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
      end

      it "redacts the arguments if the command isn't executable" do
        file = tmpfile('exec')
        Puppet::FileSystem.touch(file)
        Puppet::FileSystem.chmod(0644, file)

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'echo':
            command => Sensitive.new('#{file} #{supersecret}'),
          }
        MANIFEST
        # Execute permission works differently on Windows, but execute will fail since the
        # file doesn't have a valid extension and isn't a valid executable. The raised error
        # will be Errno::EIO, which is not useful. The Windows execute code needs to raise
        # Puppet::Util::Windows::Error so the Win32 error message is preserved.
        pending("PUP-3561 Needs to raise a meaningful Puppet::Error") if Puppet::Util::Platform.windows?
        expect(@logs).to include(an_object_having_attributes(level: :err, message: /'#{file}' is not executable/))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
      end

      it "redacts the arguments if the relative command cannot be resolved using the path parameter" do
        file = File.basename(tmpfile('exec'))
        dir = tmpdir('exec')

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'echo':
            command => Sensitive.new('#{file} #{supersecret}'),
            path    => "#{dir}",
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :err, message: /Could not find command '#{file}'/))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
      end
    end

    it "redacts the command on success" do
      command = echo_from_ruby_exit_0(supersecret)

      apply_compiled_manifest(<<-MANIFEST)
        exec { 'true':
          command => Sensitive.new("#{command}"),
          path    => "#{path}",
        }
      MANIFEST
      expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing '[redacted]'", source: /Exec\[true\]/))
      expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
      expect(@logs).to include(an_object_having_attributes(level: :notice, message: "executed successfully"))
      expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
    end

    it "redacts the command on failure" do
      command = echo_from_ruby_exit_1(supersecret)

      apply_compiled_manifest(<<-MANIFEST)
        exec { 'false':
          command => Sensitive.new("#{command}"),
          path    => "#{path}",
        }
      MANIFEST
      expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing '[redacted]'", source: /Exec\[false\]/))
      expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
      expect(@logs).to include(an_object_having_attributes(level: :err, message: "[command redacted] returned 1 instead of one of [0]"))
      expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
    end

    context "when handling checks" do
      let(:onlyifsecret) { "onlyifsecret" }
      let(:unlesssecret) { "unlesssecret" }

      it "redacts command and onlyif outputs" do
        onlyif = echo_from_ruby_exit_0(onlyifsecret)

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'true':
            command => Sensitive.new("#{ruby_exit_0}"),
            onlyif  => Sensitive.new("#{onlyif}"),
            path    => "#{path}",
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing check '[redacted]'"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing '[redacted]'", source: /Exec\[true\]/))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "[output redacted]"))
        expect(@logs).to include(an_object_having_attributes(level: :notice, message: "executed successfully"))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{onlyifsecret}/))
      end

      it "redacts the command that would have been executed but didn't due to onlyif" do
        command = echo_from_ruby_exit_0(supersecret)
        onlyif = echo_from_ruby_exit_1(onlyifsecret)

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'true':
            command => Sensitive.new("#{command}"),
            onlyif  => Sensitive.new("#{onlyif}"),
            path    => "#{path}",
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing check '[redacted]'"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "[output redacted]"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "'[command redacted]' won't be executed because of failed check 'onlyif'"))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{onlyifsecret}/))
      end

      it "redacts command and unless outputs" do
        unlesscmd = echo_from_ruby_exit_1(unlesssecret)

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'true':
            command => Sensitive.new("#{ruby_exit_0}"),
            unless  => Sensitive.new("#{unlesscmd}"),
            path    => "#{path}",
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing check '[redacted]'"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing '[redacted]'", source: /Exec\[true\]/))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "[output redacted]"))
        expect(@logs).to include(an_object_having_attributes(level: :notice, message: "executed successfully"))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{unlesssecret}/))
      end

      it "redacts the command that would have been executed but didn't due to unless" do
        command = echo_from_ruby_exit_0(supersecret)
        unlesscmd = echo_from_ruby_exit_0(unlesssecret)

        apply_compiled_manifest(<<-MANIFEST)
          exec { 'true':
            command => Sensitive.new("#{command}"),
            unless  => Sensitive.new("#{unlesscmd}"),
            path    => "#{path}",
          }
        MANIFEST
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing check '[redacted]'"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "Executing: '[redacted]'", source: "Puppet"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "[output redacted]"))
        expect(@logs).to include(an_object_having_attributes(level: :debug, message: "'[command redacted]' won't be executed because of failed check 'unless'"))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{supersecret}/))
        expect(@logs).to_not include(an_object_having_attributes(message: /#{unlesssecret}/))
      end
    end
  end
end