New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate temporary security groups in AWS #44
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41941a1
Create a temp security group, if no sg id is provided
orien 72d81cd
Wait until EC2 instance has terminated
orien 707c333
Apply suggestions from code review
orien 527af58
Make subnet_id a required argument
orien 99defc0
Add --allow_any_temporary_security_group option
orien File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'aws-sdk-ec2' | ||
require 'forwardable' | ||
require 'securerandom' | ||
|
||
module AmiSpec | ||
class AwsSecurityGroup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new class facilitates the creating and deleting of AWS security groups. |
||
extend Forwardable | ||
|
||
def self.create(**args) | ||
new(**args).tap(&:create) | ||
end | ||
|
||
def initialize(ec2: Aws::EC2::Resource.new, | ||
group_name_prefix: "ami-spec-", | ||
connection_port: 22, | ||
subnet_id:, | ||
allow_any_ip: false, | ||
logger: Logger.new(STDOUT)) | ||
@ec2 = ec2 | ||
@group_name = "#{group_name_prefix}#{SecureRandom.uuid}" | ||
@connection_port = connection_port | ||
@subnet_id = subnet_id | ||
@allow_any_ip = allow_any_ip | ||
@logger = logger | ||
end | ||
|
||
def_delegators :@security_group, :group_id | ||
attr_reader :group_name | ||
|
||
def create | ||
@logger.info "Creating temporary AWS security group: #{@group_name}" | ||
create_security_group | ||
allow_ingress_via_connection_port | ||
end | ||
|
||
def delete | ||
@logger.info "Deleting temporary AWS security group: #{@group_name}" | ||
@security_group.delete | ||
end | ||
|
||
private | ||
|
||
def create_security_group | ||
@security_group = @ec2.create_security_group( | ||
group_name: @group_name, | ||
description: "A temporary security group for running AmiSpec", | ||
vpc_id: subnet.vpc_id, | ||
) | ||
end | ||
|
||
def allow_ingress_via_connection_port | ||
@security_group.authorize_ingress( | ||
ip_permissions: [ | ||
{ | ||
ip_protocol: "tcp", | ||
from_port: @connection_port, | ||
to_port: @connection_port, | ||
ip_ranges: [{cidr_ip: cidr_block}], | ||
}, | ||
], | ||
) | ||
end | ||
|
||
def cidr_block | ||
@allow_any_ip ? "0.0.0.0/0" : subnet.cidr_block | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A much less surprising default 👍 |
||
end | ||
|
||
def subnet | ||
@subnet ||= @ec2.subnet(@subnet_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require 'spec_helper' | ||
|
||
describe AmiSpec::AwsSecurityGroup do | ||
subject(:aws_security_group) do | ||
described_class.create( | ||
ec2: ec2, | ||
subnet_id: test_subnet_id, | ||
allow_any_ip: allow_any_ip, | ||
logger: logger | ||
) | ||
end | ||
|
||
let(:ec2) { instance_spy(Aws::EC2::Resource, create_security_group: security_group, subnet: subnet) } | ||
let(:security_group) { instance_spy(Aws::EC2::SecurityGroup, group_id: test_group_id) } | ||
let(:subnet) { instance_spy(Aws::EC2::Subnet, vpc_id: test_vpc_id, cidr_block: test_cidr_block) } | ||
let(:test_subnet_id) { 'test-subnet-id' } | ||
let(:test_group_id) { 'test-group-id' } | ||
let(:test_vpc_id) { 'test-vpc-id' } | ||
let(:test_cidr_block) { '172.16.0.0/24' } | ||
let(:allow_any_ip) { false } | ||
let(:logger) { instance_spy(Logger) } | ||
|
||
describe '#create' do | ||
subject(:create) { aws_security_group } | ||
|
||
it 'creates the security group in AWS' do | ||
create | ||
expect(ec2).to have_received(:create_security_group).with(group_name: aws_security_group.group_name, vpc_id: test_vpc_id, description: anything) | ||
end | ||
|
||
context 'given allow_any_ip: true' do | ||
let(:allow_any_ip) { true } | ||
|
||
it 'adds the ingress rule for SSH, allowing any IP address' do | ||
create | ||
expect(security_group).to have_received(:authorize_ingress).with( | ||
ip_permissions: [{ip_protocol: "tcp", from_port: 22, to_port: 22, ip_ranges: [{cidr_ip: "0.0.0.0/0"}]}] | ||
) | ||
end | ||
end | ||
|
||
context 'given allow_any_ip: false' do | ||
let(:allow_any_ip) { false } | ||
|
||
it 'adds the ingress rule for SSH, allowing only IP addresses from the subnet CIDR block' do | ||
create | ||
expect(security_group).to have_received(:authorize_ingress).with( | ||
ip_permissions: [{ip_protocol: "tcp", from_port: 22, to_port: 22, ip_ranges: [{cidr_ip: test_cidr_block}]}] | ||
) | ||
end | ||
end | ||
|
||
it 'loads the subnet to find the vpc id' do | ||
create | ||
expect(ec2).to have_received(:subnet).with(test_subnet_id) | ||
end | ||
|
||
it 'logs the creation of the key pair in AWS' do | ||
create | ||
expect(logger).to have_received(:info).with "Creating temporary AWS security group: #{aws_security_group.group_name}" | ||
end | ||
end | ||
|
||
describe '#group_name' do | ||
subject(:key_name) { aws_security_group.group_name } | ||
|
||
it { should start_with('ami-spec-') } | ||
end | ||
|
||
describe '#group_id' do | ||
subject(:group_id) { aws_security_group.group_id } | ||
|
||
it 'is obtained from the AWS create-security-group API response' do | ||
expect(group_id).to eq test_group_id | ||
end | ||
end | ||
|
||
describe '#delete' do | ||
subject(:delete) { aws_security_group.delete } | ||
|
||
it 'deletes the security group in AWS' do | ||
delete | ||
expect(security_group).to have_received(:delete) | ||
end | ||
|
||
it 'logs the deletion of the key pair in AWS' do | ||
delete | ||
expect(logger).to have_received(:info).with "Deleting temporary AWS security group: #{aws_security_group.group_name}" | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To successfully delete a security group, any EC2 instances using it must be terminated. Hence we'll need to start waiting for this EC2 instance fully terminate before attempting to delete the security group.