openvas-cli 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -1,9 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
- gem "activesupport", ">= 3.0.5"
5
- gem 'activerecord', '>= 3.0.5'
6
- gem 'rails', '>=3.0.5'
4
+ gem "activesupport", "= 3.0.5"
5
+ gem 'activerecord', '= 3.0.5'
6
+ gem 'rails', '=3.0.5'
7
7
  gem 'nokogiri', '>= 1.4.4'
8
8
  gem 'ipaddress', '>=0.7.0'
9
9
 
data/Gemfile.lock CHANGED
@@ -88,13 +88,13 @@ PLATFORMS
88
88
 
89
89
  DEPENDENCIES
90
90
  ZenTest
91
- activerecord (>= 3.0.5)
92
- activesupport (>= 3.0.5)
91
+ activerecord (= 3.0.5)
92
+ activesupport (= 3.0.5)
93
93
  bundler (~> 1.0.0)
94
94
  ipaddress (>= 0.7.0)
95
95
  jeweler (~> 1.5.2)
96
96
  log4r
97
97
  nokogiri (>= 1.4.4)
98
- rails (>= 3.0.5)
98
+ rails (= 3.0.5)
99
99
  rcov
100
100
  rspec (~> 2.3.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.8
1
+ 0.2.9
@@ -21,7 +21,14 @@ module OpenvasCli
21
21
 
22
22
  def initialize(attributes={})
23
23
  attributes.each { |key, value|
24
- instance_variable_set("@#{key}", value)
24
+ send("#{key}=".to_sym, value) if public_methods.include?("#{key}=")
25
+ }
26
+ reset_changes
27
+ end
28
+
29
+ def update_attributes(attrs={})
30
+ attrs.each { |key, value|
31
+ send("#{key}=".to_sym, value) if public_methods.include?("#{key}=")
25
32
  }
26
33
  end
27
34
 
@@ -95,33 +95,31 @@ module OpenvasCli
95
95
  end
96
96
  end
97
97
 
98
- def save!
98
+ def create_or_update
99
99
  @previously_changed = changes
100
- preferences.each{ |p| p.save! if p.changed? }
101
- @changed_attributes.clear
102
- end
103
-
104
- def destroy
105
100
  begin
106
- destroy!
101
+ preferences.each{ |p| p.save! if p.changed? }
102
+ @changed_attributes.clear
107
103
 
108
- true
104
+ self
109
105
  rescue Exception => e
110
- # logger = OpenvasCli.logger
111
- # logger.error("Could Not Destroy Configuration: (#{e.class})#{e.message}") if logger
112
-
113
- false
106
+ errors[:command] << e.message
107
+ nil
114
108
  end
115
109
  end
116
110
 
117
- def destroy!
118
- tasks.each { |t| t.destroy! }
119
-
111
+ def delete_record
120
112
  req = Nokogiri::XML::Builder.new { |xml|
121
113
  xml.delete_config(:config_id => @id)
122
114
  }
123
115
 
124
- VasConfig.connection.send_receive(req.doc)
116
+ begin
117
+ VasConfig.connection.send_receive(req.doc)
118
+ self
119
+ rescue Exception => e
120
+ errors[:command] << e.message
121
+ nil
122
+ end
125
123
  end
126
124
  end
127
125
  end
@@ -18,6 +18,8 @@ module OpenvasCli
18
18
  # NVT id
19
19
  attr_accessor :nvt_id
20
20
 
21
+ attr_accessor :nvt_name
22
+
21
23
  validates :name, :presence => true, :length=>{:minimum => 1}
22
24
 
23
25
  define_attribute_methods [:name, :value]
@@ -98,14 +100,16 @@ module OpenvasCli
98
100
 
99
101
  # Pulls Vas preferences.
100
102
  # === Options:
103
+ # [:config_id] => [configuration_id]] pulls preferences associated with the provided configuration id
101
104
  # [:nvt_oid => [oid]] pulls preferences associated associated with the provided VasNVT.oid
102
105
  # [:name => [name]] pulls the preference with the specified name
103
- # [:sort_by => [field_name]] filters the results by the provided field name. Valid symbols are, :name, :value, :config_id.
106
+ # [:sort_by => [field_name]] filters the results by the provided field name. Valid symbols are, :name, :nvt_name, :nvt_id.
104
107
  def self.get_all(options={})
105
108
  manual_filter = false
106
109
  params = {}
107
- params[:nvt_oid] = options[:nvt_oid] if options[:nvt_oid]
108
- manual_filter = true if options[:name]
110
+ params[:config_id] = options[:config_id] if options[:config_id]
111
+ params[:nvt_oid] = options[:nvt_oid] if options[:nvt_oid]
112
+ manual_filter = true if options[:name]
109
113
 
110
114
  req = Nokogiri::XML::Builder.new { |xml|
111
115
  if params.empty?
@@ -114,31 +118,35 @@ module OpenvasCli
114
118
  xml.get_preferences(params)
115
119
  end
116
120
  }
117
-
118
- prefs = connection.send_receive(req.doc)
119
121
  ret = []
120
- prefs.xpath("//preference").each { |p|
121
- pref = from_xml_node(p)
122
- if manual_filter
123
- ret << pref if options[:name] && options[:name] == pref.name
124
- else
125
- ret << pref
126
- end
127
- }
128
-
129
- ret.sort!{ |a,b| a.name <=> b.name } if params[:sort_by] == :name
130
- ret.sort!{ |a,b| a.value <=> b.value } if params[:sort_by] == :value
131
- ret.sort!{ |a,b| a.config_id <=> b.config_id } if params[:sort_by] == :config_id
132
-
122
+ begin
123
+ prefs = connection.send_receive(req.doc)
124
+
125
+ prefs.xpath("//preference").each { |p|
126
+ pref = from_xml_node(p, options[:config_id])
127
+ if manual_filter
128
+ ret << pref if options[:name] && options[:name] == pref.name
129
+ else
130
+ ret << pref
131
+ end
132
+ }
133
+
134
+ ret.sort!{ |a,b| a.name <=> b.name } if options[:sort_by] == :name
135
+ ret.sort!{ |a,b| a.nvt_id <=> b.nvt_id } if options[:sort_by] == :nvt_id
136
+ ret.sort!{ |a,b| a.nvt_name <=> b.nvt_name } if options[:sort_by] == :nvt_name
137
+ rescue VasExceptions::CommandException => e
138
+ end
133
139
  ret
134
140
  end
135
141
 
136
- def self.from_xml_node(node)
137
- pref = VasPreference.new
138
- pref.name = extract_value_from("name", node)
139
- pref.value = extract_value_from("value", node)
140
- pref.nvt_id = extract_value_from("nvt/@oid", node)
142
+ def self.from_xml_node(node, config_id=nil)
143
+ pref = VasPreference.new
144
+ pref.name = extract_value_from("name", node)
145
+ pref.value = extract_value_from("value", node)
146
+ pref.nvt_id = extract_value_from("nvt/@oid", node)
147
+ pref.nvt_name = extract_value_from("nvt/name", node)
141
148
  pref.val_type_desc = extract_value_from("type", node)
149
+ pref.config_id = config_id
142
150
  case pref.val_type_desc
143
151
  when "checkbox"
144
152
  pref.val_type = :boolean
@@ -62,7 +62,7 @@ module OpenvasCli
62
62
  rep.task_id = extract_value_from("task/@id", r)
63
63
  rep.task_name = extract_value_from("task/name", r)
64
64
  rep.status = extract_value_from("scan_run_status", r)
65
- rep.started_at = extract_value_from("scan_start", r)
65
+ rep.started_at = Time.parse(extract_value_from("scan_start", r))
66
66
 
67
67
  rep.result_count[:total] = extract_value_from("result_count/full", r).to_i
68
68
  rep.result_count[:filtered] = extract_value_from("result_count/filtered", r).to_i
@@ -120,7 +120,7 @@ module OpenvasCli
120
120
  when :next_time
121
121
  manual_sort = true
122
122
  when :first_time
123
- params[:first_time] = 'first_time'
123
+ params[:sort_field] = 'first_time'
124
124
  else
125
125
  params[:sort_field] = 'name'
126
126
  end
@@ -67,6 +67,22 @@ module OpenvasCli
67
67
  @credential_keys ||= { :smb => nil, :ssh => nil }
68
68
  end
69
69
 
70
+ def smb_credential_id
71
+ credentials[:smb].id if credentials[:smb]
72
+ end
73
+
74
+ def smb_credential_id=(val)
75
+ credentials[:smb] = VasLscCredential.get_by_id(val)
76
+ end
77
+
78
+ def ssh_credential_id
79
+ credentials[:ssh].id if credentials[:ssh]
80
+ end
81
+
82
+ def ssh_credential_id=(val)
83
+ credentials[:ssh] = VasLscCredential.get_by_id(val)
84
+ end
85
+
70
86
  def tasks
71
87
  @tasks ||= get_tasks
72
88
  end
@@ -86,6 +102,14 @@ module OpenvasCli
86
102
  @org_hosts = val.collect { |h| h } if val
87
103
  end
88
104
 
105
+ def hosts_string
106
+ hosts.join(", ")
107
+ end
108
+
109
+ def hosts_string=(val)
110
+ self.hosts = val.split(/, ?/)
111
+ end
112
+
89
113
  def create_or_update
90
114
  if @id
91
115
  return unless destroy
@@ -94,7 +118,7 @@ module OpenvasCli
94
118
  xml.create_target {
95
119
  xml.name { xml.text(@name) }
96
120
  xml.comment { xml.text(@comment) } if @comment
97
- xml.hosts { xml.text(@hosts.join(', ')) }
121
+ xml.hosts { xml.text(hosts_string) }
98
122
  xml.ssh_lsc_credential(:id => credentials[:ssh].id) if credentials[:ssh]
99
123
  xml.smb_lsc_credential(:id => credentials[:smb].id) if credentials[:smb]
100
124
  xml.port_range { xml.text(@port_range) } if @port_range
@@ -107,7 +131,7 @@ module OpenvasCli
107
131
  reset_changes
108
132
 
109
133
  true
110
- rescue VaxExceptions::CommandException => e
134
+ rescue VasExceptions::CommandException => e
111
135
  errors[:command_failure] << e.message
112
136
 
113
137
  nil
@@ -127,7 +151,7 @@ module OpenvasCli
127
151
  @id = nil
128
152
 
129
153
  true
130
- rescue VaxExceptions::CommandException => e
154
+ rescue VasExceptions::CommandException => e
131
155
  errors[:command_failure] << e.message
132
156
 
133
157
  nil
@@ -72,6 +72,10 @@ module OpenvasCli
72
72
  target_id = val.id if val
73
73
  end
74
74
 
75
+ def reports
76
+ @reports ||= []
77
+ end
78
+
75
79
  def create_or_update
76
80
 
77
81
  if schedule && schedule.changed?
@@ -107,7 +111,7 @@ module OpenvasCli
107
111
  reset_changes
108
112
 
109
113
  true
110
- rescue VaxExceptions::CommandException => e
114
+ rescue VasExceptions::CommandException => e
111
115
  errors[:command_failure] << e.message
112
116
 
113
117
  nil
@@ -125,7 +129,7 @@ module OpenvasCli
125
129
  VasTask.connection.send_receive(req.doc)
126
130
 
127
131
  true
128
- rescue VaxExceptions::CommandException => e
132
+ rescue VasExceptions::CommandException => e
129
133
  errors[:command_failure] << e.message
130
134
 
131
135
  nil
@@ -208,6 +212,12 @@ module OpenvasCli
208
212
  t.last_report_id = extract_value_from("last_report/report/@id", node)
209
213
  t.config_id = extract_value_from("config/@id", node)
210
214
  t.target_id = extract_value_from("target/@id", node)
215
+ node.xpath("reports/report").each { |xr|
216
+ t.reports << VasReport.new({
217
+ :id => extract_value_from("@id", xr),
218
+ :started_at => extract_value_from("timestamp", xr)
219
+ })
220
+ }
211
221
 
212
222
  t.reset_changes
213
223
 
data/openvas-cli.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{openvas-cli}
8
- s.version = "0.2.8"
8
+ s.version = "0.2.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Reed Swenson"]
12
- s.date = %q{2011-04-07}
12
+ s.date = %q{2011-04-13}
13
13
  s.description = %q{A full ruby implementation of the OpenVAS OMP (version 2.0) protocol.}
14
14
  s.email = %q{fleureed@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -90,9 +90,9 @@ Gem::Specification.new do |s|
90
90
  s.specification_version = 3
91
91
 
92
92
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
93
- s.add_runtime_dependency(%q<activesupport>, [">= 3.0.5"])
94
- s.add_runtime_dependency(%q<activerecord>, [">= 3.0.5"])
95
- s.add_runtime_dependency(%q<rails>, [">= 3.0.5"])
93
+ s.add_runtime_dependency(%q<activesupport>, ["= 3.0.5"])
94
+ s.add_runtime_dependency(%q<activerecord>, ["= 3.0.5"])
95
+ s.add_runtime_dependency(%q<rails>, ["= 3.0.5"])
96
96
  s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.4"])
97
97
  s.add_runtime_dependency(%q<ipaddress>, [">= 0.7.0"])
98
98
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
@@ -102,9 +102,9 @@ Gem::Specification.new do |s|
102
102
  s.add_development_dependency(%q<ZenTest>, [">= 0"])
103
103
  s.add_development_dependency(%q<log4r>, [">= 0"])
104
104
  else
105
- s.add_dependency(%q<activesupport>, [">= 3.0.5"])
106
- s.add_dependency(%q<activerecord>, [">= 3.0.5"])
107
- s.add_dependency(%q<rails>, [">= 3.0.5"])
105
+ s.add_dependency(%q<activesupport>, ["= 3.0.5"])
106
+ s.add_dependency(%q<activerecord>, ["= 3.0.5"])
107
+ s.add_dependency(%q<rails>, ["= 3.0.5"])
108
108
  s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
109
109
  s.add_dependency(%q<ipaddress>, [">= 0.7.0"])
110
110
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
@@ -115,9 +115,9 @@ Gem::Specification.new do |s|
115
115
  s.add_dependency(%q<log4r>, [">= 0"])
116
116
  end
117
117
  else
118
- s.add_dependency(%q<activesupport>, [">= 3.0.5"])
119
- s.add_dependency(%q<activerecord>, [">= 3.0.5"])
120
- s.add_dependency(%q<rails>, [">= 3.0.5"])
118
+ s.add_dependency(%q<activesupport>, ["= 3.0.5"])
119
+ s.add_dependency(%q<activerecord>, ["= 3.0.5"])
120
+ s.add_dependency(%q<rails>, ["= 3.0.5"])
121
121
  s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
122
122
  s.add_dependency(%q<ipaddress>, [">= 0.7.0"])
123
123
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
@@ -62,7 +62,7 @@ module OpenvasCli
62
62
  cred.save!
63
63
 
64
64
  n_cred = VasLscCredential.get_by_id(cred.id)
65
- n_cred.login = "NOT_FOOBAR"
65
+ n_cred.update_attributes(:login => "NOT_FOOBAR")
66
66
  n_cred.save!
67
67
 
68
68
  n_cred.id.should == cred.id
@@ -24,5 +24,60 @@ module OpenvasCli
24
24
  o_pref.value.should == n_pref.value
25
25
  o_pref.config_id.should == n_pref.config_id
26
26
  end
27
+
28
+ it 'should pull all preferences for a given configuration' do
29
+ config = VasConfig.get_all.choice
30
+
31
+ all = VasPreference.get_all(:config_id => config.id)
32
+ all.should_not be_empty
33
+ all.each { |p| p.config_id.should == config.id }
34
+ end
35
+
36
+ it 'should have an nvt_name if nvt_id exists' do
37
+ config = VasConfig.get_all.choice
38
+
39
+ all = VasPreference.get_all(:config_id => config.id)
40
+ all.should_not be_empty
41
+
42
+ all.each { |p|
43
+ if p.nvt_id && !p.nvt_id.empty?
44
+ p.nvt_name.should_not be nil
45
+ p.nvt_name.should_not be_empty
46
+ end
47
+ }
48
+ end
49
+
50
+ it 'should sort by name' do
51
+ config = VasConfig.get_all.choice
52
+
53
+ all = VasPreference.get_all(:config_id => config.id, :sort_by => :name)
54
+ all.each { |p|
55
+ old_name = p.name unless old_name
56
+ p.name.should >= old_name
57
+ old_name = p.name
58
+ }
59
+ end
60
+
61
+ it 'should sort by nvt_id' do
62
+ config = VasConfig.get_all.choice
63
+
64
+ all = VasPreference.get_all(:config_id => config.id, :sort_by => :nvt_id)
65
+ all.each { |p|
66
+ old_id = p.nvt_id unless old_id
67
+ p.nvt_id.should >= old_id
68
+ old_id = p.nvt_id
69
+ }
70
+ end
71
+
72
+ it 'should sort by nvt_name' do
73
+ config = VasConfig.get_all.choice
74
+
75
+ all = VasPreference.get_all(:config_id => config.id, :sort_by => :nvt_name)
76
+ all.each { |p|
77
+ old_name = p.nvt_name unless old_name
78
+ p.nvt_name.should >= old_name
79
+ old_name = p.nvt_name
80
+ }
81
+ end
27
82
  end
28
83
  end
@@ -92,18 +92,18 @@ module OpenvasCli
92
92
  it 'should sort by task_name (default :descending)' do
93
93
  last_name = nil
94
94
  VasReport.get_all(:sort => :task_name).each { |r|
95
- last_name = r.started_at unless last_name
96
- r.started_at.should <= last_name
97
- last_date = r.started_at
95
+ last_name = r.task_name unless last_name
96
+ r.task_name.should <= last_name
97
+ last_date = r.task_name
98
98
  }
99
99
  end
100
100
 
101
101
  it 'should sort by :task_name, :ascending' do
102
102
  last_name = nil
103
103
  VasReport.get_all(:sort => :task_name, :sort_order => :ascending).each { |r|
104
- last_name = r.started_at unless last_name
105
- r.started_at.should >= last_name
106
- last_date = r.started_at
104
+ last_name = r.task_name unless last_name
105
+ r.task_name.should >= last_name
106
+ last_date = r.task_name
107
107
  }
108
108
  end
109
109
  end
@@ -2,6 +2,10 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  module OpenvasCli
4
4
  describe VasResult do
5
+ def get_random_result
6
+ VasResult.get_all.choice
7
+ end
8
+
5
9
  it 'should pull all results' do
6
10
  all_results = VasResult.get_all
7
11
 
@@ -114,31 +118,31 @@ module OpenvasCli
114
118
  end
115
119
 
116
120
  it 'should not raise an error on save!' do
117
- result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
121
+ result = get_random_result
118
122
  lambda {
119
123
  result.save!
120
124
  }.should_not raise_error
121
125
  end
122
126
 
123
127
  it 'should return true on save' do
124
- result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
128
+ result = get_random_result
125
129
  result.save.should be true
126
130
  end
127
131
 
128
132
  it 'should not raise an error on destroy!' do
129
- result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
133
+ result = get_random_result
130
134
  lambda {
131
135
  result.destroy!
132
136
  }.should_not raise_error
133
137
  end
134
138
 
135
139
  it 'should return true on save' do
136
- result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
140
+ result = get_random_result
137
141
  result.destroy.should be true
138
142
  end
139
143
 
140
144
  it 'should return a result by ID' do
141
- result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
145
+ result = get_random_result
142
146
 
143
147
  nr = VasResult.get_by_id(result.id)
144
148
  nr.should_not be nil
@@ -146,5 +146,35 @@ module OpenvasCli
146
146
 
147
147
  t.should be_changed
148
148
  end
149
+
150
+ it 'should pull hosts_string' do
151
+ t = VasTarget.new(valid_params)
152
+ t.hosts << ['10.10.10.0/24', '192.168.1.0/24']
153
+
154
+ t.hosts_string.should == (VasTarget.get_local_subnets + ['10.10.10.0/24', '192.168.1.0/24']).join(", ")
155
+ end
156
+
157
+ it 'should parse hosts_string' do
158
+ my_hosts = ['10.10.10.0/24', '192.168.1.0/24']
159
+
160
+ t = VasTarget.new(valid_params)
161
+ t.hosts_string = my_hosts.join(", ")
162
+
163
+ t.hosts.each { |h|
164
+ my_hosts.include?(h).should be true
165
+ }
166
+ end
167
+
168
+ it 'update_attributes should update hosts when hosts_string is passed' do
169
+ t = VasTarget.new(valid_params)
170
+ org_hosts_string = t.hosts_string
171
+ new_hosts = ['10.10.10.0/24', '192.168.1.0/24']
172
+ t.update_attributes({:hosts_string => new_hosts.join(", ")})
173
+
174
+ t.hosts_string.should_not == org_hosts_string
175
+ t.hosts.each { |h|
176
+ new_hosts.include?(h).should be true
177
+ }
178
+ end
149
179
  end
150
180
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openvas-cli
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 8
10
- version: 0.2.8
9
+ - 9
10
+ version: 0.2.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Reed Swenson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-07 00:00:00 -05:00
18
+ date: 2011-04-13 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -23,7 +23,7 @@ dependencies:
23
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - "="
27
27
  - !ruby/object:Gem::Version
28
28
  hash: 13
29
29
  segments:
@@ -39,7 +39,7 @@ dependencies:
39
39
  version_requirements: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ">="
42
+ - - "="
43
43
  - !ruby/object:Gem::Version
44
44
  hash: 13
45
45
  segments:
@@ -55,7 +55,7 @@ dependencies:
55
55
  version_requirements: &id003 !ruby/object:Gem::Requirement
56
56
  none: false
57
57
  requirements:
58
- - - ">="
58
+ - - "="
59
59
  - !ruby/object:Gem::Version
60
60
  hash: 13
61
61
  segments: