Parcourir la source

get peering information

Chris Mague il y a 6 ans
Parent
commit
486a190c64
5 fichiers modifiés avec 82 ajouts et 1 suppressions
  1. 6 0
      QUERIES.md
  2. 1 0
      Rakefile
  3. 45 0
      lib/neoinfra/vpcs.rb
  4. 22 0
      models/peers.rb
  5. 8 1
      tasks/load_data.rake

+ 6 - 0
QUERIES.md

@@ -31,3 +31,9 @@ MATCH (i:IpRules)-[r:ip_rules]-(s:SecurityGroup)-[p:node_sg]-(n:Node) WHERE i.fr
 ```
 ```
 MATCH (n:Node{ip: "172.20.1.107"})-[x:node_sg]-(s:SecurityGroup)-[y:ip_rules]-(i:IpRules) return i.proto, i.from_port, i.to_port, i.cidr_block
 MATCH (n:Node{ip: "172.20.1.107"})-[x:node_sg]-(s:SecurityGroup)-[y:ip_rules]-(i:IpRules) return i.proto, i.from_port, i.to_port, i.cidr_block
 ```
 ```
+
+## Find all VPC peers
+```
+MATCH (f:Vpc{name: "TSprod-AUX"})-[x:peered]-(p:Peer)-[y:peered]-(t:Vpc) RETURN f.name, t.name
+```
+

+ 1 - 0
Rakefile

@@ -20,6 +20,7 @@ Dir.glob('tasks/*.rake').each { |r| import r }
 
 
 RuboCop::RakeTask.new(:rubocop) do |t|
 RuboCop::RakeTask.new(:rubocop) do |t|
   t.options = ['--display-cop-names']
   t.options = ['--display-cop-names']
+  t.warning = false
 end
 end
 
 
 RSpec::Core::RakeTask.new(:spec) do |t|
 RSpec::Core::RakeTask.new(:spec) do |t|

+ 45 - 0
lib/neoinfra/vpcs.rb

@@ -2,6 +2,7 @@
 
 
 require 'neoinfra'
 require 'neoinfra'
 require 'vpc'
 require 'vpc'
+require 'peers'
 require 'accounts'
 require 'accounts'
 require 'fog-aws'
 require 'fog-aws'
 require 'neo4j'
 require 'neo4j'
@@ -35,6 +36,50 @@ module NeoInfra
       end
       end
       Vpc.all.collect { |x| { 'nodes' => node_counts[x.name], 'vpc_id' => x.vpc_id, 'name' => x.name, 'region' => x.region.region, 'owner' => x.owned.name, 'cidr' => x.cidr, 'default' => x.default } }.select { |y| y['default'] == 'false' }.sort_by { |h| h['nodes'] }.reverse
       Vpc.all.collect { |x| { 'nodes' => node_counts[x.name], 'vpc_id' => x.vpc_id, 'name' => x.name, 'region' => x.region.region, 'owner' => x.owned.name, 'cidr' => x.cidr, 'default' => x.default } }.select { |y| y['default'] == 'false' }.sort_by { |h| h['nodes'] }.reverse
     end
     end
+ 
+    def load_peers
+      aws = NeoInfra::Aws.new
+      @cfg.accounts.each do |account|
+        base_conf = {
+          provider: 'AWS',
+          aws_access_key_id: account[:key],
+          aws_secret_access_key: account[:secret]
+        }
+        aws.regions.each do |region|
+          region_conf = { region: region }
+          begin
+            new_conn = Fog::Compute.new(region_conf.merge(base_conf))
+          rescue StandardError
+            puts "Error loading Peering in region: #{region}"
+            next
+          end
+          new_conn.route_tables.each do |rt|
+            rt.routes.select{ |x| not x["vpcPeeringConnectionId"].nil? }.each do |r|
+              if Peer.where(peer_id: r["vpcPeeringConnectionId"]).empty?
+                mypeer = Peer.new(
+                  peer_id: r['vpcPeeringConnectionId']
+                )
+                mypeer.save
+              else
+                mypeer = Peer.where(peer_id: r["vpcPeeringConnectionId"]).first
+              end
+              puts r["vpcPeeringConnectionId"]
+              ### TODO: make this more efficient
+              match_count = 0
+              PeerVpc.all.each do |x|
+                if x.from_node.peer_id == r["vpcPeeringConnectionId"] and x.to_node.vpc_id == rt.vpc_id
+                  puts "matched #{x.from_node.peer_id} and #{x.to_node.vpc_id}"
+                  match_count += 1
+                end
+              end
+              if match_count < 1
+                PeerVpc.create(from_node: mypeer, to_node: Vpc.where(vpc_id: rt.vpc_id).first)
+              end
+            end
+          end
+        end
+      end
+    end
 
 
     def load
     def load
       aws = NeoInfra::Aws.new
       aws = NeoInfra::Aws.new

+ 22 - 0
models/peers.rb

@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Provide Neo4J Model for VPCs
+class Peer
+  include Neo4j::ActiveNode
+  property :peer_id, constraint: :unique
+  has_many :out, :vpc, rel_class: :PeerVpc
+end
+
+###############################################################################
+# Relationships go below here
+###############################################################################
+# Provide Neo4J Model for VPC Owners
+class PeerVpc
+  include Neo4j::ActiveRel
+  from_class :Peer
+  to_class :Vpc
+  type :peered
+end
+

+ 8 - 1
tasks/load_data.rake

@@ -15,6 +15,13 @@ namespace :load_data do
     j.load
     j.load
   end
   end
 
 
+  desc 'Load VPC Peers into the neo4j container'
+  task :peer do
+    puts 'loading vpc peers'
+    j = NeoInfra::Vpcs.new
+    j.load_peers
+  end
+
   desc 'Load Region and Availability Zone information'
   desc 'Load Region and Availability Zone information'
   task :regions do
   task :regions do
     puts 'loading regions'
     puts 'loading regions'
@@ -73,5 +80,5 @@ namespace :load_data do
 
 
 
 
   desc 'Load Everything'
   desc 'Load Everything'
-  task all: %i[accounts regions vpcs buckets security_groups nodes rds dynamo lambda sqs]
+  task all: %i[accounts regions vpcs peer buckets security_groups nodes rds dynamo lambda sqs]
 end
 end