summaryrefslogtreecommitdiff
path: root/spec/unit/compliance/runner_spec.rb
blob: e8a08abfc16a5adbba43841e66a72f6e88d35b0c (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
require "spec_helper"
require "tmpdir"

describe Chef::Compliance::Runner do
  let(:logger) { double(:logger).as_null_object }
  let(:node) { Chef::Node.new(logger: logger) }

  let(:runner) do
    described_class.new.tap do |r|
      r.node = node
      r.run_id = "my_run_id"
    end
  end

  describe "#enabled?" do
    context "when the node is not available" do
      let(:runner) { described_class.new }
      it "is false because it needs the node to answer that question" do
        expect(runner).not_to be_enabled
      end
    end

    it "is true if the node attributes have audit profiles and the audit cookbook is not present, and the compliance mode attribute is nil" do
      node.normal["audit"]["profiles"]["ssh"] = { 'compliance': "base/ssh" }
      node.normal["audit"]["compliance_phase"] = nil

      expect(runner).to be_enabled
    end

    it "is true if the node attributes have audit profiles and the audit cookbook is not present, and the compliance mode attribute is true" do
      node.normal["audit"]["profiles"]["ssh"] = { 'compliance': "base/ssh" }
      node.normal["audit"]["compliance_phase"] = true

      expect(runner).to be_enabled
    end

    it "is false if the node attributes have audit profiles and the audit cookbook is not present, and the compliance mode attribute is false" do
      node.normal["audit"]["profiles"]["ssh"] = { 'compliance': "base/ssh" }
      node.normal["audit"]["compliance_phase"] = false

      expect(runner).not_to be_enabled
    end

    it "is false if the node attributes have audit profiles and the audit cookbook is present, and the complince mode attribute is nil" do
      stub_const("::Reporter::ChefAutomate", true)
      node.normal["audit"]["profiles"]["ssh"] = { 'compliance': "base/ssh" }
      node.normal["audit"]["compliance_phase"] = nil

      expect(runner).not_to be_enabled
    end

    it "is true if the node attributes have audit profiles and the audit cookbook is present, and the complince mode attribute is true" do
      stub_const("::Reporter::ChefAutomate", true)
      node.normal["audit"]["profiles"]["ssh"] = { 'compliance': "base/ssh" }
      node.normal["audit"]["compliance_phase"] = true

      expect(runner).to be_enabled
    end

    it "is false if the node attributes do not have audit profiles and the audit cookbook is not present, and the complince mode attribute is nil" do
      node.normal["audit"]["profiles"] = {}
      node.normal["audit"]["compliance_phase"] = nil

      expect(runner).not_to be_enabled
    end

    it "is false if the node attributes do not have audit profiles and the audit cookbook is present, and the complince mode attribute is nil" do
      stub_const("::Reporter::ChefAutomate", true)
      node.automatic["recipes"] = %w{ audit::default fancy_cookbook::fanciness tacobell::nachos }
      node.normal["audit"]["compliance_phase"] = nil

      expect(runner).not_to be_enabled
    end

    it "is false if the node attributes do not have audit attributes and the audit cookbook is not present, and the complince mode attribute is nil" do
      node.automatic["recipes"] = %w{ fancy_cookbook::fanciness tacobell::nachos }
      node.normal["audit"]["compliance_phase"] = nil

      expect(runner).not_to be_enabled
    end

    it "is true if the node attributes do not have audit profiles and the audit cookbook is not present, and the complince mode attribute is true" do
      node.normal["audit"]["profiles"] = {}
      node.normal["audit"]["compliance_phase"] = true

      expect(runner).to be_enabled
    end

    it "is true if the node attributes do not have audit profiles and the audit cookbook is present, and the complince mode attribute is true" do
      stub_const("::Reporter::ChefAutomate", true)
      node.automatic["recipes"] = %w{ audit::default fancy_cookbook::fanciness tacobell::nachos }
      node.normal["audit"]["compliance_phase"] = true

      expect(runner).to be_enabled
    end

    it "is true if the node attributes do not have audit attributes and the audit cookbook is not present, and the complince mode attribute is true" do
      node.automatic["recipes"] = %w{ fancy_cookbook::fanciness tacobell::nachos }
      node.normal["audit"]["compliance_phase"] = true
      expect(runner).to be_enabled
    end
  end

  describe "#inspec_profiles" do
    it "returns an empty list with no profiles defined" do
      expect(runner.inspec_profiles).to eq([])
    end

    it "converts from the attribute format to the format Inspec expects" do
      node.normal["audit"]["profiles"]["linux-baseline"] = {
        'compliance': "user/linux-baseline",
        'version': "2.1.0",
      }

      node.normal["audit"]["profiles"]["ssh"] = {
        'supermarket': "hardening/ssh-hardening",
      }

      expected = [
        {
          compliance: "user/linux-baseline",
          name: "linux-baseline",
          version: "2.1.0",
        },
        {
          name: "ssh",
          supermarket: "hardening/ssh-hardening",
        },
      ]

      expect(runner.inspec_profiles).to eq(expected)
    end

    it "raises a CMPL010 message when the profiles are in the old audit-cookbook format" do
      node.normal["audit"]["profiles"] = [
        {
          name: "Windows 2019 Baseline",
          compliance: "admin/windows-2019-baseline",
        },
      ]

      expect { runner.inspec_profiles }.to raise_error(/CMPL010:/)
    end
  end

  describe "#warn_for_deprecated_config_values!" do
    it "logs a warning when deprecated config values are present" do
      node.normal["audit"]["owner"] = "my_org"
      node.normal["audit"]["inspec_version"] = "90210"

      expect(logger).to receive(:warn).with(/config values 'inspec_version', 'owner' are not supported/)

      runner.warn_for_deprecated_config_values!
    end

    it "does not log a warning with no deprecated config values" do
      node.normal["audit"]["profiles"]["linux-baseline"] = {
        'compliance': "user/linux-baseline",
        'version': "2.1.0",
      }

      expect(logger).not_to receive(:warn)

      runner.warn_for_deprecated_config_values!
    end
  end

  describe "#reporter" do
    context "chef-server-automate reporter" do
      it "uses the correct URL when 'server' attribute is set" do
        Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org"
        node.normal["audit"]["server"] = "https://server_attribute_url.example.com/application/sub_application"

        reporter = runner.reporter("chef-server-automate")

        expect(reporter).to be_kind_of(Chef::Compliance::Reporter::ChefServerAutomate)
        expect(reporter.url).to eq(URI("https://server_attribute_url.example.com/application/sub_application/organizations/my_org/data-collector"))
      end

      it "falls back to chef_server_url for URL when 'server' attribute is not set" do
        Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org"

        reporter = runner.reporter("chef-server-automate")

        expect(reporter).to be_kind_of(Chef::Compliance::Reporter::ChefServerAutomate)
        expect(reporter.url).to eq(URI("https://chef_config_url.example.com/organizations/my_org/data-collector"))
      end
    end

  end

  describe "#load_and_validate! when compliance is enabled" do
    before do
      allow(runner).to receive(:enabled?).and_return(true)
    end

    it "raises CMPL003 when the reporter is not a supported reporter type" do
      node.normal["audit"]["reporter"] = [ "invalid" ]
      expect { runner.load_and_validate! }.to raise_error(/^CMPL003:/)
    end
    it "raises CMPL002 if the configured fetcher is not supported" do
      node.normal["audit"]["fetcher"] = "invalid"
      expect { runner.load_and_validate! }.to raise_error(/^CMPL002:/)
    end

    it "raises CMPL004 if both the inputs and attributes node attributes are set" do
      node.normal["audit"]["attributes"] = {
        "tacos" => "lunch",
      }
      node.normal["audit"]["inputs"] = {
        "tacos" => "lunch",
      }
      expect { runner.load_and_validate! }.to raise_error(/^CMPL011:/)
    end

    it "validates configured reporters" do
      node.normal["audit"]["reporter"] = [ "chef-automate" ]
      reporter_double = double("reporter", validate_config!: nil)
      expect(runner).to receive(:reporter).with("chef-automate").and_return(reporter_double)
      expect(runner).to receive(:reporter).with("cli").and_return(reporter_double)
      runner.load_and_validate!
    end

  end

  describe "#inspec_opts" do
    it "pulls inputs from the attributes setting" do
      node.normal["audit"]["attributes"] = {
        "tacos" => "lunch",
      }

      inputs = runner.inspec_opts[:inputs]

      expect(inputs["tacos"]).to eq("lunch")
    end

    it "pulls inputs from the inputs setting" do
      node.normal["audit"]["inputs"] = {
        "tacos" => "lunch",
      }

      inputs = runner.inspec_opts[:inputs]

      expect(inputs["tacos"]).to eq("lunch")
    end

    it "favors inputs over attributes" do
      node.normal["audit"]["attributes"] = {
        "tacos" => "dinner",
      }

      node.normal["audit"]["inputs"] = {
        "tacos" => "lunch",
      }

      inputs = runner.inspec_opts[:inputs]

      expect(inputs["tacos"]).to eq("lunch")
    end

    it "does not include chef_node in inputs by default" do
      node.normal["audit"]["attributes"] = {
        "tacos" => "lunch",
        "nachos" => "dinner",
      }

      inputs = runner.inspec_opts[:inputs]

      expect(inputs["tacos"]).to eq("lunch")
      expect(inputs.key?("chef_node")).to eq(true)
    end

    it "includes chef_node in inputs with chef_node_attribute_enabled set" do
      node.normal["audit"]["chef_node_attribute_enabled"] = true
      node.normal["audit"]["attributes"] = {
        "tacos" => "lunch",
        "nachos" => "dinner",
      }

      inputs = runner.inspec_opts[:inputs]

      expect(inputs["tacos"]).to eq("lunch")
      expect(inputs["chef_node"]["audit"]["reporter"]).to eq(nil)
      expect(inputs["chef_node"]["chef_environment"]).to eq("_default")
    end
  end

  describe "interval running" do
    let(:tempdir) { Dir.mktmpdir("chef-compliance-tests") }

    before do
      allow(runner).to receive(:report_timing_file).and_return("#{tempdir}/report_timing.json")
    end

    it "is disabled by default" do
      expect(runner.node["audit"]["interval"]["enabled"]).to be false
    end

    it "defaults to 24 hours / 1440 minutes" do
      expect(runner.node["audit"]["interval"]["time"]).to be 1440
    end

    it "runs when the timing file does not exist" do
      expect(runner).to receive(:report)
      runner.report_with_interval
    end

    it "runs when the timing file does not exist and intervals are enabled" do
      node.normal["audit"]["interval"]["enabled"] = true
      expect(runner).to receive(:report)
      runner.report_with_interval
    end

    it "runs when the timing file exists and has a recent timestamp" do
      FileUtils.touch runner.report_timing_file
      expect(runner).to receive(:report)
      runner.report_with_interval
    end

    it "does not runs when the timing file exists and has a recent timestamp and intervals are enabled" do
      node.normal["audit"]["interval"]["enabled"] = true
      FileUtils.touch runner.report_timing_file
      expect(runner).not_to receive(:report)
      runner.report_with_interval
    end

    it "does not runs when the timing file exists and has a recent timestamp and intervals are enabled" do
      node.normal["audit"]["interval"]["enabled"] = true
      FileUtils.touch runner.report_timing_file
      ten_minutes_ago = Time.now - 600
      File.utime ten_minutes_ago, ten_minutes_ago, runner.report_timing_file
      expect(runner).not_to receive(:report)
      runner.report_with_interval
    end

    it "runs when the timing file exists and has a recent timestamp and intervals are enabled and the time is short" do
      node.normal["audit"]["interval"]["enabled"] = true
      node.normal["audit"]["interval"]["time"] = 9
      FileUtils.touch runner.report_timing_file
      ten_minutes_ago = Time.now - 600
      File.utime ten_minutes_ago, ten_minutes_ago, runner.report_timing_file
      expect(runner).to receive(:report)
      runner.report_with_interval
    end
  end
end