mirror of
https://github.com/inspec/inspec
synced 2024-11-27 07:00:39 +00:00
rewrite passwd resource to extract parser
This commit is contained in:
parent
3ff4a5d769
commit
535fc10b5d
1 changed files with 30 additions and 38 deletions
|
@ -32,7 +32,7 @@ class Passwd < Vulcano.resource(1)
|
|||
def initialize(path = nil)
|
||||
@path = path || '/etc/passwd'
|
||||
@content = vulcano.file(@path).content
|
||||
@parsed = parse(@content)
|
||||
@parsed = parse_passwd(@content)
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
@ -45,31 +45,44 @@ class Passwd < Vulcano.resource(1)
|
|||
PasswdUid.new(self, uid)
|
||||
end
|
||||
|
||||
# works without uid parameter
|
||||
def map_data(id)
|
||||
@parsed.map {|x|
|
||||
x.at(id)
|
||||
}
|
||||
end
|
||||
|
||||
def usernames
|
||||
map_data(0)
|
||||
map_data('name')
|
||||
end
|
||||
|
||||
def passwords
|
||||
map_data(1)
|
||||
map_data('password')
|
||||
end
|
||||
|
||||
def uids
|
||||
map_data(2)
|
||||
map_data('uid')
|
||||
end
|
||||
|
||||
def gids
|
||||
map_data(3)
|
||||
map_data('gid')
|
||||
end
|
||||
|
||||
def users
|
||||
@parsed.map {|x|
|
||||
x['name']
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def map_data(id)
|
||||
@parsed.map {|x|
|
||||
x[id]
|
||||
}
|
||||
end
|
||||
|
||||
def parse_passwd(content)
|
||||
content.split("\n").map do |line|
|
||||
parse_passwd_line(line)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_passwd_line(line)
|
||||
x = line.split(':')
|
||||
{
|
||||
'name' => x.at(0),
|
||||
'password' => x.at(1),
|
||||
|
@ -79,15 +92,6 @@ class Passwd < Vulcano.resource(1)
|
|||
'home' => x.at(5),
|
||||
'shell' => x.at(6),
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse(content)
|
||||
content.split("\n").map do |line|
|
||||
line.split(':')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -95,26 +99,14 @@ end
|
|||
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
|
||||
@users = @passwd.parsed.select { |x| x['uid'] == "#{uid}" }
|
||||
end
|
||||
|
||||
def username
|
||||
uids = determine_uid
|
||||
uids.at(0)
|
||||
@users.at(0)['name']
|
||||
end
|
||||
|
||||
def count
|
||||
arr = determine_uid
|
||||
arr.length
|
||||
@users.size
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue