Skip to content

Commit fd4265c

Browse files
committed
Fix Rubocop findings
1 parent 51c8022 commit fd4265c

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

app/messages/validators/security_group_rule_validator.rb

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,26 @@ def validate(record)
2929

3030
add_rule_error("protocol must be 'tcp', 'udp', 'icmp', 'icmpv6' or 'all'", record, index) unless valid_protocol(rule[:protocol])
3131

32-
if rule[:protocol] == 'icmp'
33-
allowed_ip_version = NetAddr::IPv4Net
34-
elsif rule[:protocol] == 'icmpv6'
35-
allowed_ip_version = NetAddr::IPv6Net
36-
else
37-
allowed_ip_version = nil
38-
end
39-
4032
if valid_destination_type(rule[:destination], record, index)
4133
destinations = rule[:destination].split(',', -1)
4234
add_rule_error("maximum destinations per rule exceeded - must be under #{MAX_DESTINATIONS_PER_RULE}", record, index) unless destinations.length <= MAX_DESTINATIONS_PER_RULE
4335

4436
destinations.each do |d|
45-
validate_destination(d, rule[:protocol], allowed_ip_version, record, index)
37+
validate_destination(d, rule[:protocol], get_allowed_ip_version(rule), record, index)
4638
end
4739
end
4840

4941
validate_description(rule, record, index)
5042
validate_log(rule, record, index)
43+
validate_protocol(rule, record, index)
44+
end
45+
end
5146

52-
case rule[:protocol]
53-
when 'tcp', 'udp'
54-
validate_tcp_udp_protocol(rule, record, index)
55-
when 'icmp'
56-
validate_icmp_protocol(rule, record, index)
57-
when 'icmpv6'
58-
add_rule_error("icmpv6 cannot be used if enable_ipv6 is false", record, index) unless CloudController::RuleValidator.ipv6_enabled?
59-
validate_icmp_protocol(rule, record, index)
60-
when 'all'
61-
add_rule_error('ports are not allowed for protocols of type all', record, index) if rule[:ports]
62-
end
47+
def get_allowed_ip_version(rule)
48+
if rule[:protocol] == 'icmp'
49+
NetAddr::IPv4Net
50+
elsif rule[:protocol] == 'icmpv6'
51+
NetAddr::IPv6Net
6352
end
6453
end
6554

@@ -84,6 +73,20 @@ def validate_log(rule, record, index)
8473
add_rule_error('log must be a boolean', record, index) if rule[:log] && !boolean?(rule[:log])
8574
end
8675

76+
def validate_protocol(rule, record, index)
77+
case rule[:protocol]
78+
when 'tcp', 'udp'
79+
validate_tcp_udp_protocol(rule, record, index)
80+
when 'icmp'
81+
validate_icmp_protocol(rule, record, index)
82+
when 'icmpv6'
83+
add_rule_error('icmpv6 cannot be used if enable_ipv6 is false', record, index) unless CloudController::RuleValidator.ipv6_enabled?
84+
validate_icmp_protocol(rule, record, index)
85+
when 'all'
86+
add_rule_error('ports are not allowed for protocols of type all', record, index) if rule[:ports]
87+
end
88+
end
89+
8790
def validate_tcp_udp_protocol(rule, record, index)
8891
add_rule_error('ports are required for protocols of type TCP and UDP', record, index) unless rule[:ports]
8992

@@ -148,11 +151,11 @@ def validate_destination(destination, protocol, allowed_ip_version, record, inde
148151

149152
zeros_error_message = 'destination octets cannot contain leading zeros'
150153
add_rule_error(zeros_error_message, record, index) unless CloudController::RuleValidator.no_leading_zeros(address_list)
151-
152154
if address_list.length == 1
153155
parsed_ip = CloudController::RuleValidator.parse_ip(address_list.first)
154156
add_rule_error(error_message, record, index) unless parsed_ip
155-
add_rule_error("for protocol \"#{protocol}\" you cannot use IPv#{parsed_ip.version} addresses", record, index) unless parsed_ip.nil? || allowed_ip_version.nil? || parsed_ip.is_a?(allowed_ip_version)
157+
add_rule_error("for protocol \"#{protocol}\" you cannot use IPv#{parsed_ip.version} addresses", record, index) \
158+
unless valid_ip_version?(allowed_ip_version, parsed_ip)
156159
elsif address_list.length == 2
157160
ips = CloudController::RuleValidator.parse_ip(address_list)
158161
return add_rule_error('destination IP address range is invalid', record, index) unless ips
@@ -165,13 +168,17 @@ def validate_destination(destination, protocol, allowed_ip_version, record, inde
165168

166169
reversed_range_error = 'beginning of IP address range is numerically greater than the end of its range (range endpoints are inverted)'
167170
add_rule_error(reversed_range_error, record, index) unless ips.first == sorted_ips.first
168-
add_rule_error("for protocol \"#{protocol}\" you cannot use IPv#{ips.first.version} addresses", record, index) unless ips.first.nil? || allowed_ip_version.nil? || ips.first.is_a?(allowed_ip_version)
169-
171+
add_rule_error("for protocol \"#{protocol}\" you cannot use IPv#{ips.first.version} addresses", record, index) \
172+
unless valid_ip_version?(allowed_ip_version, ips.first)
170173
else
171174
add_rule_error(error_message, record, index)
172175
end
173176
end
174177

178+
def valid_ip_version?(allowed_ip_version, parsed_ip)
179+
parsed_ip.nil? || allowed_ip_version.nil? || parsed_ip.is_a?(allowed_ip_version)
180+
end
181+
175182
def add_rule_error(message, record, index)
176183
record.errors.add("Rules[#{index}]:", message)
177184
end

spec/unit/messages/validators/security_group_rule_validator_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,6 @@ def self.name
15031503
expect(subject.errors.full_messages).to include 'Rules[0]: code is required for protocols of type ICMP'
15041504
end
15051505
end
1506-
15071506
end
15081507
end
15091508
end

0 commit comments

Comments
 (0)