Skip to content

Commit 1dbe8e8

Browse files
committed
[Test] In DCV integ tests, set Dcv/AllowedIps to the test host IP to prevent public access to DCV on the head node.
1 parent b0f1094 commit 1dbe8e8

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

tests/integration-tests/tests/dcv/test_dcv.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
from assertpy import assert_that
1919
from framework.credential_providers import run_pcluster_command
2020
from remote_command_executor import RemoteCommandExecutionError, RemoteCommandExecutor
21-
from utils import add_keys_to_known_hosts, check_node_security_group, get_username_for_os, remove_keys_from_known_hosts
21+
from utils import (
22+
add_keys_to_known_hosts,
23+
check_node_security_group,
24+
get_cidr_from_ip,
25+
get_local_ip,
26+
get_username_for_os,
27+
remove_keys_from_known_hosts,
28+
)
2229

2330
from tests.cloudwatch_logging.test_cloudwatch_logging import FeatureSpecificCloudWatchLoggingTestRunner
2431

@@ -27,17 +34,32 @@
2734

2835

2936
def test_dcv_configuration(region, instance, os, scheduler, pcluster_config_reader, clusters_factory, test_datadir):
37+
host_ip = get_local_ip()
38+
dcv_allowed_ips = get_cidr_from_ip(host_ip) if host_ip else "0.0.0.0/0"
3039
_test_dcv_configuration(
31-
8443, "0.0.0.0/0", region, instance, os, scheduler, pcluster_config_reader, clusters_factory, test_datadir
40+
8443, dcv_allowed_ips, region, instance, os, scheduler, pcluster_config_reader, clusters_factory, test_datadir
3241
)
3342

3443

35-
@pytest.mark.parametrize("dcv_port, access_from", [(8443, "0.0.0.0/0"), (5678, "192.168.1.1/32")])
44+
@pytest.mark.parametrize("dcv_port, access_from", [(8443, "PLACEHOLDER_TEST_HOST_CIDR"), (5678, "192.168.1.1/32")])
3645
def test_dcv_with_remote_access(
3746
dcv_port, access_from, region, instance, os, scheduler, pcluster_config_reader, clusters_factory, test_datadir
3847
):
48+
if access_from == "PLACEHOLDER_TEST_HOST_CIDR":
49+
host_ip = get_local_ip()
50+
dcv_allowed_ips = get_cidr_from_ip(host_ip) if host_ip else "0.0.0.0/0"
51+
else:
52+
dcv_allowed_ips = access_from
3953
_test_dcv_configuration(
40-
dcv_port, access_from, region, instance, os, scheduler, pcluster_config_reader, clusters_factory, test_datadir
54+
dcv_port,
55+
dcv_allowed_ips,
56+
region,
57+
instance,
58+
os,
59+
scheduler,
60+
pcluster_config_reader,
61+
clusters_factory,
62+
test_datadir,
4163
)
4264

4365

tests/integration-tests/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,3 +907,37 @@ def find_stack_by_tag(tag, region, stack_prefix):
907907
logging.info(f"Found stack: {name} (created on {creation_date})")
908908
return name
909909
return None
910+
911+
912+
def get_local_ip():
913+
"""
914+
Attempts to retrieve the local IP address of the machine.
915+
916+
This function uses the socket library to get the hostname and then resolve
917+
it to an IP address. This typically returns the primary local IP address
918+
of the machine.
919+
920+
Returns:
921+
str: The local IP address if successfully retrieved.
922+
None: If the IP address could not be determined.
923+
"""
924+
try:
925+
hostname = socket.gethostname()
926+
return socket.gethostbyname(hostname)
927+
except Exception as e:
928+
logging.error(f"Cannot determine local IP: {e}")
929+
return None
930+
931+
932+
def get_cidr_from_ip(ip):
933+
"""
934+
Converts an IP address to CIDR notation with /32 suffix.
935+
936+
Args:
937+
ip (str): The IP address to convert.
938+
939+
Returns:
940+
str: IP address in CIDR notation (e.g., "192.168.1.1/32") if IP is provided,
941+
otherwise returns None
942+
"""
943+
return f"{ip}/32" if ip else None

0 commit comments

Comments
 (0)