Merge pull request #5758 from inspec/nm/timezone-resource

Added Timezone Resource
This commit is contained in:
Clinton Wolfe 2022-01-09 20:45:15 -05:00 committed by GitHub
commit 3664b0c47d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,88 @@
+++
title = "timezone resource"
draft = false
gh_repo = "inspec"
platform = "linux"
[menu]
[menu.inspec]
title = "timezone"
identifier = "inspec/resources/os/timezone.md timezone resource"
parent = "inspec/resources/os"
+++
Use the `timezone` Chef InSpec audit resource to test timezone configurations of the system.
## Availability
### Installation
This resource is distributed along with Chef InSpec itself. You can use it automatically.
## Syntax
A `timezone` resource block fetches the time zone configuration of a system and compares the output with the test:
describe timezone do
its('property') { should eq 'expected value' }
end
where
- `'property'` is one of `identifier` , `name` and `time_offset`
- `'expected value'` tests the output of the command run on the system versus the expected output stated in the test
For example:
describe timezone do
its('identifier') { should eq 'Asia/Kolkata' }
its('name') { should eq 'IST' }
its('time_offset') { should eq '+0530' }
end
## Properties
### identifier
The `identifier` property verifies the time zone name of a system.
An example of checking the **identifier** for the Asia/Kolkata time zone name:
its('identifier') { should eq 'Asia/Kolkata' }
### name
The `name` property verifies the time zone of a system.
{{< note >}}
The `name` property accepts the time zone abbreviation on Linux systems and the full time zone name on Windows systems.
{{< /note >}}
An example of verifying that the time zone is set to IST on a Linux system:
its('name') { should eq 'IST' }
{{< note >}}
Several time zones share the same time zone abbreviation. Use one of the other properties to verify a specific time zone with a common abbreviation.
{{< /note >}}
An example of verifying that the time zone is set to India Standard Time on a Windows system:
its('name') { should eq 'India Standard Time' }
### time_offset
The `time_offset` property verifies the time offset of a system from UTC (Coordinated Universal Time).
An example of verifying that the **time_offset** is UTC+05:30:
its('time_offset') { should eq '+0530' }
## Matchers
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).

View file

@ -41,6 +41,7 @@ require "inspec/resources/cassandradb_session"
require "inspec/resources/cassandradb_conf"
require "inspec/resources/cassandra"
require "inspec/resources/crontab"
require "inspec/resources/timezone"
require "inspec/resources/dh_params"
require "inspec/resources/directory"
require "inspec/resources/docker"

View file

@ -0,0 +1,65 @@
require "inspec/resources/command"
module Inspec::Resources
class TimeZone < Cmd
name "timezone"
supports platform: "unix"
supports platform: "windows"
desc "Check for timezone configurations"
example <<~EXAMPLE
describe timezone do
its('identifier') { should eq 'Asia/Kolkata' }
its('name') { should eq 'IST' }
its('time_offset') { should eq '+0530' }
end
EXAMPLE
def initialize
@output = {}
os = inspec.os
cmd = if os.windows?
inspec.command("Get-TimeZone")
else
inspec.command("timedatectl status | grep -i 'Time zone'")
end
if cmd.exit_status != 0
raise Inspec::Exceptions::ResourceFailed, "Time Zone resource with error: #{cmd.stderr}"
else
if os.windows?
splitted_output = cmd.stdout.strip.gsub(/\r/, "").split("\n").select { |out| (out.include? "Id") || (out.include? "DisplayName") || (out.include? "BaseUtcOffset") }
@output["identifier"] = split_and_fetch_last(splitted_output[1])
@output["name"] = split_and_fetch_last(splitted_output[0])
@output["time_offset"] = split_and_fetch_last(splitted_output[2])
else
splitted_output = cmd.stdout.split(":")[-1]&.strip&.gsub(/[(),^]*/, "")&.split(" ") || []
@output["identifier"] = splitted_output[0]
@output["name"] = splitted_output[1]
@output["time_offset"] = splitted_output[2]
end
end
end
def identifier
@output["identifier"]
end
def name
@output["name"]
end
def time_offset
@output["time_offset"]
end
def to_s
"Time Zone resource"
end
private
def split_and_fetch_last(string_value)
string_value.split(" :")[-1].strip
end
end
end

6
test/fixtures/cmd/get-timezone vendored Normal file
View file

@ -0,0 +1,6 @@
Id : India Standard Time
DisplayName : (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
StandardName : India Standard Time
DaylightName : India Daylight Time
BaseUtcOffset : 05:30:00
SupportsDaylightSavingTime : False

View file

@ -0,0 +1 @@
Time zone: Asia/Kolkata (IST, +0530)

View file

@ -231,6 +231,7 @@ class MockLoader
"ps -o pid,vsz,rss,tty,stat,time,ruser,args" => cmd.call("ps-busybox"),
"env" => cmd.call("env"),
"${Env:PATH}" => cmd.call("$env-PATH"),
"timedatectl status | grep -i 'Time zone'" => cmd.call("timedatectl-timezone"),
# registry key test using winrm 2.0
"9417f24311a9dcd90f1b1734080a2d4c6516ec8ff2d452a2328f68eb0ed676cf" => cmd.call("reg_schedule"),
"Auditpol /get /subcategory:'User Account Management' /r" => cmd.call("auditpol"),
@ -268,6 +269,7 @@ class MockLoader
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call("get-package-firefox"),
"Get-Package -Name 'Ruby 2.1.6-p336-x64' | ConvertTo-Json" => cmd.call("get-package-ruby"),
'Get-Command "choco"' => empty.call,
"Get-TimeZone" => cmd.call("get-timezone"),
'sh -c \'type "choco"\'' => cmd_exit_1.call,
'(choco list --local-only --exact --include-programs --limit-output \'nssm\') -Replace "\|", "=" | ConvertFrom-StringData | ConvertTo-JSON' => cmd.call("choco-list-nssm"),
'(choco list --local-only --exact --include-programs --limit-output \'git\') -Replace "\|", "=" | ConvertFrom-StringData | ConvertTo-JSON' => empty.call,

View file

@ -0,0 +1,21 @@
require "helper"
require "inspec/resource"
require "inspec/resources/timezone"
describe "Inspec::Resources::TimeZone" do
it "verify time configurations" do
resource = MockLoader.new(:centos7).load_resource("timezone")
_(resource.identifier).must_equal "Asia/Kolkata"
_(resource.name).must_equal "IST"
_(resource.time_offset).must_equal "+0530"
_(resource.stderr).must_equal ""
end
it "verify time configurations" do
resource = MockLoader.new(:windows).load_resource("timezone")
_(resource.identifier).must_equal "(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi"
_(resource.name).must_equal "India Standard Time"
_(resource.time_offset).must_equal "05:30:00"
_(resource.stderr).must_equal ""
end
end