openvas-cli 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.2.7
@@ -19,6 +19,12 @@ module OpenvasCli
19
19
 
20
20
  attr_accessor :id
21
21
 
22
+ def initialize(attributes={})
23
+ attributes.each { |key, value|
24
+ instance_variable_set("@#{key}", value)
25
+ }
26
+ end
27
+
22
28
  def new_record?
23
29
  @id == nil || @id.empty?
24
30
  end
@@ -30,5 +36,42 @@ module OpenvasCli
30
36
  def to_param
31
37
  id
32
38
  end
39
+
40
+ def to_key
41
+ if new_record?
42
+ nil
43
+ else
44
+ [@id]
45
+ end
46
+ end
47
+
48
+ def save!
49
+ raise(VasExceptions::RecordNotSaved) unless valid?
50
+ create_or_update || raise(VasExceptions::RecordNotSaved)
51
+ end
52
+
53
+ def save
54
+ create_or_update if valid?
55
+ end
56
+
57
+ def destroy!
58
+ delete_record || raise(VasExceptions::RecordNotSaved)
59
+ end
60
+
61
+ def destroy
62
+ delete_record
63
+ end
64
+
65
+ def self.get_by_id(id)
66
+ get_all(:id => id).first
67
+ end
68
+
69
+ def create_or_update
70
+ true
71
+ end
72
+
73
+ def delete_record
74
+ true
75
+ end
33
76
  end
34
77
  end
@@ -6,5 +6,7 @@ module OpenvasCli
6
6
  # Thrown when some sort of communications error (like a timeout or unexpected
7
7
  # end-of-file) is encountered.
8
8
  class CommunicationException < StandardError; end
9
+
10
+ class RecordNotSaved < StandardError; end
9
11
  end
10
12
  end
@@ -1,6 +1,14 @@
1
1
  require 'vas_base'
2
2
 
3
3
  module OpenvasCli
4
+ class VasLscCredentialValidator < ActiveModel::Validator
5
+ def validate(record)
6
+ if record.password && record.password != record.password_confirmation
7
+ record.errors[:password] << "and Password Confirmation do not match."
8
+ end
9
+ end
10
+ end
11
+
4
12
  class VasLscCredential < VasBase
5
13
 
6
14
  attr_accessor :name
@@ -12,17 +20,12 @@ module OpenvasCli
12
20
  attr_accessor :package
13
21
  attr_accessor :package_type
14
22
  attr_accessor :password
23
+ attr_accessor :password_confirmation
15
24
 
16
25
  validates :name, :presence => true, :length => {:minimum => 1}
17
26
  validates :login, :presence => true, :length => {:minimum => 1}
18
27
 
19
- def initialize(params = {})
20
- @id = params[:id] if params[:id]
21
- @name = params[:name] if params[:name]
22
- @login = params[:login] if params[:login]
23
- @comment = params[:comment] if params[:comment]
24
- @password = params[:password] if params[:password]
25
- end
28
+ validates_with VasLscCredentialValidator
26
29
 
27
30
  def target_keys
28
31
  @target_keys ||= []
@@ -32,9 +35,7 @@ module OpenvasCli
32
35
  @targets ||= []
33
36
  end
34
37
 
35
- def save!
36
- raise "VasLscCredential is not valid" unless valid?
37
-
38
+ def create_or_update
38
39
  if @id
39
40
  #update
40
41
  req = Nokogiri::XML::Builder.new { |xml|
@@ -57,18 +58,33 @@ module OpenvasCli
57
58
  }
58
59
  end
59
60
 
60
- resp = VasLscCredential.connection.send_receive(req.doc)
61
-
62
- @id = VasLscCredential.extract_value_from("create_lsc_credential_response/@id", resp) unless @id
61
+ begin
62
+ resp = VasLscCredential.connection.send_receive(req.doc)
63
+ @id = VasLscCredential.extract_value_from("create_lsc_credential_response/@id", resp) unless @id
64
+
65
+ true
66
+ rescue VasExceptions::CommandException => e
67
+ errors[:command_failure] << e.message
68
+
69
+ nil
70
+ end
63
71
  end
64
72
 
65
- def destroy!
73
+ def delete_record
66
74
  return unless @id
67
75
  req = Nokogiri::XML::Builder.new { |xml|
68
76
  xml.delete_lsc_credential( :lsc_credential_id => @id )
69
77
  }
70
78
 
71
- VasLscCredential.connection.send_receive(req.doc)
79
+ begin
80
+ VasLscCredential.connection.send_receive(req.doc)
81
+
82
+ true
83
+ rescue VasExceptions::CommandException
84
+ errors[:command_failure] << e.message
85
+
86
+ nil
87
+ end
72
88
  end
73
89
 
74
90
  def self.get_all(options={})
@@ -78,12 +94,17 @@ module OpenvasCli
78
94
  xml.get_lsc_credentials(params)
79
95
  }
80
96
 
81
- resp = connection.send_receive(req.doc)
82
-
83
97
  ret = []
84
- resp.xpath("//lsc_credential").each { |cred|
85
- ret << from_xml_node(cred)
86
- }
98
+
99
+ begin
100
+ resp = connection.send_receive(req.doc)
101
+
102
+ resp.xpath("//lsc_credential").each { |cred|
103
+ ret << from_xml_node(cred)
104
+ }
105
+ rescue VasExceptions::CommandException => e
106
+
107
+ end
87
108
 
88
109
  ret
89
110
  end
@@ -39,19 +39,13 @@ module OpenvasCli
39
39
  value_will_change! unless val == @value
40
40
  @value = val
41
41
  end
42
-
43
- def initialize(attributes={})
44
- @name = attributes[:name] if attributes[:name]
45
- @value = attributes[:value] if attributes[:value]
46
- @config_id = attributes[:config_id] if attributes[:config_id]
47
- end
48
-
42
+
49
43
  def config
50
- @config ||= VasConfig.get_all(:id => @config_id)[0]
44
+ @config ||= VasConfig.get_by_id(@config_id)
51
45
  end
52
46
 
53
47
  def nvt
54
- @nvt ||= VasNVT.get_all(:id => @nvt_id)[0]
48
+ @nvt ||= VasNVT.get_by_id(@nvt_id)
55
49
  end
56
50
 
57
51
  def val_choices
@@ -66,8 +60,11 @@ module OpenvasCli
66
60
  end
67
61
  end
68
62
 
69
- def save!
70
- raise "Cannot save a preference that is not tied to a configuration" unless @config_id
63
+ def create_or_update
64
+ unless @config_id
65
+ errors[:config_id] << "Config_id required to save"
66
+ return
67
+ end
71
68
  req = Nokogiri::XML::Builder.new { |xml|
72
69
  xml.modify_config(:config_id => @config_id) {
73
70
  xml.preference {
@@ -84,7 +81,19 @@ module OpenvasCli
84
81
  }
85
82
  }
86
83
 
87
- VasPreference.connection.send_receive(req.doc)
84
+ begin
85
+ VasPreference.connection.send_receive(req.doc)
86
+
87
+ true
88
+ rescue VasExceptions::CommandException => e
89
+ errors[:command_failure] << e.message
90
+
91
+ nil
92
+ end
93
+ end
94
+
95
+ def self.get_by_id(id)
96
+ nil
88
97
  end
89
98
 
90
99
  # Pulls Vas preferences.
@@ -3,14 +3,12 @@ require 'vas_base'
3
3
  module OpenvasCli
4
4
  # Contains the details of a single OpenVAS report.
5
5
  class VasReport < VasBase
6
- attr_accessor :report_id
7
6
  attr_accessor :task_id
8
7
  attr_accessor :task_name
9
8
  attr_accessor :started_at
10
9
  # Overall Status Only
11
10
  attr_accessor :status
12
11
 
13
- validates :report_id, :presence => true, :UUID => true
14
12
  validates :task_id, :presence => true, :UUID => true,
15
13
  :unless => Proc.new { |report| report.empty? }
16
14
 
@@ -19,9 +17,11 @@ module OpenvasCli
19
17
  # === Options:
20
18
  # [:report_id => [report_id]] Pulls a specific +report_id+. If the id provided is bogus, an empty set is returned.
21
19
  # [:filter => [array_of_filter_symbols]] Filters the report results by severity. Valid symbols are: [:high, :medium, :low, :log, :deubg].
20
+ # [:sort => [sort_field]] Sorts the report by the given field. Possible values are +:task_name+, +:started_at+. defaults to +:started_at+
21
+ # [:sort_order => [:ascending, :descending]] Order of sort. Defaults to :descending.
22
22
  def self.get_all(options={})
23
23
  params = {}
24
- params[:report_id] = options[:report_id] if options[:report_id]
24
+ params[:report_id] = options[:id] if options[:id]
25
25
  if options[:filter]
26
26
  params[:levels] = ""
27
27
  options[:filter].each { |f|
@@ -57,48 +57,65 @@ module OpenvasCli
57
57
 
58
58
  ret = []
59
59
  repts.xpath('//report').each { |r|
60
- r_id = VasReport.new
61
- r_id.report_id = extract_value_from("@id", r)
62
- r_id.task_id = extract_value_from("task/@id", r)
63
- r_id.task_name = extract_value_from("task/name", r)
64
- r_id.status = extract_value_from("scan_run_status", r)
65
- r_id.started_at = extract_value_from("scan_start", r)
60
+ rep = VasReport.new
61
+ rep.id = extract_value_from("@id", r)
62
+ rep.task_id = extract_value_from("task/@id", r)
63
+ rep.task_name = extract_value_from("task/name", r)
64
+ rep.status = extract_value_from("scan_run_status", r)
65
+ rep.started_at = extract_value_from("scan_start", r)
66
66
 
67
- r_id.result_count[:total] = extract_value_from("result_count/full", r).to_i
68
- r_id.result_count[:filtered] = extract_value_from("result_count/filtered", r).to_i
69
- r_id.result_count[:debug][:total] = extract_value_from("result_count/debug/full", r).to_i
70
- r_id.result_count[:debug][:filtered] = extract_value_from("result_count/debug/filtered", r).to_i
71
- r_id.result_count[:high][:total] = extract_value_from("result_count/hole/full", r).to_i
72
- r_id.result_count[:high][:filtered] = extract_value_from("result_count/hold/filtered", r).to_i
73
- r_id.result_count[:low][:total] = extract_value_from("result_count/info/full", r).to_i
74
- r_id.result_count[:low][:filtered] = extract_value_from("result_count/info/filtered", r).to_i
75
- r_id.result_count[:log][:total] = extract_value_from("result_count/log/full", r).to_i
76
- r_id.result_count[:log][:filtered] = extract_value_from("result_count/log/filtered", r).to_i
77
- r_id.result_count[:medium][:total] = extract_value_from("result_count/warning/full", r).to_i
78
- r_id.result_count[:medium][:filtered] = extract_value_from("result_count/warning/filtered", r).to_i
67
+ rep.result_count[:total] = extract_value_from("result_count/full", r).to_i
68
+ rep.result_count[:filtered] = extract_value_from("result_count/filtered", r).to_i
69
+ rep.result_count[:debug][:total] = extract_value_from("result_count/debug/full", r).to_i
70
+ rep.result_count[:debug][:filtered] = extract_value_from("result_count/debug/filtered", r).to_i
71
+ rep.result_count[:high][:total] = extract_value_from("result_count/hole/full", r).to_i
72
+ rep.result_count[:high][:filtered] = extract_value_from("result_count/hold/filtered", r).to_i
73
+ rep.result_count[:low][:total] = extract_value_from("result_count/info/full", r).to_i
74
+ rep.result_count[:low][:filtered] = extract_value_from("result_count/info/filtered", r).to_i
75
+ rep.result_count[:log][:total] = extract_value_from("result_count/log/full", r).to_i
76
+ rep.result_count[:log][:filtered] = extract_value_from("result_count/log/filtered", r).to_i
77
+ rep.result_count[:medium][:total] = extract_value_from("result_count/warning/full", r).to_i
78
+ rep.result_count[:medium][:filtered] = extract_value_from("result_count/warning/filtered", r).to_i
79
79
 
80
80
  r.xpath("results/result").each { |result|
81
- r_id.results << VasResult.parse_result_node(result)
81
+ rep.results << VasResult.parse_result_node(result)
82
82
  }
83
83
 
84
- ret << r_id
84
+ ret << rep
85
85
  }
86
+
87
+ options[:sort] = :started_at unless options[:sort]
88
+ options[:sort_order] = :descending unless options[:sort_order]
89
+
90
+ if options[:sort] == :started_at
91
+ if options[:sort_order] == :ascending
92
+ ret.sort! { |a,b| a.started_at <=> b.started_at }
93
+ else
94
+ ret.sort! { |a,b| b.started_at <=> a.started_at }
95
+ end
96
+ elsif options[:sort] == :task_name
97
+ if options[:sort_order] == :ascending
98
+ ret.sort! { |a,b| a.task_name <=> b.task_name }
99
+ else
100
+ ret.sort! { |a,b| b.task_name <=> a.task_name }
101
+ end
102
+ end
86
103
 
87
104
  ret
88
105
  end
89
106
 
90
107
  def to_xml
91
108
  req = Nokogiri::XML::Builder.new { |xml|
92
- xml.get_reports(:report_id => @report_id)
109
+ xml.get_reports(:report_id => @id)
93
110
  }
94
111
 
95
112
  report = VasReport.connection.send_receive(req.doc)
96
113
 
97
- report.at_xpath('//report').to_xml
114
+ report.at_xpath('/get_reports_response/report').to_xml
98
115
  end
99
116
 
100
117
  def task
101
- @task ||= VasTask.get_all(:task_id => @task_id)[0]
118
+ @task ||= VasTask.get_by_id(@task_id)
102
119
  end
103
120
 
104
121
  def results
@@ -6,14 +6,18 @@ module OpenvasCli
6
6
  attr_accessor :result_id, :subnet, :host, :port, :rule_id, :threat,
7
7
  :description, :notes, :overrides, :task_id
8
8
 
9
- validates :result_id, :presence=>true, :UUID=>true
10
-
9
+ validates :id, :presence=>true, :UUID=>true
10
+
11
+ def self.get_by_id(id)
12
+ nil
13
+ end
14
+
11
15
  def self.get_all(options = {})
12
16
  options[:sort_by] ||= :threat
13
17
 
14
18
  params = {:overrides => 0, :notes => 0}
15
- if options[:id]
16
- params[:task_id] = options[:id]
19
+ if options[:task_id]
20
+ params[:task_id] = options[:task_id]
17
21
  params[:apply_overrides] = 1 if options[:apply_overrides]
18
22
  end
19
23
 
@@ -72,7 +76,7 @@ module OpenvasCli
72
76
 
73
77
  def self.parse_result_node(node, task_id = nil)
74
78
  res = VasResult.new
75
- res.result_id = extract_value_from("@id", node)
79
+ res.id = extract_value_from("@id", node)
76
80
  res.threat = extract_value_from("threat", node)
77
81
  res.subnet = extract_value_from("subnet", node)
78
82
  res.host = extract_value_from("host", node)
@@ -18,14 +18,6 @@ module OpenvasCli
18
18
 
19
19
  define_attribute_methods [:name, :comment, :first_time, :period]
20
20
 
21
- def initialize(params = {})
22
- @name = params[:name] if params[:name]
23
- @comment = params[:comment] if params[:comment]
24
- @first_time = params[:first_time] if params[:first_time]
25
- @period = params[:period] if params[:period]
26
- reset_changes
27
- end
28
-
29
21
  def changed?
30
22
  local_changes = false
31
23
  local_changes = @period.changed? if @period
@@ -53,23 +45,31 @@ module OpenvasCli
53
45
  @period = v
54
46
  end
55
47
 
56
- def destroy!
48
+ def delete_record
57
49
  return unless @id
58
50
 
59
51
  req = Nokogiri::XML::Builder.new { |xml|
60
52
  xml.delete_schedule(:schedule_id => @id)
61
53
  }
62
54
 
63
- VasSchedule.connection.send_receive(req.doc)
64
- @id = nil
65
- reset_changes
55
+ begin
56
+ VasSchedule.connection.send_receive(req.doc)
57
+ @id = nil
58
+ reset_changes
59
+
60
+ true
61
+ rescue VasExceptions::CommandException => e
62
+ errors[:command_failure] << e.message
63
+
64
+ nil
65
+ end
66
66
  end
67
67
 
68
- def save!
68
+ def create_or_update
69
69
  return unless changed? || @id == nil
70
70
 
71
71
  if @id
72
- destroy!
72
+ return unless destroy
73
73
  end
74
74
 
75
75
  req = Nokogiri::XML::Builder.new { |xml|
@@ -96,9 +96,17 @@ module OpenvasCli
96
96
  }
97
97
  }
98
98
 
99
- resp = VasSchedule.connection.send_receive(req.doc)
100
- @id = VasSchedule.extract_value_from("/create_schedule_response/@id", resp) unless @id
101
- reset_changes
99
+ begin
100
+ resp = VasSchedule.connection.send_receive(req.doc)
101
+ @id = VasSchedule.extract_value_from("/create_schedule_response/@id", resp) unless @id
102
+ reset_changes
103
+
104
+ true
105
+ rescue VasExceptions::CommandException => e
106
+ errors[:command_failure] << e.message
107
+
108
+ nil
109
+ end
102
110
  end
103
111
 
104
112
  def self.get_all(options = {})
@@ -29,12 +29,13 @@ module OpenvasCli
29
29
 
30
30
  def changed?
31
31
  local_changes = false
32
- local_changes = !@org_hosts.eql?(@hosts) if @org_hosts
33
-
32
+
33
+ local_changes = true unless @org_hosts && @org_hosts.eql?(@hosts)
34
+
34
35
  unless local_changes == true
35
36
  local_changes = credential_changed?(:ssh) || credential_changed?(:smb)
36
37
  end
37
-
38
+
38
39
  local_changes || super
39
40
  end
40
41
 
@@ -53,15 +54,9 @@ module OpenvasCli
53
54
  end
54
55
 
55
56
  def initialize(params = {})
56
- @id = params[:id] if params[:id]
57
- @name = params[:name] if params[:name]
58
- @hosts = params[:hosts] if params[:hosts]
59
- @comment = params[:comment] if params[:comment]
60
- @port_range = params[:port_range] if params[:port_range]
61
-
57
+ super(params)
58
+
62
59
  @org_hosts = @hosts.collect { |h| h } if @hosts
63
-
64
- reset_changes
65
60
  end
66
61
 
67
62
  def credentials
@@ -86,14 +81,14 @@ module OpenvasCli
86
81
 
87
82
  def hosts=(val)
88
83
  hosts_will_change! unless @hosts.eql?(val)
89
- @host = val
84
+ @hosts = val
90
85
 
91
86
  @org_hosts = val.collect { |h| h } if val
92
87
  end
93
88
 
94
- def save!
89
+ def create_or_update
95
90
  if @id
96
- destroy!
91
+ return unless destroy
97
92
  end
98
93
  req = Nokogiri::XML::Builder.new { |xml|
99
94
  xml.create_target {
@@ -105,23 +100,38 @@ module OpenvasCli
105
100
  xml.port_range { xml.text(@port_range) } if @port_range
106
101
  }
107
102
  }
108
- resp = VasTarget.connection.send_receive(req.doc)
109
103
 
110
- @id = VasTarget.extract_value_from("create_target_response/@id", resp)
104
+ begin
105
+ resp = VasTarget.connection.send_receive(req.doc)
106
+ @id = VasTarget.extract_value_from("create_target_response/@id", resp)
107
+ reset_changes
108
+
109
+ true
110
+ rescue VaxExceptions::CommandException => e
111
+ errors[:command_failure] << e.message
112
+
113
+ nil
114
+ end
111
115
 
112
- reset_changes
113
116
  end
114
117
 
115
- def destroy!
118
+ def delete_record
116
119
  return unless @id
117
120
 
118
121
  req = Nokogiri::XML::Builder.new { |xml|
119
122
  xml.delete_target(:target_id => @id)
120
123
  }
121
124
 
122
- VasTarget.connection.send_receive(req.doc)
123
-
124
- @id = nil
125
+ begin
126
+ VasTarget.connection.send_receive(req.doc)
127
+ @id = nil
128
+
129
+ true
130
+ rescue VaxExceptions::CommandException => e
131
+ errors[:command_failure] << e.message
132
+
133
+ nil
134
+ end
125
135
  end
126
136
 
127
137
  def self.create!(params={})
@@ -147,7 +157,9 @@ module OpenvasCli
147
157
  targ.id = extract_value_from("@id", t)
148
158
  targ.name = extract_value_from("name", t)
149
159
  host_string = extract_value_from("hosts", t)
150
- targ.hosts << host_string.split(/, /)
160
+ all_hosts = host_string.split(/,/)
161
+ all_hosts.each { |hst| hst.strip! }
162
+ targ.hosts = all_hosts
151
163
  targ.comment = extract_value_from("comment", t)
152
164
  targ.port_range = extract_value_from("port_range", t)
153
165
  targ.in_use = extract_value_from("in_use", t).to_i > 0
@@ -159,6 +171,8 @@ module OpenvasCli
159
171
  targ.task_keys << extract_value_from("@id", task)
160
172
  }
161
173
 
174
+ targ.reset_changes
175
+
162
176
  ret << targ
163
177
  }
164
178
 
@@ -184,13 +198,14 @@ module OpenvasCli
184
198
  ret = { :ssh => nil, :smb => nil }
185
199
 
186
200
  @credential_keys.each { |key, value|
187
- ret[key] = VasLscCredential.get_all(:id => value) if value && !value.empty?
201
+ ret[key] = VasLscCredential.get_all(:id => value)[0] if value && !value.empty?
188
202
  } if @credential_keys
189
203
 
190
204
  ret
191
205
  end
192
206
 
193
207
  def credential_changed?(key)
208
+ credential_keys[key] = nil if credential_keys[key] && credential_keys[key].empty?
194
209
  if credential_keys[key] == nil && credentials[key] != nil
195
210
  return true
196
211
  elsif credential_keys[key] != nil && credentials[key] == nil
@@ -19,16 +19,6 @@ module OpenvasCli
19
19
 
20
20
 
21
21
  define_attribute_methods [:name, :comment, :config_id, :target_id, :schedule_id]
22
-
23
- def initialize(params = {})
24
- @name = params[:name] if params[:name]
25
- @comment = params[:comment] if params[:comment]
26
- @config_id = params[:config_id] if params[:config_id]
27
- @target_id = params[:target_id] if params[:target_id]
28
- @schedule_id = params[:schedule_id] if params[:schedule_id]
29
- @schedule = params[:schedule] if params[:schedule]
30
- reset_changes
31
- end
32
22
 
33
23
  def name=(val)
34
24
  name_will_change! unless @name == val
@@ -56,7 +46,7 @@ module OpenvasCli
56
46
  end
57
47
 
58
48
  def schedule
59
- @schedule || pull_my_schedule
49
+ @schedule ||= pull_my_schedule
60
50
  end
61
51
 
62
52
  def schedule=(v)
@@ -65,33 +55,32 @@ module OpenvasCli
65
55
  end
66
56
 
67
57
  def config
68
- @config || pull_my_config
58
+ @config ||= pull_my_config
69
59
  end
70
60
 
71
61
  def config=(val)
72
62
  @config = val
73
- config_id = val ? val.id : nil
63
+ config_id = val.id if val
74
64
  end
75
65
 
76
66
  def target
77
- @target || pull_my_target
67
+ @target ||= pull_my_target
78
68
  end
79
69
 
80
70
  def target=(val)
81
71
  @target = val
82
- target_id = val ? val.id : nil
72
+ target_id = val.id if val
83
73
  end
84
74
 
85
- def save!
86
- raise "Invalid State" unless valid?
87
-
75
+ def create_or_update
76
+
88
77
  if schedule && schedule.changed?
89
- schedule.save!
78
+ return unless schedule.save
90
79
  schedule_id = schedule.id
91
80
  end
92
81
 
93
82
  if config.changed?
94
- config.save!
83
+ return unless config.save
95
84
  end
96
85
 
97
86
  req = Nokogiri::XML::Builder.new { |xml|
@@ -112,19 +101,35 @@ module OpenvasCli
112
101
  end
113
102
  }
114
103
 
115
- resp = VasTask.connection.send_receive(req.doc)
104
+ begin
105
+ resp = VasTask.connection.send_receive(req.doc)
106
+ @id = VasTask.extract_value_from("/create_task_response/@id", resp) unless @id
107
+ reset_changes
108
+
109
+ true
110
+ rescue VaxExceptions::CommandException => e
111
+ errors[:command_failure] << e.message
112
+
113
+ nil
114
+ end
116
115
 
117
- @id = VasTask.extract_value_from("/create_task_response/@id", resp) unless @id
118
116
 
119
- reset_changes
120
117
  end
121
118
 
122
- def destroy!
119
+ def delete_record
123
120
  req = Nokogiri::XML::Builder.new { |xml|
124
121
  xml.delete_task(:task_id => @id)
125
122
  }
126
123
 
127
- VasTask.connection.send_receive(req.doc)
124
+ begin
125
+ VasTask.connection.send_receive(req.doc)
126
+
127
+ true
128
+ rescue VaxExceptions::CommandException => e
129
+ errors[:command_failure] << e.message
130
+
131
+ nil
132
+ end
128
133
  end
129
134
 
130
135
  def start
@@ -174,10 +179,10 @@ module OpenvasCli
174
179
  def self.get_all(options = {})
175
180
  params = {:apply_overrides => 0, :sort_field => "name"}
176
181
 
177
- params[:task_id] = options[:task_id] if options[:task_id]
182
+ params[:task_id] = options[:id] if options[:id]
178
183
 
