Class: OpenvasCli::VasConfig

Inherits:
VasBase
  • Object
show all
Defined in:
lib/openvas-cli/vas_config.rb

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#destroy, #destroy!, get_by_id, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

#initialize(attributes = {}) ⇒ VasConfig

Returns a new instance of VasConfig.



21
22
23
24
25
26
27
# File 'lib/openvas-cli/vas_config.rb', line 21

def initialize(attributes = {})
  @id = attributes[:id] if attributes[:id]
  @name = attributes[:name] if attributes[:name]
  @comment = attributes[:comment] if attributes[:comment] 
  @families_grow = attributes[:families_grow] if attributes[:families_grow]
  @rules_grow = attributes[:rules_grow] if attributes[:rules_grow]
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



10
11
12
# File 'lib/openvas-cli/vas_config.rb', line 10

def comment
  @comment
end

#families_growObject

Returns the value of attribute families_grow.



11
12
13
# File 'lib/openvas-cli/vas_config.rb', line 11

def families_grow
  @families_grow
end

#in_useObject

Returns the value of attribute in_use.



13
14
15
# File 'lib/openvas-cli/vas_config.rb', line 13

def in_use
  @in_use
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/openvas-cli/vas_config.rb', line 9

def name
  @name
end

#rules_growObject

Returns the value of attribute rules_grow.



12
13
14
# File 'lib/openvas-cli/vas_config.rb', line 12

def rules_grow
  @rules_grow
end

Class Method Details

.copy_config(id, name, comment = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/openvas-cli/vas_config.rb', line 84

def self.copy_config(id, name, comment = nil)
  begin
    req = Nokogiri::XML::Builder.new { |xml|
      xml.create_config {
        xml.copy { xml.text(id) }
        xml.name { xml.text(name) }
        if comment && !comment.empty?
          xml.comment { xml.text(comment) }
        end
      }
    }
    
    resp = connection.send_receive(req.doc)
    new_id = extract_value_from("/create_config_response/@id", resp)
    
    get_all(:id => new_id, :show_details => true)[0]
  rescue VasExceptions::CommandException => e
    nil
  end
end

.get_all(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/openvas-cli/vas_config.rb', line 41

def self.get_all(options = {})
  params = {:sort_field  => "name"}
  if options[:show_details] && options[:show_details] == true
    params.merge!({:families => "1", :preferences => "1"})
  end    
  params[:config_id] = options[:id] if options[:id]
  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_configs(params)
  }
  ret = []
  
  begin
    resp = connection.send_receive(req.doc)
    

    resp.xpath("/get_configs_response/config").each { |xml|
      cfg               = VasConfig.new
      cfg.id            = extract_value_from("@id", xml)
      cfg.name          = extract_value_from("name", xml)
      cfg.comment       = extract_value_from("comment", xml)
      cfg.families_grow = extract_value_from("family_count/growing", xml).to_i > 0
      cfg.rules_grow    = extract_value_from("nvt_count/growing", xml).to_i > 0
      cfg.in_use        = extract_value_from("in_use", xml).to_i > 0
      
      xml.xpath("tasks/task").each { |t| cfg.tasks << VasTask.from_xml_node(t) }
      xml.xpath("families/family").each { |f| cfg.families << VasNVTFamily.from_xml_node(f) }
      xml.xpath("preferences/preference").each { |p| 
        p = VasPreference.from_xml_node(p) 
        p.config_id = cfg.id
        p.reset_changes
        cfg.preferences << p
      }
      
      cfg.reset_changes
      
      ret << cfg
    }
  rescue Exception => e
  end

  ret
end

Instance Method Details

#create_or_updateObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openvas-cli/vas_config.rb', line 105

def create_or_update
  @previously_changed = changes
  begin
    preferences.each{ |p| p.save! if p.changed? }
    @changed_attributes.clear
    
    self
  rescue Exception => e
    errors[:command] << e.message
    nil
  end
end

#delete_recordObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/openvas-cli/vas_config.rb', line 118

def delete_record
  req = Nokogiri::XML::Builder.new { |xml|
    xml.delete_config(:config_id => @id)
  }
  
  begin
    VasConfig.connection.send_receive(req.doc)
    self
  rescue Exception => e
    errors[:command] << e.message
    nil
  end
end

#familiesObject



37
38
39
# File 'lib/openvas-cli/vas_config.rb', line 37

def families
  @families ||= []
end

#preferencesObject



33
34
35
# File 'lib/openvas-cli/vas_config.rb', line 33

def preferences
  @preferences ||= []
end

#tasksObject



29
30
31
# File 'lib/openvas-cli/vas_config.rb', line 29

def tasks
  @tasks ||= []
end