Class: Inspec::Resources::UnixUser

Inherits:
UserInfo
  • Object
show all
Defined in:
lib/resources/user.rb

Overview

implements generic unix id handling

Direct Known Subclasses

AixUser, DarwinUser, FreeBSDUser, LinuxUser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UserInfo

#credentials

Methods included from Converter

#convert_to_i

Constructor Details

#initialize(inspec) ⇒ UnixUser

Returns a new instance of UnixUser.



193
194
195
196
197
# File 'lib/resources/user.rb', line 193

def initialize(inspec)
  @inspec = inspec
  @id_cmd ||= 'id'
  super
end

Instance Attribute Details

#id_cmdObject (readonly)

Returns the value of attribute id_cmd.



192
193
194
# File 'lib/resources/user.rb', line 192

def id_cmd
  @id_cmd
end

#inspecObject (readonly)

Returns the value of attribute inspec.



192
193
194
# File 'lib/resources/user.rb', line 192

def inspec
  @inspec
end

Instance Method Details

#identity(username) ⇒ Object

extracts the identity



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/resources/user.rb', line 211

def identity(username)
  cmd = inspec.command("#{id_cmd} #{username}")
  return nil if cmd.exit_status != 0

  # parse words
  params = SimpleConfig.new(
    parse_id_entries(cmd.stdout.chomp),
    assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
    group_re: nil,
    multiple_values: false,
  ).params

  {
    uid: convert_to_i(parse_value(params['uid']).keys[0]),
    user: parse_value(params['uid']).values[0],
    gid: convert_to_i(parse_value(params['gid']).keys[0]),
    group: parse_value(params['gid']).values[0],
    groups: parse_value(params['groups']).values,
  }
end

#parse_id_entries(raw) ⇒ Object

splits the results of id into seperate lines



233
234
235
236
237
238
239
240
241
# File 'lib/resources/user.rb', line 233

def parse_id_entries(raw)
  data = []
  until (index = raw.index(/\)\s{1}/)).nil?
    data.push(raw[0, index+1]) # inclue closing )
    raw = raw[index+2, raw.length-index-2]
  end
  data.push(raw) if !raw.nil?
  data.join("\n")
end

#parse_value(line) ⇒ Object

parse one id entry like ‘0(wheel)”



200
201
202
203
204
205
206
207
208
# File 'lib/resources/user.rb', line 200

def parse_value(line)
  SimpleConfig.new(
    line,
    line_separator: ',',
    assignment_re: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
    group_re: nil,
    multiple_values: false,
  ).params
end