mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
improve handling on uid data view
This commit is contained in:
parent
413267c322
commit
7295e4c16f
1 changed files with 48 additions and 20 deletions
|
@ -11,12 +11,25 @@
|
|||
# - home directory
|
||||
# - command
|
||||
|
||||
# usage:
|
||||
#
|
||||
# describe passwd do
|
||||
# its(:usernames) { should eq 'root' }
|
||||
# its(:uids) { should eq 1 }
|
||||
# end
|
||||
#
|
||||
# describe passwd.uid(0) do
|
||||
# its(:username) { should eq 'root' }
|
||||
# its(:count) { should eq 1 }
|
||||
# end
|
||||
|
||||
class Passwd < Vulcano.resource(1)
|
||||
name 'passwd'
|
||||
|
||||
attr_accessor :uid
|
||||
attr_reader :uid
|
||||
attr_reader :parsed
|
||||
|
||||
def initialize(path = nil, uid: nil)
|
||||
def initialize(path = nil)
|
||||
@path = path || '/etc/passwd'
|
||||
@content = vulcano.file(@path).content
|
||||
@parsed = parse(@content)
|
||||
|
@ -26,26 +39,13 @@ class Passwd < Vulcano.resource(1)
|
|||
@path
|
||||
end
|
||||
|
||||
def determine_uid
|
||||
uids = []
|
||||
@parsed.each {|x|
|
||||
if ( x.at(2) == "#{@uid}") then
|
||||
uids.push(x.at(0))
|
||||
end
|
||||
}
|
||||
uids
|
||||
end
|
||||
|
||||
def username
|
||||
uids = determine_uid
|
||||
uids.at(0)
|
||||
end
|
||||
|
||||
def count
|
||||
arr = determine_uid
|
||||
arr.length
|
||||
# call passwd().uid(0)
|
||||
# returns a seperate object with reference to this object
|
||||
def uid(uid)
|
||||
PasswdUid.new(self, uid)
|
||||
end
|
||||
|
||||
# works without uid parameter
|
||||
def map_data(id)
|
||||
@parsed.map {|x|
|
||||
x.at(id)
|
||||
|
@ -90,3 +90,31 @@ class Passwd < Vulcano.resource(1)
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# object that hold a specifc uid view on passwd
|
||||
class PasswdUid
|
||||
def initialize(passwd, uid)
|
||||
@passwd = passwd
|
||||
@uid = uid
|
||||
end
|
||||
|
||||
def determine_uid
|
||||
uids = []
|
||||
@passwd.parsed.each {|x|
|
||||
if (x.at(2) == "#{@uid}")
|
||||
uids.push(x.at(0))
|
||||
end
|
||||
}
|
||||
uids
|
||||
end
|
||||
|
||||
def username
|
||||
uids = determine_uid
|
||||
uids.at(0)
|
||||
end
|
||||
|
||||
def count
|
||||
arr = determine_uid
|
||||
arr.length
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue