Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 157 additions & 86 deletions test/integration/component/test_security_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
VirtualMachine,
SecurityGroup,
Router,
Host)
Host,
Network)
from marvin.lib.common import (get_domain,
get_zone,
get_template,
Expand Down Expand Up @@ -528,6 +529,12 @@ def tearDownClass(cls):

return

def revokeSGRule(self, sgid):
cmd = revokeSecurityGroupIngress.revokeSecurityGroupIngressCmd()
cmd.id = sgid
self.apiclient.revokeSecurityGroupIngress(cmd)
return

@attr(tags=["sg", "eip", "advancedsg"])
def test_01_revokeIngressRule(self):
"""Test revoke ingress rule
Expand Down Expand Up @@ -605,19 +612,30 @@ def test_01_revokeIngressRule(self):

self.debug("Revoking ingress rule for sec group ID: %s for ssh access"
% security_group.id)
# Revoke Security group to SSH to VM
security_group.revoke(
security_groups = SecurityGroup.list(
self.apiclient,
id=ssh_rule["ruleid"]
account=self.account.name,
domainid=self.account.domainid,
listall=True
)
self.assertEqual(
validateList(security_groups)[0],
PASS,
"Security groups list validation failed"
)
for sg in security_groups:
if not sg.ingressrule:
continue
self.revokeSGRule(sg.ingressrule[0].ruleid)

# SSH Attempt to VM should fail
with self.assertRaises(Exception):
self.debug("SSH into VM: %s" % self.virtual_machine.id)
SshClient(self.virtual_machine.ssh_ip,
self.virtual_machine.ssh_port,
self.virtual_machine.username,
self.virtual_machine.password
self.virtual_machine.password,
retries=5
)
return

Expand Down Expand Up @@ -1490,32 +1508,45 @@ def test_02_revokeIngressRule_AfterDeployVM(self):
security_group.id,
self.account.name
))
result = security_group.revoke(
#skip revoke and ping tests in case of EIP/ELB zone
vm_res = VirtualMachine.list(
self.apiclient,
id=icmp_rule["ruleid"]
id=self.virtual_machine.id
)
self.debug("Revoke ingress rule result: %s" % result)
time.sleep(self.testdata["sleep"])
# User should not be able to ping VM
try:
self.debug("Trying to ping VM %s" % self.virtual_machine.ssh_ip)
if platform_type == 'windows':
result = subprocess.call(
['ping', '-n', '1', self.virtual_machine.ssh_ip])
else:
result = subprocess.call(
['ping', '-c 1', self.virtual_machine.ssh_ip])

self.debug("Ping result: %s" % result)
# if ping successful, then result should be 0
self.assertNotEqual(
result,
0,
"Check if ping is successful or not"
self.assertEqual(validateList(vm_res)[0], PASS, "invalid vm response")
vm_nw = Network.list(
self.apiclient,
id=vm_res[0].nic[0].networkid
)
self.assertEqual(validateList(vm_nw)[0], PASS, "invalid network response")
vm_nw_off = vm_nw[0].networkofferingname
if vm_nw_off != "DefaultSharedNetscalerEIPandELBNetworkOffering":
result = security_group.revoke(
self.apiclient,
id=icmp_rule["ruleid"]
)
except Exception as e:
self.fail("Ping failed for ingress rule ID: %s, %s"
% (icmp_rule["ruleid"], e))
self.debug("Revoke ingress rule result: %s" % result)
time.sleep(self.testdata["sleep"])
# User should not be able to ping VM
try:
self.debug("Trying to ping VM %s" % self.virtual_machine.ssh_ip)
if platform_type == 'windows':
result = subprocess.call(
['ping', '-n', '1', self.virtual_machine.ssh_ip])
else:
result = subprocess.call(
['ping', '-c 1', self.virtual_machine.ssh_ip])

self.debug("Ping result: %s" % result)
# if ping successful, then result should be 0
self.assertNotEqual(
result,
0,
"Check if ping is successful or not"
)
except Exception as e:
self.fail("Ping failed for ingress rule ID: %s, %s"
% (icmp_rule["ruleid"], e))
return

@attr(tags=["sg", "eip", "advancedsg"])
Expand Down Expand Up @@ -1754,6 +1785,12 @@ def setHostConfiguration(self):
self.debug(response)
return

def revokeSGRule(self, sgid):
cmd = revokeSecurityGroupIngress.revokeSecurityGroupIngressCmd()
cmd.id = sgid
self.apiclient.revokeSecurityGroupIngress(cmd)
return

@attr(tags=["sg", "eip", "advancedsg"])
def test_ingress_rules_specific_IP_set(self):
"""Test ingress rules for specific IP set
Expand Down Expand Up @@ -1803,18 +1840,20 @@ def test_ingress_rules_specific_IP_set(self):
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id]
securitygroupids=[defaultSecurityGroup.id],
mode=self.testdata['mode']
)

