Skip to content

Commit 557f1a6

Browse files
Pathways-on-Cloud Teamcopybara-github
authored andcommitted
Fix argument and resource type injection in gke_utils.py
- Added strict validation for Kubernetes resource names to prevent injection of flags or alternate resource types. - Updated kubectl commands to use the '--' delimiter to separate flags from positional arguments. - Prefixed pod names with 'pod/' in kubectl commands to be explicit. - Updated unit tests in gke_utils_test.py to cover new validation and command formats. - Fixed broken test in isc_pathways_test.py due to command format change. - Fixed broken test in gke_utils_test.py regarding fetch_cluster_credentials. PiperOrigin-RevId: 918642929
1 parent 0cb05b8 commit 557f1a6

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

  • pathwaysutils/experimental/shared_pathways_service

pathwaysutils/experimental/shared_pathways_service/gke_utils.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""GKE utils for deploying and managing the Pathways proxy."""
22

33
import logging
4+
import re
45
import socket
56
import subprocess
67
import time
@@ -14,20 +15,39 @@
1415
# Python API for kubectl calls.
1516

1617

18+
def _validate_k8s_name(name: str) -> None:
19+
"""Validates that the name is a valid Kubernetes resource name.
20+
21+
Args:
22+
name: The name to validate.
23+
24+
Raises:
25+
ValueError: If the name is invalid.
26+
"""
27+
if not re.match(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", name):
28+
raise ValueError(
29+
f"Invalid Kubernetes resource name: '{name}'. "
30+
"Must consist of lower case alphanumeric characters or '-', and must "
31+
"start and end with an alphanumeric character."
32+
)
33+
34+
1735
def fetch_cluster_credentials(
1836
*, cluster_name: str, project_id: str, location: str
1937
) -> None:
2038
"""Fetches credentials for the GKE cluster."""
39+
_validate_k8s_name(cluster_name)
2140
_logger.info("Fetching credentials for '%s'.", cluster_name)
2241
get_credentials_command = [
2342
"gcloud",
2443
"container",
2544
"clusters",
2645
"get-credentials",
27-
cluster_name,
2846
f"--location={location}",
2947
f"--project={project_id}",
30-
"--dns-endpoint"
48+
"--dns-endpoint",
49+
"--",
50+
cluster_name,
3151
]
3252
try:
3353
subprocess.run(
@@ -87,6 +107,7 @@ def get_pod_from_job(job_name: str) -> str:
87107
RuntimeError: If the pod is missing or the pod name is not in the expected
88108
format.
89109
"""
110+
_validate_k8s_name(job_name)
90111
get_pod_command = [
91112
"kubectl",
92113
"get",
@@ -140,12 +161,14 @@ def check_pod_ready(pod_name: str, timeout: int = 30) -> str:
140161
Raises:
141162
RuntimeError: If the pod fails to become ready within the timeout.
142163
"""
164+
_validate_k8s_name(pod_name)
143165
wait_command = [
144166
"kubectl",
145167
"wait",
146168
"--for=condition=Ready",
147-
f"pod/{pod_name}",
148169
f"--timeout={timeout}s",
170+
"--",
171+
f"pod/{pod_name}",
149172
]
150173
try:
151174
subprocess.run(wait_command, check=True, capture_output=True, text=True)
@@ -245,12 +268,14 @@ def enable_port_forwarding(
245268
server_port,
246269
)
247270

271+
_validate_k8s_name(pod_name)
248272
port_forward_command = [
249273
"kubectl",
250274
"port-forward",
251275
"--address",
252276
"localhost",
253-
pod_name,
277+
"--",
278+
f"pod/{pod_name}",
254279
f"{port_available}:{server_port}",
255280
]
256281
try:
@@ -311,7 +336,8 @@ def stream_pod_logs(pod_name: str) -> subprocess.Popen[str]:
311336
Raises:
312337
Exception: If the log streaming fails.
313338
"""
314-
command = ["kubectl", "logs", "-f", pod_name]
339+
_validate_k8s_name(pod_name)
340+
command = ["kubectl", "logs", "-f", "--", f"pod/{pod_name}"]
315341
try:
316342
return subprocess.Popen(
317343
command,
@@ -331,13 +357,15 @@ def delete_gke_job(job_name: str) -> None:
331357
Args:
332358
job_name: The name of the job.
333359
"""
360+
_validate_k8s_name(job_name)
334361
_logger.info("Deleting job: %s", job_name)
335362
delete_job_command = [
336363
"kubectl",
337364
"delete",
338365
"job",
339-
job_name,
340366
"--ignore-not-found",
367+
"--",
368+
job_name,
341369
]
342370
try:
343371
result = subprocess.run(

0 commit comments

Comments
 (0)