Sha256: bfcd8fa53757ce862501a50039397dc74dadc01646eeea15220c6a1eb607cdd4

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'aws-sdk'
require 'sgupdater/updater'

module Sgupdater
  class Client
    attr_reader :logger

    def initialize(cli_options = {}, aws_configuration = {})
      @cli_options = cli_options
      @logger ||= Logger.new STDOUT

      aws_configuration[:logger] = Logger.new STDOUT if @cli_options[:verbose]

      @ec2 = Aws::EC2::Resource.new aws_configuration
    end

    def get(cidr = @cli_options[:from_cidr])
      security_groups_with_cidr(cidr)
    end

    def update
      updater = Sgupdater::Updater.new(@cli_options)
      updater.replace(@cli_options[:from_cidr], @cli_options[:to_cidr])
      updater.update
    end

    def add
      updater = Sgupdater::Updater.new(@cli_options)
      updater.add(@cli_options[:from_cidr], @cli_options[:to_cidr])
      updater.update
    end

    def security_groups_with_cidr(cidr)
      @ec2.security_groups(
        filters: [
          {name: 'ip-permission.cidr', values: [cidr]}
        ]
      )
    end

    def put_perms(sg, cidr)
      sg.ip_permissions.each do |perm|
        perm.ip_ranges.select {|ip| ip.values.include? cidr}.each do
          puts [sg.vpc_id, sg.group_id, sg.group_name, perm.from_port, perm.to_port, cidr].join("\t")
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sgupdater-0.2.0 lib/sgupdater/client.rb