self.cleanup.append(virtual_machine_1)
virtual_machine_2 = VirtualMachine.create(
self.apiclient,
self.testdata["virtual_machine"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id]
securitygroupids=[defaultSecurityGroup.id],
mode=self.testdata['mode']
)

self.cleanup.append(virtual_machine_2)
try:
SshClient(
virtual_machine_1.ssh_ip,
Expand All @@ -1823,9 +1862,10 @@ def test_ingress_rules_specific_IP_set(self):
virtual_machine_1.password
)
except Exception as e:
self.revokeSGRule(ingress_rule.ingressrule[0].ruleid)
self.fail("SSH Access failed for %s: %s" %
(virtual_machine_1.ipaddress, e)
)
)

try:
SshClient(
Expand All @@ -1835,42 +1875,66 @@ def test_ingress_rules_specific_IP_set(self):
virtual_machine_2.password
)
except Exception as e:
self.revokeSGRule(ingress_rule.ingressrule[0].ruleid)
self.fail("SSH Access failed for %s: %s" %
(virtual_machine_2.ipaddress, e)
)

sshClient = SshClient(
self.mgtSvrDetails["mgtSvrIp"],
22,
self.mgtSvrDetails["user"],
self.mgtSvrDetails["passwd"]
)

)
try:
sshClient = SshClient(
self.mgtSvrDetails["mgtSvrIp"],
22,
self.mgtSvrDetails["user"],
self.mgtSvrDetails["passwd"]
)
except Exception as e:
self.revokeSGRule(ingress_rule.ingressrule[0].ruleid)
self.fail("SSH Access failed for %s: %s" %
(self.mgtSvrDetails["mgtSvrIp"], e)
)
response = sshClient.execute("ssh %s@%s -v" %
(virtual_machine_1.username,
virtual_machine_1.ssh_ip))
(virtual_machine_1.username,
virtual_machine_1.ssh_ip))
self.debug("Response is :%s" % response)

self.assertTrue("connection established" in str(response).lower(),
"SSH to VM at %s failed from external machine ip %s other than test machine" %
(virtual_machine_1.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))
"SSH to VM at %s failed from external machine ip %s other than test machine" %
(virtual_machine_1.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))

response = sshClient.execute("ssh %s@%s -v" %
(virtual_machine_2.username,
virtual_machine_2.ssh_ip))
(virtual_machine_2.username,
virtual_machine_2.ssh_ip))
self.debug("Response is :%s" % response)

self.assertTrue("connection established" in str(response).lower(),
"SSH to VM at %s failed from external machine ip %s other than test machine" %
(virtual_machine_2.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))


cmd = revokeSecurityGroupIngress.revokeSecurityGroupIngressCmd()
cmd.id = ingress_rule.ingressrule[0].ruleid
self.apiclient.revokeSecurityGroupIngress(cmd)

"SSH to VM at %s failed from external machine ip %s other than test machine" %
(virtual_machine_2.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))
virtual_machine_3 = VirtualMachine.create(
self.apiclient,
self.testdata["virtual_machine"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id],
mode=self.testdata['mode']
)
self.cleanup.append(virtual_machine_3)
security_groups = SecurityGroup.list(
self.apiclient,
account=self.account.name,
domainid=self.account.domainid,
listall=True
)
self.assertEqual(
validateList(security_groups)[0],
PASS,
"Security groups list validation failed"
)
for sg in security_groups:
if not sg.ingressrule:
continue
self.revokeSGRule(sg.ingressrule[0].ruleid)
localMachineIpAddress = self.getLocalMachineIpAddress()
cidr = localMachineIpAddress + "/32"

