-
Notifications
You must be signed in to change notification settings - Fork 1.4k
server: scope IPv6 security group member rules to the exact host #14037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nagaboinaramgopal
wants to merge
2
commits into
apache:4.20
Choose a base branch
from
nagaboinaramgopal:fix/secgroup-ipv6-host-cidr
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
server/src/test/java/com/cloud/network/security/SecurityGroupManagerImpl2Ipv6RuleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.network.security; | ||
|
|
||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import com.cloud.network.security.SecurityGroupManagerImpl.PortAndProto; | ||
| import com.cloud.network.security.SecurityRule.SecurityRuleType; | ||
| import com.cloud.network.security.dao.SecurityGroupRuleDao; | ||
| import com.cloud.network.security.dao.SecurityGroupVMMapDao; | ||
| import com.cloud.vm.VirtualMachine.State; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.Silent.class) | ||
| public class SecurityGroupManagerImpl2Ipv6RuleTest { | ||
|
|
||
| @Mock | ||
| SecurityGroupRuleDao _securityGroupRuleDao; | ||
| @Mock | ||
| SecurityGroupVMMapDao _securityGroupVMMapDao; | ||
|
|
||
| @InjectMocks | ||
| SecurityGroupManagerImpl2 manager = new SecurityGroupManagerImpl2(); | ||
|
|
||
| @Test | ||
| public void securityGroupMemberRuleUsesExactIpv6HostCidr() { | ||
| Long vmId = 1L; | ||
| SecurityRuleType type = SecurityRuleType.IngressRule; | ||
|
|
||
| // The VM belongs to security group 10, which has one rule referencing another security group (20). | ||
| SecurityGroupVMMapVO groupMap = Mockito.mock(SecurityGroupVMMapVO.class); | ||
| when(groupMap.getSecurityGroupId()).thenReturn(10L); | ||
| when(_securityGroupVMMapDao.listByInstanceId(vmId)).thenReturn(Collections.singletonList(groupMap)); | ||
|
|
||
| SecurityGroupRuleVO rule = Mockito.mock(SecurityGroupRuleVO.class); | ||
| when(rule.getProtocol()).thenReturn("tcp"); | ||
| when(rule.getStartPort()).thenReturn(80); | ||
| when(rule.getEndPort()).thenReturn(80); | ||
| when(rule.getAllowedNetworkId()).thenReturn(20L); | ||
| when(_securityGroupRuleDao.listBySecurityGroupId(10L, type)).thenReturn(Collections.singletonList(rule)); | ||
|
|
||
| // Unlike the superclass, Impl2 reads the member addresses straight from the VO join, not the nics table. | ||
| SecurityGroupVMMapVO memberMap = Mockito.mock(SecurityGroupVMMapVO.class); | ||
| when(memberMap.getGuestIpAddress()).thenReturn("10.1.1.5"); | ||
| when(memberMap.getGuestIpv6Address()).thenReturn("2001:db8::5"); | ||
| when(_securityGroupVMMapDao.listBySecurityGroup(20L, State.Running)).thenReturn(Collections.singletonList(memberMap)); | ||
|
|
||
| Map<PortAndProto, Set<String>> allowed = manager.generateRulesForVM(vmId, type); | ||
|
|
||
| Assert.assertEquals(1, allowed.size()); | ||
| Set<String> cidrs = allowed.values().iterator().next(); | ||
| // The member must be authorized as an exact host, matching the IPv4 /32 behaviour. | ||
| Assert.assertTrue("IPv4 member should be pinned to /32", cidrs.contains("10.1.1.5/32")); | ||
| Assert.assertTrue("IPv6 member should be pinned to the exact /128 host", cidrs.contains("2001:db8::5/128")); | ||
| Assert.assertFalse("IPv6 member must not open the whole /64 subnet", cidrs.contains("2001:db8::5/64")); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
server/src/test/java/com/cloud/network/security/SecurityGroupManagerImplIpv6RuleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.network.security; | ||
|
|
||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import com.cloud.network.NetworkModel; | ||
| import com.cloud.network.security.SecurityGroupManagerImpl.PortAndProto; | ||
| import com.cloud.network.security.SecurityRule.SecurityRuleType; | ||
| import com.cloud.network.security.dao.SecurityGroupRuleDao; | ||
| import com.cloud.network.security.dao.SecurityGroupVMMapDao; | ||
| import com.cloud.vm.Nic; | ||
| import com.cloud.vm.VirtualMachine.State; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.Silent.class) | ||
| public class SecurityGroupManagerImplIpv6RuleTest { | ||
|
|
||
| @Mock | ||
| SecurityGroupRuleDao _securityGroupRuleDao; | ||
| @Mock | ||
| SecurityGroupVMMapDao _securityGroupVMMapDao; | ||
| @Mock | ||
| NetworkModel _networkModel; | ||
|
|
||
| @InjectMocks | ||
| SecurityGroupManagerImpl manager = new SecurityGroupManagerImpl(); | ||
|
|
||
| @Test | ||
| public void securityGroupMemberRuleUsesExactIpv6HostCidr() { | ||
| Long vmId = 1L; | ||
| SecurityRuleType type = SecurityRuleType.IngressRule; | ||
|
|
||
| // The VM belongs to security group 10, which has one rule referencing another security group (20). | ||
| SecurityGroupVMMapVO groupMap = Mockito.mock(SecurityGroupVMMapVO.class); | ||
| when(groupMap.getSecurityGroupId()).thenReturn(10L); | ||
| when(_securityGroupVMMapDao.listByInstanceId(vmId)).thenReturn(Collections.singletonList(groupMap)); | ||
|
|
||
| SecurityGroupRuleVO rule = Mockito.mock(SecurityGroupRuleVO.class); | ||
| when(rule.getProtocol()).thenReturn("tcp"); | ||
| when(rule.getStartPort()).thenReturn(80); | ||
| when(rule.getEndPort()).thenReturn(80); | ||
| when(rule.getAllowedNetworkId()).thenReturn(20L); | ||
| when(_securityGroupRuleDao.listBySecurityGroupId(10L, type)).thenReturn(Collections.singletonList(rule)); | ||
|
|
||
| // Group 20 has one running member VM with both an IPv4 and an IPv6 address. | ||
| SecurityGroupVMMapVO memberMap = Mockito.mock(SecurityGroupVMMapVO.class); | ||
| when(memberMap.getInstanceId()).thenReturn(2L); | ||
| when(_securityGroupVMMapDao.listBySecurityGroup(20L, State.Running)).thenReturn(Collections.singletonList(memberMap)); | ||
|
|
||
| Nic nic = Mockito.mock(Nic.class); | ||
| when(nic.getIPv4Address()).thenReturn("10.1.1.5"); | ||
| when(nic.getIPv6Address()).thenReturn("2001:db8::5"); | ||
| when(_networkModel.getDefaultNic(2L)).thenReturn(nic); | ||
|
|
||
| Map<PortAndProto, Set<String>> allowed = manager.generateRulesForVM(vmId, type); | ||
|
|
||
| Assert.assertEquals(1, allowed.size()); | ||
| Set<String> cidrs = allowed.values().iterator().next(); | ||
| // The member must be authorized as an exact host, matching the IPv4 /32 behaviour. | ||
| Assert.assertTrue("IPv4 member should be pinned to /32", cidrs.contains("10.1.1.5/32")); | ||
| Assert.assertTrue("IPv6 member should be pinned to the exact /128 host", cidrs.contains("2001:db8::5/128")); | ||
| Assert.assertFalse("IPv6 member must not open the whole /64 subnet", cidrs.contains("2001:db8::5/64")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens to vms that pick their own random ipv6 address, like windows does by default? looks like their traffic wont match this single address anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for raising this, it's a good thing to pin down. I went and looked at the agent side to be sure, and I think we might actually be covered here, though let me know if I'm missing a case.
From what I can see, a VM can't really put a self-chosen address on the wire in an SG network anyway. The KVM agent installs a source anti-spoof rule in
security_group.py(the-m set ! --match-set <vmipset6> src -j DROPnear the end of the IPv6 default chain), and that ipset only gets the addresses CloudStack assigned to the NIC. So a Windows temporary/privacy address wouldn't be in the set, and its traffic would get dropped at that VM's own vNIC before the receiving VM's member rule is ever looked at. If that reading is right, then/64wasn't really letting those packets through either, they just died earlier in the path.Where
/64did make a difference, I think, is that it also let any other VM sharing that/64reach the protected VM, member or not, and narrowing to/128lines it up with what the IPv4 side already does at/32. Does that match how you understand it, or is there a setup where a VM ends up sending from an address CloudStack didn't assign?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, the agent drops anything from an address it didnt assign so /64 wasnt helping there. thanks for checking