From 4008af0be83347612885296af9b40ce7e40de430 Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Mon, 6 Dec 2021 14:37:53 +0530 Subject: [PATCH] Implemented timezone resource to test timezone configuration values Signed-off-by: Nikita Mathur --- .../content/inspec/resources/timezone.md | 75 +++++++++++++++++++ lib/inspec/resources.rb | 1 + lib/inspec/resources/timezone.rb | 44 +++++++++++ test/fixtures/cmd/timedatectl-timezone | 1 + test/helpers/mock_loader.rb | 1 + test/unit/resources/timezone_test.rb | 13 ++++ 6 files changed, 135 insertions(+) create mode 100644 docs-chef-io/content/inspec/resources/timezone.md create mode 100644 lib/inspec/resources/timezone.rb create mode 100644 test/fixtures/cmd/timedatectl-timezone create mode 100644 test/unit/resources/timezone_test.rb diff --git a/docs-chef-io/content/inspec/resources/timezone.md b/docs-chef-io/content/inspec/resources/timezone.md new file mode 100644 index 000000000..b52f7901b --- /dev/null +++ b/docs-chef-io/content/inspec/resources/timezone.md @@ -0,0 +1,75 @@ ++++ +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. + +### Version + +This resource first became available in v1.0.0 of InSpec. + +## Syntax + +A `timezone` resource fetches the timezone configurations of the system and compares the output with the test: + + describe timezone do + its('property') { should eq 'expected value' } + end + +where + +- `'property'` is one of `identifier` , `abbreviation` 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('abbreviation') { should eq 'IST' } + its('time_offset') { should eq '+0530' } + end + +## Properties + +### identifier + +The `identifier` property returns the location identifier of the timezone. + +An example of checking the **identifier** for India Time : + + its('identifier') { should eq 'Asia/Kolkata } + +### abbreviation + +The `abbreviation` property returns the abbreviated representation of the timezone. + +An example of checking the **abbreviation** of India Time : + + its('abbreviation') { should eq 'IST' } + +### time_offset + +The `time_offset` property returns the identifier of a time offset from UTC (Coordinated Universal Time). + +An example of checking the **time_offset** of India Time: + + its('time_offset') { should eq '+0530' } + +## Matchers + +For a full list of available matchers, please visit our [matchers page](/inspec/matchers/). \ No newline at end of file diff --git a/lib/inspec/resources.rb b/lib/inspec/resources.rb index 486a19947..1620a9416 100644 --- a/lib/inspec/resources.rb +++ b/lib/inspec/resources.rb @@ -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" diff --git a/lib/inspec/resources/timezone.rb b/lib/inspec/resources/timezone.rb new file mode 100644 index 000000000..2ab579f7f --- /dev/null +++ b/lib/inspec/resources/timezone.rb @@ -0,0 +1,44 @@ +require "inspec/resources/command" + +module Inspec::Resources + class TimeZone < Cmd + name "timezone" + supports platform: "unix" + desc "Check for timezone configurations" + example <<~EXAMPLE + describe timezone do + its('identifier') { should eq 'Asia/Kolkata' } + its('abbreviation') { should eq 'IST' } + its('time_offset') { should eq '+0530' } + its('stderr') { should eq '' } + its('exit_status') { should eq 0 } + end + EXAMPLE + + def initialize + @output = [] + cmd = inspec.command("timedatectl status | grep -i 'Time zone'") + if cmd.exit_status != 0 + raise Inspec::Exceptions::ResourceFailed, "Time Zone resource with error: #{cmd.stderr}" + else + @output = cmd.stdout.split(":")[-1]&.strip&.gsub(/[(),^]*/, "")&.split(" ") || [] + end + end + + def identifier + @output[0] + end + + def abbreviation + @output[1] + end + + def time_offset + @output[2] + end + + def to_s + "Time Zone resource" + end + end +end diff --git a/test/fixtures/cmd/timedatectl-timezone b/test/fixtures/cmd/timedatectl-timezone new file mode 100644 index 000000000..5b9e4389f --- /dev/null +++ b/test/fixtures/cmd/timedatectl-timezone @@ -0,0 +1 @@ + Time zone: Asia/Kolkata (IST, +0530) \ No newline at end of file diff --git a/test/helpers/mock_loader.rb b/test/helpers/mock_loader.rb index 97d1d6fc4..3cfc65974 100644 --- a/test/helpers/mock_loader.rb +++ b/test/helpers/mock_loader.rb @@ -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"), diff --git a/test/unit/resources/timezone_test.rb b/test/unit/resources/timezone_test.rb new file mode 100644 index 000000000..ea91dc7ae --- /dev/null +++ b/test/unit/resources/timezone_test.rb @@ -0,0 +1,13 @@ +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.abbreviation).must_equal "IST" + _(resource.time_offset).must_equal "+0530" + _(resource.stderr).must_equal "" + end +end