mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
Add a Postfix-specific configuration testing resource (#4378)
Add a Postfix-specific configuration testing resource
This commit is contained in:
commit
8dc350bc6c
6 changed files with 126 additions and 0 deletions
76
docs/resources/postfix_conf.md.erb
Normal file
76
docs/resources/postfix_conf.md.erb
Normal 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/).
|
|
@ -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"
|
||||
|
|
31
lib/inspec/resources/postfix_conf.rb
Normal file
31
lib/inspec/resources/postfix_conf.rb
Normal 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
|
|
@ -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
|
||||
|
|
4
test/unit/mock/files/main.cf
Normal file
4
test/unit/mock/files/main.cf
Normal file
|
@ -0,0 +1,4 @@
|
|||
# Test main configuration for Postfix
|
||||
|
||||
test_parameter = value
|
||||
other_test_param = $value
|
13
test/unit/resources/postfix_conf_test.rb
Normal file
13
test/unit/resources/postfix_conf_test.rb
Normal 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
|
Loading…
Reference in a new issue