Skip to content
Browse files

Adding specs. restructuring code

  • Loading branch information...
1 parent 585e82f commit d35ab86813806e925907376c926d03555e0e610c @anaynayak committed Feb 3, 2012
Showing with 117 additions and 42 deletions.
  1. +6 −0 Gemfile
  2. +25 −0 Gemfile.lock
  3. +19 −0 Guardfile
  4. +4 −0 config/boot.rb
  5. +52 −0 lib/visualize_aws.rb
  6. +4 −0 spec/spec_helper.rb
  7. +7 −0 spec/visualize_aws_spec.rb
  8. +0 −42 visualize_aws.rb
View
6 Gemfile
@@ -2,3 +2,9 @@ source 'http://rubygems.org'
gem 'ruby-graphviz'
gem 'right_aws'
+
+group :development do
+ gem 'rspec'
+ gem 'guard-rspec'
+ gem 'libnotify'
+end
View
25 Gemfile.lock
@@ -1,10 +1,35 @@
GEM
remote: http://rubygems.org/
specs:
+ diff-lcs (1.1.3)
+ ffi (1.0.11)
+ guard (1.0.0)
+ ffi (>= 0.5.0)
+ thor (~> 0.14.6)
+ guard-rspec (0.6.0)
+ guard (>= 0.10.0)
+ libnotify (0.7.2)
+ ffi (~> 1.0.0)
+ right_aws (3.0.0)
+ right_http_connection (>= 1.2.5)
+ right_http_connection (1.3.0)
+ rspec (2.8.0)
+ rspec-core (~> 2.8.0)
+ rspec-expectations (~> 2.8.0)
+ rspec-mocks (~> 2.8.0)
+ rspec-core (2.8.0)
+ rspec-expectations (2.8.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.8.0)
ruby-graphviz (1.0.0)
+ thor (0.14.6)
PLATFORMS
ruby
DEPENDENCIES
+ guard-rspec
+ libnotify
+ right_aws
+ rspec
ruby-graphviz
View
19 Guardfile
@@ -0,0 +1,19 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+
+ # Rails example
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
+ watch('config/routes.rb') { "spec/routing" }
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
+ # Capybara request specs
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
+end
+
View
4 config/boot.rb
@@ -0,0 +1,4 @@
+require 'bundler'
+Bundler.setup
+
+Dir["./lib/**/*.rb"].each { |f| require f }
View
52 lib/visualize_aws.rb
@@ -0,0 +1,52 @@
+require 'right_aws'
+require 'graphviz'
+require './groups'
+require 'set'
+
+class VisualizeAws
+ def initialize(access_key, secret_key)
+ @ec2 = RightAws::Ec2.new(access_key, secret_key)
+ end
+ def unleash
+ groups = @ec2.describe_security_groups
+ p groups
+ nodes = groups.collect {|group| group[:aws_group_name]}
+
+ g = GraphViz::new( "G" )
+ nodes.each {|n| g.add_node(n)}
+ identify_group_ingress(groups) {|from, to, port_range| g.add_edge( from, to, :color => "blue", :style => "bold", :label => port_range )}
+ identify_cidr_ingress(groups) {|from, to, port_range| g.add_edge( from, to, :color => "green", :style => "bold", :label => port_range )}
+ g.output( :png => "aws-security-viz.png" )
+ end
+
+ def identify_group_ingress(groups)
+ groups.each do |group|
+ group[:aws_perms].each do |perm|
+ yield perm[:group], group[:aws_group_name], [perm[:from_port], perm[:to_port]].uniq.join("-") if perm[:group]
+ end
+ end
+ end
+
+ def mapping(val)
+ USER_GROUPS[val]? USER_GROUPS[val] : val
+ end
+
+ def identify_cidr_ingress(groups)
+ seen = Set.new
+ groups.each do |group|
+ group[:aws_perms].each do |perm|
+ next if not perm[:cidr_ips]
+ args = [mapping(perm[:cidr_ips]), group[:aws_group_name], [perm[:from_port], perm[:to_port]].uniq.join("-")]
+ yield args if not seen.include?(args)
+ seen.add(args)
+ end
+ end
+ end
+
+end
+
+if __FILE__ == $0
+ access_key = ARGV[0]
+ secret_key = ARGV[1]
+ VisualizeAws.new(access_key, secret_key).unleash
+end
View
4 spec/spec_helper.rb
@@ -0,0 +1,4 @@
+require 'bundler'
+Bundler.require
+
+require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
View
7 spec/visualize_aws_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe VisualizeAws do
+ it "should identify mapping" do
+
+ end
+end
View
42 visualize_aws.rb
@@ -1,42 +0,0 @@
-require 'right_aws'
-require 'graphviz'
-require './groups'
-require 'set'
-
-access_key = ARGV[0]
-secret_key = ARGV[1]
-
-ec2 = RightAws::Ec2.new(access_key, secret_key)
-groups = ec2.describe_security_groups
-
-def identify_group_ingress(groups)
- groups.each do |group|
- group[:aws_perms].each do |perm|
- yield perm[:group], group[:aws_group_name], [perm[:from_port], perm[:to_port]].uniq.join("-") if perm[:group]
- end
- end
-end
-
-def mapping(val)
- USER_GROUPS[val]? USER_GROUPS[val] : val
-end
-
-def identify_cidr_ingress(groups)
- seen = Set.new
- groups.each do |group|
- group[:aws_perms].each do |perm|
- next if not perm[:cidr_ips]
- args = [mapping(perm[:cidr_ips]), group[:aws_group_name], [perm[:from_port], perm[:to_port]].uniq.join("-")]
- yield args if not seen.include?(args)
- seen.add(args)
- end
- end
-end
-
-nodes = groups.collect {|group| group[:aws_group_name]}
-
-g = GraphViz::new( "G" )
-nodes.each {|n| g.add_node(n)}
-identify_group_ingress(groups) {|from, to, port_range| g.add_edge( from, to, :color => "blue", :style => "bold", :label => port_range )}
-identify_cidr_ingress(groups) {|from, to, port_range| g.add_edge( from, to, :color => "green", :style => "bold", :label => port_range )}
-g.output( :png => "aws-security-viz.png" )

0 comments on commit d35ab86

Please sign in to comment.
Something went wrong with that request. Please try again.