Skip to content

Commit 29190b8

Browse files
authored
Merge pull request #66 from eole/feature/support-systemd-sockets
feat(systemd): check sockets created by systemd
2 parents 946fa1f + 60a4177 commit 29190b8

File tree

7 files changed

+135
-9
lines changed

7 files changed

+135
-9
lines changed

test/integration/default/controls/socket_admin_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
describe libvirt_socket_admin do
1111
it { should exist }
1212
its('type') { should eq :socket }
13-
its('owner') { should eq 'root' }
14-
its('group') { should eq 'root' }
15-
its('mode') { should cmp '0700' }
13+
its('owner') { should eq libvirt_socket_admin.config_owner }
14+
its('group') { should eq libvirt_socket_admin.config_group }
15+
its('mode') { should cmp libvirt_socket_admin.config_mode }
1616
end
1717
end

test/integration/default/controls/socket_ro_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
describe libvirt_socket_ro do
1111
it { should exist }
1212
its('type') { should eq :socket }
13-
its('owner') { should eq 'root' }
14-
its('group') { should eq 'root' }
15-
its('mode') { should cmp '0777' }
13+
its('owner') { should eq libvirt_socket_ro.config_owner }
14+
its('group') { should eq libvirt_socket_ro.config_group }
15+
its('mode') { should cmp libvirt_socket_ro.config_mode }
1616
end
1717
end

test/integration/default/controls/socket_rw_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
describe libvirt_socket_rw do
1111
it { should exist }
1212
its('type') { should eq :socket }
13-
its('owner') { should eq 'root' }
14-
its('group') { should eq 'root' }
15-
its('mode') { should cmp '0770' }
13+
its('owner') { should eq libvirt_socket_rw.config_owner }
14+
its('group') { should eq libvirt_socket_rw.config_group }
15+
its('mode') { should cmp libvirt_socket_rw.config_mode }
1616
end
1717
end

test/integration/share/libraries/libvirt_socket_admin.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ class LibvirtSocketAdminResource < Inspec.resource(1)
1515

1616
def initialize
1717
@file = inspec.file('/var/run/libvirt/libvirt-admin-sock')
18+
@systemd_status = inspec.systemd_config('libvirtd-admin.socket')
19+
end
20+
21+
def config_owner
22+
if @systemd_status.active?
23+
@systemd_status.config('SocketUser') || 'root'
24+
else
25+
'root'
26+
end
27+
end
28+
29+
def config_group
30+
if @systemd_status.active?
31+
@systemd_status.config('SocketGroup') || 'root'
32+
else
33+
'root'
34+
end
35+
end
36+
37+
def config_mode
38+
if @systemd_status.active?
39+
@systemd_status.config('SocketMode') || '0666'
40+
else
41+
'0700'
42+
end
1843
end
1944

2045
# We could not inherit from Inspec::Resources::FileResource

test/integration/share/libraries/libvirt_socket_ro.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ class LibvirtSocketRoResource < Inspec.resource(1)
1515

1616
def initialize
1717
@file = inspec.file('/var/run/libvirt/libvirt-sock-ro')
18+
@systemd_status = inspec.systemd_config('libvirtd-ro.socket')
19+
end
20+
21+
def config_owner
22+
if @systemd_status.active?
23+
@systemd_status.config('SocketUser') || 'root'
24+
else
25+
'root'
26+
end
27+
end
28+
29+
def config_group
30+
if @systemd_status.active?
31+
@systemd_status.config('SocketGroup') || 'root'
32+
else
33+
'root'
34+
end
35+
end
36+
37+
def config_mode
38+
if @systemd_status.active?
39+
@systemd_status.config('SocketMode') || '0666'
40+
else
41+
'0777'
42+
end
1843
end
1944

2045
# We could not inherit from Inspec::Resources::FileResource

test/integration/share/libraries/libvirt_socket_rw.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ class LibvirtSocketRwResource < Inspec.resource(1)
1515

1616
def initialize
1717
@file = inspec.file('/var/run/libvirt/libvirt-sock')
18+
@systemd_status = inspec.systemd_config('libvirtd.socket')
19+
end
20+
21+
def config_owner
22+
if @systemd_status.active?
23+
@systemd_status.config('SocketUser') || 'root'
24+
else
25+
'root'
26+
end
27+
end
28+
29+
def config_group
30+
if @systemd_status.active?
31+
@systemd_status.config('SocketGroup') || 'root'
32+
else
33+
'root'
34+
end
35+
end
36+
37+
def config_mode
38+
if @systemd_status.active?
39+
@systemd_status.config('SocketMode') || '0666'
40+
else
41+
'0770'
42+
end
1843
end
1944

2045
# We could not inherit from Inspec::Resources::FileResource
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
# systemd_config.rb -- InSpec resource for systemd service configuration
4+
# Author: Daniel Dehennin <[email protected]>
5+
# Copyright (C) 2020 Daniel Dehennin <[email protected]>
6+
7+
class SystemdConfigResource < Inspec.resource(1)
8+
name 'systemd_config'
9+
10+
supports platform_name: 'debian'
11+
supports platform_name: 'ubuntu'
12+
supports platform_name: 'centos'
13+
supports platform_name: 'fedora'
14+
supports platform_name: 'opensuse'
15+
16+
def initialize(service_name)
17+
@service_name = service_name
18+
@service_config = read_systemd_show
19+
end
20+
21+
def enabled?
22+
@service_config.send('UnitFileState') == 'enabled'
23+
end
24+
25+
def active?
26+
@service_config.send('ActiveState') == 'active'
27+
end
28+
29+
def test
30+
@service_config.methods
31+
end
32+
33+
def config(param)
34+
@service_config.send(param)
35+
end
36+
37+
def read_systemd_show
38+
cmd_string = "systemctl show #{@service_name}"
39+
cmd = inspec.command(cmd_string)
40+
41+
unless cmd.exit_status.zero?
42+
raise Inspec::Exceptions::ResourceSkipped,
43+
"Error running '#{cmd_string}': #{cmd.stderr}"
44+
end
45+
46+
parse_options = {
47+
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
48+
}
49+
inspec.parse_config(cmd.stdout.strip, parse_options)
50+
end
51+
end

0 commit comments

Comments
 (0)