Module: Msf::WebServices::HostServlet

Defined in:
lib/msf/core/web_services/servlet/host_servlet.rb

Class Method Summary collapse

Class Method Details

.add_host_tag ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 107

def self.add_host_tag
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)

      tmp_params = sanitize_params(params)
      opts[:id] = tmp_params[:id] if tmp_params[:id]
      data = get_db.add_host_tag(opts)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error adding the host tag:', code: 500)
    end
  }
end

.api_path ⇒ Object



3
4
5
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 3

def self.api_path
  '/api/v1/hosts'
end

.api_path_with_id ⇒ Object



7
8
9
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 7

def self.api_path_with_id
  "#{self.api_path}/?:id?"
end

.api_path_with_id_and_tags ⇒ Object



11
12
13
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 11

def self.api_path_with_id_and_tags
  "#{self.api_path_with_id}/tags"
end

.api_search_path ⇒ Object



15
16
17
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 15

def self.api_search_path
  "#{self.api_path}/search"
end

.delete_host ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 78

def self.delete_host
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)
      data = get_db.delete_host(opts)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error deleting hosts:', code: 500)
    end
  }
end

.delete_host_tag ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 123

def self.delete_host_tag
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)
      tmp_params = sanitize_params(params)
      opts[:id] = tmp_params[:id] if tmp_params[:id]
      data = get_db.delete_host_tag(opts)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error deleting the host tag:', code: 500)
    end
  }
end

.get_host ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 35

def self.get_host
  lambda {
    warden.authenticate!
    begin
      sanitized_params = sanitize_params(params, env['rack.request.query_hash'])
      data = get_db.hosts(sanitized_params)
      data = data.first if is_single_object?(data, sanitized_params)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error retrieving hosts:', code: 500)
    end
  }
end

.get_host_tags ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 91

def self.get_host_tags
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)

      tmp_params = sanitize_params(params)
      opts[:id] = tmp_params[:id] if tmp_params[:id]
      data = get_db.get_host_tags(opts)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error retrieving the host tag(s):', code: 500)
    end
  }
end

.registered(app) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 19

def self.registered(app)
  app.get self.api_path, &get_host
  app.get self.api_path_with_id, &get_host
  app.post self.api_path, &report_host
  app.put self.api_path_with_id, &update_host
  app.delete self.api_path, &delete_host
  app.post self.api_search_path, &search
  app.get self.api_path_with_id_and_tags, &get_host_tags
  app.post self.api_path_with_id_and_tags, &add_host_tag
  app.delete self.api_path_with_id_and_tags, &delete_host_tag
end

.report_host ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 49

def self.report_host
  lambda {
    warden.authenticate!
    begin
      job = lambda { |opts|
        get_db.report_host(opts)
      }
      exec_report_job(request, &job)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error creating the host:', code: 500)
    end
  }
end

.search ⇒ Object

TODO: remove once hosts and get_host method is merged



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 139

def self.search
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)
      data = get_db.get_host(opts)
      set_json_data_response(response: data)
    rescue Exception => e
      print_error_and_create_response(error: e, message: 'There was an error searching for hosts:', code: 500)
    end
  }
end

.update_host ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/msf/core/web_services/servlet/host_servlet.rb', line 63

def self.update_host
  lambda {
    warden.authenticate!
    begin
      opts = parse_json_request(request, false)
      tmp_params = sanitize_params(params)
      opts[:id] = tmp_params[:id] if tmp_params[:id]
      data = get_db.update_host(opts)
      set_json_data_response(response: data)
    rescue => e
      print_error_and_create_response(error: e, message: 'There was an error updating the host:', code: 500)
    end
  }
end