Best Inspec_ruby code snippet using UserManagementSelector.meta_info
users.rb
Source:users.rb
...169 def groups170 identity[:groups] unless identity.nil?171 end172 def home173 meta_info[:home] unless meta_info.nil?174 end175 def shell176 meta_info[:shell] unless meta_info.nil?177 end178 # returns the minimum days between password changes179 def mindays180 credentials[:mindays] unless credentials.nil?181 end182 # returns the maximum days between password changes183 def maxdays184 credentials[:maxdays] unless credentials.nil?185 end186 # returns the days for password change warning187 def warndays188 credentials[:warndays] unless credentials.nil?189 end190 # implement 'mindays' method to be compatible with serverspec191 def minimum_days_between_password_change192 deprecated('minimum_days_between_password_change', "Please use: its('mindays')")193 mindays194 end195 # implement 'maxdays' method to be compatible with serverspec196 def maximum_days_between_password_change197 deprecated('maximum_days_between_password_change', "Please use: its('maxdays')")198 maxdays199 end200 # implements rspec has matcher, to be compatible with serverspec201 # @see: https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/has.rb202 def has_uid?(compare_uid)203 deprecated('has_uid?')204 uid == compare_uid205 end206 def has_home_directory?(compare_home)207 deprecated('has_home_directory?', "Please use: its('home')")208 home == compare_home209 end210 def has_login_shell?(compare_shell)211 deprecated('has_login_shell?', "Please use: its('shell')")212 shell == compare_shell213 end214 def has_authorized_key?(_compare_key)215 deprecated('has_authorized_key?')216 raise NotImplementedError217 end218 def deprecated(name, alternative = nil)219 warn "[DEPRECATION] #{name} is deprecated. #{alternative}"220 end221 def to_s222 "User #{@username}"223 end224 private225 # returns the iden226 def identity227 return @id_cache if defined?(@id_cache)228 @id_cache = @user_provider.identity(@username) if !@user_provider.nil?229 end230 def meta_info231 return @meta_cache if defined?(@meta_cache)232 @meta_cache = @user_provider.meta_info(@username) if !@user_provider.nil?233 end234 def credentials235 return @cred_cache if defined?(@cred_cache)236 @cred_cache = @user_provider.credentials(@username) if !@user_provider.nil?237 end238 end239 # This is an abstract class that every user provoider has to implement.240 # A user provider implements a system abstracts and helps the InSpec resource241 # hand-over system specific behavior to those providers242 class UserInfo243 include Converter244 attr_reader :inspec245 def initialize(inspec)246 @inspec = inspec247 end248 # returns a hash with user-specific values:249 # {250 # uid: '',251 # user: '',252 # gid: '',253 # group: '',254 # groups: '',255 # }256 def identity(_username)257 raise 'user provider must implement the `identity` method'258 end259 # returns optional information about a user, eg shell260 def meta_info(_username)261 nil262 end263 # returns a hash with meta-data about user credentials264 # {265 # mindays: 1,266 # maxdays: 1,267 # warndays: 1,268 # }269 # this method is optional and may not be implemented by each provider270 def credentials(_username)271 nil272 end273 # returns an array with users274 def list_users275 raise 'user provider must implement the `list_users` method'276 end277 # retuns all aspects of the user as one hash278 def user_details(username)279 item = {}280 id = identity(username)281 item.merge!(id) unless id.nil?282 meta = meta_info(username)283 item.merge!(meta) unless meta.nil?284 cred = credentials(username)285 item.merge!(cred) unless cred.nil?286 item287 end288 # returns the full information list for a user289 def collect_user_details290 list_users.map { |username|291 user_details(username.chomp)292 }293 end294 end295 # implements generic unix id handling296 class UnixUser < UserInfo297 attr_reader :inspec, :id_cmd, :list_users_cmd298 def initialize(inspec)299 @inspec = inspec300 @id_cmd ||= 'id'301 @list_users_cmd ||= 'cut -d: -f1 /etc/passwd | grep -v "^#"'302 super303 end304 # returns a list of all local users on a system305 def list_users306 cmd = inspec.command(list_users_cmd)307 return [] if cmd.exit_status != 0308 cmd.stdout.chomp.lines309 end310 # parse one id entry like '0(wheel)''311 def parse_value(line)312 SimpleConfig.new(313 line,314 line_separator: ',',315 assignment_regex: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,316 group_re: nil,317 multiple_values: false,318 ).params319 end320 # extracts the identity321 def identity(username)322 cmd = inspec.command("#{id_cmd} #{username}")323 return nil if cmd.exit_status != 0324 # parse words325 params = SimpleConfig.new(326 parse_id_entries(cmd.stdout.chomp),327 assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,328 group_re: nil,329 multiple_values: false,330 ).params331 {332 uid: convert_to_i(parse_value(params['uid']).keys[0]),333 username: parse_value(params['uid']).values[0],334 gid: convert_to_i(parse_value(params['gid']).keys[0]),335 groupname: parse_value(params['gid']).values[0],336 groups: parse_value(params['groups']).values,337 }338 end339 # splits the results of id into seperate lines340 def parse_id_entries(raw)341 data = []342 until (index = raw.index(/\)\s{1}/)).nil?343 data.push(raw[0, index+1]) # inclue closing )344 raw = raw[index+2, raw.length-index-2]345 end346 data.push(raw) if !raw.nil?347 data.join("\n")348 end349 end350 class LinuxUser < UnixUser351 include PasswdParser352 include CommentParser353 def meta_info(username)354 cmd = inspec.command("getent passwd #{username}")355 return nil if cmd.exit_status != 0356 # returns: root:x:0:0:root:/root:/bin/bash357 passwd = parse_passwd_line(cmd.stdout.chomp)358 {359 home: passwd['home'],360 shell: passwd['shell'],361 }362 end363 def credentials(username)364 cmd = inspec.command("chage -l #{username}")365 return nil if cmd.exit_status != 0366 params = SimpleConfig.new(367 cmd.stdout.chomp,368 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,369 group_re: nil,370 multiple_values: false,371 ).params372 {373 mindays: convert_to_i(params['Minimum number of days between password change']),374 maxdays: convert_to_i(params['Maximum number of days between password change']),375 warndays: convert_to_i(params['Number of days of warning before password expires']),376 }377 end378 end379 class SolarisUser < LinuxUser380 def initialize(inspec)381 @inspec = inspec382 @id_cmd ||= 'id -a'383 super384 end385 end386 class AixUser < UnixUser387 def identity(username)388 id = super(username)389 return nil if id.nil?390 # AIX 'id' command doesn't include the primary group in the supplementary391 # yet it can be somewhere in the supplementary list if someone added root392 # to a groups list in /etc/group393 # we rearrange to expected list if that is the case394 if id[:groups].first != id[:group]395 id[:groups].reject! { |i| i == id[:group] } if id[:groups].include?(id[:group])396 id[:groups].unshift(id[:group])397 end398 id399 end400 def meta_info(username)401 lsuser = inspec.command("lsuser -C -a home shell #{username}")402 return nil if lsuser.exit_status != 0403 user = lsuser.stdout.chomp.split("\n").last.split(':')404 {405 home: user[1],406 shell: user[2],407 }408 end409 def credentials(username)410 cmd = inspec.command(411 "lssec -c -f /etc/security/user -s #{username} -a minage -a maxage -a pwdwarntime",412 )413 return nil if cmd.exit_status != 0414 user_sec = cmd.stdout.chomp.split("\n").last.split(':')415 {416 mindays: user_sec[1].to_i * 7,417 maxdays: user_sec[2].to_i * 7,418 warndays: user_sec[3].to_i,419 }420 end421 end422 class HpuxUser < UnixUser423 def meta_info(username)424 hpuxuser = inspec.command("logins -x -l #{username}")425 return nil if hpuxuser.exit_status != 0426 user = hpuxuser.stdout.chomp.split(' ')427 {428 home: user[4],429 shell: user[5],430 }431 end432 end433 # we do not use 'finger' for MacOS, because it is harder to parse data with it434 # @see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/fingerd.8.html435 # instead we use 'dscl' to request user data436 # @see https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/dscl.1.html437 # @see http://superuser.com/questions/592921/mac-osx-users-vs-dscl-command-to-list-user438 class DarwinUser < UnixUser439 def initialize(inspec)440 @list_users_cmd ||= 'dscl . list /Users'441 super442 end443 def meta_info(username)444 cmd = inspec.command("dscl -q . -read /Users/#{username} NFSHomeDirectory PrimaryGroupID RecordName UniqueID UserShell")445 return nil if cmd.exit_status != 0446 params = SimpleConfig.new(447 cmd.stdout.chomp,448 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,449 group_re: nil,450 multiple_values: false,451 ).params452 {453 home: params['NFSHomeDirectory'],454 shell: params['UserShell'],455 }456 end457 end458 # FreeBSD recommends to use the 'pw' command for user management459 # @see: https://www.freebsd.org/doc/handbook/users-synopsis.html460 # @see: https://www.freebsd.org/cgi/man.cgi?pw(8)461 # It offers the following commands:462 # - adduser(8) The recommended command-line application for adding new users.463 # - rmuser(8) The recommended command-line application for removing users.464 # - chpass(1) A flexible tool for changing user database information.465 # - passwd(1) The command-line tool to change user passwords.466 class FreeBSDUser < UnixUser467 include PasswdParser468 def meta_info(username)469 cmd = inspec.command("pw usershow #{username} -7")470 return nil if cmd.exit_status != 0471 # returns: root:*:0:0:Charlie &:/root:/bin/csh472 passwd = parse_passwd_line(cmd.stdout.chomp)473 {474 home: passwd['home'],475 shell: passwd['shell'],476 }477 end478 end479 # This optimization was inspired by480 # @see https://mcpmag.com/articles/2015/04/15/reporting-on-local-accounts.aspx481 # Alternative solutions are WMI Win32_UserAccount482 # @see https://msdn.microsoft.com/en-us/library/aa394507(v=vs.85).aspx...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!