Skip to content

Commit 38126a3

Browse files
ci(appended): add second test suite
1 parent 751eff7 commit 38126a3

33 files changed

Lines changed: 921 additions & 9 deletions

.gitlab-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ rubocop:
137137
# default-rockylinux-8-tiamat-py3: {extends: '.test_instance'}
138138
default-debian-11-master-py3: {extends: '.test_instance'}
139139
default-debian-10-master-py3: {extends: '.test_instance'}
140-
default-debian-9-master-py3: {extends: '.test_instance'}
140+
appended-debian-9-master-py3: {extends: '.test_instance'}
141141
default-ubuntu-2004-master-py3: {extends: '.test_instance'}
142142
default-ubuntu-1804-master-py3: {extends: '.test_instance'}
143143
default-centos-8-master-py3: {extends: '.test_instance'}
144-
default-centos-7-master-py3: {extends: '.test_instance'}
144+
appended-centos-7-master-py3: {extends: '.test_instance'}
145145
default-fedora-34-master-py3: {extends: '.test_instance'}
146146
default-fedora-33-master-py3: {extends: '.test_instance'}
147147
default-opensuse-leap-153-master-py3: {extends: '.test_instance'}
148-
default-opensuse-leap-152-master-py3: {extends: '.test_instance'}
148+
appended-opensuse-leap-152-master-py3: {extends: '.test_instance'}
149149
default-opensuse-tmbl-latest-master-py3: {extends: '.test_instance'}
150150
default-amazonlinux-2-master-py3: {extends: '.test_instance'}
151151
default-oraclelinux-8-master-py3: {extends: '.test_instance'}
152-
default-oraclelinux-7-master-py3: {extends: '.test_instance'}
152+
appended-oraclelinux-7-master-py3: {extends: '.test_instance'}
153153
default-arch-base-latest-master-py3: {extends: '.test_instance'}
154154
default-gentoo-stage3-latest-master-py3: {extends: '.test_instance'}
155155
default-gentoo-stage3-systemd-master-py3: {extends: '.test_instance'}

kitchen.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,23 @@ suites:
448448
verifier:
449449
inspec_tests:
450450
- path: test/integration/default
451+
- name: appended
452+
provisioner:
453+
state_top:
454+
base:
455+
'*':
456+
- sudoers._mapdata
457+
- sudoers
458+
- sudoers.included
459+
pillars:
460+
top.sls:
461+
base:
462+
'*':
463+
- kitchen
464+
- sudoers
465+
pillars_from_files:
466+
kitchen.sls: test/salt/pillar/kitchen.sls
467+
sudoers.sls: test/salt/pillar/appended.sls
468+
verifier:
469+
inspec_tests:
470+
- path: test/integration/appended

