-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement an 'audit' reporter that will terminate Chef Client ru… (#380)
Implement an 'audit' reporter that will terminate Chef Client runs on profile failures
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# encoding: utf-8 | ||
|
||
module Reporter | ||
# | ||
# Used to raise an error on conformance failure | ||
# | ||
class AuditEnforcer | ||
def send_report(report) | ||
# iterate over each profile and control | ||
report[:profiles].each do |profile| | ||
next if profile[:controls].nil? | ||
profile[:controls].each do |control| | ||
next if control[:results].nil? | ||
control[:results].each do |result| | ||
raise "Audit #{control[:id]} has failed. Aborting chef-client run." if result[:status] == 'failed' | ||
end | ||
end | ||
end | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# Cookbook Name:: audit | ||
# Spec:: automate_spec | ||
# | ||
# Copyright 2016 Chef Software, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require 'spec_helper' | ||
require_relative '../../../libraries/reporters/audit-enforcer' | ||
|
||
describe 'Reporter::ChefAutomate methods' do | ||
before :each do | ||
@min_report = { | ||
"profiles": [ | ||
{ | ||
"controls": [ | ||
{ "id": 'c1', "results": [{ "status": 'passed' }] }, | ||
{ "id": 'c2', "results": [{ "status": 'passed' }] }, | ||
], | ||
}, | ||
], | ||
} | ||
@automate = Reporter::AuditEnforcer.new | ||
end | ||
|
||
it 'is not raising error for a successful InSpec report' do | ||
expect(@automate.send_report(@min_report)).to eq(true) | ||
end | ||
|
||
it 'is not raising error for an InSpec report with no controls' do | ||
expect(@automate.send_report({ "profiles": [{ "name": 'empty' }] })).to eq(true) | ||
end | ||
|
||
it 'is not raising error for an InSpec report with controls but no results' do | ||
expect(@automate.send_report({ "profiles": [{ "controls": [{ "id": 'empty' }] }] })).to eq(true) | ||
end | ||
|
||
it 'raises an error for a failed InSpec report' do | ||
@min_report[:profiles][0][:controls][1][:results][0][:status] = 'failed' | ||
expect { @automate.send_report(@min_report) }.to raise_error('Audit c2 has failed. Aborting chef-client run.') | ||
end | ||
end |