Class: Inspec::Resources::User

Inherits:
Object
  • Object
show all
Includes:
UserManagementSelector
Defined in:
lib/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.



153
154
155
156
157
158
# File 'lib/resources/users.rb', line 153

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

#deprecated(name, alternative = nil) ⇒ Object



250
251
252
# File 'lib/resources/users.rb', line 250

def deprecated(name, alternative = nil)
  warn "[DEPRECATION] #{name} is deprecated. #{alternative}"
end

#disabled?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/resources/users.rb', line 164

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

#enabled?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/resources/users.rb', line 168

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

#exists?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/resources/users.rb', line 160

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

#gidObject



180
181
182
# File 'lib/resources/users.rb', line 180

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

#groupnameObject Also known as: group



184
185
186
# File 'lib/resources/users.rb', line 184

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

#groupsObject



189
190
191
# File 'lib/resources/users.rb', line 189

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

#has_authorized_key?(_compare_key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


245
246
247
248
# File 'lib/resources/users.rb', line 245

def has_authorized_key?(_compare_key)
  deprecated('has_authorized_key?')
  raise NotImplementedError
end

#has_home_directory?(compare_home) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
238
# File 'lib/resources/users.rb', line 235

def has_home_directory?(compare_home)
  deprecated('has_home_directory?', "Please use: its('home')")
  home == compare_home
end

#has_login_shell?(compare_shell) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
243
# File 'lib/resources/users.rb', line 240

def (compare_shell)
  deprecated('has_login_shell?', "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)


230
231
232
233
# File 'lib/resources/users.rb', line 230

def has_uid?(compare_uid)
  deprecated('has_uid?')
  uid == compare_uid
end

#homeObject



193
194
195
# File 'lib/resources/users.rb', line 193

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

#maxdaysObject

returns the maximum days between password changes



207
208
209
# File 'lib/resources/users.rb', line 207

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

#maximum_days_between_password_changeObject

implement ‘maxdays’ method to be compatible with serverspec



223
224
225
226
# File 'lib/resources/users.rb', line 223

def maximum_days_between_password_change
  deprecated('maximum_days_between_password_change', "Please use: its('maxdays')")
  maxdays
end

#mindaysObject

returns the minimum days between password changes



202
203
204
# File 'lib/resources/users.rb', line 202

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

#minimum_days_between_password_changeObject

implement ‘mindays’ method to be compatible with serverspec



217
218
219
220
# File 'lib/resources/users.rb', line 217

def minimum_days_between_password_change
  deprecated('minimum_days_between_password_change', "Please use: its('mindays')")
  mindays
end

#shellObject



197
198
199
# File 'lib/resources/users.rb', line 197

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

#to_sObject



254
255
256
# File 'lib/resources/users.rb', line 254

def to_s
  "User #{@username}"
end

#uidObject



176
177
178
# File 'lib/resources/users.rb', line 176

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

#usernameObject



172
173
174
# File 'lib/resources/users.rb', line 172

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

#warndaysObject

returns the days for password change warning



212
213
214
# File 'lib/resources/users.rb', line 212

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