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.



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

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



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

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

#disabled?Boolean

Returns:

  • (Boolean)


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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#exists?Boolean

Returns:

  • (Boolean)


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

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

#gidObject



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

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

#groupnameObject Also known as: group



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

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

#groupsObject



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

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

#has_authorized_key?(_compare_key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#has_home_directory?(compare_home) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

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

#homeObject



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

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

#maxdaysObject

returns the maximum days between password changes



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

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

#maximum_days_between_password_changeObject

implement ‘maxdays’ method to be compatible with serverspec



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

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



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

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

#minimum_days_between_password_changeObject

implement ‘mindays’ method to be compatible with serverspec



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

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

#shellObject



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

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

#to_sObject



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

def to_s
  "User #{@username}"
end

#uidObject



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

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

#usernameObject



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

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

#warndaysObject

returns the days for password change warning



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

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