Class: Inspec::Resources::User

Inherits:
Object
  • Object
show all
Includes:
UserManagementSelector
Defined in:
lib/inspec/resources/users.rb

Overview

The ‘user` resource handles the special case where only one resource is required

describe user(‘root’) do

it { should exist }
its('uid') { should eq 0 }
its('gid') { should eq 0 }
its('group') { should eq 'root' }
its('groups') { should eq ['root', 'wheel']}
its('home') { should eq '/root' }
its('shell') { should eq '/bin/bash' }
its('mindays') { should eq 0 }
its('maxdays') { should eq 99 }
its('warndays') { should eq 5 }

end

The following Serverspec matchers are deprecated in favor for direct value access

describe user(‘root’) do

it { should belong_to_group 'root' }
it { should have_uid 0 }
it { should have_home_directory '/root' }
it { should  '/bin/bash' }
its('minimum_days_between_password_change') { should eq 0 }
its('maximum_days_between_password_change') { should eq 99 }

end

ServerSpec tests that are not supported:

describe user(‘root’) do

it { should have_authorized_key 'ssh-rsa ADg54...3434 [email protected]' }
its(:encrypted_password) { should eq 1234 }

end

Instance Method Summary collapse

Methods included from UserManagementSelector

#select_user_manager

Constructor Details

#initialize(username = nil) ⇒ User

Returns a new instance of User.



151
152
153
154
155
156
# File 'lib/inspec/resources/users.rb', line 151

def initialize(username = nil)
  @username = username
  # select user provider
  @user_provider = select_user_manager(inspec.os)
  return skip_resource "The `user` resource is not supported on your OS yet." if @user_provider.nil?
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/inspec/resources/users.rb', line 162

def disabled?
  identity[:disabled] == true unless identity.nil?
end

#enabled?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/inspec/resources/users.rb', line 166

def enabled?
  identity[:disabled] == false unless identity.nil?
end

#exists?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/inspec/resources/users.rb', line 158

def exists?
  !identity.nil? && !identity[:username].nil?
end

#gidObject



178
179
180
# File 'lib/inspec/resources/users.rb', line 178

def gid
  identity[:gid] unless identity.nil?
end

#groupnameObject Also known as: group



182
183
184
# File 'lib/inspec/resources/users.rb', line 182

def groupname
  identity[:groupname] unless identity.nil?
end

#groupsObject



187
188
189
# File 'lib/inspec/resources/users.rb', line 187

def groups
  identity[:groups] unless identity.nil?
end

#has_authorized_key?(_compare_key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


243
244
245
246
# File 'lib/inspec/resources/users.rb', line 243

def has_authorized_key?(_compare_key)
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `has_authorized_key?` matcher is deprecated. There is no currently implemented alternative")
  raise NotImplementedError
end

#has_home_directory?(compare_home) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
236
# File 'lib/inspec/resources/users.rb', line 233

def has_home_directory?(compare_home)
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `has_home_directory?` matcher is deprecated. Please use `its('home')`.")
  home == compare_home
end

#has_login_shell?(compare_shell) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
241
# File 'lib/inspec/resources/users.rb', line 238

def (compare_shell)
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `has_login_shell?` matcher is deprecated. Please use `its('shell')`.")
  shell == compare_shell
end

#has_uid?(compare_uid) ⇒ Boolean

implements rspec has matcher, to be compatible with serverspec @see: github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/has.rb

Returns:

  • (Boolean)


228
229
230
231
# File 'lib/inspec/resources/users.rb', line 228

def has_uid?(compare_uid)
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `has_uid?` matcher is deprecated.")
  uid == compare_uid
end

#homeObject



191
192
193
# File 'lib/inspec/resources/users.rb', line 191

def home
  meta_info[:home] unless meta_info.nil?
end

#maxdaysObject

returns the maximum days between password changes



205
206
207
# File 'lib/inspec/resources/users.rb', line 205

def maxdays
  credentials[:maxdays] unless credentials.nil?
end

#maximum_days_between_password_changeObject

implement ‘maxdays’ method to be compatible with serverspec



221
222
223
224
# File 'lib/inspec/resources/users.rb', line 221

def maximum_days_between_password_change
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `maximum_days_between_password_change` property is deprecated. Please use `maxdays`.")
  maxdays
end

#mindaysObject

returns the minimum days between password changes



200
201
202
# File 'lib/inspec/resources/users.rb', line 200

def mindays
  credentials[:mindays] unless credentials.nil?
end

#minimum_days_between_password_changeObject

implement ‘mindays’ method to be compatible with serverspec



215
216
217
218
# File 'lib/inspec/resources/users.rb', line 215

def minimum_days_between_password_change
  Inspec.deprecate(:resource_user_serverspec_compat, "The user resource `minimum_days_between_password_change` property is deprecated. Please use `mindays`.")
  mindays
end

#shellObject



195
196
197
# File 'lib/inspec/resources/users.rb', line 195

def shell
  meta_info[:shell] unless meta_info.nil?
end

#to_sObject



248
249
250
# File 'lib/inspec/resources/users.rb', line 248

def to_s
  "User #{@username}"
end

#uidObject



174
175
176
# File 'lib/inspec/resources/users.rb', line 174

def uid
  identity[:uid] unless identity.nil?
end

#usernameObject



170
171
172
# File 'lib/inspec/resources/users.rb', line 170

def username
  identity[:username] unless identity.nil?
end

#warndaysObject

returns the days for password change warning



210
211
212
# File 'lib/inspec/resources/users.rb', line 210

def warndays
  credentials[:warndays] unless credentials.nil?
end