Module: Taxii::Client

Included in:
PollClient
Defined in:
lib/taxii/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/taxii/client.rb', line 3

def self.included(klass)
  klass.class_eval do
    attr_accessor :url, :user, :pass, :client_cert, :client_key, :ca_cert
    attr_reader   :xml
    def initialize(*args)
      Hash[*args].each {|k,v| self.send(format('%s=',k),v)}
      @user ||= 'admin'
      @pass ||= 'avalanche'
      @xml = Nori.new(strip_namespaces: true)
      self
    end
  end
end

Instance Method Details

#build_request(url: self.url, payload: {}, format: Taxii::Messages::TAXII_11_HEADERS) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/taxii/client.rb', line 17

def build_request(url: self.url,payload: {}, format: Taxii::Messages::TAXII_11_HEADERS)
  RestClient::Request.new(
    method:   :post,
    url:      url,
    user:     user,
    password: pass,
    payload:  payload,
    timeout:  nil,
    headers: {
      'X-Taxii-Services' => 'urn:taxii.mitre.org:services:1.1',
      :content_type      => 'application/xml',
      :accept            => 'application/xml'
    }.merge(format)
     .merge(scheme_protocol)
  )
end

#collection_management_service_urlObject



56
57
58
# File 'lib/taxii/client.rb', line 56

def collection_management_service_url
  @collection_management_service_url ||= get_service_address('COLLECTION_MANAGEMENT')
end

#discover_feeds(url = self.collection_management_service_url) ⇒ Object



64
65
66
67
68
69
# File 'lib/taxii/client.rb', line 64

def discover_feeds(url=self.collection_management_service_url)
  msg  = Taxii::Messages::FeedInformationRequest.new.to_xml
  http  = build_request(url: url, payload: msg, format: Taxii::Messages::TAXII_10_HEADERS)
  parsed  = xml.parse(http.execute.body)
  parsed['Feed_Information_Response'].fetch('Feed',[])
end

#discover_servicesObject



41
42
43
44
45
46
# File 'lib/taxii/client.rb', line 41

def discover_services
  payload  = Taxii::Messages::DiscoveryRequest.new.to_xml
  response = build_request(url: self.url, payload: payload).execute
  parsed   = xml.parse(response.body)
  parsed['Discovery_Response'].fetch('Service_Instance',[])
end

#discovery_service_urlObject



48
49
50
# File 'lib/taxii/client.rb', line 48

def discovery_service_url
  self.url
end

#get_service_address(service_name) ⇒ Object



34
35
36
37
38
39
# File 'lib/taxii/client.rb', line 34

def get_service_address(service_name)
  service = discover_services.find do |svc|
    svc['@service_type']==service_name && svc['@available']=='true'
  end
  service.nil? ? nil : service['Address']
end

#inbox_service_urlObject



52
53
54
# File 'lib/taxii/client.rb', line 52

def inbox_service_url
  @inbox_service_url ||= get_service_address('INBOX')
end

#poll_service_urlObject



60
61
62
# File 'lib/taxii/client.rb', line 60

def poll_service_url
  @poll_service_url ||= get_service_address('POLL')
end

#scheme_protocolObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/taxii/client.rb', line 71

def scheme_protocol
  case URI.parse(url).scheme
    when "http"
      { 'X-Taxii-Protocol' => 'urn:taxii.mitre.org:protocol:http:1.0' }
    when "https"
      { 'X-Taxii-Protocol' => 'urn:taxii.mitre.org:protocol:https:1.0' }
    else
      {}
  end
end