Expand All @@ -1882,16 +1946,6 @@ def test_ingress_rules_specific_IP_set(self):
cmd.endport = 22
cmd.cidrlist = cidr
ingress_rule = self.apiclient.authorizeSecurityGroupIngress(cmd)

virtual_machine_3 = VirtualMachine.create(
self.apiclient,
self.testdata["virtual_machine"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id]
)

if self.testdata["configurableData"]["setHostConfigurationForIngressRule"]:
self.setHostConfiguration()
time.sleep(180)
Expand All @@ -1905,28 +1959,28 @@ def test_ingress_rules_specific_IP_set(self):
virtual_machine_3.ssh_port,
virtual_machine_3.username,
virtual_machine_3.password
)
)
except Exception as e:
self.fail("SSH Access failed for %s: %s" %
(virtual_machine_3.ssh_ip, e)
)
)

sshClient = SshClient(
self.mgtSvrDetails["mgtSvrIp"],
22,
self.mgtSvrDetails["user"],
self.mgtSvrDetails["passwd"]
self.mgtSvrDetails["mgtSvrIp"],
22,
self.mgtSvrDetails["user"],
self.mgtSvrDetails["passwd"]
)

response = sshClient.execute("ssh %s@%s -v" %
(virtual_machine_3.username,
virtual_machine_3.ssh_ip))
(virtual_machine_3.username,
virtual_machine_3.ssh_ip))
self.debug("Response is :%s" % response)

self.assertFalse("connection established" in str(response).lower(),
"SSH to VM at %s succeeded from external machine ip %s other than test machine" %
(virtual_machine_3.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))
"SSH to VM at %s succeeded from external machine ip %s other than test machine" %
(virtual_machine_3.ssh_ip,
self.mgtSvrDetails["mgtSvrIp"]))
return

@attr(tags=["sg", "eip", "advancedsg"])
Expand Down Expand Up @@ -1978,18 +2032,20 @@ def test_ingress_rules_specific_IP_set_non_def_sec_group(self):
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id]
securitygroupids=[defaultSecurityGroup.id],
mode=self.testdata['mode']
)

self.cleanup.append(virtual_machine_1)
virtual_machine_2 = VirtualMachine.create(
self.apiclient,
self.testdata["virtual_machine"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[defaultSecurityGroup.id]
securitygroupids=[defaultSecurityGroup.id],
mode=self.testdata['mode']
)

self.cleanup.append(virtual_machine_2)
try:
SshClient(
virtual_machine_1.ssh_ip,
Expand Down Expand Up @@ -2066,9 +2122,10 @@ def test_ingress_rules_specific_IP_set_non_def_sec_group(self):
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
securitygroupids=[security_group.id]
securitygroupids=[security_group.id],
mode=self.testdata['mode']
)

self.cleanup.append(virtual_machine_3)
if self.testdata["configurableData"]["setHostConfigurationForIngressRule"]:
self.setHostConfiguration()
time.sleep(180)
Expand All @@ -2087,7 +2144,21 @@ def test_ingress_rules_specific_IP_set_non_def_sec_group(self):
self.fail("SSH Access failed for %s: %s" %
(virtual_machine_3.ssh_ip, e)
)

security_groups = SecurityGroup.list(
self.apiclient,
account=self.account.name,
domainid=self.account.domainid,
listall=True
)
self.assertEqual(
validateList(security_groups)[0],
PASS,
"Security groups list validation failed"
)
for sg in security_groups:
if sg.id == security_group.id or not sg.ingressrule:
continue
self.revokeSGRule(sg.ingressrule[0].ruleid)
sshClient = SshClient(
self.mgtSvrDetails["mgtSvrIp"],
22,
Expand Down