Skip to content

Commit 4250ec7

Browse files
authored
Better error messaging for ASGs with empty destinatinos in comma-delimited lists (#3783)
1 parent 9fc7afa commit 4250ec7

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

app/messages/validators/security_group_rule_validator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def validate(record)
3030
add_rule_error("protocol must be 'tcp', 'udp', 'icmp', or 'all'", record, index) unless valid_protocol(rule[:protocol])
3131

3232
if valid_destination_type(rule[:destination], record, index)
33-
rules = rule[:destination].split(',')
33+
rules = rule[:destination].split(',', -1)
3434
add_rule_error("maximum destinations per rule exceeded - must be under #{MAX_DESTINATIONS_PER_RULE}", record, index) unless rules.length <= MAX_DESTINATIONS_PER_RULE
3535

3636
rules.each do |d|
@@ -131,6 +131,7 @@ def valid_destination_type(destination, record, index)
131131
def validate_destination(destination, record, index)
132132
error_message = 'destination must be a valid CIDR, IP address, or IP address range'
133133
error_message = 'destination must contain valid CIDR(s), IP address(es), or IP address range(s)' if CloudController::RuleValidator.comma_delimited_destinations_enabled?
134+
add_rule_error('empty destination specified in comma-delimited list', record, index) if destination.empty?
134135

135136
address_list = destination.split('-')
136137

spec/unit/messages/validators/security_group_rule_validator_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,84 @@ def self.name
404404
expect(subject.errors.full_messages).to include 'Rules[0]: maximum destinations per rule exceeded - must be under 6000'
405405
end
406406
end
407+
408+
context 'empty destinations in the front are invalid' do
409+
let(:rules) do
410+
[
411+
{
412+
protocol: 'udp',
413+
destination: ',10.10.10.10,11.11.11.11',
414+
ports: '8080'
415+
}
416+
]
417+
end
418+
419+
it 'throws an error for the missing destination' do
420+
expect(subject).not_to be_valid
421+
expect(subject.errors.full_messages.length).to equal(2)
422+
expect(subject.errors.full_messages).to include 'Rules[0]: destination must contain valid CIDR(s), IP address(es), or IP address range(s)'
423+
expect(subject.errors.full_messages).to include 'Rules[0]: empty destination specified in comma-delimited list'
424+
end
425+
end
426+
427+
context 'empty destinations in the middle are invalid' do
428+
let(:rules) do
429+
[
430+
{
431+
protocol: 'udp',
432+
destination: '10.10.10.10,,11.11.11.11',
433+
ports: '8080'
434+
}
435+
]
436+
end
437+
438+
it 'throws an error for the missing destination' do
439+
expect(subject).not_to be_valid
440+
expect(subject.errors.full_messages.length).to equal(2)
441+
expect(subject.errors.full_messages).to include 'Rules[0]: destination must contain valid CIDR(s), IP address(es), or IP address range(s)'
442+
expect(subject.errors.full_messages).to include 'Rules[0]: empty destination specified in comma-delimited list'
443+
end
444+
end
445+
446+
context 'empty destinations at the end are invalid' do
447+
let(:rules) do
448+
[
449+
{
450+
protocol: 'udp',
451+
destination: '10.10.10.10,11.11.11.11,',
452+
ports: '8080'
453+
}
454+
]
455+
end
456+
457+
it 'throws an error for the missing destination' do
458+
expect(subject).not_to be_valid
459+
expect(subject.errors.full_messages.length).to equal(2)
460+
expect(subject.errors.full_messages).to include 'Rules[0]: destination must contain valid CIDR(s), IP address(es), or IP address range(s)'
461+
expect(subject.errors.full_messages).to include 'Rules[0]: empty destination specified in comma-delimited list'
462+
end
463+
end
464+
465+
context 'multiple empty destinations are invalid' do
466+
let(:rules) do
467+
[
468+
{
469+
protocol: 'udp',
470+
destination: ',10.10.10.10,,11.11.11.11,',
471+
ports: '8080'
472+
}
473+
]
474+
end
475+
476+
it 'throws an error for each missing destination' do
477+
expect(subject).not_to be_valid
478+
expect(subject.errors.full_messages.length).to equal(6)
479+
expect(subject.errors.full_messages).to include 'Rules[0]: destination must contain valid CIDR(s), IP address(es), or IP address range(s)'
480+
expect(subject.errors.full_messages[0]).to eq 'Rules[0]: empty destination specified in comma-delimited list'
481+
expect(subject.errors.full_messages[2]).to eq 'Rules[0]: empty destination specified in comma-delimited list'
482+
expect(subject.errors.full_messages[4]).to eq 'Rules[0]: empty destination specified in comma-delimited list'
483+
end
484+
end
407485
end
408486
end
409487

0 commit comments

Comments
 (0)