File: network_acl.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (178 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module Fog
  module AWS
    class Compute
      class NetworkAcl < Fog::Model
        ICMP = 1
        TCP  = 6
        UDP  = 17

        identity  :network_acl_id, :aliases => 'networkAclId'
        attribute :vpc_id,         :aliases => 'vpcId'
        attribute :default
        attribute :entries,        :aliases => 'entrySet'
        attribute :associations,   :aliases => 'associationSet'
        attribute :tags,           :aliases => 'tagSet'

        # Add an inbound rule, shortcut method for #add_rule
        def add_inbound_rule(rule_number, protocol, rule_action, cidr_block, options = {})
          add_rule(rule_number, protocol, rule_action, cidr_block, false, options)
        end

        # Add an outbound rule, shortcut method for #add_rule
        def add_outbound_rule(rule_number, protocol, rule_action, cidr_block, options = {})
          add_rule(rule_number, protocol, rule_action, cidr_block, true, options)
        end

        # Add a new rule
        #
        # network_acl.add_rule(100, Fog::AWS::Compute::NetworkAcl::TCP, 'allow', '0.0.0.0/0', true, 'PortRange.From' => 22, 'PortRange.To' => 22)
        #
        # ==== Parameters
        # * rule_number<~Integer>   - The rule number for the entry, between 100 and 32766
        # * protocol<~Integer>      - The IP protocol to which the rule applies. You can use -1 to mean all protocols.
        # * rule_action<~String>    - Allows or denies traffic that matches the rule. (either allow or deny)
        # * cidr_block<~String>     - The CIDR range to allow or deny
        # * egress<~Boolean>        - Indicates whether this rule applies to egress traffic from the subnet (true) or ingress traffic to the subnet (false).
        # * options<~Hash>:
        # *   'Icmp.Code'           - ICMP code, required if protocol is 1
        # *   'Icmp.Type'           - ICMP type, required if protocol is 1
        # *   'PortRange.From'      - The first port in the range, required if protocol is 6 (TCP) or 17 (UDP)
        # *   'PortRange.To'        - The last port in the range, required if protocol is 6 (TCP) or 17 (UDP)
        #
        # ==== Returns
        #
        # True or false depending on the result
        #
        def add_rule(rule_number, protocol, rule_action, cidr_block, egress, options = {})
          requires :network_acl_id

          service.create_network_acl_entry(network_acl_id, rule_number, protocol, rule_action, cidr_block, egress, options)
          true
        end

        # Remove an inbound rule, shortcut method for #remove_rule
        def remove_inbound_rule(rule_number)
          remove_rule(rule_number, false)
        end

        # Remove an outbound rule, shortcut method for #remove_rule
        def remove_outbound_rule(rule_number)
          remove_rule(rule_number, true)
        end

        # Update a specific rule number
        #
        # network_acl.remove_rule(100, true)
        #
        # ==== Parameters
        # * rule_number<~Integer>   - The rule number for the entry, between 100 and 32766
        # * egress<~Boolean>        - Indicates whether this rule applies to egress traffic from the subnet (true) or ingress traffic to the subnet (false).
        #
        # ==== Returns
        #
        # True or false depending on the result
        #
        def remove_rule(rule_number, egress)
          requires :network_acl_id

          service.delete_network_acl_entry(network_acl_id, rule_number, egress)
          true
        end

        # Update an inbound rule, shortcut method for #update_rule
        def update_inbound_rule(rule_number, protocol, rule_action, cidr_block, options = {})
          update_rule(rule_number, protocol, rule_action, cidr_block, false, options)
        end

        # Update an outbound rule, shortcut method for #update_rule
        def update_outbound_rule(rule_number, protocol, rule_action, cidr_block, options = {})
          update_rule(rule_number, protocol, rule_action, cidr_block, true, options)
        end

        # Update a specific rule number
        #
        # network_acl.update_rule(100, Fog::AWS::Compute::NetworkAcl::TCP, 'allow', '0.0.0.0/0', true, 'PortRange.From' => 22, 'PortRange.To' => 22)
        #
        # ==== Parameters
        # * rule_number<~Integer>   - The rule number for the entry, between 100 and 32766
        # * protocol<~Integer>      - The IP protocol to which the rule applies. You can use -1 to mean all protocols.
        # * rule_action<~String>    - Allows or denies traffic that matches the rule. (either allow or deny)
        # * cidr_block<~String>     - The CIDR range to allow or deny
        # * egress<~Boolean>        - Indicates whether this rule applies to egress traffic from the subnet (true) or ingress traffic to the subnet (false).
        # * options<~Hash>:
        # *   'Icmp.Code'           - ICMP code, required if protocol is 1
        # *   'Icmp.Type'           - ICMP type, required if protocol is 1
        # *   'PortRange.From'      - The first port in the range, required if protocol is 6 (TCP) or 17 (UDP)
        # *   'PortRange.To'        - The last port in the range, required if protocol is 6 (TCP) or 17 (UDP)
        #
        # ==== Returns
        #
        # True or false depending on the result
        #
        def update_rule(rule_number, protocol, rule_action, cidr_block, egress, options = {})
          requires :network_acl_id

          service.replace_network_acl_entry(network_acl_id, rule_number, protocol, rule_action, cidr_block, egress, options)
          true
        end

        # Associate a subnet with this network ACL
        #
        # network_acl.associate_with(subnet)
        #
        # ==== Parameters
        # * subnet<~Subnet> - Subnet object to associate with this network ACL
        #
        # ==== Returns
        #
        # True or false depending on the result
        #
        def associate_with(subnet)
          requires :network_acl_id

          # We have to manually find out the network ACL the subnet is currently associated with
          old_id = service.network_acls.all('association.subnet-id' => subnet.subnet_id).first.associations.find { |a| a['subnetId'] == subnet.subnet_id }['networkAclAssociationId']
          service.replace_network_acl_association(old_id, network_acl_id)
          true
        end

        # Removes an existing network ACL
        #
        # network_acl.destroy
        #
        # ==== Returns
        #
        # True or false depending on the result
        #
        def destroy
          requires :network_acl_id

          service.delete_network_acl(network_acl_id)
          true
        end

        # Create a network ACL
        #
        #  >> g = AWS.network_acls.new(:vpc_id => 'vpc-abcdefgh')
        #  >> g.save
        def save
          requires :vpc_id
          data = service.create_network_acl(vpc_id).body['networkAcl']
          new_attributes = data.reject { |key,value| key == 'tagSet' }
          merge_attributes(new_attributes)

          if tags = self.tags
            # expect eventual consistency
            Fog.wait_for { self.reload rescue nil }
            service.create_tags(
              self.identity,
              tags
            )
          end

          true
        end
      end
    end
  end
end