Class: ActiveStix::Relationship

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

Constant Summary collapse

@@stix_map =
{
    'bundle' => ActiveStix::Bundle,
    'attack-pattern' => ActiveStix::AttackPattern,
    'relationship' => ActiveStix::Relationship,
    'course-of-action' => ActiveStix::CourseOfAction,
    'identity' => ActiveStix::Identity,
    'intrusion-set' => ActiveStix::IntrusionSet,
    'malware' => ActiveStix::Malware,
    'tool' => ActiveStix::Tool,
    'marking-definition' => ActiveStix::MarkingDefinition,
    'report' => ActiveStix::Report,
    'campaign' => ActiveStix::Campaign,
    'indicator' => ActiveStix::Indicator
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ingest_json(obj) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/active_stix/relationship.rb', line 30

def self.ingest_json(obj)
  #puts "Loc B: #{obj['id'].to_s}"
  #puts "source: #{obj['source_ref']}"
  #puts "target: #{obj['target_ref']}"
  #pause = gets

  src_type = obj['source_ref'].split('--')[0]
  tgt_type = obj['target_ref'].split('--')[0]

  src = @@stix_map[src_type].where("stix_id = ?", obj['source_ref']).first
  tgt = @@stix_map[tgt_type].where("stix_id = ?", obj['target_ref']).first


  relationship = find_or_create_by!(stix_id: obj['id'], source: src, target: tgt, relationship_type: obj['relationship_type'], source_type: @@stix_map[src_type].to_s, target_type: @@stix_map[tgt_type].to_s)

  if obj.has_key?('description')
    relationship.description = obj['description']
  end

  if obj.has_key?('object_marking_refs')
    obj['object_marking_refs'].each do |mr|
      # marking_definition = ActiveStix::MarkingDefinition.create_by_id(mr)
      # relationship.marking_definitions << marking_definition unless ActiveStix::ReferenceObjectMarkingRelationship.find_by(stix_marking_definition_id: marking_definition.id, stix_relationship_id: relationship.id)
      # todo
    end
  end

  relationship.save
  relationship
end

.relate(source, target, relationship_type) ⇒ Object



61
62
63
# File 'app/models/active_stix/relationship.rb', line 61

def self.relate(source, target, relationship_type)
  find_or_create_by!(source: source, target: target, relationship_type: relationship_type)
end

Instance Method Details

#as_stixObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/active_stix/relationship.rb', line 82

def as_stix

  as_json(only: [:relationship_type]).tap do |hash|
    hash["id"] = stix_id
    hash["description"] = description if description
    hash["type"] = type
    hash["created"] = created_at.utc.iso8601(3)
    hash["modified"] = created_at.utc.iso8601(3)
    hash["source_ref"] = source.stix_id
    hash["target_ref"] = target.stix_id
    hash["spec_version"] = "2.0"
  end
end

#convert_to_jsonObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/active_stix/relationship.rb', line 69

def convert_to_json
  {
      :type => "relationship",
      :id => stix_id,
      :created => created_at.to_s,
      :modified => updated_at.to_s,
      :relationship_type => relationship_type,
      :source_ref => source_ref.to_s,
      :target_ref => target_ref.to_s,
      :description => description
  }
end

#typeObject



65
66
67
# File 'app/models/active_stix/relationship.rb', line 65

def type
  'relationship'
end