Sha256: 8fbb4f52193e14a076c7f6bab39adb22baffaa7e8b7466776ff38a014dcc20e7

Contents?: true

Size: 1.75 KB

Versions: 143

Compression:

Stored size: 1.75 KB

Contents

require 'aws-sdk-ec2'

class Ec2Provider

  def initialize(options)
    @options = options
    conn_opts = {
      region: options[:region],
      access_key_id: options[:access_key],
      secret_access_key: options[:secret_key],
      session_token: options[:session_token]
    }.delete_if {|k,v| v.nil?}

    @client = Aws::EC2::Client.new(conn_opts)
  end

  def security_groups
    @client.describe_security_groups.security_groups.reject { |sg|
      @options[:vpc_id] && sg.vpc_id != @options[:vpc_id]
    }.collect { |sg|
      Ec2::SecurityGroup.new(sg)
    }
  end
end

module Ec2
  class SecurityGroup
    extend Forwardable
    def_delegators :@sg, :name, :group_id, :vpc_id
    def initialize(sg)
      @sg = sg
    end

    def name
      @sg.group_name
    end

    def ip_permissions
      @sg.ip_permissions.collect { |ip|
        Ec2::IpPermission.new(ip)
      }
    end

    def ip_permissions_egress
      @sg.ip_permissions_egress.collect { |ip|
        Ec2::IpPermission.new(ip)
      }
    end
  end

  class IpPermission
    def initialize(ip)
      @ip = ip
    end

    def protocol
      @ip['ip_protocol']
    end

    def from
      @ip['from_port']
    end

    def to
      @ip['to_port']
    end

    def ip_ranges
      @ip['ip_ranges'].collect {|gp|
        Ec2::IpPermissionRange.new(gp)
      }
    end

    def groups
      @ip['user_id_group_pairs'].collect {|gp|
        Ec2::IpPermissionGroup.new(gp)
      }
    end
  end

  class IpPermissionRange
    def initialize(range)
      @range = range
    end

    def cidr_ip
      @range['cidr_ip']
    end

    def to_str
      cidr_ip
    end
  end

  class IpPermissionGroup
    def initialize(gp)
      @gp = gp
    end

    def name
      @gp['group_name'] || @gp['group_id']
    end
  end

end

Version data entries

143 entries across 143 versions & 1 rubygems

Version Path
aws_security_viz-0.2.5.pre.alpha.pre.35 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.34 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.33 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.32 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.31 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.30 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.29 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.28 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.27 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.26 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.25 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.24 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.23 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.22 lib/provider/ec2.rb
aws_security_viz-0.2.5.pre.alpha.pre.21 lib/provider/ec2.rb
aws_security_viz-0.2.4 lib/provider/ec2.rb
aws_security_viz-0.2.4.pre.alpha.pre.20 lib/provider/ec2.rb
aws_security_viz-0.2.4.pre.alpha.pre.19 lib/provider/ec2.rb
aws_security_viz-0.2.4.pre.alpha.pre.18 lib/provider/ec2.rb
aws_security_viz-0.2.4.pre.alpha.pre.15 lib/provider/ec2.rb