sudoers/included/install.sls

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ include:
1818
1919
{% set included_files = sudoers.included_files %}
2020
{% for included_file, spec in included_files.items() -%}
21+
{%- if '/' not in included_file %}
22+
{%- set included_file = sudoers.includedir ~ '/' ~ included_file %}
23+
{%- endif %}
24+
2125
sudoers include {{ included_file }}:
2226
file.managed:
23-
{% if '/' in included_file %}
2427
- name: {{ included_file }}
25-
{% else %}
26-
- name: {{ sudoers.includedir }}/{{ included_file }}
27-
{% endif %}
2828
- user: root
2929
- group: {{ sudoers.group }}
3030
- mode: 440
@@ -40,9 +40,15 @@ sudoers include {{ included_file }}:
4040
- file: {{ sudoers.configpath }}/sudoers
4141
- require_in:
4242
- file: {{ sudoers.includedir }}
43+
4344
{% elif sudoers.append_included_files_to_endof_main_config %}
45+
46+
sudoers append {{ included_file }}:
4447
file.append:
4548
- name: {{ sudoers.configpath }}/sudoers
46-
- text: '#include {{ sudoers.configpath }}/sudoers.d/{{ included_file }}'
49+
- text: '#include {{ included_file }}'
50+
- require:
51+
- file: sudoers include {{ included_file }}
4752
{% endif %}
53+
4854
{% endfor %}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# InSpec Profile: `appended`
2+
3+
This shows the implementation of the `appended` InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).
4+
5+
## Verify a profile
6+
7+
InSpec ships with built-in features to verify a profile structure.
8+
9+
```bash
10+
$ inspec check appended
11+
Summary
12+
-------
13+
Location: appended
14+
Profile: profile
15+
Controls: 4
16+
Timestamp: 2019-06-24T23:09:01+00:00
17+
Valid: true
18+
19+
Errors
20+
------
21+
22+
Warnings
23+
--------
24+
```
25+
26+
## Execute a profile
27+
28+
To run all **supported** controls on a local machine use `inspec exec /path/to/profile`.
29+
30+
```bash
31+
$ inspec exec appended
32+
..
33+
34+
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
35+
8 examples, 0 failures
36+
```
37+
38+
## Execute a specific control from a profile
39+
40+
To run one control from the profile use `inspec exec /path/to/profile --controls name`.
41+
42+
```bash
43+
$ inspec exec appended --controls package
44+
.
45+
46+
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
47+
1 examples, 0 failures
48+
```
49+
50+
See an [example control here](https://github.com/inspec/inspec/blob/master/examples/profile/controls/example.rb).
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
require 'yaml'
4+
5+
control 'sudoers._mapdata' do
6+
title '`map.jinja` should match the reference file'
7+
8+
### Method
9+
# The steps below for each file appear convoluted but they are both required
10+
# and similar in nature:
11+
# 1. The earliest method was to simply compare the files textually but this often
12+
# led to false positives due to inconsistencies (e.g. spacing, ordering)
13+
# 2. The next method was to load the files back into YAML structures and then
14+
# compare but InSpec provided block diffs this way, unusable by end users
15+
# 3. The final step was to dump the YAML structures back into a string to use
16+
# for the comparison; this both worked and provided human-friendly diffs
17+
18+
### Comparison file for the specific platform
19+
### Static, adjusted as part of code contributions, as map data is changed
20+
# Strip the `platform[:finger]` version number down to the "OS major release"
21+
platform_finger = system.platform[:finger].split('.').first.to_s
22+
# Use that to set the path to the file (relative to the InSpec suite directory)
23+
mapdata_file_path = "_mapdata/#{platform_finger}.yaml"
24+
# Load the mapdata from profile, into a YAML structure
25+
# https://docs.chef.io/inspec/profiles/#profile-files
26+
mapdata_file_yaml = YAML.load(inspec.profile.file(mapdata_file_path))
27+
# Dump the YAML back into a string for comparison
28+
mapdata_file_dump = YAML.dump(mapdata_file_yaml)
29+
30+
### Output file produced by running the `_mapdata` state
31+
### Dynamic, generated during Kitchen's `converge` phase
32+
# Derive the location of the dumped mapdata (differs for Windows)
33+
output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
34+
# Use that to set the path to the file (absolute path, i.e. within the container)
35+
output_file_path = "#{output_dir}/salt_mapdata_dump.yaml"
36+
# Load the output into a YAML structure using InSpec's `yaml` resource
37+
# https://github.com/inspec/inspec/blob/49b7d10/lib/inspec/resources/yaml.rb#L29
38+
output_file_yaml = yaml(output_file_path).params
39+
# Dump the YAML back into a string for comparison
40+
output_file_dump = YAML.dump(output_file_yaml)
41+
42+
describe 'File content' do
43+
it 'should match profile map data exactly' do
44+
expect(output_file_dump).to eq(mapdata_file_dump)
45+
end
46+
end
47+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
control 'Sudoers configuration' do
4+
title 'should match desired lines'
5+
6+
describe file('/etc/sudoers') do
7+
it { should be_file }
8+
it { should be_owned_by 'root' }
9+
it { should be_grouped_into 'root' }
10+
its('content') { should include '#include /etc/sudoers.d/extra-file1' }
11+
its('content') { should include '#include /etc/sudoers.d/extra-file2' }
12+
its('content') { should include '#include /etc/sudoers.d/extra-file3' }
13+
end
14+
15+
describe file('/etc/sudoers.d/extra-file1') do
16+
it { should be_file }
17+
it { should be_owned_by 'root' }
18+
it { should be_grouped_into 'root' }
19+
its('mode') { should cmp '0440' }
20+
its('content') { should include 'foo ALL=(ALL) ALL' }
21+
end
22+
23+
describe file('/etc/sudoers.d/extra-file2') do
24+
it { should be_file }
25+
it { should be_owned_by 'root' }
26+
it { should be_grouped_into 'root' }
27+
its('mode') { should cmp '0440' }
28+
its('content') { should include '%bargroup ALL=(ALL) NOPASSWD: ALL' }
29+
end
30+
31+
describe file('/etc/sudoers.d/extra-file3') do
32+
it { should be_file }
33+
it { should be_owned_by 'root' }
34+
it { should be_grouped_into 'root' }
35+
its('mode') { should cmp '0440' }
36+
its('content') { should include '+other_netgroup ALL=(ALL) ALL' }
37+
end
38+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
control 'Sudo package' do
4+
title 'should be installed'
5+
6+
describe package('sudo') do
7+
it { should be_installed }
8+
end
9+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# yamllint disable rule:indentation rule:line-length
2+
# AlmaLinux-8
3+
---
4+
values:
5+
append_included_files_to_endof_main_config: true
6+
arch: amd64
7+
configpath: /etc
8+
execprefix: /usr/sbin
9+
group: root
10+
included_files:
11+
/etc/sudoers.d/extra-file1:
12+
users:
13+
foo:
14+
- ALL=(ALL) ALL
15+
/etc/sudoers.d/extra-file2:
16+
groups:
17+
bargroup:
18+
- 'ALL=(ALL) NOPASSWD: ALL'
19+
/etc/sudoers.d/extra-file3:
20+
netgroups:
21+
other_netgroup:
22+
- ALL=(ALL) ALL
23+
includedir: /etc/sudoers.d
24+
manage_main_config: false
25+
pkg: sudo
26+
purge_includedir: false
27+
users:
28+
kitchen:
29+
- 'ALL=(root) NOPASSWD: ALL'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# yamllint disable rule:indentation rule:line-length
2+
# AlmaLinux-8
3+
---
4+
values:
5+
append_included_files_to_endof_main_config: true
6+
arch: amd64
7+
configpath: /etc
8+
execprefix: /usr/sbin
9+
group: root
10+
included_files:
11+
/etc/sudoers.d/extra-file1:
12+
users:
13+
foo:
14+
- ALL=(ALL) ALL
15+
/etc/sudoers.d/extra-file2:
16+
groups:
17+
bargroup:
18+
- 'ALL=(ALL) NOPASSWD: ALL'
19+
/etc/sudoers.d/extra-file3:
20+
netgroups:
21+
other_netgroup:
22+
- ALL=(ALL) ALL
23+
includedir: /etc/sudoers.d
24+
manage_main_config: false
25+
pkg: sudo
26+
purge_includedir: false
27+
users:
28+
kitchen:
29+
- 'ALL=(root) NOPASSWD: ALL'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# yamllint disable rule:indentation rule:line-length
2+
# AlmaLinux-8
3+
---
4+
values:
5+
append_included_files_to_endof_main_config: true
6+
arch: amd64
7+
configpath: /etc
8+
execprefix: /usr/sbin
9+
group: root
10+
included_files:
11+
/etc/sudoers.d/extra-file1:
12+
users:
13+
foo:
14+
- ALL=(ALL) ALL
15+
/etc/sudoers.d/extra-file2:
16+
groups:
17+
bargroup:
18+
- 'ALL=(ALL) NOPASSWD: ALL'
19+
/etc/sudoers.d/extra-file3:
20+
netgroups:
21+
other_netgroup:
22+
- ALL=(ALL) ALL
23+
includedir: /etc/sudoers.d
24+
manage_main_config: false
25+
pkg: sudo
26+
purge_includedir: false
27+
users:
28+
kitchen:
29+
- 'ALL=(root) NOPASSWD: ALL'

0 commit comments

Comments
 (0)