Class: ActiveStix::Tool

Inherits:
ApplicationRecord show all
Defined in:
app/models/active_stix/tool.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.created_by_ref(tool, obj) ⇒ Object



46
47
48
# File 'app/models/active_stix/tool.rb', line 46

def self.created_by_ref(tool, obj)
  tool.created_by_ref = obj['created_by_ref']
end

.description(tool, obj) ⇒ Object



42
43
44
# File 'app/models/active_stix/tool.rb', line 42

def self.description(tool, obj)
  tool.description = obj['description']
end

.expected_keysObject



8
9
10
11
12
13
14
15
16
# File 'app/models/active_stix/tool.rb', line 8

def self.expected_keys
  [
      'description',
      'external_references',
      'object_marking_refs',
      'created_by_ref',
      'labels'
  ]
end

.external_references(tool, obj) ⇒ Object



50
51
52
53
54
55
# File 'app/models/active_stix/tool.rb', line 50

def self.external_references(tool, obj)
  obj['external_references'].each do |er|
    external_reference = ActiveStix::ExternalReference.ingest_json(er, obj['id'])
    tool.external_references << external_reference unless ActiveStix::ReferenceItem.find_by(external_reference: external_reference, referrer: tool)
  end
end

.ingest_json(obj) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/active_stix/tool.rb', line 22

def self.ingest_json(obj)
  tool = find_or_create_by(stix_id: obj['id'], name: obj['name'])

  expected_keys.each do |expected_key|
    if obj.has_key?(expected_key)
      send(expected_key, *[tool, obj])
    end
  end

  tool.save
  tool
end

.labels(tool, obj) ⇒ Object



58
59
60
61
62
63
# File 'app/models/active_stix/tool.rb', line 58

def self.labels(tool, obj)
  obj['labels'].each do |lab|
    label = ActiveStix::Label.ingest_label('tool', lab)
    tool.labels << label unless ActiveStix::Markup.find_by(labelable: tool, label: label)
  end
end

.object_marking_refs(tool, obj) ⇒ Object



35
36
37
38
39
40
# File 'app/models/active_stix/tool.rb', line 35

def self.object_marking_refs(tool, obj)
  # obj['object_marking_refs'].each do |mr|
  #   marking_definition = ActiveStix::MarkingDefinition.create_by_id(mr)
  #   tool.marking_definitions << marking_definition unless ActiveStix::ReferenceObjectMarkingTool.find_by(stix_marking_definition_id:marking_definition.id, stix_tool_id:tool.id)
  # end todo
end

Instance Method Details

#as_stix(classification = nil, chess = nil) ⇒ Object



66
67
68
# File 'app/models/active_stix/tool.rb', line 66

def as_stix(classification = nil, chess = nil)
  as_json
end

#convert_to_jsonObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/active_stix/tool.rb', line 71

def convert_to_json
  external_refs_arr = []
  external_references.each do |x|
    external_refs_arr << x.convert_to_json
  end

  # marking_def_arr = []
  # marking_definitions.each do | x |
  #   marking_def_arr << x.convert_to_json
  # end todo

  platform_arr = []
  platforms.each do |x|
    platform_arr << x.convert_to_json
  end

  labels_arr = []
  labels.each do |x|
    labels_arr << x.open_vocabulary.convert_to_json
  end


  alias_arr = []
  attack_aliases.each do |x|
    alias_arr << x.convert_to_json
  end

  {
      :external_references => external_refs_arr,
      # :object_marking_refs => marking_def_arr,
      :modified => updated_at.to_s,
      :created_by_ref => created_by_ref,
      :id => stix_id,
      :name => name,
      :created => created_at.to_s,
      :labels => labels_arr,
      :type => "tool",
      :description => description
  }
end

#typeObject



18
19
20
# File 'app/models/active_stix/tool.rb', line 18

def type
  'tool'
end