added platform-name wildcard support

added unit test for platform-name wildcard
added doc for release wildcard
added doc for platform-name wildcard

Signed-off-by: superyarick <yarick@yarick.net>
This commit is contained in:
superyarick 2020-11-08 15:24:43 -05:00
parent 7eb694a6e7
commit c653ce343a
4 changed files with 56 additions and 3 deletions

View file

@ -115,8 +115,8 @@ inspec check examples/profile
Use the `supports` setting in the `inspec.yml` file to specify one (or more) platforms for which a profile is targeting. The list of supported platforms may contain the following:
- Use `platform-family` to restrict to a specific platform family.
- Use `platform-name` to restrict on a specific platform name.
- Use `release` to restrict to a specific platform version (used with platform-name).
- Use `platform-name` to restrict on a specific platform name (supports bash wildcards).
- Use `release` to restrict to a specific platform version (used with platform-name, supports bash wildcards).
- Use `platform` to restrict on either platform-name or platform-family.
For compatibility we support `os-name` and `os-family`. We recommend all users
@ -142,6 +142,15 @@ supports:
release: 14.04
```
and to target entire release of Ubuntu version 14.x
```YAML
name: ssh
supports:
- platform-name: ubuntu
release: 14.*
```
and to target the entire RedHat platform (including CentOS and Oracle Linux):
```YAML
@ -150,6 +159,14 @@ supports:
- platform-family: redhat
```
and to target the entire Windows 2019 platform family (including Datcenter and Core Servers):
```YAML
name: ssh
supports:
- platform-name: windows_server_2019*
```
and to target anything running on Amazon AWS:
```YAML
@ -170,6 +187,7 @@ supports:
- platform: aws
```
## Profile Dependencies
A Chef InSpec profile can bring in the controls and custom resources from another

View file

@ -81,7 +81,7 @@ module Inspec::Resources
when :os, :platform then
platform?(v)
when :os_name, :platform_name then
name == v
check_name(v)
when :release then
check_release(v)
end
@ -99,6 +99,16 @@ module Inspec::Resources
private
def check_name(value)
# allow wild card matching
if value.include?("*")
cleaned = Regexp.escape(value).gsub('\*', ".*?")
name =~ /#{cleaned}/
else
name == value
end
end
def check_release(value)
# allow wild card matching
if value.include?("*")

View file

@ -25,6 +25,8 @@ class MockLoader
mint17: { name: "linuxmint", family: "debian", release: "17.3", arch: "x86_64" },
mint18: { name: "linuxmint", family: "debian", release: "18", arch: "x86_64" },
windows: { name: "windows", family: "windows", release: "6.2.9200", arch: "x86_64" },
windows2016: { name: "windows_server_2016_datacenter", family: "windows", release: "10.0.14393", arch: "x86_64" },
windows2019: { name: "windows_server_2019_datacenter", family: "windows", release: "10.0.17763", arch: "x86_64" },
wrlinux: { name: "wrlinux", family: "redhat", release: "7.0(3)I2(2)", arch: "x86_64" },
solaris11: { name: "solaris", family: "solaris", release: "11", arch: "i386" },
solaris10: { name: "solaris", family: "solaris", release: "10", arch: "i386" },

View file

@ -98,4 +98,27 @@ describe "Inspec::Resources::Platform" do
]
_(resource).wont_be :supported?, supports
end
let(:resource2) { MockLoader.new(:windows2016).load_resource("platform") }
it "loads a profile which supports platform-name windows_server_2016*" do
supports = [
{ 'platform-name': "windows_server_2016*" },
]
_(resource2).must_be :supported?, supports
end
it "loads a profile which supports platform-name windows_server_2016*" do
supports = [
{ 'platform-name': "*2016*" },
]
_(resource2).must_be :supported?, supports
end
it "reject a profile which supports platform-name not matching regex windows_server_2016*" do
supports = [
{ 'platform-name': "*2019*" },
]
_(resource2).wont_be :supported?, supports
end
end