179
184
  req = Nokogiri::XML::Builder.new { |xml|
180
- xml.get_tasks
185
+ xml.get_tasks(params)
181
186
  }
182
187
 
183
188
  tasks = connection.send_receive(req.doc)
@@ -191,20 +196,22 @@ module OpenvasCli
191
196
  end
192
197
 
193
198
  def self.from_xml_node(node)
194
- t_id = VasTask.new
195
- t_id.id = extract_value_from("@id", node)
196
- t_id.name = extract_value_from("name", node)
197
- t_id.comment = extract_value_from("comment", node)
198
- t_id.status = extract_value_from("status", node)
199
+ t = VasTask.new
200
+ t.id = extract_value_from("@id", node)
201
+ t.name = extract_value_from("name", node)
202
+ t.comment = extract_value_from("comment", node)
203
+ t.status = extract_value_from("status", node)
199
204
  if node.at_xpath("progress")
200
- t_id.progress = VasTaskProgress.from_xml_node(node.at_xpath("progress"))
205
+ t.progress = VasTaskProgress.from_xml_node(node.at_xpath("progress"))
201
206
  end
202
- t_id.times_run = extract_value_from("report_count/finished", node).to_i
203
- t_id.last_report_id = extract_value_from("last_report/report/@id", node)
204
- t_id.config_id = extract_value_from("config/@id", node)
205
- t_id.target_id = extract_value_from("target/@id", node)
207
+ t.times_run = extract_value_from("report_count/finished", node).to_i
208
+ t.last_report_id = extract_value_from("last_report/report/@id", node)
209
+ t.config_id = extract_value_from("config/@id", node)
210
+ t.target_id = extract_value_from("target/@id", node)
211
+
212
+ t.reset_changes
206
213
 
207
- t_id
214
+ t
208
215
  end
209
216
 
210
217
  private
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.6"
8
+ s.version = "0.2.7"
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-03-28}
12
+ s.date = %q{2011-04-04}
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 = [
@@ -19,10 +19,23 @@ module OpenvasCli
19
19
  :name => next_test_name,
20
20
  :login => "FOOBAR",
21
21
  :comment => "Yet another test credential",
22
- :password => "thisCanTr3@llyBe1"
22
+ :password => "thisCanTr3@llyBe1",
23
+ :password_confirmation => "thisCanTr3@llyBe1"
23
24
  }
24
25
  end
25
26
 
27
+ it 'should require password and confirmation to be the same' do
28
+ cred = VasLscCredential.new(valid_params)
29
+ cred.should be_valid
30
+
31
+ cred.password_confirmation = "something different"
32
+ cred.should_not be_valid
33
+
34
+ cred.should have(1).errors
35
+
36
+ cred.errors[:password].should_not be nil
37
+ end
38
+
26
39
  it 'should pull all available credentials' do
27
40
  all = VasLscCredential.get_all
28
41
  all.should_not be nil
@@ -36,7 +49,7 @@ module OpenvasCli
36
49
  cred.id.should_not be nil
37
50
  cred.id.should_not be_empty
38
51
 
39
- n_cred = VasLscCredential.get_all(:id => cred.id)[0]
52
+ n_cred = VasLscCredential.get_by_id(cred.id)
40
53
  n_cred.should_not be nil
41
54
  n_cred.id.should == cred.id
42
55
  n_cred.name.should == cred.name
@@ -48,13 +61,13 @@ module OpenvasCli
48
61
  cred = VasLscCredential.new(valid_params)
49
62
  cred.save!
50
63
 
51
- n_cred = VasLscCredential.get_all(:id => cred.id)[0]
64
+ n_cred = VasLscCredential.get_by_id(cred.id)
52
65
  n_cred.login = "NOT_FOOBAR"
53
66
  n_cred.save!
54
67
 
55
68
  n_cred.id.should == cred.id
56
69
 
57
- n_cred = VasLscCredential.get_all(:id => cred.id)[0]
70
+ n_cred = VasLscCredential.get_by_id(cred.id)
58
71
  n_cred.login.should == "NOT_FOOBAR"
59
72
  end
60
73
 
@@ -64,9 +77,13 @@ module OpenvasCli
64
77
  cred.id.should_not be nil
65
78
  cred.id.should_not be_empty
66
79
 
67
- n_cred = VasLscCredential.get_all(:id => cred.id)[0]
80
+ n_cred = VasLscCredential.get_by_id(cred.id)
68
81
  n_cred.should_not be nil
69
82
  n_cred.login.should == 'FOO\\BAR'
70
83
  end
84
+
85
+ it 'should not barf if it is given an invalid ID to pull' do
86
+ lambda {VasLscCredential.get_all(:id => 'foo')}.should_not raise_error
87
+ end
71
88
  end
72
89
  end
@@ -9,14 +9,14 @@ module OpenvasCli
9
9
  rules.each{ |r| r.should be_valid }
10
10
  end
11
11
 
12
- it 'should pull an NVT by OID', :slow => true do
13
- id = VasNVT.get_all.choice.id
12
+ it 'should pull an NVT by OID' do
13
+ fam = VasNVTFamily.get_all.choice
14
+
15
+ id = VasNVT.get_all(:family => fam.name).choice.id
14
16
 
