Class: Serverspec::Commands::Windows
- Inherits:
-
Object
- Object
- Serverspec::Commands::Windows
show all
- Defined in:
- lib/serverspec/commands/windows.rb
Defined Under Namespace
Classes: NotSupportedError
Constant Summary
collapse
- REGISTRY_KEY_TYPES =
{
:type_string => 'String',
:type_binary => 'Binary',
:type_dword => 'DWord',
:type_qword => 'QWord',
:type_multistring => 'MultiString',
:type_expandstring => 'ExpandString'
}
Instance Method Summary
collapse
-
#check_access_by_user(file, user, access) ⇒ Object
-
#check_belonging_group(user, group) ⇒ Object
-
#check_directory(dir) ⇒ Object
-
#check_enabled(service, level = nil) ⇒ Object
-
#check_executable(file, by_whom) ⇒ Object
-
#check_file(file) ⇒ Object
-
#check_file_contain(file, pattern) ⇒ Object
-
#check_file_contain_within(file, pattern, from = nil, to = nil) ⇒ Object
-
#check_file_hidden(file) ⇒ Object
-
#check_file_readonly(file) ⇒ Object
-
#check_file_system(file) ⇒ Object
-
#check_group(group) ⇒ Object
-
#check_installed(package, version = nil) ⇒ Object
-
#check_listening(port) ⇒ Object
-
#check_listening_with_protocol(port, protocol) ⇒ Object
-
#check_process(process) ⇒ Object
-
#check_readable(file, by_whom) ⇒ Object
-
#check_registry_key(key_name, key_property = {}) ⇒ Object
-
#check_running(service) ⇒ Object
-
#check_user(user) ⇒ Object
-
#check_writable(file, by_whom) ⇒ Object
-
#method_missing(method, *args) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
14
15
16
17
|
# File 'lib/serverspec/commands/windows.rb', line 14
def method_missing method, *args
raise NotSupportedError.new "#{method} currently not supported in Windows os" if method.to_s =~ /^check_.+/
super(method, *args)
end
|
Instance Method Details
#check_access_by_user(file, user, access) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/serverspec/commands/windows.rb', line 69
def check_access_by_user(file, user, access)
case access
when 'r'
check_readable(file, user)
when 'w'
check_writable(file, user)
when 'x'
check_executable(file, user)
end
end
|
#check_belonging_group(user, group) ⇒ Object
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/serverspec/commands/windows.rb', line 159
def check_belonging_group(user, group)
user_id, user_domain = windows_account user
group_id, group_domain = windows_account group
Backend::PowerShell::Command.new do
using 'find_user.ps1'
using 'find_group.ps1'
using 'find_usergroup.ps1'
exec "(FindUserGroup -userName '#{user_id}'#{user_domain.nil? ? "" : " -userDomain '#{user_domain}'"} -groupName '#{group_id}'#{group_domain.nil? ? "" : " -groupDomain '#{group_domain}'"}) -ne $null"
end
end
|
#check_directory(dir) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/serverspec/commands/windows.rb', line 47
def check_directory(dir)
cmd = item_has_attribute dir, 'Directory'
Backend::PowerShell::Command.new do
exec cmd
end
end
|
#check_enabled(service, level = nil) ⇒ Object
109
110
111
112
113
114
|
# File 'lib/serverspec/commands/windows.rb', line 109
def check_enabled(service, level=nil)
Backend::PowerShell::Command.new do
using 'find_service.ps1'
exec "(FindService -name '#{service}').StartMode -eq 'Auto'"
end
end
|
#check_executable(file, by_whom) ⇒ Object
94
95
96
97
98
99
|
# File 'lib/serverspec/commands/windows.rb', line 94
def check_executable(file, by_whom)
Backend::PowerShell::Command.new do
using 'check_file_access_rules.ps1'
exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'ExecuteFile')"
end
end
|
#check_file(file) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/serverspec/commands/windows.rb', line 19
def check_file(file)
cmd = item_has_attribute file, 'Archive'
Backend::PowerShell::Command.new do
exec cmd
end
end
|
#check_file_contain(file, pattern) ⇒ Object
54
55
56
57
58
|
# File 'lib/serverspec/commands/windows.rb', line 54
def check_file_contain(file, pattern)
Backend::PowerShell::Command.new do
exec "[Io.File]::ReadAllText('#{file}') -match '#{convert_regexp(pattern)}'"
end
end
|
#check_file_contain_within(file, pattern, from = nil, to = nil) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/serverspec/commands/windows.rb', line 60
def check_file_contain_within file, pattern, from=nil, to=nil
from ||= '^'
to ||= '$'
Backend::PowerShell::Command.new do
using 'crop_text.ps1'
exec %Q[(CropText -text ([Io.File]::ReadAllText('#{file}')) -fromPattern '#{convert_regexp(from)}' -toPattern '#{convert_regexp(to)}') -match '#{pattern}']
end
end
|
#check_file_hidden(file) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/serverspec/commands/windows.rb', line 26
def check_file_hidden(file)
cmd = item_has_attribute file, 'Hidden'
Backend::PowerShell::Command.new do
exec cmd
end
end
|
#check_file_readonly(file) ⇒ Object
33
34
35
36
37
38
|
# File 'lib/serverspec/commands/windows.rb', line 33
def check_file_readonly(file)
cmd = item_has_attribute file, 'ReadOnly'
Backend::PowerShell::Command.new do
exec cmd
end
end
|
#check_file_system(file) ⇒ Object
40
41
42
43
44
45
|
# File 'lib/serverspec/commands/windows.rb', line 40
def check_file_system(file)
cmd = item_has_attribute file, 'System'
Backend::PowerShell::Command.new do
exec cmd
end
end
|
#check_group(group) ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/serverspec/commands/windows.rb', line 151
def check_group(group)
group_id, domain = windows_account group
Backend::PowerShell::Command.new do
using 'find_group.ps1'
exec "(FindGroup -groupName '#{group_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
end
end
|
#check_installed(package, version = nil) ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/serverspec/commands/windows.rb', line 101
def check_installed(package, version=nil)
version_selection = version.nil? ? "" : "-appVersion '#{version}'"
Backend::PowerShell::Command.new do
using 'find_installed_application.ps1'
exec "(FindInstalledApplication -appName '#{package}' #{version_selection}) -ne $null"
end
end
|
#check_listening(port) ⇒ Object
129
130
131
132
133
134
|
# File 'lib/serverspec/commands/windows.rb', line 129
def check_listening(port)
Backend::PowerShell::Command.new do
using 'is_port_listening.ps1'
exec "IsPortListening -portNumber #{port}"
end
end
|
#check_listening_with_protocol(port, protocol) ⇒ Object
136
137
138
139
140
141
|
# File 'lib/serverspec/commands/windows.rb', line 136
def check_listening_with_protocol(port, protocol)
Backend::PowerShell::Command.new do
using 'is_port_listening.ps1'
exec "IsPortListening -portNumber #{port} -protocol '#{protocol}'"
end
end
|
#check_process(process) ⇒ Object
123
124
125
126
127
|
# File 'lib/serverspec/commands/windows.rb', line 123
def check_process(process)
Backend::PowerShell::Command.new do
exec "(Get-Process '#{process}') -ne $null"
end
end
|
#check_readable(file, by_whom) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/serverspec/commands/windows.rb', line 80
def check_readable(file, by_whom)
Backend::PowerShell::Command.new do
using 'check_file_access_rules.ps1'
exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'Read', 'ListDirectory')"
end
end
|
#check_registry_key(key_name, key_property = {}) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/serverspec/commands/windows.rb', line 170
def check_registry_key(key_name, key_property = {})
if key_property.empty?
cmd = "(Get-Item 'Registry::#{key_name}') -ne $null"
else
if key_property.key? :value
value = convert_key_property_value key_property
cmd = "(Compare-Object (Get-Item 'Registry::#{key_name}').GetValue('#{key_property[:name]}') #{value}) -eq $null"
else
cmd = "(Get-Item 'Registry::#{key_name}').GetValueKind('#{key_property[:name]}') -eq '#{REGISTRY_KEY_TYPES[key_property[:type]]}'"
end
end
Backend::PowerShell::Command.new { exec cmd }
end
|
#check_running(service) ⇒ Object
116
117
118
119
120
121
|
# File 'lib/serverspec/commands/windows.rb', line 116
def check_running(service)
Backend::PowerShell::Command.new do
using 'find_service.ps1'
exec "(FindService -name '#{service}').State -eq 'Running'"
end
end
|
#check_user(user) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/serverspec/commands/windows.rb', line 143
def check_user(user)
user_id, domain = windows_account user
Backend::PowerShell::Command.new do
using 'find_user.ps1'
exec "(FindUser -userName '#{user_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
end
end
|
#check_writable(file, by_whom) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/serverspec/commands/windows.rb', line 87
def check_writable(file, by_whom)
Backend::PowerShell::Command.new do
using 'check_file_access_rules.ps1'
exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'Write')"
end
end
|