BACKPORT 6262: Prevent negative status from crashing launchd service resource (#6751)

* Prevent negative status from crashing launchd service resource (#6262)

Signed-off-by: dtheyer <davidheyer2016@gmail.com>

Signed-off-by: dtheyer <davidheyer2016@gmail.com>

* Add missing resource_id for service resource

Signed-off-by: Sonu Saha <sonu.saha@progress.com>

* Rebase with inspec-4 to get hab pipeline changes

Signed-off-by: Sonu Saha <sonu.saha@progress.com>

---------

Signed-off-by: dtheyer <davidheyer2016@gmail.com>
Signed-off-by: Sonu Saha <sonu.saha@progress.com>
Co-authored-by: dtheyer <davidheyer2016@gmail.com>
Co-authored-by: Sonu Saha <sonu.saha@progress.com>
This commit is contained in:
Nikita Mathur 2023-10-09 19:57:28 +05:30 committed by GitHub
parent 7bb16c3c22
commit 8e6395e037
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View file

@ -273,6 +273,10 @@ module Inspec::Resources
info[:startname] info[:startname]
end end
def resource_id
@service_name || "Service"
end
def to_s def to_s
"Service #{@service_name}" "Service #{@service_name}"
end end
@ -605,7 +609,7 @@ module Inspec::Resources
return nil if srv.nil? || srv[0].nil? return nil if srv.nil? || srv[0].nil?
# extract values from service # extract values from service
parsed_srv = /^(?<pid>[0-9-]+)\t(?<exit>[0-9]+)\t(?<name>\S*)$/.match(srv[0]) parsed_srv = /^(?<pid>[0-9-]+)\t(?<exit>[\-0-9]+)\t(?<name>\S*)$/.match(srv[0])
enabled = !parsed_srv["name"].nil? # it's in the list enabled = !parsed_srv["name"].nil? # it's in the list
# check if the service is running # check if the service is running

View file

@ -1,3 +1,4 @@
PID Status Label PID Status Label
2892 0 org.openbsd.ssh-agent 2892 0 org.openbsd.ssh-agent
- 0 com.apple.FilesystemUI - 0 com.apple.FilesystemUI
- -15 org.example.killed-agent

View file

@ -463,6 +463,21 @@ describe "Inspec::Resources::Service" do
_(resource.params).must_equal params _(resource.params).must_equal params
end end
it "verify mac osx service parsing with negative status launchd_service" do
resource = MockLoader.new(:macos10_10).load_resource(
"launchd_service", "org.example.killed-agent"
)
params = Hashie::Mash.new({})
_(resource.type).must_equal "darwin"
_(resource.name).must_equal "org.example.killed-agent"
_(resource.description).must_be_nil
_(resource.installed?).must_equal true
_(resource.enabled?).must_equal true
_(resource.running?).must_equal false
_(resource.params).must_equal params
_(resource.resource_id).must_equal "org.example.killed-agent"
end
# wrlinux # wrlinux
it "verify wrlinux service parsing" do it "verify wrlinux service parsing" do
resource = MockLoader.new(:wrlinux).load_resource("service", "sshd") resource = MockLoader.new(:wrlinux).load_resource("service", "sshd")