15
- rule = VasNVT.get_all(:id => id)
17
+ rule = VasNVT.get_by_id(id)
16
18
  rule.should_not be nil
17
- rule.count.should == 1
18
-
19
- rule[0].should be_valid
19
+ rule.should be_valid
20
20
  end
21
21
 
22
22
  it 'should pull an NVT by Family' do
@@ -12,24 +12,22 @@ module OpenvasCli
12
12
  end
13
13
 
14
14
  it 'should pull a raw XML dump for a single report' do
15
- rept = VasReport.get_all[0]
15
+ rept = VasReport.get_all.choice
16
16
 
17
17
  xml = rept.to_xml
18
18
 
19
19
  xdoc = Nokogiri::XML(xml) #it should be a valid xml document
20
20
 
21
- xdoc.at_xpath("//report/@id").value.should == rept.report_id
21
+ xdoc.at_xpath("//report/@id").value.should == rept.id
22
22
  end
23
23
 
24
24
  it 'should pull a specific report by id' do
25
- all = VasReport.get_all
26
-
27
- o_rept = all[rand(all.count)]
25
+ o_rept = VasReport.get_all.choice
28
26
  o_rept.should_not be nil
29
- n_rept = VasReport.get_all(:report_id => o_rept.report_id)[0]
27
+ n_rept = VasReport.get_by_id(o_rept.id)
30
28
  n_rept.should_not be nil
31
29
 
32
- o_rept.report_id.should == n_rept.report_id
30
+ o_rept.id.should == n_rept.id
33
31
  end
34
32
 
35
33
  it 'should report the associated results' do
@@ -68,10 +66,19 @@ module OpenvasCli
68
66
 
69
67
  it 'should return an empty set when passed a bad report_id' do
70
68
  lambda {
71
- r = VasReport.get_all(:report_id => '0000000000')
72
- r.should_not be nil
73
- r.should be_empty
69
+ r = VasReport.get_by_id(:id => '0000000000')
70
+ r.should be nil
74
71
  }.should_not raise_error
75
72
  end
73
+
74
+ it 'default sort should be by started_at descending' do
75
+
76
+ end
77
+
78
+ it 'just adding :sort_order => :ascending should reverse the started_at sort'
79
+
80
+ it 'should sort by task_name (default :descending)'
81
+
82
+ it 'should sort by :task_name, :ascending'
76
83
  end
77
84
  end
@@ -40,8 +40,8 @@ module OpenvasCli
40
40
 
41
41
  last_id = ""
42
42
  results.each { |r|
43
- r.result_id.should >= last_id
44
- last_id = r.result_id
43
+ r.id.should >= last_id
44
+ last_id = r.id
45
45
  }
46
46
  end
47
47
 
@@ -60,8 +60,8 @@ module OpenvasCli
60
60
 
61
61
  last_val = ""
62
62
  results.each { |r|
63
- r.rule_id.should >= last_val
64
- last_val = r.rule_id
63
+ r.id.should >= last_val
64
+ last_val = r.id
65
65
  }
66
66
  end
67
67
 
@@ -89,7 +89,7 @@ module OpenvasCli
89
89
  tasks = VasTask.get_all
90
90
 
91
91
  unless tasks.empty?
92
- task_id = tasks[0].task_id
92
+ task_id = tasks.choice.id
93
93
  results = VasResult.get_all(:task_id => task_id)
94
94
  results.each { |r| r.task_id.should == task_id }
95
95
 
@@ -106,5 +106,35 @@ module OpenvasCli
106
106
  threats.should include r.threat
107
107
  }
108
108
  end
109
+
110
+ it 'should return null when pulling by task_id' do
111
+ lambda {
112
+ VasResult.get_by_id('foo').should be nil
113
+ }.should_not raise_error
114
+ end
115
+
116
+ it 'should not raise an error on save!' do
117
+ result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
118
+ lambda {
119
+ result.save!
120
+ }.should_not raise_error
121
+ end
122
+
123
+ it 'should return true on save' do
124
+ result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
125
+ result.save.should be true
126
+ end
127
+
128
+ it 'should not raise an error on destroy!' do
129
+ result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
130
+ lambda {
131
+ result.destroy!
132
+ }.should_not raise_error
133
+ end
134
+
135
+ it 'should return true on save' do
136
+ result = VasResult.get_all(:task_id => VasTask.get_all.choice.id).choice
137
+ result.destroy.should be true
138
+ end
109
139
  end
110
140
  end
@@ -32,11 +32,16 @@ module OpenvasCli
32
32
 
33
33
  unless all.empty?
34
34
  schedule_id = all.choice.id
35
- schedules = VasSchedule.get_all(:id => schedule_id)
36
- schedules.count.should == 1
37
- schedules[0].id.should == schedule_id
35
+ schedule = VasSchedule.get_by_id(schedule_id)
36
+ schedule.should_not be nil
38
37
  end
39
38
  end
39
+
40
+ it 'should return nil if bogus ID is provided' do
41
+ lambda {
42
+ VasSchedule.get_by_id("8675309").should be nil
43
+ }.should_not raise_error
44
+ end
40
45
 
41
46
  it 'default sort should be by name' do
42
47
  all = VasSchedule.get_all
@@ -86,7 +91,7 @@ module OpenvasCli
86
91
  s.should_not be_changed
