Skip to content

Commit 9fc97a4

Browse files
Check the ports passed on an ACL update before the stored ones
On update, take the source ports from the command first and fall back to the rule's stored ports only when none was passed and the protocol is given. This reads in the order the values apply and does not depend on transferDataToNetworkAclRulePojo() having already copied the command's ports onto the rule. Behaviour is unchanged: that copy runs first, so the rule already held the command's ports when they were passed. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent 252038f commit 9fc97a4

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎server/src/main/java/com/cloud/network/vpc/NetworkACLServiceImpl.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,19 @@ protected void validatePortsAreUsableWithProtocol(String protocol, Integer sourc
708708
}
709709

710710
/**
711-
* On update the ports a rule already carries are only checked when the protocol is given too, so
712-
* that a rule stored with ports on another protocol number can still be edited otherwise, while a
713-
* protocol change cannot leave them on it.
711+
* On update the ports passed in the command are checked first. The ports a rule already carries are
712+
* only used for any not passed, and only when the protocol is given too, so that a rule stored with
713+
* ports on another protocol number can still be edited otherwise, while a protocol change cannot
714+
* leave them on it.
714715
*/
715716
protected void validatePortsAreUsableWithProtocolOnUpdate(UpdateNetworkACLItemCmd updateNetworkACLItemCmd, NetworkACLItemVO networkACLItemVo) {
716-
boolean protocolGiven = StringUtils.isNotBlank(updateNetworkACLItemCmd.getProtocol());
717-
validatePortsAreUsableWithProtocol(networkACLItemVo.getProtocol(),
718-
protocolGiven ? networkACLItemVo.getSourcePortStart() : updateNetworkACLItemCmd.getSourcePortStart(),
719-
protocolGiven ? networkACLItemVo.getSourcePortEnd() : updateNetworkACLItemCmd.getSourcePortEnd());
717+
Integer sourcePortStart = updateNetworkACLItemCmd.getSourcePortStart();
718+
Integer sourcePortEnd = updateNetworkACLItemCmd.getSourcePortEnd();
719+
if (StringUtils.isNotBlank(updateNetworkACLItemCmd.getProtocol())) {
720+
sourcePortStart = ObjectUtils.defaultIfNull(sourcePortStart, networkACLItemVo.getSourcePortStart());
721+
sourcePortEnd = ObjectUtils.defaultIfNull(sourcePortEnd, networkACLItemVo.getSourcePortEnd());
722+
}
723+
validatePortsAreUsableWithProtocol(networkACLItemVo.getProtocol(), sourcePortStart, sourcePortEnd);
720724
}
721725

722726
@Override

‎server/src/test/java/com/cloud/network/vpc/NetworkACLServiceImplTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,16 @@ public void validatePortsAreUsableWithProtocolOnUpdateTestProtocolChangedOntoSto
584584
networkAclServiceImpl.validatePortsAreUsableWithProtocolOnUpdate(updateNetworkACLItemCmdMock, networkAclItemVoMock);
585585
}
586586

587+
@Test(expected = InvalidParameterValueException.class)
588+
public void validatePortsAreUsableWithProtocolOnUpdateTestPortsPassedWithProtocol() {
589+
Mockito.when(updateNetworkACLItemCmdMock.getProtocol()).thenReturn("47");
590+
Mockito.when(updateNetworkACLItemCmdMock.getSourcePortStart()).thenReturn(22);
591+
Mockito.when(updateNetworkACLItemCmdMock.getSourcePortEnd()).thenReturn(22);
592+
Mockito.when(networkAclItemVoMock.getProtocol()).thenReturn("47");
593+
594+
networkAclServiceImpl.validatePortsAreUsableWithProtocolOnUpdate(updateNetworkACLItemCmdMock, networkAclItemVoMock);
595+
}
596+
587597
@Test
588598
public void validateSourceCidrListTestEmptySourceCirdList() {
589599
Mockito.when(networkAclItemVoMock.getSourceCidrList()).thenReturn(new ArrayList<>());

0 commit comments

Comments
 (0)