3636# 'This is my security group.',
3737# 'vpc-6713dfEX'
3838# )
39- def create_security_group (
40- ec2_client ,
41- group_name ,
42- description ,
43- vpc_id
44- )
39+ def create_security_group ( ec2_client , group_name , description , vpc_id )
4540 security_group = ec2_client . create_security_group (
4641 group_name : group_name ,
4742 description : description ,
@@ -79,12 +74,7 @@ def create_security_group(
7974# '0.0.0.0/0'
8075# )
8176def security_group_ingress_authorized? (
82- ec2_client ,
83- security_group_id ,
84- ip_protocol ,
85- from_port ,
86- to_port ,
87- cidr_ip_range
77+ ec2_client , security_group_id , ip_protocol , from_port , to_port , cidr_ip_range
8878)
8979 ec2_client . authorize_security_group_ingress (
9080 group_id : security_group_id ,
@@ -110,88 +100,35 @@ def security_group_ingress_authorized?(
110100 false
111101end
112102
103+ # Refactored method to simplify complexity for describing security group permissions
104+ def format_port_information ( perm )
105+ from_port_str = perm . from_port == '-1' || perm . from_port == -1 ? 'All' : perm . from_port . to_s
106+ to_port_str = perm . to_port == '-1' || perm . to_port == -1 ? 'All' : perm . to_port . to_s
107+ { from_port : from_port_str , to_port : to_port_str }
108+ end
109+
113110# Displays information about a security group's IP permissions set in
114111# Amazon Elastic Compute Cloud (Amazon EC2).
115- #
116- # Prerequisites:
117- #
118- # - A security group with inbound rules, outbound rules, or both.
119- #
120- # @param p [Aws::EC2::Types::IpPermission] The IP permissions set.
121- # @example
122- # ec2_client = Aws::EC2::Client.new(region: 'us-west-2')
123- # response = ec2_client.describe_security_groups
124- # unless sg.ip_permissions.empty?
125- # describe_security_group_permissions(
126- # response.security_groups[0].ip_permissions[0]
127- # )
128- # end
129112def describe_security_group_permissions ( perm )
130- print " Protocol: #{ perm . ip_protocol == '-1' ? 'All' : perm . ip_protocol } "
113+ ports = format_port_information ( perm )
131114
132- unless perm . from_port . nil?
133- if perm . from_port == '-1' || perm . from_port == -1
134- print ', From: All'
135- else
136- print ", From: #{ perm . from_port } "
137- end
138- end
139-
140- unless perm . to_port . nil?
141- if perm . to_port == '-1' || perm . to_port == -1
142- print ', To: All'
143- else
144- print ", To: #{ perm . to_port } "
145- end
146- end
115+ print " Protocol: #{ perm . ip_protocol == '-1' ? 'All' : perm . ip_protocol } "
116+ print ", From: #{ ports [ :from_port ] } , To: #{ ports [ :to_port ] } "
147117
148- if perm . key? ( :ipv_6_ranges ) && perm . ipv_6_ranges . count . positive?
149- print ", CIDR IPv6: #{ perm . ipv_6_ranges [ 0 ] . cidr_ipv_6 } "
150- end
118+ print ", CIDR IPv6: #{ perm . ipv_6_ranges [ 0 ] . cidr_ipv_6 } " if perm . key? ( :ipv_6_ranges ) && perm . ipv_6_ranges . count . positive?
151119
152120 print ", CIDR IPv4: #{ perm . ip_ranges [ 0 ] . cidr_ip } " if perm . key? ( :ip_ranges ) && perm . ip_ranges . count . positive?
153-
154121 print "\n "
155122end
156123
157124# Displays information about available security groups in
158125# Amazon Elastic Compute Cloud (Amazon EC2).
159- #
160- # @param ec2_client [Aws::EC2::Client] An initialized Amazon EC2 client.
161- # @example
162- # describe_security_groups(Aws::EC2::Client.new(region: 'us-west-2'))
163126def describe_security_groups ( ec2_client )
164127 response = ec2_client . describe_security_groups
165128
166129 if response . security_groups . count . positive?
167130 response . security_groups . each do |sg |
168- puts '-' * ( sg . group_name . length + 13 )
169- puts "Name: #{ sg . group_name } "
170- puts "Description: #{ sg . description } "
171- puts "Group ID: #{ sg . group_id } "
172- puts "Owner ID: #{ sg . owner_id } "
173- puts "VPC ID: #{ sg . vpc_id } "
174-
175- if sg . tags . count . positive?
176- puts 'Tags:'
177- sg . tags . each do |tag |
178- puts " Key: #{ tag . key } , Value: #{ tag . value } "
179- end
180- end
181-
182- unless sg . ip_permissions . empty?
183- puts 'Inbound rules:' if sg . ip_permissions . count . positive?
184- sg . ip_permissions . each do |p |
185- describe_security_group_permissions ( p )
186- end
187- end
188-
189- next if sg . ip_permissions_egress . empty?
190-
191- puts 'Outbound rules:' if sg . ip_permissions . count . positive?
192- sg . ip_permissions_egress . each do |p |
193- describe_security_group_permissions ( p )
194- end
131+ display_group_details ( sg )
195132 end
196133 else
197134 puts 'No security groups found.'
@@ -200,22 +137,44 @@ def describe_security_groups(ec2_client)
200137 puts "Error getting information about security groups: #{ e . message } "
201138end
202139
140+ # Helper method to display the details of security groups
141+ def display_group_details ( sg )
142+ puts '-' * ( sg . group_name . length + 13 )
143+ puts "Name: #{ sg . group_name } "
144+ puts "Description: #{ sg . description } "
145+ puts "Group ID: #{ sg . group_id } "
146+ puts "Owner ID: #{ sg . owner_id } "
147+ puts "VPC ID: #{ sg . vpc_id } "
148+
149+ display_group_tags ( sg . tags ) if sg . tags . count . positive?
150+ display_group_permissions ( sg )
151+ end
152+
153+ def display_group_tags ( tags )
154+ puts 'Tags:'
155+ tags . each do |tag |
156+ puts " Key: #{ tag . key } , Value: #{ tag . value } "
157+ end
158+ end
159+
160+ def display_group_permissions ( sg )
161+ if sg . ip_permissions . count . positive?
162+ puts 'Inbound rules:'
163+ sg . ip_permissions . each do |p |
164+ describe_security_group_permissions ( p )
165+ end
166+ end
167+
168+ return if sg . ip_permissions_egress . empty?
169+
170+ puts 'Outbound rules:'
171+ sg . ip_permissions_egress . each do |p |
172+ describe_security_group_permissions ( p )
173+ end
174+ end
175+
203176# Deletes an Amazon Elastic Compute Cloud (Amazon EC2)
204177# security group.
205- #
206- # Prerequisites:
207- #
208- # - The security group.
209- #
210- # @param ec2_client [Aws::EC2::Client] An initialized
211- # Amazon EC2 client.
212- # @param security_group_id [String] The ID of the security group to delete.
213- # @return [Boolean] true if the security group was deleted; otherwise, false.
214- # @example
215- # exit 1 unless security_group_deleted?(
216- # Aws::EC2::Client.new(region: 'us-west-2'),
217- # 'sg-030a858e078f1b9EX'
218- # )
219178def security_group_deleted? ( ec2_client , security_group_id )
220179 ec2_client . delete_security_group ( group_id : security_group_id )
221180 puts "Deleted security group '#{ security_group_id } '."
@@ -225,113 +184,74 @@ def security_group_deleted?(ec2_client, security_group_id)
225184 false
226185end
227186
228- # Example usage:
187+ # Example usage with refactored run_me to reduce complexity
229188def run_me
230- group_name = ''
231- description = ''
232- vpc_id = ''
233- ip_protocol_http = ''
234- from_port_http = ''
235- to_port_http = ''
236- cidr_ip_range_http = ''
237- ip_protocol_ssh = ''
238- from_port_ssh = ''
239- to_port_ssh = ''
240- cidr_ip_range_ssh = ''
241- region = ''
242- # Print usage information and then stop.
189+ group_name , description , vpc_id , ip_protocol_http , from_port_http , to_port_http , \
190+ cidr_ip_range_http , ip_protocol_ssh , from_port_ssh , to_port_ssh , \
191+ cidr_ip_range_ssh , region = process_arguments
192+ ec2_client = Aws ::EC2 ::Client . new ( region : region )
193+
194+ security_group_id = attempt_create_security_group ( ec2_client , group_name , description , vpc_id )
195+ security_group_exists = security_group_id != 'Error'
196+
197+ if security_group_exists
198+ add_inbound_rules ( ec2_client , security_group_id , ip_protocol_http , from_port_http , to_port_http , cidr_ip_range_http )
199+ add_inbound_rules ( ec2_client , security_group_id , ip_protocol_ssh , from_port_ssh , to_port_ssh , cidr_ip_range_ssh )
200+ end
201+
202+ describe_security_groups ( ec2_client )
203+ attempt_delete_security_group ( ec2_client , security_group_id ) if security_group_exists
204+ end
205+
206+ def process_arguments
243207 if ARGV [ 0 ] == '--help' || ARGV [ 0 ] == '-h'
244- puts 'Usage: ruby ec2-ruby-example-security-group.rb ' \
245- 'GROUP_NAME DESCRIPTION VPC_ID IP_PROTOCOL_1 FROM_PORT_1 TO_PORT_1 ' \
246- 'CIDR_IP_RANGE_1 IP_PROTOCOL_2 FROM_PORT_2 TO_PORT_2 ' \
247- 'CIDR_IP_RANGE_2 REGION'
248- puts 'Example: ruby ec2-ruby-example-security-group.rb ' \
249- "my-security-group 'This is my security group.' vpc-6713dfEX " \
250- "tcp 80 80 '0.0.0.0/0' tcp 22 22 '0.0.0.0/0' us-west-2"
208+ display_help
251209 exit 1
252- # If no values are specified at the command prompt, use these default values.
253210 elsif ARGV . count . zero?
254- group_name = 'my-security-group'
255- description = 'This is my security group.'
256- vpc_id = 'vpc-6713dfEX'
257- ip_protocol_http = 'tcp'
258- from_port_http = '80'
259- to_port_http = '80'
260- cidr_ip_range_http = '0.0.0.0/0'
261- ip_protocol_ssh = 'tcp'
262- from_port_ssh = '22'
263- to_port_ssh = '22'
264- cidr_ip_range_ssh = '0.0.0.0/0'
265- # Replace us-west-2 with the AWS Region you're using for Amazon EC2.
266- region = 'us-west-2'
267- # Otherwise, use the values as specified at the command prompt.
211+ default_values
268212 else
269- group_name = ARGV [ 0 ]
270- description = ARGV [ 1 ]
271- vpc_id = ARGV [ 2 ]
272- ip_protocol_http = ARGV [ 3 ]
273- from_port_http = ARGV [ 4 ]
274- to_port_http = ARGV [ 5 ]
275- cidr_ip_range_http = ARGV [ 6 ]
276- ip_protocol_ssh = ARGV [ 7 ]
277- from_port_ssh = ARGV [ 8 ]
278- to_port_ssh = ARGV [ 9 ]
279- cidr_ip_range_ssh = ARGV [ 10 ]
280- region = ARGV [ 11 ]
213+ ARGV
281214 end
282- security_group_exists = false
283- ec2_client = Aws ::EC2 ::Client . new ( region : region )
215+ end
284216
217+ def attempt_create_security_group ( ec2_client , group_name , description , vpc_id )
285218 puts 'Attempting to create security group...'
286- security_group_id = create_security_group (
287- ec2_client ,
288- group_name ,
289- description ,
290- vpc_id
291- )
292- if security_group_id == 'Error'
293- puts 'Could not create security group. Skipping this step.'
294- else
295- security_group_exists = true
296- end
297-
298- if security_group_exists
299- puts 'Attempting to add inbound rules to security group...'
300- unless security_group_ingress_authorized? (
301- ec2_client ,
302- security_group_id ,
303- ip_protocol_http ,
304- from_port_http ,
305- to_port_http ,
306- cidr_ip_range_http
307- )
308- puts 'Could not add inbound HTTP rule to security group. ' \
309- 'Skipping this step.'
310- end
311-
312- unless security_group_ingress_authorized? (
313- ec2_client ,
314- security_group_id ,
315- ip_protocol_ssh ,
316- from_port_ssh ,
317- to_port_ssh ,
318- cidr_ip_range_ssh
319- )
320- puts 'Could not add inbound SSH rule to security group. ' \
321- 'Skipping this step.'
322- end
323- end
219+ security_group_id = create_security_group ( ec2_client , group_name , description , vpc_id )
220+ puts 'Could not create security group. Skipping this step.' if security_group_id == 'Error'
221+ security_group_id
222+ end
324223
325- puts "\n Information about available security groups:"
326- describe_security_groups ( ec2_client )
224+ def add_inbound_rules ( ec2_client , security_group_id , ip_protocol , from_port , to_port , cidr_ip_range )
225+ puts 'Attempting to add inbound rules to security group...'
226+ return if security_group_ingress_authorized? ( ec2_client , security_group_id , ip_protocol , from_port , to_port ,
227+ cidr_ip_range )
327228
328- return unless security_group_exists
229+ puts 'Could not add inbound rule to security group. Skipping this step.'
230+ end
329231
232+ def attempt_delete_security_group ( ec2_client , security_group_id )
330233 puts "\n Attempting to delete security group..."
331234 return if security_group_deleted? ( ec2_client , security_group_id )
332235
333236 puts 'Could not delete security group. You must delete it yourself.'
334237end
335238
239+ def display_help
240+ puts 'Usage: ruby ec2-ruby-example-security-group.rb ' \
241+ 'GROUP_NAME DESCRIPTION VPC_ID IP_PROTOCOL_1 FROM_PORT_1 TO_PORT_1 ' \
242+ 'CIDR_IP_RANGE_1 IP_PROTOCOL_2 FROM_PORT_2 TO_PORT_2 ' \
243+ 'CIDR_IP_RANGE_2 REGION'
244+ puts 'Example: ruby ec2-ruby-example-security-group.rb ' \
245+ "my-security-group 'This is my security group.' vpc-6713dfEX " \
246+ "tcp 80 80 '0.0.0.0/0' tcp 22 22 '0.0.0.0/0' us-west-2"
247+ end
248+
249+ def default_values
250+ [
251+ 'my-security-group' , 'This is my security group.' , 'vpc-6713dfEX' , 'tcp' , '80' , '80' ,
252+ '0.0.0.0/0' , 'tcp' , '22' , '22' , '0.0.0.0/0' , 'us-west-2'
253+ ]
254+ end
255+
336256run_me if $PROGRAM_NAME == __FILE__
337257# snippet-end:[ec2.Ruby.exampleSecurityGroup]
0 commit comments