Class: Inspec::Resources::Passwd
- Inherits:
-
Object
- Object
- Inspec::Resources::Passwd
show all
- Includes:
- PasswdParser
- Defined in:
- lib/resources/passwd.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#parse_passwd, #parse_passwd_line
Constructor Details
#initialize(path = nil, opts = nil) ⇒ Passwd
Returns a new instance of Passwd.
45
46
47
48
49
50
51
52
|
# File 'lib/resources/passwd.rb', line 45
def initialize(path = nil, opts = nil)
opts ||= {}
@path = path || '/etc/passwd'
@content = opts[:content] || inspec.file(@path).content
@lines = @content.to_s.split("\n")
@filters = opts[:filters] || ''
@params = parse_passwd(@content)
end
|
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
42
43
44
|
# File 'lib/resources/passwd.rb', line 42
def content
@content
end
|
#lines ⇒ Object
Returns the value of attribute lines.
43
44
45
|
# File 'lib/resources/passwd.rb', line 43
def lines
@lines
end
|
#params ⇒ Object
Returns the value of attribute params.
41
42
43
|
# File 'lib/resources/passwd.rb', line 41
def params
@params
end
|
#uid(x) ⇒ Object
Returns the value of attribute uid.
40
41
42
|
# File 'lib/resources/passwd.rb', line 40
def uid
@uid
end
|
Instance Method Details
#count ⇒ Object
118
119
120
|
# File 'lib/resources/passwd.rb', line 118
def count
@params.length
end
|
#filter(hm = {}) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/resources/passwd.rb', line 54
def filter(hm = {})
return self if hm.nil? || hm.empty?
res = @params
filters = ''
hm.each do |attr, condition|
condition = condition.to_s if condition.is_a? Integer
filters += " #{attr} = #{condition.inspect}"
res = res.find_all do |line|
case line[attr.to_s]
when condition
true
else
false
end
end
end
content = res.map { |x| x.values.join(':') }.join("\n")
Passwd.new(@path, content: content, filters: @filters + filters)
end
|
#gids(gid = nil) ⇒ Object
101
102
103
|
# File 'lib/resources/passwd.rb', line 101
def gids(gid = nil)
gid.nil? ? map_data('gid') : filter(gid: gid)
end
|
#homes(home = nil) ⇒ Object
105
106
107
|
# File 'lib/resources/passwd.rb', line 105
def homes(home = nil)
home.nil? ? map_data('home') : filter(home: home)
end
|
#passwords(password = nil) ⇒ Object
93
94
95
|
# File 'lib/resources/passwd.rb', line 93
def passwords(password = nil)
password.nil? ? map_data('password') : filter(password: password)
end
|
#shells(shell = nil) ⇒ Object
109
110
111
|
# File 'lib/resources/passwd.rb', line 109
def shells(shell = nil)
shell.nil? ? map_data('shell') : filter(shell: shell)
end
|
#to_s ⇒ Object
113
114
115
116
|
# File 'lib/resources/passwd.rb', line 113
def to_s
f = @filters.empty? ? '' : ' with'+@filters
"/etc/passwd#{f}"
end
|
#uids(uid = nil) ⇒ Object
97
98
99
|
# File 'lib/resources/passwd.rb', line 97
def uids(uid = nil)
uid.nil? ? map_data('uid') : filter(uid: uid)
end
|
#username ⇒ Object
79
80
81
82
|
# File 'lib/resources/passwd.rb', line 79
def username
warn '[DEPRECATION] `passwd.user` is deprecated. Please use `passwd.users` instead. It will be removed in version 1.0.0.'
users[0]
end
|
#usernames ⇒ Object
74
75
76
77
|
# File 'lib/resources/passwd.rb', line 74
def usernames
warn '[DEPRECATION] `passwd.usernames` is deprecated. Please use `passwd.users` instead. It will be removed in version 1.0.0.'
users
end
|
#users(name = nil) ⇒ Object
89
90
91
|
# File 'lib/resources/passwd.rb', line 89
def users(name = nil)
name.nil? ? map_data('user') : filter(user: name)
end
|