Best Inspec_ruby code snippet using UserManagementSelector.groupname
users.rb
Source:users.rb
...69 .add_accessor(:entries)70 .add(:usernames, field: :username)71 .add(:uids, field: :uid)72 .add(:gids, field: :gid)73 .add(:groupnames, field: :groupname)74 .add(:groups, field: :groups)75 .add(:homes, field: :home)76 .add(:shells, field: :shell)77 .add(:mindays, field: :mindays)78 .add(:maxdays, field: :maxdays)79 .add(:warndays, field: :warndays)80 .add(:disabled, field: :disabled)81 .add(:exists?) { |x| !x.entries.empty? }82 .add(:disabled?) { |x| x.where { disabled == false }.entries.empty? }83 .add(:enabled?) { |x| x.where { disabled == true }.entries.empty? }84 filter.connect(self, :collect_user_details)85 def to_s86 'Users'87 end88 private89 # method to get all available users90 def list_users91 @username_cache ||= @user_provider.list_users unless @user_provider.nil?92 end93 # collects information about every user94 def collect_user_details95 @users_cache ||= @user_provider.collect_user_details unless @user_provider.nil?96 end97 end98 # The `user` resource handles the special case where only one resource is required99 #100 # describe user('root') do101 # it { should exist }102 # its('uid') { should eq 0 }103 # its('gid') { should eq 0 }104 # its('group') { should eq 'root' }105 # its('groups') { should eq ['root', 'wheel']}106 # its('home') { should eq '/root' }107 # its('shell') { should eq '/bin/bash' }108 # its('mindays') { should eq 0 }109 # its('maxdays') { should eq 99 }110 # its('warndays') { should eq 5 }111 # end112 #113 # The following Serverspec matchers are deprecated in favor for direct value access114 #115 # describe user('root') do116 # it { should belong_to_group 'root' }117 # it { should have_uid 0 }118 # it { should have_home_directory '/root' }119 # it { should have_login_shell '/bin/bash' }120 # its('minimum_days_between_password_change') { should eq 0 }121 # its('maximum_days_between_password_change') { should eq 99 }122 # end123 #124 # ServerSpec tests that are not supported:125 #126 # describe user('root') do127 # it { should have_authorized_key 'ssh-rsa ADg54...3434 user@example.local' }128 # its(:encrypted_password) { should eq 1234 }129 # end130 class User < Inspec.resource(1)131 include UserManagementSelector132 name 'user'133 desc 'Use the user InSpec audit resource to test user profiles, including the groups to which they belong, the frequency of required password changes, the directory paths to home and shell.'134 example "135 describe user('root') do136 it { should exist }137 its('uid') { should eq 1234 }138 its('gid') { should eq 1234 }139 end140 "141 def initialize(username = nil)142 @username = username143 # select user provider144 @user_provider = select_user_manager(inspec.os)145 return skip_resource 'The `user` resource is not supported on your OS yet.' if @user_provider.nil?146 end147 def exists?148 !identity.nil? && !identity[:username].nil?149 end150 def disabled?151 identity[:disabled] == true unless identity.nil?152 end153 def enabled?154 identity[:disabled] == false unless identity.nil?155 end156 def username157 identity[:username] unless identity.nil?158 end159 def uid160 identity[:uid] unless identity.nil?161 end162 def gid163 identity[:gid] unless identity.nil?164 end165 def groupname166 identity[:groupname] unless identity.nil?167 end168 alias group groupname169 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 fail 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 fail '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 fail '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_re: /^\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_re: /^\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 end...
groupname
Using AI Code Generation
1puts um.groupname(1000)2puts um.groupname(1001)3puts um.groupname(1002)4puts um.groupname(1003)5puts um.groupname(1004)6puts um.groupname(1005)7puts um.groupname(1006)8puts um.groupname(1007)9puts um.groupname(1008)
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!!