Add a Postfix-specific configuration testing resource (#4378)

Add a Postfix-specific configuration testing resource
This commit is contained in:
Clinton Wolfe 2019-08-19 12:38:17 -04:00 committed by GitHub
commit 8dc350bc6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,76 @@
---
title: About the postfix_conf Resource
platform: os
---
# postfix_conf
Use the `postfix_conf` Chef InSpec audit resource to test the main configuration of the Postfix Mail Transfer Agent.
<br>
## Availability
### Installation
This resource is distributed along with Chef InSpec itself. You can use it automatically.
### Version
## Syntax
A `postfix_conf` resource block declares the configuration settings to be tested:
describe postfix_conf do
its('setting_name') { should cmp 'value' }
end
where
* `'setting_name'` is a setting key defined in main.cf
* `{ should cmp 'value' }` is the value to be expected
When using `postfix_conf` with a custom configuration directory, the following syntax can be used:
describe postfix_conf('path') do
...
end
where
* `'path'` is the path to your Postfix configuration (ex. '/etc/my/postfix/path/main.cf')
<br>
## Properties
This resource supports any of the settings listed in the main.cf file as properties.
<br>
## Examples
The following examples show how to use this Chef InSpec audit resource.
### Test basic Postfix configuration settings in the main.cf file
For example, the following Postfix configuration:
/etc/postfix/main.cf:
myorigin = $myhostname
myhostname = host.local.domain
mynetworks = 127.0.0.0/8
can be tested like this:
describe postfix_conf do
its('myorigin') { should cmp '$myhostname' }
its('myhostname') { should cmp 'host.local.domain' }
its('mynetworks') { should cmp '127.0.0.0/8' }
end
<br>
## Matchers
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).

View file

@ -84,6 +84,7 @@ require "inspec/resources/passwd"
require "inspec/resources/pip"
require "inspec/resources/platform"
require "inspec/resources/port"
require "inspec/resources/postfix_conf"
require "inspec/resources/postgres"
require "inspec/resources/postgres_conf"
require "inspec/resources/postgres_hba_conf"

View file

@ -0,0 +1,31 @@
require "inspec/resources/ini"
require "inspec/utils/simpleconfig"
module Inspec::Resources
class PostfixConf < IniConfig
name "postfix_conf"
supports platform: "linux"
desc "Use the postfix_conf Inspec audit resource to test the configuration of the Postfix Mail Transfer Agent"
# Allow user to specify a custom configuration path, use default Postfix configuration path if no custom path is provided
def initialize(*opts)
@params = {}
if opts.length == 1
@raw_content = load_raw_content(opts)
else
@raw_content = load_raw_content("/etc/postfix/main.cf")
end
@params = parse(@raw_content)
end
def parse(content)
SimpleConfig.new(content).params
end
private
def resource_base_name
"POSTFIX_CONF"
end
end
end

View file

@ -159,6 +159,7 @@ class MockLoader
"/fakepath/fakefile" => emptyfile.call,
"C:/fakepath/fakefile" => emptyfile.call,
"/etc/cron.d/crondotd" => mockfile.call("crondotd"),
"/etc/postfix/main.cf" => mockfile.call("main.cf"),
}
# create all mock commands

View file

@ -0,0 +1,4 @@
# Test main configuration for Postfix
test_parameter = value
other_test_param = $value

View file

@ -0,0 +1,13 @@
require "helper"
require "inspec/resource"
require "inspec/resources/postfix_conf"
describe "Inspec::Resources::Postfix_Conf" do
it "Test default parsing of main.cf on Centos 7" do
resource = MockLoader.new(:centos7).load_resource("postfix_conf")
result = { "test_parameter" => "value", "other_test_param" => "$value" }
_(resource.params).must_equal result
_(resource.value(%w{test_parameter})).must_equal "value"
end
end