Module: Grouper
- Defined in:
- lib/grouper.rb
Defined Under Namespace
Classes: Rule
Instance Method Summary collapse
-
#add_rule(group, rule) ⇒ Object
add a rule to a security group.
-
#apply_rules(group, rules) ⇒ Object
Takes an array of rules and applies them to a security froup if the security group has rules that are not part of the rules array being applied these are revoked.
-
#find_or_create(ec2, group_name) ⇒ Object
find a security group, create it if it does not exist.
-
#is_rule?(permission, rules) ⇒ Boolean
checks to see if an EC2 IP permission is in array of rules.
-
#match?(permission, rule) ⇒ Boolean
checks to see if an EC2 IP permission matches a rule AWS doesn’t do clever recombination of rules in the background so we do simple comparaisons to keep things simples.
-
#remove_old_rules(group, rules) ⇒ Object
revoke old rules that are not part of the rules array.
-
#revoke_rule(group, rule) ⇒ Object
remove rule from a security group.
Instance Method Details
#add_rule(group, rule) ⇒ Object
add a rule to a security group
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/grouper.rb', line 58 def add_rule(group, rule) begin case rule.direction when :in group.(rule.protocol, rule.ports, *rule.sources) when :out group.(*rule.sources, :protocol => rule.protocol, :ports => rule.ports) else group.(rule.protocol, rule.ports, *rule.sources) group.(*rule.sources, :protocol => rule.protocol, :ports => rule.ports) end rescue AWS::EC2::Errors::InvalidPermission::Duplicate end end |
#apply_rules(group, rules) ⇒ Object
Takes an array of rules and applies them to a security froup if the security group has rules that are not part of the rules array being applied these are revoked
16 17 18 19 20 21 |
# File 'lib/grouper.rb', line 16 def apply_rules(group, rules) remove_old_rules(group, rules) rules.each do |rule| add_rule(group, rule) end end |
#find_or_create(ec2, group_name) ⇒ Object
find a security group, create it if it does not exist
5 6 7 8 9 10 11 |
# File 'lib/grouper.rb', line 5 def find_or_create(ec2, group_name) if ec2.security_groups.map(&:name).include?(group_name) ec2.security_groups.filter('group-name', group_name).first else ec2.security_groups.create(group_name) end end |
#is_rule?(permission, rules) ⇒ Boolean
checks to see if an EC2 IP permission is in array of rules
36 37 38 39 40 41 |
# File 'lib/grouper.rb', line 36 def is_rule?(, rules) rules.each do |rule| return true if match?(, rule) end false end |
#match?(permission, rule) ⇒ Boolean
checks to see if an EC2 IP permission matches a rule AWS doesn’t do clever recombination of rules in the background so we do simple comparaisons to keep things simples
46 47 48 49 50 51 52 53 54 |
# File 'lib/grouper.rb', line 46 def match?(, rule) if rule.direction == :in (.port_range == rule.ports) and (.ip_ranges == rule.sources) and (.protocol == rule.protocol) and (!.egress) elsif rule.direction == :out (.port_range == rule.ports) and (.ip_ranges == rule.sources) and (.protocol == rule.protocol) and (.egress) else #rule.direction == :both (.port_range == rule.ports) and (.ip_ranges == rule.sources) and (.protocol == rule.protocol) end end |
#remove_old_rules(group, rules) ⇒ Object
revoke old rules that are not part of the rules array
25 26 27 28 29 30 31 32 |
# File 'lib/grouper.rb', line 25 def remove_old_rules(group, rules) group..each do |p| p.revoke if !is_rule?(p, rules) end group..each do |p| p.revoke if !is_rule?(p, rules) end end |
#revoke_rule(group, rule) ⇒ Object
remove rule from a security group
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/grouper.rb', line 76 def revoke_rule(group, rule) case rule.direction when :in group.revoke_ingress(rule.protocol, rule.ports, *rule.sources) when :out group.revoke_egress(*rule.sources, :protocol => rule.protocol, :ports => rule.ports) else group.revoke_ingress(rule.protocol, rule.ports, *rule.sources) group.revoke_egress(*rule.sources, :protocol => rule.protocol, :ports => rule.ports) end end |