87
92
  s.id.should_not be nil
88
93
 
89
- new_s = VasSchedule.get_all(:id => s.id)[0]
94
+ new_s = VasSchedule.get_by_id(s.id)
90
95
  new_s.should_not be nil
91
96
  new_s.id.should == s.id
92
97
  new_s.name.should == s.name
@@ -106,9 +111,9 @@ module OpenvasCli
106
111
 
107
112
  s.id.should_not == old_id
108
113
 
109
- VasSchedule.get_all(:id => old_id).should be_empty
114
+ VasSchedule.get_by_id(old_id).should be nil
110
115
 
111
- new_s = VasSchedule.get_all(:id => s.id)[0]
116
+ new_s = VasSchedule.get_by_id(s.id)
112
117
  new_s.should_not be nil
113
118
  new_s.id.should == s.id
114
119
  new_s.name.should == s.name
@@ -69,14 +69,14 @@ module OpenvasCli
69
69
 
70
70
  t.id.should_not be nil
71
71
 
72
- n_t = VasTarget.get_all(:id => t.id)[0]
72
+ n_t = VasTarget.get_by_id(t.id)
73
73
  n_t.should_not be nil
74
74
  n_t.id.should == t.id
75
75
  n_t.name.should == t.name
76
76
  n_t.should have(t.hosts.count).hosts
77
- # n_t.hosts.each { |h|
78
- # t.hosts.should include h
79
- # }
77
+ n_t.hosts.each { |h|
78
+ t.hosts.should include h
79
+ }
80
80
  end
81
81
 
82
82
  it 'should record a change when the name has changed' do
@@ -133,9 +133,8 @@ module OpenvasCli
133
133
 
134
134
  t.id.should_not == org_id
135
135
 
136
- srch = VasTarget.get_all(:id => org_id)
137
- srch.should_not be nil
138
- srch.size.should == 0
136
+ srch = VasTarget.get_by_id(org_id)
137
+ srch.should be nil
139
138
  end
140
139
 
141
140
  it 'should show a change when a new credential is added' do
@@ -60,19 +60,22 @@ module OpenvasCli
60
60
 
61
61
  tasks.should_not be nil
62
62
 
63
- tasks.each { |t| t.should be_valid }
63
+ tasks.each { |t|
64
+ t.should be_valid
65
+ }
64
66
  end
65
67
 
66
68
  it 'should create a task' do
67
69
  t = VasTask.new(valid_params)
68
- t.save!
70
+ t.save.should_not be nil
69
71
 
70
72
  t.id.should_not be nil
71
73
  t.id.should_not be_empty
72
74
 
73
- new_t = VasTask.get_all(:id => t.id)[0]
75
+ new_t = VasTask.get_by_id(t.id)
74
76
  new_t.should_not be nil
75
77
 
78
+ new_t.id.should == t.id
76
79
  new_t.name.should == t.name
77
80
  new_t.config_id.should == t.config_id
78
81
  new_t.target_id.should == t.target_id
@@ -86,9 +89,9 @@ module OpenvasCli
86
89
  if all.length > 0
87
90
  task = all[0]
88
91
 
89
- new_task = VasTask.get_all(:task_id => task.task_id)[0]
92
+ new_task = VasTask.get_by_id(task.id)
90
93
 
91
- task.task_id.should == new_task.task_id
94
+ task.id.should == new_task.id
92
95
  end
93
96
  end
94
97
 
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
  require 'rspec'
6
6
  require 'log4r'
7
7
  require 'fileutils'
8
- require 'openvas_cli'
8
+ require 'openvas-cli'
9
9
 
10
10
  Dir["#{File.dirname(__FILE__)}/../lib/openvas-cli/vas_*.rb"].each {|f| require f}
11
11
  # Requires supporting files with custom matchers and macros, etc,
@@ -13,7 +13,7 @@ Dir["#{File.dirname(__FILE__)}/../lib/openvas-cli/vas_*.rb"].each {|f| require f
13
13
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
14
14
 
15
15
  RSpec.configure do |config|
16
- # config.filter_run_excluding :slow => true
16
+ config.filter_run_excluding :slow => true
17
17
  end
18
18
 
19
19
 
@@ -29,9 +29,6 @@ log_out = Log4r::RollingFileOutputter.new('test_log' ,
29
29
  :maxsize => 10485760})
30
30
  log_out.level = Log4r::INFO
31
31
  logger.outputters = log_out
32
- #VasManager.logger = logger
33
- #VasManager.user = "admin"
34
- #VasManager.password = "Password"
35
32
 
36
33
  OpenvasCli.configure { |config|
37
34
  config.password = "Password"
data/vas_test.rb CHANGED
@@ -8,5 +8,6 @@ OpenvasCli.configure { |config|
8
8
 
9
9
  families = OpenvasCli::VasNVTFamily.get_all
10
10
  nvts = OpenvasCli::VasNVT.get_all(:family => families.choice.name)
11
- nvt = nvts.choice
12
- puts nvt.to_param
11
+ nvts.each { |n|
12
+ puts "risk_factor => #{n.risk_factor}"
13
+ }
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 6
10
- version: 0.2.6
9
+ - 7
10
+ version: 0.2.7
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-03-28 00:00:00 -05:00
18
+ date: 2011-04-04 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency