Use the user Chef InSpec audit resource to test user profiles for a single, known/expected local user, including the groups to which that user belongs, the frequency of required password changes, and the directory paths to home and shell.
This resource is distributed along with Chef InSpec itself. You can use it automatically.
This resource first became available in v1.0.0 of InSpec.
A user resource block declares a user name, and then one (or more) matchers:
describe user('root') do
it { should exist }
its('uid') { should eq 1234 }
its('gid') { should eq 1234 }
its('group') { should eq 'root' }
its('groups') { should eq ['root', 'other']}
its('home') { should eq '/root' }
its('shell') { should eq '/bin/bash' }
its('mindays') { should eq 0 }
its('maxdays') { should eq 90 }
its('warndays') { should eq 8 }
its('passwordage') { should eq 355 }
its('maxbadpasswords') { should eq nil } // Only valid on Windows OS
its('badpasswordattempts') { should eq 0 }
its('lastlogin') { should eq nil } // Only valid on Windows OS
end
where
('root') is the user to be testedit { should exist } tests if the user existsgid, group, groups, home, maxdays, mindays, shell, uid, warndays´, passwordage, maxbadpasswords, badpasswordattempts and lastlogin are valid matchers for this resourceThe gid property tests the group identifier:
its('gid') { should eq 1234 }
where 1234 represents the user identifier. The gid option is only available on Linux and will return nil for Windows os.
The group property tests the group to which the user belongs:
its('group') { should eq 'root' }
where root represents the group. The group option is only available on Linux and will return nil for Windows os.
The groups property tests two (or more) groups to which the user belongs:
its('groups') { should eq ['root', 'other'] }
The home property tests the home directory path for the user:
its('home') { should eq '/root' }
The maxdays property tests the maximum number of days between password changes:
its('maxdays') { should eq 99 }
where 99 represents the maximum number of days.
The mindays property tests the minimum number of days between password changes:
its('mindays') { should eq 0 }
where 0 represents the maximum number of days.
The shell property tests the path to the default shell for the user:
its('shell') { should eq '/bin/bash' }
The uid property tests the user identifier:
its('uid') { should eq 1234 }
where 1234 represents the user identifier.
The warndays property tests the number of days a user is warned before a password must be changed:
its('warndays') { should eq 5 }
where 5 represents the number of days a user is warned.
The passwordage property tests the number of days a user changed its password:
its('passwordage') { should_be <= 365 }
where 365 represents the number of days since the last password change.
The maxbadpasswords property tests the count of max badpassword settings for a specific user.
its('maxbadpasswords') { should eq 7 }
where 7 is the count of maximum bad password attempts.
The badpasswordattempts property tests the count of bad password attempts for a user.
its('badpasswordattempts') { should eq 0 }
where 0 is the count of bad passwords for a user. On Linux based operating systems it relies on lastb and for Windows it uses information stored for the user object. These settings will be reset to 0 depending on your operating system configuration.
The lastlogin property tests the last login date for a specific user.
its('lastlogin') { should eq nil }
The nil value means this user has never logged in.
The following examples show how to use this Chef InSpec audit resource.
describe user('root') do
it { should exist }
its('uid') { should eq 0 }
its('groups') { should eq ['root'] }
end
describe user('mysql') do
it { should_not exist }
end
The nginx user is typically www-data, but on CentOS it’s nginx. The following example shows how to test for the nginx user with a single test, but accounting for all platforms:
web_user = 'www-data'
web_user = 'nginx' if os[:family] == 'centos'
describe user(web_user) do
it { should exist }
end
For a full list of available matchers, please visit our matchers page.
The exist matcher tests if the named user exists:
it { should exist }
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/inspec/resources/user/